Syncronous indexed database

The syncronous indexed database uses the same data structures as IndexedDB, and much of the programming interface is the same, but without asyncronous transaction event handling, so it is much simpler to use. A database can have any number of object stores, which are like the tables in an SQL database. Each object store contains a list of records which can be searched using a key. var db = dev.db.Database.create ("sample_db"); var my_store = db.createObjectStore ("my_store", { autoIncrement: true }); my_store.add ( { some: "kind of data" } ); db.commit();

Changes to the database are saved to disk using commit(). With autoIncrement, the key for the first record will simply be 1. The record can later be retrieved from the database... var db = dev.db.Database.open ("sample_db"); var my_store = db.objectStore ("my_store"); console.log (my_store.get (1));

Web.dev.app's built-in databases can currently store Number, String, Boolean, Object, Array, Set, Map, Date, RegExp, null and undefined. Each record is stored separately but as a single block, regardless of how deep the properties go. Only records that change are saved to disk during a commit. No circular or diamond references are permitted.

A database can only be accessed by one thread at a time. A thread will lock a database when opening it or when lock() is called. It remains locked until the script finishes or unlock() is called. Competing threads wait for a database to be unlocked before locking it themselves. A database remains open in memory and ready to be locked again until the close() method is called or the app shuts down.

This database was designed for A.C.I.D. compliance. If there is a problem during the commit then either no changes are made, or the commit will be attempted again next time the database is opened. Future versions will support encryption. Database files have no size limit and future versions may allow spanning across multiple disks. ACID compliance has not been tested.

Methods of the dev.db.Database constructor

If no filename folder is given then the database will be stored in the app's private data folder.

dev.db.Database.close (filename)

dev.db.Database.create (filename)

dev.db.Database.delete (filename)

dev.db.Database.exists (filename)

dev.db.Database.open (filename)

Properties and methods of a Database instance

database_instance.name

database_instance.objectStoreNames

database_instance.version


database_instance.commit ()

database_instance.createObjectStore (name)
database_instance.createObjectStore (name, options)

database_instance.deleteObjectStore (name)

database_instance.lock ()

database_instance.objectStore (name)

database_instance.unlock ()

Properties and methods of an ObjectStore

object_store.autoIncrement

object_store.indexNames

object_store.keyPath

object_store.name


object_store.add (value)
object_store.add (value, key)

object_store.clear ()

object_store.count ()
object_store.count (key_range)

object_store.createIndex ()

object_store.delete (key)
object_store.delete (key_range)

object_store.deleteIndex (name)

object_store.get (key)
object_store.get (key_range)

object_store.getAll (key)
object_store.getAll (key_range)

object_store.getAllKeys (key)
object_store.getAllKeys (key_range)

object_store.getKey (key)
object_store.getKey (key_range)

object_store.openCursor ()

object_store.openKeyCursor ()

object_store.put (value)
object_store.put (value, key)

Search IndexedDB documentation online for more information about these methods.

Methods of the dev.db.KeyRange constructor

dev.db.KeyRange.bound (lower, upper)
dev.db.KeyRange.bound (lower, upper, lowerOpen, upperOpen)

dev.db.KeyRange.only (value)

dev.db.KeyRange.lowerBound (lower)
dev.db.KeyRange.lowerBound (lower, lowerOpen)

dev.db.KeyRange.upperBound (upper)
dev.db.KeyRange.upperBound (upper, upperOpen)

Search IndexedDB documentation online for more information about these methods.

Properties and methods of a KeyRange

key_range.lower

key_range.lowerOpen

key_range.upper

key_range.upperOpen


key_range.includes (key)

Search IndexedDB documentation online for more information about these methods.

Properties and methods of a Cursor

key_range.source

key_range.direction

key_range.key

key_range.primaryKey

key_range.value


key_range.advance (key)

key_range.continue (key)

key_range.continuePrimaryKey (key)

key_range.delete (key)

key_range.update (key)

Search IndexedDB documentation online for more information about these properties and methods.

Example

The following database contains the populations of some capital cities. Key ranges are used to search for cities within certain population limits. <script> var cities_by_population = [ { name: "Beijing", pop: 20693000 }, { name: "Tokyo", pop: 13189000 }, { name: "Moscow", pop: 11541000 }, { name: "London", pop: 8630000 }, { name: "Singapore", pop: 5535000 }, { name: "Berlin", pop: 3520000 }, { name: "Rome", pop: 2868000 }, { name: "Vienna", pop: 1749000 }, { name: "Amsterdam", pop: 810909 }, { name: "Lisbon", pop: 564657 } ]; var db, DB = dev.db.Database; if (DB.exists ("my_sync_db")) db = DB.open ("my_sync_db"); else CreateDatabase(); function CreateDatabase (){ db = DB.create ("my_sync_db"); var my_store = db.createObjectStore ("my_store", { keyPath: 'pop' }); cities_by_population.forEach (function (item){ my_store.add (item);}); db.commit();} </script> <ul> <button onclick='Show (0, 1e8)'> All capital cities </button> <button onclick='Show (0, 1e6)'> cities < 1 million people </button> <button onclick='Show (1e6, 1e7)'> cities from 1 to 10 million </button> <button onclick='Show (1e7, 1e8)'> cities > 10 million people </button> </ul> <ul id='cities'></ul> <script> function Show (a, b){ db.lock(); var key_range = dev.db.KeyRange.bound (a, b, true, false); var array = db.objectStore ("my_store").getAll (key_range); var html = ""; array.forEach (function (item){ var pop = String (item.pop).replace (/(.)(?=(\d{3})+$)/g,'$1,'); html += "<li>" + item.name + " has " + pop + " people";}); cities.innerHTML = html;} Show (0, 1e8); </script>