-
-
Notifications
You must be signed in to change notification settings - Fork 675
Dexie
Dexie.js is a library that makes it dead-simple to use indexedDB - the standard client-side database in browsers.
Dexie is the main class and the default export in the Dexie.js library. An instance of the Dexie class represents an indexedDB database connection.
With Dexie.js, you declare your database versions and their schemas in a minimalistic fashion instead of explicitly subscribing to the 'upgradeneeded' event. But it is also possible to open existing databasess even if they weren't created via Dexie. The library will adapt to any existing indexedDB database and provide the simple api to work with your existing database. Dexie is not dependent on any proprietary meta data. Just plain indexedDB.
var db = new Dexie (databaseName, options);
dbName : String | Database name |
options: Object (optional) | Options |
addons: Array<DexieAddon> | Explicitly define the addons to activate for this db instance |
autoOpen: boolean | Default true. Whether database will open automatically on first query. |
If the 'addons' option is omitted, it will default to the value of Dexie.addons.
// Declare db instance
var db = new Dexie("MyDatabase");
// Define Database Schema
db.version(1).stores({
friends: "++id, name, age, isCloseFriend",
notes: "++id, title, date"
});
// Interact With Database
db.transaction('rw', db.friends, db.notes, function () {
// Let's add some data to db:
db.friends.add({name: 'Camilla', age: 25, isCloseFriend: 1 });
db.friends.add({name: 'Ban Ki-moon', age: 70, isCloseFriend: 0 });
db.notes.add({title: 'Shop tomorrow', date: new Date(), items: ['milk', 'butter']});
// Let's query the db
db.friends.where('isCloseFriend').equals(1).each(function(friend) {
console.log("Found close friend: " + JSON.stringify(friend));
});
db.notes.where('title').startsWithIgnoreCase ('shop').each (function(note) {
console.log("Found shopping note: " + JSON.stringify(note));
});
}).catch(function(err) {
// Catch any error event or exception and log it:
console.error(err.stack || err);
});
// Define the addon
function myForEachAddon(db) {
db.Collection.prototype.forEach = db.Collection.each;
}
// Register it (optional)
Dexie.addons.push(myForEachAddon);
// Use it:
var db = new Dexie('dbname');
db.version(1).stores({friends: 'name'});
db.transaction('rw', db.friends, function () {
db.friends.clear();
db.friends.bulkAdd([{name: "Foo"},{name: "Bar"}]);
}).then(function() {
db.friends.where('name').anyOfIgnoreCase('foo','bar').forEach(friend => {
console.log(friend.name);
});
}).catch(ex => {
console.error(ex);
});
// Explicitely tell Dexie to not ignored any addons:
var db2 = new Dexie('dbname', {addons: []});
// Explicitely tell Dexie to use just your set of addons:
var db3 = new Dexie('dbname', {addons: [myForEachAddon]});
Specify the database schema (object stores and indexes) for a certain version.
Subscribe to events
Open database and make it start functioning.
Returns an object store to operate on
Start a database transaction
Close the database
Delete the database
Returns true if database is open.
Returns true if database failed to open.
Returns the native IDBDatabase instance.
Enable on('ready') subscribers to use db before open() is complete.
The database name.
Each object store defined in version().stores() gets a WriteableTable instance named by the object store.
Javascript Array containing all Table instances.
Version of current database
Declare an async function in today's modern browsers (2015) without the need for a transpiler.
Spawn an async function in today's modern browsers (2015) without the need for a transpiler.
Delete a database.
List all database names at current origin.
Detect whether a database with the given name exists.
Retrieve a property from an object given a key path.
Modify a property in an object given a key path and value.
Delete a property from an object given a key path.
Shallow clones an object.
Deep clones an object.
Create a a new scope where current transaction is ignored.
Override existing method and be able to call the original method.
Creates a function and populates its prototype with given structure.
Fixes the prototype chain for OOP inheritance.
Set given additional properties into given object.
Create a set of events to subscribe to and fire.
Array of extended constructors.
Contains the version number of Dexie as a numeric comparable decimal value.
Contains the semantic version string from package.json.
Dexie.js - minimalistic and bullet proof indexedDB library