0.1.11 • Published 9 years ago

nedb-active-record v0.1.11

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

nedb-active-record

Simple realization of ActiveRecord pattern for NeDB

Installation

Through NPM:

npm install nedb-active-record

Code Example

ActiveRecord = require('nedb-active-record');

/*
    Global configuration for all instances.
    This is initial parameters for NeDB.
    Also was added two additional params:
    - location (internal folder), by default 'database',
    - format (e.c. 'db', 'txt')
*/
ActiveRecord.config({
    timestampData: true,
    autoload: true
});

//Creation of new collection (will create new file '/database/users.txt')
var User = new ActiveRecord('users');

//Create new user record.
var user = new User();
user.name = 'John';
user.age = 31;
user.save();

user.surname = 'Smith';
user.save();

//You can add parameters to constructor.
var user2 = new User({
    name: 'Rocky',
    surname: 'Balboa',
    age: 69
});

user2.save();

/*
    You can find data like in NeDB, unfortunately without chaining.
    Instead uses: $$sort, $$limit, $$skip, $$projection.
*/
User.find({age: {$gt: 23}, $$sort: {name: 1}})
    .then(function(users) {
        //users -> collection of User instances

        //Return new user record
        return User.insert({_id: 1, name: 'Bohdan'});
    })
    .then(function(newUser){
        newUser.citizenship = 'Ukrainian';
        newUser.save();
    })
    .then(function(){
        return User.findOne({_id:1}).then(function(foundUser){
            //Removing
            return foundUser.remove();
        });
    })
    .then(function(numRemoved) {
        console.log(numRemoved)
    })
    .catch(function(error){
        console.log(error);
    });
0.1.11

9 years ago

0.1.10

9 years ago

0.1.9

10 years ago

0.1.8

10 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago