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

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() {
association: true
}, options)
// passes the changed field to save, so only that field get updated.
return this.save(options)
}
......
......@@ -82,6 +82,10 @@ var DECIMAL = function() {
return DECIMAL.prototype.construct.apply(this, [DECIMAL].concat(Array.prototype.slice.apply(arguments)))
}
var VIRTUAL = function() {
};
FLOAT._type = FLOAT
FLOAT._typeName = 'FLOAT'
INTEGER._type = INTEGER
......@@ -287,6 +291,9 @@ module.exports = {
UUIDV1: 'UUIDV1',
UUIDV4: 'UUIDV4',
VIRTUAL: VIRTUAL,
NONE: VIRTUAL,
get ENUM() {
var result = function() {
return {
......
......@@ -355,7 +355,7 @@ module.exports = (function() {
if (factory.attributes.hasOwnProperty(name)) {
var definition = factory.attributes[name]
if (definition && (definition.indexOf('auto_increment') > -1)) {
if (definition && definition.autoIncrement) {
fields.push(name)
}
}
......
......@@ -502,7 +502,7 @@ module.exports = (function() {
for (var name in factory.attributes) {
var definition = factory.attributes[name]
if (definition && (definition.indexOf('SERIAL') > -1)) {
if (definition && definition.autoIncrement) {
fields.push(name)
}
}
......
......@@ -291,8 +291,7 @@ module.exports = (function() {
for (var name in factory.attributes) {
if (factory.attributes.hasOwnProperty(name)) {
var definition = factory.attributes[name]
if (definition && (definition.indexOf('INTEGER PRIMARY KEY AUTOINCREMENT') === 0)) {
if (definition && definition.autoIncrement) {
fields.push(name)
}
}
......
var Utils = require("./utils")
, Instance = require("./instance")
, Attribute = require("./model/attribute")
, DataTypes = require("./data-types")
, Util = require('util')
, sql = require('sql')
......@@ -63,16 +64,19 @@ module.exports = (function() {
}
// If you don't specify a valid data type lets help you debug it
Utils._.each(attributes, function(dataType, name) {
if (Utils.isHash(dataType)) {
Utils._.each(attributes, function(attribute, name) {
var dataType;
if (Utils.isHash(attribute)) {
// We have special cases where the type is an object containing
// the values (e.g. Sequelize.ENUM(value, value2) returns an object
// instead of a function)
// 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
dataType = dataType.type
dataType = attribute.type
} else {
dataType = attribute;
}
if (dataType === undefined) {
......@@ -80,6 +84,9 @@ module.exports = (function() {
}
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 || {
_checkEnum: function(value, next) {
var hasValue = value !== undefined
......@@ -112,6 +119,7 @@ module.exports = (function() {
this.options.hooks = this.replaceHookAliases(this.options.hooks)
this.attributes =
this.rawAttributes = attributes
this.modelManager =
this.daoFactoryManager = null // defined in init 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
* @property sequelize
* @return {Sequelize}
......@@ -224,7 +221,6 @@ module.exports = (function() {
// Add head and tail default attributes (id, timestamps)
addDefaultAttributes.call(this)
addOptionalClassMethods.call(this)
findAutoIncrementField.call(this)
// Primary key convenience variables
this.primaryKeyAttributes = Object.keys(this.primaryKeys)
......@@ -261,6 +257,7 @@ module.exports = (function() {
}
this.refreshAttributes();
findAutoIncrementField.call(this);
this._booleanAttributes = []
this._dateAttributes = []
......@@ -368,6 +365,8 @@ module.exports = (function() {
})
})
this.attributes = this.rawAttributes;
this.Instance.prototype._hasCustomGetters = Object.keys(this.Instance.prototype._customGetters).length
this.Instance.prototype._hasCustomSetters = Object.keys(this.Instance.prototype._customSetters).length
......@@ -1597,7 +1596,7 @@ module.exports = (function() {
})
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')
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types")
, Sequelize = require('../../index')
, Promise = Sequelize.Promise
, assert = require('assert')
chai.config.includeStack = true
......@@ -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 })
, Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING })
Task.belongsTo(User)
User.sync({ force: true }).success(function() {
Task.sync({ force: true }).success(function() {
User.create({ username: 'foo', gender: 'male' }).success(function(user) {
User.create({ username: 'bar', gender: 'female' }).success(function() {
Task.create({ title: 'task', status: 'inactive' }).success(function(task) {
task.setUserXYZ(user).success(function() {
task.getUserXYZ({where: ['gender = ?', 'female']}).success(function(user) {
expect(user).to.be.null
done()
})
})
})
})
})
})
})
return User.sync({ force: true }).then(function () {
// Can't use Promise.all cause of foreign key references
return Task.sync({ force: true });
}).then(function () {
return Promise.all([
User.create({ username: 'foo', gender: 'male' }),
User.create({ username: 'bar', gender: 'female' }),
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')
, Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING }).schema('archive')
, self = this
Task.belongsTo(User)
self.sequelize.dropAllSchemas().done(function() {
self.sequelize.createSchema('archive').done(function () {
self.sequelize.sync({force: true }).done(function () {
User.create({ username: 'foo', gender: 'male' }).success(function(user) {
Task.create({ title: 'task', status: 'inactive' }).success(function(task) {
task.setUserXYZ(user).success(function() {
task.getUserXYZ().success(function(user) {
expect(user).to.be.ok
done()
})
})
})
})
})
})
})
return self.sequelize.dropAllSchemas().then(function() {
return self.sequelize.createSchema('archive');
}).then(function () {
return self.sequelize.sync({force: true });
}).then(function () {
return Promise.all([
User.create({ username: 'foo', gender: 'male' }),
Task.create({ title: 'task', status: 'inactive' })
]);
}).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 () {
});
});
})
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!