1
votes

My goal is to persist a simple relationship between two breeze entities on my OData server.

I've setup a server, I am able to:

  • insert data
  • query data
  • create a relation between two entities client-side
  • BUT not able to persist the relationship server-side.

Any ideas of what I'm missing?

Below is my setup details


  • I'm using OData/MongoDB backend running JayData on Node.js (virtualBox, ubuntu 12.04, node.js, MongoDB)
  • I have created a simple data model with a user and a person entity based on this stack overflow question. This is the JayData datamodel definition

>

$data.Class.define("$org.Types.user", $data.Entity, null, {
    Id: {type: "id", key: true, computed: true, nullable: false },
    Person: { type: "$org.Types.person", inverseProperty: "User", required:true },
    EmailAddress: { type: "string"},
    Password: { type: "string"}
}, null)

$data.Class.define("$org.Types.person", $data.Entity, null, {
    Id: {type: "id", key: true, computed: true, nullable: false },
    User: { type: "$org.Types.user", inverseProperty: "Person"},
    FirstName: { type: "string"},
    LastName: { type: "string"}
}, null);


$data.Class.defineEx("$org.Types.Context", [$data.EntityContext, $data.ServiceBase], null, {
    User: {type: $data.EntitySet, elementType: $org.Types.user } ,
    Person: {type: $data.EntitySet, elementType: $org.Types.person }
});

exports = $org.Types.Context;
  • I'm able to query the data using Breeze.js
  • I'm able to insert User and Person entities using Breeze.js
  • I'm able to set a relation in Breeze and I see the changes to the entities
  • I've updated Breeze.js to 1.3.3

>

var manager = new breeze.EntityManager('api/');
// other breeze initialization stuff, metadata etc.

var person = manager.createEntity('Person', undefined, breeze.EntityState.Detached);
var user = manager.createEntity('User', undefined, breeze.EntityState.Detached);

// set other fields, name, email, password
user.Person(user);
manager.addEntity(user);
manager.addEntity(person);

// save the changes
manager.saveChanges().then(function() { // etc

But I only see two OData post for the two entities but nothing to tie the two entities together

  • I ruled out the OData adapter as both the WebAPI and OData adapter call the same functions in the Breeze code base and there are multiple Breeze/WebAPI that demonstrate the ability to query and inserted related entities.
  • I haven't seen a Breeze.js / OData sample that seems to address this.
  • The closest example that works is a datajs code sample. It posts the related entity to /$links/ to create the relationship (something I don't see in the OData adapter of neither Breeze.js or JayData).
1

1 Answers

0
votes

I've never used Breeze with JayData server, but I've been using JayData client library to save data over OData for a year and it works with static schema (you generate it with JaySvcUtil) and dynamic schema (grabs the metadata from the server in runtime) as well.ű

To important things:

  • use typed entites, not POCO objects
  • set the required side of the relationship to have the items persisted

    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://include.jaydata.org/datajs-1.0.3.min.js"></script>
    <script src="http://include.jaydata.org/jaydata.js"></script>
    <script>
            var url = 'http://localhost:54001/personService';
    
            $data.service(url, function(contextFactory, contextType){
                var db = contextFactory();
                db.onReady(function(){
    
                    var user = new db.User.elementType({ EmailAddress: '[email protected]'});
                    var person = new db.Person.elementType({ FistName: 'Joe', LastName: 'Smith', User: user }); //set the value to the required side of the relationship to have your entities saved in the batch
                    db.Person.add(person)
    
                    db.saveChanges(function () {
                        db.Person.include('User').toArray(function (p) { console.log(p);});
                    });
                })
            });
    

disclaimer: I'm member of the JayData dev team, so you can help you with the server-side, too.