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

Commit f11d1675 by Mick Hansen

refactor(model/attributes): model.attributes is no longer a SQL representation of rawAttributes

1 parent 882d8492
...@@ -101,6 +101,7 @@ module.exports = (function() { ...@@ -101,6 +101,7 @@ module.exports = (function() {
association: true association: true
}, options) }, options)
// passes the changed field to save, so only that field get updated. // passes the changed field to save, so only that field get updated.
return this.save(options) return this.save(options)
} }
......
...@@ -82,6 +82,10 @@ var DECIMAL = function() { ...@@ -82,6 +82,10 @@ var DECIMAL = function() {
return DECIMAL.prototype.construct.apply(this, [DECIMAL].concat(Array.prototype.slice.apply(arguments))) return DECIMAL.prototype.construct.apply(this, [DECIMAL].concat(Array.prototype.slice.apply(arguments)))
} }
var VIRTUAL = function() {
};
FLOAT._type = FLOAT FLOAT._type = FLOAT
FLOAT._typeName = 'FLOAT' FLOAT._typeName = 'FLOAT'
INTEGER._type = INTEGER INTEGER._type = INTEGER
...@@ -287,6 +291,9 @@ module.exports = { ...@@ -287,6 +291,9 @@ module.exports = {
UUIDV1: 'UUIDV1', UUIDV1: 'UUIDV1',
UUIDV4: 'UUIDV4', UUIDV4: 'UUIDV4',
VIRTUAL: VIRTUAL,
NONE: VIRTUAL,
get ENUM() { get ENUM() {
var result = function() { var result = function() {
return { return {
......
...@@ -355,7 +355,7 @@ module.exports = (function() { ...@@ -355,7 +355,7 @@ module.exports = (function() {
if (factory.attributes.hasOwnProperty(name)) { if (factory.attributes.hasOwnProperty(name)) {
var definition = factory.attributes[name] var definition = factory.attributes[name]
if (definition && (definition.indexOf('auto_increment') > -1)) { if (definition && definition.autoIncrement) {
fields.push(name) fields.push(name)
} }
} }
......
...@@ -502,7 +502,7 @@ module.exports = (function() { ...@@ -502,7 +502,7 @@ module.exports = (function() {
for (var name in factory.attributes) { for (var name in factory.attributes) {
var definition = factory.attributes[name] var definition = factory.attributes[name]
if (definition && (definition.indexOf('SERIAL') > -1)) { if (definition && definition.autoIncrement) {
fields.push(name) fields.push(name)
} }
} }
......
...@@ -291,8 +291,7 @@ module.exports = (function() { ...@@ -291,8 +291,7 @@ module.exports = (function() {
for (var name in factory.attributes) { for (var name in factory.attributes) {
if (factory.attributes.hasOwnProperty(name)) { if (factory.attributes.hasOwnProperty(name)) {
var definition = factory.attributes[name] var definition = factory.attributes[name]
if (definition && definition.autoIncrement) {
if (definition && (definition.indexOf('INTEGER PRIMARY KEY AUTOINCREMENT') === 0)) {
fields.push(name) fields.push(name)
} }
} }
......
...@@ -879,4 +879,4 @@ module.exports = (function() { ...@@ -879,4 +879,4 @@ module.exports = (function() {
} }
return Instance return Instance
})() })()
\ No newline at end of file
var Utils = require("./utils") var Utils = require("./utils")
, Instance = require("./instance") , Instance = require("./instance")
, Attribute = require("./model/attribute")
, DataTypes = require("./data-types") , DataTypes = require("./data-types")
, Util = require('util') , Util = require('util')
, sql = require('sql') , sql = require('sql')
...@@ -63,16 +64,19 @@ module.exports = (function() { ...@@ -63,16 +64,19 @@ module.exports = (function() {
} }
// If you don't specify a valid data type lets help you debug it // If you don't specify a valid data type lets help you debug it
Utils._.each(attributes, function(dataType, name) { Utils._.each(attributes, function(attribute, name) {
if (Utils.isHash(dataType)) { var dataType;
if (Utils.isHash(attribute)) {
// We have special cases where the type is an object containing // We have special cases where the type is an object containing
// the values (e.g. Sequelize.ENUM(value, value2) returns an object // the values (e.g. Sequelize.ENUM(value, value2) returns an object
// instead of a function) // instead of a function)
// Copy these values to the dataType // Copy these values to the dataType
dataType.values = (dataType.type && dataType.type.values) || dataType.values; attribute.values = (attribute.type && attribute.type.values) || attribute.values;
// We keep on working with the actual type object // We keep on working with the actual type object
dataType = dataType.type dataType = attribute.type
} else {
dataType = attribute;
} }
if (dataType === undefined) { if (dataType === undefined) {
...@@ -80,6 +84,9 @@ module.exports = (function() { ...@@ -80,6 +84,9 @@ module.exports = (function() {
} }
if (dataType.toString() === "ENUM") { if (dataType.toString() === "ENUM") {
if (!(Array.isArray(attribute.values) && (attribute.values.length > 0))) {
throw new Error('Values for ENUM haven\'t been defined.')
}
attributes[name].validate = attributes[name].validate || { attributes[name].validate = attributes[name].validate || {
_checkEnum: function(value, next) { _checkEnum: function(value, next) {
var hasValue = value !== undefined var hasValue = value !== undefined
...@@ -112,6 +119,7 @@ module.exports = (function() { ...@@ -112,6 +119,7 @@ module.exports = (function() {
this.options.hooks = this.replaceHookAliases(this.options.hooks) this.options.hooks = this.replaceHookAliases(this.options.hooks)
this.attributes =
this.rawAttributes = attributes this.rawAttributes = attributes
this.modelManager = this.modelManager =
this.daoFactoryManager = null // defined in init function this.daoFactoryManager = null // defined in init function
...@@ -120,17 +128,6 @@ module.exports = (function() { ...@@ -120,17 +128,6 @@ module.exports = (function() {
} }
/** /**
* Return a hash of the columns defined for this table. Keys are attributes, values are the SQL representation of their type
* @property attributes
* @return {Object}
*/
Object.defineProperty(Model.prototype, 'attributes', {
get: function() {
return this.QueryGenerator.attributesToSQL(this.rawAttributes)
}
})
/**
* A reference to the sequelize instance * A reference to the sequelize instance
* @property sequelize * @property sequelize
* @return {Sequelize} * @return {Sequelize}
...@@ -224,7 +221,6 @@ module.exports = (function() { ...@@ -224,7 +221,6 @@ module.exports = (function() {
// Add head and tail default attributes (id, timestamps) // Add head and tail default attributes (id, timestamps)
addDefaultAttributes.call(this) addDefaultAttributes.call(this)
addOptionalClassMethods.call(this) addOptionalClassMethods.call(this)
findAutoIncrementField.call(this)
// Primary key convenience variables // Primary key convenience variables
this.primaryKeyAttributes = Object.keys(this.primaryKeys) this.primaryKeyAttributes = Object.keys(this.primaryKeys)
...@@ -261,6 +257,7 @@ module.exports = (function() { ...@@ -261,6 +257,7 @@ module.exports = (function() {
} }
this.refreshAttributes(); this.refreshAttributes();
findAutoIncrementField.call(this);
this._booleanAttributes = [] this._booleanAttributes = []
this._dateAttributes = [] this._dateAttributes = []
...@@ -367,6 +364,8 @@ module.exports = (function() { ...@@ -367,6 +364,8 @@ module.exports = (function() {
attributeManipulation[name][type] = fct attributeManipulation[name][type] = fct
}) })
}) })
this.attributes = this.rawAttributes;
this.Instance.prototype._hasCustomGetters = Object.keys(this.Instance.prototype._customGetters).length this.Instance.prototype._hasCustomGetters = Object.keys(this.Instance.prototype._customGetters).length
this.Instance.prototype._hasCustomSetters = Object.keys(this.Instance.prototype._customSetters).length this.Instance.prototype._hasCustomSetters = Object.keys(this.Instance.prototype._customSetters).length
...@@ -1597,7 +1596,7 @@ module.exports = (function() { ...@@ -1597,7 +1596,7 @@ module.exports = (function() {
}) })
if (!Object.keys(this.primaryKeys).length) { if (!Object.keys(this.primaryKeys).length) {
self.primaryKeys['id'] = self.attributes['id'] self.primaryKeys.id = self.rawAttributes.id
} }
} }
......
module.exports = (function () {
var Attribute = function(options) {
if (options.type === undefined) options = {type: options};
this.type = options.type;
};
return Attribute;
})();
\ No newline at end of file
...@@ -4,6 +4,7 @@ var chai = require('chai') ...@@ -4,6 +4,7 @@ var chai = require('chai')
, Support = require(__dirname + '/../support') , Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types") , DataTypes = require(__dirname + "/../../lib/data-types")
, Sequelize = require('../../index') , Sequelize = require('../../index')
, Promise = Sequelize.Promise
, assert = require('assert') , assert = require('assert')
chai.config.includeStack = true chai.config.includeStack = true
...@@ -54,52 +55,53 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() { ...@@ -54,52 +55,53 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
}) })
}) })
it('should be able to handle a where object that\'s a first class citizen.', function(done) { it('should be able to handle a where object that\'s a first class citizen.', function() {
var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING, gender: Sequelize.STRING }) var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING, gender: Sequelize.STRING })
, Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING }) , Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING })
Task.belongsTo(User) Task.belongsTo(User)
User.sync({ force: true }).success(function() {
Task.sync({ force: true }).success(function() { return User.sync({ force: true }).then(function () {
User.create({ username: 'foo', gender: 'male' }).success(function(user) { // Can't use Promise.all cause of foreign key references
User.create({ username: 'bar', gender: 'female' }).success(function() { return Task.sync({ force: true });
Task.create({ title: 'task', status: 'inactive' }).success(function(task) { }).then(function () {
task.setUserXYZ(user).success(function() { return Promise.all([
task.getUserXYZ({where: ['gender = ?', 'female']}).success(function(user) { User.create({ username: 'foo', gender: 'male' }),
expect(user).to.be.null User.create({ username: 'bar', gender: 'female' }),
done() Task.create({ title: 'task', status: 'inactive' })
}) ]);
}) }).spread(function (userA, userB, task) {
}) return task.setUserXYZ(userA).then(function () {
}) return task.getUserXYZ({where: ['gender = ?', 'female']});
}) });
}) }).then(function (user) {
}) expect(user).to.be.null;
});
}) })
it('supports schemas', function (done) { it('supports schemas', function () {
var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING, gender: Sequelize.STRING }).schema('archive') var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING, gender: Sequelize.STRING }).schema('archive')
, Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING }).schema('archive') , Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING }).schema('archive')
, self = this , self = this
Task.belongsTo(User) Task.belongsTo(User)
self.sequelize.dropAllSchemas().done(function() { return self.sequelize.dropAllSchemas().then(function() {
self.sequelize.createSchema('archive').done(function () { return self.sequelize.createSchema('archive');
self.sequelize.sync({force: true }).done(function () { }).then(function () {
User.create({ username: 'foo', gender: 'male' }).success(function(user) { return self.sequelize.sync({force: true });
Task.create({ title: 'task', status: 'inactive' }).success(function(task) { }).then(function () {
task.setUserXYZ(user).success(function() { return Promise.all([
task.getUserXYZ().success(function(user) { User.create({ username: 'foo', gender: 'male' }),
expect(user).to.be.ok Task.create({ title: 'task', status: 'inactive' })
done() ]);
}) }).spread(function (user, task) {
}) return task.setUserXYZ(user).then(function () {
}) return task.getUserXYZ();
}) });
}) }).then(function (user) {
}) expect(user).to.be.ok;
}) });
}) })
}) })
......
...@@ -160,5 +160,12 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -160,5 +160,12 @@ describe(Support.getTestDialectTeaser("Model"), function () {
}); });
}); });
}) })
describe('types', function () {
describe('VIRTUAL', function () {
it('should be ignored in create, updateAttributes and find');
it('should be ignored in bulkCreate and findAll');
})
})
}) })
}) })
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!