Giter Site home page Giter Site logo

serby / save Goto Github PK

View Code? Open in Web Editor NEW
143.0 8.0 20.0 825 KB

A simple CRUD based persistence abstraction for storing objects to any backend data store. eg. Memory, MongoDB, Redis, CouchDB, Postgres, Punch Card etc.

License: ISC License

JavaScript 100.00%

save's Introduction

save - A simple CRUD based persistence abstraction for storing objects to any backend data store. eg. Memory, MongoDB, Redis, CouchDB, Postgres, Punch Card etc.

NPM Details

build status Dependences Join the chat at https://gitter.im/serby/save

save comes with a fully featured in memory engine which is super handy for testing your models. For real world use you'll need to get one of the database powered engines:

If your data store of choice isn't listed here please create an engine and send me a pull request. To see an example of how to create an engine, please see save-mongodb.

Installation

npm install save

Example

var save = require('save')
  , s = save('person')

s.on('create', function() {
  console.log('New person created!')
})

s.create({ name: 'Dom' }, function(err, person) {
  // Outputs { name: 'Dom', _id: 1 }
  console.log(person)
})

Usage

var save = require('save')

var s = save(name, [options])

Save by default returns an in memory engine which means you can unit test your models independently from your database. name is the name of your model.

Possible options are:

  • idProperty. Defaults to _id for mongodb
  • logger. Defaults to console logging: { info: console.info, verbose: console.info }
  • engine. Persistence engine to use, defaults to memory engine: require(./memory-engine)

s.create(object, [cb])

Creates a new entity. cb called with cb(err, savedObject).

s.read(id, [cb])

Reads a single entity with an idProperty of id. cb called with cb(err, readObject).

s.update(object, overwrite, [cb])

Updates a single entity. Optionally overwrites the entire entity, by default just extends it with the new values. cb called with cb(err, readObject).

s.delete(id, [cb])

Deletes one entity. Returns an error if the object can not be found. cb called with cb(err).

s.deleteMany(query, [cb])

Deletes entities based on a query. Performs a find by query, then calls delete for each item returned Returns an error if no items match the query. cb called with cb(err).

s.find(query, options, cb)

Performs a find on the data. cb called with cb(err, foundObjectsArray).

s.findOne(query, options, cb)

Performs a find on the data and limits the result set to 1. cb called with cb(err, foundObject).

s.count(query, cb)

Performs a count by query. cb called with cb(err, count).

s.idProperty

Provides access to the idProperty. Mostly used for testing.

Events

s.on('create', cb)

This event fires with cb(object) where object is the item that will be created.

s.on('afterCreate', cb)

This event fires with cb(object) where object is the item that has been created.

s.on('update', cb)

This event fires with cb(object, overwrite) where object is the item that will be updated and overwrite is whether the object is to be overwritten or extended.

s.on('afterUpdate', cb)

This event fires with cb(object, overwrite) where object is the item that has been updated and overwrite is whether the object is to be overwritten or extended.

s.on('delete', cb)

This event fires with cb(id) where id is the item that will be deleted.

s.on('afterDelete', cb)

This event fires with cb(id) where id is the item that has been deleted.

s.on('deleteMany', cb)

This event fires with cb(query) where query is the query used to deleteMany.

s.on('afterDeleteMany', cb)

This event fires with cb(query) where query is the query that has used deleteMany.

s.on('read', cb)

This event fires with cb(id) where id is the item that has been read.

s.on('find', cb)

This event fires with cb(query) where query is the query used to find.

s.on('findOne', cb)

This event fires with cb(query) where query is the query used to findOne.

s.on('count', cb)

This event fires with cb(query) where query is the query used to count.

s.on('error', cb)

This event fires with cb(err) where err is any error that may have occured.

Credits

Paul Serby follow me on twitter @serby

Dom Harrington

Licence

Licenced under the New BSD License

save's People

Contributors

bengourley avatar dependabot[bot] avatar gitter-badger avatar kofrasa avatar mantasmikal avatar richardgoater avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

save's Issues

t

y

Issue _.extend(opts, defaults)

Im at a hackathon and don't have time to submit a pull request, if I remember to after I will, if not sorry.

In memory-enigne.js,

_.extend(defaults, opts)

should be

_.extend(opts, defaults)

as your settings wont take effect else

Cursor support

Addition of a method that would allow engines to provide cursor (like) implementations.

Would require implementation within the engines themselves as well.

Example interface similar to async.each
save.findEach(query, function (obj, cb) { cb(null) }, callback)

Exposing a read stream for find?

Would be great to expose a way to return streams of data from .find(). Maybe using the same interface that mongodb uses: var stream = collection.find().stream()

Obviously this would need to be implemented in the memory-engine and then again in save-mongodb.

CouchDB

Will you be able to offer CouchDB support please?

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.