In a web application when the code is changed a simple refresh is enough. In a mobile application you have to upgrade the app. When your data structure is changed then change management on the device can be tricky.

In a simple case you add a new field or table or remove one. Piece of cake. But when you rename a field or change its type then change can not be done automagically, it requires custom logic. As JayData provides an abstract interface to the developer a generic, provider independent and customizable solution to this problem.

tl;dr

The idea is that in your code you only have to define your current schema only. This of course puts some burden onto us as we have to find out when the schema has changed and also we have to store the previous schema so we can still access your data stored with the previous schema. We do this by storing the schemas in a local database and when you define your current schema we calculate a hash of it so we can find out when the schema has changed. If you want to avoid unnecessary schema upgrades then do not fiddle with the schema definition. The migration must be done in a provider independent way so you can use JayData’s JSQL or ItemStore API for it. The old and the new schema must exists parallel so the name of the local database is no longer is what you specified but its name has a suffix which is its version number. When you define your schema JayData creates global types for it so that you can easily instantiate your objects. During the migration you should be able to access your old schema and your new schema and that’d cause name collision therefore we have a new feature to define a type inside a container. This type-in-a-container feature is available in the free version, too, while schema migration feature is not.

The syntax has sligthly changed but of course you can use the old syntax if you do not want to use schema evolution feature.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

$data.defineDatabase(dbName,function(container){

varType1=$data.define("Type1",container,{

Name:String,

Address:String,

Tags:String

});

return$data.EntityContext.extend("MyContext",container,{

Type1s:{type:$data.EntitySet,elementType:Type1}

});

}).on("databaseCreated",function(newDB,options){

// you can initialize your database

// it only runs once

newDB.Type1s.addMany([{Name:'abc'},{Name:'def'}]);

returnnewDB.saveChanges();

}).on("schemaChanged",function(oldDb,newDB,options){

// simple migration, copy everything

returnoldDb.Type1.toArray()

.then(function(oldValues){

newDB.Type1.addMany(oldValues);

returnnewDB.saveChanges();

});

});

$data.openDatabase(dbName).then(function(db){

// db is your context ready to use

db.Type1.toArray()

.then(function(items){

…

});

}).fail(function(e){

alert("fail");

});

You can have many openDatabase call for the same database and you do not have to worry about databaseCreated ot schemaChanged events, then run only once and before any of the contexts get ready.

Get JayData

JayData on the Social Networks

Copyright JayStack Technologies co. Ltd. 2012.-2018. All Rights Reserved - Privacy policy
Non-JayStack logos/trademarks are copyright to their respective owners and do not constitute an endorsement of any JayStack products.