Plasmid.js makes working with browser-side storage easy. It eases the effort in working with the IndexedDB API across different browsers and devices.
var database = new Database({
name: 'todo',
schema: {
version: 1,
stores: {
todo: {
indexes: {
todo: {
key: "todo",
unique: false,
multi: false
}
}
},
},
}
});
Now, you've setup a local database and you can start working with your data.
var todos = database.stores.todo;
todos.put(null, {
text: "Learn how to use Plasmid.js!",
completed: false
})
null
as the key, so Plasmid will generate one
for us randomly.
.then(
function ok(key, value) {
console.log("I saved my first todo item this key: ");
},
put()
request is done, we'll be given the key and
value that were used,
function error(msg) {
console.error("Could not save: " + msg);
}
)
Eventually, you'll learn how to use Plasmid.js and will need to check off this
item. You'll need to fetch the current version of the object, update the completed
property, and put it back into the store.
If you know anything about using a database, you're wondering if this two-step operation can be atomic.
var txn = database.transaction('todo', 'readwrite');
var todos = txn.stores.todo;
'readwrite'
mode tells
the underlying IndexedDB system this transaction involves both reading and writing
the stores involved.
todos.get(key).then(update);
function update(todo) {
todo.completed = true;
todos.put(key, todo);
}
Of course, you won't be checking that item as completed until after you've read through the complete Plasmid.js API Reference.