Class Index | File Index

## Classes

- _Association
- _global_
- Dataset
- ManyToMany
- ManyToOne
- MayToOne
- Migrations
- Model
- moose
- moose.adapters
- moose.adapters.client.Client
- moose.adapters.client.ConnectionPool
- moose.adapters.client.mysql.ConnectionPool
- moose.adapters.client.mysql.Query
- moose.adapters.client.mysql.TransactionQuery
- moose.adapters.mysql
- moose.adapters.mysql.types
- moose.plugins
- moose.plugins.AssociationPlugin
- moose.plugins.CachePlugin
- moose.plugins.QueryPlugin
- moose.plugins.TimeStampPlugin
- moose.Table
- OneToMany
- SQL
- Type

# 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.

Class Summary
Constructor Attributes | Constructor Name and Description
--- | ---
| moose.Table(tableName, properties)

Field Summary
Field Attributes | Field Name and Description
--- | ---
| alterTableSql valid alter table SQL for this table.
| columns the columns contained in this table.
| createTableSql valid create table SQL for this table.
| database the schema of the table resides in.
| dropTableSql valid drop table SQL for this table.

Method Summary
Method Attributes | Method Name and Description
--- | ---
| addColumn(name, options) Adds a column to this table.
| addForeignKey() Add a foreign key to this table, see moose.adapters.mysql.addForeignKey.
| addPrimaryKey() Replace the current primary key, see moose.adapters.mysql.addPrimaryKey.
| addUnique() 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.
| dropForeignKey() Drop a foreign key on this table, see moose.adapters.mysql.dropForeignKey.
| dropPrimaryKey() Drop current primary key, see moose.adapters.mysql.dropPrimaryKey.
| dropUnique() 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.

Class Detail
moose.Table(tableName, properties)

    //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.

Field Detail
{String} alterTableSql
valid alter table SQL for this table.

{Object} columns
the columns contained in this table.

{String} createTableSql
valid create table SQL for this table.

{String} database
the schema of the table resides in.

{String} dropTableSql
valid drop table SQL for this table.
Method Detail
addColumn(name, options)
Adds a column to this 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.

addForeignKey()
Add a foreign key to this table, see moose.adapters.mysql.addForeignKey.

This is only valid on previously saved tables.

addPrimaryKey()
Replace the current primary key, see moose.adapters.mysql.addPrimaryKey.

This is only valid on previously saved tables.

addUnique()
Add a unique constraint to this table, see moose.adapters.mysql.addUnique.

This is only valid on previously saved tables.

column(name, options)
Add a column to this table.

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

dropColumn(name)
Drops a column from this table.

This is only valid on previously saved tables.

Parameters:
{String} name
  the name of the column to drop.

dropForeignKey()
Drop a foreign key on this table, see moose.adapters.mysql.dropForeignKey.

This is only valid on previously saved tables.

dropPrimaryKey()
Drop current primary key, see moose.adapters.mysql.dropPrimaryKey.

This is only valid on previously saved tables.

dropUnique()
Drop a unique constraint on this table, see moose.adapters.mysql.dropUnique.

This is only valid on previously saved tables.

engine(engine)
Set the engine that the table should leverage.
Not all databases support this property

Parameters:
{String} engine
  the name of the engine.

foreignKey(name, options)
Adds a foreign key to this table, see moose.adapters.mysql.foreignKey

This is only valid on new tables.

Parameters:
name
options

{String|Object} fromSql(name, value)
Convert an object or column and value from a sql value.

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.

{Boolean} isInTable(columnName)
Takes a columnName and determines if it is in the table.

Parameters:
{String} columnName
  the name of the column.

Returns:
  {Boolean} true if the columnd 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.

Parameters:
{Array | String} name
  the name or names to assign to a primary key.

rename(newName)
Rename this table.

This is only valid on previously saved tables.

Parameters:
{String} newName
  the new table name.

renameColumn(name, newName)
Renames a column contained in this table.

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.

setAllowNull(name, allowNull)
Set if a column should allow null.

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.

setColumnDefault(name, defaultvalue)
Set the default value of a column.

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.

setColumnType(name, options)
Set a new type on a column contained in this table.

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.

{String|Object} toSql(name, value)

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.

unique(name)
Adds a unique constraint to this table, see moose.adapters.mysql.unique

This is only valid on new tables.

Parameters:
name

{Boolean} validate(name, value)
Validate an object or columns and value against the columns in this table.

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.

Documentation generated by JsDoc Toolkit 2.4.0 on Sat Jun 11 2011 03:16:40 GMT-0500 (CDT)