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

Commit 63677587 by Mick Hansen

more stuff

1 parent 74c6204a
...@@ -1186,7 +1186,9 @@ module.exports = (function() { ...@@ -1186,7 +1186,9 @@ module.exports = (function() {
var addDefaultAttributes = function() { var addDefaultAttributes = function() {
var self = this var self = this
, defaultAttributes = { , head = {}
, tail = {}
, head = {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false, allowNull: false,
...@@ -1196,18 +1198,29 @@ module.exports = (function() { ...@@ -1196,18 +1198,29 @@ module.exports = (function() {
} }
if (this.hasPrimaryKeys) { if (this.hasPrimaryKeys) {
defaultAttributes = {} head = {}
} }
if (this.options.timestamps) { if (this.options.timestamps) {
defaultAttributes[Utils._.underscoredIf(this.options.createdAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false} tail[Utils._.underscoredIf(this.options.createdAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false}
defaultAttributes[Utils._.underscoredIf(this.options.updatedAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false} tail[Utils._.underscoredIf(this.options.updatedAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false}
if (this.options.paranoid) if (this.options.paranoid)
defaultAttributes[Utils._.underscoredIf(this.options.deletedAt, this.options.underscored)] = {type: DataTypes.DATE} tail[Utils._.underscoredIf(this.options.deletedAt, this.options.underscored)] = {type: DataTypes.DATE}
} }
Utils._.each(defaultAttributes, function(value, attr) { var existingAttributes = Utils._.clone(self.rawAttributes)
self.rawAttributes = {}
Utils._.each(head, function(value, attr) {
self.rawAttributes[attr] = value
})
Utils._.each(existingAttributes, function(value, attr) {
self.rawAttributes[attr] = value
})
Utils._.each(tail, function(value, attr) {
if (Utils._.isUndefined(self.rawAttributes[attr])) { if (Utils._.isUndefined(self.rawAttributes[attr])) {
self.rawAttributes[attr] = value self.rawAttributes[attr] = value
} }
......
...@@ -42,7 +42,7 @@ module.exports = (function() { ...@@ -42,7 +42,7 @@ module.exports = (function() {
Object.defineProperty(DAO.prototype, 'values', { Object.defineProperty(DAO.prototype, 'values', {
get: function() { get: function() {
return this.dataValues return this.get()
} }
}) })
...@@ -88,11 +88,15 @@ module.exports = (function() { ...@@ -88,11 +88,15 @@ module.exports = (function() {
} }
DAO.prototype.get = function (key) { DAO.prototype.get = function (key) {
if (key) {
if (this._customGetters[key]) { if (this._customGetters[key]) {
return this._customGetters[key].call(this) return this._customGetters[key].call(this, key)
} }
return this.dataValues[key] return this.dataValues[key]
} }
return this.dataValues
}
DAO.prototype.set = function (key, value, options) { DAO.prototype.set = function (key, value, options) {
var values var values
, originalValue , originalValue
...@@ -126,7 +130,7 @@ module.exports = (function() { ...@@ -126,7 +130,7 @@ module.exports = (function() {
// If not raw, and there's a customer setter // If not raw, and there's a customer setter
if (!options.raw && this._customSetters[key]) { if (!options.raw && this._customSetters[key]) {
this._customSetters[key].call(this, value) this._customSetters[key].call(this, value, key)
} else { } else {
// Check if we have included models, and if this key matches the include model names/aliases // Check if we have included models, and if this key matches the include model names/aliases
if (this.options && this.options.include && this.options.includeNames.indexOf(key) !== -1) { if (this.options && this.options.include && this.options.includeNames.indexOf(key) !== -1) {
...@@ -606,7 +610,7 @@ module.exports = (function() { ...@@ -606,7 +610,7 @@ module.exports = (function() {
} }
DAO.prototype.toJSON = function() { DAO.prototype.toJSON = function() {
return this.dataValues; return this.get();
} }
// private // private
...@@ -651,6 +655,12 @@ module.exports = (function() { ...@@ -651,6 +655,12 @@ module.exports = (function() {
} }
} }
Utils._.each(this.attributes, function(key) {
if (!values.hasOwnProperty(key)) {
values[key] = undefined
}
})
this.set(values, options) this.set(values, options)
} }
......
...@@ -700,7 +700,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -700,7 +700,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it('properly handles disparate field lists', function(done) { it.only('properly handles disparate field lists', function(done) {
var self = this var self = this
, data = [{username: 'Peter', secretValue: '42' }, , data = [{username: 'Peter', secretValue: '42' },
{username: 'Paul'}, {username: 'Paul'},
...@@ -713,6 +713,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -713,6 +713,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect(users[0].secretValue).to.be.null expect(users[0].secretValue).to.be.null
done() done()
}) })
}).on('sql', function (sql) {
console.log(sql)
}) })
}) })
......
...@@ -365,9 +365,11 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -365,9 +365,11 @@ describe(Support.getTestDialectTeaser("Include"), function () {
ranks: function(callback) { ranks: function(callback) {
Rank.bulkCreate([ Rank.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1}, {name: 'Admin', canInvite: 1, canRemove: 1},
{name: 'Member', canInvite: 1} {name: 'Member', canInvite: 1, canRemove: 0}
]).done(function () { ]).done(function () {
Rank.findAll().done(callback) Rank.findAll().done(callback)
}).on('sql', function (sql) {
console.log(sql)
}) })
}, },
memberships: ['user', 'groups', 'ranks', function (callback, results) { memberships: ['user', 'groups', 'ranks', function (callback, results) {
...@@ -545,7 +547,7 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -545,7 +547,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
ranks: function(callback) { ranks: function(callback) {
Rank.bulkCreate([ Rank.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1}, {name: 'Admin', canInvite: 1, canRemove: 1},
{name: 'Member', canInvite: 1} {name: 'Member', canInvite: 1, canRemove: 0}
]).done(function () { ]).done(function () {
Rank.findAll().done(callback) Rank.findAll().done(callback)
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!