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 8ab2e6d3
authored
Feb 12, 2012
by
sdepold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
renamed Model* to DAO*
1 parent
1b41c161
Hide whitespace changes
Inline
Side-by-side
Showing
23 changed files
with
193 additions
and
193 deletions
changelog.md
lib/associations/belongs-to.js
lib/associations/has-many-double-linked.js
lib/associations/has-many.js
lib/associations/has-one.js
lib/associations/mixin.js
lib/dao-factory-manager.js
lib/model-factory.js → lib/dao-factory.js
lib/model.js → lib/dao.js
lib/migrator.js
lib/model-factory-manager.js
lib/query-interface.js
lib/sequelize.js
spec/associations/has-many.spec.js
spec/config/factories.js
spec/model-factory.spec.js → spec/dao-factory.spec.js
spec/model.spec.js → spec/dao.spec.js
spec/migrator.spec.js
spec/mysql/associations.has-many.spec.js
spec/mysql/associations.spec.js
spec/mysql/model-factory.spec.js → spec/mysql/dao-factory.spec.js
spec/sequelize.spec.js
spec/sqlite/model-factory.spec.js → spec/sqlite/dao-factory.spec.js
changelog.md
View file @
8ab2e6d
...
...
@@ -5,7 +5,7 @@
-
[
FEATURE
]
sqlite is now emitting the 'sql'-event as well (thanks to megshark)
# v1.3.1 #
-
[
REFACTORING
]
renamed ModelManager to
Model
FactoryManager
-
[
REFACTORING
]
renamed ModelManager to
DAO
FactoryManager
-
[
IMPROVEMENT
]
decreased delay of CustomEventEmitter execution from 5ms to 1ms
-
[
IMPROVEMENT
]
improved performance of association handling (many-to-many) (thanks to magshark)
-
[
FEATURE
]
added possibility to specify name of the join table (thanks to magshark)
...
...
@@ -15,7 +15,7 @@
# v1.3.0 #
-
[
REFACTORING
]
Model#all is now a function and not a getter.
-
[
REFACTORING
]
Renamed ModelDefinition to
Model
Factory
-
[
REFACTORING
]
Renamed ModelDefinition to
DAO
Factory
-
[
REFACTORING
]
Private method scoping; Attributes are still public
-
[
REFACTORING
]
Use the new util module for node 0.6.2
-
[
FEATURE
]
QueryChainer can now run serially
...
...
lib/associations/belongs-to.js
View file @
8ab2e6d
...
...
@@ -2,9 +2,9 @@ var Utils = require("./../utils")
,
DataTypes
=
require
(
'./../data-types'
)
module
.
exports
=
(
function
()
{
var
BelongsTo
=
function
(
src
Model
,
targetModel
,
options
)
{
this
.
source
=
src
Model
this
.
target
=
target
Model
var
BelongsTo
=
function
(
src
DAO
,
targetDAO
,
options
)
{
this
.
source
=
src
DAO
this
.
target
=
target
DAO
this
.
options
=
options
this
.
isSelfAssociation
=
(
this
.
source
.
tableName
==
this
.
target
.
tableName
)
...
...
lib/associations/has-many-double-linked.js
View file @
8ab2e6d
...
...
@@ -12,13 +12,13 @@ module.exports = (function() {
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
where
=
{}
//fully qualify
where
[
self
.
__factory
.
connector
Model
.
tableName
+
"."
+
self
.
__factory
.
identifier
]
=
self
.
instance
.
id
where
[
self
.
__factory
.
connector
DAO
.
tableName
+
"."
+
self
.
__factory
.
identifier
]
=
self
.
instance
.
id
var
primaryKeys
=
Utils
.
_
.
keys
(
self
.
__factory
.
connector
Model
.
rawAttributes
)
var
primaryKeys
=
Utils
.
_
.
keys
(
self
.
__factory
.
connector
DAO
.
rawAttributes
)
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
__factory
.
identifier
})[
0
]
where
[
self
.
__factory
.
connector
Model
.
tableName
+
"."
+
foreignKey
]
=
{
join
:
self
.
__factory
.
target
.
tableName
+
".id"
}
self
.
__factory
.
target
.
findAllJoin
(
self
.
__factory
.
connector
Model
.
tableName
,
{
where
:
where
})
where
[
self
.
__factory
.
connector
DAO
.
tableName
+
"."
+
foreignKey
]
=
{
join
:
self
.
__factory
.
target
.
tableName
+
".id"
}
self
.
__factory
.
target
.
findAllJoin
(
self
.
__factory
.
connector
DAO
.
tableName
,
{
where
:
where
})
.
on
(
'success'
,
function
(
objects
)
{
customEventEmitter
.
emit
(
'success'
,
objects
)
})
.
on
(
'failure'
,
function
(
err
){
customEventEmitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
customEventEmitter
.
emit
(
'sql'
,
sql
)})
...
...
@@ -44,7 +44,7 @@ module.exports = (function() {
attributes
[
self
.
__factory
.
identifier
]
=
self
.
instance
.
id
attributes
[
foreignIdentifier
]
=
unassociatedObject
.
id
chainer
.
add
(
self
.
__factory
.
connector
Model
.
create
(
attributes
))
chainer
.
add
(
self
.
__factory
.
connector
DAO
.
create
(
attributes
))
})
chainer
...
...
@@ -70,13 +70,13 @@ module.exports = (function() {
obsoleteAssociations
.
forEach
(
function
(
associatedObject
)
{
var
where
=
{}
,
primaryKeys
=
Utils
.
_
.
keys
(
self
.
__factory
.
connector
Model
.
rawAttributes
)
,
primaryKeys
=
Utils
.
_
.
keys
(
self
.
__factory
.
connector
DAO
.
rawAttributes
)
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
__factory
.
identifier
})[
0
]
where
[
self
.
__factory
.
identifier
]
=
self
.
instance
.
id
where
[
foreignKey
]
=
associatedObject
.
id
self
.
__factory
.
connector
Model
.
find
({
where
:
where
}).
success
(
function
(
connector
)
{
self
.
__factory
.
connector
DAO
.
find
({
where
:
where
}).
success
(
function
(
connector
)
{
chainer
.
add
(
connector
.
destroy
())
if
(
chainer
.
emitters
.
length
==
obsoleteAssociations
.
length
)
{
...
...
lib/associations/has-many.js
View file @
8ab2e6d
...
...
@@ -5,9 +5,9 @@ var HasManySingleLinked = require("./has-many-single-linked")
,
HasManyMultiLinked
=
require
(
"./has-many-double-linked"
)
module
.
exports
=
(
function
()
{
var
HasMany
=
function
(
src
Model
,
targetModel
,
options
)
{
this
.
source
=
src
Model
this
.
target
=
target
Model
var
HasMany
=
function
(
src
DAO
,
targetDAO
,
options
)
{
this
.
source
=
src
DAO
this
.
target
=
target
DAO
this
.
options
=
options
this
.
isSelfAssociation
=
(
this
.
source
.
tableName
==
this
.
target
.
tableName
)
...
...
@@ -48,10 +48,10 @@ module.exports = (function() {
combinedTableAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
}
combinedTableAttributes
[
this
.
foreignIdentifier
]
=
{
type
:
DataTypes
.
INTEGER
,
primaryKey
:
true
}
this
.
connector
Model
=
this
.
source
.
model
FactoryManager
.
sequelize
.
define
(
this
.
combinedName
,
combinedTableAttributes
,
this
.
options
)
if
(
!
this
.
isSelfAssociation
)
this
.
target
.
associations
[
this
.
associationAccessor
].
connector
Model
=
this
.
connectorModel
this
.
connector
DAO
=
this
.
source
.
dao
FactoryManager
.
sequelize
.
define
(
this
.
combinedName
,
combinedTableAttributes
,
this
.
options
)
if
(
!
this
.
isSelfAssociation
)
this
.
target
.
associations
[
this
.
associationAccessor
].
connector
DAO
=
this
.
connectorDAO
this
.
connector
Model
.
sync
()
this
.
connector
DAO
.
sync
()
}
else
{
var
newAttributes
=
{}
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
...
...
@@ -65,7 +65,7 @@ module.exports = (function() {
var
self
=
this
obj
[
this
.
accessors
.
get
]
=
function
()
{
var
Class
=
self
.
connector
Model
?
HasManyMultiLinked
:
HasManySingleLinked
var
Class
=
self
.
connector
DAO
?
HasManyMultiLinked
:
HasManySingleLinked
return
new
Class
(
self
,
this
).
injectGetter
()
}
...
...
@@ -81,7 +81,7 @@ module.exports = (function() {
// define the returned customEventEmitter, which will emit the success event once everything is done
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
instance
[
self
.
accessors
.
get
]().
success
(
function
(
oldAssociatedObjects
)
{
var
Class
=
self
.
connector
Model
?
HasManyMultiLinked
:
HasManySingleLinked
var
Class
=
self
.
connector
DAO
?
HasManyMultiLinked
:
HasManySingleLinked
new
Class
(
self
,
instance
).
injectSetter
(
customEventEmitter
,
oldAssociatedObjects
,
newAssociatedObjects
)
})
})
...
...
lib/associations/has-one.js
View file @
8ab2e6d
...
...
@@ -2,9 +2,9 @@ var Utils = require("./../utils")
,
DataTypes
=
require
(
'./../data-types'
)
module
.
exports
=
(
function
()
{
var
HasOne
=
function
(
src
Model
,
targetModel
,
options
)
{
this
.
source
=
src
Model
this
.
target
=
target
Model
var
HasOne
=
function
(
src
DAO
,
targetDAO
,
options
)
{
this
.
source
=
src
DAO
this
.
target
=
target
DAO
this
.
options
=
options
this
.
isSelfAssociation
=
(
this
.
source
.
tableName
==
this
.
target
.
tableName
)
...
...
lib/associations/mixin.js
View file @
8ab2e6d
...
...
@@ -6,21 +6,21 @@ var Utils = require("./../utils")
/* Defines Mixin for all models. */
var
Mixin
=
module
.
exports
=
function
(){}
Mixin
.
hasOne
=
function
(
associated
Model
,
options
)
{
Mixin
.
hasOne
=
function
(
associated
DAO
,
options
)
{
// the id is in the foreign table
var
association
=
new
HasOne
(
this
,
associated
Model
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
))
var
association
=
new
HasOne
(
this
,
associated
DAO
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
))
this
.
associations
[
association
.
associationAccessor
]
=
association
.
injectAttributes
()
return
this
}
Mixin
.
belongsTo
=
function
(
associated
Model
,
options
)
{
Mixin
.
belongsTo
=
function
(
associated
DAO
,
options
)
{
// the id is in this table
var
association
=
new
BelongsTo
(
this
,
associated
Model
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
))
var
association
=
new
BelongsTo
(
this
,
associated
DAO
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
))
this
.
associations
[
association
.
associationAccessor
]
=
association
.
injectAttributes
()
return
this
}
Mixin
.
hasMany
=
function
(
associated
Model
,
options
)
{
Mixin
.
hasMany
=
function
(
associated
DAO
,
options
)
{
// the id is in the foreign table or in a connecting table
var
association
=
new
HasMany
(
this
,
associated
Model
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
))
var
association
=
new
HasMany
(
this
,
associated
DAO
,
Utils
.
_
.
extend
((
options
||
{}),
this
.
options
))
this
.
associations
[
association
.
associationAccessor
]
=
association
.
injectAttributes
()
return
this
}
...
...
lib/dao-factory-manager.js
0 → 100644
View file @
8ab2e6d
module
.
exports
=
(
function
()
{
var
DAOFactoryManager
=
function
(
sequelize
)
{
this
.
daos
=
[]
this
.
sequelize
=
sequelize
}
DAOFactoryManager
.
prototype
.
addDAO
=
function
(
dao
)
{
this
.
daos
.
push
(
dao
)
return
dao
}
DAOFactoryManager
.
prototype
.
removeDAO
=
function
(
dao
)
{
this
.
daos
=
this
.
daos
.
filter
(
function
(
_dao
)
{
return
_dao
.
name
!=
dao
.
name
})
}
DAOFactoryManager
.
prototype
.
getDAO
=
function
(
daoName
)
{
var
dao
=
this
.
daos
.
filter
(
function
(
dao
)
{
return
dao
.
name
==
daoName
})
return
!!
dao
?
dao
[
0
]
:
null
}
DAOFactoryManager
.
prototype
.
__defineGetter__
(
'all'
,
function
()
{
return
this
.
daos
})
return
DAOFactoryManager
})()
lib/
model
-factory.js
→
lib/
dao
-factory.js
View file @
8ab2e6d
var
Utils
=
require
(
"./utils"
)
,
Model
=
require
(
"./model
"
)
,
DAO
=
require
(
"./dao
"
)
,
DataTypes
=
require
(
"./data-types"
)
module
.
exports
=
(
function
()
{
var
Model
Factory
=
function
(
name
,
attributes
,
options
)
{
var
DAO
Factory
=
function
(
name
,
attributes
,
options
)
{
var
self
=
this
this
.
options
=
Utils
.
_
.
extend
({
...
...
@@ -19,37 +19,37 @@ module.exports = (function() {
this
.
name
=
name
this
.
tableName
=
this
.
options
.
freezeTableName
?
name
:
Utils
.
pluralize
(
name
)
this
.
rawAttributes
=
attributes
this
.
model
FactoryManager
=
null
// defined in init function
this
.
dao
FactoryManager
=
null
// defined in init function
this
.
associations
=
{}
// extract validation
this
.
validate
=
this
.
options
.
validate
||
{}
}
Object
.
defineProperty
(
Model
Factory
.
prototype
,
'attributes'
,
{
Object
.
defineProperty
(
DAO
Factory
.
prototype
,
'attributes'
,
{
get
:
function
()
{
return
this
.
QueryGenerator
.
attributesToSQL
(
this
.
rawAttributes
)
}
})
Object
.
defineProperty
(
Model
Factory
.
prototype
,
'QueryInterface'
,
{
get
:
function
()
{
return
this
.
model
FactoryManager
.
sequelize
.
getQueryInterface
()
}
Object
.
defineProperty
(
DAO
Factory
.
prototype
,
'QueryInterface'
,
{
get
:
function
()
{
return
this
.
dao
FactoryManager
.
sequelize
.
getQueryInterface
()
}
})
Object
.
defineProperty
(
Model
Factory
.
prototype
,
'QueryGenerator'
,
{
Object
.
defineProperty
(
DAO
Factory
.
prototype
,
'QueryGenerator'
,
{
get
:
function
()
{
return
this
.
QueryInterface
.
QueryGenerator
}
})
Object
.
defineProperty
(
Model
Factory
.
prototype
,
'primaryKeyCount'
,
{
Object
.
defineProperty
(
DAO
Factory
.
prototype
,
'primaryKeyCount'
,
{
get
:
function
()
{
return
Utils
.
_
.
keys
(
this
.
primaryKeys
).
length
}
})
Object
.
defineProperty
(
Model
Factory
.
prototype
,
'hasPrimaryKeys'
,
{
Object
.
defineProperty
(
DAO
Factory
.
prototype
,
'hasPrimaryKeys'
,
{
get
:
function
()
{
return
this
.
primaryKeyCount
>
0
}
})
ModelFactory
.
prototype
.
init
=
function
(
model
FactoryManager
)
{
this
.
modelFactoryManager
=
model
FactoryManager
DAOFactory
.
prototype
.
init
=
function
(
dao
FactoryManager
)
{
this
.
daoFactoryManager
=
dao
FactoryManager
addDefaultAttributes
.
call
(
this
)
addOptionalClassMethods
.
call
(
this
)
...
...
@@ -58,7 +58,7 @@ module.exports = (function() {
return
this
}
Model
Factory
.
prototype
.
sync
=
function
(
options
)
{
DAO
Factory
.
prototype
.
sync
=
function
(
options
)
{
options
=
Utils
.
merge
(
options
||
{},
this
.
options
)
var
self
=
this
...
...
@@ -79,27 +79,27 @@ module.exports = (function() {
}).
run
()
}
Model
Factory
.
prototype
.
drop
=
function
()
{
DAO
Factory
.
prototype
.
drop
=
function
()
{
return
this
.
QueryInterface
.
dropTable
(
this
.
tableName
)
}
Model
Factory
.
prototype
.
all
=
function
()
{
DAO
Factory
.
prototype
.
all
=
function
()
{
return
this
.
findAll
()
}
Model
Factory
.
prototype
.
findAll
=
function
(
options
)
{
DAO
Factory
.
prototype
.
findAll
=
function
(
options
)
{
return
this
.
QueryInterface
.
select
(
this
,
this
.
tableName
,
options
)
}
//right now, the caller (has-many-double-linked) is in charge of the where clause
Model
Factory
.
prototype
.
findAllJoin
=
function
(
joinTableName
,
options
)
{
DAO
Factory
.
prototype
.
findAllJoin
=
function
(
joinTableName
,
options
)
{
optcpy
=
Utils
.
_
.
clone
(
options
)
optcpy
.
attributes
=
optcpy
.
attributes
||
[
Utils
.
addTicks
(
this
.
tableName
)
+
".*"
]
return
this
.
QueryInterface
.
select
(
this
,
[
this
.
tableName
,
joinTableName
],
optcpy
)
}
Model
Factory
.
prototype
.
find
=
function
(
options
)
{
DAO
Factory
.
prototype
.
find
=
function
(
options
)
{
if
([
null
,
undefined
].
indexOf
(
options
)
>
-
1
)
{
var
NullEmitter
=
require
(
"./emitters/null-emitter"
)
return
new
NullEmitter
()
...
...
@@ -125,28 +125,28 @@ module.exports = (function() {
return
this
.
QueryInterface
.
select
(
this
,
this
.
tableName
,
options
,
{
plain
:
true
})
}
Model
Factory
.
prototype
.
count
=
function
(
options
)
{
DAO
Factory
.
prototype
.
count
=
function
(
options
)
{
options
=
Utils
.
_
.
extend
({
attributes
:
[]
},
options
||
{})
options
.
attributes
.
push
([
'count(*)'
,
'count'
])
return
this
.
QueryInterface
.
rawSelect
(
this
.
tableName
,
options
,
'count'
)
}
Model
Factory
.
prototype
.
max
=
function
(
field
,
options
)
{
DAO
Factory
.
prototype
.
max
=
function
(
field
,
options
)
{
options
=
Utils
.
_
.
extend
({
attributes
:
[]
},
options
||
{})
options
.
attributes
.
push
([
'max('
+
field
+
')'
,
'max'
])
return
this
.
QueryInterface
.
rawSelect
(
this
.
tableName
,
options
,
'max'
)
}
Model
Factory
.
prototype
.
min
=
function
(
field
,
options
)
{
DAO
Factory
.
prototype
.
min
=
function
(
field
,
options
)
{
options
=
Utils
.
_
.
extend
({
attributes
:
[]
},
options
||
{})
options
.
attributes
.
push
([
'min('
+
field
+
')'
,
'min'
])
return
this
.
QueryInterface
.
rawSelect
(
this
.
tableName
,
options
,
'min'
)
}
Model
Factory
.
prototype
.
build
=
function
(
values
,
options
)
{
var
instance
=
new
Model
(
values
,
Utils
.
_
.
extend
(
this
.
options
,
this
.
attributes
,
{
hasPrimaryKeys
:
this
.
hasPrimaryKeys
}))
DAO
Factory
.
prototype
.
build
=
function
(
values
,
options
)
{
var
instance
=
new
DAO
(
values
,
Utils
.
_
.
extend
(
this
.
options
,
this
.
attributes
,
{
hasPrimaryKeys
:
this
.
hasPrimaryKeys
}))
,
self
=
this
options
=
options
||
{}
...
...
@@ -179,11 +179,11 @@ module.exports = (function() {
return
instance
}
Model
Factory
.
prototype
.
create
=
function
(
values
)
{
DAO
Factory
.
prototype
.
create
=
function
(
values
)
{
return
this
.
build
(
values
).
save
()
}
Model
Factory
.
prototype
.
__defineGetter__
(
'primaryKeys'
,
function
()
{
DAO
Factory
.
prototype
.
__defineGetter__
(
'primaryKeys'
,
function
()
{
var
result
=
{}
Utils
.
_
.
each
(
this
.
attributes
,
function
(
dataTypeString
,
attributeName
)
{
if
((
attributeName
!=
'id'
)
&&
(
dataTypeString
.
indexOf
(
'PRIMARY KEY'
)
>
-
1
))
...
...
@@ -197,7 +197,7 @@ module.exports = (function() {
var
query
=
function
()
{
var
args
=
Utils
.
_
.
map
(
arguments
,
function
(
arg
,
_
)
{
return
arg
})
,
s
=
this
.
model
FactoryManager
.
sequelize
,
s
=
this
.
dao
FactoryManager
.
sequelize
// add this as the second argument
if
(
arguments
.
length
==
1
)
args
.
push
(
this
)
...
...
@@ -243,13 +243,13 @@ module.exports = (function() {
fields
.
forEach
(
function
(
field
)
{
if
(
self
.
autoIncrementField
)
throw
new
Error
(
'Invalid
model
definition. Only one autoincrement field allowed.'
)
throw
new
Error
(
'Invalid
DAO
definition. Only one autoincrement field allowed.'
)
else
self
.
autoIncrementField
=
field
})
}
Utils
.
_
.
extend
(
Model
Factory
.
prototype
,
require
(
"./associations/mixin"
))
Utils
.
_
.
extend
(
DAO
Factory
.
prototype
,
require
(
"./associations/mixin"
))
return
Model
Factory
return
DAO
Factory
})()
lib/
model
.js
→
lib/
dao
.js
View file @
8ab2e6d
...
...
@@ -3,12 +3,12 @@ var Utils = require("./utils")
,
Validator
=
require
(
"validator"
)
module
.
exports
=
(
function
()
{
var
Model
=
function
(
values
,
options
)
{
var
DAO
=
function
(
values
,
options
)
{
var
self
=
this
this
.
attributes
=
[]
this
.
validators
=
{}
// holds validation settings for each attribute
this
.
__factory
=
null
// will be set in
Model
.build
this
.
__factory
=
null
// will be set in
DAO
.build
this
.
__options
=
Utils
.
_
.
extend
({
underscored
:
false
,
hasPrimaryKeys
:
false
,
...
...
@@ -18,17 +18,17 @@ module.exports = (function() {
initAttributes
.
call
(
this
,
values
)
}
Utils
.
_
.
extend
(
Model
.
prototype
,
Mixin
.
prototype
)
Utils
.
_
.
extend
(
DAO
.
prototype
,
Mixin
.
prototype
)
Object
.
defineProperty
(
Model
.
prototype
,
'sequelize'
,
{
get
:
function
(){
return
this
.
__factory
.
model
FactoryManager
.
sequelize
}
Object
.
defineProperty
(
DAO
.
prototype
,
'sequelize'
,
{
get
:
function
(){
return
this
.
__factory
.
dao
FactoryManager
.
sequelize
}
})
Object
.
defineProperty
(
Model
.
prototype
,
'QueryInterface'
,
{
Object
.
defineProperty
(
DAO
.
prototype
,
'QueryInterface'
,
{
get
:
function
(){
return
this
.
sequelize
.
getQueryInterface
()
}
})
Object
.
defineProperty
(
Model
.
prototype
,
'isDeleted'
,
{
Object
.
defineProperty
(
DAO
.
prototype
,
'isDeleted'
,
{
get
:
function
()
{
var
result
=
this
.
__options
.
timestamps
&&
this
.
__options
.
paranoid
result
=
result
&&
this
[
this
.
__options
.
underscored
?
'deleted_at'
:
'deletedAt'
]
!=
null
...
...
@@ -37,7 +37,7 @@ module.exports = (function() {
}
})
Object
.
defineProperty
(
Model
.
prototype
,
'values'
,
{
Object
.
defineProperty
(
DAO
.
prototype
,
'values'
,
{
get
:
function
()
{
var
result
=
{}
,
self
=
this
...
...
@@ -50,7 +50,7 @@ module.exports = (function() {
}
})
Object
.
defineProperty
(
Model
.
prototype
,
'primaryKeyValues'
,
{
Object
.
defineProperty
(
DAO
.
prototype
,
'primaryKeyValues'
,
{
get
:
function
()
{
var
result
=
{}
,
self
=
this
...
...
@@ -63,7 +63,7 @@ module.exports = (function() {
}
})
Object
.
defineProperty
(
Model
.
prototype
,
"identifiers"
,
{
Object
.
defineProperty
(
DAO
.
prototype
,
"identifiers"
,
{
get
:
function
()
{
var
primaryKeys
=
Utils
.
_
.
keys
(
this
.
__factory
.
primaryKeys
)
,
result
=
{}
...
...
@@ -80,7 +80,7 @@ module.exports = (function() {
}
})
Model
.
prototype
.
save
=
function
()
{
DAO
.
prototype
.
save
=
function
()
{
var
updatedAtAttr
=
this
.
__options
.
underscored
?
'updated_at'
:
'updatedAt'
if
(
this
.
hasOwnProperty
(
updatedAtAttr
))
...
...
@@ -98,11 +98,11 @@ module.exports = (function() {
}
/*
* Validate this
model's attribute values according to validation rules set in the model
definition.
* Validate this
dao's attribute values according to validation rules set in the dao
definition.
*
* @return null if and only if validation successful; otherwise an object containing { field name : [error msgs] } entries.
*/
Model
.
prototype
.
validate
=
function
()
{
DAO
.
prototype
.
validate
=
function
()
{
var
self
=
this
var
failures
=
{}
...
...
@@ -163,7 +163,7 @@ module.exports = (function() {
}
Model
.
prototype
.
updateAttributes
=
function
(
updates
)
{
DAO
.
prototype
.
updateAttributes
=
function
(
updates
)
{
var
self
=
this
var
readOnlyAttributes
=
Utils
.
_
.
keys
(
this
.
__factory
.
primaryKeys
)
...
...
@@ -184,7 +184,7 @@ module.exports = (function() {
return
this
.
save
()
}
Model
.
prototype
.
destroy
=
function
()
{
DAO
.
prototype
.
destroy
=
function
()
{
if
(
this
.
__options
.
timestamps
&&
this
.
__options
.
paranoid
)
{
var
attr
=
this
.
__options
.
underscored
?
'deleted_at'
:
'deletedAt'
this
[
attr
]
=
new
Date
()
...
...
@@ -195,7 +195,7 @@ module.exports = (function() {
}
}
Model
.
prototype
.
equals
=
function
(
other
)
{
DAO
.
prototype
.
equals
=
function
(
other
)
{
var
result
=
true
,
self
=
this
...
...
@@ -206,7 +206,7 @@ module.exports = (function() {
return
result
}
Model
.
prototype
.
equalsOneOf
=
function
(
others
)
{
DAO
.
prototype
.
equalsOneOf
=
function
(
others
)
{
var
result
=
false
,
self
=
this
...
...
@@ -215,12 +215,12 @@ module.exports = (function() {
return
result
}
Model
.
prototype
.
addAttribute
=
function
(
attribute
,
value
)
{
DAO
.
prototype
.
addAttribute
=
function
(
attribute
,
value
)
{
this
[
attribute
]
=
value
this
.
attributes
.
push
(
attribute
)
}
Model
.
prototype
.
setValidators
=
function
(
attribute
,
validators
)
{
DAO
.
prototype
.
setValidators
=
function
(
attribute
,
validators
)
{
this
.
validators
[
attribute
]
=
validators
}
...
...
@@ -229,11 +229,11 @@ module.exports = (function() {
var
initAttributes
=
function
(
values
)
{
var
self
=
this
// add all passed values to the
model
and store the attribute names in this.attributes
// add all passed values to the
dao
and store the attribute names in this.attributes
Utils
.
_
.
map
(
values
,
function
(
value
,
key
)
{
self
.
addAttribute
(
key
,
value
)
})
// set id to null if not passed as value
// a newly created
model
has no id
// a newly created
dao
has no id
var
defaults
=
this
.
__options
.
hasPrimaryKeys
?
{}
:
{
id
:
null
}
if
(
this
.
__options
.
timestamps
)
{
...
...
@@ -250,8 +250,8 @@ module.exports = (function() {
})
}
/* Add the instance methods to
Model
*/
Utils
.
_
.
extend
(
Model
.
prototype
,
Mixin
.
prototype
)
/* Add the instance methods to
DAO
*/
Utils
.
_
.
extend
(
DAO
.
prototype
,
Mixin
.
prototype
)
return
Model
return
DAO
})()
lib/migrator.js
View file @
8ab2e6d
...
...
@@ -121,14 +121,14 @@ module.exports = (function() {
}
}
Migrator
.
prototype
.
findOrCreateSequelizeMeta
Model
=
function
(
syncOptions
)
{
Migrator
.
prototype
.
findOrCreateSequelizeMeta
DAO
=
function
(
syncOptions
)
{
var
self
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
stored
Model
=
self
.
sequelize
.
modelFactoryManager
.
getModel
(
'SequelizeMeta'
)
,
SequelizeMeta
=
stored
Model
var
stored
DAO
=
self
.
sequelize
.
daoFactoryManager
.
getDAO
(
'SequelizeMeta'
)
,
SequelizeMeta
=
stored
DAO
if
(
!
stored
Model
)
{
if
(
!
stored
DAO
)
{
SequelizeMeta
=
self
.
sequelize
.
define
(
'SequelizeMeta'
,
{
from
:
DataTypes
.
STRING
,
to
:
DataTypes
.
STRING
...
...
@@ -136,7 +136,7 @@ module.exports = (function() {
}
// force sync when model has newly created or if syncOptions are passed
if
(
!
stored
Model
||
syncOptions
)
{
if
(
!
stored
DAO
||
syncOptions
)
{
SequelizeMeta
.
sync
(
syncOptions
||
{})
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
SequelizeMeta
)
})
...
...
@@ -153,7 +153,7 @@ module.exports = (function() {
var
self
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
findOrCreateSequelizeMeta
Model
().
success
(
function
(
SequelizeMeta
)
{
self
.
findOrCreateSequelizeMeta
DAO
().
success
(
function
(
SequelizeMeta
)
{
SequelizeMeta
.
find
({
order
:
'id DESC'
}).
success
(
function
(
meta
)
{
emitter
.
emit
(
'success'
,
meta
?
meta
:
null
)
}).
error
(
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
...
...
@@ -194,7 +194,7 @@ module.exports = (function() {
var
saveSuccessfulMigration
=
function
(
from
,
to
,
callback
)
{
var
self
=
this
self
.
findOrCreateSequelizeMeta
Model
().
success
(
function
(
SequelizeMeta
)
{
self
.
findOrCreateSequelizeMeta
DAO
().
success
(
function
(
SequelizeMeta
)
{
SequelizeMeta
.
create
({
from
:
from
.
migrationId
,
to
:
to
.
migrationId
})
.
success
(
callback
)
...
...
@@ -204,7 +204,7 @@ module.exports = (function() {
var
deleteUndoneMigration
=
function
(
from
,
to
,
callback
)
{
var
self
=
this
self
.
findOrCreateSequelizeMeta
Model
().
success
(
function
(
SequelizeMeta
)
{
self
.
findOrCreateSequelizeMeta
DAO
().
success
(
function
(
SequelizeMeta
)
{
SequelizeMeta
.
find
({
from
:
from
.
migrationId
,
to
:
to
.
migrationId
})
.
success
(
function
(
meta
)
{
...
...
lib/model-factory-manager.js
deleted
100644 → 0
View file @
1b41c16
module
.
exports
=
(
function
()
{
var
ModelFactoryManager
=
function
(
sequelize
)
{
this
.
models
=
[]
this
.
sequelize
=
sequelize
}
ModelFactoryManager
.
prototype
.
addModel
=
function
(
model
)
{
this
.
models
.
push
(
model
)
return
model
}
ModelFactoryManager
.
prototype
.
removeModel
=
function
(
model
)
{
this
.
models
=
this
.
models
.
filter
(
function
(
_model
)
{
return
_model
.
name
!=
model
.
name
})
}
ModelFactoryManager
.
prototype
.
getModel
=
function
(
modelName
)
{
var
model
=
this
.
models
.
filter
(
function
(
model
)
{
return
model
.
name
==
modelName
})
return
!!
model
?
model
[
0
]
:
null
}
ModelFactoryManager
.
prototype
.
__defineGetter__
(
'all'
,
function
()
{
return
this
.
models
})
return
ModelFactoryManager
})()
lib/query-interface.js
View file @
8ab2e6d
...
...
@@ -170,21 +170,21 @@ module.exports = (function() {
return
queryAndEmit
.
call
(
this
,
sql
,
"removeIndex"
)
}
QueryInterface
.
prototype
.
insert
=
function
(
model
,
tableName
,
values
)
{
QueryInterface
.
prototype
.
insert
=
function
(
dao
,
tableName
,
values
)
{
var
sql
=
this
.
QueryGenerator
.
insertQuery
(
tableName
,
values
)
return
queryAndEmit
.
call
(
this
,
[
sql
,
model
],
'insert'
,
{
return
queryAndEmit
.
call
(
this
,
[
sql
,
dao
],
'insert'
,
{
success
:
function
(
obj
)
{
obj
.
isNewRecord
=
false
}
})
}
QueryInterface
.
prototype
.
update
=
function
(
model
,
tableName
,
values
,
identifier
)
{
QueryInterface
.
prototype
.
update
=
function
(
dao
,
tableName
,
values
,
identifier
)
{
var
sql
=
this
.
QueryGenerator
.
updateQuery
(
tableName
,
values
,
identifier
)
return
queryAndEmit
.
call
(
this
,
[
sql
,
model
],
'update'
)
return
queryAndEmit
.
call
(
this
,
[
sql
,
dao
],
'update'
)
}
QueryInterface
.
prototype
.
delete
=
function
(
model
,
tableName
,
identifier
)
{
QueryInterface
.
prototype
.
delete
=
function
(
dao
,
tableName
,
identifier
)
{
var
sql
=
this
.
QueryGenerator
.
deleteQuery
(
tableName
,
identifier
)
return
queryAndEmit
.
call
(
this
,
[
sql
,
model
],
'delete'
)
return
queryAndEmit
.
call
(
this
,
[
sql
,
dao
],
'delete'
)
}
QueryInterface
.
prototype
.
select
=
function
(
factory
,
tableName
,
options
,
queryOptions
)
{
...
...
lib/sequelize.js
View file @
8ab2e6d
var
Utils
=
require
(
"./utils"
)
,
ModelFactory
=
require
(
"./model
-factory"
)
,
DAOFactory
=
require
(
"./dao
-factory"
)
,
DataTypes
=
require
(
'./data-types'
)
,
ModelFactoryManager
=
require
(
"./model
-factory-manager"
)
,
DAOFactoryManager
=
require
(
"./dao
-factory-manager"
)
,
Migrator
=
require
(
"./migrator"
)
,
QueryInterface
=
require
(
"./query-interface"
)
...
...
@@ -26,7 +26,7 @@ module.exports = (function() {
var
ConnectorManager
=
require
(
"./dialects/"
+
this
.
options
.
dialect
+
"/connector-manager"
)
this
.
modelFactoryManager
=
new
Model
FactoryManager
(
this
)
this
.
daoFactoryManager
=
new
DAO
FactoryManager
(
this
)
this
.
connectorManager
=
new
ConnectorManager
(
this
,
this
.
config
)
}
...
...
@@ -47,15 +47,15 @@ module.exports = (function() {
return
this
.
migrator
}
Sequelize
.
prototype
.
define
=
function
(
model
Name
,
attributes
,
options
)
{
Sequelize
.
prototype
.
define
=
function
(
dao
Name
,
attributes
,
options
)
{
options
=
options
||
{}
if
(
this
.
options
.
define
)
options
=
Sequelize
.
Utils
.
merge
(
options
,
this
.
options
.
define
)
var
factory
=
new
ModelFactory
(
model
Name
,
attributes
,
options
)
var
factory
=
new
DAOFactory
(
dao
Name
,
attributes
,
options
)
this
.
modelFactoryManager
.
addModel
(
factory
.
init
(
this
.
model
FactoryManager
))
this
.
daoFactoryManager
.
addDAO
(
factory
.
init
(
this
.
dao
FactoryManager
))
return
factory
}
...
...
@@ -89,7 +89,7 @@ module.exports = (function() {
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
chainer
=
new
Utils
.
QueryChainer
self
.
modelFactoryManager
.
models
.
forEach
(
function
(
model
)
{
chainer
.
add
(
model
.
sync
(
options
))
})
self
.
daoFactoryManager
.
daos
.
forEach
(
function
(
dao
)
{
chainer
.
add
(
dao
.
sync
(
options
))
})
chainer
.
run
()
...
...
@@ -104,7 +104,7 @@ module.exports = (function() {
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
chainer
=
new
Utils
.
QueryChainer
self
.
modelFactoryManager
.
models
.
forEach
(
function
(
model
)
{
chainer
.
add
(
model
.
drop
())
})
self
.
daoFactoryManager
.
daos
.
forEach
(
function
(
dao
)
{
chainer
.
add
(
dao
.
drop
())
})
chainer
.
run
()
...
...
spec/associations/has-many.spec.js
View file @
8ab2e6d
...
...
@@ -103,13 +103,13 @@ describe('HasMany', function() {
expect
(
Task
.
attributes
.
UserId
).
toBeUndefined
()
expect
(
User
.
attributes
.
UserId
).
toBeUndefined
()
var
models
=
sequelize
.
modelFactoryManager
.
models
.
filter
(
function
(
model
)
{
return
(
model
.
tableName
==
(
Task
.
tableName
+
User
.
tableName
))
var
daos
=
sequelize
.
daoFactoryManager
.
daos
.
filter
(
function
(
dao
)
{
return
(
dao
.
tableName
==
(
Task
.
tableName
+
User
.
tableName
))
})
models
.
forEach
(
function
(
model
)
{
expect
(
model
.
attributes
.
UserId
).
toBeDefined
()
expect
(
model
.
attributes
.
TaskId
).
toBeDefined
()
daos
.
forEach
(
function
(
dao
)
{
expect
(
dao
.
attributes
.
UserId
).
toBeDefined
()
expect
(
dao
.
attributes
.
TaskId
).
toBeDefined
()
})
})
...
...
@@ -123,13 +123,13 @@ describe('HasMany', function() {
expect
(
Task
.
attributes
.
user_id
).
toBeUndefined
()
expect
(
User
.
attributes
.
user_id
).
toBeUndefined
()
var
models
=
sequelize
.
modelFactoryManager
.
models
.
filter
(
function
(
model
)
{
return
(
model
.
tableName
==
(
Task
.
tableName
+
User
.
tableName
))
var
daos
=
sequelize
.
daoFactoryManager
.
daos
.
filter
(
function
(
dao
)
{
return
(
dao
.
tableName
==
(
Task
.
tableName
+
User
.
tableName
))
})
models
.
forEach
(
function
(
model
)
{
expect
(
model
.
attributes
.
user_id
).
toBeDefined
()
expect
(
model
.
attributes
.
TaskId
).
toBeDefined
()
daos
.
forEach
(
function
(
dao
)
{
expect
(
dao
.
attributes
.
user_id
).
toBeDefined
()
expect
(
dao
.
attributes
.
TaskId
).
toBeDefined
()
})
})
...
...
@@ -137,13 +137,13 @@ describe('HasMany', function() {
User
.
hasMany
(
Task
,
{
foreignKey
:
'person_id'
})
Task
.
hasMany
(
User
,
{
foreignKey
:
'work_item_id'
})
var
models
=
sequelize
.
modelFactoryManager
.
models
.
filter
(
function
(
model
)
{
return
(
model
.
tableName
==
(
Task
.
tableName
+
User
.
tableName
))
var
daos
=
sequelize
.
daoFactoryManager
.
daos
.
filter
(
function
(
dao
)
{
return
(
dao
.
tableName
==
(
Task
.
tableName
+
User
.
tableName
))
})
models
.
forEach
(
function
(
model
)
{
expect
(
model
.
attributes
.
person_id
).
toBeDefined
()
expect
(
model
.
attributes
.
work_item_id
).
toBeDefined
()
daos
.
forEach
(
function
(
dao
)
{
expect
(
dao
.
attributes
.
person_id
).
toBeDefined
()
expect
(
dao
.
attributes
.
work_item_id
).
toBeDefined
()
})
})
...
...
@@ -219,7 +219,7 @@ describe('HasMany', function() {
})
})
it
(
"build the connector
model
s name"
,
function
()
{
it
(
"build the connector
dao
s name"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
var
Person
=
sequelize
.
define
(
'Person'
,
{
name
:
Sequelize
.
STRING
})
...
...
@@ -228,11 +228,11 @@ describe('HasMany', function() {
Person
.
hasMany
(
Person
,
{
as
:
'CoWorkers'
})
Person
.
sync
({
force
:
true
}).
success
(
function
()
{
var
modelNames
=
sequelize
.
modelFactoryManager
.
models
.
map
(
function
(
model
)
{
return
model
.
tableName
})
var
daoNames
=
sequelize
.
daoFactoryManager
.
daos
.
map
(
function
(
dao
)
{
return
dao
.
tableName
})
,
expectation
=
[
"Persons"
,
"ChildrenPersons"
,
"CoWorkersPersons"
,
"FriendsPersons"
]
expectation
.
forEach
(
function
(
ex
)
{
expect
(
model
Names
.
indexOf
(
ex
)
>
-
1
).
toBeTruthy
()
expect
(
dao
Names
.
indexOf
(
ex
)
>
-
1
).
toBeTruthy
()
})
done
()
...
...
@@ -279,7 +279,7 @@ describe('HasMany', function() {
})
it
(
"gets and sets the connector
model
s"
,
function
()
{
it
(
"gets and sets the connector
dao
s"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
var
Person
=
sequelize
.
define
(
'Person'
,
{
name
:
Sequelize
.
STRING
})
...
...
spec/config/factories.js
View file @
8ab2e6d
...
...
@@ -3,18 +3,18 @@ var Factories = module.exports = function(helpers) {
this
.
sequelize
=
this
.
helpers
.
sequelize
}
Factories
.
prototype
.
Model
=
function
(
model
Name
,
options
,
callback
,
count
)
{
Factories
.
prototype
.
DAO
=
function
(
dao
Name
,
options
,
callback
,
count
)
{
count
=
count
||
1
var
self
=
this
,
model
s
=
[]
,
dao
s
=
[]
this
.
helpers
.
async
(
function
(
done
)
{
var
Model
=
self
.
sequelize
.
modelFactoryManager
.
getModel
(
model
Name
)
var
DAO
=
self
.
sequelize
.
daoFactoryManager
.
getDAO
(
dao
Name
)
var
create
=
function
(
cb
)
{
Model
.
create
(
options
).
on
(
'success'
,
function
(
model
)
{
models
.
push
(
model
)
DAO
.
create
(
options
).
on
(
'success'
,
function
(
dao
)
{
daos
.
push
(
dao
)
cb
&&
cb
()
}).
on
(
'failure'
,
function
(
err
)
{
console
.
log
(
err
)
...
...
@@ -27,7 +27,7 @@ Factories.prototype.Model = function(modelName, options, callback, count) {
create
(
cb
)
}
else
{
done
()
callback
&&
callback
(
model
s
)
callback
&&
callback
(
dao
s
)
}
}
...
...
@@ -36,5 +36,5 @@ Factories.prototype.Model = function(modelName, options, callback, count) {
}
Factories
.
prototype
.
User
=
function
(
options
,
callback
,
count
)
{
this
.
Model
(
'User'
,
options
,
callback
,
count
)
this
.
DAO
(
'User'
,
options
,
callback
,
count
)
}
spec/
model
-factory.spec.js
→
spec/
dao
-factory.spec.js
View file @
8ab2e6d
...
...
@@ -2,7 +2,7 @@ var config = require("./config/config")
,
Sequelize
=
require
(
"../index"
)
,
dialects
=
[
'sqlite'
,
'mysql'
]
describe
(
'
Model
Factory'
,
function
()
{
describe
(
'
DAO
Factory'
,
function
()
{
dialects
.
forEach
(
function
(
dialect
)
{
describe
(
'with dialect "'
+
dialect
+
'"'
,
function
()
{
var
User
=
null
...
...
@@ -37,12 +37,12 @@ describe('ModelFactory', function() {
afterEach
(
function
()
{
Helpers
.
dropAllTables
()
})
describe
(
'constructor'
,
function
()
{
it
(
"uses the passed
model
name as tablename if freezeTableName"
,
function
()
{
it
(
"uses the passed
dao
name as tablename if freezeTableName"
,
function
()
{
var
User
=
sequelize
.
define
(
'User'
,
{},
{
freezeTableName
:
true
})
expect
(
User
.
tableName
).
toEqual
(
'User'
)
})
it
(
"uses the pluralized
model
name as tablename unless freezeTableName"
,
function
()
{
it
(
"uses the pluralized
dao
name as tablename unless freezeTableName"
,
function
()
{
var
User
=
sequelize
.
define
(
'User'
,
{},
{
freezeTableName
:
false
})
expect
(
User
.
tableName
).
toEqual
(
'Users'
)
})
...
...
@@ -66,7 +66,7 @@ describe('ModelFactory', function() {
userid
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
userscore
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
})
}).
toThrow
(
'Invalid
model
definition. Only one autoincrement field allowed.'
)
}).
toThrow
(
'Invalid
DAO
definition. Only one autoincrement field allowed.'
)
})
})
...
...
@@ -199,7 +199,7 @@ describe('ModelFactory', function() {
})
describe
(
'destroy'
,
function
()
{
it
(
'deletes a record from the database if
model
is not paranoid'
,
function
()
{
it
(
'deletes a record from the database if
dao
is not paranoid'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
=
sequelize
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
,
...
...
@@ -246,7 +246,7 @@ describe('ModelFactory', function() {
})
})
it
(
'marks the database entry as deleted if
model
is paranoid'
,
function
()
{
it
(
'marks the database entry as deleted if
dao
is paranoid'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
=
sequelize
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
...
...
@@ -305,7 +305,7 @@ describe('ModelFactory', function() {
})
})
it
(
'returns a single
model
'
,
function
()
{
it
(
'returns a single
dao
'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
.
find
(
users
[
0
].
id
).
success
(
function
(
user
)
{
expect
(
Array
.
isArray
(
user
)).
toBeFalsy
()
...
...
@@ -635,12 +635,12 @@ describe('ModelFactory', function() {
})
describe
(
'Mixin'
,
function
()
{
var
ModelFactory
=
require
(
"../lib/model
-factory"
)
var
DAOFactory
=
require
(
"../lib/dao
-factory"
)
it
(
"adds the mixed-in functions to the
model
"
,
function
()
{
expect
(
Model
Factory
.
prototype
.
hasOne
).
toBeDefined
()
expect
(
Model
Factory
.
prototype
.
hasMany
).
toBeDefined
()
expect
(
Model
Factory
.
prototype
.
belongsTo
).
toBeDefined
()
it
(
"adds the mixed-in functions to the
dao
"
,
function
()
{
expect
(
DAO
Factory
.
prototype
.
hasOne
).
toBeDefined
()
expect
(
DAO
Factory
.
prototype
.
hasMany
).
toBeDefined
()
expect
(
DAO
Factory
.
prototype
.
belongsTo
).
toBeDefined
()
})
})
...
...
spec/
model
.spec.js
→
spec/
dao
.spec.js
View file @
8ab2e6d
...
...
@@ -2,7 +2,7 @@ var config = require("./config/config")
,
Sequelize
=
require
(
"../index"
)
,
dialects
=
[
'sqlite'
,
'mysql'
]
describe
(
'
Model
'
,
function
()
{
describe
(
'
DAO
'
,
function
()
{
dialects
.
forEach
(
function
(
dialect
)
{
describe
(
'with dialect "'
+
dialect
+
'"'
,
function
()
{
var
User
=
null
...
...
spec/migrator.spec.js
View file @
8ab2e6d
...
...
@@ -18,7 +18,7 @@ describe('Migrator', function() {
migrator
=
new
Migrator
(
sequelize
,
options
)
migrator
.
findOrCreateSequelizeMeta
Model
({
force
:
true
})
.
findOrCreateSequelizeMeta
DAO
({
force
:
true
})
.
success
(
function
(
_SequelizeMeta
)
{
SequelizeMeta
=
_SequelizeMeta
done
()
...
...
spec/mysql/associations.has-many.spec.js
View file @
8ab2e6d
...
...
@@ -18,18 +18,18 @@ describe('HasMany', function() {
beforeEach
(
function
()
{
Helpers
.
async
(
function
(
_done
)
{
Helpers
.
Factories
.
Model
(
User
.
name
,
{
name
:
'User'
+
Math
.
random
()},
function
(
_users
)
{
Helpers
.
Factories
.
DAO
(
User
.
name
,
{
name
:
'User'
+
Math
.
random
()},
function
(
_users
)
{
users
=
_users
;
_done
()
},
5
)
})
Helpers
.
async
(
function
(
_done
)
{
Helpers
.
Factories
.
Model
(
Task
.
name
,
{
name
:
'Task'
+
Math
.
random
()},
function
(
_tasks
)
{
Helpers
.
Factories
.
DAO
(
Task
.
name
,
{
name
:
'Task'
+
Math
.
random
()},
function
(
_tasks
)
{
tasks
=
_tasks
;
_done
()
},
2
)
})
})
describe
(
'add
Model / getModel
'
,
function
()
{
describe
(
'add
DAO / getDAO
'
,
function
()
{
var
user
=
null
,
task
=
null
...
...
@@ -46,7 +46,7 @@ describe('HasMany', function() {
})
})
it
(
'should correctly add an association to the
model
'
,
function
()
{
it
(
'should correctly add an association to the
dao
'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
user
.
getTasks
().
on
(
'success'
,
function
(
_tasks
)
{
expect
(
_tasks
.
length
).
toEqual
(
0
)
...
...
@@ -61,7 +61,7 @@ describe('HasMany', function() {
})
})
describe
(
'remove
Model
'
,
function
()
{
describe
(
'remove
DAO
'
,
function
()
{
var
user
=
null
,
tasks
=
null
...
...
spec/mysql/associations.spec.js
View file @
8ab2e6d
...
...
@@ -19,7 +19,7 @@ describe('Associations', function() {
it
(
"should create a table wp_table1wp_table2s"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
expect
(
sequelize
.
modelFactoryManager
.
getModel
(
'wp_table1swp_table2s'
)).
toBeDefined
()
expect
(
sequelize
.
daoFactoryManager
.
getDAO
(
'wp_table1swp_table2s'
)).
toBeDefined
()
done
()
})
})
...
...
@@ -32,10 +32,10 @@ describe('Associations', function() {
Table2
.
hasMany
(
Table1
,
{
joinTableName
:
'table1_to_table2'
})
it
(
"should not use a combined name"
,
function
()
{
expect
(
sequelize
.
modelFactoryManager
.
getModel
(
'ms_table1sms_table2s'
)).
toBeUndefined
()
expect
(
sequelize
.
daoFactoryManager
.
getDAO
(
'ms_table1sms_table2s'
)).
toBeUndefined
()
})
it
(
"should use the specified name"
,
function
()
{
expect
(
sequelize
.
modelFactoryManager
.
getModel
(
'table1_to_table2'
)).
toBeDefined
()
expect
(
sequelize
.
daoFactoryManager
.
getDAO
(
'table1_to_table2'
)).
toBeDefined
()
})
})
})
...
...
spec/mysql/
model
-factory.spec.js
→
spec/mysql/
dao
-factory.spec.js
View file @
8ab2e6d
...
...
@@ -3,7 +3,7 @@ var config = require("../config/config")
,
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
{
logging
:
false
})
,
Helpers
=
new
(
require
(
"../config/helpers"
))(
sequelize
)
describe
(
'
Model
Factory'
,
function
()
{
describe
(
'
DAO
Factory'
,
function
()
{
beforeEach
(
function
()
{
Helpers
.
sync
()
})
afterEach
(
function
()
{
Helpers
.
drop
()
})
...
...
spec/sequelize.spec.js
View file @
8ab2e6d
...
...
@@ -22,8 +22,8 @@ describe('Sequelize', function() {
it
(
'should pass the global options correctly'
,
function
()
{
setup
({
logging
:
false
,
define
:
{
underscored
:
true
}
})
var
Model
=
sequelize
.
define
(
'model
'
,
{
name
:
Sequelize
.
STRING
})
expect
(
Model
.
options
.
underscored
).
toBeTruthy
()
var
DAO
=
sequelize
.
define
(
'dao
'
,
{
name
:
Sequelize
.
STRING
})
expect
(
DAO
.
options
.
underscored
).
toBeTruthy
()
})
it
(
'should correctly set the host and the port'
,
function
()
{
...
...
@@ -35,15 +35,15 @@ describe('Sequelize', function() {
})
describe
(
'define'
,
function
()
{
it
(
"adds a new
model to the model
manager"
,
function
()
{
expect
(
sequelize
.
model
FactoryManager
.
all
.
length
).
toEqual
(
0
)
it
(
"adds a new
dao to the dao
manager"
,
function
()
{
expect
(
sequelize
.
dao
FactoryManager
.
all
.
length
).
toEqual
(
0
)
sequelize
.
define
(
'foo'
,
{
title
:
Sequelize
.
STRING
})
expect
(
sequelize
.
model
FactoryManager
.
all
.
length
).
toEqual
(
1
)
expect
(
sequelize
.
dao
FactoryManager
.
all
.
length
).
toEqual
(
1
)
})
})
describe
(
'sync'
,
function
()
{
it
(
"synchronizes all
model
s"
,
function
()
{
it
(
"synchronizes all
dao
s"
,
function
()
{
var
Project
=
sequelize
.
define
(
'project'
+
config
.
rand
(),
{
title
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'task'
+
config
.
rand
(),
{
title
:
Sequelize
.
STRING
})
...
...
@@ -58,7 +58,7 @@ describe('Sequelize', function() {
})
describe
(
'import'
,
function
()
{
it
(
"imports a
model
definition from a file"
,
function
()
{
it
(
"imports a
dao
definition from a file"
,
function
()
{
var
Project
=
sequelize
.
import
(
__dirname
+
"/assets/project"
)
expect
(
Project
).
toBeDefined
()
})
...
...
spec/sqlite/
model
-factory.spec.js
→
spec/sqlite/
dao
-factory.spec.js
View file @
8ab2e6d
...
...
@@ -3,7 +3,7 @@ var config = require("../config/config")
,
dbFile
=
__dirname
+
'/test.sqlite'
,
storages
=
[
':memory:'
,
dbFile
]
describe
(
'
Model
Factory'
,
function
()
{
describe
(
'
DAO
Factory'
,
function
()
{
storages
.
forEach
(
function
(
storage
)
{
describe
(
'with storage "'
+
storage
+
'"'
,
function
()
{
var
User
=
null
...
...
@@ -68,7 +68,7 @@ describe('ModelFactory', function() {
Helpers
.
async
(
function
(
done
)
{
var
options
=
JSON
.
stringify
({
foo
:
'bar'
,
bar
:
'foo'
})
Helpers
.
Factories
.
Model
(
'Person'
,
{
Helpers
.
Factories
.
DAO
(
'Person'
,
{
name
:
'John Doe'
,
options
:
options
},
function
(
people
)
{
...
...
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