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

Commit 1b222915 by Domas Lapinskas

Merge remote-tracking branch 'upstream/master'

2 parents fb4345c3 85838c2e
......@@ -324,7 +324,8 @@ module.exports = (function() {
DAOFactory.prototype.findAll = function(options, queryOptions) {
var hasJoin = false
var options = Utils._.clone(options)
options = optClone(options)
if (typeof options === 'object') {
if (options.hasOwnProperty('include')) {
......@@ -378,6 +379,7 @@ module.exports = (function() {
var primaryKeys = this.primaryKeys
, keys = Object.keys(primaryKeys)
, keysLength = keys.length
options = optClone(options)
// options is not a hash but an id
if (typeof options === 'number') {
......@@ -804,6 +806,13 @@ module.exports = (function() {
return attributes
}
var optClone = function (options) {
return Utils._.cloneDeep(options, function (elem) {
// The DAOFactories used for include are pass by ref, so don't clone them. Otherwise return undefined, meaning, 'handle this lodash'
return elem instanceof DAOFactory ? elem : undefined
})
}
Utils._.extend(DAOFactory.prototype, require("./associations/mixin"))
return DAOFactory
......
......@@ -402,7 +402,7 @@ module.exports = (function() {
return Utils._.template(query)(replacements)
},
deleteQuery: function(tableName, where, options) {
deleteQuery: function(tableName, where, options, factory) {
options = options || {}
if (options.truncate === true) {
......@@ -415,6 +415,10 @@ module.exports = (function() {
primaryKeys[tableName] = primaryKeys[tableName] || [];
if (!!factory && primaryKeys[tableName].length < 1) {
primaryKeys[tableName] = Object.keys(factory.primaryKeys)
}
var query = "DELETE FROM <%= table %> WHERE <%= primaryKeys %> IN (SELECT <%= primaryKeysSelection %> FROM <%= table %> WHERE <%= where %><%= limit %>)"
var pks;
......
......@@ -481,7 +481,7 @@ module.exports = (function() {
QueryInterface.prototype.delete = function(dao, tableName, identifier) {
var self = this
, restrict = false
, sql = self.QueryGenerator.deleteQuery(tableName, identifier)
, sql = self.QueryGenerator.deleteQuery(tableName, identifier, null, dao.daoFactory)
// Check for a restrict field
if (!!dao.daoFactory && !!dao.daoFactory.associations) {
......
......@@ -1426,14 +1426,14 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
var self = this
this.User.create({username: 'barfooz'}).success(function(user) {
self.UserPrimary = self.sequelize.define('UserPrimary', {
specialKey: {
specialkey: {
type: DataTypes.STRING,
primaryKey: true
}
})
self.UserPrimary.sync({force: true}).success(function() {
self.UserPrimary.create({specialKey: 'a string'}).success(function() {
self.UserPrimary.create({specialkey: 'a string'}).success(function() {
self.user = user
done()
})
......@@ -1441,9 +1441,18 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
it('does not modify the passed arguments', function (done) {
var options = { where: ['specialkey = ?', 'awesome']}
this.UserPrimary.find(options).success(function(user) {
expect(options).to.deep.equal({ where: ['specialkey = ?', 'awesome']})
done()
})
})
it('doesn\'t throw an error when entering in a non integer value for a specified primary field', function(done) {
this.UserPrimary.find('a string').success(function(user) {
expect(user.specialKey).to.equal('a string')
expect(user.specialkey).to.equal('a string')
done()
})
})
......@@ -2458,6 +2467,15 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
it('does not modify the passed arguments', function (done) {
var options = { where: ['username = ?', 'awesome']}
this.User.findAll(options).success(function(user) {
expect(options).to.deep.equal({ where: ['username = ?', 'awesome']})
done()
})
})
it("finds all users matching the passed conditions", function(done) {
this.User.findAll({where: "id != " + this.users[1].id}).success(function(users) {
expect(users.length).to.equal(1)
......
......@@ -1000,6 +1000,34 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
})
})
it('destroys a record with a primary key of something other than id', function(done) {
var UserDestroy = this.sequelize.define('UserDestroy', {
newId: {
type: DataTypes.STRING,
primaryKey: true
},
email: DataTypes.STRING
})
UserDestroy.sync().success(function() {
UserDestroy.create({newId: '123ABC', email: 'hello'}).success(function() {
UserDestroy.find({where: {email: 'hello'}}).success(function(user) {
user.destroy().on('sql', function(sql) {
if (dialect === "postgres" || dialect === "postgres-native") {
expect(sql).to.equal('DELETE FROM "UserDestroys" WHERE "newId" IN (SELECT "newId" FROM "UserDestroys" WHERE "newId"=\'123ABC\' LIMIT 1)')
}
else if (dialect === "mysql") {
expect(sql).to.equal("DELETE FROM `UserDestroys` WHERE `newId`='123ABC' LIMIT 1")
} else {
expect(sql).to.equal("DELETE FROM `UserDestroys` WHERE `newId`='123ABC'")
}
done()
})
})
})
})
})
it("sets deletedAt property to a specific date when deleting an instance", function(done) {
var self = this
this.ParanoidUser.create({ username: 'fnord' }).success(function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!