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 ebcf8ab6
authored
Apr 15, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
some first steps for hasOne
1 parent
963e63f4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
57 additions
and
10 deletions
lib/sequelize/associations/belongs-to.js
lib/sequelize/associations/has-one.js
lib/sequelize/associations/mixin.js
test/Model/has-one.js
lib/sequelize/associations/belongs-to.js
View file @
ebcf8ab
...
...
@@ -16,7 +16,6 @@ BelongsTo.prototype.injectAttributes = function() {
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
Utils
.
_
.
extend
(
this
.
source
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
// this.source.associations[this.associationName] = association
return
this
}
...
...
lib/sequelize/associations/has-one.js
View file @
ebcf8ab
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
var
HasOne
=
module
.
exports
=
function
(
associationName
,
srcModel
,
targetModel
,
options
)
{
this
.
associationName
=
associationName
this
.
source
=
srcModel
this
.
target
=
targetModel
this
.
options
=
options
}
// the id is in the target table
HasOne
.
prototype
.
injectAttributes
=
function
()
{
var
newAttributes
=
{}
this
.
identifier
=
Utils
.
_
.
underscoredIf
(
this
.
source
.
tableName
+
"Id"
,
this
.
options
.
underscored
)
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
Utils
.
_
.
extend
(
this
.
target
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
return
this
}
HasOne
.
prototype
.
injectGetter
=
function
(
obj
)
{
var
self
=
this
obj
[
Utils
.
_
.
camelize
(
'get_'
+
this
.
associationName
)]
=
function
()
{
var
id
=
obj
.
id
,
where
=
{}
where
[
self
.
identifier
]
=
id
return
self
.
target
.
find
({
where
:
where
})
}
return
this
}
HasOne
.
prototype
.
injectSetter
=
function
(
obj
)
{
var
self
=
this
obj
[
Utils
.
_
.
camelize
(
'set_'
+
this
.
associationName
)]
=
function
(
associatedObject
)
{
associatedObject
[
self
.
identifier
]
=
self
.
id
return
associatedObject
.
save
()
}
return
this
}
\ No newline at end of file
lib/sequelize/associations/mixin.js
View file @
ebcf8ab
...
...
@@ -6,7 +6,7 @@ var Associations = module.exports = {
hasOne
:
function
(
associationName
,
associatedModel
)
{
// the id is in the foreign table
var
HasOne
=
require
(
'./has-one'
)
var
association
=
new
HasOne
(
association
,
this
,
associatedModel
,
this
.
options
)
var
association
=
new
HasOne
(
association
Name
,
this
,
associatedModel
,
this
.
options
)
this
.
associations
[
associationName
]
=
association
.
injectAttributes
()
},
belongsTo
:
function
(
associationName
,
associatedModel
)
{
...
...
test/Model/has-one.js
View file @
ebcf8ab
...
...
@@ -2,27 +2,29 @@ var assert = require("assert")
,
config
=
require
(
"./../config"
)
,
Sequelize
=
require
(
"./../../index"
)
,
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
{
logging
:
false
})
return
module
.
exports
=
{
'asd'
:
function
(){}}
module
.
exports
=
{
'it should correctly add the foreign id'
:
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
})
Task
.
hasOne
(
'user'
,
User
)
assert
.
eql
(
User
.
attributes
.
taskId
,
"INT"
)
User
.
hasOne
(
'task'
,
Task
)
assert
.
eql
(
Task
.
attributes
[
'User'
+
num
+
'Id'
]
,
"INT"
)
},
'it should correctly add the foreign id with underscore'
:
function
()
{
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
username
:
Sequelize
.
STRING
},
{
underscored
:
true
})
var
num
=
parseInt
(
Math
.
random
()
*
99999999
)
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
},
{
underscored
:
true
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
User
.
belongsTo
(
'task'
,
Task
)
assert
.
eql
(
User
.
attributes
.
task_id
,
"INT"
)
User
.
hasOne
(
'task'
,
Task
)
assert
.
eql
(
Task
.
attributes
[
'user'
+
num
+
'_id'
]
,
"INT"
)
},
'it should define getter and setter'
:
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
})
User
.
belongsTo
(
'task'
,
Task
)
User
.
hasOne
(
'task'
,
Task
)
var
u
=
User
.
build
({
username
:
'asd'
})
assert
.
isDefined
(
u
.
setTask
)
...
...
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