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 d1456c7d
authored
Nov 09, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
minor refactoring
1 parent
c546996c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
59 deletions
lib/model-definition.js
lib/model-definition.js
View file @
d1456c7
...
...
@@ -14,9 +14,9 @@ var ModelDefinition = module.exports = function(name, attributes, options) {
this
.
modelManager
=
null
// defined by model-manager during addModel
this
.
associations
=
{}
this
.
addDefaultAttributes
()
this
.
addOptionalClassMethods
()
this
.
findAutoIncrementField
()
this
.
__
addDefaultAttributes
()
this
.
__
addOptionalClassMethods
()
this
.
__
findAutoIncrementField
()
}
Utils
.
addEventEmitter
(
ModelDefinition
)
...
...
@@ -24,58 +24,12 @@ ModelDefinition.prototype.__defineGetter__('QueryGenerator', function() {
return
this
.
modelManager
.
sequelize
.
connectorManager
.
getQueryGenerator
()
})
ModelDefinition
.
prototype
.
addOptionalClassMethods
=
function
()
{
var
self
=
this
Utils
.
_
.
each
(
this
.
options
.
classMethods
||
{},
function
(
fct
,
name
)
{
self
[
name
]
=
fct
})
}
ModelDefinition
.
prototype
.
addDefaultAttributes
=
function
()
{
var
defaultAttributes
=
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
}}
,
self
=
this
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
.
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
})
}
ModelDefinition
.
prototype
.
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
}
})
}
ModelDefinition
.
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
)
}
ModelDefinition
.
prototype
.
sync
=
function
(
options
)
{
options
=
Utils
.
merge
(
options
||
{},
this
.
options
)
var
self
=
this
var
doQuery
=
function
()
{
self
.
query
(
self
.
QueryGenerator
.
createTableQuery
(
self
.
tableName
,
self
.
attributes
,
options
))
self
.
__
query
(
self
.
QueryGenerator
.
createTableQuery
(
self
.
tableName
,
self
.
attributes
,
options
))
.
on
(
'success'
,
function
()
{
self
.
emit
(
'success'
,
self
)
})
.
on
(
'failure'
,
function
(
err
)
{
self
.
emit
(
'failure'
,
err
)
})
}
...
...
@@ -92,18 +46,18 @@ ModelDefinition.prototype.sync = function(options) {
}
ModelDefinition
.
prototype
.
drop
=
function
()
{
return
this
.
query
(
this
.
QueryGenerator
.
dropTableQuery
(
this
.
tableName
,
this
.
id
))
return
this
.
__
query
(
this
.
QueryGenerator
.
dropTableQuery
(
this
.
tableName
,
this
.
id
))
}
ModelDefinition
.
prototype
.
__defineGetter__
(
'all'
,
function
()
{
return
this
.
query
(
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
))
}
)
ModelDefinition
.
prototype
.
all
=
function
()
{
return
this
.
__
query
(
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
))
}
ModelDefinition
.
prototype
.
count
=
function
(
options
)
{
var
self
=
this
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
self
.
query
(
self
.
QueryGenerator
.
countQuery
(
self
.
tableName
,
options
),
self
,
{
plain
:
true
}).
on
(
'success'
,
function
(
obj
)
{
self
.
__
query
(
self
.
QueryGenerator
.
countQuery
(
self
.
tableName
,
options
),
self
,
{
plain
:
true
}).
on
(
'success'
,
function
(
obj
)
{
emitter
.
emit
(
'success'
,
obj
[
'count(*)'
])
})
})
...
...
@@ -114,7 +68,7 @@ ModelDefinition.prototype.max = function(field, 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
)
{
self
.
__
query
(
self
.
QueryGenerator
.
maxQuery
(
self
.
tableName
,
field
,
options
),
self
,
{
plain
:
true
}).
on
(
'success'
,
function
(
obj
)
{
emitter
.
emit
(
'success'
,
obj
[
'max'
])
})
})
...
...
@@ -124,14 +78,14 @@ ModelDefinition.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
)
{
self
.
__
query
(
self
.
QueryGenerator
.
minQuery
(
self
.
tableName
,
field
,
options
),
self
,
{
plain
:
true
}).
on
(
'success'
,
function
(
obj
)
{
emitter
.
emit
(
'success'
,
obj
[
'min'
])
})
})
return
emitter
.
run
()
}
ModelDefinition
.
prototype
.
findAll
=
function
(
options
)
{
return
this
.
query
(
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
,
options
))
return
this
.
__
query
(
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
,
options
))
}
ModelDefinition
.
prototype
.
find
=
function
(
options
)
{
...
...
@@ -156,7 +110,7 @@ ModelDefinition.prototype.find = function(options) {
options
.
limit
=
1
var
query
=
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
,
options
)
return
this
.
query
(
query
,
this
,
{
plain
:
true
})
return
this
.
__
query
(
query
,
this
,
{
plain
:
true
})
}
ModelDefinition
.
prototype
.
build
=
function
(
values
,
options
)
{
...
...
@@ -211,4 +165,53 @@ ModelDefinition.prototype.__defineGetter__('hasPrimaryKeys', function() {
return
this
.
primaryKeyCount
>
0
})
// private
ModelDefinition
.
prototype
.
__addOptionalClassMethods
=
function
()
{
var
self
=
this
Utils
.
_
.
each
(
this
.
options
.
classMethods
||
{},
function
(
fct
,
name
)
{
self
[
name
]
=
fct
})
}
ModelDefinition
.
prototype
.
__addDefaultAttributes
=
function
()
{
var
defaultAttributes
=
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
}}
,
self
=
this
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
.
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
})
}
ModelDefinition
.
prototype
.
__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
}
})
}
ModelDefinition
.
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
)
}
Utils
.
_
.
map
(
require
(
"./associations/mixin"
).
classMethods
,
function
(
fct
,
name
)
{
ModelDefinition
.
prototype
[
name
]
=
fct
})
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