Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The example below creates the car model with the specified schema. The car models will be stored in Mobile Backend Services as CustomObjects. Since the autogen property was not set to false, API Builder automatically generates the pre-defined endpoints for the client to access the car models using the <SEVER_ADDRESS>/ api/car endpoints.

Code Block
languagejs
var Arrow = require('arrow');

var car = Arrow.createModel('car', {
    fields: {
        make:{type:String, description:'the make of a car '},
        model:{type:String, description:'the model of the car', required:true},
        year: {type:Number, description:'year the car was made', required:true},
        bluebook: {type:Number, description:'kelly bluebook value of the car', required:true},
        mileage: {type:Number, description:'current mileage of the car', required:true}
    },
    connector: 'appc.arrowdb'
});

module.exports = car;

...

Code Block
titlemodels/baseemp.js
languagejs
var Arrow = require('arrow');

var baseEmp = Arrow.Model.reduce('appc.mysql/employee','baseEmp',{
    fields: {
        fname: {type:String, description:'First name', required:true, name:'first_name'},
        lname: {type:String, description:'Last name', required:true, name:'last_name'},
        email: {type:String, description:'Email address', required:true, name:'email_address'}
    }
});

module.exports = baseEmp;

...

Code Block
titlemodels/fullemp.js
languagejs
var Arrow = require('arrow');

var fullEmp = Arrow.Model.extend('employee','fullEmp',{
    fields: {
        headquarters:{type:Boolean, custom:true, 
            get:function(val,key,model){
                return model.get('state') === 'CA';
            }
        }
    }
});

module.exports = fullEmp;

...

Code Block
titlemodels/employee_manager.js
languagejs
var Arrow = require('arrow');

var employee_manager = Arrow.createModel('employee_manager',{
    fields: {
        fname: {type:String, description:'First name', name:'fname', model:'employee'},
        manager: {type:String, description:'manager of employee', name: 'manager', model:'managers'}
    },
    connector: 'appc.composite',
    metadata: {
        left_join: {
            model: 'managers',
            join_properties: {
                employee_id: 'id'
            }
        }
    }
});

module.exports = employee_manager;
Code Block
titlemodels/employee.js
languagejs
var Arrow = require('arrow');

var employee = Arrow.Model.reduce('appc.mysql/employee','employee',{
    fields: {
        fname: {type:String, description:'First name', name:'first_name'}
    },
    connector: 'appc.mysql'
});

module.exports = employee;
Code Block
titlemodels/managers.js
languagejs
var Arrow = require('arrow');

var managers = Arrow.Model.reduce('appc.mysql/employee_manager','managers',{
    fields: {
        employee_id: { type: Number, description: 'Employee ID' },
        manager: {type:String, name:'manager_name', description:'manager name'}
    },
    connector: 'appc.mysql'
});

module.exports = managers;

...

The example below performs an inner join on the employeeemployee_manager and employee_habit models. Both the employee_manager and employee_habit employee_id properties will try to match the employee id property. The description of every habit which matches the employee ID will be listed in the habit property. 

Code Block
languagejs
collapsetrue
var Arrow = require('arrow');

// create a model from a mysql table
var employee_composite = Arrow.createModel('employee_composite',{
    fields: {
        fname: {type: String, description: 'First name', name: 'fname', model: 'employee'},
        manager: {type: String, description: 'Manager of employee', name: 'manager', model: 'employee_manager'},
        habits: {type: Array, description: 'Habits of employee', name: 'description', model: 'employee_habit'}
    },
    connector: 'appc.composite',
    metadata: {
        inner_join: [
            {
                model: 'employee_manager',
                join_properties: {
                    employee_id: 'id'
                }
            },
            {
                model:'employee_habit',
                multiple: true,
                join_properties:{
                    employee_id:'id'
                }
            }
        ]
    }
});

module.exports = employee_composite;

...

The following are the main interfaces most connectors support.

Code Block
languagejs
 // delete all records for a model
Model.deleteAll(callback);

// query a model.
Model.query(options, callback);

// find all records for a model
Model.findAll(callback);

// find a record by id for a model
Model.findByID(id, callback);

// delete a record for a model
Model.delete(instance, callback);

// update a record
Model.update(instance, callback);

// create a record
Model.create(object, callback);

...

The following is an example of creating a record and then updating and deleting it. It’s not necessarily a practical example but demonstrates how to use some additional interfaces available on a model.

Code Block
languagejs
collapsetrue
// setup record object
var title = 'Test',
    content = 'Hello world',
    object = {
        title: title,
        content: content
    };

// create record then update then delete
Model.create(object, function(err, instance) {
    if (err) {
        // do something
    }

    // update instance
    instance.set('content', 'foo');

    // save instance
    instance.update(function(err, result){
        // logic here
    });

    // delete instance
    instance.delete(function(err,result){
        // logic here
    });
});

...

Code Block
titlemodels/employee.js
languagejs
var Arrow = require('arrow');

var employee = createModel('employee', {
    fields: {
        first_name: {type:String, description:'First name', required:true},
        last_name: {type:String, description:'Last name', required:true},
        email_address: {type:String, description:'Email address', required:true}
    },
    connector: 'memory',
    autogen: false
});

module.exports = employee;

...

Code Block
titleapis/employeefindById.js
languagejs
var Arrow = require('arrow');

var findEmployeeById = Arrow.API.extend({
    group: 'employeeAPIs',
    path: '/api/employee/:id',
    method: 'GET',
    description: 'This API finds one employee record',
    model: 'employee',
    parameters: {
        id: {description: 'the employee id'}
    },
    action: function (req, resp, next) {
        resp.stream(req.model.find, req.params.id, next);
    }
});

module.exports = findEmployeeById;