koa2-mongoose-crud

1.1.4 • Public • Published

koa2-mongoose-crud

Koa 2 CRUD middleware for Mongoose models.

NPM Build Status JavaScript Style Guide

Installation

npm install --save koa2-mongoose-crud

This module uses async await and therefore requires node >= 8.

Usage

const crud = require('koa2-mongoose-crud')
const mongoose = require('mongoose')
 
// setup minimal koa application
const Koa = require('koa')
const app = new Koa()
 
// setup basic mongoose model
const fooSchema = mongoose.Schema({
  name: String
})
const Foo = mongoose.model('Foo', fooSchema)
 
{ // setup basic CRUD REST API for Foo model
  const Router = require('koa-router')
  const router = new Router()
 
  const model = Foo
  const idParamName = 'foo'
 
  router.get('/foo/:foo', crud.read({ model, idParamName }))
  router.post('/foo', crud.create({ model, idParamName }))
  router.put('/foo/:foo', crud.update({ model, idParamName }))
  router.del('/foo/:foo', crud.delete({ model, idParamName }))
 
  app.use(router.middleware())
}
 
app.listen(3000)

Note that any model used with koa2-mongoose-crud must implement model.getSafePaths(label, ctx) and model.getPublicDocument(doc, safePaths) for basic filtering of readable / writable properties on different operations where label can be something like create, read, update, or delete.

API

CRUD

  • crud.create = ({ model, label = 'create', acceptsPopulate, defaultPopulate = [], acl } = {})
  • crud.read = ({ model, idParamName, label = 'read', acceptsPopulate, defaultPopulate = [], acl } = {})
  • crud.update = ({ model, idParamName, label = 'update', acceptsPopulate, defaultPopulate = [], acl } = {})
  • crud.delete = ({ model, idParamName, label = 'archive', acl } = {})

CRUD++

  • crud.upsert = ({ model, idParamName, updateLabel = 'update', createLabel = 'create', acceptsPopulate, defaultPopulate = [], acl } = {})
  • crud.index = ({ model, acceptsFilters, canPaginate, defaultFilters, label = 'index', acceptsPopulate, defaultPopulate = [], acl } = {})
  • crud.count = ({ model, acceptsFilters, acl } = {})
  • crud.archive = ({ model, idParamName, label = 'archive', acl } = {})
  • crud.deleteByQuery = ({ model, acceptsFilters, acl } = {})

ACLs

You may pass an acl parameter (short for Access Control List) to any of the crud functions which has the signature async function (ctx: Koa.Context, doc: mongoose.Document): Promise.

For all the methods aside from create, acl will be passed a mongoose document which matched the crud operation, and you may perform access control to verify, for example, that the currently authenticated user is authorized to perform the crud operation on that document.

For create, you can use this to overwrite the document before it's created in Mongo. This is useful, for example, to ensure that the currently authenticated user is stored on a new doocument as it's created for future authorization control.

License

MIT © Travis Fischer

Package Sidebar

Install

npm i koa2-mongoose-crud

Weekly Downloads

1

Version

1.1.4

License

MIT

Unpacked Size

29.6 kB

Total Files

18

Last publish

Collaborators

  • fisch0920