Question

How I create relations between entities on JayData?

This is my table schema:

$data.Entity.extend("OrdemServico", {
    Status: { type: String },
    SafAnoSafra: { type: "int" },
    LancObservacao: { type: String },
    LancDtPrevIni: { type: Date },
    LancDtPrevFim: { type: Date },
    LancData: { type: Date },
    CodSubprocesso: { type: "int" },
    CodProcesso: { type: "int" },
    CodOs: { type: "int" },
    CodFuncEmpr: { type: "int" },
    CodFuncAplic: { type: "int" },
    CodFuncApliEmpr: { type: "int" },
    CodFunc: { type: "int" },
    CodFrente: { type: "int" },
    CodEmpr: { type: "int" }
});

$data.Entity.extend("Local", {
    SafAnoSafra: { type: "int" },
    PerAreaOs: { type: "decimal" },
    IdDivi4: { type: "int" },
    CodOs: { type: "int" },
    CodEmpr: { type: "int" },
    CodDivi4: { type: "int" },
    CodDivi3: { type: "int" },
    CodDivi2: { type: "int" },
    CodDivi1: { type: "int" },
    AreaOs: { type: "decimal" },
    AreaLiquida: { type: "decimal" }
});

The relation is:

OrdemServico.SafAnoSafra -> Local.SafAnoSafra
OrdemServico.CodEmpr -> Local.CodEmpr
OrdemServico.CodOs -> Local.CodOs

After a lot of searches I have found something near this on the official JayData tutorials, but it still not so clear about it(at least to me) on this link. According to it, what I have to do to stablish a relation is something like this:

Locais: {type: "Array", elementType: "$org.types.Local", navigationProperty: "OrdemServico"} for OrdemServico entity...

OrdemServico: { type: "Array", elementType: "$org.types.OrdemServico", navigationProperty: "Local"} for Local entity.

That breaks my code and doesn't works. Don't know how to go any further.

Was it helpful?

Solution

Check out the code snippets on JayData main page - look for "Relations".

I explain the basics, to make it clear with an up-to-date example:

$data.Entity.extend("Todo", {
    Id: { type: "int", key: true, computed: true },
    Task: { type: String, required: true, maxLength: 200 },
    Person: { type: "Person", required: true, inverseProperty: "Todos"}
});

$data.Entity.extend("Person", {
    Id: { type: "int", key: true, computed: true },
    Name: { type: String, required: true, maxLength: 200 },
    Todos: { type: Array, elementType: Todo, inverseProperty: "Person" }
});

$data.EntityContext.extend("TodoDatabase", {
    Todos: { type: $data.EntitySet, elementType: Todo },
    People: { type: $data.EntitySet, elementType: Person }
});

Todo Entity: We define the Person navigation property as a reference field, its type is "Person" - will be declared later. The inverseProperty must be set in order let JayData to help you to find the other side of the relationship.

Person Entity: One person can have multiple todos, so we define a collection of Todos. The elementType defines the type of the items in the collection. You need the inverseProperty here, too.

Note: The navigationProperty was valid in early versions of JayData, it was renamed to inverseProperty after the feedback from the developer community. Unfortunately, this page wasn't updated...until now... Thank you for asking, let us know if yous still find confusing info, we really want to have the documentation clear and up-to-date, but we have many contents, we can do it only with your feedback. Thank you!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top