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 f0f55ae0
authored
Apr 26, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pluralize tablenames, singularize identifier fields
1 parent
0ca2d91c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
24 additions
and
15 deletions
lib/sequelize/associations/belongs-to.js
lib/sequelize/associations/has-many.js
lib/sequelize/associations/has-one.js
lib/sequelize/model-definition.js
lib/sequelize/utils.js
lib/sequelize/associations/belongs-to.js
View file @
f0f55ae
...
...
@@ -11,7 +11,7 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) {
BelongsTo
.
prototype
.
injectAttributes
=
function
()
{
var
newAttributes
=
{}
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
this
.
target
.
tableName
+
"Id"
,
this
.
source
.
options
.
underscored
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
target
.
tableName
)
+
"Id"
,
this
.
source
.
options
.
underscored
)
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
Utils
.
_
.
extend
(
this
.
source
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
...
...
@@ -20,7 +20,7 @@ BelongsTo.prototype.injectAttributes = function() {
BelongsTo
.
prototype
.
injectGetter
=
function
(
obj
)
{
var
self
=
this
,
accessor
=
Utils
.
_
.
camelize
(
'get_'
+
(
this
.
options
.
as
||
this
.
target
.
tableName
))
,
accessor
=
Utils
.
_
.
camelize
(
'get_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
)
))
obj
[
accessor
]
=
function
()
{
var
id
=
obj
[
self
.
identifier
]
...
...
@@ -32,7 +32,7 @@ BelongsTo.prototype.injectGetter = function(obj) {
BelongsTo
.
prototype
.
injectSetter
=
function
(
obj
)
{
var
self
=
this
,
accessor
=
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
this
.
target
.
tableName
))
,
accessor
=
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
)
))
obj
[
accessor
]
=
function
(
associatedObject
)
{
obj
[
self
.
identifier
]
=
associatedObject
?
associatedObject
.
id
:
null
...
...
lib/sequelize/associations/has-many.js
View file @
f0f55ae
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
var
HasMany
=
module
.
exports
=
function
(
srcModel
,
targetModel
,
options
)
{
this
.
source
=
srcModel
this
.
target
=
targetModel
this
.
options
=
options
this
.
accessors
=
{
get
:
Utils
.
_
.
camelize
(
'get_'
+
(
this
.
options
.
as
||
this
.
target
.
tableName
+
's'
)),
set
:
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
this
.
target
.
tableName
+
's'
))
var
as
=
(
this
.
options
.
as
||
Utils
.
pluralize
(
this
.
target
.
tableName
))
this
.
accessors
=
{
get
:
Utils
.
_
.
camelize
(
'get_'
+
as
),
set
:
Utils
.
_
.
camelize
(
'set_'
+
as
)
}
}
// the id is in the target table
// or in an extra table which connects two tables
HasMany
.
prototype
.
injectAttributes
=
function
()
{
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
this
.
source
.
tableName
+
"Id"
,
this
.
options
.
underscored
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
)
+
"Id"
,
this
.
options
.
underscored
)
if
(
this
.
target
.
associations
.
hasOwnProperty
(
this
.
source
.
tableName
))
{
// remove the obsolete association identifier from the source
...
...
lib/sequelize/associations/has-one.js
View file @
f0f55ae
...
...
@@ -6,8 +6,8 @@ var HasOne = module.exports = function(srcModel, targetModel, options) {
this
.
target
=
targetModel
this
.
options
=
options
this
.
accessors
=
{
get
:
Utils
.
_
.
camelize
(
'get_'
+
(
this
.
options
.
as
||
this
.
target
.
tableName
)),
set
:
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
this
.
target
.
tableName
))
get
:
Utils
.
_
.
camelize
(
'get_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
)
)),
set
:
Utils
.
_
.
camelize
(
'set_'
+
(
this
.
options
.
as
||
Utils
.
singularize
(
this
.
target
.
tableName
)
))
}
}
...
...
@@ -15,7 +15,7 @@ var HasOne = module.exports = function(srcModel, targetModel, options) {
HasOne
.
prototype
.
injectAttributes
=
function
()
{
var
newAttributes
=
{}
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
this
.
source
.
tableName
+
"Id"
,
this
.
options
.
underscored
)
this
.
identifier
=
this
.
options
.
foreignKey
||
Utils
.
_
.
underscoredIf
(
Utils
.
singularize
(
this
.
source
.
tableName
)
+
"Id"
,
this
.
options
.
underscored
)
newAttributes
[
this
.
identifier
]
=
{
type
:
DataTypes
.
INTEGER
}
Utils
.
_
.
extend
(
this
.
target
.
attributes
,
Utils
.
simplifyAttributes
(
newAttributes
))
...
...
lib/sequelize/model-definition.js
View file @
f0f55ae
...
...
@@ -9,7 +9,7 @@ var ModelDefinition = module.exports = function(name, attributes, options) {
this
.
options
=
options
||
{}
this
.
options
.
timestamps
=
this
.
options
.
hasOwnProperty
(
'timestamps'
)
?
this
.
options
.
timestamps
:
true
this
.
name
=
name
this
.
tableName
=
name
this
.
tableName
=
Utils
.
pluralize
(
name
)
this
.
attributes
=
Utils
.
simplifyAttributes
(
attributes
)
this
.
modelManager
=
null
// defined by model-manager during addModel
this
.
associations
=
{}
...
...
lib/sequelize/utils.js
View file @
f0f55ae
...
...
@@ -125,6 +125,12 @@ var Utils = module.exports = {
,
chars2
=
Utils
.
_
.
chars
(
tableName2
.
toLowerCase
())
return
(
chars1
[
0
]
<
chars2
[
0
])
?
(
tableName1
+
tableName2
)
:
(
tableName2
+
tableName1
)
},
singularize
:
function
(
s
)
{
return
Utils
.
Lingo
.
en
.
isSingular
(
s
)
?
s
:
Utils
.
Lingo
.
en
.
singularize
(
s
)
},
pluralize
:
function
(
s
)
{
return
Utils
.
Lingo
.
en
.
isPlural
(
s
)
?
s
:
Utils
.
Lingo
.
en
.
pluralize
(
s
)
}
}
...
...
@@ -140,4 +146,5 @@ CustomEventEmitter.prototype.run = function() {
return
this
}
Utils
.
QueryChainer
=
require
(
"./query-chainer"
)
\ No newline at end of file
Utils
.
QueryChainer
=
require
(
"./query-chainer"
)
Utils
.
Lingo
=
require
(
"lingo"
)
\ No newline at end of file
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