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 4309d203
authored
Apr 16, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
unstable commit
1 parent
ebcf8ab6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
60 additions
and
20 deletions
lib/sequelize/associations/belongs-to.js
lib/sequelize/associations/has-one.js
lib/sequelize/associations/mixin.js
lib/sequelize/model-definition.js
test/Model/belongsTo.js
test/Model/has-one.js
lib/sequelize/associations/belongs-to.js
View file @
4309d20
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
var
BelongsTo
=
module
.
exports
=
function
(
associationName
,
srcModel
,
targetModel
,
options
)
{
this
.
associationName
=
associationName
var
BelongsTo
=
module
.
exports
=
function
(
srcModel
,
targetModel
,
options
)
{
this
.
source
=
srcModel
this
.
target
=
targetModel
this
.
options
=
options
...
...
@@ -10,12 +9,14 @@ var BelongsTo = module.exports = function(associationName, srcModel, targetModel
// the id is in the source table
BelongsTo
.
prototype
.
injectAttributes
=
function
()
{
var
newAttributes
=
{}
this
.
identifier
=
Utils
.
_
.
underscoredIf
(
this
.
associationName
+
"Id"
,
this
.
options
.
underscored
)
var
newAttributes
=
{}
this
.
identifier
=
this
.
target
.
identifierName
console
.
log
(
this
.
identifier
)
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
console
.
log
(
newAttributes
)
Utils
.
_
.
extend
(
this
.
source
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
console
.
log
(
'ok'
)
console
.
log
(
this
.
source
)
return
this
}
...
...
lib/sequelize/associations/has-one.js
View file @
4309d20
...
...
@@ -11,8 +11,8 @@ var HasOne = module.exports = function(associationName, srcModel, targetModel, o
// 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
))
...
...
@@ -21,9 +21,9 @@ HasOne.prototype.injectAttributes = function() {
HasOne
.
prototype
.
injectGetter
=
function
(
obj
)
{
var
self
=
this
obj
[
Utils
.
_
.
camelize
(
'get_'
+
this
.
associationName
)]
=
function
()
{
var
id
=
obj
.
id
var
id
=
obj
.
id
,
where
=
{}
where
[
self
.
identifier
]
=
id
...
...
lib/sequelize/associations/mixin.js
View file @
4309d20
var
Utils
=
require
(
"./../utils"
)
/*
Defines Mixin for all models.
*/
var
Associations
=
module
.
exports
=
{
classMethods
:
{
hasOne
:
function
(
associat
ionName
,
associatedModel
)
{
hasOne
:
function
(
associat
edModel
,
options
)
{
// the id is in the foreign table
var
HasOne
=
require
(
'./has-one'
)
var
association
=
new
HasOne
(
associationName
,
this
,
associatedModel
,
this
.
options
)
this
.
associations
[
associat
ion
Name
]
=
association
.
injectAttributes
()
var
association
=
new
HasOne
(
this
,
associatedModel
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
)
)
this
.
associations
[
associat
edModel
.
table
Name
]
=
association
.
injectAttributes
()
},
belongsTo
:
function
(
associat
ionName
,
associatedModel
)
{
belongsTo
:
function
(
associat
edModel
,
options
)
{
// the id is in this table
var
BelongsTo
=
require
(
"./belongs-to"
)
var
association
=
new
BelongsTo
(
associationName
,
this
,
associatedModel
,
this
.
options
)
this
.
associations
[
associat
ion
Name
]
=
association
.
injectAttributes
()
var
association
=
new
BelongsTo
(
this
,
associatedModel
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
)
)
this
.
associations
[
associat
edModel
.
table
Name
]
=
association
.
injectAttributes
()
},
hasMany
:
function
(
associat
ionName
,
associat
edModel
,
options
)
{
hasMany
:
function
(
associatedModel
,
options
)
{
// the id is in the foreign table or in a connecting table
}
},
...
...
lib/sequelize/model-definition.js
View file @
4309d20
...
...
@@ -36,7 +36,6 @@ ModelDefinition.prototype.addDefaultAttributes = function() {
Utils
.
_
.
map
(
defaultAttributes
,
function
(
value
,
attr
)
{
self
.
attributes
[
attr
]
=
value
})
}
ModelDefinition
.
prototype
.
query
=
function
()
{
var
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
,
s
=
this
.
modelManager
.
sequelize
...
...
@@ -143,4 +142,8 @@ ModelDefinition.prototype.__defineGetter__('hasPrimaryKeys', function() {
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
})
\ No newline at end of file
test/Model/belongsTo.js
View file @
4309d20
...
...
@@ -5,17 +5,19 @@ var assert = require("assert")
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
.
belongsTo
(
'user'
,
User
)
assert
.
eql
(
Task
.
attributes
.
userId
,
"INT"
)
Task
.
belongsTo
(
User
)
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
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
},
{
underscored
:
true
})
Task
.
belongsTo
(
'user'
,
User
)
console
.
log
(
Task
.
attributes
)
assert
.
eql
(
Task
.
attributes
.
user_id
,
"INT"
)
},
'it should define getter and setter'
:
function
()
{
...
...
@@ -28,6 +30,15 @@ module.exports = {
assert
.
isDefined
(
t
.
setUser
)
assert
.
isDefined
(
t
.
getUser
)
},
'it should set the foreign id to null'
:
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'
,
User
)
var
t
=
Task
.
build
({
title
:
'asd'
})
assert
.
isNull
(
t
.
userId
)
},
'it should set and get the correct object'
:
function
(
exit
)
{
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
...
...
test/Model/has-one.js
View file @
4309d20
...
...
@@ -29,5 +29,26 @@ module.exports = {
var
u
=
User
.
build
({
username
:
'asd'
})
assert
.
isDefined
(
u
.
setTask
)
assert
.
isDefined
(
u
.
getTask
)
},
'it should set and get the correct objects'
:
function
(
exit
)
{
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
.
hasOne
(
'task'
,
Task
)
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
Task
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
create
({
username
:
'name'
}).
on
(
'success'
,
function
(
user
)
{
Task
.
create
({
title
:
'snafu'
}).
on
(
'success'
,
function
(
task
)
{
user
.
setTask
(
task
).
on
(
'success'
,
function
()
{
user
.
getTask
().
on
(
'success'
,
function
(
task2
)
{
assert
.
eql
(
task
.
title
,
task2
.
title
)
exit
(
function
(){})
})
})
})
})
})
})
}
}
\ 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