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

Commit 9ebc53f5 by Mick Hansen

feat(attributes): select all attributes by default, simple case covered for defi…

…ning field names for attributes
1 parent 9ccd6864
......@@ -520,11 +520,18 @@ module.exports = (function() {
// the values hash
values = {}
options.fields.forEach(function(field) {
if (self.dataValues[field] !== undefined) {
values[field] = self.dataValues[field]
options.fields.forEach(function(attr) {
if (self.dataValues[attr] !== undefined) {
values[attr] = self.dataValues[attr]
}
// Field name mapping
if (self.Model.rawAttributes[attr].field) {
values[self.Model.rawAttributes[attr].field] = values[attr];
delete values[attr];
}
})
args[2] = values
return self.QueryInterface[query].apply(self.QueryInterface, args).catch(function(err) {
......
......@@ -653,7 +653,7 @@ module.exports = (function() {
tableNames[this.tableName] = true
options = optClone(options)
options = optClone(options || {})
if (typeof options === 'object') {
if (options.hasOwnProperty('include') && options.include) {
hasJoin = true
......@@ -672,6 +672,11 @@ module.exports = (function() {
this.options.whereCollection = options.where || null
}
if (options.attributes === undefined) {
options.attributes = Object.keys(this.rawAttributes);
}
options.attributes = mapFieldNames.call(this, options.attributes, this)
options = paranoidClause.call(this, options)
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
......@@ -770,6 +775,11 @@ module.exports = (function() {
}
}
if (options.attributes === undefined) {
options.attributes = Object.keys(this.rawAttributes);
}
options.attributes = mapFieldNames.call(this, options.attributes, this)
options = paranoidClause.call(this, options)
if (options.limit === undefined) {
options.limit = 1
......@@ -1526,6 +1536,17 @@ module.exports = (function() {
this.__sql = sql.setDialect(dialect === 'mariadb' ? 'mysql' : dialect)
}
var mapFieldNames = function(attributes, Model) {
attributes = attributes.map(function (attr) {
if (Model.rawAttributes[attr] && Model.rawAttributes[attr].field) {
return [Model.rawAttributes[attr].field, attr];
}
return attr;
});
return attributes
}
// private
var paranoidClause = function ( options ) {
......
......@@ -1695,7 +1695,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
self.Task.create({ id: 52, title: 'task' }).success(function(task) {
user.setTasks([task]).success(function() {
user.destroy().success(function() {
self.UserTasks.findAll({ where: { userId: user.id }}).success(function(usertasks) {
self.sequelize.model('tasksusers').findAll({ where: { userId: user.id }}).success(function(usertasks) {
expect(usertasks).to.have.length(0)
_done()
})
......@@ -1708,7 +1708,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
self.Task.create({ id: 42, title: 'kast' }).success(function(task) {
task.setUsers([user]).success(function() {
task.destroy().success(function() {
self.UserTasks.findAll({ where: { taskId: task.id }}).success(function(usertasks) {
self.sequelize.model('tasksusers').findAll({ where: { taskId: task.id }}).success(function(usertasks) {
expect(usertasks).to.have.length(0)
_done()
})
......@@ -1774,7 +1774,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
self.Task.create({ id: 42, title: 'kast' }).success(function(task) {
task.setUsers([user]).success(function() {
task.destroy().success(function () {
self.UserTasks.findAll({ where: { taskId: task.id }}).success(function(usertasks) {
self.sequelize.model('tasksusers').findAll({ where: { taskId: task.id }}).success(function(usertasks) {
// This should not exist because deletes cascade
expect(usertasks).to.have.length(0)
_done()
......@@ -1798,7 +1798,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
self.Task.create({ id: 52, title: 'task' }).success(function(task) {
user.setTasks([task]).success(function() {
user.destroy().success(function () {
self.UserTasks.findAll({ where: { userId: user.id }}).success(function(usertasks) {
self.sequelize.model('tasksusers').findAll({ where: { userId: user.id }}).success(function(usertasks) {
// When we're not using foreign keys, join table rows will be orphaned
expect(usertasks).to.have.length(1)
_done()
......@@ -1812,7 +1812,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
self.Task.create({ id: 42, title: 'kast' }).success(function(task) {
task.setUsers([user]).success(function() {
task.destroy().success(function () {
self.UserTasks.findAll({ where: { taskId: task.id }}).success(function(usertasks) {
self.sequelize.model('tasksusers').findAll({ where: { taskId: task.id }}).success(function(usertasks) {
// When we're not using foreign keys, join table rows will be orphaned
expect(usertasks).to.have.length(1)
_done()
......
......@@ -668,7 +668,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
{model: Tag}
],
order: [
['id', 'ASC'],
['Product.id', 'ASC'],
['Tags.id', 'ASC']
]
}).done(function (err, products) {
......@@ -1349,7 +1349,9 @@ describe(Support.getTestDialectTeaser("Include"), function () {
}}
]}
],
order: 'id ASC'
order: [
['User.id', 'ASC']
]
}).done(function (err, users) {
expect(err).not.to.be.ok
......@@ -1543,7 +1545,9 @@ describe(Support.getTestDialectTeaser("Include"), function () {
}}
],
limit: 6,
order: 'id ASC'
order: [
['Product.id', 'ASC']
]
}).done(function (err, products) {
expect(err).not.to.be.ok
expect(products.length).to.equal(6)
......@@ -1571,7 +1575,9 @@ describe(Support.getTestDialectTeaser("Include"), function () {
{model: self.models.Price}
],
limit: 10,
order: 'id ASC'
order: [
['Product.id', 'ASC']
]
}).done(function (err, products) {
expect(err).not.to.be.ok
expect(products.length).to.equal(10)
......
......@@ -680,7 +680,7 @@ describe(Support.getTestDialectTeaser("Includes with schemas"), function () {
{model: Tag}
],
order: [
['id', 'ASC'],
['Product.id', 'ASC'],
['Tags.id', 'ASC']
]
}).done(function (err, products) {
......@@ -1315,7 +1315,9 @@ describe(Support.getTestDialectTeaser("Includes with schemas"), function () {
}}
]}
],
order: 'id ASC'
order: [
['User.id', 'ASC']
]
}).done(function (err, users) {
expect(err).not.to.be.ok
......@@ -1419,7 +1421,6 @@ describe(Support.getTestDialectTeaser("Includes with schemas"), function () {
var self = this
this.fixtureA(function () {
self.models.Product.findAll({
include: [
{model: self.models.Company},
{model: self.models.Tag},
......@@ -1428,7 +1429,9 @@ describe(Support.getTestDialectTeaser("Includes with schemas"), function () {
}}
],
limit: 6,
order: 'id ASC'
order: [
['Product.id', 'ASC']
]
}).done(function (err, products) {
expect(err).not.to.be.ok
expect(products.length).to.equal(6)
......@@ -1456,7 +1459,9 @@ describe(Support.getTestDialectTeaser("Includes with schemas"), function () {
{model: self.models.Price}
],
limit: 10,
order: 'id ASC'
order: [
['Product.id', 'ASC']
]
}).done(function (err, products) {
expect(err).not.to.be.ok
expect(products.length).to.equal(10)
......
/* jshint camelcase: false */
/* jshint expr: true */
var chai = require('chai')
, Sequelize = require('../../index')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types")
, dialect = Support.getTestDialect()
, datetime = require('chai-datetime')
chai.use(datetime)
chai.config.includeStack = true
describe(Support.getTestDialectTeaser("Model"), function () {
describe('attributes', function () {
describe('field', function () {
it('should create and fetch with alternative field names from a simple model', function () {
var queryInterface = this.sequelize.getQueryInterface()
, User = this.sequelize.define('user', {
name: {
type: DataTypes.STRING,
field: 'full_name'
}
}, {
tableName: 'users',
timestamps: false
})
return queryInterface.createTable('users', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
full_name: {
type: DataTypes.STRING
}
}).then(function () {
return User.create({
name: 'Foobar'
});
}).then(function () {
return User.find({
limit: 1
});
}).then(function (user) {
expect(user.get('name')).to.equal('Foobar');
});
})
})
})
})
\ 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!