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 35780624
authored
Mar 19, 2019
by
Simon Schick
Committed by
Sushant
Mar 19, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: optimize with spread operator (#10570)
1 parent
493d8e32
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
43 additions
and
32 deletions
lib/deferrable.js
lib/dialects/postgres/connection-manager.js
lib/model.js
lib/sequelize.js
lib/utils.js
test/unit/dialects/mariadb/query-generator.test.js
test/unit/dialects/mssql/query-generator.test.js
test/unit/dialects/mysql/query-generator.test.js
test/unit/dialects/postgres/query-generator.test.js
test/unit/dialects/sqlite/query-generator.test.js
lib/deferrable.js
View file @
3578062
...
...
@@ -3,13 +3,12 @@
const
{
classToInvokable
}
=
require
(
'./utils'
);
class
ABSTRACT
{
static
toString
()
{
const
instance
=
new
this
();
return
instance
.
toString
.
apply
(
instance
,
arguments
);
static
toString
(...
args
)
{
return
new
this
().
toString
(...
args
);
}
toString
()
{
return
this
.
toSql
.
apply
(
this
,
argument
s
);
toString
(
...
args
)
{
return
this
.
toSql
(...
arg
s
);
}
toSql
()
{
...
...
lib/dialects/postgres/connection-manager.js
View file @
3578062
...
...
@@ -77,10 +77,10 @@ class ConnectionManager extends AbstractConnectionManager {
this
.
oidParserMap
=
new
Map
();
}
getTypeParser
(
oid
)
{
getTypeParser
(
oid
,
...
args
)
{
if
(
this
.
oidParserMap
.
get
(
oid
))
return
this
.
oidParserMap
.
get
(
oid
);
return
this
.
lib
.
types
.
getTypeParser
.
apply
(
undefined
,
argument
s
);
return
this
.
lib
.
types
.
getTypeParser
(
oid
,
...
arg
s
);
}
connect
(
config
)
{
...
...
lib/model.js
View file @
3578062
...
...
@@ -19,6 +19,14 @@ const Hooks = require('./hooks');
const
associationsMixin
=
require
(
'./associations/mixin'
);
const
Op
=
require
(
'./operators'
);
// This list will quickly become dated, but failing to maintain this list just means
// we won't throw a warning when we should. At least most common cases will forever be covered
// so we stop throwing erroneous warnings when we shouldn't.
const
validQueryKeywords
=
new
Set
([
'where'
,
'attributes'
,
'paranoid'
,
'include'
,
'order'
,
'limit'
,
'offset'
,
'transaction'
,
'lock'
,
'raw'
,
'logging'
,
'benchmark'
,
'having'
,
'searchPath'
,
'rejectOnEmpty'
,
'plain'
,
'scope'
,
'group'
,
'through'
,
'defaults'
,
'distinct'
,
'primary'
,
'exception'
,
'type'
,
'hooks'
,
'force'
,
'name'
]);
/**
* A Model represents a table in the database. Instances of this class represent a database row.
*
...
...
@@ -1763,15 +1771,7 @@ class Model {
return
;
}
// This list will quickly become dated, but failing to maintain this list just means
// we won't throw a warning when we should. At least most common cases will forever be covered
// so we stop throwing erroneous warnings when we shouldn't.
const
validQueryKeywords
=
[
'where'
,
'attributes'
,
'paranoid'
,
'include'
,
'order'
,
'limit'
,
'offset'
,
'transaction'
,
'lock'
,
'raw'
,
'logging'
,
'benchmark'
,
'having'
,
'searchPath'
,
'rejectOnEmpty'
,
'plain'
,
'scope'
,
'group'
,
'through'
,
'defaults'
,
'distinct'
,
'primary'
,
'exception'
,
'type'
,
'hooks'
,
'force'
,
'name'
];
const
unrecognizedOptions
=
_
.
difference
(
Object
.
keys
(
options
),
validQueryKeywords
);
const
unrecognizedOptions
=
Object
.
keys
(
options
).
filter
(
k
=>
!
validQueryKeywords
.
has
(
k
));
const
unexpectedModelAttributes
=
_
.
intersection
(
unrecognizedOptions
,
validColumnNames
);
if
(
!
options
.
where
&&
unexpectedModelAttributes
.
length
>
0
)
{
logger
.
warn
(
`Model attributes (
${
unexpectedModelAttributes
.
join
(
', '
)}
) passed into finder method options of model
${
this
.
name
}
, but the options.where object is empty. Did you forget to use options.where?`
);
...
...
@@ -2320,13 +2320,13 @@ class Model {
.
filter
(
name
=>
this
.
rawAttributes
[
name
])
.
map
(
name
=>
this
.
rawAttributes
[
name
].
field
||
name
);
if
(
defaultFields
)
{
if
(
!
_
.
intersection
(
Object
.
keys
(
err
.
fields
),
whereFields
).
length
&&
_
.
intersection
(
Object
.
keys
(
err
.
fields
),
defaultFields
).
length
)
{
throw
err
;
}
const
errFieldKeys
=
Object
.
keys
(
err
.
fields
);
const
errFieldsWhereIntersects
=
Utils
.
intersects
(
errFieldKeys
,
whereFields
);
if
(
defaultFields
&&
!
errFieldsWhereIntersects
&&
Utils
.
intersects
(
errFieldKeys
,
defaultFields
))
{
throw
err
;
}
if
(
_
.
intersection
(
Object
.
keys
(
err
.
fields
),
whereFields
).
length
)
{
if
(
errFieldsWhereIntersects
)
{
_
.
each
(
err
.
fields
,
(
value
,
key
)
=>
{
const
name
=
this
.
fieldRawAttributesMap
[
key
].
fieldName
;
if
(
value
.
toString
()
!==
options
.
where
[
name
].
toString
())
{
...
...
@@ -3795,7 +3795,7 @@ class Model {
args
=
[
this
,
this
.
constructor
.
getTableName
(
options
),
values
,
where
,
options
];
}
return
this
.
constructor
.
QueryInterface
[
query
]
.
apply
(
this
.
constructor
.
QueryInterface
,
args
)
return
this
.
constructor
.
QueryInterface
[
query
]
(...
args
)
.
then
(([
result
,
rowsUpdated
])
=>
{
if
(
versionAttr
)
{
// Check to see that a row was updated, otherwise it's an optimistic locking error.
...
...
@@ -3978,7 +3978,7 @@ class Model {
this
.
set
(
values
,
setOptions
);
// Now we need to figure out which fields were actually affected by the setter.
const
sideEffects
=
_
.
without
.
apply
(
this
,
[
this
.
changed
()
||
[]].
concat
(
changedBefore
)
);
const
sideEffects
=
_
.
without
(
this
.
changed
(),
...
changedBefore
);
const
fields
=
_
.
union
(
Object
.
keys
(
values
),
sideEffects
);
if
(
!
options
.
fields
)
{
...
...
lib/sequelize.js
View file @
3578062
...
...
@@ -1115,7 +1115,7 @@ class Sequelize {
args
=
[
`
${
args
[
0
]}
Elapsed time:
${
args
[
1
]}
ms`
];
}
options
.
logging
.
apply
(
null
,
args
);
options
.
logging
(...
args
);
}
}
...
...
lib/utils.js
View file @
3578062
...
...
@@ -7,7 +7,7 @@ const uuidv1 = require('uuid/v1');
const
uuidv4
=
require
(
'uuid/v4'
);
const
Promise
=
require
(
'./promise'
);
const
operators
=
require
(
'./operators'
);
const
operators
Array
=
_
.
values
(
operators
);
const
operators
Set
=
new
Set
(
_
.
values
(
operators
)
);
let
inflection
=
require
(
'inflection'
);
...
...
@@ -492,7 +492,7 @@ exports.Where = Where;
* @private
*/
function
getOperators
(
obj
)
{
return
_
.
intersection
(
Object
.
getOwnPropertySymbols
(
obj
||
{}),
operatorsArray
);
return
Object
.
getOwnPropertySymbols
(
obj
).
filter
(
s
=>
operatorsSet
.
has
(
s
)
);
}
exports
.
getOperators
=
getOperators
;
...
...
@@ -528,7 +528,7 @@ exports.getComplexSize = getComplexSize;
* @private
*/
function
isWhereEmpty
(
obj
)
{
return
_
.
isEmpty
(
obj
)
&&
getOperators
(
obj
).
length
===
0
;
return
!!
obj
&&
_
.
isEmpty
(
obj
)
&&
getOperators
(
obj
).
length
===
0
;
}
exports
.
isWhereEmpty
=
isWhereEmpty
;
...
...
@@ -622,3 +622,15 @@ function nameIndex(index, tableName) {
return
index
;
}
exports
.
nameIndex
=
nameIndex
;
/**
* Checks if 2 arrays intersect.
*
* @param {Array} arr1
* @param {Array} arr2
* @private
*/
function
intersects
(
arr1
,
arr2
)
{
return
arr1
.
some
(
v
=>
arr2
.
includes
(
v
));
}
exports
.
intersects
=
intersects
;
test/unit/dialects/mariadb/query-generator.test.js
View file @
3578062
...
...
@@ -788,7 +788,7 @@ if (dialect === 'mariadb') {
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
this
.
queryGenerator
.
options
=
Object
.
assign
({},
this
.
queryGenerator
.
options
,
test
.
context
&&
test
.
context
.
options
||
{});
const
conditions
=
this
.
queryGenerator
[
suiteTitle
]
.
apply
(
this
.
queryGenerator
,
test
.
arguments
);
const
conditions
=
this
.
queryGenerator
[
suiteTitle
]
(...
test
.
arguments
);
expect
(
conditions
).
to
.
deep
.
equal
(
test
.
expectation
);
});
});
...
...
test/unit/dialects/mssql/query-generator.test.js
View file @
3578062
...
...
@@ -295,7 +295,7 @@ if (current.dialect.name === 'mssql') {
}
].
forEach
(
test
=>
{
it
(
test
.
title
,
function
()
{
expectsql
(
this
.
queryGenerator
.
arithmeticQuery
.
apply
(
this
.
queryGenerator
,
test
.
arguments
),
{
expectsql
(
this
.
queryGenerator
.
arithmeticQuery
(...
test
.
arguments
),
{
mssql
:
test
.
expectation
});
});
...
...
test/unit/dialects/mysql/query-generator.test.js
View file @
3578062
...
...
@@ -738,7 +738,7 @@ if (dialect === 'mysql') {
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
this
.
queryGenerator
.
options
=
Object
.
assign
({},
this
.
queryGenerator
.
options
,
test
.
context
&&
test
.
context
.
options
||
{});
const
conditions
=
this
.
queryGenerator
[
suiteTitle
]
.
apply
(
this
.
queryGenerator
,
test
.
arguments
);
const
conditions
=
this
.
queryGenerator
[
suiteTitle
]
(...
test
.
arguments
);
expect
(
conditions
).
to
.
deep
.
equal
(
test
.
expectation
);
});
});
...
...
test/unit/dialects/postgres/query-generator.test.js
View file @
3578062
...
...
@@ -1231,7 +1231,7 @@ if (dialect.startsWith('postgres')) {
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
this
.
queryGenerator
.
options
=
Object
.
assign
({},
this
.
queryGenerator
.
options
,
test
.
context
&&
test
.
context
.
options
||
{});
const
conditions
=
this
.
queryGenerator
[
suiteTitle
]
.
apply
(
this
.
queryGenerator
,
test
.
arguments
);
const
conditions
=
this
.
queryGenerator
[
suiteTitle
]
(...
test
.
arguments
);
expect
(
conditions
).
to
.
deep
.
equal
(
test
.
expectation
);
});
});
...
...
test/unit/dialects/sqlite/query-generator.test.js
View file @
3578062
...
...
@@ -644,7 +644,7 @@ if (dialect === 'sqlite') {
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
this
.
queryGenerator
.
options
=
Object
.
assign
({},
this
.
queryGenerator
.
options
,
test
.
context
&&
test
.
context
.
options
||
{});
const
conditions
=
this
.
queryGenerator
[
suiteTitle
]
.
apply
(
this
.
queryGenerator
,
test
.
arguments
);
const
conditions
=
this
.
queryGenerator
[
suiteTitle
]
(...
test
.
arguments
);
expect
(
conditions
).
to
.
deep
.
equal
(
test
.
expectation
);
});
});
...
...
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