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

Commit 73d07be6 by Sascha Gehlich

Added `.createAssociation` support for `HasOne` and `BelongsTo`

1 parent 7dd01aa0
var Utils = require("./../utils")
, DataTypes = require('./../data-types')
, Helpers = require('./helpers')
, Transaction = require('../transaction')
module.exports = (function() {
var BelongsTo = function(source, target, options) {
......@@ -28,7 +29,8 @@ module.exports = (function() {
this.accessors = {
get: Utils._.camelize('get_' + this.as),
set: Utils._.camelize('set_' + this.as)
set: Utils._.camelize('set_' + this.as),
create: Utils._.camelize('create_' + this.as)
}
}
......@@ -98,5 +100,30 @@ module.exports = (function() {
return this
}
BelongsTo.prototype.injectCreator = function(obj) {
var self = this
obj[this.accessors.create] = function(values, fieldsOrOptions) {
var instance = this
, options = {}
if ((fieldsOrOptions || {}).transaction instanceof Transaction) {
options.transaction = fieldsOrOptions.transaction
}
return new Utils.CustomEventEmitter(function(emitter) {
self.target
.create(values, fieldsOrOptions)
.proxy(emitter, { events: ['error', 'sql'] })
.success(function(newAssociatedObject) {
instance[self.accessors.set](newAssociatedObject, options)
.proxy(emitter)
})
}).run()
}
return this
}
return BelongsTo
})()
var Utils = require("./../utils")
, DataTypes = require('./../data-types')
, Helpers = require("./helpers")
, Transaction = require("../transaction")
module.exports = (function() {
var HasOne = function(srcDAO, targetDAO, options) {
......@@ -27,7 +28,8 @@ module.exports = (function() {
this.accessors = {
get: Utils._.camelize('get_' + this.options.as),
set: Utils._.camelize('set_' + this.options.as)
set: Utils._.camelize('set_' + this.options.as),
create: Utils._.camelize('create_' + this.options.as)
}
}
......@@ -73,7 +75,12 @@ module.exports = (function() {
params.where = smart
}
return self.target.find(params)
var options = {}
if (params.transaction) {
options.transaction = params.transaction;
delete params.transaction;
}
return self.target.find(params, options)
}
return this
......@@ -128,5 +135,31 @@ module.exports = (function() {
return this
}
HasOne.prototype.injectCreator = function(obj) {
var self = this
obj[this.accessors.create] = function(values, fieldsOrOptions) {
var instance = this
, options = {}
if ((fieldsOrOptions || {}).transaction instanceof Transaction) {
options.transaction = fieldsOrOptions.transaction
}
return new Utils.CustomEventEmitter(function(emitter) {
self.target
.create(values, fieldsOrOptions)
.proxy(emitter, { events: ['error', 'sql'] })
.success(function(newAssociatedObject) {
console.log(self.accessors.set, !!newAssociatedObject, !!options.transaction);
instance[self.accessors.set](newAssociatedObject, options)
.proxy(emitter)
})
}).run()
}
return this
};
return HasOne
})()
......@@ -18,6 +18,7 @@ Mixin.hasOne = function(associatedDAOFactory, options) {
association.injectGetter(this.DAO.prototype);
association.injectSetter(this.DAO.prototype);
association.injectCreator(this.DAO.prototype);
return this
}
......@@ -34,6 +35,7 @@ Mixin.belongsTo = function(associatedDAOFactory, options) {
association.injectGetter(this.DAO.prototype)
association.injectSetter(this.DAO.prototype)
association.injectCreator(this.DAO.prototype)
return this
}
......
......@@ -165,6 +165,55 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
})
})
describe('createAssociation', function() {
it('creates an associated model instance', function(done) {
var User = this.sequelize.define('User', { username: DataTypes.STRING })
, Task = this.sequelize.define('Task', { title: DataTypes.STRING })
Task.belongsTo(User)
this.sequelize.sync({ force: true }).success(function() {
Task.create({ title: 'task' }).success(function(task) {
task.createUser({ username: 'bob' }).success(function() {
task.getUser().success(function(user) {
expect(user).not.to.be.null
expect(user.username).to.equal('bob')
done()
})
})
})
})
})
it('supports transactions', function(done) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Support.Sequelize.STRING })
, Group = sequelize.define('Group', { name: Support.Sequelize.STRING })
Group.belongsTo(User)
sequelize.sync({ force: true }).success(function() {
Group.create({ name: 'bar' }).success(function(group) {
sequelize.transaction(function(t) {
group.createUser({ username: 'foo' }, { transaction: t }).success(function() {
group.getUser().success(function(user) {
expect(user).to.be.null
group.getUser({ transaction: t }).success(function(user) {
expect(user).not.to.be.null
t.rollback().success(function() { done() })
})
})
})
})
})
})
})
})
})
describe("Foreign key constraints", function() {
it("are not enabled by default", function(done) {
var Task = this.sequelize.define('Task', { title: DataTypes.STRING })
......
......@@ -160,6 +160,57 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
})
})
describe('createAssociation', function() {
it('creates an associated model instance', function(done) {
var User = this.sequelize.define('User', { username: Sequelize.STRING })
, Task = this.sequelize.define('Task', { title: Sequelize.STRING })
User.hasOne(Task)
this.sequelize.sync({ force: true }).success(function() {
User.create({ username: 'bob' }).success(function(user) {
user.createTask({ title: 'task' }).success(function() {
user.getTask().success(function(task) {
expect(task).not.to.be.null
expect(task.title).to.equal('task')
done()
})
})
})
})
})
it('supports transactions', function(done) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Sequelize.STRING })
, Group = sequelize.define('Group', { name: Sequelize.STRING })
User.hasOne(Group)
sequelize.sync({ force: true }).success(function() {
User.create({ username: 'bob' }).success(function(user) {
sequelize.transaction(function(t) {
user.createGroup({ name: 'testgroup' }, { transaction: t }).success(function(group) {
User.all().success(function (users) {
users[0].getGroup().success(function (group) {
expect(group).to.be.null;
User.all({ transaction: t }).success(function (users) {
users[0].getGroup({ transaction: t }).success(function (group) {
expect(group).to.be.not.null;
t.rollback().success(function() { done() })
})
})
})
})
})
})
})
})
})
})
})
describe("Foreign key constraints", function() {
it("are not enabled by default", function(done) {
var Task = this.sequelize.define('Task', { title: Sequelize.STRING })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!