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 cae4d8da
authored
Jan 28, 2016
by
Mick Hansen
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5312 from taylorhakes/order-field-alias
Fixed field alias in order clause
2 parents
3f6100e6
313b5006
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
267 additions
and
2 deletions
lib/utils.js
test/unit/utils.test.js
lib/utils.js
View file @
cae4d8d
...
@@ -115,6 +115,37 @@ var Utils = module.exports = {
...
@@ -115,6 +115,37 @@ var Utils = module.exports = {
options
.
where
=
Utils
.
mapWhereFieldNames
(
options
.
where
,
Model
);
options
.
where
=
Utils
.
mapWhereFieldNames
(
options
.
where
,
Model
);
}
}
if
(
Array
.
isArray
(
options
.
order
))
{
options
.
order
.
forEach
(
function
(
oGroup
)
{
var
OrderModel
,
attr
,
attrOffset
;
if
(
Array
.
isArray
(
oGroup
))
{
OrderModel
=
Model
;
// Check if we have ['attr', 'DESC'] or [Model, 'attr', 'DESC']
if
(
typeof
oGroup
[
oGroup
.
length
-
2
]
===
'string'
)
{
attrOffset
=
2
;
// Assume ['attr'], [Model, 'attr'] or [seq.fn('somefn', 1), 'DESC']
}
else
{
attrOffset
=
1
;
}
attr
=
oGroup
[
oGroup
.
length
-
attrOffset
];
if
(
oGroup
.
length
>
attrOffset
)
{
OrderModel
=
oGroup
[
oGroup
.
length
-
(
attrOffset
+
1
)];
if
(
OrderModel
.
model
)
{
OrderModel
=
OrderModel
.
model
;
}
}
if
(
OrderModel
.
rawAttributes
&&
OrderModel
.
rawAttributes
[
attr
]
&&
attr
!==
OrderModel
.
rawAttributes
[
attr
].
field
)
{
oGroup
[
oGroup
.
length
-
attrOffset
]
=
OrderModel
.
rawAttributes
[
attr
].
field
;
}
}
});
}
return
options
;
return
options
;
},
},
...
...
test/unit/utils.test.js
View file @
cae4d8d
...
@@ -186,6 +186,241 @@ suite(Support.getTestDialectTeaser('Utils'), function() {
...
@@ -186,6 +186,241 @@ suite(Support.getTestDialectTeaser('Utils'), function() {
}
}
});
});
});
});
test
(
'string field order'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
order
:
'firstName DESC'
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
,
field
:
'first_name'
}
}))).
to
.
eql
({
order
:
'firstName DESC'
});
});
test
(
'string in array order'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
order
:
[
'firstName DESC'
]
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
,
field
:
'first_name'
}
}))).
to
.
eql
({
order
:
[
'firstName DESC'
]
});
});
test
(
'single field alias order'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
order
:
[[
'firstName'
,
'DESC'
]]
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
,
field
:
'first_name'
}
}))).
to
.
eql
({
order
:
[[
'first_name'
,
'DESC'
]]
});
});
test
(
'multi field alias order'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
order
:
[[
'firstName'
,
'DESC'
],
[
'lastName'
,
'ASC'
]]
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
,
field
:
'first_name'
},
lastName
:
{
type
:
DataTypes
.
STRING
,
field
:
'last_name'
}
}))).
to
.
eql
({
order
:
[[
'first_name'
,
'DESC'
],
[
'last_name'
,
'ASC'
]]
});
});
test
(
'multi field alias no direction order'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
order
:
[[
'firstName'
],
[
'lastName'
]]
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
,
field
:
'first_name'
},
lastName
:
{
type
:
DataTypes
.
STRING
,
field
:
'last_name'
}
}))).
to
.
eql
({
order
:
[[
'first_name'
],
[
'last_name'
]]
});
});
test
(
'field alias to another field order'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
order
:
[[
'firstName'
,
'DESC'
]]
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
,
field
:
'lastName'
},
lastName
:
{
type
:
DataTypes
.
STRING
,
field
:
'firstName'
}
}))).
to
.
eql
({
order
:
[[
'lastName'
,
'DESC'
]]
});
});
test
(
'multi field no alias order'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
order
:
[[
'firstName'
,
'DESC'
],
[
'lastName'
,
'ASC'
]]
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
},
lastName
:
{
type
:
DataTypes
.
STRING
}
}))).
to
.
eql
({
order
:
[[
'firstName'
,
'DESC'
],
[
'lastName'
,
'ASC'
]]
});
});
test
(
'multi field alias sub model order'
,
function
()
{
var
Location
=
this
.
sequelize
.
define
(
'Location'
,
{
latLong
:
{
type
:
DataTypes
.
STRING
,
field
:
'lat_long'
}
});
var
Item
=
this
.
sequelize
.
define
(
'Item'
,
{
fontColor
:
{
type
:
DataTypes
.
STRING
,
field
:
'font_color'
}
});
expect
(
Utils
.
mapOptionFieldNames
({
order
:
[[
Item
,
Location
,
'latLong'
,
'DESC'
],
[
'lastName'
,
'ASC'
]]
},
this
.
sequelize
.
define
(
'User'
,
{
lastName
:
{
type
:
DataTypes
.
STRING
}
}))).
to
.
eql
({
order
:
[[
Item
,
Location
,
'lat_long'
,
'DESC'
],
[
'lastName'
,
'ASC'
]]
});
});
test
(
'multi field alias sub model no direction order'
,
function
()
{
var
Location
=
this
.
sequelize
.
define
(
'Location'
,
{
latLong
:
{
type
:
DataTypes
.
STRING
,
field
:
'lat_long'
}
});
var
Item
=
this
.
sequelize
.
define
(
'Item'
,
{
fontColor
:
{
type
:
DataTypes
.
STRING
,
field
:
'font_color'
}
});
expect
(
Utils
.
mapOptionFieldNames
({
order
:
[[
Item
,
Location
,
'latLong'
],
[
'lastName'
,
'ASC'
]]
},
this
.
sequelize
.
define
(
'User'
,
{
lastName
:
{
type
:
DataTypes
.
STRING
}
}))).
to
.
eql
({
order
:
[[
Item
,
Location
,
'lat_long'
],
[
'lastName'
,
'ASC'
]]
});
});
test
(
'function order'
,
function
()
{
var
fn
=
this
.
sequelize
.
fn
(
'otherfn'
,
123
);
expect
(
Utils
.
mapOptionFieldNames
({
order
:
[[
fn
,
'ASC'
]]
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
}
}))).
to
.
eql
({
order
:
[[
fn
,
'ASC'
]]
});
});
test
(
'function no direction order'
,
function
()
{
var
fn
=
this
.
sequelize
.
fn
(
'otherfn'
,
123
);
expect
(
Utils
.
mapOptionFieldNames
({
order
:
[[
fn
]]
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
}
}))).
to
.
eql
({
order
:
[[
fn
]]
});
});
test
(
'string no direction order'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
order
:
[[
'firstName'
]]
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
,
field
:
'first_name'
}
}))).
to
.
eql
({
order
:
[[
'first_name'
]]
});
});
test
(
'model alias order'
,
function
()
{
var
Item
=
this
.
sequelize
.
define
(
'Item'
,
{
fontColor
:
{
type
:
DataTypes
.
STRING
,
field
:
'font_color'
}
});
expect
(
Utils
.
mapOptionFieldNames
({
order
:
[[{
model
:
Item
,
as
:
'another'
},
'fontColor'
,
'ASC'
]]
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
},
lastName
:
{
type
:
DataTypes
.
STRING
}
}))).
to
.
eql
({
order
:
[[{
model
:
Item
,
as
:
'another'
},
'font_color'
,
'ASC'
]]
});
});
test
(
'model alias no direction order'
,
function
()
{
var
Item
=
this
.
sequelize
.
define
(
'Item'
,
{
fontColor
:
{
type
:
DataTypes
.
STRING
,
field
:
'font_color'
}
});
expect
(
Utils
.
mapOptionFieldNames
({
order
:
[[{
model
:
Item
,
as
:
'another'
},
'fontColor'
]]
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
}
}))).
to
.
eql
({
order
:
[[{
model
:
Item
,
as
:
'another'
},
'font_color'
]]
});
});
test
(
'model alias wrong field order'
,
function
()
{
var
Item
=
this
.
sequelize
.
define
(
'Item'
,
{
fontColor
:
{
type
:
DataTypes
.
STRING
,
field
:
'font_color'
}
});
expect
(
Utils
.
mapOptionFieldNames
({
order
:
[[{
model
:
Item
,
as
:
'another'
},
'firstName'
,
'ASC'
]]
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
}
}))).
to
.
eql
({
order
:
[[{
model
:
Item
,
as
:
'another'
},
'firstName'
,
'ASC'
]]
});
});
});
});
suite
(
'stack'
,
function
()
{
suite
(
'stack'
,
function
()
{
...
@@ -228,4 +463,4 @@ suite(Support.getTestDialectTeaser('Utils'), function() {
...
@@ -228,4 +463,4 @@ suite(Support.getTestDialectTeaser('Utils'), function() {
});
});
});
});
});
});
});
});
\ 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