@fibus-digital/sluby v0.4.4
comment: <> (TODO: faire un tuto à part ?)
Sluby
A node.js-typescript framework to build REST APIs, around Fastify and Prisma
comment: <> (TODO: auto-documented)
Why ?
This opinionated project is conceived to hold all the usual technicals issues with a REST Api, with a "convention over configuration" philosophy, to unify projects.
Getting started
npm install @fibus-digital/sluby
To launch the project :
sluby dev
Build and run for production :
sluby build
sluby start
Prisma
(See prisma documentation)
npx prisma init
This command create a "prisma" folder at the root, and a "schema.prisma" file. Update the schema with your models, and the ".env" file with your DB connection information.
For exemple, a client can have multiples transactions :
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Client {
id Int @id @default(autoincrement())
email String @unique
nom String?
prenom String?
transactions Transaction[]
deletedAt DateTime?
}
model Transaction {
id Int @id @default(autoincrement())
libelle String?
client Client @relation(fields: [clientId], references: [id])
clientId Int
@@index([clientId])
}
Then, to generate the prisma client and sync the database run :
npx prisma migrate dev
Config files
In order to create CRUD endpoints, each model should have 4 files. In our exemple :
- client.conf.ts
- client.create.schema.ts
- client.output.schema.ts
- client.update.schema.ts
The minimal configuration must export a default object :
// client.conf.ts
import { PrismaClient } from '@prisma/client'
import {AutorouteConfig} from "@fibus-digital/sluby"
const clientConf: AutorouteConfig<PrismaClient> = {
model: 'client',
endPoint: '/v1/clients',
}
export default clientConf;
Each schema file defined the structure of inputs and outputs, respecting the (awesome) AJV’s JSONSchemaType :
// client.create.schema.ts
import {Client} from "@prisma/client";
import {JSONSchemaType} from "@fibus-digital/sluby"
export interface ClientCreateInterface extends Pick<Client, 'email' | 'nom' | 'prenom'> {}
const clientInputSchema: JSONSchemaType<ClientCreateInterface> = {
type: 'object',
$id: 'clientCreate',
properties: {
email: { type: 'string', format: 'email'},
nom: { type: 'string', nullable: true },
prenom: { type: 'string', nullable: true },
},
required: ['email'],
additionalProperties: false,
}
export default clientInputSchema;
// client.output.schema.ts
import {Client} from "@prisma/client";
import {JSONSchemaType} from "@fibus-digital/sluby"
export interface UserOutputInterface extends Pick<Client, 'id' | 'email' | 'nom' | 'prenom'> {}
const clientOutputSchema: JSONSchemaType<UserOutputInterface> = {
type: 'object',
$id: 'clientOutput',
properties: {
id: { type: 'number' },
email: { type: 'string', format: 'email'},
nom: { type: 'string' },
prenom: { type: 'string' },
},
required: ['id'],
additionalProperties: false,
}
export default clientOutputSchema;
// client.update.schema.ts
import {Client} from "@prisma/client";
import {JSONSchemaType} from "@fibus-digital/sluby"
export interface ClientUpdateInterface extends Pick<Client, 'email' | 'nom' | 'prenom'> {}
const clientUpdateSchema: JSONSchemaType<ClientUpdateInterface> = {
type: 'object',
$id: 'clientUpdate',
properties: {
email: { type: 'string', format: 'email'},
nom: { type: 'string', nullable: true },
prenom: { type: 'string', nullable: true },
},
required: [],
additionalProperties: false,
}
export default clientUpdateSchema;
IMPORTANT
Each schema MUST have a $id property, with the model's name and the purpose :
- clientCreate
- clientUpdate
- clientOutput
This configuration create the following endpoints :
Route | Description |
---|---|
GET api/v1/clients | Clients list (paginated) |
GET api/v1/clients/:id | Get one client from id |
POST api/v1/clients | Create one client |
POST api/v1/clients/:id | Modifier un client |
DELETE api/v1/clients/:id | Supprimer un client |
Includes
Soft-delete
CreatedAt, updatedAt, createdBy
Prisma Events
Security (user provider)
Services (DI)
Commands
Testing (ts-jest & code-coverage)
1 month ago
2 months ago
2 months ago
2 months ago
3 months ago
4 months ago
4 months ago
4 months ago
5 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
8 months ago
8 months ago
7 months ago
7 months ago
9 months ago
10 months ago
8 months ago
8 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
7 months ago
5 months ago
7 months ago
7 months ago
7 months ago
7 months ago
11 months ago
11 months ago
11 months ago
11 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
1 year ago
1 year ago
12 months ago
12 months ago
11 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago