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 151a23f6
authored
May 05, 2013
by
Martin Aspeli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Factor FK constraint logic out into a helper function
1 parent
1df90277
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
31 additions
and
36 deletions
lib/associations/belongs-to.js
lib/associations/has-many.js
lib/associations/has-one.js
lib/associations/helpers.js
lib/associations/belongs-to.js
View file @
151a23f
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
,
Helpers
=
require
(
'./helpers'
)
module
.
exports
=
(
function
()
{
var
BelongsTo
=
function
(
srcDAO
,
targetDAO
,
options
)
{
...
...
@@ -24,18 +25,7 @@ module.exports = (function() {
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
target
.
tableName
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
if
(
this
.
options
.
foreignKeyConstraint
||
this
.
options
.
onDelete
||
this
.
options
.
onUpdate
)
{
var
primaryKeys
=
Utils
.
_
.
filter
(
Utils
.
_
.
keys
(
this
.
target
.
rawAttributes
),
function
(
key
)
{
return
this
.
target
.
rawAttributes
[
key
].
primaryKey
},
this
)
if
(
primaryKeys
.
length
==
1
)
{
// composite keys not supported with this approach
newAttributes
[
this
.
identifier
].
references
=
this
.
target
.
tableName
,
newAttributes
[
this
.
identifier
].
referencesKey
=
primaryKeys
[
0
]
newAttributes
[
this
.
identifier
].
onDelete
=
this
.
options
.
onDelete
,
newAttributes
[
this
.
identifier
].
onUpdate
=
this
.
options
.
onUpdate
}
}
Helpers
.
addForeignKeyConstraints
(
newAttributes
[
this
.
identifier
],
this
.
target
,
this
.
source
,
this
.
options
)
Utils
.
_
.
defaults
(
this
.
source
.
rawAttributes
,
newAttributes
)
// Sync attributes to DAO proto each time a new assoc is added
...
...
lib/associations/has-many.js
View file @
151a23f
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
,
Helpers
=
require
(
'./helpers'
)
var
HasManySingleLinked
=
require
(
"./has-many-single-linked"
)
,
HasManyMultiLinked
=
require
(
"./has-many-double-linked"
)
...
...
@@ -65,18 +66,7 @@ module.exports = (function() {
}
else
{
var
newAttributes
=
{}
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
if
(
this
.
options
.
foreignKeyConstraint
||
this
.
options
.
onDelete
||
this
.
options
.
onUpdate
)
{
var
primaryKeys
=
Utils
.
_
.
filter
(
Utils
.
_
.
keys
(
this
.
source
.
rawAttributes
),
function
(
key
)
{
return
this
.
source
.
rawAttributes
[
key
].
primaryKey
},
this
)
if
(
primaryKeys
.
length
==
1
)
{
// composite keys not supported with this approach
newAttributes
[
this
.
identifier
].
references
=
this
.
source
.
tableName
newAttributes
[
this
.
identifier
].
referencesKey
=
primaryKeys
[
0
]
newAttributes
[
this
.
identifier
].
onDelete
=
this
.
options
.
onDelete
newAttributes
[
this
.
identifier
].
onUpdate
=
this
.
options
.
onUpdate
}
}
Helpers
.
addForeignKeyConstraints
(
newAttributes
[
this
.
identifier
],
this
.
source
,
this
.
target
,
this
.
options
)
Utils
.
_
.
defaults
(
this
.
target
.
rawAttributes
,
newAttributes
)
}
...
...
lib/associations/has-one.js
View file @
151a23f
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
,
Helpers
=
require
(
"./helpers"
)
module
.
exports
=
(
function
()
{
var
HasOne
=
function
(
srcDAO
,
targetDAO
,
options
)
{
...
...
@@ -29,18 +30,7 @@ module.exports = (function() {
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
)
+
"Id"
,
this
.
options
.
underscored
)
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
if
(
this
.
options
.
foreignKeyConstraint
||
this
.
options
.
onDelete
||
this
.
options
.
onUpdate
)
{
var
primaryKeys
=
Utils
.
_
.
filter
(
Utils
.
_
.
keys
(
this
.
source
.
rawAttributes
),
function
(
key
)
{
return
this
.
source
.
rawAttributes
[
key
].
primaryKey
},
this
)
if
(
primaryKeys
.
length
==
1
)
{
// composite keys not supported with this approach
newAttributes
[
this
.
identifier
].
references
=
this
.
source
.
tableName
,
newAttributes
[
this
.
identifier
].
referencesKey
=
primaryKeys
[
0
]
newAttributes
[
this
.
identifier
].
onDelete
=
this
.
options
.
onDelete
,
newAttributes
[
this
.
identifier
].
onUpdate
=
this
.
options
.
onUpdate
}
}
Helpers
.
addForeignKeyConstraints
(
newAttributes
[
this
.
identifier
],
this
.
source
,
this
.
target
,
this
.
options
)
Utils
.
_
.
defaults
(
this
.
target
.
rawAttributes
,
newAttributes
)
// Sync attributes to DAO proto each time a new assoc is added
...
...
lib/associations/helpers.js
0 → 100644
View file @
151a23f
var
Utils
=
require
(
"./../utils"
)
module
.
exports
=
{
addForeignKeyConstraints
:
function
(
newAttribute
,
source
,
target
,
options
)
{
// FK constraints are opt-in: users must either rset `foreignKeyConstraints`
// on the association, or request an `onDelete` or `onUpdate` behaviour
if
(
options
.
foreignKeyConstraint
||
options
.
onDelete
||
options
.
onUpdate
)
{
// Find primary keys: composite keys not supported with this approach
var
primaryKeys
=
Utils
.
_
.
filter
(
Utils
.
_
.
keys
(
source
.
rawAttributes
),
function
(
key
)
{
return
source
.
rawAttributes
[
key
].
primaryKey
})
if
(
primaryKeys
.
length
==
1
)
{
newAttribute
.
references
=
source
.
tableName
,
newAttribute
.
referencesKey
=
primaryKeys
[
0
]
newAttribute
.
onDelete
=
options
.
onDelete
,
newAttribute
.
onUpdate
=
options
.
onUpdate
}
}
}
}
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