Skip to content

paranoiasystem/fastify-autocrud

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fastify-autocrud

build status issues Coverage Status license version js-standard-style Beerpay

Plugin for autogenerate CRUD routes as fast as possible.

Install

npm i fastify-autocrud --save

Usage

fastify.register(require('fastify-autocrud'), {
    prefix: '/api/products',
    Collection: require('../models/Product')
})

Product.js

const mongoose = require('mongoose')

const Product = new mongoose.Schema({
  name: String,
  description: String,
  image: String,
  cost: Number,
  price: Number,
  qty: Number
}, {
  timestamps: true
})

module.exports = mongoose.model('Product', Product)

If you want add custom routes

const customController = require('customController')

fastify.register(require('fastify-autocrud'), {
    prefix: '/api/products',
    Collection: require('../models/Product'),
    additionalRoute: customController
})

customController.js

const boom = require('boom')

const customRoute = {
    method: 'GET',
    url: '/custom',
    handler: async function (req, reply) {
        try {
            reply.type('application/json').code(200).send({ "customroute": "ok" })
        } catch (err) {
            throw boom.boomify(err)
        }
    }
}

module.exports = [customRoute]

License

Licensed under MIT.

About

Plugin for autogenerate CRUD routes as fast as possible

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published