koa-sequelize-rest

    0.0.2 • Public • Published

    koa-sequelize-rest

    Generates REST middlewares for Sequelize models.

    Examples

    Simple setup

    let koa = require('koa');
    let Rest = require('koa-sequelize-rest');
     
    let app = koa();
     
    let Model = require('./model');
    let ModelRest = new Rest(Model);
     
    app.use(ModelRest.readAll());

    Returns

    {
      "model": [
        { "value": "A" },
        { "value": "B" },
        { "value": "C" }
      ]
    }

    Router example

    koa-sequelize-rest is especially handy when used alongside koa-router :

    let router = koaRouter();
     
    router.get('/', ModelRest.readAll());
    router.post('/', ModelRest.create());
    router.get('/:uuid', ModelRest.readOne());
    router.put('/:uuid', ModelRest.update());
    router.delete('/:uuid', ModelRest.delete());
     
    app.use(router.routes());
    app.use(router.allowedMethods());

    API

    new Rest(model)

    Instanciate a new koa-sequelize-rest helper instance.

    Signature

    • (param) model, a Sequelize model
    • (returns) rest, a Rest instance tied to model

    rest.getEntity(includes)

    Loads a model instance in Koa's context state.

    Signature

    • (param) includes
    • (returns) a generator

    rest.readOne()

    Fetches a model instance and returns it in the response.

    Signature

    • (returns) a generator

    rest.readAll(options)

    Fetches a bunch of model instances and returns them in the response.

    Signature

    • (param) options, a javascript object passed on to Sequelize's instance.findAll
    • (returns) a generator

    rest.create()

    Creates and saves a new instance of model, then returns the newly created instance in the response.

    Signature

    • (returns) a generator

    rest.update(options)

    Updates an instance of model, then returns the updated instance in the response.

    Signature

    • (param) options, a javascript object passed on to Sequelize's instance.update
    • (returns) a generator

    rest.delete()

    Deletes an instance of model.

    Signature

    • (returns) a generator

    License

    MIT

    Install

    npm i koa-sequelize-rest

    DownloadsWeekly Downloads

    6

    Version

    0.0.2

    License

    MIT

    Last publish

    Collaborators

    • as0n