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 5a73894c
authored
Nov 09, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
renamed modeldefinition to modelfactory
1 parent
ea5c4337
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
28 additions
and
28 deletions
lib/model-definition.js → lib/model-factory.js
lib/sequelize.js
spec/model-definition.spec.js
test/Model/mixin.js
lib/model-
definition
.js
→
lib/model-
factory
.js
View file @
5a73894
...
@@ -2,7 +2,7 @@ var Utils = require("./utils")
...
@@ -2,7 +2,7 @@ var Utils = require("./utils")
,
Model
=
require
(
"./model"
)
,
Model
=
require
(
"./model"
)
,
DataTypes
=
require
(
"./data-types"
)
,
DataTypes
=
require
(
"./data-types"
)
var
Model
Definition
=
module
.
exports
=
function
(
name
,
attributes
,
options
)
{
var
Model
Factory
=
module
.
exports
=
function
(
name
,
attributes
,
options
)
{
var
self
=
this
var
self
=
this
this
.
options
=
options
||
{}
this
.
options
=
options
||
{}
...
@@ -18,13 +18,13 @@ var ModelDefinition = module.exports = function(name, attributes, options) {
...
@@ -18,13 +18,13 @@ var ModelDefinition = module.exports = function(name, attributes, options) {
this
.
__addOptionalClassMethods
()
this
.
__addOptionalClassMethods
()
this
.
__findAutoIncrementField
()
this
.
__findAutoIncrementField
()
}
}
Utils
.
addEventEmitter
(
Model
Definition
)
Utils
.
addEventEmitter
(
Model
Factory
)
Model
Definition
.
prototype
.
__defineGetter__
(
'QueryGenerator'
,
function
()
{
Model
Factory
.
prototype
.
__defineGetter__
(
'QueryGenerator'
,
function
()
{
return
this
.
modelManager
.
sequelize
.
connectorManager
.
getQueryGenerator
()
return
this
.
modelManager
.
sequelize
.
connectorManager
.
getQueryGenerator
()
})
})
Model
Definition
.
prototype
.
sync
=
function
(
options
)
{
Model
Factory
.
prototype
.
sync
=
function
(
options
)
{
options
=
Utils
.
merge
(
options
||
{},
this
.
options
)
options
=
Utils
.
merge
(
options
||
{},
this
.
options
)
var
self
=
this
var
self
=
this
...
@@ -45,15 +45,15 @@ ModelDefinition.prototype.sync = function(options) {
...
@@ -45,15 +45,15 @@ ModelDefinition.prototype.sync = function(options) {
return
this
return
this
}
}
Model
Definition
.
prototype
.
drop
=
function
()
{
Model
Factory
.
prototype
.
drop
=
function
()
{
return
this
.
__query
(
this
.
QueryGenerator
.
dropTableQuery
(
this
.
tableName
,
this
.
id
))
return
this
.
__query
(
this
.
QueryGenerator
.
dropTableQuery
(
this
.
tableName
,
this
.
id
))
}
}
Model
Definition
.
prototype
.
all
=
function
()
{
Model
Factory
.
prototype
.
all
=
function
()
{
return
this
.
__query
(
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
))
return
this
.
__query
(
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
))
}
}
Model
Definition
.
prototype
.
count
=
function
(
options
)
{
Model
Factory
.
prototype
.
count
=
function
(
options
)
{
var
self
=
this
var
self
=
this
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
...
@@ -64,7 +64,7 @@ ModelDefinition.prototype.count = function(options) {
...
@@ -64,7 +64,7 @@ ModelDefinition.prototype.count = function(options) {
return
emitter
.
run
()
return
emitter
.
run
()
}
}
Model
Definition
.
prototype
.
max
=
function
(
field
,
options
)
{
Model
Factory
.
prototype
.
max
=
function
(
field
,
options
)
{
var
self
=
this
var
self
=
this
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
...
@@ -74,7 +74,7 @@ ModelDefinition.prototype.max = function(field, options) {
...
@@ -74,7 +74,7 @@ ModelDefinition.prototype.max = function(field, options) {
})
})
return
emitter
.
run
()
return
emitter
.
run
()
}
}
Model
Definition
.
prototype
.
min
=
function
(
field
,
options
)
{
Model
Factory
.
prototype
.
min
=
function
(
field
,
options
)
{
var
self
=
this
var
self
=
this
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
...
@@ -84,11 +84,11 @@ ModelDefinition.prototype.min = function(field, options) {
...
@@ -84,11 +84,11 @@ ModelDefinition.prototype.min = function(field, options) {
})
})
return
emitter
.
run
()
return
emitter
.
run
()
}
}
Model
Definition
.
prototype
.
findAll
=
function
(
options
)
{
Model
Factory
.
prototype
.
findAll
=
function
(
options
)
{
return
this
.
__query
(
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
,
options
))
return
this
.
__query
(
this
.
QueryGenerator
.
selectQuery
(
this
.
tableName
,
options
))
}
}
Model
Definition
.
prototype
.
find
=
function
(
options
)
{
Model
Factory
.
prototype
.
find
=
function
(
options
)
{
// options is not a hash but an id
// options is not a hash but an id
if
(
typeof
options
==
'number'
)
if
(
typeof
options
==
'number'
)
options
=
{
where
:
options
}
options
=
{
where
:
options
}
...
@@ -113,7 +113,7 @@ ModelDefinition.prototype.find = function(options) {
...
@@ -113,7 +113,7 @@ ModelDefinition.prototype.find = function(options) {
return
this
.
__query
(
query
,
this
,
{
plain
:
true
})
return
this
.
__query
(
query
,
this
,
{
plain
:
true
})
}
}
Model
Definition
.
prototype
.
build
=
function
(
values
,
options
)
{
Model
Factory
.
prototype
.
build
=
function
(
values
,
options
)
{
var
instance
=
new
Model
(
values
,
Utils
.
_
.
extend
(
this
.
options
,
{
hasPrimaryKeys
:
this
.
hasPrimaryKeys
}))
var
instance
=
new
Model
(
values
,
Utils
.
_
.
extend
(
this
.
options
,
{
hasPrimaryKeys
:
this
.
hasPrimaryKeys
}))
,
self
=
this
,
self
=
this
...
@@ -142,11 +142,11 @@ ModelDefinition.prototype.build = function(values, options) {
...
@@ -142,11 +142,11 @@ ModelDefinition.prototype.build = function(values, options) {
return
instance
return
instance
}
}
Model
Definition
.
prototype
.
create
=
function
(
values
)
{
Model
Factory
.
prototype
.
create
=
function
(
values
)
{
return
this
.
build
(
values
).
save
()
return
this
.
build
(
values
).
save
()
}
}
Model
Definition
.
prototype
.
__defineGetter__
(
'primaryKeys'
,
function
()
{
Model
Factory
.
prototype
.
__defineGetter__
(
'primaryKeys'
,
function
()
{
var
result
=
{}
var
result
=
{}
Utils
.
_
.
each
(
this
.
attributes
,
function
(
dataTypeString
,
attributeName
)
{
Utils
.
_
.
each
(
this
.
attributes
,
function
(
dataTypeString
,
attributeName
)
{
...
@@ -157,17 +157,17 @@ ModelDefinition.prototype.__defineGetter__('primaryKeys', function() {
...
@@ -157,17 +157,17 @@ ModelDefinition.prototype.__defineGetter__('primaryKeys', function() {
return
result
return
result
})
})
Model
Definition
.
prototype
.
__defineGetter__
(
'primaryKeyCount'
,
function
()
{
Model
Factory
.
prototype
.
__defineGetter__
(
'primaryKeyCount'
,
function
()
{
return
Utils
.
_
.
keys
(
this
.
primaryKeys
).
length
return
Utils
.
_
.
keys
(
this
.
primaryKeys
).
length
})
})
Model
Definition
.
prototype
.
__defineGetter__
(
'hasPrimaryKeys'
,
function
()
{
Model
Factory
.
prototype
.
__defineGetter__
(
'hasPrimaryKeys'
,
function
()
{
return
this
.
primaryKeyCount
>
0
return
this
.
primaryKeyCount
>
0
})
})
// private
// private
Model
Definition
.
prototype
.
__query
=
function
()
{
Model
Factory
.
prototype
.
__query
=
function
()
{
var
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
var
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
,
s
=
this
.
modelManager
.
sequelize
,
s
=
this
.
modelManager
.
sequelize
...
@@ -176,12 +176,12 @@ ModelDefinition.prototype.__query = function() {
...
@@ -176,12 +176,12 @@ ModelDefinition.prototype.__query = function() {
return
s
.
query
.
apply
(
s
,
args
)
return
s
.
query
.
apply
(
s
,
args
)
}
}
Model
Definition
.
prototype
.
__addOptionalClassMethods
=
function
()
{
Model
Factory
.
prototype
.
__addOptionalClassMethods
=
function
()
{
var
self
=
this
var
self
=
this
Utils
.
_
.
each
(
this
.
options
.
classMethods
||
{},
function
(
fct
,
name
)
{
self
[
name
]
=
fct
})
Utils
.
_
.
each
(
this
.
options
.
classMethods
||
{},
function
(
fct
,
name
)
{
self
[
name
]
=
fct
})
}
}
Model
Definition
.
prototype
.
__addDefaultAttributes
=
function
()
{
Model
Factory
.
prototype
.
__addDefaultAttributes
=
function
()
{
var
defaultAttributes
=
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
}}
var
defaultAttributes
=
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
}}
,
self
=
this
,
self
=
this
...
@@ -199,7 +199,7 @@ ModelDefinition.prototype.__addDefaultAttributes = function() {
...
@@ -199,7 +199,7 @@ ModelDefinition.prototype.__addDefaultAttributes = function() {
Utils
.
_
.
map
(
defaultAttributes
,
function
(
value
,
attr
)
{
self
.
attributes
[
attr
]
=
value
})
Utils
.
_
.
map
(
defaultAttributes
,
function
(
value
,
attr
)
{
self
.
attributes
[
attr
]
=
value
})
}
}
Model
Definition
.
prototype
.
__findAutoIncrementField
=
function
()
{
Model
Factory
.
prototype
.
__findAutoIncrementField
=
function
()
{
var
self
=
this
var
self
=
this
this
.
autoIncrementField
=
null
this
.
autoIncrementField
=
null
...
@@ -213,4 +213,4 @@ ModelDefinition.prototype.__findAutoIncrementField = function() {
...
@@ -213,4 +213,4 @@ ModelDefinition.prototype.__findAutoIncrementField = function() {
})
})
}
}
Utils
.
_
.
extend
(
Model
Definition
.
prototype
,
require
(
"./associations/mixin"
))
Utils
.
_
.
extend
(
Model
Factory
.
prototype
,
require
(
"./associations/mixin"
))
lib/sequelize.js
View file @
5a73894
var
Utils
=
require
(
"./utils"
)
var
Utils
=
require
(
"./utils"
)
,
Model
Definition
=
require
(
"./model-definition
"
)
,
Model
Factory
=
require
(
"./model-factory
"
)
,
DataTypes
=
require
(
'./data-types'
)
,
DataTypes
=
require
(
'./data-types'
)
,
ModelManager
=
require
(
"./model-manager"
)
,
ModelManager
=
require
(
"./model-manager"
)
...
@@ -34,7 +34,7 @@ var instanceMethods = {
...
@@ -34,7 +34,7 @@ var instanceMethods = {
if
(
this
.
options
.
define
)
if
(
this
.
options
.
define
)
options
=
Sequelize
.
Utils
.
merge
(
options
,
this
.
options
.
define
)
options
=
Sequelize
.
Utils
.
merge
(
options
,
this
.
options
.
define
)
var
model
=
this
.
modelManager
.
addModel
(
new
Model
Definition
(
modelName
,
attributes
,
options
))
var
model
=
this
.
modelManager
.
addModel
(
new
Model
Factory
(
modelName
,
attributes
,
options
))
return
model
return
model
},
},
...
...
spec/model-definition.spec.js
View file @
5a73894
...
@@ -3,7 +3,7 @@ var config = require("./config/config")
...
@@ -3,7 +3,7 @@ var config = require("./config/config")
,
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
{
logging
:
false
})
,
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
{
logging
:
false
})
,
Helpers
=
new
(
require
(
"./config/helpers"
))(
sequelize
)
,
Helpers
=
new
(
require
(
"./config/helpers"
))(
sequelize
)
describe
(
'Model
Definition
'
,
function
()
{
describe
(
'Model
Factory
'
,
function
()
{
beforeEach
(
function
()
{
Helpers
.
sync
()
})
beforeEach
(
function
()
{
Helpers
.
sync
()
})
afterEach
(
function
()
{
Helpers
.
drop
()
})
afterEach
(
function
()
{
Helpers
.
drop
()
})
...
...
test/Model/mixin.js
View file @
5a73894
var
assert
=
require
(
"assert"
)
var
assert
=
require
(
"assert"
)
,
Model
Definition
=
require
(
"./../../lib/model-definition
"
)
,
Model
Factory
=
require
(
"./../../lib/model-factory
"
)
module
.
exports
=
{
module
.
exports
=
{
'mixin should be correctly added to the model'
:
function
()
{
'mixin should be correctly added to the model'
:
function
()
{
assert
.
isDefined
(
Model
Definition
.
prototype
.
hasOne
)
assert
.
isDefined
(
Model
Factory
.
prototype
.
hasOne
)
assert
.
isDefined
(
Model
Definition
.
prototype
.
hasMany
)
assert
.
isDefined
(
Model
Factory
.
prototype
.
hasMany
)
assert
.
isDefined
(
Model
Definition
.
prototype
.
belongsTo
)
assert
.
isDefined
(
Model
Factory
.
prototype
.
belongsTo
)
}
}
}
}
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