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() {
this
.
primaryKeys
=
{}
self
.
options
.
uniqueKeys
=
{}
Utils
.
_
.
each
(
this
.
rawAttributes
,
function
(
columnValues
,
columnName
)
{
if
(
columnValues
.
hasOwnProperty
(
'unique'
)
&&
columnValues
.
unique
!==
true
&&
columnValues
.
unique
!==
false
)
{
var
idxName
=
columnValues
.
unique
if
(
typeof
columnValues
.
unique
===
"object"
)
{
idxName
=
columnValues
.
unique
.
name
// Setup names of timestamp attributes
this
.
_timestampAttributes
=
{}
if
(
this
.
options
.
timestamps
)
{
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
)
}
}
// 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
].
fields
.
push
(
columnNam
e
)
self
.
options
.
uniqueKeys
[
idxName
].
msg
=
self
.
options
.
uniqueKeys
[
idxName
].
msg
||
columnValue
s
.
unique
.
msg
||
null
self
.
options
.
uniqueKeys
[
idxName
].
fields
.
push
(
attribut
e
)
self
.
options
.
uniqueKeys
[
idxName
].
msg
=
self
.
options
.
uniqueKeys
[
idxName
].
msg
||
option
s
.
unique
.
msg
||
null
}
})
Utils
.
_
.
each
(
this
.
attributes
,
function
(
dataTypeString
,
attributeName
)
{
if
(
dataTypeString
.
indexOf
(
'PRIMARY KEY'
)
!==
-
1
)
{
self
.
primaryKeys
[
attributeName
]
=
dataTypeString
if
(
options
.
primaryKey
===
true
)
{
self
.
primaryKeys
[
attribute
]
=
self
.
attributes
[
attribute
]
}
})
// 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
.
primaryKeyAttribute
=
this
.
primaryKeyAttributes
[
0
]
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"
)
{
Utils
.
injectScope
.
call
(
this
,
this
.
options
.
defaultScope
)
}
// DAO prototype
// WTF ... ?
this
.
DAO
=
function
()
{
DAO
.
apply
(
this
,
arguments
);
}
Util
.
inherits
(
this
.
DAO
,
DAO
);
this
.
_timestampAttributes
=
{}
if
(
this
.
options
.
timestamps
)
{
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
.
_readOnlyAttributes
=
Object
.
keys
(
this
.
_timestampAttributes
)
this
.
DAO
.
prototype
.
_readOnlyAttributes
=
this
.
_readOnlyAttributes
// To be removed
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
.
DAO
.
prototype
.
_isReadOnlyAttribute
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
DAO
.
prototype
.
_hasReadOnlyAttributes
&&
self
.
DAO
.
prototype
.
_readOnlyAttributes
.
indexOf
(
key
)
!==
-
1
this
.
_isReadOnlyAttribute
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
_hasReadOnlyAttributes
&&
self
.
_readOnlyAttributes
.
indexOf
(
key
)
!==
-
1
})
this
.
DAO
.
prototype
.
_isReadOnlyAttribute
=
this
.
_isReadOnlyAttribute
// To be removed
addDefaultAttributes
.
call
(
this
)
addOptionalClassMethods
.
call
(
this
)
findAutoIncrementField
.
call
(
this
)
this
.
DAO
.
prototype
.
_hasPrimaryKeys
=
this
.
_hasPrimaryKeys
// To be removed
this
.
DAO
.
prototype
.
_isPrimaryKey
=
this
.
_isPrimaryKey
// To be removed
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
)
{
Utils
.
_
.
each
(
this
.
options
.
instanceMethods
,
function
(
fct
,
name
)
{
self
.
DAO
.
prototype
[
name
]
=
fct
...
...
@@ -1273,7 +1282,11 @@ module.exports = (function() {
var
addDefaultAttributes
=
function
()
{
var
self
=
this
,
tail
=
{}
,
head
=
{
,
head
=
{}
// Add id if no primary key was manually added to definition
if
(
!
Object
.
keys
(
this
.
primaryKeys
).
length
)
{
head
=
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
...
...
@@ -1282,9 +1295,6 @@ module.exports = (function() {
_autoGenerated
:
true
}
}
if
(
this
.
hasPrimaryKeys
)
{
head
=
{}
}
if
(
this
.
_timestampAttributes
.
createdAt
)
{
...
...
@@ -1313,6 +1323,10 @@ module.exports = (function() {
self
.
rawAttributes
[
attr
]
=
value
}
})
if
(
!
Object
.
keys
(
this
.
primaryKeys
).
length
)
{
self
.
primaryKeys
[
'id'
]
=
self
.
attributes
[
'id'
]
}
}
var
findAutoIncrementField
=
function
()
{
...
...
@@ -1341,8 +1355,12 @@ module.exports = (function() {
options
.
includeNames
.
push
(
include
.
as
)
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
.
isSingleAssociation
||
include
.
hasSingleAssociation
)
options
.
hasSingleAssociation
=
true
if
(
include
.
association
.
isMultiAssociation
||
include
.
hasMultiAssociation
)
{
options
.
hasMultiAssociation
=
true
}
if
(
include
.
association
.
isSingleAssociation
||
include
.
hasSingleAssociation
)
{
options
.
hasSingleAssociation
=
true
}
options
.
hasIncludeWhere
=
options
.
hasIncludeWhere
||
include
.
hasIncludeWhere
||
!!
include
.
where
options
.
hasIncludeRequired
=
options
.
hasIncludeRequired
||
include
.
hasIncludeRequired
||
!!
include
.
required
...
...
@@ -1374,8 +1392,8 @@ module.exports = (function() {
var
primaryKeys
;
if
(
include
.
daoFactory
.
hasPrimaryKeys
)
{
primaryKeys
=
[]
for
(
var
field
_name
in
include
.
daoFactory
.
primaryKeys
)
{
primaryKeys
.
push
(
field
_name
)
for
(
var
field
in
include
.
daoFactory
.
primaryKeys
)
{
primaryKeys
.
push
(
field
)
}
}
else
{
primaryKeys
=
[
'id'
]
...
...
@@ -1386,7 +1404,9 @@ module.exports = (function() {
}
// 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'
)))
{
var
usesAlias
=
(
include
.
as
!==
include
.
daoFactory
.
tableName
)
...
...
@@ -1403,7 +1423,9 @@ module.exports = (function() {
// If through, we create a pseudo child include, to ease our parsing later on
if
(
Object
(
include
.
association
.
through
)
===
include
.
association
.
through
)
{
if
(
!
include
.
include
)
include
.
include
=
[]
if
(
!
include
.
include
)
{
include
.
include
=
[]
}
var
through
=
include
.
association
.
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 () {
var
product
=
Product
.
build
({
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
)
{
...
...
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