1.0.0 • Published 7 months ago

@uscreen.de/fastify-mongo-crud v1.0.0

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

fastify-mongo-crud

Test CI Test Coverage Known Vulnerabilities NPM Version

Tiny mongodb decorator providing CRUD-style DB-methods

Install

$ yarn add @uscreen.de/fastify-mongo-crud

Adding @uscreen.de/fastify-mongo-crud also adds fastify-mongodb as direct dependency.

Note: You still need to register mongodb prior to crud as seen in example below. This is due to encapsulation of fastify plugins assuming you will want to resuse same fastify.mongo accross your application.

Example

Setup within a plugins/mongo.js file:

import fp from 'fastify-plugin'
import mongodb from '@fastify/mongodb'
import crud from '@uscreen.de/fastify-mongo-crud'

export default fp((fastify, opts, next) => {
  /**
   * 1) setup mongodb connection
   */
  fastify.register(mongodb, { url: opts.mongoUri })

  /**
   * 2) setup CRUD factory
   */
  fastify.register(crud)

  /**
   * 3) proceed
   */
  next()
})

Usage within a services/accounts.js file:

/**
 * let's write a classic CRUD service
 */
export default (fastify, opts, next) => {
  const accounts = fastify.crud('accounts')

  /**
   * plug in some appropiate authentication
   * middleware - of course!
   *
   * plus: add schemas to routes.
   */

  /**
   * create
   */
  fastify.post('/accounts', async (req) => {
    return { account: await accounts.create(req.body) }
  })

  /**
   * read
   */
  fastify.get('/accounts/:id', async (req) => {
    return { account: await accounts.read(req.params.id) }
  })

  /**
   * update
   */
  fastify.put('/accounts/:id', async (req) => {
    return { account: await accounts.update(req.params.id, req.body) }
  })

  /**
   * delete
   */
  fastify.delete('/accounts/:id', async (req) => {
    return { account: await accounts.delete(req.params.id) }
  })

  /**
   * list
   */
  fastify.get('/accounts', async () => {
    return { accounts: await accounts.list() }
  })

  next()
}

Options

Api


Roadmap

  • embed and configure fastify-mongodb and expose fastify.mongo from within this module, if not installed in parent

Changelog

v1.0.0

Changed

  • switch to ESM only
  • upgrade to fastify@4.x

v0.3.0

  • upgraded to fastify 3.x

v0.2.0

  • added implicit timestamps for record "created" & "modified"

v0.1.0

  • added tests

v0.0.0

  • init

License

Licensed under MIT.

Published, Supported and Sponsored by u|screen

1.0.0

7 months ago

1.0.0-2

1 year ago

1.0.0-1

1 year ago

0.4.13

1 year ago

0.4.12

2 years ago

0.4.11

2 years ago

0.4.10

2 years ago

0.4.9

2 years ago

0.4.8

2 years ago

0.4.7

2 years ago

0.4.6

2 years ago

0.4.5

2 years ago

0.4.4

2 years ago

0.4.3

2 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.2

4 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.0

5 years ago