Dexie.js
A Minimalistic Wrapper for IndexedDB
Easy to learn
Dexie was written to be straightforward and easy to learn. If you’ve ever had to work with native IndexedDB then you’ll certainly appreciate Dexie’s concise API.
Well documented
What good is any development tool without great documentation? Dexie is thoroughly explained, and examples are available to help you on your way.
Performant
Dexie has an near-native performance. It’s’ bulk operations utilize a rarely used feature in indexedDB – to ignore success callbacks when possible.
Basic examples
/*
|----------------------------|
| Make a database connection |
|----------------------------|
*/
var db = new Dexie('MyDatabase');
// Define a schema
db.version(1).stores({
friends: 'name, age'
});
// Open the database
db.open().catch(function(error) {
alert('Uh oh : ' + error);
});
/*
|-----------------------|
| Then run some queries |
|-----------------------|
*/
// Find some old friends
db.friends
.where('age')
.above(75)
.each (function (friend) {
console.log (friend.name);
});
// or make a new one
db.friends.add({
name: 'Camilla',
age: 25
});
Getting Started
Dexie.js can be consumed as a module. But let’s skip that for now and just show the simplest possible setup. You will just need a text editor and a web browser.
Quick Start
- Copy this code to and save as ‘name.html’
<!doctype html>
<html>
<head>
<!-- Include Dexie -->
<script src="https://unpkg.com/dexie@latest/dist/dexie.js"></script>
<script>
//
// Define your database
//
var db = new Dexie("friend_database");
db.version(1).stores({
friends: 'name,shoeSize'
});
//
// Put some data into it
//
db.friends.put({name: "Nicolas", shoeSize: 8}).then (function(){
//
// Then when data is stored, read from it
//
return db.friends.get('Nicolas');
}).then(function (friend) {
//
// Display the result
//
alert ("Nicolas has shoe size " + friend.shoeSize);
}).catch(function(error) {
//
// Finally don't forget to catch any error
// that could have happened anywhere in the
// code blocks above.
//
alert ("Ooops: " + error);
});
</script>
</head>
</html>
- Open the file in Chrome, Opera or Firefox. If you need to test on IE, Edge or Safari, make sure to serve the page over http or https
- You should see an alert box pops up saying
Nicolas has shoe size 8
. - Play with this file. Change some code and see what happens.
Documentation: http://dexie.org/
0 Comments