Search code examples
javascriptnode.jsinheritancemongoosebreeze

Using Breeze metadata helper how to construct inheritance on client side by hand


Using mongoose and Node.js i made a Base entity that is sub classed into a User entity and a Person entity, and both have a reference to a Family entity, my question is using the breeze-MetadataHelper how can i define that a family can have navigationProperties array that takes either a Person or a User entity . In short am losing OOP, so is there a way to construct inheritance with polymorphic support on the client with Breeze, this is what i have come up with.

   var addFamilyItemType = (): any => {
            var entityType = {
                name: this.entityNames.Family,
                dataProperties: {
                    id: {type: DT.MongoObjectId},
                    name: {type: DT.String, required: true, max: 50}
                },

                navigationProperties: {
                //this is where i want one navigation since they are a subclass
                // members: {type: [], hasMany: true}
                    users: {type: [this.entityNames.User], hasMany: true},
                    people: {type: [this.entityNames.Person], hasMany: true}
                }
            };

            helper.addTypeToStore(store, entityType);
            return entityType;
        };

  var addUserItemType = (): any => {
            var entityType = {
                name: this.entityNames.User,
                dataProperties: {
                    id: {type: DT.MongoObjectId},
                    email: {type: DT.String, required: true, max: 50},
                    username: {type: DT.String, required: true, max: 20},
                    password: {type: DT.String, 'default': ''}
                },
                navigationProperties: {
                    family: {
                        entityTypeName: this.entityNames.Family,
                        associationName: 'User_Family',
                        foreignKeyNames: ['familyId']
                    }
                }
            };

            helper.addTypeToStore(store, entityType);
            return entityType;
        };

 var addPersonItemType = (): any => {
            var entityType = {
                name: this.entityNames.Person,
                dataProperties: {
                    id: {type: DT.MongoObjectId},
                    email: {type: DT.String, max: 50},
                    profileImageURL: {type: DT.String,'default':''}                       
                },
                navigationProperties: {
                    family: {
                        entityTypeName: this.entityNames.Family,
                        associationName: 'Person_Family',
                        foreignKeyNames: ['familyId']
                    }
                }
            };

            helper.addTypeToStore(store, entityType);
            return entityType;
        };

Solution

  • The solution i found is to add a getter property called members to the FamilyModel object that will return the union of both arrays User and Person when calling registerEntityTypeCtor, i thought breeze supported inheritance out of the box.

    var metadataStore = this.metadata.createMetadataStore();
    var registerType = metadataStore.registerEntityTypeCtor.bind(metadataStore);
    
    registerType(this.metadata.entityNames.Family, FamilyModel);