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 313b5006
authored
Jan 27, 2016
by
taylorhakes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed field alias in order clause
1 parent
ae89e846
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
266 additions
and
0 deletions
lib/utils.js
test/unit/utils.test.js
lib/utils.js
View file @
313b500
...
...
@@ -115,6 +115,37 @@ var Utils = module.exports = {
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
;
},
...
...
test/unit/utils.test.js
View file @
313b500
...
...
@@ -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
()
{
...
...
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