fastify-mongo-crud
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) => {
fastify.register(mongodb, { url: opts.mongoUri })
fastify.register(crud)
next()
})
Usage within a services/accounts.js
file:
export default (fastify, opts, next) => {
const accounts = fastify.crud('accounts')
fastify.post('/accounts', async (req) => {
return { account: await accounts.create(req.body) }
})
fastify.get('/accounts/:id', async (req) => {
return { account: await accounts.read(req.params.id) }
})
fastify.put('/accounts/:id', async (req) => {
return { account: await accounts.update(req.params.id, req.body) }
})
fastify.delete('/accounts/:id', async (req) => {
return { account: await accounts.delete(req.params.id) }
})
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
v0.3.0
v0.2.0
- added implicit timestamps for record "created" & "modified"
v0.1.0
v0.0.0
License
Licensed under MIT.
Published, Supported and Sponsored by u|screen