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 8a0b2d5b
authored
Jan 05, 2012
by
sdepold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
renamed __definition to __factory
1 parent
0ce1a625
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
32 additions
and
32 deletions
lib/associations/has-many-double-linked.js
lib/associations/has-many-single-linked.js
lib/dialects/mysql/query.js
lib/dialects/sqlite/query.js
lib/model-factory.js
lib/model.js
lib/associations/has-many-double-linked.js
View file @
8a0b2d5
...
@@ -2,7 +2,7 @@ var Utils = require('./../utils')
...
@@ -2,7 +2,7 @@ var Utils = require('./../utils')
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
HasManyDoubleLinked
=
function
(
definition
,
instance
)
{
var
HasManyDoubleLinked
=
function
(
definition
,
instance
)
{
this
.
__
definition
=
definition
this
.
__
factory
=
definition
this
.
instance
=
instance
this
.
instance
=
instance
}
}
...
@@ -11,18 +11,18 @@ module.exports = (function() {
...
@@ -11,18 +11,18 @@ module.exports = (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
.
__
factory
.
identifier
]
=
self
.
instance
.
id
var
primaryKeys
=
Utils
.
_
.
keys
(
self
.
__
definition
.
connectorModel
.
rawAttributes
)
var
primaryKeys
=
Utils
.
_
.
keys
(
self
.
__
factory
.
connectorModel
.
rawAttributes
)
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
__
definition
.
identifier
})[
0
]
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
__
factory
.
identifier
})[
0
]
self
.
__
definition
.
connectorModel
.
findAll
({
where
:
where
}).
success
(
function
(
associatedObjects
)
{
self
.
__
factory
.
connectorModel
.
findAll
({
where
:
where
}).
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
.
__
factory
.
target
.
findAll
({
where
:
'id in ('
+
ids
.
join
(
", "
)
+
')'
})
.
success
(
function
(
objects
)
{
customEventEmitter
.
emit
(
'success'
,
objects
)
})
.
success
(
function
(
objects
)
{
customEventEmitter
.
emit
(
'success'
,
objects
)
})
.
error
(
function
(
err
){
customEventEmitter
.
emit
(
'failure'
,
err
)
})
.
error
(
function
(
err
){
customEventEmitter
.
emit
(
'failure'
,
err
)
})
}
}
...
@@ -39,16 +39,16 @@ module.exports = (function() {
...
@@ -39,16 +39,16 @@ module.exports = (function() {
.
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
success
(
function
()
{
.
success
(
function
()
{
var
chainer
=
new
Utils
.
QueryChainer
var
chainer
=
new
Utils
.
QueryChainer
,
association
=
self
.
__
definition
.
target
.
associations
[
self
.
__definition
.
associationAccessor
]
,
association
=
self
.
__
factory
.
target
.
associations
[
self
.
__factory
.
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
.
__
factory
.
identifier
]
=
self
.
instance
.
id
attributes
[
foreignIdentifier
]
=
unassociatedObject
.
id
attributes
[
foreignIdentifier
]
=
unassociatedObject
.
id
chainer
.
add
(
self
.
__
definition
.
connectorModel
.
create
(
attributes
))
chainer
.
add
(
self
.
__
factory
.
connectorModel
.
create
(
attributes
))
})
})
chainer
chainer
...
@@ -65,7 +65,7 @@ module.exports = (function() {
...
@@ -65,7 +65,7 @@ module.exports = (function() {
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
.
__
factory
.
target
.
associations
[
self
.
__factory
.
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
)
...
@@ -73,13 +73,13 @@ module.exports = (function() {
...
@@ -73,13 +73,13 @@ module.exports = (function() {
obsoleteAssociations
.
forEach
(
function
(
associatedObject
)
{
obsoleteAssociations
.
forEach
(
function
(
associatedObject
)
{
var
where
=
{}
var
where
=
{}
,
primaryKeys
=
Utils
.
_
.
keys
(
self
.
__
definition
.
connectorModel
.
rawAttributes
)
,
primaryKeys
=
Utils
.
_
.
keys
(
self
.
__
factory
.
connectorModel
.
rawAttributes
)
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
__
definition
.
identifier
})[
0
]
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
__
factory
.
identifier
})[
0
]
where
[
self
.
__
definition
.
identifier
]
=
self
.
instance
.
id
where
[
self
.
__
factory
.
identifier
]
=
self
.
instance
.
id
where
[
foreignKey
]
=
associatedObject
.
id
where
[
foreignKey
]
=
associatedObject
.
id
self
.
__
definition
.
connectorModel
.
find
({
where
:
where
}).
success
(
function
(
connector
)
{
self
.
__
factory
.
connectorModel
.
find
({
where
:
where
}).
success
(
function
(
connector
)
{
chainer
.
add
(
connector
.
destroy
())
chainer
.
add
(
connector
.
destroy
())
if
(
chainer
.
emitters
.
length
==
obsoleteAssociations
.
length
)
{
if
(
chainer
.
emitters
.
length
==
obsoleteAssociations
.
length
)
{
...
...
lib/associations/has-many-single-linked.js
View file @
8a0b2d5
...
@@ -2,15 +2,15 @@ var Utils = require('./../utils')
...
@@ -2,15 +2,15 @@ var Utils = require('./../utils')
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
HasManySingleLinked
=
function
(
definition
,
instance
)
{
var
HasManySingleLinked
=
function
(
definition
,
instance
)
{
this
.
__
definition
=
definition
this
.
__
factory
=
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
.
__
factory
.
identifier
]
=
this
.
instance
.
id
return
this
.
__
definition
.
target
.
findAll
({
where
:
where
})
return
this
.
__
factory
.
target
.
findAll
({
where
:
where
})
}
}
HasManySingleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
HasManySingleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
...
@@ -18,14 +18,14 @@ module.exports = (function() {
...
@@ -18,14 +18,14 @@ module.exports = (function() {
// clear the old associations
// clear the old associations
oldAssociations
.
forEach
(
function
(
associatedObject
)
{
oldAssociations
.
forEach
(
function
(
associatedObject
)
{
associatedObject
[
self
.
__
definition
.
identifier
]
=
null
associatedObject
[
self
.
__
factory
.
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
.
__
factory
.
identifier
]
=
self
.
instance
.
id
chainer
.
add
(
associatedObject
.
save
())
chainer
.
add
(
associatedObject
.
save
())
})
})
chainer
chainer
...
...
lib/dialects/mysql/query.js
View file @
8a0b2d5
...
@@ -59,7 +59,7 @@ module.exports = (function() {
...
@@ -59,7 +59,7 @@ module.exports = (function() {
// add the inserted row id to the instance
// add the inserted row id to the instance
if
(
this
.
callee
&&
(
this
.
sql
.
indexOf
(
'INSERT INTO'
)
==
0
)
&&
(
results
.
hasOwnProperty
(
'insertId'
)))
if
(
this
.
callee
&&
(
this
.
sql
.
indexOf
(
'INSERT INTO'
)
==
0
)
&&
(
results
.
hasOwnProperty
(
'insertId'
)))
this
.
callee
[
this
.
callee
.
__
definition
.
autoIncrementField
]
=
results
.
insertId
this
.
callee
[
this
.
callee
.
__
factory
.
autoIncrementField
]
=
results
.
insertId
if
(
this
.
sql
.
indexOf
(
'SELECT'
)
==
0
)
{
if
(
this
.
sql
.
indexOf
(
'SELECT'
)
==
0
)
{
// transform results into real model instances
// transform results into real model instances
...
...
lib/dialects/sqlite/query.js
View file @
8a0b2d5
...
@@ -53,7 +53,7 @@ module.exports = (function() {
...
@@ -53,7 +53,7 @@ module.exports = (function() {
// add the inserted row id to the instance
// add the inserted row id to the instance
if
(
this
.
callee
&&
(
this
.
sql
.
indexOf
(
'INSERT INTO'
)
==
0
)
&&
metaData
.
hasOwnProperty
(
'lastID'
))
{
if
(
this
.
callee
&&
(
this
.
sql
.
indexOf
(
'INSERT INTO'
)
==
0
)
&&
metaData
.
hasOwnProperty
(
'lastID'
))
{
var
autoIncrementField
=
this
.
callee
.
__
definition
.
autoIncrementField
var
autoIncrementField
=
this
.
callee
.
__
factory
.
autoIncrementField
this
.
callee
[
autoIncrementField
]
=
metaData
.
lastID
this
.
callee
[
autoIncrementField
]
=
metaData
.
lastID
}
}
...
...
lib/model-factory.js
View file @
8a0b2d5
...
@@ -141,7 +141,7 @@ module.exports = (function() {
...
@@ -141,7 +141,7 @@ module.exports = (function() {
,
self
=
this
,
self
=
this
options
=
options
||
{}
options
=
options
||
{}
instance
.
__
definition
=
this
instance
.
__
factory
=
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/model.js
View file @
8a0b2d5
...
@@ -7,7 +7,7 @@ module.exports = (function() {
...
@@ -7,7 +7,7 @@ module.exports = (function() {
var
Model
=
function
(
values
,
options
)
{
var
Model
=
function
(
values
,
options
)
{
var
self
=
this
var
self
=
this
this
.
__
definition
=
null
// will be set in Model.build
this
.
__
factory
=
null
// will be set in Model.build
this
.
attributes
=
[]
this
.
attributes
=
[]
this
.
validators
=
{}
// holds validation settings for each attribute
this
.
validators
=
{}
// holds validation settings for each attribute
this
.
__options
=
Utils
.
_
.
extend
({
this
.
__options
=
Utils
.
_
.
extend
({
...
@@ -22,7 +22,7 @@ module.exports = (function() {
...
@@ -22,7 +22,7 @@ module.exports = (function() {
Utils
.
_
.
extend
(
Model
.
prototype
,
Mixin
.
prototype
)
Utils
.
_
.
extend
(
Model
.
prototype
,
Mixin
.
prototype
)
Object
.
defineProperty
(
Model
.
prototype
,
'sequelize'
,
{
Object
.
defineProperty
(
Model
.
prototype
,
'sequelize'
,
{
get
:
function
(){
return
this
.
__
definition
.
modelManager
.
sequelize
}
get
:
function
(){
return
this
.
__
factory
.
modelManager
.
sequelize
}
})
})
Object
.
defineProperty
(
Model
.
prototype
,
'QueryInterface'
,
{
Object
.
defineProperty
(
Model
.
prototype
,
'QueryInterface'
,
{
...
@@ -40,10 +40,10 @@ module.exports = (function() {
...
@@ -40,10 +40,10 @@ module.exports = (function() {
this
[
updatedAtAttr
]
=
new
Date
()
this
[
updatedAtAttr
]
=
new
Date
()
if
(
this
.
isNewRecord
)
{
if
(
this
.
isNewRecord
)
{
return
this
.
QueryInterface
.
insert
(
this
,
this
.
__
definition
.
tableName
,
this
.
values
)
return
this
.
QueryInterface
.
insert
(
this
,
this
.
__
factory
.
tableName
,
this
.
values
)
}
else
{
}
else
{
var
identifier
=
this
.
__options
.
hasPrimaryKeys
?
this
.
primaryKeyValues
:
this
.
id
var
identifier
=
this
.
__options
.
hasPrimaryKeys
?
this
.
primaryKeyValues
:
this
.
id
,
tableName
=
this
.
__
definition
.
tableName
,
tableName
=
this
.
__
factory
.
tableName
,
query
=
this
.
QueryInterface
.
update
(
this
,
tableName
,
this
.
values
,
identifier
)
,
query
=
this
.
QueryInterface
.
update
(
this
,
tableName
,
this
.
values
,
identifier
)
return
query
return
query
...
@@ -119,7 +119,7 @@ module.exports = (function() {
...
@@ -119,7 +119,7 @@ module.exports = (function() {
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
.
__
factory
.
primaryKeys
)
readOnlyAttributes
.
push
(
'id'
)
readOnlyAttributes
.
push
(
'id'
)
readOnlyAttributes
.
push
(
'createdAt'
)
readOnlyAttributes
.
push
(
'createdAt'
)
readOnlyAttributes
.
push
(
'updatedAt'
)
readOnlyAttributes
.
push
(
'updatedAt'
)
...
@@ -143,16 +143,16 @@ module.exports = (function() {
...
@@ -143,16 +143,16 @@ module.exports = (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
.
QueryInterface
.
delete
(
this
,
this
.
__
definition
.
tableName
,
identifier
)
return
this
.
QueryInterface
.
delete
(
this
,
this
.
__
factory
.
tableName
,
identifier
)
}
}
}
}
Model
.
prototype
.
__defineGetter__
(
"identifiers"
,
function
()
{
Model
.
prototype
.
__defineGetter__
(
"identifiers"
,
function
()
{
var
primaryKeys
=
Utils
.
_
.
keys
(
this
.
__
definition
.
primaryKeys
)
var
primaryKeys
=
Utils
.
_
.
keys
(
this
.
__
factory
.
primaryKeys
)
,
result
=
{}
,
result
=
{}
,
self
=
this
,
self
=
this
if
(
!
this
.
__
definition
.
hasPrimaryKeys
)
if
(
!
this
.
__
factory
.
hasPrimaryKeys
)
primaryKeys
=
[
'id'
]
primaryKeys
=
[
'id'
]
primaryKeys
.
forEach
(
function
(
identifier
)
{
primaryKeys
.
forEach
(
function
(
identifier
)
{
...
@@ -184,7 +184,7 @@ module.exports = (function() {
...
@@ -184,7 +184,7 @@ module.exports = (function() {
var
result
=
{}
var
result
=
{}
,
self
=
this
,
self
=
this
Utils
.
_
.
each
(
this
.
__
definition
.
primaryKeys
,
function
(
_
,
attr
)
{
Utils
.
_
.
each
(
this
.
__
factory
.
primaryKeys
,
function
(
_
,
attr
)
{
result
[
attr
]
=
self
[
attr
]
result
[
attr
]
=
self
[
attr
]
})
})
...
@@ -248,7 +248,7 @@ module.exports = (function() {
...
@@ -248,7 +248,7 @@ module.exports = (function() {
var
query
=
function
()
{
var
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
.
__
factory
.
modelManager
.
sequelize
args
.
push
(
this
)
args
.
push
(
this
)
return
s
.
query
.
apply
(
s
,
args
)
return
s
.
query
.
apply
(
s
,
args
)
...
...
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