Dexie.version()

Syntax

db.version(versionNumber)

Parameters

versionNumber : NumberThe version to declare

Note the backend DB will get the given version number multiplied by 10. See Issue #59 where the reason for this behavior is described.

Return Value

Version

Sample

This sample shows how to upgrade your database. Assume your first app defines version 1 of the database like this:

var db = new Dexie("FriendsAndPetsDatabase");
db.version(1).stores({
    friends: "++id,name,age",
    pets: "++id,name,kind"
});
db.open(); 

// Do some application logic on the database:
db.transaction("rw", db.friends, db.pets, function () {
    db.friends.add({name: "David", age: 40, sex: "male"});
    db.friends.add({name: "Ylva", age: 39, sex: "female"});
    db.pets.add({name: "Josephina", kind: "dog"});

    db.friends.where("name").equalsIgnoreCase("david").each(function(friend) {
        console.log("Found friend: " + friend.name);
    });
    db.pets.where("kind").anyOf("dog", "cat").each(function(pet) {
        console.log("Found dog or cat: " + pet.name);
    });
}).catch (function (e) {
    console.error(e.stack || e);
});

Then you realise “age” was not a good thing to store because it will change as time goes by. You want to redesign your app to store birthdate instead. Here is how version 2 of your released app would look:

var db = new Dexie("FriendsAndPetsDatabase");
db.version(2).stores({
    friends: "++id,name,birthdate,sex",
    pets: "++id,name,kind"
}).upgrade (function () {
    var YEAR = 365 * 24 * 60 * 60 * 1000;
    db.friends.toCollection().modify (function (friend) {
        friend.birthdate = new Date(Date.now() - (friend.age * YEAR));
        delete friend.age;
    });
});
// Always keep the declarations previous versions
// as long as there might be users having them running.
db.version(1).stores({
    friends: "++id,name,age",
    pets: "++id,name,kind"
});
db.open();

See Also

Database Versioning

Version.stores()

Version.upgrade()

Table of Contents