What is Plasmid.js?
Plasmid.js makes working with browser-side storage easy. It eases the effort in working with the IndexedDB API across different browsers and devices.
Getting Started
var database = new Database({
name: 'todo',
schema: {
version: 1,
We create a local database very easily, with a name and begin defining the first
version of the schema.
stores: {
todo: {
The schema defines the stores in the database, each of which we can fill with data objects.
indexes: {
todo: {
Optionally, we can define indexes on these stores.
key: "todo",
unique: false,
multi: false
}
}
},
},
}
});
Our index is defined by a key on a property path into the objects we'll
put in the store, and flags to determine if we enforce uniqueness and
index arrays as one or multiple values.
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
})
We can use any store defined in our database.
Here, we're putting our first object into the todo store, and we're passing
Here, we're putting our first object into the todo store, and we're passing
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: ");
},
When our
put()
request is done, we'll be given the key and
value that were used,
function error(msg) {
console.error("Could not save: " + msg);
}
)
and we handle an error, if it happens.
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;
Plasmid supports transactions, which have the same API as a database that contains
only the stores involved in the transaction. The
'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);
}
Actually updating the item is simple, and should be self explainatory.
Of course, you won't be checking that item as completed until after you've read through the complete Plasmid.js API Reference.