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 1f89402e
authored
Apr 25, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
some convenience stuff
1 parent
803192aa
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
16 additions
and
11 deletions
lib/sequelize/associations/has-many.js
lib/sequelize/sequelize.js
lib/sequelize/utils.js
test/Model/belongsTo.js
test/config.js
lib/sequelize/associations/has-many.js
View file @
1f89402
var
Utils
=
require
(
"./../utils"
)
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
,
DataTypes
=
require
(
'./../data-types'
)
,
QueryChainer
=
require
(
"./../query-chainer"
)
var
HasMany
=
module
.
exports
=
function
(
srcModel
,
targetModel
,
options
)
{
var
HasMany
=
module
.
exports
=
function
(
srcModel
,
targetModel
,
options
)
{
this
.
source
=
srcModel
this
.
source
=
srcModel
...
@@ -78,7 +77,7 @@ HasMany.prototype.injectSetter = function(obj) {
...
@@ -78,7 +77,7 @@ HasMany.prototype.injectSetter = function(obj) {
instance
[
self
.
accessors
.
get
]().
on
(
'success'
,
function
(
oldAssociatedObjects
)
{
instance
[
self
.
accessors
.
get
]().
on
(
'success'
,
function
(
oldAssociatedObjects
)
{
if
(
self
.
connectorModel
)
{
if
(
self
.
connectorModel
)
{
var
foreignIdentifier
=
self
.
target
.
associations
[
self
.
source
.
tableName
].
identifier
var
foreignIdentifier
=
self
.
target
.
associations
[
self
.
source
.
tableName
].
identifier
var
destroyChainer
=
new
QueryChainer
var
destroyChainer
=
new
Utils
.
QueryChainer
// destroy the old association objects
// destroy the old association objects
oldAssociatedObjects
.
forEach
(
function
(
associatedObject
)
{
destroyChainer
.
add
(
associatedObject
.
destroy
())
})
oldAssociatedObjects
.
forEach
(
function
(
associatedObject
)
{
destroyChainer
.
add
(
associatedObject
.
destroy
())
})
destroyChainer
destroyChainer
...
@@ -87,7 +86,7 @@ HasMany.prototype.injectSetter = function(obj) {
...
@@ -87,7 +86,7 @@ HasMany.prototype.injectSetter = function(obj) {
.
on
(
'success'
,
function
()
{
.
on
(
'success'
,
function
()
{
// create new one
// create new one
var
createChainer
=
new
QueryChainer
var
createChainer
=
new
Utils
.
QueryChainer
newAssociatedObjects
.
forEach
(
function
(
associatedObject
)
{
newAssociatedObjects
.
forEach
(
function
(
associatedObject
)
{
var
attributes
=
{}
var
attributes
=
{}
attributes
[
self
.
identifier
]
=
instance
.
id
attributes
[
self
.
identifier
]
=
instance
.
id
...
@@ -110,7 +109,7 @@ HasMany.prototype.injectSetter = function(obj) {
...
@@ -110,7 +109,7 @@ HasMany.prototype.injectSetter = function(obj) {
})
})
// set the new one
// set the new one
var
chainer
=
new
QueryChainer
var
chainer
=
new
Utils
.
QueryChainer
newAssociatedObjects
.
forEach
(
function
(
associatedObject
)
{
newAssociatedObjects
.
forEach
(
function
(
associatedObject
)
{
associatedObject
[
self
.
identifier
]
=
instance
.
id
associatedObject
[
self
.
identifier
]
=
instance
.
id
chainer
.
add
(
associatedObject
.
save
())
chainer
.
add
(
associatedObject
.
save
())
...
...
lib/sequelize/sequelize.js
View file @
1f89402
...
@@ -19,6 +19,7 @@ var Sequelize = module.exports = function(database, username, password, options)
...
@@ -19,6 +19,7 @@ var Sequelize = module.exports = function(database, username, password, options)
port
:
options
.
port
||
3306
port
:
options
.
port
||
3306
}
}
}
}
Sequelize
.
Utils
=
Utils
var
instanceMethods
=
{
var
instanceMethods
=
{
define
:
function
(
modelName
,
attributes
,
options
)
{
define
:
function
(
modelName
,
attributes
,
options
)
{
...
@@ -37,5 +38,5 @@ var instanceMethods = {
...
@@ -37,5 +38,5 @@ var instanceMethods = {
}
}
Utils
.
_
.
map
(
require
(
'./data-types'
),
function
(
sql
,
accessor
)
{
Sequelize
[
accessor
]
=
sql
})
Sequelize
.
Utils
.
_
.
map
(
require
(
'./data-types'
),
function
(
sql
,
accessor
)
{
Sequelize
[
accessor
]
=
sql
})
Utils
.
_
.
map
(
instanceMethods
,
function
(
fct
,
name
)
{
Sequelize
.
prototype
[
name
]
=
fct
})
Sequelize
.
Utils
.
_
.
map
(
instanceMethods
,
function
(
fct
,
name
)
{
Sequelize
.
prototype
[
name
]
=
fct
})
\ No newline at end of file
\ No newline at end of file
lib/sequelize/utils.js
View file @
1f89402
...
@@ -128,6 +128,7 @@ var Utils = module.exports = {
...
@@ -128,6 +128,7 @@ var Utils = module.exports = {
}
}
}
}
// Some nice class accessors
var
CustomEventEmitter
=
Utils
.
CustomEventEmitter
=
function
(
fct
)
{
var
CustomEventEmitter
=
Utils
.
CustomEventEmitter
=
function
(
fct
)
{
this
.
fct
=
fct
this
.
fct
=
fct
}
}
...
@@ -137,4 +138,6 @@ CustomEventEmitter.prototype.run = function() {
...
@@ -137,4 +138,6 @@ CustomEventEmitter.prototype.run = function() {
var
self
=
this
var
self
=
this
setTimeout
(
function
(){
self
.
fct
()
},
5
)
// delay the function call and return the emitter
setTimeout
(
function
(){
self
.
fct
()
},
5
)
// delay the function call and return the emitter
return
this
return
this
}
}
\ No newline at end of file
Utils
.
QueryChainer
=
require
(
"./query-chainer"
)
\ No newline at end of file
test/Model/belongsTo.js
View file @
1f89402
...
@@ -2,7 +2,7 @@ var assert = require("assert")
...
@@ -2,7 +2,7 @@ var assert = require("assert")
,
config
=
require
(
"./../config"
)
,
config
=
require
(
"./../config"
)
,
Sequelize
=
require
(
"./../../index"
)
,
Sequelize
=
require
(
"./../../index"
)
,
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
{
logging
:
false
})
,
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
{
logging
:
false
})
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
)
...
...
test/config.js
View file @
1f89402
module
.
exports
=
{
module
.
exports
=
{
username
:
'root'
,
username
:
'root'
,
password
:
null
,
password
:
null
,
database
:
'sequelize_test'
database
:
'sequelize_test'
,
host
:
'127.0.0.1'
}
}
\ 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