0.4.33 • Published 8 months ago

@fibus-digital/sluby v0.4.33

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
8 months 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.32-beta

10 months ago

0.4.30

10 months ago

0.4.33

8 months ago

0.4.30-beta1

10 months ago

0.4.30-beta2

10 months ago

0.4.20

12 months ago

0.4.21

12 months ago

0.4.28

11 months ago

0.4.29

10 months ago

0.4.26

11 months ago

0.4.27

11 months ago

0.4.24

11 months ago

0.4.25

11 months ago

0.4.22

12 months ago

0.4.23

12 months ago

0.4.19

1 year ago

0.4.32-beta5

8 months ago

0.4.32-beta4

8 months ago

0.4.32-beta3

9 months ago

0.4.32-beta2

9 months ago

0.4.17

1 year ago

0.4.18

1 year ago

0.4.15

1 year ago

0.4.31-beta

10 months ago

0.4.16

1 year ago

0.4.14

1 year ago

0.4.32-beta6

8 months ago

0.4.22-beta-3

12 months ago

0.4.22-beta-2

12 months ago

0.4.22-beta

12 months ago

0.4.13

1 year ago

0.4.12

1 year ago

0.4.10

1 year ago

0.4.11

1 year ago

0.4.9

1 year ago

0.4.8

1 year ago

0.4.5

2 years ago

0.4.7

2 years ago

0.4.6

2 years ago

0.4.4

2 years ago

0.4.3

2 years ago

0.4.2

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.18

2 years ago

0.3.17

2 years ago

0.3.16

2 years ago

0.3.15

2 years ago

0.4.0-beta.14

2 years ago

0.4.0-beta.13

2 years ago

0.4.0-beta.12

2 years ago

0.4.0-beta.11

2 years ago

0.4.0-beta.10

2 years ago

0.3.6

2 years ago

0.3.5

2 years ago

0.3.8

2 years ago

0.3.7

2 years ago

0.3.2

2 years ago

0.3.1

3 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.4.0-beta.9

2 years ago

0.4.0-beta.1

2 years ago

0.4.0-beta.2

2 years ago

0.4.0-beta.3

2 years ago

0.4.0-beta.4

2 years ago

0.4.0-beta.5

2 years ago

0.4.0-beta.6

2 years ago

0.4.0-beta.7

2 years ago

0.4.0-beta.8

2 years ago

0.3.6-beta.1

2 years ago

0.3.6-beta.0

2 years ago

0.3.6-beta.4

2 years ago

0.3.6-beta.3

2 years ago

0.3.6-beta.2

2 years ago

0.3.9

2 years ago

0.3.14

2 years ago

0.3.13

2 years ago

0.3.12

2 years ago

0.3.11

2 years ago

0.3.10

2 years ago

0.3.0

3 years ago

0.3.0-beta.18

3 years ago

0.3.0-beta.17

3 years ago

0.3.0-beta.19

3 years ago

0.3.0-beta.8

3 years ago

0.3.0-beta.9

3 years ago

0.3.0-beta.0

3 years ago

0.3.0-beta.1

3 years ago

0.3.0-beta.6

3 years ago

0.3.0-beta.7

3 years ago

0.3.0-beta.4

3 years ago

0.3.0-beta.5

3 years ago

0.3.0-beta.14

3 years ago

0.3.0-beta.13

3 years ago

0.3.0-beta.12

3 years ago

0.3.0-beta.11

3 years ago

0.3.0-beta.16

3 years ago

0.3.0-beta.15

3 years ago

0.3.0-beta.10

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.8

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.1.38

3 years ago

0.1.33

3 years ago

0.1.34

3 years ago

0.1.35

3 years ago

0.1.36

3 years ago

0.1.37

3 years ago

0.1.10

3 years ago

0.1.11

3 years ago

0.1.12

3 years ago

0.1.13

3 years ago

0.1.14

3 years ago

0.1.15

3 years ago

0.1.0

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.8

3 years ago

0.0.26

3 years ago

0.1.7

3 years ago

0.1.9

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.30

3 years ago

0.1.31

3 years ago

0.1.32

3 years ago

0.0.20

3 years ago

0.0.21

3 years ago

0.0.22

3 years ago

0.0.23

3 years ago

0.0.24

3 years ago

0.0.25

3 years ago

0.1.27

3 years ago

0.0.15

3 years ago

0.1.28

3 years ago

0.0.16

3 years ago

0.1.29

3 years ago

0.0.17

3 years ago

0.0.18

3 years ago

0.0.19

3 years ago

0.1.20

3 years ago

0.1.21

3 years ago

0.1.22

3 years ago

0.0.10

3 years ago

0.1.23

3 years ago

0.0.11

3 years ago

0.1.24

3 years ago

0.0.12

3 years ago

0.1.25

3 years ago

0.0.13

3 years ago

0.1.26

3 years ago

0.0.14

3 years ago

0.1.16

3 years ago

0.0.9

3 years ago

0.1.17

3 years ago

0.0.8

3 years ago

0.1.18

3 years ago

0.1.19

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago