Module-documentation

version 1.0.0
module: client-db version 0.0.1
size-min gzipped: 1.99 kb (incl. dependencies: 15.32 kb)
dependencies:
maintanance: Marco Asbreuk
home


all modules

DB for storage of objects

This module creates storages for objects which can be retrieved convieniently or fast by indexes. The databases use indexeddb, or localStorage when indexeddb is not present. The localStorage polyfill has exactly the same features. Items can be retrieved by specifying a search-condition. When the search-condition uses indexes, retrieval is extremely fast.

Getting Started

With nodejs:

The module client-db is a typical browser-module.

In the browser:

For browser-usage, ITSA has a predefined loaderfiles. Once included, a global ITSA object with default features is available. For customized loaderfiles, read: Customized build.

<script src="/itsabuild-min.js"></script>
<script>
    var DB = ITSA.DB;
</script>
When using your own customized build-file, this module-requirement needs to be invoked with the window-object.

The Basics

DB is a Class that creates a database with a name, version and predefined tables. These tables have a name and optionally indexes can be specified.

Once a database is setup, you can use it to store and retrieve records. When setting up a database that does not exist, the database will be created. If a database of a previous version exists, it will be upgraded to the new version.

All methods are Promise-based. This means, you can use them, but need the thenable to inspect the returnvalues.

Storage-system

Under the hood, by default indexeddb is used as storagesystem. When indexeddb is not available, localStorage will be used, but upgraded so it has the same features.

Usage

You always need to use the exact database-definition before you can start using a DB-instance. This is required, because it will lead to a new database-definition when the client doesn't have it yet. Also this definition is responsible for version-upgrades.

Example creating storage

var databaseName = 'myProject',
    version = 1, // needs to be a number
    tables = [
        {
            name: 'customers',
            uniqueIndexes: ['name'],
            indexes: ['birth']
        },
        {
            name: 'projects',
            uniqueIndexes: ['projectId'],
            indexes: ['name', 'location']
        }
    ],
    dbProjects;

dbProjects = new ITSA.DB(databaseName, version, tables);

Example save a value

dbProjects.save('customers', {name: 'Barack', lastName: 'Obama', 'birth': 1961});

Example save a value and inspect

dbProjects.save('customers', {name: 'Barack', lastName: 'Obama', 'birth': 1961}).then(
    function() {
        // record is stored
    }
).catch(function(err) {
    ITSA.warn(err);
});

Example read a value

var key = 'name';
dbProjects.read('customers', key, 'Barack').then(
    function(record) {
        // record === {name: 'Barack', lastName: 'Obama', 'birth': 1961}
    }
).catch(function(err) {
    ITSA.warn(err);
});

The promises only rejects on errors. When reading a record that isn't available, the promise gets resolved with undefined as response-value.

Storage-methods

All examples are based upon the myProject-database above.

Available methods

contains

Example contains

dbProjects.contains('customers', {name: 'Barack', lastName: 'Obama', 'birth': 1961}).then(
    function(result) {
        // result === true
    }
);
dbProjects.contains('customers', {name: 'Bill', lastName: 'Clinton', 'birth': 1946}).then(
    function(result) {
        // result === false
    }
);

clear

Example clear

dbProjects.clear('customers').then(
    function() {
        dbProjects.contains('customers', {name: 'Barack', lastName: 'Obama', 'birth': 1961}).then(
            function(result) {
                // result === false
            }
        );
    }
);

delete

Example delete

dbProjects.delete('customers', 'birth', [1960, 1961, 1962]).then(
    function() {
        dbProjects.contains('customers', {name: 'Barack', lastName: 'Obama', 'birth': 1961}).then(
            function(result) {
                // result === false
            }
        );
    }
);

deleteDatabase

Example deleteDatabase

dbProjects.deleteDatabase();
// dbProjects should cannot be used anymore

each

Example each

var years = 0;
dbProjects.each('customers', function(record) {
    years += record.birth;
}).then(
    function() {
        // years has the total amount of years of all Presidents
    }
);

has

Example has

var key = 'birth';
dbProjects.has('customers', key, [1961, 1962]).then(
    function(result) {
        // result === true
    }
);
dbProjects.has('customers', key, 1999).then(
    function(result) {
        // result === false
    }
);

read

Example get

var key = 'name';
dbProjects.read('customers', key, 'Barack').then(
    function(record) {
        // record === {name: 'Barack', lastName: 'Obama', 'birth': 1961}
    }
});

readMany

Example get

var key = 'birth';
dbProjects.readMany('customers', key, 1961).then(
    function(records) {
        // records === [{name: 'Barack', lastName: 'Obama', 'birth': 1961}]
    }
});

readAll

Example get

dbProjects.read('customers').then(
    function(records) {
        // records === [{name: 'Barack', lastName: 'Obama', 'birth': 1961}]
    }
});

save

Example set

var president = {
        name: 'Barack',
        lastName: 'Obama',
        'birth': 1961
    };
dbProjects.save('customers', president);

size

Example size

dbProjects.size('customers').then(
    function(value) {
        // value === size of table `customers`
    }
);

some

Example some

dbProjects.some('customers', function(record) {
    return (record.birth===1917);
}).then(
    function(president) {
        // `president` is an object (JFK)
    }
);
dbProjects.some('customers', function(record) {
    return (record.birth===1999);
}).then(
    function(president) {
        // `president` is `undefined`
    }
);
API Docs

Examples