0

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.

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105

1 Answers1

1

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!

Robesz
  • 1,646
  • 11
  • 13
  • Thank you for the answer. We are enjoying JayData but we feel a little helpless when needed the docs to implement some features. We are using JayData models, they're working great, but only for data persistence. Because of our lack of experience and results on JayData usage, we chose to execute pure SQL queries directly on local database with JavaScript native functions. It's working for now and we'll keep JayData in hope to improve it usage in near future. The problem I have posted I already fixed, anyway, thank you for your attention. – DontVoteMeDown Aug 07 '13 at 17:31
  • could you please share examples to you native SQL queries? Does this article help you? - http://jaydata.org/blog/javascript-language-query-jslq-101 Our are considering publishing THE JayData book. Let us know know what you want to achieve, there are many documented features at not-so-easy-to-find places and undocumented features, too. You feedback could trigger an update on the old docs and creation of new ones – Robesz Aug 08 '13 at 14:22
  • Great, I'm glad to hear that! The are many community member who would be interested in you opinion. I've just seen you post in JayData forum, this is the best communication channel to collaborate with the community - you dont get stuck if I'm offline for a week :) – Robesz Aug 09 '13 at 14:47