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 b8a548a5
authored
Sep 10, 2010
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
factory stuff for one to one assocs
1 parent
670ebd01
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
34 deletions
lib/sequelize/Factory.js
lib/sequelize/SequelizeTable.js
lib/sequelize/Factory.js
View file @
b8a548a
...
@@ -11,7 +11,43 @@ module.exports.Factory = function(Sequelize, sequelize) {
...
@@ -11,7 +11,43 @@ module.exports.Factory = function(Sequelize, sequelize) {
table1
.
prototype
[
setterName
]
=
Factory
.
createOneToManySetter
(
table1
,
assocName
)
table1
.
prototype
[
setterName
]
=
Factory
.
createOneToManySetter
(
table1
,
assocName
)
table1
.
prototype
[
getterName
]
=
Factory
.
createOneToManyGetter
(
table1
,
table2
,
assocName
)
table1
.
prototype
[
getterName
]
=
Factory
.
createOneToManyGetter
(
table1
,
table2
,
assocName
)
return
table1
},
addOneToOneMethods
:
function
(
table1
,
table2
,
assocName
,
backAssocName
)
{
var
setterName
=
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
backAssocName
||
assocName
),
getterName
=
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
backAssocName
||
assocName
)
// getter
table1
.
prototype
[
getterName
]
=
function
(
callback
)
{
var
whereConditions
=
{}
whereConditions
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
assocName
)]
=
this
.
id
table2
.
find
(
whereConditions
,
callback
)
}
// setter
table1
.
prototype
[
setterName
]
=
function
(
object
,
callback
)
{
var
self
=
this
this
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
backAssocName
)](
function
(
currentAssociation
)
{
var
attr
=
{}
if
(
currentAssociation
==
null
)
{
attr
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
assocName
)]
=
self
.
id
object
.
updateAttributes
(
attr
,
callback
)
}
else
{
if
(
object
.
id
==
currentAssociation
.
id
)
callback
(
currentAssociation
)
else
{
// first update the currently associated item to have no association any more
attr
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
assocName
)]
=
null
currentAssociation
.
updateAttributes
(
attr
,
function
()
{
// now update the object itself to set the new association
attr
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
assocName
)]
=
self
.
id
object
.
updateAttributes
(
attr
,
callback
)
})
}
}
})
}
},
},
createManyToManyGetter
:
function
(
table1
,
table2
,
assocName
,
backAssocName
)
{
createManyToManyGetter
:
function
(
table1
,
table2
,
assocName
,
backAssocName
)
{
...
...
lib/sequelize/SequelizeTable.js
View file @
b8a548a
...
@@ -151,35 +151,11 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
...
@@ -151,35 +151,11 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
},
},
hasOne
:
function
(
assocName
,
_table
)
{
hasOne
:
function
(
assocName
,
_table
)
{
var
association
=
{
name
:
assocName
,
table
:
_table
,
type
:
'hasOne'
}
var
Factory
=
new
require
(
"./Factory"
).
Factory
(
Sequelize
,
sequelize
),
association
=
{
name
:
assocName
,
table
:
_table
,
type
:
'hasOne'
}
table
.
associations
.
push
(
association
)
table
.
associations
.
push
(
association
)
Factory
.
addOneToOneMethods
(
table
,
_table
,
assocName
)
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
assocName
)]
=
function
(
callback
)
{
var
whereConditions
=
{}
whereConditions
[
table
.
identifier
]
=
this
.
id
_table
.
find
(
whereConditions
,
callback
)
}
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
assocName
)]
=
function
(
object
,
callback
)
{
var
self
=
this
this
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
assocName
)](
function
(
currentAssociation
)
{
var
attr
=
{}
if
(
currentAssociation
==
null
)
{
attr
[
table
.
identifier
]
=
self
.
id
object
.
updateAttributes
(
attr
,
callback
)
}
else
{
if
(
object
.
id
==
currentAssociation
.
id
)
callback
()
else
{
attr
[
table
.
identifier
]
=
null
currentAssociation
.
updateAttributes
(
attr
,
function
()
{
attr
[
table
.
identifier
]
=
self
.
id
object
.
updateAttributes
(
attr
,
callback
)
})
}
}
})
}
return
association
return
association
},
},
...
@@ -190,9 +166,15 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
...
@@ -190,9 +166,15 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
// start - overwrite the association of the before defined hasOne or hasMany relation, to fit the belongsTo foreign keys
// start - overwrite the association of the before defined hasOne or hasMany relation, to fit the belongsTo foreign keys
var
Factory
=
new
require
(
"./Factory"
).
Factory
(
Sequelize
,
sequelize
)
var
Factory
=
new
require
(
"./Factory"
).
Factory
(
Sequelize
,
sequelize
)
delete
_table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
backAssociation
.
name
)]
if
(
backAssociation
.
type
==
'hasMany'
)
{
delete
_table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
backAssociation
.
name
)]
delete
_table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
backAssociation
.
name
)]
Factory
.
addOneToManyMethods
(
_table
,
table
,
assocName
,
backAssociation
.
name
)
delete
_table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
backAssociation
.
name
)]
Factory
.
addOneToManyMethods
(
_table
,
table
,
assocName
,
backAssociation
.
name
)
}
else
{
delete
_table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
backAssociation
.
name
)]
delete
_table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
backAssociation
.
name
)]
Factory
.
addOneToOneMethods
(
_table
,
table
,
assocName
,
backAssociation
.
name
)
}
// end - overwrite the association of the before defined hasOne or hasMany relation, to fit the belongsTo foreign keys
// end - overwrite the association of the before defined hasOne or hasMany relation, to fit the belongsTo foreign keys
table
.
associations
.
push
({
name
:
assocName
,
table
:
_table
,
type
:
'belongsTo'
})
table
.
associations
.
push
({
name
:
assocName
,
table
:
_table
,
type
:
'belongsTo'
})
...
@@ -200,7 +182,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
...
@@ -200,7 +182,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
// getter
// getter
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
assocName
)]
=
function
(
callback
)
{
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
assocName
)]
=
function
(
callback
)
{
var
identifier
=
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
assocName
)
var
identifier
=
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
assocName
)
if
((
this
[
identifier
]
==
null
)
||
(
isNaN
(
this
[
identifier
])))
callback
([])
if
((
this
[
identifier
]
==
null
)
||
(
isNaN
(
this
[
identifier
])))
callback
([])
else
_table
.
find
(
this
[
identifier
],
callback
)
else
_table
.
find
(
this
[
identifier
],
callback
)
}
}
...
@@ -211,7 +193,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
...
@@ -211,7 +193,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
attr
=
{}
attr
=
{}
attr
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
assocName
)]
=
object
.
id
attr
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
assocName
)]
=
object
.
id
this
.
updateAttributes
(
attr
,
function
()
{
this
.
updateAttributes
(
attr
,
function
()
{
self
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
assocName
)](
callback
)
self
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
assocName
)](
callback
)
})
})
...
...
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