不要怂,就是干,撸起袖子干!

Commit 57073848 by Mick Hansen

chore(model): rename daoFactory to model

1 parent 7aebecad
...@@ -4,7 +4,7 @@ Notice: All 1.7.x changes are present in 2.0.x aswell ...@@ -4,7 +4,7 @@ Notice: All 1.7.x changes are present in 2.0.x aswell
#### Breaking changes #### Breaking changes
- Sequelize now returns promises instead of its custom event emitter from most calls. This affects methods that return multiple values (like `findOrCreate` or `findOrInitialize`). If your current callbacks do not accept the 2nd success parameter you might be seeing an array as the first param. Either use `.spread()` for these methods or add another argument to your callback: `.success(instance)` -> `.success(instance, created)`. - Sequelize now returns promises instead of its custom event emitter from most calls. This affects methods that return multiple values (like `findOrCreate` or `findOrInitialize`). If your current callbacks do not accept the 2nd success parameter you might be seeing an array as the first param. Either use `.spread()` for these methods or add another argument to your callback: `.success(instance)` -> `.success(instance, created)`.
- `.success()`/`.done()` and any other non promise methods are now deprecated (we will keep the codebase around for a few versions though). on('sql') persists for debugging purposes. - `.success()`/`.done()` and any other non promise methods are now deprecated (we will keep the codebase around for a few versions though). on('sql') persists for debugging purposes.
- Model association calls (belongsTo/hasOne/hasMany) are no longer chainable. - Model association calls (belongsTo/hasOne/hasMany) are no longer chainable. (this is to support being able to pass association references to include rather than model/as combinations)
# v2.0.0-dev11 # v2.0.0-dev11
### Caution: This release contains many changes and is highly experimental ### Caution: This release contains many changes and is highly experimental
......
...@@ -71,7 +71,7 @@ var Mixin = module.exports = function(){} ...@@ -71,7 +71,7 @@ var Mixin = module.exports = function(){}
* *
* All methods return an event emitter. * All methods return an event emitter.
* *
* @param {DAOFactory} target * @param {Model} target
* @param {object} [options] * @param {object} [options]
* @param {boolean} [options.hooks=false] Set to true to run before-/afterDestroy hooks when an associated model is deleted because of a cascade. For example if `User.hasOne(Profile, {onDelete: 'cascade', hooks:true})`, the before-/afterDestroy hooks for profile will be called when a user is deleted. Otherwise the profile will be deleted without invoking any hooks * @param {boolean} [options.hooks=false] Set to true to run before-/afterDestroy hooks when an associated model is deleted because of a cascade. For example if `User.hasOne(Profile, {onDelete: 'cascade', hooks:true})`, the before-/afterDestroy hooks for profile will be called when a user is deleted. Otherwise the profile will be deleted without invoking any hooks
* @param {string} [options.as] The alias of this model. If you create multiple associations between the same tables, you should provide an alias to be able to distinguish between them. If you provide an alias when creating the assocition, you should provide the same alias when eager loading and when getting assocated models. Defaults to the singularized version of target.name * @param {string} [options.as] The alias of this model. If you create multiple associations between the same tables, you should provide an alias to be able to distinguish between them. If you provide an alias when creating the assocition, you should provide the same alias when eager loading and when getting assocated models. Defaults to the singularized version of target.name
...@@ -80,14 +80,14 @@ var Mixin = module.exports = function(){} ...@@ -80,14 +80,14 @@ var Mixin = module.exports = function(){}
* @param {string} [options.onUpdate='CASCADE'] * @param {string} [options.onUpdate='CASCADE']
* @param {boolean} [options.constraints=true] Should on update and on delete constraints be enabled on the foreign key. * @param {boolean} [options.constraints=true] Should on update and on delete constraints be enabled on the foreign key.
*/ */
Mixin.hasOne = function(associatedDAOFactory, options) { Mixin.hasOne = function(associatedModel, options) {
// Since this is a mixin, we'll need a unique variable name for hooks (since DAOFactory will override our hooks option) // Since this is a mixin, we'll need a unique variable name for hooks (since Model will override our hooks option)
options = options || {} options = options || {}
options.hooks = options.hooks === undefined ? false : Boolean(options.hooks) options.hooks = options.hooks === undefined ? false : Boolean(options.hooks)
options.useHooks = options.hooks options.useHooks = options.hooks
// the id is in the foreign table // the id is in the foreign table
var association = new HasOne(this, associatedDAOFactory, Utils._.extend(options, this.options)) var association = new HasOne(this, associatedModel, Utils._.extend(options, this.options))
this.associations[association.associationAccessor] = association.injectAttributes() this.associations[association.associationAccessor] = association.injectAttributes()
association.injectGetter(this.DAO.prototype); association.injectGetter(this.DAO.prototype);
...@@ -110,7 +110,7 @@ Mixin.hasOne = function(associatedDAOFactory, options) { ...@@ -110,7 +110,7 @@ Mixin.hasOne = function(associatedDAOFactory, options) {
* *
* All methods return an event emitter. * All methods return an event emitter.
* *
* @param {DAOFactory} target * @param {Model} target
* @param {object} [options] * @param {object} [options]
* @param {boolean} [options.hooks=false] Set to true to run before-/afterDestroy hooks when an associated model is deleted because of a cascade. For example if `User.hasOne(Profile, {onDelete: 'cascade', hooks:true})`, the before-/afterDestroy hooks for profile will be called when a user is deleted. Otherwise the profile will be deleted without invoking any hooks * @param {boolean} [options.hooks=false] Set to true to run before-/afterDestroy hooks when an associated model is deleted because of a cascade. For example if `User.hasOne(Profile, {onDelete: 'cascade', hooks:true})`, the before-/afterDestroy hooks for profile will be called when a user is deleted. Otherwise the profile will be deleted without invoking any hooks
* @param {string} [options.as] The alias of this model. If you create multiple associations between the same tables, you should provide an alias to be able to distinguish between them. If you provide an alias when creating the assocition, you should provide the same alias when eager loading and when getting assocated models. Defaults to the singularized version of target.name * @param {string} [options.as] The alias of this model. If you create multiple associations between the same tables, you should provide an alias to be able to distinguish between them. If you provide an alias when creating the assocition, you should provide the same alias when eager loading and when getting assocated models. Defaults to the singularized version of target.name
...@@ -119,14 +119,14 @@ Mixin.hasOne = function(associatedDAOFactory, options) { ...@@ -119,14 +119,14 @@ Mixin.hasOne = function(associatedDAOFactory, options) {
* @param {string} [options.onUpdate='CASCADE'] * @param {string} [options.onUpdate='CASCADE']
* @param {boolean} [options.constraints=true] Should on update and on delete constraints be enabled on the foreign key. * @param {boolean} [options.constraints=true] Should on update and on delete constraints be enabled on the foreign key.
*/ */
Mixin.belongsTo = function(associatedDAOFactory, options) { Mixin.belongsTo = function(associatedModel, options) {
// Since this is a mixin, we'll need a unique variable name for hooks (since DAOFactory will override our hooks option) // Since this is a mixin, we'll need a unique variable name for hooks (since Model will override our hooks option)
options = options || {} options = options || {}
options.hooks = options.hooks === undefined ? false : Boolean(options.hooks) options.hooks = options.hooks === undefined ? false : Boolean(options.hooks)
options.useHooks = options.hooks options.useHooks = options.hooks
// the id is in this table // the id is in this table
var association = new BelongsTo(this, associatedDAOFactory, Utils._.extend(options, this.options)) var association = new BelongsTo(this, associatedModel, Utils._.extend(options, this.options))
this.associations[association.associationAccessor] = association.injectAttributes() this.associations[association.associationAccessor] = association.injectAttributes()
association.injectGetter(this.DAO.prototype) association.injectGetter(this.DAO.prototype)
...@@ -148,7 +148,7 @@ Mixin.belongsTo = function(associatedDAOFactory, options) { ...@@ -148,7 +148,7 @@ Mixin.belongsTo = function(associatedDAOFactory, options) {
* User.hasMany(Project) * User.hasMany(Project)
* Project.hasMany(User) * Project.hasMany(User)
* ``` * ```
* By default, the name of the join table will be source+target, so in this case projectsusers. This can be overridden by providing either a string or a DAOFactory as `through` in the options. * By default, the name of the join table will be source+target, so in this case projectsusers. This can be overridden by providing either a string or a Model as `through` in the options.
* The following methods are injected on the source: * The following methods are injected on the source:
* *
...@@ -193,18 +193,18 @@ Mixin.belongsTo = function(associatedDAOFactory, options) { ...@@ -193,18 +193,18 @@ Mixin.belongsTo = function(associatedDAOFactory, options) {
* }) * })
* ``` * ```
* *
* @param {DAOFactory} target * @param {Model} target
* @param {object} [options] * @param {object} [options]
* @param {boolean} [options.hooks=false] Set to true to run before-/afterDestroy hooks when an associated model is deleted because of a cascade. For example if `User.hasOne(Profile, {onDelete: 'cascade', hooks:true})`, the before-/afterDestroy hooks for profile will be called when a user is deleted. Otherwise the profile will be deleted without invoking any hooks * @param {boolean} [options.hooks=false] Set to true to run before-/afterDestroy hooks when an associated model is deleted because of a cascade. For example if `User.hasOne(Profile, {onDelete: 'cascade', hooks:true})`, the before-/afterDestroy hooks for profile will be called when a user is deleted. Otherwise the profile will be deleted without invoking any hooks
* @param {DAOFactory|string} [options.through] The name of the table that is used to join source and target in n:m associations. Can also be a sequelize model if you want to define the junction table yourself and add extra attributes to it. * @param {Model|string} [options.through] The name of the table that is used to join source and target in n:m associations. Can also be a sequelize model if you want to define the junction table yourself and add extra attributes to it.
* @param {string} [options.as] The alias of this model. If you create multiple associations between the same tables, you should provide an alias to be able to distinguish between them. If you provide an alias when creating the assocition, you should provide the same alias when eager loading and when getting assocated models. Defaults to the singularized version of target.name * @param {string} [options.as] The alias of this model. If you create multiple associations between the same tables, you should provide an alias to be able to distinguish between them. If you provide an alias when creating the assocition, you should provide the same alias when eager loading and when getting assocated models. Defaults to the singularized version of target.name
* @param {string} [options.foreignKey] The name of the foreign key in the source table. Defaults to the name of target + primary key of target * @param {string} [options.foreignKey] The name of the foreign key in the source table. Defaults to the name of target + primary key of target
* @param {string} [options.onDelete='SET NULL|CASCADE'] Cascade if this is a n:m, and set null if it is a 1:m * @param {string} [options.onDelete='SET NULL|CASCADE'] Cascade if this is a n:m, and set null if it is a 1:m
* @param {string} [options.onUpdate='CASCADE'] * @param {string} [options.onUpdate='CASCADE']
* @param {boolean} [options.constraints=true] Should on update and on delete constraints be enabled on the foreign key. * @param {boolean} [options.constraints=true] Should on update and on delete constraints be enabled on the foreign key.
*/ */
Mixin.hasMany = function(associatedDAOFactory, options) { Mixin.hasMany = function(associatedModel, options) {
// Since this is a mixin, we'll need a unique variable name for hooks (since DAOFactory will override our hooks option) // Since this is a mixin, we'll need a unique variable name for hooks (since Model will override our hooks option)
options = options || {} options = options || {}
options.hooks = options.hooks === undefined ? false : Boolean(options.hooks) options.hooks = options.hooks === undefined ? false : Boolean(options.hooks)
options.useHooks = options.hooks options.useHooks = options.hooks
...@@ -212,7 +212,7 @@ Mixin.hasMany = function(associatedDAOFactory, options) { ...@@ -212,7 +212,7 @@ Mixin.hasMany = function(associatedDAOFactory, options) {
options = Utils._.extend(options, Utils._.omit(this.options, ['hooks'])) options = Utils._.extend(options, Utils._.omit(this.options, ['hooks']))
// the id is in the foreign table or in a connecting table // the id is in the foreign table or in a connecting table
var association = new HasMany(this, associatedDAOFactory, options) var association = new HasMany(this, associatedModel, options)
this.associations[association.associationAccessor] = association.injectAttributes() this.associations[association.associationAccessor] = association.injectAttributes()
association.injectGetter(this.DAO.prototype) association.injectGetter(this.DAO.prototype)
......
...@@ -42,9 +42,9 @@ module.exports = (function() { ...@@ -42,9 +42,9 @@ module.exports = (function() {
/** /**
* Returns the Model the instance was created from. * Returns the Model the instance was created from.
* @see {DAOFactory} * @see {Model}
* @property Model * @property Model
* @return DAOFactory * @return Model
*/ */
initValues.call(this, values, options); initValues.call(this, values, options);
...@@ -59,7 +59,7 @@ module.exports = (function() { ...@@ -59,7 +59,7 @@ module.exports = (function() {
* @return {Sequelize} * @return {Sequelize}
*/ */
Object.defineProperty(DAO.prototype, 'sequelize', { Object.defineProperty(DAO.prototype, 'sequelize', {
get: function(){ return this.Model.daoFactoryManager.sequelize } get: function(){ return this.Model.modelManager.sequelize }
}) })
Object.defineProperty(DAO.prototype, 'QueryInterface', { Object.defineProperty(DAO.prototype, 'QueryInterface', {
...@@ -202,7 +202,7 @@ module.exports = (function() { ...@@ -202,7 +202,7 @@ module.exports = (function() {
* *
* Set can also be used to build instances for associations, if you have values for those. TODO - mick should probably write something here about how includes in set works - perhaps also even some tests? * Set can also be used to build instances for associations, if you have values for those. TODO - mick should probably write something here about how includes in set works - perhaps also even some tests?
* *
* @see {DAOFactory#find} for more information about includes * @see {Model#find} for more information about includes
* @param {String|Object} key * @param {String|Object} key
* @param {any} value * @param {any} value
* @param {Object} [options] * @param {Object} [options]
...@@ -456,7 +456,7 @@ module.exports = (function() { ...@@ -456,7 +456,7 @@ module.exports = (function() {
if (self.Model.rawAttributes.hasOwnProperty(attrName)) { if (self.Model.rawAttributes.hasOwnProperty(attrName)) {
var definition = self.Model.rawAttributes[attrName] var definition = self.Model.rawAttributes[attrName]
, isEnum = !!definition.type && (definition.type.toString() === DataTypes.ENUM.toString()) , isEnum = !!definition.type && (definition.type.toString() === DataTypes.ENUM.toString())
, isMySQL = ['mysql', 'mariadb'].indexOf(self.Model.daoFactoryManager.sequelize.options.dialect) !== -1 , isMySQL = ['mysql', 'mariadb'].indexOf(self.Model.modelManager.sequelize.options.dialect) !== -1
, ciCollation = !!self.Model.options.collate && self.Model.options.collate.match(/_ci$/i) , ciCollation = !!self.Model.options.collate && self.Model.options.collate.match(/_ci$/i)
, valueOutOfScope , valueOutOfScope
...@@ -563,8 +563,8 @@ module.exports = (function() { ...@@ -563,8 +563,8 @@ module.exports = (function() {
* This is different from doing a `find(DAO.id)`, because that would create and return a new instance. With this method, * This is different from doing a `find(DAO.id)`, because that would create and return a new instance. With this method,
* all references to the DAO are updated with the new data and no new objects are created. * all references to the DAO are updated with the new data and no new objects are created.
* *
* @see {DAOFactory#find} * @see {Model#find}
* @param {Object} [options] Options that are passed on to `DAOFactory.find` * @param {Object} [options] Options that are passed on to `Model.find`
* @return {Promise} * @return {Promise}
*/ */
DAO.prototype.reload = function(options) { DAO.prototype.reload = function(options) {
...@@ -718,7 +718,7 @@ module.exports = (function() { ...@@ -718,7 +718,7 @@ module.exports = (function() {
} }
if (updatedAtAttr && !values[updatedAtAttr]) { if (updatedAtAttr && !values[updatedAtAttr]) {
countOrOptions.attributes[updatedAtAttr] = Utils.now(this.Model.daoFactoryManager.sequelize.options.dialect) countOrOptions.attributes[updatedAtAttr] = Utils.now(this.Model.modelManager.sequelize.options.dialect)
} }
return this.QueryInterface.increment(this, this.QueryInterface.QueryGenerator.addSchema(this.Model.tableName, this.Model.options.schema), values, where, countOrOptions) return this.QueryInterface.increment(this, this.QueryInterface.QueryGenerator.addSchema(this.Model.tableName, this.Model.options.schema), values, where, countOrOptions)
......
var Utils = require("../../utils") var Utils = require("../../utils")
, SqlString = require("../../sql-string") , SqlString = require("../../sql-string")
, daoFactory = require("../../dao-factory") , daoFactory = require("../../model")
, _ = require('lodash') , _ = require('lodash')
module.exports = (function() { module.exports = (function() {
...@@ -591,7 +591,7 @@ module.exports = (function() { ...@@ -591,7 +591,7 @@ module.exports = (function() {
, query , query
, limit = options.limit , limit = options.limit
, mainQueryItems = [] , mainQueryItems = []
, mainAttributes = options.attributes , mainAttributes = options.attributes && options.attributes.slice(0)
, mainJoinQueries = [] , mainJoinQueries = []
// We'll use a subquery if we have hasMany associations and a limit and a filtered/required association // We'll use a subquery if we have hasMany associations and a limit and a filtered/required association
, subQuery = limit && (options.hasIncludeWhere || options.hasIncludeRequired || options.hasMultiAssociation) , subQuery = limit && (options.hasIncludeWhere || options.hasIncludeRequired || options.hasMultiAssociation)
......
var Toposort = require('toposort-class') var Toposort = require('toposort-class')
, DaoFactory = require('./dao-factory')
, _ = require('lodash') , _ = require('lodash')
module.exports = (function() { module.exports = (function() {
var DAOFactoryManager = function(sequelize) { var ModelManager = function(sequelize) {
this.daos = [] this.daos = []
this.sequelize = sequelize this.sequelize = sequelize
} }
DAOFactoryManager.prototype.addDAO = function(dao) { ModelManager.prototype.addDAO = function(dao) {
this.daos.push(dao) this.daos.push(dao)
return dao return dao
} }
DAOFactoryManager.prototype.removeDAO = function(dao) { ModelManager.prototype.removeDAO = function(dao) {
this.daos = this.daos.filter(function(_dao) { this.daos = this.daos.filter(function(_dao) {
return _dao.name != dao.name return _dao.name != dao.name
}) })
} }
DAOFactoryManager.prototype.getDAO = function(daoName, options) { ModelManager.prototype.getDAO = function(daoName, options) {
options = options || {} options = options || {}
options.attribute = options.attribute || 'name' options.attribute = options.attribute || 'name'
...@@ -31,7 +30,7 @@ module.exports = (function() { ...@@ -31,7 +30,7 @@ module.exports = (function() {
return !!dao ? dao[0] : null return !!dao ? dao[0] : null
} }
DAOFactoryManager.prototype.__defineGetter__('all', function() { ModelManager.prototype.__defineGetter__('all', function() {
return this.daos return this.daos
}) })
...@@ -40,7 +39,7 @@ module.exports = (function() { ...@@ -40,7 +39,7 @@ module.exports = (function() {
* take foreign key constraints into account so that dependencies are visited * take foreign key constraints into account so that dependencies are visited
* before dependents. * before dependents.
*/ */
DAOFactoryManager.prototype.forEachDAO = function(iterator, options) { ModelManager.prototype.forEachDAO = function(iterator, options) {
var daos = {} var daos = {}
, sorter = new Toposort() , sorter = new Toposort()
, sorted , sorted
...@@ -89,5 +88,5 @@ module.exports = (function() { ...@@ -89,5 +88,5 @@ module.exports = (function() {
}) })
} }
return DAOFactoryManager return ModelManager
})() })()
var url = require("url") var url = require("url")
, Path = require("path") , Path = require("path")
, Utils = require("./utils") , Utils = require("./utils")
, DAOFactory = require("./dao-factory") , Model = require("./model")
, DataTypes = require('./data-types') , DataTypes = require('./data-types')
, DAOFactoryManager = require("./dao-factory-manager") , ModelManager = require("./model-manager")
, QueryInterface = require("./query-interface") , QueryInterface = require("./query-interface")
, Transaction = require("./transaction") , Transaction = require("./transaction")
, TransactionManager = require('./transaction-manager') , TransactionManager = require('./transaction-manager')
...@@ -162,7 +162,7 @@ module.exports = (function() { ...@@ -162,7 +162,7 @@ module.exports = (function() {
} catch(err) { } catch(err) {
throw new Error("The dialect " + this.getDialect() + " is not supported.") throw new Error("The dialect " + this.getDialect() + " is not supported.")
} }
this.daoFactoryManager = new DAOFactoryManager(this) this.modelManager = this.daoFactoryManager = new ModelManager(this)
this.transactionManager = new TransactionManager(this) this.transactionManager = new TransactionManager(this)
this.importCache = {} this.importCache = {}
...@@ -191,7 +191,7 @@ module.exports = (function() { ...@@ -191,7 +191,7 @@ module.exports = (function() {
*/ */
Sequelize.prototype.Validator = Sequelize.Validator = require('validator') Sequelize.prototype.Validator = Sequelize.Validator = require('validator')
Sequelize.DAOFactory = Sequelize.Model = DAOFactory Sequelize.Model = Sequelize.Model = Model
for (var dataType in DataTypes) { for (var dataType in DataTypes) {
Sequelize[dataType] = DataTypes[dataType] Sequelize[dataType] = DataTypes[dataType]
...@@ -314,7 +314,7 @@ module.exports = (function() { ...@@ -314,7 +314,7 @@ module.exports = (function() {
* @param {Boolean} [attributes.column.primaryKey=false] * @param {Boolean} [attributes.column.primaryKey=false]
* @param {Boolean} [attributes.column.autoIncrement=false] * @param {Boolean} [attributes.column.autoIncrement=false]
* @param {String} [attributes.column.comment=null] * @param {String} [attributes.column.comment=null]
* @param {String|DAOFactory} [attributes.column.references] If this column references another table, provide it here as a DAOFactory, or a string * @param {String|Model} [attributes.column.references] If this column references another table, provide it here as a Model, or a string
* @param {String} [attributes.column.referencesKey='id'] The column of the foreign table that this column references * @param {String} [attributes.column.referencesKey='id'] The column of the foreign table that this column references
* @param {String} [attributes.column.onUpdate] What should happen when the referenced key is updated. One of CASCADE, RESTRICT, SET DEFAULT, SET NULL or NO ACTION * @param {String} [attributes.column.onUpdate] What should happen when the referenced key is updated. One of CASCADE, RESTRICT, SET DEFAULT, SET NULL or NO ACTION
* @param {String} [attributes.column.onDelete] What should happen when the referenced key is deleted. One of CASCADE, RESTRICT, SET DEFAULT, SET NULL or NO ACTION * @param {String} [attributes.column.onDelete] What should happen when the referenced key is deleted. One of CASCADE, RESTRICT, SET DEFAULT, SET NULL or NO ACTION
...@@ -324,7 +324,7 @@ module.exports = (function() { ...@@ -324,7 +324,7 @@ module.exports = (function() {
* @param {Object} [options] These options are merged with the default define options provided to the Sequelize constructor * @param {Object} [options] These options are merged with the default define options provided to the Sequelize constructor
* @param {Object} [options.defaultScope] Define the default search scope to use for this model. Scopes have the same form as the options passed to find / findAll * @param {Object} [options.defaultScope] Define the default search scope to use for this model. Scopes have the same form as the options passed to find / findAll
* @param {Object} [options.scopes] More scopes, defined in the same way as defaultScope above. See `DAOFactory.scope` for more information about how scopes are defined, and what you can do with them * @param {Object} [options.scopes] More scopes, defined in the same way as defaultScope above. See `Model.scope` for more information about how scopes are defined, and what you can do with them
* @param {Boolean} [options.omitNull] Don't persits null values. This means that all columns with null values will not be saved * @param {Boolean} [options.omitNull] Don't persits null values. This means that all columns with null values will not be saved
* @param {Boolean} [options.timestamps=true] Adds createdAt and updatedAt timestamps to the model. * @param {Boolean} [options.timestamps=true] Adds createdAt and updatedAt timestamps to the model.
* @param {Boolean} [options.paranoid=false] Calling `destroy` will not delete the model, but instead set a `deletedAt` timestamp if this is true. Needs `timestamps=true` to work * @param {Boolean} [options.paranoid=false] Calling `destroy` will not delete the model, but instead set a `deletedAt` timestamp if this is true. Needs `timestamps=true` to work
...@@ -337,7 +337,7 @@ module.exports = (function() { ...@@ -337,7 +337,7 @@ module.exports = (function() {
* @param {Object} [options.getterMethods] Provide getter functions that work like those defined per column. If you provide a getter method with the same name as a column, it will be used to access the value of that column. If you provide a name that does not match a column, this function will act as a virtual getter, that can fetch multiple other values * @param {Object} [options.getterMethods] Provide getter functions that work like those defined per column. If you provide a getter method with the same name as a column, it will be used to access the value of that column. If you provide a name that does not match a column, this function will act as a virtual getter, that can fetch multiple other values
* @param {Object} [options.setterMethods] Provide setter functions that work like those defined per column. If you provide a setter method with the same name as a column, it will be used to update the value of that column. If you provide a name that does not match a column, this function will act as a virtual setter, that can act on and set other values, but will not be persisted * @param {Object} [options.setterMethods] Provide setter functions that work like those defined per column. If you provide a setter method with the same name as a column, it will be used to update the value of that column. If you provide a name that does not match a column, this function will act as a virtual setter, that can act on and set other values, but will not be persisted
* @param {Object} [options.instanceMethods] Provide functions that are added to each instance (DAO) * @param {Object} [options.instanceMethods] Provide functions that are added to each instance (DAO)
* @param {Object} [options.classMethods] Provide functions that are added to the model (DAOFactory) * @param {Object} [options.classMethods] Provide functions that are added to the model (Model)
* @param {String} [options.schema='public'] * @param {String} [options.schema='public']
* @param {String} [options.engine] * @param {String} [options.engine]
* @param {String} [options.charset] * @param {String} [options.charset]
...@@ -346,7 +346,7 @@ module.exports = (function() { ...@@ -346,7 +346,7 @@ module.exports = (function() {
* @param {Object} [options.hooks] An object of hook function that are called before and after certain lifecycle events. The possible hooks are: beforeValidate, afterValidate, beforeBulkCreate, beforeBulkDestroy, beforeBulkUpdate, beforeCreate, beforeDestroy, beforeUpdate, afterCreate, afterDestroy, afterUpdate, afterBulkCreate, afterBulkDestory and afterBulkUpdate. See Hooks for more information about hook functions and their signatures. Each property can either be a function, or an array of functions. * @param {Object} [options.hooks] An object of hook function that are called before and after certain lifecycle events. The possible hooks are: beforeValidate, afterValidate, beforeBulkCreate, beforeBulkDestroy, beforeBulkUpdate, beforeCreate, beforeDestroy, beforeUpdate, afterCreate, afterDestroy, afterUpdate, afterBulkCreate, afterBulkDestory and afterBulkUpdate. See Hooks for more information about hook functions and their signatures. Each property can either be a function, or an array of functions.
* @param {Object} [options.validate] An object of model wide validations. Validations have access to all model values via `this`. If the validator function takes an argument, it is asumed to be async, and is called with a callback that accepts an optional error. * @param {Object} [options.validate] An object of model wide validations. Validations have access to all model values via `this`. If the validator function takes an argument, it is asumed to be async, and is called with a callback that accepts an optional error.
* *
* @return {DaoFactory} * @return {Model}
*/ */
Sequelize.prototype.define = function(daoName, attributes, options) { Sequelize.prototype.define = function(daoName, attributes, options) {
options = options || {} options = options || {}
...@@ -368,12 +368,12 @@ module.exports = (function() { ...@@ -368,12 +368,12 @@ module.exports = (function() {
// if you call "define" multiple times for the same daoName, do not clutter the factory // if you call "define" multiple times for the same daoName, do not clutter the factory
if(this.isDefined(daoName)) { if(this.isDefined(daoName)) {
this.daoFactoryManager.removeDAO(this.daoFactoryManager.getDAO(daoName)) this.modelManager.removeDAO(this.modelManager.getDAO(daoName))
} }
options.sequelize = this options.sequelize = this
var factory = new DAOFactory(daoName, attributes, options) var factory = new Model(daoName, attributes, options)
this.daoFactoryManager.addDAO(factory.init(this.daoFactoryManager)) this.modelManager.addDAO(factory.init(this.modelManager))
return factory return factory
} }
...@@ -383,14 +383,14 @@ module.exports = (function() { ...@@ -383,14 +383,14 @@ module.exports = (function() {
* *
* @param {String} daoName The name of a model defined with Sequelize.define * @param {String} daoName The name of a model defined with Sequelize.define
* @throws Will throw an error if the DAO is not define (that is, if sequelize#isDefined returns false) * @throws Will throw an error if the DAO is not define (that is, if sequelize#isDefined returns false)
* @return {DAOFactory} * @return {Model}
*/ */
Sequelize.prototype.model = function(daoName) { Sequelize.prototype.model = function(daoName) {
if(!this.isDefined(daoName)) { if(!this.isDefined(daoName)) {
throw new Error(daoName + ' has not been defined') throw new Error(daoName + ' has not been defined')
} }
return this.daoFactoryManager.getDAO(daoName) return this.modelManager.getDAO(daoName)
} }
/** /**
...@@ -400,7 +400,7 @@ module.exports = (function() { ...@@ -400,7 +400,7 @@ module.exports = (function() {
* @return {Boolean} * @return {Boolean}
*/ */
Sequelize.prototype.isDefined = function(daoName) { Sequelize.prototype.isDefined = function(daoName) {
var daos = this.daoFactoryManager.daos var daos = this.modelManager.daos
return (daos.filter(function(dao) { return dao.name === daoName }).length !== 0) return (daos.filter(function(dao) { return dao.name === daoName }).length !== 0)
} }
...@@ -411,7 +411,7 @@ module.exports = (function() { ...@@ -411,7 +411,7 @@ module.exports = (function() {
* *
* See https://github.com/sequelize/sequelize/blob/master/examples/using-multiple-model-files/Task.js for a short example of how to define your models in separate files so that they can be imported by sequelize.import * See https://github.com/sequelize/sequelize/blob/master/examples/using-multiple-model-files/Task.js for a short example of how to define your models in separate files so that they can be imported by sequelize.import
* @param {String} path The path to the file that holds the model you want to import. If the part is relative, it will be resolved relatively to the calling file * @param {String} path The path to the file that holds the model you want to import. If the part is relative, it will be resolved relatively to the calling file
* @return {DAOFactory} * @return {Model}
*/ */
Sequelize.prototype.import = function(path) { Sequelize.prototype.import = function(path) {
// is it a relative path? // is it a relative path?
...@@ -442,7 +442,7 @@ module.exports = (function() { ...@@ -442,7 +442,7 @@ module.exports = (function() {
* *
* @method query * @method query
* @param {String} sql * @param {String} sql
* @param {DAOFactory} [callee] If callee is provided, the selected data will be used to build an instance of the DAO represented by the factory. Equivalent to calling DAOFactory.build with the values provided by the query. * @param {Model} [callee] If callee is provided, the selected data will be used to build an instance of the DAO represented by the factory. Equivalent to calling Model.build with the values provided by the query.
* @param {Object} [options={}] Query options. * @param {Object} [options={}] Query options.
* @param {Boolean} [options.raw] If true, sequelize will not try to format the results of the query, or build an instance of a model from the result * @param {Boolean} [options.raw] If true, sequelize will not try to format the results of the query, or build an instance of a model from the result
* @param {Transaction} [options.transaction=null] The transaction that the query should be executed under * @param {Transaction} [options.transaction=null] The transaction that the query should be executed under
...@@ -450,7 +450,7 @@ module.exports = (function() { ...@@ -450,7 +450,7 @@ module.exports = (function() {
* @param {Object|Array} [replacements] Either an object of named parameter replacements in the format `:param` or an array of unnamed replacements to replace `?` in your SQL. * @param {Object|Array} [replacements] Either an object of named parameter replacements in the format `:param` or an array of unnamed replacements to replace `?` in your SQL.
* @return {EventEmitter} * @return {EventEmitter}
* *
* @see {DAOFactory#build} for more information about callee. * @see {Model#build} for more information about callee.
*/ */
Sequelize.prototype.query = function(sql, callee, options, replacements) { Sequelize.prototype.query = function(sql, callee, options, replacements) {
if (arguments.length === 4) { if (arguments.length === 4) {
...@@ -550,7 +550,7 @@ module.exports = (function() { ...@@ -550,7 +550,7 @@ module.exports = (function() {
chainer.add(this, 'drop', [options]) chainer.add(this, 'drop', [options])
} }
this.daoFactoryManager.forEachDAO(function(dao) { this.modelManager.forEachDAO(function(dao) {
if (dao) { if (dao) {
chainer.add(dao, 'sync', [options]) chainer.add(dao, 'sync', [options])
} else { } else {
...@@ -574,7 +574,7 @@ module.exports = (function() { ...@@ -574,7 +574,7 @@ module.exports = (function() {
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
var chainer = new Utils.QueryChainer() var chainer = new Utils.QueryChainer()
self.daoFactoryManager.forEachDAO(function(dao) { self.modelManager.forEachDAO(function(dao) {
if (dao) { if (dao) {
chainer.add(dao, 'drop', [options]) chainer.add(dao, 'drop', [options])
} }
...@@ -624,9 +624,9 @@ module.exports = (function() { ...@@ -624,9 +624,9 @@ module.exports = (function() {
* }) * })
* ``` * ```
* *
* @see {DAOFactory#find} * @see {Model#find}
* @see {DAOFactory#findAll} * @see {Model#findAll}
* @see {DAOFactory#define} * @see {Model#define}
* @see {Sequelize#col} * @see {Sequelize#col}
* @method fn * @method fn
* *
...@@ -686,7 +686,7 @@ module.exports = (function() { ...@@ -686,7 +686,7 @@ module.exports = (function() {
/** /**
* An AND query * An AND query
* @see {DAOFactory#find} * @see {Model#find}
* *
* @method and * @method and
* @param {String|Object} args Each argument will be joined by AND * @param {String|Object} args Each argument will be joined by AND
...@@ -699,7 +699,7 @@ module.exports = (function() { ...@@ -699,7 +699,7 @@ module.exports = (function() {
/** /**
* An OR query * An OR query
* @see {DAOFactory#find} * @see {Model#find}
* *
* @method or * @method or
* @param {String|Object} args Each argument will be joined by OR * @param {String|Object} args Each argument will be joined by OR
...@@ -712,7 +712,7 @@ module.exports = (function() { ...@@ -712,7 +712,7 @@ module.exports = (function() {
/* /*
* A way of specifying attr = condition. Mostly used internally * A way of specifying attr = condition. Mostly used internally
* @see {DAOFactory#find} * @see {Model#find}
* *
* @param {string} attr The attribute * @param {string} attr The attribute
* @param {String|Object} condition The condition. Can be both a simply type, or a further condition (`.or`, `.and`, `.literal` etc.) * @param {String|Object} condition The condition. Can be both a simply type, or a further condition (`.or`, `.and`, `.literal` etc.)
......
...@@ -293,7 +293,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -293,7 +293,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
var self = this var self = this
expect(function() { expect(function() {
self.Worker.find({ include: [ 1 ] }) self.Worker.find({ include: [ 1 ] })
}).to.throw(Error, 'Include unexpected. Element has to be either an instance of DAOFactory or an object.') }).to.throw(Error, 'Include unexpected. Element has to be either an instance of Model or an object.')
done() done()
}) })
...@@ -329,9 +329,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -329,9 +329,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
var self = Object.create(this) var self = Object.create(this)
self.Domain = self.sequelize.define('Domain', { ip: Sequelize.STRING }) self.Domain = self.sequelize.define('Domain', { ip: Sequelize.STRING })
self.Environment = self.sequelize.define('Environment', { name: Sequelize.STRING }) self.Environment = self.sequelize.define('Environment', { name: Sequelize.STRING })
self.Environment self.Environment.belongsTo(self.Domain, { as: 'PrivateDomain', foreignKey: 'privateDomainId' })
.belongsTo(self.Domain, { as: 'PrivateDomain', foreignKey: 'privateDomainId' }) self.Environment.belongsTo(self.Domain, { as: 'PublicDomain', foreignKey: 'publicDomainId' })
.belongsTo(self.Domain, { as: 'PublicDomain', foreignKey: 'publicDomainId' })
self.Domain.sync({ force: true }).success(function() { self.Domain.sync({ force: true }).success(function() {
self.Environment.sync({ force: true }).success(function() { self.Environment.sync({ force: true }).success(function() {
......
...@@ -545,7 +545,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -545,7 +545,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
var self = this var self = this
expect(function() { expect(function() {
self.Worker.all({ include: [ 1 ] }) self.Worker.all({ include: [ 1 ] })
}).to.throw(Error, 'Include unexpected. Element has to be either an instance of DAOFactory or an object.') }).to.throw(Error, 'Include unexpected. Element has to be either an instance of Model or an object.')
done() done()
}) })
......
...@@ -21,13 +21,11 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -21,13 +21,11 @@ describe(Support.getTestDialectTeaser("Include"), function () {
D = S.define('D', { name: DT.STRING(40) }, { paranoid: true }) D = S.define('D', { name: DT.STRING(40) }, { paranoid: true })
// Associations // Associations
A A.hasMany( B )
.hasMany( B )
B B.belongsTo( B )
.belongsTo( B ) B.belongsTo( D )
.belongsTo( D ) B.hasMany( C, {
.hasMany( C, {
through: 'BC', through: 'BC',
}) })
......
...@@ -28,11 +28,10 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -28,11 +28,10 @@ describe(Support.getTestDialectTeaser("Include"), function () {
// Associate them // Associate them
User.hasMany( SomeConnection, { foreignKey: 'u' }) User.hasMany( SomeConnection, { foreignKey: 'u' })
SomeConnection SomeConnection.belongsTo( User, { foreignKey: 'u' })
.belongsTo( User, { foreignKey: 'u' }) SomeConnection.belongsTo( A, { foreignKey: 'fk' })
.belongsTo( A, { foreignKey: 'fk' }) SomeConnection.belongsTo( B, { foreignKey: 'fk' })
.belongsTo( B, { foreignKey: 'fk' }) SomeConnection.belongsTo( C, { foreignKey: 'fk' })
.belongsTo( C, { foreignKey: 'fk' })
A.hasMany( SomeConnection, { foreignKey: 'fk' }) A.hasMany( SomeConnection, { foreignKey: 'fk' })
B.hasMany( SomeConnection, { foreignKey: 'fk' }) B.hasMany( SomeConnection, { foreignKey: 'fk' })
......
...@@ -20,21 +20,17 @@ describe(Support.getTestDialectTeaser("Paranoid"), function () { ...@@ -20,21 +20,17 @@ describe(Support.getTestDialectTeaser("Paranoid"), function () {
C = this.C = S.define( 'C', { name: DT.STRING }, { paranoid: true }), C = this.C = S.define( 'C', { name: DT.STRING }, { paranoid: true }),
D = this.D = S.define( 'D', { name: DT.STRING }, { paranoid: true }) D = this.D = S.define( 'D', { name: DT.STRING }, { paranoid: true })
A A.belongsTo( B )
.belongsTo( B ) A.hasMany( D )
.hasMany( D ) A.hasMany( C )
.hasMany( C )
B B.hasMany( A )
.hasMany( A ) B.hasMany( C )
.hasMany( C )
C C.belongsTo( A )
.belongsTo( A ) C.belongsTo( B )
.belongsTo( B )
D D.hasMany( A )
.hasMany( A )
S.sync({ force: true }).done(function ( err ) { S.sync({ force: true }).done(function ( err ) {
expect( err ).not.to.be.ok expect( err ).not.to.be.ok
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!