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

Commit 5ecb1266 by Jan Aagaard Meier

Support for getting join table model data

1 parent 7639e69f
......@@ -12,6 +12,7 @@ module.exports = (function() {
var customEventEmitter = new Utils.CustomEventEmitter(function() {
var where = {}
, options = _options || {}
, queryOptions = {}
, association = self.__factory.target.associations[self.__factory.associationAccessor]
//fully qualify
......@@ -29,17 +30,30 @@ module.exports = (function() {
where[self.__factory.connectorDAO.tableName+"."+foreignKey] = {join: self.__factory.target.tableName+"."+foreignPrimary}
if (association.customJoinTableModel) {
options.attributes = []
queryOptions.hasJoinTableModel = true
queryOptions.joinTableModel = self.__factory.connectorDAO
Utils._.forOwn(self.__factory.connectorDAO.rawAttributes, function (elem, key) {
if (!(key in self.__factory.connectorDAO.primaryKeys)) {
options.attributes.push(self.__factory.target.QueryInterface.quoteIdentifier(self.__factory.connectorDAO.tableName) + '.' + key)
}
})
options.attributes.push(self.__factory.target.QueryInterface.quoteIdentifier(self.__factory.target.tableName)+".*")
if (!options.attributes) {
options.attributes = [
self.__factory.target.QueryInterface.quoteIdentifier(self.__factory.target.tableName)+".*"
]
}
console.log(options.attributes);
if (options.joinTableAttributes) {
options.joinTableAttributes.forEach(function (elem) {
options.attributes.push(
self.__factory.target.QueryInterface.quoteIdentifiers(self.__factory.connectorDAO.tableName + '.' + elem) + ' as ' +
self.__factory.target.QueryInterface.quoteIdentifier(self.__factory.connectorDAO.name + '.' + elem)
)
})
} else {
Utils._.forOwn(self.__factory.connectorDAO.rawAttributes, function (elem, key) {
options.attributes.push(
self.__factory.target.QueryInterface.quoteIdentifiers(self.__factory.connectorDAO.tableName + '.' + key) + ' as ' +
self.__factory.target.QueryInterface.quoteIdentifier(self.__factory.connectorDAO.name + '.' + key)
)
})
}
}
if (options.where) {
......@@ -60,7 +74,7 @@ module.exports = (function() {
options.where = where;
}
self.__factory.target.findAllJoin(self.__factory.connectorDAO.tableName, options)
self.__factory.target.findAllJoin(self.__factory.connectorDAO.tableName, options, queryOptions)
.on('success', function(objects) { customEventEmitter.emit('success', objects) })
.on('error', function(err){ customEventEmitter.emit('error', err) })
.on('sql', function(sql) { customEventEmitter.emit('sql', sql)})
......@@ -159,12 +173,9 @@ module.exports = (function() {
var sourceKeys = Object.keys(this.__factory.source.primaryKeys);
var targetKeys = Object.keys(this.__factory.target.primaryKeys);
<<<<<<< HEAD
attributes[this.__factory.identifier] = ((sourceKeys.length === 1) ? this.instance[sourceKeys[0]] : this.instance.id)
attributes[foreignIdentifier] = ((targetKeys.length === 1) ? newAssociation[targetKeys[0]] : newAssociation.id)
=======
>>>>>>> f20377983f85cc09a62ba96d1502d0172dfd366a
if (exists) { // implies customJoinTableModel === true
var where = attributes
attributes = Utils._.defaults({}, newAssociation[association.connectorDAO.name], additionalAttributes)
......
......@@ -346,14 +346,16 @@ module.exports = (function() {
}
//right now, the caller (has-many-double-linked) is in charge of the where clause
DAOFactory.prototype.findAllJoin = function(joinTableName, options) {
DAOFactory.prototype.findAllJoin = function(joinTableName, options, queryOptions) {
var optcpy = Utils._.clone(options)
optcpy.attributes = optcpy.attributes || [this.QueryInterface.quoteIdentifier(this.tableName)+".*"]
// whereCollection is used for non-primary key updates
this.options.whereCollection = optcpy.where || null;
return this.QueryInterface.select(this, [this.getTableName(), joinTableName], optcpy, { type: 'SELECT' })
return this.QueryInterface.select(this, [this.getTableName(), joinTableName], optcpy, Utils._.defaults({
type: 'SELECT'
}, queryOptions))
}
/**
......
......@@ -260,6 +260,21 @@ module.exports = (function() {
result = result.map(Dot.transform)
} else if (this.options.hasJoin === true) {
result = transformRowsWithEagerLoadingIntoDaos.call(this, results)
} else if (this.options.hasJoinTableModel === true) {
result = results.map(function(result) {
result = Dot.transform(result)
var joinTableData = result[this.options.joinTableModel.name]
, joinTableDAO = this.options.joinTableModel.build(joinTableData, { isNewRecord: false, isDirty: false })
, mainDao
delete result[this.options.joinTableModel.name]
mainDao = this.callee.build(result, { isNewRecord: false, isDirty: false })
mainDao[this.options.joinTableModel.name] = joinTableDAO
return mainDao
}.bind(this))
} else {
result = results.map(function(result) {
return this.callee.build(result, { isNewRecord: false, isDirty: false })
......
......@@ -569,19 +569,66 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
describe('join table model', function () {
describe('inserting in join table', function () {
beforeEach(function (done) {
this.User = this.sequelize.define('User', {})
this.Project = this.sequelize.define('Project', {})
this.UserProjects = this.sequelize.define('UserProjects', {
status: DataTypes.STRING
beforeEach(function (done) {
this.User = this.sequelize.define('User', {})
this.Project = this.sequelize.define('Project', {})
this.UserProjects = this.sequelize.define('UserProjects', {
status: DataTypes.STRING,
data: DataTypes.INTEGER
})
this.User.hasMany(this.Project, { joinTableModel: this.UserProjects })
this.Project.hasMany(this.User, { joinTableModel: this.UserProjects })
this.sequelize.sync().success(function() { done() })
})
describe('fetching from join table', function () {
it('should contain the data from the join table on .UserProjects a DAO', function (done) {
var self = this
self.User.create().success(function (u) {
self.Project.create().success(function (p) {
u.addProject(p, { status: 'active', data: 42 }).success(function() {
u.getProjects().success(function(projects) {
var project = projects[0]
expect(project.UserProjects).to.be.defined
expect(project.status).not.to.exist
expect(project.UserProjects.status).to.equal('active')
expect(project.UserProjects.data).to.equal(42)
done()
})
})
})
})
})
it('should be able to limit the join table attributes returned', function (done) {
var self = this
self.User.create().success(function (u) {
self.Project.create().success(function (p) {
u.addProject(p, { status: 'active', data: 42 }).success(function() {
u.getProjects({ joinTableAttributes: ['status']}).success(function(projects) {
var project = projects[0]
this.User.hasMany(this.Project, { joinTableModel: this.UserProjects })
this.Project.hasMany(this.User, { joinTableModel: this.UserProjects })
this.sequelize.sync().success(function() { done() })
expect(project.UserProjects).to.be.defined
expect(project.status).not.to.exist
expect(project.UserProjects.status).to.equal('active')
expect(project.UserProjects.data).not.to.exist
done()
})
})
})
})
})
})
describe('inserting in join table', function () {
describe('add', function () {
it('should insert data provided on the object into the join table', function (done) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!