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 46f8b891
authored
Feb 07, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor primary key stuff
1 parent
feb56a6e
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
48 deletions
lib/dao-factory.js
lib/dao.js
test/dao/values.test.js
lib/dao-factory.js
View file @
46f8b89
...
@@ -120,72 +120,81 @@ module.exports = (function() {
...
@@ -120,72 +120,81 @@ module.exports = (function() {
this
.
primaryKeys
=
{}
this
.
primaryKeys
=
{}
self
.
options
.
uniqueKeys
=
{}
self
.
options
.
uniqueKeys
=
{}
Utils
.
_
.
each
(
this
.
rawAttributes
,
function
(
columnValues
,
columnName
)
{
// Setup names of timestamp attributes
if
(
columnValues
.
hasOwnProperty
(
'unique'
)
&&
columnValues
.
unique
!==
true
&&
columnValues
.
unique
!==
false
)
{
this
.
_timestampAttributes
=
{}
var
idxName
=
columnValues
.
unique
if
(
this
.
options
.
timestamps
)
{
if
(
typeof
columnValues
.
unique
===
"object"
)
{
if
(
this
.
options
.
createdAt
)
{
idxName
=
columnValues
.
unique
.
name
this
.
_timestampAttributes
.
createdAt
=
Utils
.
_
.
underscoredIf
(
this
.
options
.
createdAt
,
this
.
options
.
underscored
)
}
if
(
this
.
options
.
updatedAt
)
{
this
.
_timestampAttributes
.
updatedAt
=
Utils
.
_
.
underscoredIf
(
this
.
options
.
updatedAt
,
this
.
options
.
underscored
)
}
if
(
this
.
options
.
paranoid
&&
this
.
options
.
deletedAt
)
{
this
.
_timestampAttributes
.
deletedAt
=
Utils
.
_
.
underscoredIf
(
this
.
options
.
deletedAt
,
this
.
options
.
underscored
)
}
}
// Identify primary and unique attributes
Utils
.
_
.
each
(
this
.
rawAttributes
,
function
(
options
,
attribute
)
{
if
(
options
.
hasOwnProperty
(
'unique'
)
&&
options
.
unique
!==
true
&&
options
.
unique
!==
false
)
{
var
idxName
=
options
.
unique
if
(
typeof
options
.
unique
===
"object"
)
{
idxName
=
options
.
unique
.
name
}
}
self
.
options
.
uniqueKeys
[
idxName
]
=
self
.
options
.
uniqueKeys
[
idxName
]
||
{
fields
:
[],
msg
:
null
}
self
.
options
.
uniqueKeys
[
idxName
]
=
self
.
options
.
uniqueKeys
[
idxName
]
||
{
fields
:
[],
msg
:
null
}
self
.
options
.
uniqueKeys
[
idxName
].
fields
.
push
(
columnNam
e
)
self
.
options
.
uniqueKeys
[
idxName
].
fields
.
push
(
attribut
e
)
self
.
options
.
uniqueKeys
[
idxName
].
msg
=
self
.
options
.
uniqueKeys
[
idxName
].
msg
||
columnValue
s
.
unique
.
msg
||
null
self
.
options
.
uniqueKeys
[
idxName
].
msg
=
self
.
options
.
uniqueKeys
[
idxName
].
msg
||
option
s
.
unique
.
msg
||
null
}
}
})
Utils
.
_
.
each
(
this
.
attributes
,
function
(
dataTypeString
,
attributeName
)
{
if
(
options
.
primaryKey
===
true
)
{
if
(
dataTypeString
.
indexOf
(
'PRIMARY KEY'
)
!==
-
1
)
{
self
.
primaryKeys
[
attribute
]
=
self
.
attributes
[
attribute
]
self
.
primaryKeys
[
attributeName
]
=
dataTypeString
}
}
})
})
// Add head and tail default attributes (id, timestamps)
addDefaultAttributes
.
call
(
this
)
addOptionalClassMethods
.
call
(
this
)
findAutoIncrementField
.
call
(
this
)
// Primary key convenience variables
this
.
primaryKeyAttributes
=
Object
.
keys
(
this
.
primaryKeys
)
this
.
primaryKeyAttributes
=
Object
.
keys
(
this
.
primaryKeys
)
this
.
primaryKeyAttribute
=
this
.
primaryKeyAttributes
[
0
]
this
.
primaryKeyCount
=
this
.
primaryKeyAttributes
.
length
this
.
primaryKeyCount
=
this
.
primaryKeyAttributes
.
length
this
.
options
.
hasPrimaryKeys
=
this
.
hasPrimaryKeys
=
this
.
primaryKeyCount
>
0
this
.
_hasPrimaryKeys
=
this
.
options
.
hasPrimaryKeys
=
this
.
hasPrimaryKeys
=
this
.
primaryKeyCount
>
0
this
.
_isPrimaryKey
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
primaryKeyAttributes
.
indexOf
(
key
)
!==
-
1
})
if
(
typeof
this
.
options
.
defaultScope
===
"object"
)
{
if
(
typeof
this
.
options
.
defaultScope
===
"object"
)
{
Utils
.
injectScope
.
call
(
this
,
this
.
options
.
defaultScope
)
Utils
.
injectScope
.
call
(
this
,
this
.
options
.
defaultScope
)
}
}
// DAO prototype
// DAO prototype
// WTF ... ?
this
.
DAO
=
function
()
{
this
.
DAO
=
function
()
{
DAO
.
apply
(
this
,
arguments
);
DAO
.
apply
(
this
,
arguments
);
}
}
Util
.
inherits
(
this
.
DAO
,
DAO
);
Util
.
inherits
(
this
.
DAO
,
DAO
);
this
.
_timestampAttributes
=
{}
this
.
_readOnlyAttributes
=
Object
.
keys
(
this
.
_timestampAttributes
)
if
(
this
.
options
.
timestamps
)
{
this
.
DAO
.
prototype
.
_readOnlyAttributes
=
this
.
_readOnlyAttributes
// To be removed
if
(
this
.
options
.
createdAt
)
{
this
.
_timestampAttributes
.
createdAt
=
Utils
.
_
.
underscoredIf
(
this
.
options
.
createdAt
,
this
.
options
.
underscored
)
}
if
(
this
.
options
.
updatedAt
)
{
this
.
_timestampAttributes
.
updatedAt
=
Utils
.
_
.
underscoredIf
(
this
.
options
.
updatedAt
,
this
.
options
.
underscored
)
}
if
(
this
.
options
.
paranoid
&&
this
.
options
.
deletedAt
)
{
this
.
_timestampAttributes
.
deletedAt
=
Utils
.
_
.
underscoredIf
(
this
.
options
.
deletedAt
,
this
.
options
.
underscored
)
}
this
.
DAO
.
prototype
.
_readOnlyAttributes
=
Object
.
keys
(
this
.
_timestampAttributes
)
this
.
_hasReadOnlyAttributes
=
this
.
_readOnlyAttributes
&&
this
.
_readOnlyAttributes
.
length
}
this
.
DAO
.
prototype
.
_hasReadOnlyAttributes
=
this
.
_hasReadOnlyAttributes
// To be removed
this
.
DAO
.
prototype
.
_hasReadOnlyAttributes
=
this
.
DAO
.
prototype
.
_readOnlyAttributes
&&
this
.
DAO
.
prototype
.
_readOnlyAttributes
.
length
this
.
_isReadOnlyAttribute
=
Utils
.
_
.
memoize
(
function
(
key
)
{
this
.
DAO
.
prototype
.
_isReadOnlyAttribute
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
_hasReadOnlyAttributes
&&
self
.
_readOnlyAttributes
.
indexOf
(
key
)
!==
-
1
return
self
.
DAO
.
prototype
.
_hasReadOnlyAttributes
&&
self
.
DAO
.
prototype
.
_readOnlyAttributes
.
indexOf
(
key
)
!==
-
1
})
})
this
.
DAO
.
prototype
.
_isReadOnlyAttribute
=
this
.
_isReadOnlyAttribute
// To be removed
addDefaultAttributes
.
call
(
this
)
this
.
DAO
.
prototype
.
_hasPrimaryKeys
=
this
.
_hasPrimaryKeys
// To be removed
addOptionalClassMethods
.
call
(
this
)
this
.
DAO
.
prototype
.
_isPrimaryKey
=
this
.
_isPrimaryKey
// To be removed
findAutoIncrementField
.
call
(
this
)
this
.
DAO
.
prototype
.
rawAttributes
=
this
.
rawAttributes
;
this
.
DAO
.
prototype
.
rawAttributes
=
this
.
rawAttributes
;
this
.
DAO
.
prototype
.
_hasPrimaryKeys
=
this
.
options
.
hasPrimaryKeys
this
.
DAO
.
prototype
.
_isPrimaryKey
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
primaryKeyAttributes
.
indexOf
(
key
)
!==
-
1
&&
key
!==
'id'
})
if
(
this
.
options
.
instanceMethods
)
{
if
(
this
.
options
.
instanceMethods
)
{
Utils
.
_
.
each
(
this
.
options
.
instanceMethods
,
function
(
fct
,
name
)
{
Utils
.
_
.
each
(
this
.
options
.
instanceMethods
,
function
(
fct
,
name
)
{
self
.
DAO
.
prototype
[
name
]
=
fct
self
.
DAO
.
prototype
[
name
]
=
fct
...
@@ -1273,7 +1282,11 @@ module.exports = (function() {
...
@@ -1273,7 +1282,11 @@ module.exports = (function() {
var
addDefaultAttributes
=
function
()
{
var
addDefaultAttributes
=
function
()
{
var
self
=
this
var
self
=
this
,
tail
=
{}
,
tail
=
{}
,
head
=
{
,
head
=
{}
// Add id if no primary key was manually added to definition
if
(
!
Object
.
keys
(
this
.
primaryKeys
).
length
)
{
head
=
{
id
:
{
id
:
{
type
:
DataTypes
.
INTEGER
,
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
allowNull
:
false
,
...
@@ -1282,9 +1295,6 @@ module.exports = (function() {
...
@@ -1282,9 +1295,6 @@ module.exports = (function() {
_autoGenerated
:
true
_autoGenerated
:
true
}
}
}
}
if
(
this
.
hasPrimaryKeys
)
{
head
=
{}
}
}
if
(
this
.
_timestampAttributes
.
createdAt
)
{
if
(
this
.
_timestampAttributes
.
createdAt
)
{
...
@@ -1313,6 +1323,10 @@ module.exports = (function() {
...
@@ -1313,6 +1323,10 @@ module.exports = (function() {
self
.
rawAttributes
[
attr
]
=
value
self
.
rawAttributes
[
attr
]
=
value
}
}
})
})
if
(
!
Object
.
keys
(
this
.
primaryKeys
).
length
)
{
self
.
primaryKeys
[
'id'
]
=
self
.
attributes
[
'id'
]
}
}
}
var
findAutoIncrementField
=
function
()
{
var
findAutoIncrementField
=
function
()
{
...
@@ -1341,8 +1355,12 @@ module.exports = (function() {
...
@@ -1341,8 +1355,12 @@ module.exports = (function() {
options
.
includeNames
.
push
(
include
.
as
)
options
.
includeNames
.
push
(
include
.
as
)
options
.
includeNames
.
push
(
include
.
as
.
substr
(
0
,
1
).
toLowerCase
()
+
include
.
as
.
substr
(
1
))
options
.
includeNames
.
push
(
include
.
as
.
substr
(
0
,
1
).
toLowerCase
()
+
include
.
as
.
substr
(
1
))
if
(
include
.
association
.
isMultiAssociation
||
include
.
hasMultiAssociation
)
options
.
hasMultiAssociation
=
true
if
(
include
.
association
.
isMultiAssociation
||
include
.
hasMultiAssociation
)
{
if
(
include
.
association
.
isSingleAssociation
||
include
.
hasSingleAssociation
)
options
.
hasSingleAssociation
=
true
options
.
hasMultiAssociation
=
true
}
if
(
include
.
association
.
isSingleAssociation
||
include
.
hasSingleAssociation
)
{
options
.
hasSingleAssociation
=
true
}
options
.
hasIncludeWhere
=
options
.
hasIncludeWhere
||
include
.
hasIncludeWhere
||
!!
include
.
where
options
.
hasIncludeWhere
=
options
.
hasIncludeWhere
||
include
.
hasIncludeWhere
||
!!
include
.
where
options
.
hasIncludeRequired
=
options
.
hasIncludeRequired
||
include
.
hasIncludeRequired
||
!!
include
.
required
options
.
hasIncludeRequired
=
options
.
hasIncludeRequired
||
include
.
hasIncludeRequired
||
!!
include
.
required
...
@@ -1374,8 +1392,8 @@ module.exports = (function() {
...
@@ -1374,8 +1392,8 @@ module.exports = (function() {
var
primaryKeys
;
var
primaryKeys
;
if
(
include
.
daoFactory
.
hasPrimaryKeys
)
{
if
(
include
.
daoFactory
.
hasPrimaryKeys
)
{
primaryKeys
=
[]
primaryKeys
=
[]
for
(
var
field
_name
in
include
.
daoFactory
.
primaryKeys
)
{
for
(
var
field
in
include
.
daoFactory
.
primaryKeys
)
{
primaryKeys
.
push
(
field
_name
)
primaryKeys
.
push
(
field
)
}
}
}
else
{
}
else
{
primaryKeys
=
[
'id'
]
primaryKeys
=
[
'id'
]
...
@@ -1386,7 +1404,9 @@ module.exports = (function() {
...
@@ -1386,7 +1404,9 @@ module.exports = (function() {
}
}
// pseudo include just needed the attribute logic, return
// pseudo include just needed the attribute logic, return
if
(
include
.
_pseudo
)
return
include
if
(
include
.
_pseudo
)
{
return
include
}
if
(
include
.
hasOwnProperty
(
'daoFactory'
)
&&
(
include
.
hasOwnProperty
(
'as'
)))
{
if
(
include
.
hasOwnProperty
(
'daoFactory'
)
&&
(
include
.
hasOwnProperty
(
'as'
)))
{
var
usesAlias
=
(
include
.
as
!==
include
.
daoFactory
.
tableName
)
var
usesAlias
=
(
include
.
as
!==
include
.
daoFactory
.
tableName
)
...
@@ -1403,7 +1423,9 @@ module.exports = (function() {
...
@@ -1403,7 +1423,9 @@ module.exports = (function() {
// If through, we create a pseudo child include, to ease our parsing later on
// If through, we create a pseudo child include, to ease our parsing later on
if
(
Object
(
include
.
association
.
through
)
===
include
.
association
.
through
)
{
if
(
Object
(
include
.
association
.
through
)
===
include
.
association
.
through
)
{
if
(
!
include
.
include
)
include
.
include
=
[]
if
(
!
include
.
include
)
{
include
.
include
=
[]
}
var
through
=
include
.
association
.
through
var
through
=
include
.
association
.
through
include
.
through
=
{
include
.
through
=
{
...
...
lib/dao.js
View file @
46f8b89
This diff is collapsed.
Click to expand it.
test/dao/values.test.js
View file @
46f8b89
...
@@ -208,7 +208,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
...
@@ -208,7 +208,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
var
product
=
Product
.
build
({
var
product
=
Product
.
build
({
price
:
10
price
:
10
})
})
expect
(
product
.
toJSON
()).
to
.
deep
.
equal
({
withTaxes
:
1250
,
price
:
1000
,
id
:
null
})
expect
(
product
.
toJSON
()).
to
.
deep
.
equal
({
withTaxes
:
1250
,
price
:
1000
})
})
})
it
(
'should work with save'
,
function
(
done
)
{
it
(
'should work with save'
,
function
(
done
)
{
...
...
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