I am quite new to indexedDB, and started to create a Database to transfer WebSQL.
What I would like to know is how fields are created in indexedDB.
For example:
tx.executeSql("CREATE TABLE IF NOT EXISTS TEMPRESULTENTRIES ( "
+ " id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ " instid INTEGER NOT NULL REFERENCES institutions(id) ON DELETE CASCADE, "
+ " qid INTEGER NOT NULL REFERENCES questionnaires(id) ON DELETE CASCADE, "
+ " result TEXT NOT NULL )");
How would I create the same table in indexedDB. I am kind of confused about this part of indexedDB. Currently I am creating only keypath with id:
db.createObjectStore("TEMPRESULTENTRIES", {keypath: "id", autoIncrement: true});
And I am wondering how to add, or when will the other fields be created?
From a small testcase, I can see that I can just create Columns/Objects in Store when I anm populating table/Store, as:
store.put({ObjectName: Value});
Is it the correct way to create Objects in the stores?
Best, Amry
IndexedDB is an object-oriented database, not a relational database. The closest analog to SQL tables is object stores, created with IDBDatabase.createObjectStore. Object stores don't have a fixed schema, so a call to IDBObjectStore.put can accept objects with any fields so long as the object has a keyPath field or the keyPath is made optional by setting {autoIncrement: true}. By default, you can only query for documents using the keyPath. To query documents using other fields, an index must be created using IDBObjectStore.createIndex
This is an excerpt from an application that looks up contacts using a prefix of number or name.
dbReq.onupgradeneeded = function (event) {
var db = event.target.result,
objectStore = db.createObjectStore('calls', {keyPath: 'timestamp'}),
contactStore = db.createObjectStore('contacts', {autoIncrement: true});
objectStore.createIndex('number', 'number', {unique: false});
contactStore.createIndex('number', 'number');
contactStore.createIndex('name', 'name', {unique: false});
objectStore.createIndex('timestamp', 'timestamp', {unique: true});
};
Find by prefix:
findPrefix: function(prefix, fn) {
var transaction = this.db.transaction(['contacts', 'calls']),
contactStore = transaction.objectStore('contacts'),
callStore = transaction.objectStore('calls'),
numberIndex = callStore.index('number'),
nameIndex = contactStore.index('name'),
key = IDBKeyRange.bound(prefix, prefix + '\uffff'),
times = 0,
result = [],
consume = function(records) {
result = result.concat(records);
if (++times === 2) {
/* Remove duplicate numbers: names and numbers may have the same value*/
var numbers = {};
result = result.filter(function(contact) {
if (!numbers[contact.number]) {
return (numbers[contact.number] = true);
}
return false;
});
fn(result);
}
};
this.consumeCursor(numberIndex.openCursor(key, 'nextunique'), consume);
this.consumeCursor(nameIndex.openCursor(key), consume);
}
More on IndexedDB
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With