Giter Site home page Giter Site logo

crud-service's Introduction

crud-service

build status

NPM

Simple crud service for object definition, validation, basic persistence and collection.

Installation

npm install crud-service

Usage

const createCrudService = require('crud-service')
const save = require('save')
const schema = require('./schema')
const service = createCrudService('things', save('thing'), schema())

// service now has some slightly inflated CRUD functionality:
// .create() .read() .update() .partialUpdate()
// .delete() .deleteMany() .find() .count()

API

var service = new CrudService(String: propertyName, Save: collection, Schemata: schema, Object, options)

Create a new crud service that stores entites in the provided collection. This should be a save instance with your preferred engine. A schemata schema is required for validation

service has the following CRUD-y methods:

service.create(Object: obj, Object: options, Function: cb)

service.read(String: objId, Function: cb)

service.update(Object: obj, Object: options, Function: cb)

service.partialUpdate(Object: obj, Object: options, Function: cb)

service.delete(String: objId, Function: cb)

service.deleteMany(Object: query, Function: cb)

service.find(Object: query, Object: options, Function: cb)

All options arguments are optional.

Omitting cb when calling service.find() will return a stream.

service also has the following method:

service.pre(String: hook, Function: processor)

This facilitates a pipeline for object manipulation before certain operations. pre() can be called multiple times for the same hook and the processor functions will be queued up. A processor function has the signature function (entity, cb) {}, and should callback with cb(err, entity).

A simple example is to maintain a lastUpdated property on service objects:

const setUpdateTime = (entity, cb) => {
  entity.lastUpdated = new Date()
  cb(null, entity)
}

service.pre('update', setUpdateTime)
service.pre('partialUpdate', setUpdateTime)

The available pre hooks are:

  • create - after validation, just before persistence
  • createValidate - before validation
  • update - after validation, just before persistence
  • updateValidate - before validation
  • partialUpdate - after validation, just before persistence
  • partialValidate - before validation
  • delete - before deletion

Credits

Paul Serby follow me on twitter @serby

License

Licensed under the ISC

crud-service's People

Contributors

bengourley avatar confuser avatar jack828 avatar kuba81 avatar lpmi-13 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.