Class moose.Table
Represents a table in a Database. This class is used to update, create, alter... tables.
The Class is typically used in migrations, or wrapped by a moose.Model.
Defined in: table.js.
Constructor Attributes | Constructor Name and Description |
---|---|
moose.Table(tableName, properties)
|
Field Attributes | Field Name and Description |
---|---|
valid alter table SQL for this table.
|
|
the columns contained in this table.
|
|
valid create table SQL for this table.
|
|
the schema of the table resides in.
|
|
valid drop table SQL for this table.
|
Method Attributes | Method Name and Description |
---|---|
addColumn(name, options)
Adds a column to this table.
|
|
Add a foreign key to this table, see moose.adapters.mysql.addForeignKey.
|
|
Replace the current primary key, see moose.adapters.mysql.addPrimaryKey.
|
|
Add a unique constraint to this table, see moose.adapters.mysql.addUnique.
|
|
column(name, options)
Add a column to this table.
|
|
dropColumn(name)
Drops a column from this table.
|
|
Drop a foreign key on this table, see moose.adapters.mysql.dropForeignKey.
|
|
Drop current primary key, see moose.adapters.mysql.dropPrimaryKey.
|
|
Drop a unique constraint on this table, see moose.adapters.mysql.dropUnique.
|
|
engine(engine)
Set the engine that the table should leverage.
|
|
foreignKey(name, options)
Adds a foreign key to this table, see moose.adapters.mysql.foreignKey
This is only valid on new tables. |
|
fromSql(name, value)
Convert an object or column and value from a sql value.
|
|
isInTable(columnName)
Takes a columnName and determines if it is in the table.
|
|
primaryKey(name)
Adds a primary key to this table, see moose.adapters.mysql.primaryKey
This is only valid on new tables. |
|
rename(newName)
Rename this table.
|
|
renameColumn(name, newName)
Renames a column contained in this table.
|
|
setAllowNull(name, allowNull)
Set if a column should allow null.
|
|
setColumnDefault(name, defaultvalue)
Set the default value of a column.
|
|
setColumnType(name, options)
Set a new type on a column contained in this table.
|
|
toSql(name, value)
|
|
unique(name)
Adds a unique constraint to this table, see moose.adapters.mysql.unique
This is only valid on new tables. |
|
validate(name, value)
Validate an object or columns and value against the columns in this table.
|
//To load preexisting Table use moose.loadSchema moose.loadSchema("employee").then(function(employee){}); //Table creation example var types = moose.adapters.mysql.types; var employee = new moose.Table("employee", { id : types.INT({allowNull : false, autoIncrement : true}), firstname : types.VARCHAR({length : 20, allowNull : false}), lastname : types.VARCHAR({length : 20, allowNull : false}), midinitial : types.CHAR({length : 1}), gender : types.ENUM({enums : ["M", "F"], allowNull : false}), street : types.VARCHAR({length : 50, allowNull : false}), city : types.VARCHAR({length : 20, allowNull : false}), primaryKey : "id" }); //or use through migration moose.createTable("employee", function(table) { table.column("id", types.INT({allowNull : false, autoIncrement : true})); table.column("firstname", types.VARCHAR({length : 20, allowNull : false})); table.column("lastname", types.VARCHAR({length : 20, allowNull : false})); table.column("midinitial", types.CHAR({length : 1})); table.column("gender", types.ENUM({enums : ["M", "F"], allowNull : false})); table.column("street", types.VARCHAR({length : 50, allowNull : false})); table.column("city", types.VARCHAR({length : 20, allowNull : false})); table.primaryKey("id"); }); //alter table examples employee.rename("employeeTwo"); employee.addColumn("age", types.INT()); employee.addUnique(["firstname", "midinitial"]); //use through migration moose.alterTable("company", function(table) { table.rename("companyNew"); table.addUnique("companyName"); table.addColumn("employeeCount", types.INT()); table.addUnique(["companyName", "employeeCount"]); }); //to drop a table use moose.dropTable moose.dropTable("company"); moose.dropTable("employee");
- Parameters:
- {String} tableName
- the name of the table
- {Object} properties
- properties to describe the table, all properties excpet for type, and primaryKey will be interpreted as columns each property should be a type described by the particular adapter (i.e moose.adapters.mysql.types).
- {String} properties.type Optional, Default: "mysql"
- the type of database the table resides in.
- {String|Array} properties.primaryKey
- the primary key of the table.
This is only valid on previously saved tables.
- Parameters:
- {String} name
- the name of the column to add.
- {moose.adapters.mysql.Type} options
- the type information of the column.
This is only valid on previously saved tables.
This is only valid on previously saved tables.
This is only valid on previously saved tables.
This is only valid on new tables.
- Parameters:
- {String} name
- the name of the column to be created.
- {moose.adapters.Type} options
- a type specific to the adpater that this table uses
This is only valid on previously saved tables.
- Parameters:
- {String} name
- the name of the column to drop.
This is only valid on previously saved tables.
This is only valid on previously saved tables.
This is only valid on previously saved tables.
Not all databases support this property
- Parameters:
- {String} engine
- the name of the engine.
This is only valid on new tables.
- Parameters:
- name
- options
- Parameters:
- {String|Object} name
- If the name is a string it is assumed to be the name of the column. If the name is an object it is assumed to be a an object consisting of {columnName : value}.
- {*} value
- if a string is the first argument, then the value is compared against the type of the column contained in this table.
- Returns:
- {String|Object} the javascript value/s.
- Parameters:
- {String} columnName
- the name of the column.
- Returns:
- {Boolean} true if the columnd is in the table.
This is only valid on new tables.
- Parameters:
- {Array | String} name
- the name or names to assign to a primary key.
This is only valid on previously saved tables.
- Parameters:
- {String} newName
- the new table name.
This is only valid on previously saved tables.
- Parameters:
- {String} name
- the name of the column to rename.
- {String} newName
- the new name of the column.
This is only valid on previously saved tables.
- Parameters:
- {String} name
- the name of the column to alter.
- {Boolean} allowNull
- true if null is allowed, false otherwise.
This is only valid on previously saved tables.
- Parameters:
- {String} name
- the name of the column to alter.
- {*} defaultvalue
- the value to set as the default of the column.
This is only valid on previously saved tables.
- Parameters:
- {String} name
- the name of the column to alter.
- {moose.adapters.mysql.Type} options
- the new type information of the column.
- Parameters:
- {String|Object} name
- If the name is a string it is assumed to be the name of the column. If the name is an object it is assumed to be a an object consisting of {columnName : value}.
- {*} value
- if a string is the first argument, then the value is compared against the type of the column contained in this table.
- Returns:
- {String|Object} the sql value/s.
This is only valid on new tables.
- Parameters:
- name
- Parameters:
- {String|Object} name
- If the name is a string it is assumed to be the name of the column. If the name is an object it is assumed to be a an object consiting of {columnName : value}.
- {*} value
- if a string is the first argument to validate is a string then the value is compared against the type of the column contained in this table.
- Returns:
- {Boolean} true if the column/s are valid.