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 fea413f1
authored
Apr 16, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
has one is working :)
1 parent
66de46ae
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
14 deletions
lib/sequelize/associations/has-one.js
test/Model/has-one.js
lib/sequelize/associations/has-one.js
View file @
fea413f
var
Utils
=
require
(
"./../utils"
)
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
,
DataTypes
=
require
(
'./../data-types'
)
var
HasOne
=
module
.
exports
=
function
(
associationName
,
srcModel
,
targetModel
,
options
)
{
var
HasOne
=
module
.
exports
=
function
(
srcModel
,
targetModel
,
options
)
{
this
.
associationName
=
associationName
this
.
source
=
srcModel
this
.
source
=
srcModel
this
.
target
=
targetModel
this
.
target
=
targetModel
this
.
options
=
options
this
.
options
=
options
...
@@ -12,7 +11,7 @@ var HasOne = module.exports = function(associationName, srcModel, targetModel, o
...
@@ -12,7 +11,7 @@ var HasOne = module.exports = function(associationName, srcModel, targetModel, o
HasOne
.
prototype
.
injectAttributes
=
function
()
{
HasOne
.
prototype
.
injectAttributes
=
function
()
{
var
newAttributes
=
{}
var
newAttributes
=
{}
this
.
identifier
=
Utils
.
_
.
underscoredIf
(
this
.
source
.
tableName
+
"Id"
,
this
.
options
.
underscored
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
this
.
source
.
tableName
+
"Id"
,
this
.
options
.
underscored
)
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
Utils
.
_
.
extend
(
this
.
target
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
Utils
.
_
.
extend
(
this
.
target
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
...
@@ -21,8 +20,9 @@ HasOne.prototype.injectAttributes = function() {
...
@@ -21,8 +20,9 @@ HasOne.prototype.injectAttributes = function() {
HasOne
.
prototype
.
injectGetter
=
function
(
obj
)
{
HasOne
.
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
.
id
var
id
=
obj
.
id
,
where
=
{}
,
where
=
{}
...
@@ -35,9 +35,10 @@ HasOne.prototype.injectGetter = function(obj) {
...
@@ -35,9 +35,10 @@ HasOne.prototype.injectGetter = function(obj) {
HasOne
.
prototype
.
injectSetter
=
function
(
obj
)
{
HasOne
.
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
)
{
associatedObject
[
self
.
identifier
]
=
self
.
id
associatedObject
[
self
.
identifier
]
=
this
.
id
return
associatedObject
.
save
()
return
associatedObject
.
save
()
}
}
...
...
test/Model/has-one.js
View file @
fea413f
...
@@ -5,11 +5,11 @@ var assert = require("assert")
...
@@ -5,11 +5,11 @@ 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
})
User.hasOne(
'task',
Task)
User
.
hasOne
(
Task
)
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
()
{
...
@@ -17,14 +17,34 @@ module.exports = {
...
@@ -17,14 +17,34 @@ module.exports = {
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
},
{
underscored
:
true
})
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
},
{
underscored
:
true
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
User.hasOne(
'task',
Task)
User
.
hasOne
(
Task
)
assert
.
eql
(
Task
.
attributes
[
'user'
+
num
+
'_id'
],
"INT"
)
assert
.
eql
(
Task
.
attributes
[
'user'
+
num
+
'_id'
],
"INT"
)
},
},
'it should correctly add the foreign id when defining the foreignkey as option'
:
function
()
{
var
num
=
parseInt
(
Math
.
random
()
*
99999999
)
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
},
{
underscored
:
true
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
User
.
hasOne
(
Task
,
{
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 User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var
num
=
parseInt
(
Math
.
random
()
*
99999999
)
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
User
.
hasOne
(
Task
)
var
u
=
User
.
build
({
username
:
'asd'
})
assert
.
isDefined
(
u
[
'setTask'
+
num
])
assert
.
isDefined
(
u
[
'getTask'
+
num
])
},
'it should define getter and setter according to as option'
:
function
()
{
var
num
=
parseInt
(
Math
.
random
()
*
99999999
)
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
User.hasOne(
'task', Task
)
User
.
hasOne
(
Task
,
{
as
:
'Task'
}
)
var
u
=
User
.
build
({
username
:
'asd'
})
var
u
=
User
.
build
({
username
:
'asd'
})
assert
.
isDefined
(
u
.
setTask
)
assert
.
isDefined
(
u
.
setTask
)
...
@@ -34,7 +54,7 @@ module.exports = {
...
@@ -34,7 +54,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
})
User.hasOne(
'task', Task
)
User
.
hasOne
(
Task
,
{
as
:
'Task'
}
)
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
()
{
...
@@ -49,6 +69,6 @@ module.exports = {
...
@@ -49,6 +69,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