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 963e63f4
authored
Apr 15, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed some brainfuck in belongs-to test
1 parent
b89572f0
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
27 deletions
lib/sequelize/associations/belongs-to.js
lib/sequelize/associations/mixin.js
test/Model/belongsTo.js
test/Model/has-one.js
lib/sequelize/associations/belongs-to.js
View file @
963e63f
...
...
@@ -11,7 +11,6 @@ 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
)
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
...
...
lib/sequelize/associations/mixin.js
View file @
963e63f
...
...
@@ -3,17 +3,20 @@
*/
var
Associations
=
module
.
exports
=
{
classMethods
:
{
hasOne
:
function
(
associationName
,
associatedModel
,
options
)
{
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
)
this
.
associations
[
associationName
]
=
association
.
injectAttributes
()
},
hasMany
:
function
(
associationName
,
associatedModel
,
options
)
{
// the id is in the foreign table or in a connecting table
},
belongsTo
:
function
(
associationName
,
associatedModel
,
options
)
{
belongsTo
:
function
(
associationName
,
associatedModel
)
{
// the id is in this table
var
BelongsTo
=
require
(
"./belongs-to"
)
var
association
=
new
BelongsTo
(
associationName
,
this
,
associatedModel
,
this
.
options
)
this
.
associations
[
associationName
]
=
association
.
injectAttributes
()
},
hasMany
:
function
(
associationName
,
associatedModel
,
options
)
{
// the id is in the foreign table or in a connecting table
}
},
instanceMethods
:
{
...
...
test/Model/belongsTo.js
View file @
963e63f
...
...
@@ -8,39 +8,39 @@ module.exports = {
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
)
assert
.
eql
(
User
.
attributes
.
task
Id
,
"INT"
)
Task
.
belongsTo
(
'user'
,
User
)
assert
.
eql
(
Task
.
attributes
.
user
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
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
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
}
,
{
underscored
:
true
}
)
User
.
belongsTo
(
'task'
,
Task
)
assert
.
eql
(
User
.
attributes
.
task
_id
,
"INT"
)
Task
.
belongsTo
(
'user'
,
User
)
assert
.
eql
(
Task
.
attributes
.
user
_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
)
Task
.
belongsTo
(
'user'
,
User
)
var
u
=
User
.
build
({
usernam
e
:
'asd'
})
assert
.
isDefined
(
u
.
setTask
)
assert
.
isDefined
(
u
.
getTask
)
var
t
=
Task
.
build
({
titl
e
:
'asd'
})
assert
.
isDefined
(
t
.
setUser
)
assert
.
isDefined
(
t
.
getUser
)
},
'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
})
User
.
belongsTo
(
'task'
,
Task
)
Task
.
belongsTo
(
'user'
,
User
)
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
Task
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
create
({
username
:
'asd'
}).
on
(
'success'
,
function
(
u
)
{
Task
.
create
({
title
:
'a task'
}).
on
(
'success'
,
function
(
t
)
{
u
.
setTask
(
t
).
on
(
'success'
,
function
()
{
u
.
getTask
().
on
(
'success'
,
function
(
task
)
{
assert
.
eql
(
task
.
title
,
'a task
'
)
t
.
setUser
(
u
).
on
(
'success'
,
function
()
{
t
.
getUser
().
on
(
'success'
,
function
(
user
)
{
assert
.
eql
(
user
.
username
,
'asd
'
)
exit
(
function
(){})
})
})
...
...
@@ -53,18 +53,18 @@ module.exports = {
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
)
Task
.
belongsTo
(
'user'
,
User
)
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
Task
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
create
({
username
:
'asd'
}).
on
(
'success'
,
function
(
u
)
{
Task
.
create
({
title
:
'a task'
}).
on
(
'success'
,
function
(
t
)
{
u
.
setTask
(
t
).
on
(
'success'
,
function
()
{
u
.
getTask
().
on
(
'success'
,
function
(
task
)
{
assert
.
eql
(
task
.
title
,
'a task
'
)
u
.
setTask
(
null
).
on
(
'success'
,
function
()
{
u
.
getTask
().
on
(
'success'
,
function
(
task
)
{
assert
.
isNull
(
task
)
t
.
setUser
(
u
).
on
(
'success'
,
function
()
{
t
.
getUser
().
on
(
'success'
,
function
(
user
)
{
assert
.
eql
(
user
.
username
,
'asd
'
)
t
.
setUser
(
null
).
on
(
'success'
,
function
()
{
t
.
getUser
().
on
(
'success'
,
function
(
user
)
{
assert
.
isNull
(
user
)
exit
(
function
(){})
})
})
...
...
test/Model/has-one.js
0 → 100644
View file @
963e63f
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
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
Task
.
hasOne
(
'user'
,
User
)
assert
.
eql
(
User
.
attributes
.
taskId
,
"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
Task
=
sequelize
.
define
(
'Task'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
title
:
Sequelize
.
STRING
})
User
.
belongsTo
(
'task'
,
Task
)
assert
.
eql
(
User
.
attributes
.
task_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
)
var
u
=
User
.
build
({
username
:
'asd'
})
assert
.
isDefined
(
u
.
setTask
)
assert
.
isDefined
(
u
.
getTask
)
}
}
\ 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