-
Notifications
You must be signed in to change notification settings - Fork 62
Indexes
To create indexes, you need to pass the index information to the IDBStore() constructor, for example:
{
storeName: 'customers',
dbVersion: 1,
keyPath: 'customerid',
autoIncrement: true,
onStoreReady: function(){},
indexes: [
{ name: 'lastname', keyPath: 'lastname', unique: false, multiEntry: false }
]
}
An entry in the index Array is an object containing the following properties:
The name
property is the identifier of the index. If you want to work with the created index later, this name is used to identify the index. This is the only property that is mandatory.
The keyPath
property is the name of the property in your stored data that you want to index. If you omit that, IDBWrapper will assume that it is the same as the provided name, and will use this instead.
The unique
property tells the store whether the indexed property in your data is unique. If you set this to true, it will add a uniqueness constraint to the store which will make it throw if you try to store data that violates that constraint. If you omit that, IDBWrapper will set this to false.
The multiEntry
property is used if the indexed property is an array, and you want to have one index entry for each item in the array. A use case could be an array of tags of a blog post, or an array of phone numbers of a user. By default, this is set to false.
If you want to add an index to an existing store, you need to increase the version number of your store, as adding an index changes the structure of the database.
To modify an index, modify the object in the indexes Array in the constructor. Again, you need to increase the version of your store.
In addition, there are still some convenience methods available:
hasIndex: function(/*String*/ indexName)
Return true if an index with the given name exists in the store, false if not.
getIndexList: function()
Returns a DOMStringList
with all existing indices.