npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fibus-digital/sluby

v0.4.4

Published

A node.js-typescript framework to build REST APIs, around Fastify and Prisma

Downloads

158

Readme

Sluby

A node.js-typescript framework to build REST APIs, around Fastify and Prisma

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)