lib/components/sql/model.js
function DatabaseDbModel(name, tableName) {
  this.__name = name;
  this.hasRelationships = false;
  this.hasValidations = false;
  this.hasMethods = false;
  this.hasStatics = false;
  this.hasFields = false;
  this.fields = {};
  this.__hasMany = {};
  this.__hasOne = {};
  this.__belongsTo = {};
  this.__errors = {};
  this.__jsonFields = [];
  this.hasJsonFields = false;
  this.methods = {};
  this.statics = {};
  this.validations = {};
  this.options = {
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    deletedAt: false
  };
  if (typeof tableName === 'string') {
    this.options['tableName'] = tableName;
  } else {
    this.options['tableName'] = tableName;
  }
  this.instanceMethods();
}
function DatabaseDbModel(name, tableName) {
  this.__name = name;
  this.hasRelationships = false;
  this.hasValidations = false;
  this.hasMethods = false;
  this.hasStatics = false;
  this.hasFields = false;
  this.fields = {};
  this.__hasMany = {};
  this.__hasOne = {};
  this.__belongsTo = {};
  this.__errors = {};
  this.__jsonFields = [];
  this.hasJsonFields = false;
  this.methods = {};
  this.statics = {};
  this.validations = {};
  this.options = {
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    deletedAt: false
  };
  if (typeof tableName === 'string') {
    this.options['tableName'] = tableName;
  } else {
    this.options['tableName'] = tableName;
  }
  this.instanceMethods();
}
function DatabaseDbModel ( name , tableName ) { this . __name = name ; this . hasRelationships = false ; this . hasValidations = false ; this . hasMethods = false ; this . hasStatics = false ; this . hasFields = false ; this . fields = { } ; this . __hasMany = { } ; this . __hasOne = { } ; this . __belongsTo = { } ; this . __errors = { } ; this . __jsonFields = [ ] ; this . hasJsonFields = false ; this . methods = { } ; this . statics = { } ; this . validations = { } ; this . options = { timestamps : true , createdAt : 'created_at' , updatedAt : 'updated_at' , deletedAt : false } ; if ( typeof tableName === 'string' ) { this . options [ 'tableName' ] = tableName ; } else { this . options [ 'tableName' ] = tableName ; } this . instanceMethods ( ) ; }
This is the base model used by every crux model definition to register themselves in the crux sql component

@memberof crux.Database.Sql
@class Model
@param {String} name - the model definition's name
@param {String} tableName - the model's table name
@example
// current model file: models/user.js
module.exports = function(user, Seq, Db) {
// At this point, the table name is "user" but we can change that
user.tableName('users');
// The model's name is by default its file name. We can also change that
user.name('Users');
user
.field('id', Seq.PRIMARY) // primary int(11) auto_incremented
.field('name', Seq.STRING)
.field('age', Seq.INTEGER, {
allowNull: true,
defaultValue: null
});

// We can also manually decare indexes.
user.index('name');
// Having previously declared the model application, we can create a relationship to it
user
.hasMany('application', {
as: 'application',
foreignKey: 'application_id'
});
// We can also attach a method to our model INSTANCES.
user
.method('hello', function() {
console.log("Hello from %s", this.get('id');
})
// We can also attach a method to the MODEL object (as a static function).
// At this point, we need to use the Db (crux.Database.Sql) component to get the model name.
.static('read', function ReadUser(id) {
return crux.promise(function(resolve, reject) {
Db.getModel('user').find(id).then(function(user) {
if(!user) return reject(new Error('USER_NOT_FOUND'));
// We can also attach custom data to the model instance
user.data('someKey', 'someValue');
// And we can later on access it
var a = user.data('someKey'); // => "someValue"
resolve(user);
}).error(reject);
});
});
};
This is the base model used by every crux model definition to register themselves in the crux sql component