0

How do you add a property to an entity dynamically? I've been looking, but haven't found anything.

For example, I have this model definition (I'm using the WebSQL provider):

$data.Entity.extend('$db.Types.Person', {
        id: { type: 'int', key: true, computed: true },
        name: { type: 'string' }
    });

$data.EntityContext.extend('$db.Types.DBContext', {
        Persons: { type: $data.EntitySet, elementType: $db.Types.Person},
    });

At some point I need to extend my model with new properties. Initially I don't know these properties' names.

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
Jose Alonso Monge
  • 1,014
  • 1
  • 16
  • 29

1 Answers1

1

The syntax is very simple for this, but the background info is more important, please read the whole answer before you reuse the snippet.

The YourType can be extended with new fields using the YourType.addMember() function. See this example snippet:

$data.Entity.extend('Product', {
  id: { type: 'int', key: true, computed: true },
  Name: { type: 'string' }
});

$data.EntityContext.extend('Northwind', {
  Products: { type: $data.EntitySet, elementType: Product},
});

Product.addMember('Description', {
    type:'string', 
    key: false, 
    computed: false, 
    required: false
});

var context = new Northwind({provider: 'webSql', databaseName: 'Northwind'});

context.onReady(function() {
    var product1 = new Product({ Name: 'Beer', Description: 'tasty'});
    context.Products.add(product1);
    context.saveChanges(function(result) {
        //check the content of WebSQL DB
        console.log(product1);
    });
});

You can user the addMember() only before creating an instance of the context.

Important info: There is no data migration/merge by in the library, and the default behavior on schema modification for webSql is to drop&re-create the DB. As IndexedDB isn't bound to a schema, the existing records won't be dropped. Make a try by running this code and adding more fields, here is a working JSFiddle.

The real solution is to use Schema Evolution module of JayData Pro to manage the changes in your data model.

Robesz
  • 1,646
  • 11
  • 13
  • At the time of adding properties, the context has already been created. Anyway, since I know the number of new properties, I solved my problem by adding p1...pN properties to the Person object (in the definition) and using a map table (which is taked from the server). – Jose Alonso Monge Aug 26 '13 at 07:17
  • In case use get the properties from the server, you can user $data.initService() to create a dynamic data model that is generated 100% based on the $metadata service on the server, don't you? - http://jaydata.org/blog/odata-cookbook-with-jaydata#h3_3 – Robesz Aug 27 '13 at 04:36
  • I'm not using OData, but anyway I'll glance at ti. Thx! – Jose Alonso Monge Aug 28 '13 at 06:26
  • There is quite a way to do what Jose needs. This is how schema evolutions work. what you need to do is to subclass entities during runtime from info taken from the user. then call the datacontext with this runtime type. – Peter Aron Zentai Aug 29 '13 at 16:35