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 66de46ae
authored
Apr 16, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed and refactored belongs-to
1 parent
7d0dbfef
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
39 additions
and
21 deletions
lib/sequelize/associations/belongs-to.js
lib/sequelize/model-definition.js
test/Model/belongsTo.js
test/Model/has-one.js
lib/sequelize/associations/belongs-to.js
View file @
66de46a
...
@@ -11,7 +11,7 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) {
...
@@ -11,7 +11,7 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) {
BelongsTo
.
prototype
.
injectAttributes
=
function
()
{
BelongsTo
.
prototype
.
injectAttributes
=
function
()
{
var
newAttributes
=
{}
var
newAttributes
=
{}
this
.
identifier
=
this
.
target
.
identifierName
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
this
.
target
.
tableName
+
"Id"
,
this
.
source
.
options
.
underscored
)
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
Utils
.
_
.
extend
(
this
.
source
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
Utils
.
_
.
extend
(
this
.
source
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
...
@@ -20,8 +20,9 @@ BelongsTo.prototype.injectAttributes = function() {
...
@@ -20,8 +20,9 @@ BelongsTo.prototype.injectAttributes = function() {
BelongsTo
.
prototype
.
injectGetter
=
function
(
obj
)
{
BelongsTo
.
prototype
.
injectGetter
=
function
(
obj
)
{
var
self
=
this
var
self
=
this
,
accessor
=
Utils
.
_
.
camelize
(
'get_'
+
(
this
.
options
.
as
||
this
.
target
.
tableName
))
obj
[
Utils
.
_
.
camelize
(
'get_'
+
this
.
associationName
)
]
=
function
()
{
obj
[
accessor
]
=
function
()
{
var
id
=
obj
[
self
.
identifier
]
var
id
=
obj
[
self
.
identifier
]
return
self
.
target
.
find
(
id
)
return
self
.
target
.
find
(
id
)
}
}
...
@@ -31,8 +32,9 @@ BelongsTo.prototype.injectGetter = function(obj) {
...
@@ -31,8 +32,9 @@ BelongsTo.prototype.injectGetter = function(obj) {
BelongsTo
.
prototype
.
injectSetter
=
function
(
obj
)
{
BelongsTo
.
prototype
.
injectSetter
=
function
(
obj
)
{
var
self
=
this
var
self
=
this
,
accessor
=
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
this
.
target
.
tableName
))
obj
[
Utils
.
_
.
camelize
(
'set_'
+
this
.
associationName
)
]
=
function
(
associatedObject
)
{
obj
[
accessor
]
=
function
(
associatedObject
)
{
obj
[
self
.
identifier
]
=
associatedObject
?
associatedObject
.
id
:
null
obj
[
self
.
identifier
]
=
associatedObject
?
associatedObject
.
id
:
null
return
obj
.
save
()
return
obj
.
save
()
}
}
...
...
lib/sequelize/model-definition.js
View file @
66de46a
...
@@ -142,8 +142,4 @@ ModelDefinition.prototype.__defineGetter__('hasPrimaryKeys', function() {
...
@@ -142,8 +142,4 @@ ModelDefinition.prototype.__defineGetter__('hasPrimaryKeys', function() {
return
this
.
primaryKeyCount
>
0
return
this
.
primaryKeyCount
>
0
})
})
ModelDefinition
.
prototype
.
__defineGetter__
(
'identifierName'
,
function
()
{
return
Utils
.
_
.
underscoredIf
(
this
.
tableName
+
"Id"
,
this
.
options
.
underscored
)
})
Utils
.
_
.
map
(
require
(
"./associations/mixin"
).
classMethods
,
function
(
fct
,
name
)
{
ModelDefinition
.
prototype
[
name
]
=
fct
})
Utils
.
_
.
map
(
require
(
"./associations/mixin"
).
classMethods
,
function
(
fct
,
name
)
{
ModelDefinition
.
prototype
[
name
]
=
fct
})
\ No newline at end of file
test/Model/belongsTo.js
View file @
66de46a
...
@@ -13,36 +13,56 @@ module.exports = {
...
@@ -13,36 +13,56 @@ module.exports = {
assert
.
eql
(
Task
.
attributes
[
'User'
+
num
+
'Id'
],
"INT"
)
assert
.
eql
(
Task
.
attributes
[
'User'
+
num
+
'Id'
],
"INT"
)
},
},
'it should correctly add the foreign id with underscore'
:
function
()
{
'it should correctly add the foreign id with underscore'
:
function
()
{
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
username
:
Sequelize
.
STRING
})
var
num
=
parseInt
(
Math
.
random
()
*
99999999
)
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
},
{
underscored
:
true
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
},
{
underscored
:
true
})
Task
.
belongsTo
(
'user'
,
User
)
Task
.
belongsTo
(
User
)
assert
.
eql
(
Task
.
attributes
.
user_id
,
"INT"
)
assert
.
eql
(
Task
.
attributes
[
'user'
+
num
+
'_id'
],
"INT"
)
},
'it should correctly add the foreign id if foreignKey is passed'
:
function
()
{
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
Task
.
belongsTo
(
User
,
{
foreignKey
:
'person_id'
})
assert
.
eql
(
Task
.
attributes
[
'person_id'
],
"INT"
)
},
},
'it should define getter and setter'
:
function
()
{
'it should define getter and setter'
:
function
()
{
var
num
=
parseInt
(
Math
.
random
()
*
99999999
)
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
Task
.
belongsTo
(
User
)
var
t
=
Task
.
build
({
title
:
'asd'
})
assert
.
isDefined
(
t
[
'setUser'
+
num
])
assert
.
isDefined
(
t
[
'getUser'
+
num
])
},
'it should define getter and setter according to passed as option'
:
function
()
{
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
username
:
Sequelize
.
STRING
})
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
Task
.
belongsTo
(
'user'
,
User
)
Task
.
belongsTo
(
User
,
{
as
:
'Person'
}
)
var
t
=
Task
.
build
({
title
:
'asd'
})
var
t
=
Task
.
build
({
title
:
'asd'
})
assert
.
isDefined
(
t
.
set
User
)
assert
.
isDefined
(
t
.
set
Person
)
assert
.
isDefined
(
t
.
get
User
)
assert
.
isDefined
(
t
.
get
Person
)
},
},
'it should set the foreign id to null'
:
function
()
{
'it should set the foreign id to null'
:
function
()
{
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
username
:
Sequelize
.
STRING
})
var
num
=
parseInt
(
Math
.
random
()
*
99999999
)
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
Task
.
belongsTo
(
'user'
,
User
)
Task
.
belongsTo
(
User
)
var
t
=
Task
.
build
({
title
:
'asd'
})
var
t
=
Task
.
build
({
title
:
'asd'
})
assert
.
isNull
(
t
.
userId
)
assert
.
isNull
(
t
[
'User'
+
num
+
'Id'
]
)
},
},
'it should set and get the correct object'
:
function
(
exit
)
{
'it should set and get the correct object'
:
function
(
exit
)
{
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
username
:
Sequelize
.
STRING
})
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
Task
.
belongsTo
(
'user'
,
User
)
Task
.
belongsTo
(
User
,
{
as
:
'User'
}
)
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
Task
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
Task
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
...
@@ -63,7 +83,7 @@ module.exports = {
...
@@ -63,7 +83,7 @@ module.exports = {
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
username
:
Sequelize
.
STRING
})
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
Task
.
belongsTo
(
'user'
,
User
)
Task
.
belongsTo
(
User
,
{
as
:
'User'
}
)
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
Task
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
Task
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
...
...
test/Model/has-one.js
View file @
66de46a
...
@@ -5,7 +5,7 @@ var assert = require("assert")
...
@@ -5,7 +5,7 @@ var assert = require("assert")
module
.
exports
=
{
module
.
exports
=
{
'it should correctly add the foreign id'
:
function
()
{
'it should correctly add the foreign id'
:
function
()
{
var
num
=
parseInt
(
Math
.
random
()
*
99999999
)
/*
var num = parseInt(Math.random() * 99999999)
var User = sequelize.define('User' + num, { username: Sequelize.STRING })
var User = sequelize.define('User' + num, { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
...
@@ -49,6 +49,6 @@ module.exports = {
...
@@ -49,6 +49,6 @@ module.exports = {
})
})
})
})
})
})
})
})
*/
}
}
}
}
\ No newline at end of file
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