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
不要怂,就是干,撸起袖子干!
You need to sign in or sign up before continuing.
Commit 99481ee5
authored
Nov 12, 2011
by
Ram
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'sdepold/master'
2 parents
07ebc8d2
98bd1f44
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
93 additions
and
72 deletions
.travis.yml
lib/associations/belongs-to.js
lib/associations/has-many-double-linked.js
lib/associations/has-many-single-linked.js
lib/associations/has-many.js
lib/associations/has-one.js
lib/model.js
lib/query-chainer.js
lib/query.js
.travis.yml
View file @
99481ee
...
@@ -7,6 +7,7 @@ script:
...
@@ -7,6 +7,7 @@ script:
-
"
node_modules/.bin/expresso
-s
test/**/*"
-
"
node_modules/.bin/expresso
-s
test/**/*"
notifications
:
notifications
:
email
:
-
sascha@depold.com
-
sascha@depold.com
env
:
env
:
...
...
lib/associations/belongs-to.js
View file @
99481ee
var
Utils
=
require
(
"./../utils"
)
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
,
DataTypes
=
require
(
'./../data-types'
)
var
BelongsTo
=
module
.
exports
=
function
(
srcModel
,
targetModel
,
options
)
{
module
.
exports
=
(
function
()
{
var
BelongsTo
=
function
(
srcModel
,
targetModel
,
options
)
{
this
.
source
=
srcModel
this
.
source
=
srcModel
this
.
target
=
targetModel
this
.
target
=
targetModel
this
.
options
=
options
this
.
options
=
options
...
@@ -13,10 +14,10 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) {
...
@@ -13,10 +14,10 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) {
this
.
associationAccessor
=
this
.
isSelfAssociation
this
.
associationAccessor
=
this
.
isSelfAssociation
?
Utils
.
combineTableNames
(
this
.
target
.
tableName
,
this
.
options
.
as
||
this
.
target
.
tableName
)
?
Utils
.
combineTableNames
(
this
.
target
.
tableName
,
this
.
options
.
as
||
this
.
target
.
tableName
)
:
this
.
target
.
tableName
:
this
.
target
.
tableName
}
}
// the id is in the source table
// the id is in the source table
BelongsTo
.
prototype
.
injectAttributes
=
function
()
{
BelongsTo
.
prototype
.
injectAttributes
=
function
()
{
var
newAttributes
=
{}
var
newAttributes
=
{}
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
target
.
tableName
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
target
.
tableName
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
...
@@ -24,9 +25,9 @@ BelongsTo.prototype.injectAttributes = function() {
...
@@ -24,9 +25,9 @@ BelongsTo.prototype.injectAttributes = function() {
Utils
.
_
.
extend
(
this
.
source
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
Utils
.
_
.
extend
(
this
.
source
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
return
this
return
this
}
}
BelongsTo
.
prototype
.
injectGetter
=
function
(
obj
)
{
BelongsTo
.
prototype
.
injectGetter
=
function
(
obj
)
{
var
self
=
this
var
self
=
this
,
accessor
=
Utils
.
_
.
camelize
(
'get_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
)))
,
accessor
=
Utils
.
_
.
camelize
(
'get_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
)))
...
@@ -36,9 +37,9 @@ BelongsTo.prototype.injectGetter = function(obj) {
...
@@ -36,9 +37,9 @@ BelongsTo.prototype.injectGetter = function(obj) {
}
}
return
this
return
this
}
}
BelongsTo
.
prototype
.
injectSetter
=
function
(
obj
)
{
BelongsTo
.
prototype
.
injectSetter
=
function
(
obj
)
{
var
self
=
this
var
self
=
this
,
accessor
=
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
)))
,
accessor
=
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
)))
...
@@ -48,4 +49,7 @@ BelongsTo.prototype.injectSetter = function(obj) {
...
@@ -48,4 +49,7 @@ BelongsTo.prototype.injectSetter = function(obj) {
}
}
return
this
return
this
}
}
\ No newline at end of file
return
BelongsTo
})()
lib/associations/has-many-double-linked.js
View file @
99481ee
var
Utils
=
require
(
'./../utils'
)
var
Utils
=
require
(
'./../utils'
)
var
HasManyDoubleLinked
=
module
.
exports
=
function
(
definition
,
instance
)
{
module
.
exports
=
(
function
()
{
var
HasManyDoubleLinked
=
function
(
definition
,
instance
)
{
this
.
__definition
=
definition
this
.
__definition
=
definition
this
.
instance
=
instance
this
.
instance
=
instance
}
}
HasManyDoubleLinked
.
prototype
.
injectGetter
=
function
()
{
HasManyDoubleLinked
.
prototype
.
injectGetter
=
function
()
{
var
self
=
this
var
self
=
this
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
...
@@ -29,9 +30,37 @@ HasManyDoubleLinked.prototype.injectGetter = function() {
...
@@ -29,9 +30,37 @@ HasManyDoubleLinked.prototype.injectGetter = function() {
})
})
return
customEventEmitter
.
run
()
return
customEventEmitter
.
run
()
}
}
HasManyDoubleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
var
self
=
this
destroyObsoleteAssociations
.
call
(
this
,
oldAssociations
,
newAssociations
)
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'success'
,
function
()
{
var
chainer
=
new
Utils
.
QueryChainer
,
association
=
self
.
__definition
.
target
.
associations
[
self
.
__definition
.
associationAccessor
]
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
,
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
return
!
obj
.
equalsOneOf
(
oldAssociations
)
})
unassociatedObjects
.
forEach
(
function
(
unassociatedObject
)
{
var
attributes
=
{}
attributes
[
self
.
__definition
.
identifier
]
=
self
.
instance
.
id
attributes
[
foreignIdentifier
]
=
unassociatedObject
.
id
chainer
.
add
(
self
.
__definition
.
connectorModel
.
create
(
attributes
))
})
chainer
.
run
()
.
on
(
'success'
,
function
()
{
emitter
.
emit
(
'success'
,
newAssociations
)
})
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
})
}
// private
HasManyDoubleLinked
.
prototype
.
destroyObsoleteAssociations
=
function
(
oldAssociations
,
newAssociations
)
{
var
destroyObsoleteAssociations
=
function
(
oldAssociations
,
newAssociations
)
{
var
self
=
this
var
self
=
this
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
...
@@ -64,30 +93,7 @@ HasManyDoubleLinked.prototype.destroyObsoleteAssociations = function(oldAssociat
...
@@ -64,30 +93,7 @@ HasManyDoubleLinked.prototype.destroyObsoleteAssociations = function(oldAssociat
})
})
})
})
return
emitter
.
run
()
return
emitter
.
run
()
}
}
HasManyDoubleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
var
self
=
this
this
.
destroyObsoleteAssociations
(
oldAssociations
,
newAssociations
)
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'success'
,
function
()
{
var
chainer
=
new
Utils
.
QueryChainer
,
association
=
self
.
__definition
.
target
.
associations
[
self
.
__definition
.
associationAccessor
]
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
,
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
return
!
obj
.
equalsOneOf
(
oldAssociations
)
})
unassociatedObjects
.
forEach
(
function
(
unassociatedObject
)
{
var
attributes
=
{}
attributes
[
self
.
__definition
.
identifier
]
=
self
.
instance
.
id
attributes
[
foreignIdentifier
]
=
unassociatedObject
.
id
chainer
.
add
(
self
.
__definition
.
connectorModel
.
create
(
attributes
))
})
chainer
return
HasManyDoubleLinked
.
run
()
})()
.
on
(
'success'
,
function
()
{
emitter
.
emit
(
'success'
,
newAssociations
)
})
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
})
}
lib/associations/has-many-single-linked.js
View file @
99481ee
var
Utils
=
require
(
'./../utils'
)
var
Utils
=
require
(
'./../utils'
)
var
HasManySingleLinked
=
module
.
exports
=
function
(
definition
,
instance
)
{
module
.
exports
=
(
function
()
{
var
HasManySingleLinked
=
function
(
definition
,
instance
)
{
this
.
__definition
=
definition
this
.
__definition
=
definition
this
.
instance
=
instance
this
.
instance
=
instance
}
}
HasManySingleLinked
.
prototype
.
injectGetter
=
function
()
{
HasManySingleLinked
.
prototype
.
injectGetter
=
function
()
{
var
where
=
{}
var
where
=
{}
where
[
this
.
__definition
.
identifier
]
=
this
.
instance
.
id
where
[
this
.
__definition
.
identifier
]
=
this
.
instance
.
id
return
this
.
__definition
.
target
.
findAll
({
where
:
where
})
return
this
.
__definition
.
target
.
findAll
({
where
:
where
})
}
}
HasManySingleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
HasManySingleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
var
self
=
this
var
self
=
this
// clear the old associations
// clear the old associations
...
@@ -31,4 +32,7 @@ HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations,
...
@@ -31,4 +32,7 @@ HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations,
.
run
()
.
run
()
.
on
(
'success'
,
function
()
{
emitter
.
emit
(
'success'
,
newAssociations
)
})
.
on
(
'success'
,
function
()
{
emitter
.
emit
(
'success'
,
newAssociations
)
})
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
}
}
return
HasManySingleLinked
})()
lib/associations/has-many.js
View file @
99481ee
...
@@ -4,7 +4,8 @@ var Utils = require("./../utils")
...
@@ -4,7 +4,8 @@ var Utils = require("./../utils")
var
HasManySingleLinked
=
require
(
"./has-many-single-linked"
)
var
HasManySingleLinked
=
require
(
"./has-many-single-linked"
)
,
HasManyMultiLinked
=
require
(
"./has-many-double-linked"
)
,
HasManyMultiLinked
=
require
(
"./has-many-double-linked"
)
var
HasMany
=
module
.
exports
=
function
(
srcModel
,
targetModel
,
options
)
{
module
.
exports
=
(
function
()
{
var
HasMany
=
function
(
srcModel
,
targetModel
,
options
)
{
this
.
source
=
srcModel
this
.
source
=
srcModel
this
.
target
=
targetModel
this
.
target
=
targetModel
this
.
options
=
options
this
.
options
=
options
...
@@ -23,11 +24,11 @@ var HasMany = module.exports = function(srcModel, targetModel, options) {
...
@@ -23,11 +24,11 @@ var HasMany = module.exports = function(srcModel, targetModel, options) {
add
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'add_'
+
as
)),
add
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'add_'
+
as
)),
remove
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'remove_'
+
as
))
remove
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'remove_'
+
as
))
}
}
}
}
// the id is in the target table
// the id is in the target table
// or in an extra table which connects two tables
// or in an extra table which connects two tables
HasMany
.
prototype
.
injectAttributes
=
function
()
{
HasMany
.
prototype
.
injectAttributes
=
function
()
{
var
multiAssociation
=
this
.
target
.
associations
.
hasOwnProperty
(
this
.
associationAccessor
)
var
multiAssociation
=
this
.
target
.
associations
.
hasOwnProperty
(
this
.
associationAccessor
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
)
+
"Id"
,
this
.
options
.
underscored
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
)
+
"Id"
,
this
.
options
.
underscored
)
...
@@ -58,9 +59,9 @@ HasMany.prototype.injectAttributes = function() {
...
@@ -58,9 +59,9 @@ HasMany.prototype.injectAttributes = function() {
}
}
return
this
return
this
}
}
HasMany
.
prototype
.
injectGetter
=
function
(
obj
)
{
HasMany
.
prototype
.
injectGetter
=
function
(
obj
)
{
var
self
=
this
var
self
=
this
obj
[
this
.
accessors
.
get
]
=
function
()
{
obj
[
this
.
accessors
.
get
]
=
function
()
{
...
@@ -69,9 +70,9 @@ HasMany.prototype.injectGetter = function(obj) {
...
@@ -69,9 +70,9 @@ HasMany.prototype.injectGetter = function(obj) {
}
}
return
this
return
this
}
}
HasMany
.
prototype
.
injectSetter
=
function
(
obj
)
{
HasMany
.
prototype
.
injectSetter
=
function
(
obj
)
{
var
self
=
this
var
self
=
this
obj
[
this
.
accessors
.
set
]
=
function
(
newAssociatedObjects
)
{
obj
[
this
.
accessors
.
set
]
=
function
(
newAssociatedObjects
)
{
...
@@ -124,4 +125,7 @@ HasMany.prototype.injectSetter = function(obj) {
...
@@ -124,4 +125,7 @@ HasMany.prototype.injectSetter = function(obj) {
}
}
return
this
return
this
}
}
\ No newline at end of file
return
HasMany
})()
lib/associations/has-one.js
View file @
99481ee
var
Utils
=
require
(
"./../utils"
)
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
,
DataTypes
=
require
(
'./../data-types'
)
var
HasOne
=
module
.
exports
=
function
(
srcModel
,
targetModel
,
options
)
{
module
.
exports
=
(
function
()
{
var
HasOne
=
function
(
srcModel
,
targetModel
,
options
)
{
this
.
source
=
srcModel
this
.
source
=
srcModel
this
.
target
=
targetModel
this
.
target
=
targetModel
this
.
options
=
options
this
.
options
=
options
...
@@ -18,10 +19,10 @@ var HasOne = module.exports = function(srcModel, targetModel, options) {
...
@@ -18,10 +19,10 @@ var HasOne = module.exports = function(srcModel, targetModel, options) {
get
:
Utils
.
_
.
camelize
(
'get_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
))),
get
:
Utils
.
_
.
camelize
(
'get_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
))),
set
:
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
)))
set
:
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
)))
}
}
}
}
// the id is in the target table
// the id is in the target table
HasOne
.
prototype
.
injectAttributes
=
function
()
{
HasOne
.
prototype
.
injectAttributes
=
function
()
{
var
newAttributes
=
{}
var
newAttributes
=
{}
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
)
+
"Id"
,
this
.
options
.
underscored
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
)
+
"Id"
,
this
.
options
.
underscored
)
...
@@ -29,9 +30,9 @@ HasOne.prototype.injectAttributes = function() {
...
@@ -29,9 +30,9 @@ HasOne.prototype.injectAttributes = function() {
Utils
.
_
.
extend
(
this
.
target
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
Utils
.
_
.
extend
(
this
.
target
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
return
this
return
this
}
}
HasOne
.
prototype
.
injectGetter
=
function
(
obj
)
{
HasOne
.
prototype
.
injectGetter
=
function
(
obj
)
{
var
self
=
this
var
self
=
this
obj
[
this
.
accessors
.
get
]
=
function
()
{
obj
[
this
.
accessors
.
get
]
=
function
()
{
...
@@ -43,9 +44,9 @@ HasOne.prototype.injectGetter = function(obj) {
...
@@ -43,9 +44,9 @@ HasOne.prototype.injectGetter = function(obj) {
}
}
return
this
return
this
}
}
HasOne
.
prototype
.
injectSetter
=
function
(
obj
)
{
HasOne
.
prototype
.
injectSetter
=
function
(
obj
)
{
var
self
=
this
var
self
=
this
obj
[
this
.
accessors
.
set
]
=
function
(
associatedObject
)
{
obj
[
this
.
accessors
.
set
]
=
function
(
associatedObject
)
{
...
@@ -66,4 +67,7 @@ HasOne.prototype.injectSetter = function(obj) {
...
@@ -66,4 +67,7 @@ HasOne.prototype.injectSetter = function(obj) {
}
}
return
this
return
this
}
}
\ No newline at end of file
return
HasOne
})()
lib/model.js
View file @
99481ee
...
@@ -13,7 +13,6 @@ module.exports = (function() {
...
@@ -13,7 +13,6 @@ module.exports = (function() {
initAttributes
.
call
(
this
,
values
)
initAttributes
.
call
(
this
,
values
)
}
}
Utils
.
addEventEmitter
(
Model
)
Utils
.
_
.
extend
(
Model
.
prototype
,
Mixin
.
prototype
)
Utils
.
_
.
extend
(
Model
.
prototype
,
Mixin
.
prototype
)
Model
.
Events
=
{
Model
.
Events
=
{
...
...
lib/query-chainer.js
View file @
99481ee
...
@@ -8,7 +8,7 @@ module.exports = (function() {
...
@@ -8,7 +8,7 @@ module.exports = (function() {
this
.
emitters
=
[]
this
.
emitters
=
[]
this
.
fails
=
[]
this
.
fails
=
[]
this
.
finished
=
false
this
.
finished
=
false
this
.
runned
=
false
this
.
wasRunning
=
false
this
.
eventEmitter
=
null
this
.
eventEmitter
=
null
emitters
=
emitters
||
[]
emitters
=
emitters
||
[]
...
@@ -25,7 +25,7 @@ module.exports = (function() {
...
@@ -25,7 +25,7 @@ module.exports = (function() {
QueryChainer
.
prototype
.
run
=
function
()
{
QueryChainer
.
prototype
.
run
=
function
()
{
var
self
=
this
var
self
=
this
this
.
eventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
this
.
eventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
self
.
runned
=
true
self
.
wasRunning
=
true
finish
.
call
(
self
)
finish
.
call
(
self
)
})
})
return
this
.
eventEmitter
.
run
()
return
this
.
eventEmitter
.
run
()
...
@@ -49,7 +49,7 @@ module.exports = (function() {
...
@@ -49,7 +49,7 @@ module.exports = (function() {
var
finish
=
function
(
result
)
{
var
finish
=
function
(
result
)
{
this
.
finished
=
(
this
.
finishedEmits
==
this
.
emitters
.
length
)
this
.
finished
=
(
this
.
finishedEmits
==
this
.
emitters
.
length
)
if
(
this
.
finished
&&
this
.
runned
)
{
if
(
this
.
finished
&&
this
.
wasRunning
)
{
var
status
=
this
.
fails
.
length
==
0
?
'success'
:
'failure'
var
status
=
this
.
fails
.
length
==
0
?
'success'
:
'failure'
result
=
this
.
fails
.
length
==
0
?
result
:
this
.
fails
result
=
this
.
fails
.
length
==
0
?
result
:
this
.
fails
this
.
eventEmitter
.
emit
(
status
,
result
)
this
.
eventEmitter
.
emit
(
status
,
result
)
...
...
lib/query.js
View file @
99481ee
...
@@ -27,6 +27,8 @@ module.exports = (function() {
...
@@ -27,6 +27,8 @@ module.exports = (function() {
return
this
return
this
}
}
//private
var
bindClient
=
function
()
{
var
bindClient
=
function
()
{
this
.
client
.
on
(
'error'
,
this
.
bindClientFunction
)
this
.
client
.
on
(
'error'
,
this
.
bindClientFunction
)
}
}
...
...
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