Git Product home page Git Product logo

db.js's Introduction

db.js

IndexedDB is a powerful technology. Unfortunatelly the API exposed is complex to use and there are some cross browser differences.

db.js is an up to date indexedDB Wrapper that provides an easier to use, cross browser API. Tested on Chrome and Firefox.

Follow optimalbits for news and updates regarding this library.

##Opening a Database (new or existing one)

Open a database using the factory function,

Arguments

name      {String} Database name. If un-existent it will create a new one.
version   {Number?} Optional version number for this database.
callback  {(err?: Error, db?) => void}

DBFactory(name, version, cb) 

Example:

DBFactory("zoo", function(err, db){
  if(!err){
    // db is the open database.
  }
})

##Deleting a Database

A database can be totally deleted using the delete method:

Arguments

name      {String} Database name to be deleted.
callback  {(err?: Error) => void}

DBFactory##deleteDatabase(name, cb) 

##Getting an Object Store

A database is composed of an unlimited number of object stores. An object store is nothing more than a key value store with some simple query capabilities.

Arguments

name      {String} Object store name. If un-existent a new one will be created.
callback  {(err?: Error, store: ObjectStore?) => void}

DBWrapper##getObjectStore(name, cb)

Example

db.getObjectStore('birds', function(err, store){
  if(!err){
    // store containes our "birds" key value store.
  }
})

###Set, Get, Update & Delete

An object store supports the common CRUD operations:

Sets an object in the storage associated to the given key. Note, if there is already an object associated to the key it will be overwritten.

Arguments

key      {String} The key with the object to store.
object   {Object} Any object supported by IndexedDB
callback  {(err?: Error) => void}

ObjectStore##setObject(key, object, cb)

Gets the object associated to the given key if any.

Arguments

key       {String} The key of the object to retrieve.
callback  {(err?: Error, object: Object) => void}

ObjectStore##getObject(key, cb)

Updates the object associated to the given key (this only works for plain javascript objects, where the properties of the stored object are extended by the properties of the stored object)

Arguments

key       {String} The key with the object to update.
newObject {Object} Any object supported by IndexedDB
callback  {(err?: Error, object: Object) => void}

ObjectStore##updateObject(key, newObject, callback)

###Queries

Simple queries can be performed using the KeyRange API from indexedDB:

Arguments

keyRange  {IDBKeyRange} A key range object specifying the range to get.
filter    {(obj) => bool} A filter function used to select a subset of the
matching range.
callback  {(err?: Error, object: Object) => void} A callback with an 
object with the query results, as a mapping between keys and objects.

ObjectStore##query(keyRange, filter, callback)

##Unit tests

The unit tests are written using mocha. For running the unit tests you need to use node and a webbrowser with IndexedDB capabilities (only Chrome, IE10 and Firefox at the moment).

Go to the test directory and run the test server:

    node server.js

Open a webbrowser pointing to http://localhost:8080

##License

(The MIT License)

Copyright (c) 2013 Optimal Bits www.optimalbits.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

db.js's People

Contributors

manast avatar

Watchers

 avatar

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.