Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 73d07be6
authored
Jan 08, 2014
by
Sascha Gehlich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added `.createAssociation` support for `HasOne` and `BelongsTo`
1 parent
7dd01aa0
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
171 additions
and
9 deletions
lib/associations/belongs-to.js
lib/associations/has-one.js
lib/associations/mixin.js
test/associations/belongs-to.test.js
test/associations/has-one.test.js
lib/associations/belongs-to.js
View file @
73d07be
var
Utils
=
require
(
"./../utils"
)
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
,
DataTypes
=
require
(
'./../data-types'
)
,
Helpers
=
require
(
'./helpers'
)
,
Helpers
=
require
(
'./helpers'
)
,
Transaction
=
require
(
'../transaction'
)
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
BelongsTo
=
function
(
source
,
target
,
options
)
{
var
BelongsTo
=
function
(
source
,
target
,
options
)
{
...
@@ -28,7 +29,8 @@ module.exports = (function() {
...
@@ -28,7 +29,8 @@ module.exports = (function() {
this
.
accessors
=
{
this
.
accessors
=
{
get
:
Utils
.
_
.
camelize
(
'get_'
+
this
.
as
),
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() {
...
@@ -98,5 +100,30 @@ module.exports = (function() {
return
this
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
return
BelongsTo
})()
})()
lib/associations/has-one.js
View file @
73d07be
var
Utils
=
require
(
"./../utils"
)
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
,
DataTypes
=
require
(
'./../data-types'
)
,
Helpers
=
require
(
"./helpers"
)
,
Helpers
=
require
(
"./helpers"
)
,
Transaction
=
require
(
"../transaction"
)
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
HasOne
=
function
(
srcDAO
,
targetDAO
,
options
)
{
var
HasOne
=
function
(
srcDAO
,
targetDAO
,
options
)
{
...
@@ -27,7 +28,8 @@ module.exports = (function() {
...
@@ -27,7 +28,8 @@ module.exports = (function() {
this
.
accessors
=
{
this
.
accessors
=
{
get
:
Utils
.
_
.
camelize
(
'get_'
+
this
.
options
.
as
),
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() {
...
@@ -73,7 +75,12 @@ module.exports = (function() {
params
.
where
=
smart
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
return
this
...
@@ -128,5 +135,31 @@ module.exports = (function() {
...
@@ -128,5 +135,31 @@ module.exports = (function() {
return
this
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
return
HasOne
})()
})()
lib/associations/mixin.js
View file @
73d07be
...
@@ -18,6 +18,7 @@ Mixin.hasOne = function(associatedDAOFactory, options) {
...
@@ -18,6 +18,7 @@ Mixin.hasOne = function(associatedDAOFactory, options) {
association
.
injectGetter
(
this
.
DAO
.
prototype
);
association
.
injectGetter
(
this
.
DAO
.
prototype
);
association
.
injectSetter
(
this
.
DAO
.
prototype
);
association
.
injectSetter
(
this
.
DAO
.
prototype
);
association
.
injectCreator
(
this
.
DAO
.
prototype
);
return
this
return
this
}
}
...
@@ -34,6 +35,7 @@ Mixin.belongsTo = function(associatedDAOFactory, options) {
...
@@ -34,6 +35,7 @@ Mixin.belongsTo = function(associatedDAOFactory, options) {
association
.
injectGetter
(
this
.
DAO
.
prototype
)
association
.
injectGetter
(
this
.
DAO
.
prototype
)
association
.
injectSetter
(
this
.
DAO
.
prototype
)
association
.
injectSetter
(
this
.
DAO
.
prototype
)
association
.
injectCreator
(
this
.
DAO
.
prototype
)
return
this
return
this
}
}
...
...
test/associations/belongs-to.test.js
View file @
73d07be
...
@@ -165,6 +165,55 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
...
@@ -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
()
{
describe
(
"Foreign key constraints"
,
function
()
{
it
(
"are not enabled by default"
,
function
(
done
)
{
it
(
"are not enabled by default"
,
function
(
done
)
{
var
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
DataTypes
.
STRING
})
var
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
DataTypes
.
STRING
})
...
...
test/associations/has-one.test.js
View file @
73d07be
...
@@ -160,6 +160,57 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
...
@@ -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
()
{
describe
(
"Foreign key constraints"
,
function
()
{
it
(
"are not enabled by default"
,
function
(
done
)
{
it
(
"are not enabled by default"
,
function
(
done
)
{
var
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
var
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment