Class moose
Extends
Migrations.
A singleton class that acts as the entry point for all actions performed in moose.
Defined in: index.js.
Constructor Attributes | Constructor Name and Description |
---|---|
moose(options)
|
Field Attributes | Field Name and Description |
---|---|
the adapter moose is using.
|
|
the default database to use, this property can only be used after the conneciton has
initialized.
|
|
The type of database moose will be connecting to.
|
Method Attributes | Method Name and Description |
---|---|
addModel(table, database, options)
Adds a model to moose. |
|
Closes all connections to the database.
|
|
createConnection(options)
Initialize the connection information, and prepare moose to communicate with the DB.
|
|
execute(sql, database)
Execute raw SQL.
|
|
getConnection(autoClose, database)
Retrieves a connection to the database.
|
|
getDataset(tableName, database)
Creates a dataset to operate on a particular table.
|
|
getModel(tableName, database)
Retrieve an already created model.
|
|
getSchema(tableName, database)
Retrieve an already created table.
|
|
loadSchema(tableName, database)
Load a moose.Table to be used by a model or directly.
|
|
loadSchemas(tableNames, database)
Use to load a group of tables.
|
|
transaction(database)
Creates a context for performing database transactions.
|
- Methods borrowed from class Migrations:
- alterTable, createTable, dropTable, migrate
Field Detail
{moose.adapters}
adapter
the adapter moose is using. READ ONLY
{String}
database
the default database to use, this property can only be used after the conneciton has
initialized.
type
The type of database moose will be connecting to. Currently only mysql is supported.
Method Detail
{comb.Promise|Model}
addModel(table, database, options)
Adds a model to moose.
NOTE- If a moose.Table is the first parameter then the {moose.Model} is returned immediately
- If a table name is the first parameter then a {comb.Promise} is returned, and called back with the model once it is loaded
moose.addModel(yourTable, { plugins : [PLUGIN1, PLUGIN2, PLUGIN3] instance : { myInstanceMethod : funciton(){}, getters : { myProp : function(){ return prop; } }, setters : { myProp : function(val){ prop = val; } } }, static : { myStaticMethod : function(){ }, getters : { myStaticProp : function(){ return prop; } }, setters : { myStaticProp : function(val){ prop = val; } } }, pre : { save : function(){ } }, post : { save : function(){ } } }); //or moose.addModel("myTable", {}).then(function(model){ //do something });; //or moose.addModel("myTable", "myOtherDB").then(function(model){ //do something });
- Parameters:
- {String|moose.Table} table
- the table to be used as the base for this model. Factory for a new Model.
- {String} database Optional
- the database to retreive the table from, if not defined then the default database from moose#createConnection will be used
- {Object} options
- - Similar to comb.define with a few other conveniences
- {Array} options.plugins
- a list of plugins to enable on the model.
- {Object} options.pre
- an object containing key value pairs of events, and the corresponding callback.
{ pre : { save : funciton(){}, update : function(){}, load : function(){}, remove : function(){} } }
- {Object} options.post
- an object containing key value pairs of events, and the corresponding callback.
{ post : { save : funciton(){}, update : function(){}, load : function(){}, remove : function(){} } }
- Returns:
- {comb.Promise|Model} see description.
closeConnection()
Closes all connections to the database.
createConnection(options)
Initialize the connection information, and prepare moose to communicate with the DB.
All models, and schemas, and models that are created before this method has been called will be deferred.
moose.createConnection({ host : "127.0.0.1", port : 3306, type : "mysql", maxConnections : 1, minConnections : 1, user : "test", password : "testpass", database : 'test' });
- Parameters:
- {Object} options
- the options used to initialize the database connection.
- {String} options.type Optional, Default: "mysql"
- the type of database to communicate with.
{comb.Promise}
execute(sql, database)
Execute raw SQL.
moose.execute("select * from myTable"); moose.execute("select * from myTable", "myOtherDB");
- Parameters:
- sql
- the SQL to execute
- {String} database Optional
- the database to perform the query on, if not defined then the default database from moose#createConnection will be used
- Returns:
- {comb.Promise} a promise that will be called after the SQL execution completes.
{Query}
getConnection(autoClose, database)
Retrieves a connection to the database. Can be used to work with the database driver directly.
- Parameters:
- {Boolean} autoClose
- if set to true then a new connection will be created, otherwise a connection will be retrieved from a connection pool.
- {String} database Optional
- the database to perform the query on, if not defined then the default database from moose#createConnection will be used .
- Returns:
- {Query} a query object.
{Dataset}
getDataset(tableName, database)
Creates a dataset to operate on a particular table.
- Parameters:
- tableName
- the name of the table to perfrom operations on.
- {String} database Optional
- the database to perform the query on, if not defined then the default database from moose#createConnection will be used
- Returns:
- {Dataset} a dataset to operate on a particular table.
{moose.Model}
getModel(tableName, database)
Retrieve an already created model.
- Parameters:
- {String} tableName
- the name of the table the model wraps.
- {String} database Optional
- the database the model is part of. This typically is only used if the models table is in a database other than the default. then the default database from moose#createConnection will be used
- Returns:
- {moose.Model} return the model or null of it is not found.
{moose.Table}
getSchema(tableName, database)
Retrieve an already created table.
- Parameters:
- {String} tableName
- the name of the table
- {String} database Optional
- the database the table resides in. If database is not provided then the default database is assumed.
- Returns:
- {moose.Table} return the table or null of it is not found.
{comb.Promise}
loadSchema(tableName, database)
Load a moose.Table to be used by a model or directly.
This is typically called before, one creates a new model.
moose.loadSchema("testTable").then(function(schema){ moose.addModel(schema, ...); });
- Parameters:
- {String} tableName
- the name of the table to load
- {String} database Optional
- the database to retreive the table from, if not defined then the default database from moose#createConnection will be used
- Returns:
- {comb.Promise} A promise is called back with a table ready for use.
{comb.Promise}
loadSchemas(tableNames, database)
Use to load a group of tables.
//load from the default database moose.loadSchemas(["testTable", "testTable2", ....]).then(function(schema1, schema2,....){ moose.addModel(schema1, ...); moose.addModel(schema2, ...); }); //load schemas from a particular db moose.loadShemas(["table1", "table2"], "yourDb").then(function(table1, table2){ //do something... }); //load table from multiple databases moose.loadSchemas({db1 : ["table1","table2"], db2 : ["table3","table4"]}).then(function(table1,table2, table3,table4){ //do something.... });
- Parameters:
-
{Array
|Object} tableNames - If an array of strings is used they are assumed to be all from the same database.
- If an object is passed the key is assumed to be the database, and the value should be an array of strings
- {String} database Optional
- the database to retreive the table from, if not defined then the default database from moose#createConnection will be used
- Returns:
- {comb.Promise} A promise that is called back with the tables in the same order that they were contained in the array.
{TransactionQuery}
transaction(database)
Creates a context for performing database transactions.
When using a transaction be sure to call commit with finished!
var trans = moose.transaction(); Do lots of stuff . . . trans.commit();
- Parameters:
- {String} database Optional
- the database to perform the query on, if not defined then the default database from moose#createConnection will be used.
- Returns:
- {TransactionQuery} a transaction context.