Search code examples
javascripthtmlwebjaydata

Basic data storage with JayData


I try to implement my first JayData based application. I need my HTML5/JavaScript app to store complex data client-side (mostly one-to-many relations).

My model looks like this (sorry if the names are not very explicit):

enter image description here

I've tried to translate it with JayData, correct me if I'm wrong:

$data.Entity.extend("Test", {
    Id: { type: "int", key: true, computed: true },
    Name: { type: "string", required: true, maxLength: 200 },
    Chapters: { type: Array, elementType: "Chapter", inverseProperty: "Test" }
});

$data.Entity.extend("Chapter", {
    Id: { type: "int", key: true, computed: true },
    Name: { type: String, required: true, maxLength: 200 },
    Test: {type: "Test", inverseProperty: "Chapters" },
    Checks: { type: Array, elementType: "Check", inverseProperty: "Chapter" }
});

$data.Entity.extend("Check", {
    Id: { type: "int", key: true, computed: true },
    Name: { type: String, required: true, maxLength: 200 },
    Status: { type: "int", required: true },
    Chapter: { type: Chapter, inverseProperty: "Checks" }
});

$data.EntityContext.extend("CheckDb", {
    Tests: { type: $data.EntitySet, elementType: Test },
    Chapters: { type: $data.EntitySet, elementType: Chapter },
    Checks: { type: $data.EntitySet, elementType: Check }
});

EDIT

How to fill all tables? To start, I've tried:

var myDB = new CheckDb({ 
    name: 'local', databaseName: 'CheckDatabase'
});

myDB.onReady(function() {
    myDB.Tests.add({
        Name: "Test 1"
    });
    myDB.saveChanges();
});

But what if I want to add Chapters and Checks to my Test?

Thanks


Solution

  • Okay I've finally found it by myself:

    myDB.onReady(function() {
        var check = new Check({Name: "Check 1", Status: 1});
        var chapter = new Chapter({Name: "Chapter 1"});
        chapter.Checks = new Array();
        chapter.Checks.push(check);
    
        var myTest = myDB.Tests.add({
            Name: "Test 1",
            Chapters: [chapter]
        });
        myDB.saveChanges();
    });
    

    It wasn't that hard :)