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

Commit 51821a8b by Jan Aagaard Meier

insert into join table

1 parent 18243c9f
......@@ -10,7 +10,9 @@ module.exports = (function() {
var self = this, _options = options
var customEventEmitter = new Utils.CustomEventEmitter(function() {
var where = {}, options = _options || {}
var where = {}
, options = _options || {}
, association = self.__factory.target.associations[self.__factory.associationAccessor]
//fully qualify
var instancePrimaryKeys = Object.keys(self.instance.daoFactory.primaryKeys)
......@@ -25,6 +27,21 @@ module.exports = (function() {
foreignPrimary = foreignPrimary.length === 1 ? foreignPrimary[0] : 'id'
where[self.__factory.connectorDAO.tableName+"."+foreignKey] = {join: self.__factory.target.tableName+"."+foreignPrimary}
if (association.customJoinTableModel) {
options.attributes = []
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)+".*")
console.log(options.attributes);
}
if (options.where) {
if (Array.isArray(options.where)) {
smart = Utils.smartWhere([where, options.where], self.__factory.target.daoFactoryManager.sequelize.options.dialect)
......@@ -75,7 +92,7 @@ module.exports = (function() {
})
if (!newObj) {
obsoleteAssociations.push(obj)
obsoleteAssociations.push(old)
} else if (association.customJoinTableModel) {
var changedAssociation = {
where: {},
......
......@@ -21,7 +21,7 @@ module.exports = (function() {
} else if (this.options.joinTableName) {
combinedTableName = this.options.joinTableName
} else {
Utils.combineTableNames(
combinedTableName = Utils.combineTableNames(
this.source.tableName,
this.isSelfAssociation ? (this.options.as || this.target.tableName) : this.target.tableName
)
......
......@@ -567,6 +567,89 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
done()
})
})
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
})
this.User.hasMany(this.Project, { joinTableModel: this.UserProjects })
this.Project.hasMany(this.User, { joinTableModel: this.UserProjects })
this.sequelize.sync().success(function() { done() })
})
describe('add', function () {
it('should insert data provided on the object into the join table', function (done) {
var self = this
self.User.create().success(function (u) {
self.Project.create().success(function (p) {
p.UserProjects = {
status: 'active'
}
u.addProject(p).success(function() {
self.UserProjects.find({ where: { UserId: u.id, ProjectId: p.id }}).success(function (up) {
expect(up.status).to.equal('active')
done()
})
})
})
})
})
it('should insert data provided as a second argument into the join table', function (done) {
var self = this
self.User.create().success(function (u) {
self.Project.create().success(function (p) {
u.addProject(p, { status: 'active' }).success(function() {
self.UserProjects.find({ where: { UserId: u.id, ProjectId: p.id }}).success(function (up) {
expect(up.status).to.equal('active')
done()
})
})
})
})
})
})
describe('set', function () {
it('should be able to combine properties on the associated objects, and default values', function (done) {
var self = this
, _done = _.after(2, done)
self.User.create().success(function (u) {
self.Project.bulkCreate([{}, {}]).success(function () {
self.Project.findAll().success(function (projects) {
var p1 = projects[0]
, p2 = projects[1]
p1.UserProjects = {
status: 'inactive'
}
u.setProjects([p1, p2], { status: 'active' }).success(function() {
self.UserProjects.find({ where: { UserId: u.id, ProjectId: p1.id }}).success(function (up) {
expect(up.status).to.equal('inactive')
_done()
})
self.UserProjects.find({ where: { UserId: u.id, ProjectId: p2.id }}).success(function (up) {
expect(up.status).to.equal('active')
_done()
})
})
})
})
})
})
})
})
})
})
describe("Foreign key constraints", function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!