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 49723bf5
authored
Apr 14, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed test issue + belongs-to is almost working
1 parent
af7b75bb
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
66 additions
and
43 deletions
lib/sequelize/associations/belongs-to.js
lib/sequelize/model-definition.js
test/Model/belongsTo.js
test/Model/build.js
test/Model/create.js
test/Model/destroy.js
test/Model/find-findAll-all.js
test/Model/is-new-record.js
test/Model/save.js
test/Model/sync-drop.js
test/Model/values.js
test/Utils/underscore.js
lib/sequelize/associations/belongs-to.js
View file @
49723bf
...
...
@@ -12,8 +12,7 @@ var BelongsTo = module.exports = function(associationName, srcModel, targetModel
BelongsTo
.
prototype
.
injectAttributes
=
function
()
{
var
newAttributes
=
{}
this
.
identifier
=
this
.
associationName
+
"Id"
if
(
this
.
options
.
underscored
)
this
.
identifier
=
Utils
.
_
.
camelize
(
this
.
identifier
)
this
.
identifier
=
Utils
.
_
.
underscoredIf
(
this
.
associationName
+
"Id"
,
this
.
options
.
underscored
)
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
Utils
.
_
.
extend
(
this
.
source
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
...
...
@@ -25,8 +24,8 @@ BelongsTo.prototype.injectAttributes = function() {
BelongsTo
.
prototype
.
injectGetter
=
function
(
obj
)
{
var
self
=
this
obj
[
'get'
+
this
.
associationName
]
=
function
()
{
return
self
.
target
.
find
(
self
.
identifier
)
obj
[
Utils
.
_
.
camelize
(
'get_'
+
this
.
associationName
)
]
=
function
()
{
return
self
.
target
.
find
(
obj
[
self
.
identifier
]
)
}
return
this
...
...
@@ -35,9 +34,9 @@ BelongsTo.prototype.injectGetter = function(obj) {
BelongsTo
.
prototype
.
injectSetter
=
function
(
obj
)
{
var
self
=
this
obj
[
'set'
+
this
.
associationName
]
=
function
(
associatedObject
)
{
self
.
source
[
self
.
identifier
]
=
associatedObject
.
id
return
self
.
source
.
save
()
obj
[
Utils
.
_
.
camelize
(
'set_'
+
this
.
associationName
)
]
=
function
(
associatedObject
)
{
obj
[
self
.
identifier
]
=
associatedObject
.
id
return
obj
.
save
()
}
return
this
...
...
lib/sequelize/model-definition.js
View file @
49723bf
...
...
@@ -25,11 +25,11 @@ ModelDefinition.prototype.addDefaultAttributes = function() {
if
(
this
.
hasPrimaryKeys
)
defaultAttributes
=
{}
if
(
this
.
options
.
timestamps
)
{
defaultAttributes
[
this
.
options
.
underscored
?
'created_at'
:
'createdAt'
]
=
{
type
:
DataTypes
.
DATE
,
allowNull
:
false
}
defaultAttributes
[
this
.
options
.
underscored
?
'updated_at'
:
'updatedAt'
]
=
{
type
:
DataTypes
.
DATE
,
allowNull
:
false
}
defaultAttributes
[
Utils
.
_
.
underscoredIf
(
'createdAt'
,
this
.
options
.
underscored
)
]
=
{
type
:
DataTypes
.
DATE
,
allowNull
:
false
}
defaultAttributes
[
Utils
.
_
.
underscoredIf
(
'updatedAt'
,
this
.
options
.
underscored
)
]
=
{
type
:
DataTypes
.
DATE
,
allowNull
:
false
}
if
(
this
.
options
.
paranoid
)
defaultAttributes
[
this
.
options
.
underscored
?
'deleted_at'
:
'deletedAt'
]
=
{
type
:
DataTypes
.
DATE
}
defaultAttributes
[
Utils
.
_
.
underscoredIf
(
'deletedAt'
,
this
.
options
.
underscored
)
]
=
{
type
:
DataTypes
.
DATE
}
}
defaultAttributes
=
Utils
.
simplifyAttributes
(
defaultAttributes
)
...
...
test/Model/belongsTo.js
View file @
49723bf
...
...
@@ -11,7 +11,7 @@ module.exports = {
User
.
belongsTo
(
'task'
,
Task
)
assert
.
eql
(
User
.
attributes
.
taskId
,
"INT"
)
},
'it should correctly add the foreign id with underscore
d
'
:
function
()
{
'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
})
...
...
@@ -25,7 +25,6 @@ module.exports = {
User
.
belongsTo
(
'task'
,
Task
)
var
u
=
User
.
build
({
username
:
'asd'
})
console
.
log
(
u
)
assert
.
isDefined
(
u
.
setTask
)
assert
.
isDefined
(
u
.
getTask
)
},
...
...
@@ -36,12 +35,14 @@ module.exports = {
User
.
belongsTo
(
'task'
,
Task
)
User
.
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'
)
exit
()
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'
)
exit
(
function
(){})
})
})
})
})
...
...
test/Model/build.js
View file @
49723bf
...
...
@@ -19,7 +19,7 @@ module.exports = {
assert
.
eql
(
users
.
length
,
10
)
User
.
all
.
on
(
'success'
,
function
(
users
)
{
assert
.
eql
(
users
.
length
,
0
)
exit
()
exit
(
function
(){}
)
})
})
}
...
...
test/Model/create.js
View file @
49723bf
...
...
@@ -16,7 +16,7 @@ module.exports = {
User
.
create
({
username
:
'foo'
}).
on
(
'success'
,
function
()
{
User
.
create
({
username
:
'foo'
}).
on
(
'failure'
,
function
(
err
)
{
assert
.
eql
(
err
.
message
,
"Duplicate entry 'foo' for key 'username'"
)
exit
()
exit
(
function
(){}
)
})
})
})
...
...
@@ -31,7 +31,7 @@ module.exports = {
assert
.
eql
(
err
.
message
,
"Column 'smth' cannot be null"
)
User
.
create
({
username
:
'foo'
}).
on
(
'failure'
,
function
(
err
)
{
assert
.
eql
(
err
.
message
,
"Column 'smth' cannot be null"
)
exit
()
exit
(
function
(){}
)
})
})
})
...
...
test/Model/destroy.js
View file @
49723bf
...
...
@@ -13,7 +13,7 @@ module.exports = {
u
.
destroy
().
on
(
'success'
,
function
()
{
User
.
all
.
on
(
'success'
,
function
(
users
)
{
assert
.
eql
(
users
.
length
,
0
)
exit
()
exit
(
function
(){}
)
})
})
})
...
...
@@ -27,7 +27,7 @@ module.exports = {
assert
.
isNull
(
u
.
deletedAt
)
u
.
destroy
().
on
(
'success'
,
function
(
u
)
{
assert
.
isNotNull
(
u
.
deletedAt
)
exit
()
exit
(
function
(){}
)
})
})
})
...
...
test/Model/find-findAll-all.js
View file @
49723bf
...
...
@@ -22,7 +22,7 @@ module.exports = {
initUsers
(
2
,
function
(
_
,
User
)
{
User
.
all
.
on
(
'success'
,
function
(
users
)
{
assert
.
eql
(
users
.
length
,
2
)
exit
()
exit
(
function
(){}
)
})
})
},
...
...
@@ -30,7 +30,7 @@ module.exports = {
initUsers
(
2
,
function
(
lastInsertedUser
,
User
)
{
User
.
find
(
lastInsertedUser
.
id
).
on
(
'success'
,
function
(
user
)
{
assert
.
eql
(
user
.
id
,
lastInsertedUser
.
id
)
exit
()
exit
(
function
(){}
)
})
})
},
...
...
@@ -39,7 +39,7 @@ module.exports = {
var
username
=
'user1'
User
.
find
({
where
:
{
name
:
username
}}).
on
(
'success'
,
function
(
user
)
{
assert
.
eql
(
user
.
name
,
username
)
exit
()
exit
(
function
(){}
)
})
})
},
...
...
@@ -47,7 +47,7 @@ module.exports = {
initUsers
(
2
,
function
(
_
,
User
)
{
User
.
find
({
where
:
{
name
:
'foo'
}}).
on
(
'success'
,
function
(
user
)
{
assert
.
eql
(
user
,
null
)
exit
()
exit
(
function
(){}
)
})
})
},
...
...
@@ -56,7 +56,7 @@ module.exports = {
User
.
find
({
limit
:
10
}).
on
(
'success'
,
function
(
user
)
{
// it returns an object instead of an array
assert
.
eql
(
user
.
hasOwnProperty
(
'name'
),
true
)
exit
()
exit
(
function
(){}
)
})
})
},
...
...
@@ -71,7 +71,7 @@ module.exports = {
User
.
find
(
'an identifier'
).
on
(
'success'
,
function
(
u2
)
{
assert
.
eql
(
u
.
identifier
,
'an identifier'
)
assert
.
eql
(
u
.
name
,
'John'
)
exit
()
exit
(
function
(){}
)
})
})
})
...
...
@@ -80,7 +80,7 @@ module.exports = {
initUsers
(
2
,
function
(
_
,
User
)
{
User
.
findAll
().
on
(
'success'
,
function
(
users
)
{
assert
.
eql
(
users
.
length
,
2
)
exit
()
exit
(
function
(){}
)
})
})
},
...
...
@@ -88,7 +88,7 @@ module.exports = {
initUsers
(
2
,
function
(
lastUser
,
User
)
{
User
.
findAll
({
where
:
"id != "
+
lastUser
.
id
}).
on
(
'success'
,
function
(
users
)
{
assert
.
eql
(
users
.
length
,
1
)
exit
()
exit
(
function
(){}
)
})
})
},
...
...
@@ -96,7 +96,7 @@ module.exports = {
initUsers
(
2
,
function
(
lastUser
,
User
)
{
User
.
findAll
({
order
:
"id DESC"
}).
on
(
'success'
,
function
(
users
)
{
assert
.
eql
(
users
[
0
].
id
>
users
[
1
].
id
,
true
)
exit
()
exit
(
function
(){}
)
})
})
},
...
...
@@ -105,7 +105,7 @@ module.exports = {
User
.
findAll
({
limit
:
2
,
offset
:
2
}).
on
(
'success'
,
function
(
users
)
{
assert
.
eql
(
users
.
length
,
2
)
assert
.
eql
(
users
[
0
].
id
,
3
)
exit
()
exit
(
function
(){}
)
})
})
}
...
...
test/Model/is-new-record.js
View file @
49723bf
...
...
@@ -18,7 +18,7 @@ module.exports = {
initUsers
(
1
,
function
(
users
,
User
)
{
assert
.
isNull
(
users
[
0
].
id
)
assert
.
eql
(
users
[
0
].
isNewRecord
,
true
)
exit
()
exit
(
function
(){}
)
})
}
}
\ No newline at end of file
test/Model/save.js
View file @
49723bf
...
...
@@ -6,6 +6,7 @@ var assert = require("assert")
module
.
exports
=
{
'save should add a record to the database'
:
function
(
exit
)
{
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
var
u
=
User
.
build
({
name
:
'hallo'
,
bio
:
'welt'
})
User
.
all
.
on
(
'success'
,
function
(
users
)
{
...
...
@@ -14,7 +15,7 @@ module.exports = {
User
.
all
.
on
(
'success'
,
function
(
users
)
{
assert
.
eql
(
users
.
length
,
1
)
assert
.
eql
(
users
[
0
].
name
,
'hallo'
)
exit
()
exit
(
function
(){}
)
})
})
})
...
...
@@ -24,18 +25,17 @@ module.exports = {
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
var
now
=
Date
.
now
()
// timeout is needed, in order to check the update of the timestamp
setTimeout
(
function
()
{
var
u
=
User
.
build
({
name
:
'foo'
,
bio
:
'bar'
})
,
uNow
=
u
.
updatedAt
assert
.
eql
(
true
,
uNow
.
getTime
()
>
now
)
setTimeout
(
function
()
{
u
.
save
().
on
(
'success'
,
function
()
{
assert
.
eql
(
true
,
uNow
.
getTime
()
<
u
.
updatedAt
)
exit
()
assert
.
eql
(
true
,
uNow
.
getTime
()
<
u
.
updatedAt
.
getTime
()
)
exit
(
function
(){}
)
})
},
100
)
},
100
)
...
...
test/Model/sync-drop.js
View file @
49723bf
...
...
@@ -6,14 +6,14 @@ var assert = require("assert")
module
.
exports
=
{
'sync should work with correct database config'
:
function
(
exit
)
{
User
.
sync
().
on
(
'success'
,
exit
)
User
.
sync
().
on
(
'success'
,
function
(){
exit
(
function
(){})}
)
},
'sync should fail with incorrect database config'
:
function
(
exit
)
{
var
s
=
new
Sequelize
(
'foo'
,
'bar'
,
null
,
{
logging
:
false
})
var
User2
=
s
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
User2
.
sync
().
on
(
'failure'
,
exit
)
User2
.
sync
().
on
(
'failure'
,
function
(){
exit
(
function
(){})}
)
},
'drop should work'
:
function
(
exit
)
{
User
.
drop
().
on
(
'success'
,
exit
)
User
.
drop
().
on
(
'success'
,
function
(){
exit
(
function
(){})}
)
}
}
\ No newline at end of file
test/Model/values.js
View file @
49723bf
...
...
@@ -17,7 +17,7 @@ module.exports = {
'build should not create database entries'
:
function
(
exit
)
{
initUsers
(
1
,
function
(
users
,
User
)
{
assert
.
eql
(
users
[
0
].
values
,
{
"name"
:
"user0"
,
"bio"
:
"foobar"
,
"id"
:
null
})
exit
()
exit
(
function
(){}
)
})
}
}
\ No newline at end of file
test/Utils/underscore.js
0 → 100644
View file @
49723bf
var
assert
=
require
(
"assert"
)
,
Utils
=
require
(
"../../lib/sequelize/utils"
)
module
.
exports
=
{
'underscoredIf should be defined'
:
function
()
{
assert
.
isDefined
(
Utils
.
_
.
underscoredIf
)
},
'underscoredIf should work correctly'
:
function
()
{
assert
.
eql
(
Utils
.
_
.
underscoredIf
(
'fooBar'
,
false
),
'fooBar'
)
assert
.
eql
(
Utils
.
_
.
underscoredIf
(
'fooBar'
,
true
),
'foo_bar'
)
},
'camelizeIf should be defined'
:
function
()
{
assert
.
isDefined
(
Utils
.
_
.
camelizeIf
)
},
'camelizeIf should work correctly'
:
function
()
{
assert
.
eql
(
Utils
.
_
.
camelizeIf
(
'foo_bar'
,
false
),
'foo_bar'
)
assert
.
eql
(
Utils
.
_
.
camelizeIf
(
'foo_bar'
,
true
),
'fooBar'
)
}
}
\ 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