General setup for entities
Explanation on how to setup entities for automagic and use in Primate
Entities are concepts inside your project that will have at least:
Storage in database
Enpoints to read, write, update or delete
In this manner, to have an entity we need a group of things described in the following article.
Database Model
For Primate, our ORM is Prisma. In this manner, the database model should exist inside the schema.prisma.
Routes
For the entity to be consumed we need at least one route. In primate, there is a default way to give your entity CrUD capabilities. This can be achieved doing the following:
Create an
/entititesfolder in the root of your projectCreate a folder with the plural name of your entity, for example:
/entities/usersCreate a file called
/entities/[plural/][plural].jsfor your routes, for example:/entities/users/users.jsFinally, add your routes inside the file in the following way:
import { auth, Primate } from '@thewebchimp/primate';
const router = Primate.getRouter();
router.get('/foo', (req, res) => {
res.respond({
data: 'bar'
});
});
export { router };This will give the following response when typing http://localhost:1337/[plural]/foo:
{
"result": "success",
"status": 200,
"data": "bar",
"message": ""
}Automagic CrUD routes
Part of the automagic of Primate is that it gives automatically a way to Create, Update and Delete entities. To setup the automagic CrUD, you need the following code in your route file:
import { auth, Primate } from '@thewebchimp/primate';
const router = Primate.getRouter();
Primate.setupRoute('[singular]', router);
export { router };This will give you automatically the following routes:
GET /[plural]for listing all elementsGET /[plural]/:idfor geting one elementPOST /[plural]for creating one elementPUT /[plural]/:idfor updating one elementDELETE /[plural]/:idfor deleting one elementGET /[plural]/:id/metasfor getting the metas of one element
Services
Last updated