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 f1c73c6e
authored
Oct 24, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
scoped attributes
1 parent
17128deb
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
30 additions
and
30 deletions
lib/sequelize/associations/has-many-double-linked.js
lib/sequelize/associations/has-many-single-linked.js
lib/sequelize/model-definition.js
lib/sequelize/model.js
lib/sequelize/query.js
lib/sequelize/associations/has-many-double-linked.js
View file @
f1c73c6
var
Utils
=
require
(
'./../utils'
)
var
Utils
=
require
(
'./../utils'
)
var
HasManyDoubleLinked
=
module
.
exports
=
function
(
definition
,
instance
)
{
var
HasManyDoubleLinked
=
module
.
exports
=
function
(
definition
,
instance
)
{
this
.
definition
=
definition
this
.
__
definition
=
definition
this
.
instance
=
instance
this
.
instance
=
instance
}
}
...
@@ -10,18 +10,18 @@ HasManyDoubleLinked.prototype.injectGetter = function() {
...
@@ -10,18 +10,18 @@ HasManyDoubleLinked.prototype.injectGetter = function() {
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
where
=
{}
var
where
=
{}
where
[
self
.
definition
.
identifier
]
=
self
.
instance
.
id
where
[
self
.
__
definition
.
identifier
]
=
self
.
instance
.
id
var
primaryKeys
=
Utils
.
_
.
keys
(
self
.
definition
.
connectorModel
.
rawAttributes
)
var
primaryKeys
=
Utils
.
_
.
keys
(
self
.
__
definition
.
connectorModel
.
rawAttributes
)
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
definition
.
identifier
})[
0
]
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
__
definition
.
identifier
})[
0
]
self
.
definition
.
connectorModel
.
findAll
({
where
:
where
}).
on
(
'success'
,
function
(
associatedObjects
)
{
self
.
__
definition
.
connectorModel
.
findAll
({
where
:
where
}).
on
(
'success'
,
function
(
associatedObjects
)
{
var
ids
=
associatedObjects
.
map
(
function
(
obj
)
{
return
obj
[
foreignKey
]
})
var
ids
=
associatedObjects
.
map
(
function
(
obj
)
{
return
obj
[
foreignKey
]
})
if
(
ids
.
length
==
0
)
{
if
(
ids
.
length
==
0
)
{
customEventEmitter
.
emit
(
'success'
,
[])
customEventEmitter
.
emit
(
'success'
,
[])
}
else
{
}
else
{
self
.
definition
.
target
.
findAll
({
where
:
'id in ('
+
ids
.
join
(
", "
)
+
')'
})
self
.
__
definition
.
target
.
findAll
({
where
:
'id in ('
+
ids
.
join
(
", "
)
+
')'
})
.
on
(
'success'
,
function
(
objects
)
{
customEventEmitter
.
emit
(
'success'
,
objects
)
})
.
on
(
'success'
,
function
(
objects
)
{
customEventEmitter
.
emit
(
'success'
,
objects
)
})
.
on
(
'failure'
,
function
(
err
){
customEventEmitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'failure'
,
function
(
err
){
customEventEmitter
.
emit
(
'failure'
,
err
)
})
}
}
...
@@ -36,7 +36,7 @@ HasManyDoubleLinked.prototype.destroyObsoleteAssociations = function(oldAssociat
...
@@ -36,7 +36,7 @@ HasManyDoubleLinked.prototype.destroyObsoleteAssociations = function(oldAssociat
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
chainer
=
new
Utils
.
QueryChainer
var
chainer
=
new
Utils
.
QueryChainer
var
foreignIdentifier
=
self
.
definition
.
target
.
associations
[
self
.
definition
.
associationAccessor
].
identifier
var
foreignIdentifier
=
self
.
__definition
.
target
.
associations
[
self
.
__
definition
.
associationAccessor
].
identifier
var
obsoleteAssociations
=
oldAssociations
.
filter
(
function
(
obj
)
{
return
!
obj
.
equalsOneOf
(
newAssociations
)
})
var
obsoleteAssociations
=
oldAssociations
.
filter
(
function
(
obj
)
{
return
!
obj
.
equalsOneOf
(
newAssociations
)
})
if
(
obsoleteAssociations
.
length
==
0
)
if
(
obsoleteAssociations
.
length
==
0
)
...
@@ -44,13 +44,13 @@ HasManyDoubleLinked.prototype.destroyObsoleteAssociations = function(oldAssociat
...
@@ -44,13 +44,13 @@ HasManyDoubleLinked.prototype.destroyObsoleteAssociations = function(oldAssociat
obsoleteAssociations
.
forEach
(
function
(
associatedObject
)
{
obsoleteAssociations
.
forEach
(
function
(
associatedObject
)
{
var
where
=
{}
var
where
=
{}
,
primaryKeys
=
Utils
.
_
.
keys
(
self
.
definition
.
connectorModel
.
rawAttributes
)
,
primaryKeys
=
Utils
.
_
.
keys
(
self
.
__
definition
.
connectorModel
.
rawAttributes
)
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
definition
.
identifier
})[
0
]
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
__
definition
.
identifier
})[
0
]
where
[
self
.
definition
.
identifier
]
=
self
.
instance
.
id
where
[
self
.
__
definition
.
identifier
]
=
self
.
instance
.
id
where
[
foreignKey
]
=
associatedObject
.
id
where
[
foreignKey
]
=
associatedObject
.
id
self
.
definition
.
connectorModel
.
find
({
where
:
where
}).
on
(
'success'
,
function
(
connector
)
{
self
.
__
definition
.
connectorModel
.
find
({
where
:
where
}).
on
(
'success'
,
function
(
connector
)
{
chainer
.
add
(
connector
.
destroy
())
chainer
.
add
(
connector
.
destroy
())
if
(
chainer
.
emitters
.
length
==
obsoleteAssociations
.
length
)
{
if
(
chainer
.
emitters
.
length
==
obsoleteAssociations
.
length
)
{
...
@@ -73,16 +73,16 @@ HasManyDoubleLinked.prototype.injectSetter = function(emitter, oldAssociations,
...
@@ -73,16 +73,16 @@ HasManyDoubleLinked.prototype.injectSetter = function(emitter, oldAssociations,
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'success'
,
function
()
{
.
on
(
'success'
,
function
()
{
var
chainer
=
new
Utils
.
QueryChainer
var
chainer
=
new
Utils
.
QueryChainer
,
association
=
self
.
definition
.
target
.
associations
[
self
.
definition
.
associationAccessor
]
,
association
=
self
.
__definition
.
target
.
associations
[
self
.
__
definition
.
associationAccessor
]
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
,
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
return
!
obj
.
equalsOneOf
(
oldAssociations
)
})
,
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
return
!
obj
.
equalsOneOf
(
oldAssociations
)
})
unassociatedObjects
.
forEach
(
function
(
unassociatedObject
)
{
unassociatedObjects
.
forEach
(
function
(
unassociatedObject
)
{
var
attributes
=
{}
var
attributes
=
{}
attributes
[
self
.
definition
.
identifier
]
=
self
.
instance
.
id
attributes
[
self
.
__
definition
.
identifier
]
=
self
.
instance
.
id
attributes
[
foreignIdentifier
]
=
unassociatedObject
.
id
attributes
[
foreignIdentifier
]
=
unassociatedObject
.
id
chainer
.
add
(
self
.
definition
.
connectorModel
.
create
(
attributes
))
chainer
.
add
(
self
.
__
definition
.
connectorModel
.
create
(
attributes
))
})
})
chainer
chainer
...
...
lib/sequelize/associations/has-many-single-linked.js
View file @
f1c73c6
var
Utils
=
require
(
'./../utils'
)
var
Utils
=
require
(
'./../utils'
)
var
HasManySingleLinked
=
module
.
exports
=
function
(
definition
,
instance
)
{
var
HasManySingleLinked
=
module
.
exports
=
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
)
{
...
@@ -17,14 +17,14 @@ HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations,
...
@@ -17,14 +17,14 @@ HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations,
// clear the old associations
// clear the old associations
oldAssociations
.
forEach
(
function
(
associatedObject
)
{
oldAssociations
.
forEach
(
function
(
associatedObject
)
{
associatedObject
[
self
.
definition
.
identifier
]
=
null
associatedObject
[
self
.
__
definition
.
identifier
]
=
null
associatedObject
.
save
()
associatedObject
.
save
()
})
})
// set the new one
// set the new one
var
chainer
=
new
Utils
.
QueryChainer
var
chainer
=
new
Utils
.
QueryChainer
newAssociations
.
forEach
(
function
(
associatedObject
)
{
newAssociations
.
forEach
(
function
(
associatedObject
)
{
associatedObject
[
self
.
definition
.
identifier
]
=
self
.
instance
.
id
associatedObject
[
self
.
__
definition
.
identifier
]
=
self
.
instance
.
id
chainer
.
add
(
associatedObject
.
save
())
chainer
.
add
(
associatedObject
.
save
())
})
})
chainer
chainer
...
...
lib/sequelize/model-definition.js
View file @
f1c73c6
...
@@ -141,7 +141,7 @@ ModelDefinition.prototype.build = function(values, options) {
...
@@ -141,7 +141,7 @@ ModelDefinition.prototype.build = function(values, options) {
,
self
=
this
,
self
=
this
options
=
options
||
{}
options
=
options
||
{}
instance
.
definition
=
this
instance
.
__
definition
=
this
Utils
.
_
.
map
(
this
.
attributes
,
function
(
definition
,
name
)
{
Utils
.
_
.
map
(
this
.
attributes
,
function
(
definition
,
name
)
{
if
(
typeof
instance
[
name
]
==
'undefined'
)
{
if
(
typeof
instance
[
name
]
==
'undefined'
)
{
...
...
lib/sequelize/model.js
View file @
f1c73c6
...
@@ -5,7 +5,7 @@ var Utils = require("./utils")
...
@@ -5,7 +5,7 @@ var Utils = require("./utils")
var
Model
=
module
.
exports
=
function
(
values
,
options
)
{
var
Model
=
module
.
exports
=
function
(
values
,
options
)
{
var
self
=
this
var
self
=
this
this
.
definition
=
null
// will be set in Model.build
this
.
__
definition
=
null
// will be set in Model.build
this
.
attributes
=
[]
this
.
attributes
=
[]
this
.
__options
=
options
||
{}
this
.
__options
=
options
||
{}
...
@@ -45,7 +45,7 @@ Model.prototype.addAttribute = function(attribute, value) {
...
@@ -45,7 +45,7 @@ Model.prototype.addAttribute = function(attribute, value) {
Model
.
prototype
.
query
=
function
()
{
Model
.
prototype
.
query
=
function
()
{
var
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
var
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
,
s
=
this
.
definition
.
modelManager
.
sequelize
,
s
=
this
.
__
definition
.
modelManager
.
sequelize
args
.
push
(
this
)
args
.
push
(
this
)
return
s
.
query
.
apply
(
s
,
args
)
return
s
.
query
.
apply
(
s
,
args
)
...
@@ -60,7 +60,7 @@ Model.prototype.save = function() {
...
@@ -60,7 +60,7 @@ Model.prototype.save = function() {
if
(
this
.
isNewRecord
)
{
if
(
this
.
isNewRecord
)
{
var
self
=
this
var
self
=
this
var
eventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
eventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
self
.
query
(
QueryGenerator
.
insertQuery
(
self
.
definition
.
tableName
,
self
.
values
))
self
.
query
(
QueryGenerator
.
insertQuery
(
self
.
__
definition
.
tableName
,
self
.
values
))
.
on
(
'success'
,
function
(
obj
)
{
.
on
(
'success'
,
function
(
obj
)
{
obj
.
isNewRecord
=
false
obj
.
isNewRecord
=
false
eventEmitter
.
emit
(
'success'
,
obj
)
eventEmitter
.
emit
(
'success'
,
obj
)
...
@@ -70,14 +70,14 @@ Model.prototype.save = function() {
...
@@ -70,14 +70,14 @@ Model.prototype.save = function() {
return
eventEmitter
.
run
()
return
eventEmitter
.
run
()
}
else
{
}
else
{
var
identifier
=
this
.
__options
.
hasPrimaryKeys
?
this
.
primaryKeyValues
:
this
.
id
var
identifier
=
this
.
__options
.
hasPrimaryKeys
?
this
.
primaryKeyValues
:
this
.
id
return
this
.
query
(
QueryGenerator
.
updateQuery
(
this
.
definition
.
tableName
,
this
.
values
,
identifier
))
return
this
.
query
(
QueryGenerator
.
updateQuery
(
this
.
__
definition
.
tableName
,
this
.
values
,
identifier
))
}
}
}
}
Model
.
prototype
.
updateAttributes
=
function
(
updates
)
{
Model
.
prototype
.
updateAttributes
=
function
(
updates
)
{
var
self
=
this
var
self
=
this
var
readOnlyAttributes
=
Utils
.
_
.
keys
(
this
.
definition
.
primaryKeys
)
var
readOnlyAttributes
=
Utils
.
_
.
keys
(
this
.
__
definition
.
primaryKeys
)
readOnlyAttributes
.
push
(
'id'
)
readOnlyAttributes
.
push
(
'id'
)
readOnlyAttributes
.
push
(
'createdAt'
)
readOnlyAttributes
.
push
(
'createdAt'
)
readOnlyAttributes
.
push
(
'updatedAt'
)
readOnlyAttributes
.
push
(
'updatedAt'
)
...
@@ -100,16 +100,16 @@ Model.prototype.destroy = function() {
...
@@ -100,16 +100,16 @@ Model.prototype.destroy = function() {
return
this
.
save
()
return
this
.
save
()
}
else
{
}
else
{
var
identifier
=
this
.
__options
.
hasPrimaryKeys
?
this
.
primaryKeyValues
:
this
.
id
var
identifier
=
this
.
__options
.
hasPrimaryKeys
?
this
.
primaryKeyValues
:
this
.
id
return
this
.
query
(
QueryGenerator
.
deleteQuery
(
this
.
definition
.
tableName
,
identifier
))
return
this
.
query
(
QueryGenerator
.
deleteQuery
(
this
.
__
definition
.
tableName
,
identifier
))
}
}
}
}
Model
.
prototype
.
__defineGetter__
(
"identifiers"
,
function
()
{
Model
.
prototype
.
__defineGetter__
(
"identifiers"
,
function
()
{
var
primaryKeys
=
Utils
.
_
.
keys
(
this
.
definition
.
primaryKeys
)
var
primaryKeys
=
Utils
.
_
.
keys
(
this
.
__
definition
.
primaryKeys
)
,
result
=
{}
,
result
=
{}
,
self
=
this
,
self
=
this
if
(
!
this
.
definition
.
hasPrimaryKeys
)
if
(
!
this
.
__
definition
.
hasPrimaryKeys
)
primaryKeys
=
[
'id'
]
primaryKeys
=
[
'id'
]
primaryKeys
.
forEach
(
function
(
identifier
)
{
primaryKeys
.
forEach
(
function
(
identifier
)
{
...
@@ -141,7 +141,7 @@ Model.prototype.__defineGetter__('primaryKeyValues', function() {
...
@@ -141,7 +141,7 @@ Model.prototype.__defineGetter__('primaryKeyValues', function() {
var
result
=
{}
var
result
=
{}
,
self
=
this
,
self
=
this
Utils
.
_
.
each
(
this
.
definition
.
primaryKeys
,
function
(
_
,
attr
)
{
Utils
.
_
.
each
(
this
.
__
definition
.
primaryKeys
,
function
(
_
,
attr
)
{
result
[
attr
]
=
self
[
attr
]
result
[
attr
]
=
self
[
attr
]
})
})
...
...
lib/sequelize/query.js
View file @
f1c73c6
...
@@ -39,7 +39,7 @@ Query.prototype.onSuccess = function(query, results, fields) {
...
@@ -39,7 +39,7 @@ Query.prototype.onSuccess = function(query, results, fields) {
// add the inserted row id to the instance
// add the inserted row id to the instance
if
(
this
.
callee
&&
(
query
.
indexOf
(
'INSERT INTO'
)
==
0
)
&&
(
results
.
hasOwnProperty
(
'insertId'
)))
if
(
this
.
callee
&&
(
query
.
indexOf
(
'INSERT INTO'
)
==
0
)
&&
(
results
.
hasOwnProperty
(
'insertId'
)))
this
.
callee
[
this
.
callee
.
definition
.
autoIncrementField
]
=
results
.
insertId
this
.
callee
[
this
.
callee
.
__
definition
.
autoIncrementField
]
=
results
.
insertId
// transform results into real model instances
// transform results into real model instances
// return the first real model instance if options.plain is set (e.g. Model.find)
// return the first real model instance if options.plain is set (e.g. Model.find)
...
...
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