0.1.1 • Published 6 years ago

localbank v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

localbank

Localbank is an abstraction layer for storing objects on various endpoints.

npm install localbank

Drivers

Localbank requires "drivers" to define an endpoint for storing and retrieving objects.

  • fs - A (built in) driver that stores objects on the file system in JSON files.
  • localbank-mongodb - A driver that allows you to use MongoDB as an endpoint for storing files.
  • localbank-mysql - A driver that allows you to use MySQL as an endpoint for storing files.

To use a driver, all you have to do it load it with your localbank instance. With the following example, localbank will use the FS driver and save objects in ./storage/objects.

var localbank = require('localbank');
var localstore = new localbank.Instance();

localstore.use(localbank.drivers.fs, {
    path: path.resolve('./storage/objects')
});

Inserting Objects

In order to insert objects, you must define the properties an object will have. The following example is an example definition of a user collection.

localstore.define('users', {
    email: {
        type: 'string',
        required: true
    },
    password: {
        type: 'string',
        required: true
    },
    name: {
        type: 'string',
        required: true,
        default: 'John Smith'
    },
    active: {
        type: 'boolean',
        required: true,
        default: true
    }
});

localstore.insert('users', {
    email: 'example@npmjs.com',
    password: 'dont-use-plaintext-passwords',
    name: 'Example Smith'
}, function(error, item) {
    if (error) {
        console.log('could not insert user', error);

        return;
    }

    console.log('user was inserted', item);
});

Items automatically gain an id, updated, and created property when inserting into a collection.

Retrieving Objects

There are a few ways that objects can be retrived. The following example lists the most commons ways.

// get a user by its direct ID
localstore.get('users', '000-0000-0000-0000000', function(error, item) {
    if (error) {
        console.log('could not get user', error);

        return;
    }

    if (!item) {
        console.log('the user object was not found');

        return;
    }

    console.log(item);
});

// get all users in the collection
localstore.all('user', function(error, items) {
    if (error) {
        console.log('could not get users', error);

        return;
    }

    console.log(items);
});

// get users that are only active
localstore.filter('user', 'active', true, function(error, items) {
    if (error) {
        console.log('could not get users', error);

        return;
    }

    console.log(items);
});

// get a user directly by its email
localstore.find('user', 'email', 'example@npmjs.com', function(error, item) {
    if (error) {
        console.log('could not get users', error);

        return;
    }

    if (!item) {
        console.log('the user object was not found');

        return;
    }

    console.log(item);
});

Updating Objects

In order to update an object, the current object must be passed into the update function. The following example is how someone would update a user object to change its email.

localstore.get('users', '000-0000-0000-0000000', function(error, item) {
    if (error) {
        console.log('could not get user', error);

        return;
    }

    if (!item) {
        console.log('the user object was not found');

        return;
    }

    localstore.update('users', item.id, item, function(error, result) {
        if (error) {
            console.log('could not update user', error);

            return;
        }

        console.log('update result', result);
    });
});

License

The Localbank instance and main module is released under the open source MIT license. You can view the license file for more information.

0.1.1

6 years ago

0.1.0

6 years ago