New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@clocklimited/crud-service

Package Overview
Dependencies
Maintainers
7
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@clocklimited/crud-service

Create a crud service by combining schemata and save

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15
decreased by-70%
Maintainers
7
Weekly downloads
 
Created
Source

crud-service

CircleCI

NPM

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

Installation

npm install @clocklimited/crud-service

Usage

const createCrudService = require('@clocklimited/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

Clock Limited follow us on twitter @clock

License

Licensed under the ISC

FAQs

Package last updated on 14 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc