Skip to content
This repository was archived by the owner on Feb 13, 2018. It is now read-only.

BASE.data.Edm

Jared Barnes edited this page Sep 9, 2015 · 16 revisions

Edm stands for Entity Data Model. This is the class that is responsible to hold all the information about the classes that will be persisted. It holds their data types like Integer, Double, String (VarChar), etc. The BASE.data.DataContext depends on a service and the service depends on an Edm. The DataContext actually retrieves the Edm from the service provided through the constructor.

Here is how you'll use the Edm.

BASE.require([
    "BASE.data.Edm"
], function(){

    var Edm = BASE.data.Edm;

    var Person = function(){
        this.id = null;
        this.firstName = null;
        this.lastName = null;
        this.dateOfBirth = null;
        this.hrAccount = null;
        this.addresses = [];
        this.permissions = [];
    };

    var HrAccount = function(){
        this.id = null;
        this.accountId = null;
        this.person = null;
        this.personId = null;
    };
    
    var Address = function(){
        this.id = null;
        this.street = null;
        this.city = null;
        this.state = null;
        this.zip = null;
        this.country = null;
        this.person = null;
        this.personId = null;
    };
   
    var Permission = function(){
        this.id = null;
        this.name = null;
        this.people = [];
    };  

    // Mapping class
    var PersonToPermission = function(){
        this.personId = null;
        this.permissionId = null;
    };

    var edm = new Edm();
    
    // Note that I didn't mention the complex properties like hrAccount etc. 
    // Complex properties are expressed in the relationships.
    edm.addModel({
        type: Person,
        collectionName: "people",
        properties: {
            id: {
                type: Integer,
                primaryKey: true,
                autoIncrement: true
            },
            firstName: {
                type: String
            },
            lastName: {
                type: String
            },
            dateOfBirth: {
                type: Date
            }
        }
    });

    edm.addModel({
        type: HrAccount,
        collectionName: "hrAccounts",
        properties: {
            id: {
                type: Integer,
                primaryKey: true,
                autoIncrement: true
            },
            accountId: {
                type: Integer
            },
            personId: {
                type: Integer
            }
        }
    });

    edm.addModel({
        type: Address,
        collectionName: "addresses",
        properties: {
            id: {
                type: Integer,
                primaryKey: true,
                autoIncrement: true
            },
            street: {
                type: String
            },
            city: {
                type: String
            },
            state: {
                type: String
            },
            zip: {
                type: String
            },
            country: {
                type: String
            },
            personId: {
                type: Integer
            }
        }
    });

    edm.addModel({
        type: Permission,
        collectionName: "permissions",
        properties: {
            id: {
                type: Integer,
                primaryKey: true,
                autoIncrement: true
            },
            name: {
                type: String
            }
        }
    });

    edm.addModel({
        type: PersonToPermission,
        collectionName: "personToPermissions",
        properties: {
            personId: {
                type: Integer
            },
            permissionId: {
                type: Integer
            }
        }
    });

    
    // Now add the relationships.
    edm.addOneToOne({
        type: Person,
        hasKey: "id",
        hasOne: "hrAccount",
        ofType: HrAccount,
        withKey: "id",
        withForeignKey: "personId",
        withOne: "person"
    });

    edm.addOneToMany({
        type: Person,
        hasKey: "id",
        hasMany: "addresses",
        ofType: Address,
        withKey: "id",
        withForeignKey: "personId",
        withOne: "person"
    });

    edm.addManyToMany({
        type: Person,
        hasKey: "id",
        hasForeignKey: "permissionId",
        hasMany: "permissions",
        ofType: Permission,
        withKey: "id",
        withForeignKey: "personId",
        withMany: "people",
        usingMappingType: PersonToPermission
    });

});

#Instance Methods

edm.addOneToOne

This allows you to add a one to one relationship.

edm.addOneToOne(relationship: object) --> undefined

edm.addOneToOne({
    Type: Person, // This should be a Class (function)
    hasKey: "id", // Primary key of Source
    hasOne: "hrAccount", // Name of Source's target property
    ofType: HrAccount, // This should be a Class (function)
    withKey: "id",  // Primary key of Target
    withForeignKey: "personId",  // Foreign key
    withOne: "person"  // Name of the Target's source property
});

edm.addOneToMany

This allows you to add a one to many relationship.

edm.addOneToMany(relationship: object) --> undefined

edm.addOneToMany({
    Type: Person, // This should be a Class (function)
    hasKey: "id", // Primary key of Source
    hasOne: "phoneNumbers", // Name of Source's target array
    ofType: PhoneNumber, // This should be a Class (function)
    withKey: "id",  // Primary key of Target
    withForeignKey: "personId",  // Foreign key
    withOne: "person"  // Name of the Target's source property
});

edm.addManyToMany

This allows you to add a many to many relationship.

edm.addManyToMany(relationship: object) --> undefined

edm.addManyToMany({
    Type: Person, // This should be a Class (function)
    hasKey: "id", // Primary key of Source
    hasMany: "permissions", // Name of Source's target array
    hasForeignKey: "permissionId" // Foreign key for mapping entity
    ofType: Permission, // This should be a Class (function)
    withKey: "id",  // Primary key of Target
    withForeignKey: "personId",  // Foreign key for mapping entity
    withMany: "people",  // Name of the Target's source property
    usingMappingType: PersonToPermission
});

edm.removeOneToOne

This allows you to remove a one to one relationship. You must use the same object that you added, because it saves all one to one relationships in an array.

edm.removeOneToOne(relationship: object) --> undefined

var relationship = {
    Type: Person, // This should be a Class (function)
    hasKey: "id", // Primary key of Source
    hasOne: "hrAccount", // Name of Source's target property
    ofType: HrAccount, // This should be a Class (function)
    withKey: "id",  // Primary key of Target
    withForeignKey: "personId",  // Foreign key
    withOne: "person"  // Name of the Target's source property
};

edm.addOneToOne(relationship);

edm.removeOneToOne(relationship);

edm.removeOneToMany

This allows you to remove a one to many relationship. You must use the same object that you added, because it saves all one to many relationships in an array.

edm.removeOneToMany(relationship: object) --> undefined

var relationship = {
    Type: Person, // This should be a Class (function)
    hasKey: "id", // Primary key of Source
    hasOne: "phoneNumbers", // Name of Source's target array
    ofType: PhoneNumber, // This should be a Class (function)
    withKey: "id",  // Primary key of Target
    withForeignKey: "personId",  // Foreign key 
    withOne: "person"  // Name of the Target's source property
};

edm.addOneToMany(relationship);
edm.removeOneToMany(relationship);

edm.removeManyToMany

This allows you to add a many to many relationship. You must use the same object that you added, because it saves all many to many relationships in an array.

edm.removeManyToMany(relationship: object) --> undefined

var relationship = {
    Type: Person, // This should be a Class (function)
    hasKey: "id", // Primary key of Source
    hasMany: "permissions", // Name of Source's target array
    hasForeignKey: "permissionId" // Foreign key for mapping entity
    ofType: Permission, // This should be a Class (function)
    withKey: "id",  // Primary key of Target
    withForeignKey: "personId",  // Foreign key for mapping entity
    withMany: "people",  // Name of the Target's source property
    usingMappingType: PersonToPermission // Mapping Class
};

edm.addManyToMany(relationship);
edm.removeManyToMany(relationship);

edm.getMappingTypes

This method returns a Hashmap. The key is the mapping Type, and the value is the the mapping Type as well.

edm.getMappingTypes() --> BASE.collections.Hashmap

var mappingTypes = edm.getMappingTypes().getValues();

edm.getAllModels

This method returns all models that are of that type. This includes all subclasses.

edm.getAllModels(Type: function) --> array

var models = edm.getAllModels(Person);

edm.getPrimaryKeyProperties(Type: function)

This method returns all the primary keys for a Type. This is useful when a type uses a multi-column primary key.

var keys = edm.getPrimaryKeyProperties(Person);

edm.getAllKeyProperties

This method returns all keys which include, foreign keys and primary keys.

edm.getAllKeyProperties(Type: function) --> array

var keys = edm.getAllKeyProperties(Person);

edm.addModel

This method allows you to add how a model is persisted. Javascript doesn't natively support types, so the Edm file includes more Types. For example, Integer is just a subclass of Number. This allows javascript to be able to understand what the Type is, as well as supply strictly typed systems to save the data appropriately.

Built-in Types

  • String (VarChar)
  • Number
  • Boolean
  • Date

Added Types

  • Integer : Number
  • Double : Number
  • Float : Number
  • Binary : Number
  • Decimal : Number
  • Byte : Number
  • DateTimeOffset : Date
  • Enum : Number
  • EnumFlag : Number

Other Added Types Without Subclassing Built-in Types

  • Location
edm.addModel({
    type: Person,
    collectionName: "people",
    properties: {
        id: {
            type: Integer,
            primaryKey: true,
            autoIncrement: true
        },
        firstName: {
            type: String
        },
        lastName: {
            type: String
        }
    }
});

Declaring Base Class so relationships and other model data is inherited too.

edm.addModel({
    type: Employee,
    baseType: Person,
    collectionName: "employees",
    properties: {
        employeeId: {
            type: Integer
        }
    }
});

Clone this wiki locally