-
-
Notifications
You must be signed in to change notification settings - Fork 649
Version.stores()
version.stores(schemaDefinition);
schemaDefinition : Object | Object where each key represents the name of an object store and each value represents its schema |
Specifies tables to be added or altered in this version. Each key in the schemaDefinition argument represents a table name and each value represents the primary key followed by the list of indexed properties. Note that unlike SQL, you don't need to specify all properties but only the one you wish to index.
To add a table, add another call to Dexie.version() specifying a higher version number and call Version.stores() with the new table to add and is schema definition. Always keep the code defining previous version as long as you have users out there using it. It will tell the framework how to upgrade those users.
To add, delete or alter an index, add another version and just specify the new schemaDefinition as you want the resulting definition to be. The framework will compare and indexed for users with offer versions running. To delete a table, specify null as value for schemaDefinition.
var db = new Dexie('MyDatabase');
db.version(1).stores({
friends: '++id,name,shoeSize', // Primary Key is auto-incremented (++id)
pets: 'id,name,kind', // Primary Key is not auto-incremented (id)
cars: '++,name', // Primary Key is auto-incremented but not inbound
enemies: ',name,*weaknesses' // Primary key is neither inbound nor auto-incr.
// 'weaknesses' contains an array of entries (*)
});
++ | Auto-incremented primary key |
& | Unique |
* | Multi-valued index |
[A+B] | Compound indexes |
The first entry in the schema string will always represent the primary key.
Syntax For Primary Key | ||
++keyPath | Autoincrement primary key | Means that the primary key will be auto-incremented. Primary key must always be unique. |
++ | Hidden autoincremented primary key | Means that primary key is auto-incremented but not visible on the objects. |
keyPath | Dont autoincrement primary key | Means that primary key can be any type and we have to provide it ourself |
(blank) | Hidden primary key | Leaving the first entry blank means that primary key is hidden and not auto-incremented |
Syntax For Indexes | ||
keyPath | Means that keyPath is indexed | |
&keyPath | Unique | Means that keyPath is indexed and keys must be unique |
*keyPath | Multi-valued | Means that if key is an array, each array value will be regarded as a key to the object. |
[keyPath1+keyPath2] | Compound | Defining a compound index for keyPath1 and keyPath2 |
NOTE: keyPath represents a property name or a dotted path to a nestled property.
This sample shows how to define the schema of a given version:
var db = new Dexie("FriendsAndPetsDatabase");
db.version(1).stores({
users: "++id,name,&username,*email,address.city",
relations: "++,userId1,userId2,[userId1+userId2],relation"
});
db.open();
db.users.add({
name: "Zlatan",
username: "ibra",
email: [
"[email protected]",
"[email protected]"
],
address: {
city: "Malmö",
country: "Sweden"
}
});
db.users.where("email").startsWith("zlatan").distinct().each(function (user) {
console.log("Found user: " + user.name);
});
- Table "users" has:
- an auto-incremented primary key named id
- an index on the name property which could be of any type.
- a unique index on the username property
- a multi index on the email proptery, meaning that it allow multiple emails and the possibility to index each of them and find the single user object. NOTE! This feature lacks support in IE.
- an index on the netsted property 'address.city'
- Table "relations" doesnt have a "visible" primary key (however, it must have one autoincremented internally)
- Table "relations" has index on the userId1, userId2 and relation properties
- Table "relations" has a compound index of the properties userId1 and userId2 combined NOTE! This feature lacks support in IE.
Queries you could do with these indexes:
-
db.users.get(2)
will give you the user with id 2 -
db.users.where('name').startsWithIgnoreCase('da')
- will give you all users starting with "da" -
db.users.where('username').equals('usrname').first()
- will give you the user with username 'usrname' -
db.users.where('email').startsWith('david@').distinct()
- will give you the users that have any of their emails starting with 'david@' -
db.users.where('address.city').equalsIgnoreCase('malmö')
- will give you all users residing in Malmö. -
db.relations.where('userId1').equals(2)
- will give you all relations that user with id 2 has to other users -
db.relations.where('relation').anyOf('wife', 'husband', 'son', 'daughter')
- will give you all family relations. -
db.relations.where('userId1').equals(2).or('userId2').equals(2)
- will give you all relations that user with id 2 has to other users or other users have to user 2 -
db.relations.where('[userId1+userId2]').equals([2,3])
- will give you all the relations that user 1 has to user 2 -
db.relations.where('[userId1+userId2]').equals([2,3]).or('[userId1+userId2]').equals([3,2])
- will give you all the relations that user 1 has to user 2 or user 2 has to user 1.
Dexie.js - minimalistic and bullet proof indexedDB library