0.4.4 • Published 1 month ago

@fibus-digital/sluby v0.4.4

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
1 month ago

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 :

RouteDescription
GET api/v1/clientsClients list (paginated)
GET api/v1/clients/:idGet one client from id
POST api/v1/clientsCreate one client
POST api/v1/clients/:idModifier un client
DELETE api/v1/clients/:idSupprimer un client

Includes

Soft-delete

CreatedAt, updatedAt, createdBy

Prisma Events

Security (user provider)

Services (DI)

Commands

Testing (ts-jest & code-coverage)

0.4.4

1 month ago

0.4.3

2 months ago

0.4.2

2 months ago

0.4.1

2 months ago

0.4.0

3 months ago

0.3.18

4 months ago

0.3.17

4 months ago

0.3.16

4 months ago

0.3.15

5 months ago

0.4.0-beta.14

6 months ago

0.4.0-beta.13

6 months ago

0.4.0-beta.12

6 months ago

0.4.0-beta.11

6 months ago

0.4.0-beta.10

6 months ago

0.3.6

8 months ago

0.3.5

8 months ago

0.3.8

7 months ago

0.3.7

7 months ago

0.3.2

9 months ago

0.3.1

10 months ago

0.3.4

8 months ago

0.3.3

8 months ago

0.4.0-beta.9

6 months ago

0.4.0-beta.1

6 months ago

0.4.0-beta.2

6 months ago

0.4.0-beta.3

6 months ago

0.4.0-beta.4

6 months ago

0.4.0-beta.5

6 months ago

0.4.0-beta.6

6 months ago

0.4.0-beta.7

6 months ago

0.4.0-beta.8

6 months ago

0.3.6-beta.1

8 months ago

0.3.6-beta.0

8 months ago

0.3.6-beta.4

8 months ago

0.3.6-beta.3

8 months ago

0.3.6-beta.2

8 months ago

0.3.9

7 months ago

0.3.14

5 months ago

0.3.13

7 months ago

0.3.12

7 months ago

0.3.11

7 months ago

0.3.10

7 months ago

0.3.0

11 months ago

0.3.0-beta.18

11 months ago

0.3.0-beta.17

11 months ago

0.3.0-beta.19

11 months ago

0.3.0-beta.8

12 months ago

0.3.0-beta.9

12 months ago

0.3.0-beta.0

12 months ago

0.3.0-beta.1

12 months ago

0.3.0-beta.6

12 months ago

0.3.0-beta.7

12 months ago

0.3.0-beta.4

12 months ago

0.3.0-beta.5

12 months ago

0.3.0-beta.14

12 months ago

0.3.0-beta.13

12 months ago

0.3.0-beta.12

12 months ago

0.3.0-beta.11

12 months ago

0.3.0-beta.16

12 months ago

0.3.0-beta.15

12 months ago

0.3.0-beta.10

12 months ago

0.2.1

1 year ago

0.2.0

1 year ago

0.2.7

12 months ago

0.2.6

12 months ago

0.2.8

11 months ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.1.38

1 year ago

0.1.33

1 year ago

0.1.34

1 year ago

0.1.35

1 year ago

0.1.36

1 year ago

0.1.37

1 year ago

0.1.10

1 year ago

0.1.11

1 year ago

0.1.12

1 year ago

0.1.13

1 year ago

0.1.14

1 year ago

0.1.15

1 year ago

0.1.0

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.8

1 year ago

0.0.26

1 year ago

0.1.7

1 year ago

0.1.9

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.30

1 year ago

0.1.31

1 year ago

0.1.32

1 year ago

0.0.20

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.24

2 years ago

0.0.25

1 year ago

0.1.27

1 year ago

0.0.15

2 years ago

0.1.28

1 year ago

0.0.16

2 years ago

0.1.29

1 year ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.1.20

1 year ago

0.1.21

1 year ago

0.1.22

1 year ago

0.0.10

2 years ago

0.1.23

1 year ago

0.0.11

2 years ago

0.1.24

1 year ago

0.0.12

2 years ago

0.1.25

1 year ago

0.0.13

2 years ago

0.1.26

1 year ago

0.0.14

2 years ago

0.1.16

1 year ago

0.0.9

2 years ago

0.1.17

1 year ago

0.0.8

2 years ago

0.1.18

1 year ago

0.1.19

1 year ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago