Skip to main content

Example: Users API

This example builds on the default Users module that ships with new Flowra projects.

1. Run the migration

The scaffold includes a migration for the users table.

flowra db:migrate:latest

2. Update routes

Switch the Users routes to a more RESTful shape:

app/Modules/Users/users.routes.js
'use strict';
const group = require('routergroup');

module.exports = ({ router, container } = {}) => {
if (!router || !container) {
return router;
}

const controller = container.resolve('modules.users.controllers.main');

router.use(
group('/users', (router) => {
router.get('/', controller.list.bind(controller));
router.post('/', controller.create.bind(controller));
router.put('/:id', controller.update.bind(controller));
router.delete('/:id', controller.destroy.bind(controller));
})
);

return router;
};

3. Add update and delete logic

Extend the controller and service with update and destroy methods.

app/Modules/Users/Users.controller.js
update = async (req, res, next) => {
try {
const result = await this.usersService.update(req.params.id, req.body);
res.json({ message: 'Updated', data: result });
} catch (error) {
next(error);
}
};

destroy = async (req, res, next) => {
try {
await this.usersService.destroy(req.params.id);
res.status(204).end();
} catch (error) {
next(error);
}
};
app/Modules/Users/Users.service.js
async update(id, payload) {
return this.userModel.update(id, payload);
}

async destroy(id) {
return this.userModel.delete(id);
}

4. Run and test

npm run dev

Now you can call:

  • GET /users
  • POST /users
  • PUT /users/:id
  • DELETE /users/:id
Next steps

Try adding validation rules to update() or introduce a query class for reporting.