Search code examples
javascriptbreezesequelize.js

How to define Matrix/Link/Cross Table joins in BreezeJS Metadata?


I'm trying to get breeze working with hand generated Metadata, but I can't seem to find documentation covering navigation properties through matrix tables.

the data model is:

Organization
PK:OrganizationID

User
PK:UserID

The table that joins them is:

User_Organization
PK:UserOrganizationID
FK:OrganizationID
FK:UserID

When I retrieve an Organization I want it to have a property "users" which contains an array of User objects.

How do I define this and specify the matrix table in Breeze Metadata, seeing as User does not have an organizationID?

** update

My primary problem is that I'm linking Breeze to Sequelize, so I need to be able to manage this all through the metadata if possible. The first answer below from @Jeremy-Danyow solves the problem with client side code, but I'm looking for a way to present the final object graph to breeze as part of the metadata.


Solution

  • I think this question might be a duplicate of Many-to-many relations in Breeze. There is useful information in the answer there as well as the comments on the answer.

    That said, I want to propose a work-around for this part of your question:

    When I retrieve an Organization I want it to have a property "users" which contains an array of User objects.

    If you were to configure your metadata the supported way you could add a read-only "users" property to your organization entity like this:

    function Organization() { }
    
    Organization.prototype.users = function () {
        return this.userOrganizations().map(function(userOrganization) { return userOrganization.user() });
    };
    
    store.registerEntityTypeCtor('Organization', Organization);
    

    This uses the "Add methods to the constructor" approach documented here.


    Edit

    Sounds like breeze-sequelize support is in the works and will be released soon. See here and here. If you can afford to wait a little bit you'll have less friction getting this going.