NeDB persistence adapter for modella
npm install modella-nedb[NeDB][ne] persistence for [modella][mo].
NeDB is a tiny embedded database, written in JavaScript, with a
MongoDB-like API. It's ideal for prototyping, microservices, and
desktop/CLI applications, and has a human-readable file format.
Modella is simple, extensible no-BS models for browser and server.
$ npm install modella-nedb
``js
var nedb = require('modella-nedb')('./user.db')
var model = require('modella')
var User = model('User')
.attr('_id')
.attr('name')
.attr('email', {unique:true})
.attr('age')
User.use(nedb)
// query
User
.query({age:{$gt:10}})
.sort({age:-1})
.exec(function(err, users){
// users is an array of User instances
})
`
Call the module function with opts to return a plugin. opts can beDatastore
an object, a string file path, or an NeDB instance. Valid
options are described in the NeDB docs.
Called without any options, the datastore will be in-memory, with no
persistence.
`js`
var plugin = require('modella-nedb')
// all legit
plugin('./user.db')
plugin({filename:'./user.db'})
plugin(new Datastore('./user.db'))
Saves a model's properties to disk and calls cb.
Removes a model from the db.
Aliased to Model.get. Finds a single model using query.
Aliased to Model.query. Finds models using query, calls cb or returns a Cursor (see below).
Remove models matching query and calls cb.
Set an index on the datastore. Options are:
- fieldName Name of the field to index
- unique Enforce uniqueness
- sparse Don't index documents that don't have this field
You can also specify indexes in the model attributes, like this:
`js`
User
.attr('age', {index:true}) // indexed
.attr('email', {unique:true}) // unique
NeDB Datastore instance. Use for direct database access.
A call to Model.all/Model.query without a callback functionCursor
returns a instance. This is the same as an NeDB cursor, butModel
it will respond with an array of instances.
#### Cursor#sort(fields)
Sort the result set by an object of fields like {email:1, age:-1}.
#### Cursor#skip(n)
Skip n records in the result set.
#### Cursor#limit(n)
Limit the result set to n records.
#### Cursor#exec(fn)
Execute the query and call fn` with the results.
MIT
[mo]:https://github.com/modella/modella
[ne]:https://github.com/louischatriot/nedb
[doc]:https://github.com/louischatriot/nedb#creatingloading-a-database