6.0.0 • Published 8 months ago

@inaiat/fastify-papr v6.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

fastify-papr

Statements Branches Functions Lines

A fastify Papr plugin integration.

Getting started

yarn add @inaiat/fastify-papr @fastify/mongodb

Next, set up the plugin:

import fastifyMongodb from '@fastify/mongodb'
import fastifyPaprPlugin, { asCollection, FastifyPaprOptions } from ' @inaiat/fastify-papr'
import fp from 'fastify-plugin'
import { Model, schema, types } from 'papr'

const userSchema = schema({
  name: types.string({ required: true, minLength: 10, maxLength: 100 }),
  phone: types.string({ required: true, minLength: 8, maxLength: 20 }),
})

const userIndexes = [{ key: { name: 1 } }]

declare module 'fastify' {
  interface PaprModels {
    user: Model<typeof userSchema[0], Partial<typeof userSchema[1]>>
  }
}

export default fp<FastifyPaprOptions>(
  async (fastify) => {
    await fastify.register(fastifyMongodb, {
      url: 'mongodb://localhost:27017',
    })

    await fastify.register(fastifyPaprPlugin, {
      db: fastify.mongo.client.db('test'),
      models: { user: asCollection('user', userSchema, userIndexes) },
    })
  },
  { name: 'papr' },
)

How to use:

import { FastifyPluginAsync } from 'fastify'
import { Static, Type } from '@sinclair/typebox'

const userDto = Type.Object({
  name: Type.String({ maxLength: 100, minLength: 10 }),
  phone: Type.String({ maxLength: 20, minLength: 8 }),
})

const userRoute: FastifyPluginAsync = async (fastify) => {
  fastify.post<{ readonly Body: Static<typeof userDto> }>(
    '/user',
    {
      schema: {
        body: userDto,
      },
    },
    async (req) => {
      const result = await fastify.papr.user.insertOne(req.body)
      return result
    },
  )
}

export default userRoute

Papr Documentation

Read the documentation at: plexinc.github.io/papr

5.0.0

9 months ago

6.0.0

8 months ago

4.1.0

1 year ago

4.0.1

1 year ago

4.0.0

1 year ago

3.0.0

1 year ago

1.0.1

2 years ago

1.0.0

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago