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 ffeddc46
authored
Nov 10, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use private method scoping
1 parent
59605e88
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
176 additions
and
172 deletions
lib/model-factory.js
lib/model-factory.js
View file @
ffeddc4
...
...
@@ -2,216 +2,220 @@ var Utils = require("./utils")
,
Model
=
require
(
"./model"
)
,
DataTypes
=
require
(
"./data-types"
)
var
ModelFactory
=
module
.
exports
=
function
(
name
,
attributes
,
options
)
{
var
self
=
this
this
.
options
=
options
||
{}
this
.
options
.
timestamps
=
this
.
options
.
hasOwnProperty
(
'timestamps'
)
?
this
.
options
.
timestamps
:
true
this
.
name
=
name
this
.
tableName
=
this
.
options
.
freezeTableName
?
name
:
Utils
.
pluralize
(
name
)
this
.
attributes
=
Utils
.
simplifyAttributes
(
attributes
)
this
.
rawAttributes
=
attributes
this
.
modelManager
=
null
// defined by model-manager during addModel
this
.
associations
=
{}
this
.
__addDefaultAttributes
()
this
.
__addOptionalClassMethods
()
this
.
__findAutoIncrementField
()
}
ModelFactory
.
prototype
.
__defineGetter__
(
'QueryGenerator'
,
function
()
{
return
this
.
modelManager
.
sequelize
.
connectorManager
.
getQueryGenerator
()
})
ModelFactory
.
prototype
.
sync
=
function
(
options
)
{
options
=
Utils
.
merge
(
options
||
{},
this
.
options
)
var
self
=
this
var
eventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
doQuery
=
function
()
{
self
.
__query
(
self
.
QueryGenerator
.
createTableQuery
(
self
.
tableName
,
self
.
attributes
,
options
))
.
on
(
'success'
,
function
()
{
eventEmitter
.
emit
(
'success'
,
self
)
})
.
on
(
'failure'
,
function
(
err
)
{
eventEmitter
.
emit
(
'failure'
,
err
)
})
}
module
.
exports
=
(
function
()
{
var
ModelFactory
=
function
(
name
,
attributes
,
options
)
{
var
self
=
this
this
.
options
=
options
||
{}
this
.
options
.
timestamps
=
this
.
options
.
hasOwnProperty
(
'timestamps'
)
?
this
.
options
.
timestamps
:
true
this
.
name
=
name
this
.
tableName
=
this
.
options
.
freezeTableName
?
name
:
Utils
.
pluralize
(
name
)
this
.
attributes
=
Utils
.
simplifyAttributes
(
attributes
)
this
.
rawAttributes
=
attributes
this
.
modelManager
=
null
// defined by model-manager during addModel
this
.
associations
=
{}
addDefaultAttributes
.
call
(
this
)
addOptionalClassMethods
.
call
(
this
)
findAutoIncrementField
.
call
(
this
)
}
if
(
options
.
force
)
{
self
.
drop
()
.
on
(
'success'
,
function
()
{
doQuery
()
})
.
on
(
'failure'
,
function
(
err
)
{
eventEmitter
.
emit
(
'failure'
,
err
)
})
}
else
{
doQuery
()
}
ModelFactory
.
prototype
.
__defineGetter__
(
'QueryGenerator'
,
function
()
{
return
this
.
modelManager
.
sequelize
.
connectorManager
.
getQueryGenerator
()
})
return
eventEmitter
.
run
()
}
ModelFactory
.
prototype
.
sync
=
function
(
options
)
{
options
=
Utils
.
merge
(
options
||
{},
this
.
options
)
ModelFactory
.
prototype
.
drop
=
function
()
{
return
this
.
__query
(
this
.
QueryGenerator
.
dropTableQuery
(
this
.
tableName
,
this
.
id
))
}
var
self
=
this
var
eventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
doQuery
=
function
()
{
query
.
call
(
self
,
self
.
QueryGenerator
.
createTableQuery
(
self
.
tableName
,
self
.
attributes
,
options
))
.
on
(
'success'
,
function
()
{
eventEmitter
.
emit
(
'success'
,
self
)
})
.
on
(
'failure'
,
function
(
err
)
{
eventEmitter
.
emit
(
'failure'
,
err
)
})
}
if
(
options
.
force
)
{
self
.
drop
()
.
on
(
'success'
,
function
()
{
doQuery
()
})
.
on
(
'failure'
,
function
(
err
)
{
eventEmitter
.
emit
(
'failure'
,
err
)
})
}
else
{
doQuery
()
}
})
ModelFactory
.
prototype
.
all
=
function
()
{
return
this
.
__query
(
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
))
}
return
eventEmitter
.
run
()
}
ModelFactory
.
prototype
.
count
=
function
(
options
)
{
var
self
=
this
ModelFactory
.
prototype
.
drop
=
function
()
{
return
query
.
call
(
this
,
this
.
QueryGenerator
.
dropTableQuery
(
this
.
tableName
,
this
.
id
))
}
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
self
.
__query
(
self
.
QueryGenerator
.
countQuery
(
self
.
tableName
,
options
),
self
,
{
plain
:
true
}).
on
(
'success'
,
function
(
obj
)
{
emitter
.
emit
(
'success'
,
obj
[
'count(*)'
])
})
})
return
emitter
.
run
()
}
ModelFactory
.
prototype
.
all
=
function
()
{
return
query
.
call
(
this
,
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
))
}
ModelFactory
.
prototype
.
max
=
function
(
field
,
options
)
{
var
self
=
this
ModelFactory
.
prototype
.
count
=
function
(
options
)
{
var
self
=
this
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
self
.
__query
(
self
.
QueryGenerator
.
maxQuery
(
self
.
tableName
,
field
,
options
),
self
,
{
plain
:
true
}).
on
(
'success'
,
function
(
obj
)
{
emitter
.
emit
(
'success'
,
obj
[
'max'
])
})
})
return
emitter
.
run
()
}
ModelFactory
.
prototype
.
min
=
function
(
field
,
options
)
{
var
self
=
this
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
self
.
__query
(
self
.
QueryGenerator
.
minQuery
(
self
.
tableName
,
field
,
options
),
self
,
{
plain
:
true
}).
on
(
'success'
,
function
(
obj
)
{
emitter
.
emit
(
'success'
,
obj
[
'min'
])
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
query
.
call
(
self
,
self
.
QueryGenerator
.
countQuery
(
self
.
tableName
,
options
),
self
,
{
plain
:
true
}).
on
(
'success'
,
function
(
obj
)
{
emitter
.
emit
(
'success'
,
obj
[
'count(*)'
])
})
})
})
return
emitter
.
run
()
}
ModelFactory
.
prototype
.
findAll
=
function
(
options
)
{
return
this
.
__query
(
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
,
options
))
}
ModelFactory
.
prototype
.
find
=
function
(
options
)
{
// options is not a hash but an id
if
(
typeof
options
==
'number'
)
options
=
{
where
:
options
}
else
if
(
Utils
.
argsArePrimaryKeys
(
arguments
,
this
.
primaryKeys
))
{
var
where
=
{}
,
self
=
this
Utils
.
_
.
each
(
arguments
,
function
(
arg
,
i
)
{
var
key
=
Utils
.
_
.
keys
(
self
.
primaryKeys
)[
i
]
where
[
key
]
=
arg
return
emitter
.
run
()
}
ModelFactory
.
prototype
.
max
=
function
(
field
,
options
)
{
var
self
=
this
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
query
.
call
(
self
,
self
.
QueryGenerator
.
maxQuery
(
self
.
tableName
,
field
,
options
),
self
,
{
plain
:
true
}).
on
(
'success'
,
function
(
obj
)
{
emitter
.
emit
(
'success'
,
obj
[
'max'
])
})
})
return
emitter
.
run
()
}
ModelFactory
.
prototype
.
min
=
function
(
field
,
options
)
{
var
self
=
this
options
=
{
where
:
where
}
}
else
if
((
options
==
null
)
||
(
options
==
undefined
))
{
var
NullEmitter
=
require
(
"./null-emitter"
)
return
new
NullEmitter
()
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
query
.
call
(
self
,
self
.
QueryGenerator
.
minQuery
(
self
.
tableName
,
field
,
options
),
self
,
{
plain
:
true
}).
on
(
'success'
,
function
(
obj
)
{
emitter
.
emit
(
'success'
,
obj
[
'min'
])
})
})
return
emitter
.
run
()
}
ModelFactory
.
prototype
.
findAll
=
function
(
options
)
{
return
query
.
call
(
this
,
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
,
options
))
}
options
.
limit
=
1
ModelFactory
.
prototype
.
find
=
function
(
options
)
{
// options is not a hash but an id
if
(
typeof
options
==
'number'
)
options
=
{
where
:
options
}
else
if
(
Utils
.
argsArePrimaryKeys
(
arguments
,
this
.
primaryKeys
))
{
var
where
=
{}
,
self
=
this
Utils
.
_
.
each
(
arguments
,
function
(
arg
,
i
)
{
var
key
=
Utils
.
_
.
keys
(
self
.
primaryKeys
)[
i
]
where
[
key
]
=
arg
})
options
=
{
where
:
where
}
}
else
if
((
options
==
null
)
||
(
options
==
undefined
))
{
var
NullEmitter
=
require
(
"./null-emitter"
)
return
new
NullEmitter
()
}
var
query
=
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
,
options
)
return
this
.
__query
(
query
,
this
,
{
plain
:
true
})
}
options
.
limit
=
1
ModelFactory
.
prototype
.
build
=
function
(
values
,
options
)
{
var
instance
=
new
Model
(
values
,
Utils
.
_
.
extend
(
this
.
options
,
this
.
attributes
,
{
hasPrimaryKeys
:
this
.
hasPrimaryKeys
})
)
,
self
=
this
var
sql
=
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
,
options
)
return
query
.
call
(
this
,
sql
,
this
,
{
plain
:
true
}
)
}
options
=
options
||
{}
instance
.
__definition
=
this
ModelFactory
.
prototype
.
build
=
function
(
values
,
options
)
{
var
instance
=
new
Model
(
values
,
Utils
.
_
.
extend
(
this
.
options
,
this
.
attributes
,
{
hasPrimaryKeys
:
this
.
hasPrimaryKeys
}))
,
self
=
this
Utils
.
_
.
map
(
this
.
attributes
,
function
(
definition
,
name
)
{
if
(
typeof
instance
[
name
]
==
'undefined'
)
{
var
value
=
null
options
=
options
||
{}
instance
.
__definition
=
this
if
(
self
.
rawAttributes
.
hasOwnProperty
(
name
)
&&
self
.
rawAttributes
[
name
].
hasOwnProperty
(
'defaultValue'
))
value
=
self
.
rawAttributes
[
name
].
defaultValue
Utils
.
_
.
map
(
this
.
attributes
,
function
(
definition
,
name
)
{
if
(
typeof
instance
[
name
]
==
'undefined'
)
{
var
value
=
null
instance
[
name
]
=
value
instance
.
addAttribute
(
name
,
value
)
}
})
Utils
.
_
.
each
(
this
.
options
.
instanceMethods
||
{},
function
(
fct
,
name
)
{
instance
[
name
]
=
fct
})
Utils
.
_
.
each
(
this
.
associations
,
function
(
association
,
associationName
)
{
association
.
injectGetter
(
instance
)
association
.
injectSetter
(
instance
)
})
if
(
self
.
rawAttributes
.
hasOwnProperty
(
name
)
&&
self
.
rawAttributes
[
name
].
hasOwnProperty
(
'defaultValue'
))
value
=
self
.
rawAttributes
[
name
].
defaultValue
instance
.
isNewRecord
=
options
.
hasOwnProperty
(
'isNewRecord'
)
?
options
.
isNewRecord
:
true
instance
[
name
]
=
value
instance
.
addAttribute
(
name
,
value
)
}
})
Utils
.
_
.
each
(
this
.
options
.
instanceMethods
||
{},
function
(
fct
,
name
)
{
instance
[
name
]
=
fct
})
Utils
.
_
.
each
(
this
.
associations
,
function
(
association
,
associationName
)
{
association
.
injectGetter
(
instance
)
association
.
injectSetter
(
instance
)
})
instance
.
isNewRecord
=
options
.
hasOwnProperty
(
'isNewRecord'
)
?
options
.
isNewRecord
:
true
return
instance
}
return
instance
}
ModelFactory
.
prototype
.
create
=
function
(
values
)
{
return
this
.
build
(
values
).
save
()
}
ModelFactory
.
prototype
.
create
=
function
(
values
)
{
return
this
.
build
(
values
).
save
()
}
ModelFactory
.
prototype
.
__defineGetter__
(
'primaryKeys'
,
function
()
{
var
result
=
{}
ModelFactory
.
prototype
.
__defineGetter__
(
'primaryKeys'
,
function
()
{
var
result
=
{}
Utils
.
_
.
each
(
this
.
attributes
,
function
(
dataTypeString
,
attributeName
)
{
if
((
attributeName
!=
'id'
)
&&
(
dataTypeString
.
indexOf
(
'PRIMARY KEY'
)
>
-
1
))
result
[
attributeName
]
=
dataTypeString
})
Utils
.
_
.
each
(
this
.
attributes
,
function
(
dataTypeString
,
attributeName
)
{
if
((
attributeName
!=
'id'
)
&&
(
dataTypeString
.
indexOf
(
'PRIMARY KEY'
)
>
-
1
))
result
[
attributeName
]
=
dataTypeString
return
result
})
return
result
})
ModelFactory
.
prototype
.
__defineGetter__
(
'primaryKeyCount'
,
function
()
{
return
Utils
.
_
.
keys
(
this
.
primaryKeys
).
length
})
ModelFactory
.
prototype
.
__defineGetter__
(
'primaryKeyCount
'
,
function
()
{
return
Utils
.
_
.
keys
(
this
.
primaryKeys
).
length
})
ModelFactory
.
prototype
.
__defineGetter__
(
'hasPrimaryKeys
'
,
function
()
{
return
this
.
primaryKeyCount
>
0
})
ModelFactory
.
prototype
.
__defineGetter__
(
'hasPrimaryKeys'
,
function
()
{
return
this
.
primaryKeyCount
>
0
})
// private
// private
var
query
=
function
()
{
var
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
,
s
=
this
.
modelManager
.
sequelize
ModelFactory
.
prototype
.
__query
=
function
()
{
var
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
,
s
=
this
.
modelManager
.
sequelize
// add this as the second argument
if
(
arguments
.
length
==
1
)
args
.
push
(
this
)
return
s
.
query
.
apply
(
s
,
args
)
}
// add this as the second argument
if
(
arguments
.
length
==
1
)
args
.
push
(
this
)
return
s
.
query
.
apply
(
s
,
args
)
}
var
addOptionalClassMethods
=
function
()
{
var
self
=
this
Utils
.
_
.
each
(
this
.
options
.
classMethods
||
{},
function
(
fct
,
name
)
{
self
[
name
]
=
fct
}
)
}
ModelFactory
.
prototype
.
__addOptionalClassMethods
=
function
()
{
var
self
=
this
Utils
.
_
.
each
(
this
.
options
.
classMethods
||
{},
function
(
fct
,
name
)
{
self
[
name
]
=
fct
})
}
var
addDefaultAttributes
=
function
()
{
var
defaultAttributes
=
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
}}
,
self
=
this
ModelFactory
.
prototype
.
__addDefaultAttributes
=
function
()
{
var
defaultAttributes
=
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
}}
,
self
=
this
if
(
this
.
hasPrimaryKeys
)
defaultAttributes
=
{}
if
(
this
.
hasPrimaryKeys
)
defaultAttributes
=
{}
if
(
this
.
options
.
timestamps
)
{
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
.
timestamps
)
{
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
[
Utils
.
_
.
underscoredIf
(
'deletedAt'
,
this
.
options
.
underscored
)]
=
{
type
:
DataTypes
.
DATE
}
}
if
(
this
.
options
.
paranoid
)
defaultAttributes
[
Utils
.
_
.
underscoredIf
(
'deletedAt'
,
this
.
options
.
underscored
)]
=
{
type
:
DataTypes
.
DATE
}
defaultAttributes
=
Utils
.
simplifyAttributes
(
defaultAttributes
)
Utils
.
_
.
map
(
defaultAttributes
,
function
(
value
,
attr
)
{
self
.
attributes
[
attr
]
=
value
})
}
defaultAttributes
=
Utils
.
simplifyAttributes
(
defaultAttributes
)
Utils
.
_
.
map
(
defaultAttributes
,
function
(
value
,
attr
)
{
self
.
attributes
[
attr
]
=
value
})
}
ModelFactory
.
prototype
.
__findAutoIncrementField
=
function
()
{
var
self
=
this
var
findAutoIncrementField
=
function
()
{
var
self
=
this
this
.
autoIncrementField
=
null
Utils
.
_
.
map
(
this
.
attributes
,
function
(
definition
,
name
)
{
if
(
definition
&&
(
definition
.
indexOf
(
'auto_increment'
)
>
-
1
))
{
if
(
self
.
autoIncrementField
)
throw
new
Error
(
'Invalid model definition. Only one autoincrement field allowed.'
)
else
self
.
autoIncrementField
=
name
}
})
}
this
.
autoIncrementField
=
null
Utils
.
_
.
map
(
this
.
attributes
,
function
(
definition
,
name
)
{
if
(
definition
&&
(
definition
.
indexOf
(
'auto_increment'
)
>
-
1
))
{
if
(
self
.
autoIncrementField
)
throw
new
Error
(
'Invalid model definition. Only one autoincrement field allowed.'
)
else
self
.
autoIncrementField
=
name
}
})
}
Utils
.
_
.
extend
(
ModelFactory
.
prototype
,
require
(
"./associations/mixin"
))
Utils
.
_
.
extend
(
ModelFactory
.
prototype
,
require
(
"./associations/mixin"
))
return
ModelFactory
})()
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