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 6b1ff3b1
authored
Nov 18, 2018
by
Simon Schick
Committed by
Sushant
Nov 18, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: optimize memoize use, misc cases (#10122)
1 parent
ce9287fe
Hide whitespace changes
Inline
Side-by-side
Showing
22 changed files
with
103 additions
and
135 deletions
lib/data-types.js
lib/dialects/abstract/connection-manager.js
lib/dialects/abstract/query-generator.js
lib/dialects/mssql/query-generator.js
lib/dialects/mysql/query-generator.js
lib/dialects/mysql/query.js
lib/dialects/postgres/data-types.js
lib/dialects/postgres/query-generator.js
lib/dialects/postgres/query.js
lib/dialects/sqlite/data-types.js
lib/dialects/sqlite/query-generator.js
lib/errors/index.js
lib/instance-validator.js
lib/model.js
lib/query-interface.js
lib/sequelize.js
lib/transaction.js
lib/utils.js
test/support.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/data-types.js
View file @
6b1ff3b
...
...
@@ -67,7 +67,7 @@ STRING.prototype.toSql = function toSql() {
};
STRING
.
prototype
.
validate
=
function
validate
(
value
)
{
if
(
Object
.
prototype
.
toString
.
call
(
value
)
!==
'[object String]'
)
{
if
(
this
.
options
.
binary
&&
Buffer
.
isBuffer
(
value
)
||
_
.
isNumber
(
value
)
)
{
if
(
this
.
options
.
binary
&&
Buffer
.
isBuffer
(
value
)
||
typeof
value
===
'number'
)
{
return
true
;
}
throw
new
sequelizeErrors
.
ValidationError
(
util
.
format
(
'%j is not a valid string'
,
value
));
...
...
@@ -133,7 +133,7 @@ TEXT.prototype.toSql = function toSql() {
}
};
TEXT
.
prototype
.
validate
=
function
validate
(
value
)
{
if
(
!
_
.
isString
(
value
)
)
{
if
(
typeof
value
!==
'string'
)
{
throw
new
sequelizeErrors
.
ValidationError
(
util
.
format
(
'%j is not a valid string'
,
value
));
}
...
...
@@ -158,7 +158,7 @@ CITEXT.prototype.toSql = function toSql() {
return
'CITEXT'
;
};
CITEXT
.
prototype
.
validate
=
function
validate
(
value
)
{
if
(
!
_
.
isString
(
value
)
)
{
if
(
typeof
value
!==
'string'
)
{
throw
new
sequelizeErrors
.
ValidationError
(
util
.
format
(
'%j is not a valid string'
,
value
));
}
...
...
@@ -460,13 +460,14 @@ BOOLEAN.prototype._sanitize = function _sanitize(value) {
value
=
value
[
0
];
}
if
(
_
.
isString
(
value
))
{
const
type
=
typeof
value
;
if
(
type
===
'string'
)
{
// Only take action on valid boolean strings.
value
=
value
===
'true'
?
true
:
value
===
'false'
?
false
:
value
;
}
else
if
(
_
.
isNumber
(
value
)
)
{
return
value
===
'true'
?
true
:
value
===
'false'
?
false
:
value
;
}
if
(
type
===
'number'
)
{
// Only take action on valid boolean integers.
value
=
value
===
1
?
true
:
value
===
0
?
false
:
value
;
return
value
===
1
?
true
:
value
===
0
?
false
:
value
;
}
}
...
...
@@ -695,7 +696,7 @@ BLOB.prototype.toSql = function toSql() {
}
};
BLOB
.
prototype
.
validate
=
function
validate
(
value
)
{
if
(
!
_
.
isString
(
value
)
&&
!
Buffer
.
isBuffer
(
value
))
{
if
(
typeof
value
!==
'string'
&&
!
Buffer
.
isBuffer
(
value
))
{
throw
new
sequelizeErrors
.
ValidationError
(
util
.
format
(
'%j is not a valid blob'
,
value
));
}
...
...
@@ -744,7 +745,7 @@ function RANGE(subtype) {
if
(
!
options
.
subtype
)
options
.
subtype
=
new
INTEGER
();
if
(
_
.
isFunction
(
options
.
subtype
)
)
{
if
(
typeof
options
.
subtype
===
'function'
)
{
options
.
subtype
=
new
options
.
subtype
();
}
...
...
@@ -781,7 +782,7 @@ inherits(UUID, ABSTRACT);
UUID
.
prototype
.
key
=
UUID
.
key
=
'UUID'
;
UUID
.
prototype
.
validate
=
function
validate
(
value
,
options
)
{
if
(
!
_
.
isString
(
value
)
||
!
Validator
.
isUUID
(
value
)
&&
(
!
options
||
!
options
.
acceptStrings
))
{
if
(
typeof
value
!==
'string'
||
!
Validator
.
isUUID
(
value
)
&&
(
!
options
||
!
options
.
acceptStrings
))
{
throw
new
sequelizeErrors
.
ValidationError
(
util
.
format
(
'%j is not a valid uuid'
,
value
));
}
...
...
@@ -800,7 +801,7 @@ inherits(UUIDV1, ABSTRACT);
UUIDV1
.
prototype
.
key
=
UUIDV1
.
key
=
'UUIDV1'
;
UUIDV1
.
prototype
.
validate
=
function
validate
(
value
,
options
)
{
if
(
!
_
.
isString
(
value
)
||
!
Validator
.
isUUID
(
value
)
&&
(
!
options
||
!
options
.
acceptStrings
))
{
if
(
typeof
value
!==
'string'
||
!
Validator
.
isUUID
(
value
)
&&
(
!
options
||
!
options
.
acceptStrings
))
{
throw
new
sequelizeErrors
.
ValidationError
(
util
.
format
(
'%j is not a valid uuid'
,
value
));
}
...
...
@@ -819,7 +820,7 @@ inherits(UUIDV4, ABSTRACT);
UUIDV4
.
prototype
.
key
=
UUIDV4
.
key
=
'UUIDV4'
;
UUIDV4
.
prototype
.
validate
=
function
validate
(
value
,
options
)
{
if
(
!
_
.
isString
(
value
)
||
!
Validator
.
isUUID
(
value
,
4
)
&&
(
!
options
||
!
options
.
acceptStrings
))
{
if
(
typeof
value
!==
'string'
||
!
Validator
.
isUUID
(
value
,
4
)
&&
(
!
options
||
!
options
.
acceptStrings
))
{
throw
new
sequelizeErrors
.
ValidationError
(
util
.
format
(
'%j is not a valid uuidv4'
,
value
));
}
...
...
@@ -1084,7 +1085,7 @@ inherits(CIDR, ABSTRACT);
CIDR
.
prototype
.
key
=
CIDR
.
key
=
'CIDR'
;
CIDR
.
prototype
.
validate
=
function
validate
(
value
)
{
if
(
!
_
.
isString
(
value
)
||
!
Validator
.
isIPRange
(
value
))
{
if
(
typeof
value
!==
'string'
||
!
Validator
.
isIPRange
(
value
))
{
throw
new
sequelizeErrors
.
ValidationError
(
util
.
format
(
'%j is not a valid CIDR'
,
value
));
}
...
...
@@ -1106,7 +1107,7 @@ inherits(INET, ABSTRACT);
INET
.
prototype
.
key
=
INET
.
key
=
'INET'
;
INET
.
prototype
.
validate
=
function
validate
(
value
)
{
if
(
!
_
.
isString
(
value
)
||
!
Validator
.
isIP
(
value
))
{
if
(
typeof
value
!==
'string'
||
!
Validator
.
isIP
(
value
))
{
throw
new
sequelizeErrors
.
ValidationError
(
util
.
format
(
'%j is not a valid INET'
,
value
));
}
...
...
@@ -1128,7 +1129,7 @@ inherits(MACADDR, ABSTRACT);
MACADDR
.
prototype
.
key
=
MACADDR
.
key
=
'MACADDR'
;
MACADDR
.
prototype
.
validate
=
function
validate
(
value
)
{
if
(
!
_
.
isString
(
value
)
||
!
Validator
.
isMACAddress
(
value
))
{
if
(
typeof
value
!==
'string'
||
!
Validator
.
isMACAddress
(
value
))
{
throw
new
sequelizeErrors
.
ValidationError
(
util
.
format
(
'%j is not a valid MACADDR'
,
value
));
}
...
...
lib/dialects/abstract/connection-manager.js
View file @
6b1ff3b
...
...
@@ -167,7 +167,7 @@ class ConnectionManager {
}
},
acquire
:
(
queryType
,
useMaster
)
=>
{
useMaster
=
_
.
isUndefined
(
useMaster
)
?
false
:
useMaster
;
useMaster
=
useMaster
===
undefined
?
false
:
useMaster
;
if
(
queryType
===
'SELECT'
&&
!
useMaster
)
{
return
this
.
pool
.
read
.
acquire
();
}
...
...
lib/dialects/abstract/query-generator.js
View file @
6b1ff3b
...
...
@@ -490,7 +490,7 @@ class QueryGenerator {
}
options
.
prefix
=
options
.
prefix
||
rawTablename
||
tableName
;
if
(
options
.
prefix
&&
_
.
isString
(
options
.
prefix
)
)
{
if
(
options
.
prefix
&&
typeof
options
.
prefix
===
'string'
)
{
options
.
prefix
=
options
.
prefix
.
replace
(
/
\.
/g
,
'_'
);
options
.
prefix
=
options
.
prefix
.
replace
(
/
(\"
|
\')
/g
,
''
);
}
...
...
@@ -545,7 +545,7 @@ class QueryGenerator {
options
.
where
=
this
.
whereQuery
(
options
.
where
);
}
if
(
_
.
isString
(
tableName
)
)
{
if
(
typeof
tableName
===
'string'
)
{
tableName
=
this
.
quoteIdentifiers
(
tableName
);
}
else
{
tableName
=
this
.
quoteTable
(
tableName
);
...
...
@@ -2030,7 +2030,7 @@ class QueryGenerator {
return '';
}
if (
_.isString(where)
) {
if (
typeof where === 'string'
) {
throw new Error('Support for `
{
where
:
\
'raw query\'}` has been removed.'
);
}
...
...
@@ -2443,7 +2443,7 @@ class QueryGenerator {
options
=
options
||
{};
if
(
typeof
prepend
===
'undefined'
)
{
if
(
prepend
===
undefined
)
{
prepend
=
true
;
}
...
...
lib/dialects/mssql/query-generator.js
View file @
6b1ff3b
...
...
@@ -156,7 +156,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
if
(
options
.
uniqueKeys
)
{
_
.
each
(
options
.
uniqueKeys
,
(
columns
,
indexName
)
=>
{
if
(
columns
.
customIndex
)
{
if
(
!
_
.
isString
(
indexName
)
)
{
if
(
typeof
indexName
!==
'string'
)
{
indexName
=
`uniq_
${
tableName
}
_
${
columns
.
fields
.
join
(
'_'
)}
`
;
}
attributesClause
+=
`, CONSTRAINT
${
this
.
quoteIdentifier
(
indexName
)}
UNIQUE (
${
columns
.
fields
.
map
(
field
=>
this
.
quoteIdentifier
(
field
)).
join
(
', '
)}
)`
;
...
...
@@ -596,7 +596,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
}
}
if
(
attribute
.
comment
&&
_
.
isString
(
attribute
.
comment
)
)
{
if
(
attribute
.
comment
&&
typeof
attribute
.
comment
===
'string'
)
{
template
+=
` COMMENT
${
attribute
.
comment
}
`
;
}
...
...
lib/dialects/mysql/query-generator.js
View file @
6b1ff3b
...
...
@@ -85,7 +85,7 @@ class MySQLQueryGenerator extends AbstractQueryGenerator {
const
table
=
this
.
quoteTable
(
tableName
);
let
attributesClause
=
attrStr
.
join
(
', '
);
const
comment
=
options
.
comment
&&
_
.
isString
(
options
.
comment
)
?
` COMMENT
${
this
.
escape
(
options
.
comment
)}
`
:
''
;
const
comment
=
options
.
comment
&&
typeof
options
.
comment
===
'string'
?
` COMMENT
${
this
.
escape
(
options
.
comment
)}
`
:
''
;
const
engine
=
options
.
engine
;
const
charset
=
options
.
charset
?
` DEFAULT CHARSET=
${
options
.
charset
}
`
:
''
;
const
collation
=
options
.
collate
?
` COLLATE
${
options
.
collate
}
`
:
''
;
...
...
@@ -96,7 +96,7 @@ class MySQLQueryGenerator extends AbstractQueryGenerator {
if
(
options
.
uniqueKeys
)
{
_
.
each
(
options
.
uniqueKeys
,
(
columns
,
indexName
)
=>
{
if
(
columns
.
customIndex
)
{
if
(
!
_
.
isString
(
indexName
)
)
{
if
(
typeof
indexName
!==
'string'
)
{
indexName
=
`uniq_
${
tableName
}
_
${
columns
.
fields
.
join
(
'_'
)}
`
;
}
attributesClause
+=
`, UNIQUE
${
this
.
quoteIdentifier
(
indexName
)}
(
${
columns
.
fields
.
map
(
field
=>
this
.
quoteIdentifier
(
field
)).
join
(
', '
)}
)`
;
...
...
@@ -423,7 +423,7 @@ class MySQLQueryGenerator extends AbstractQueryGenerator {
* @private
*/
_checkValidJsonStatement
(
stmt
)
{
if
(
!
_
.
isString
(
stmt
)
)
{
if
(
typeof
stmt
!==
'string'
)
{
return
false
;
}
...
...
lib/dialects/mysql/query.js
View file @
6b1ff3b
...
...
@@ -188,7 +188,7 @@ class Query extends AbstractQuery {
const
warningMessage
=
`MySQL Warnings (
${
this
.
connection
.
uuid
||
'default'
}
): `
;
const
messages
=
[];
for
(
const
_warningRow
of
warningResults
)
{
if
(
typeof
_warningRow
===
'undefined'
||
typeof
_warningRow
[
Symbol
.
iterator
]
!==
'function'
)
continue
;
if
(
_warningRow
===
undefined
||
typeof
_warningRow
[
Symbol
.
iterator
]
!==
'function'
)
continue
;
for
(
const
_warningResult
of
_warningRow
)
{
if
(
_warningResult
.
hasOwnProperty
(
'Message'
))
{
messages
.
push
(
_warningResult
.
Message
);
...
...
lib/dialects/postgres/data-types.js
View file @
6b1ff3b
...
...
@@ -70,7 +70,7 @@ module.exports = BaseTypes => {
DATEONLY
.
prototype
.
_sanitize
=
function
_sanitize
(
value
,
options
)
{
if
((
!
options
||
options
&&
!
options
.
raw
)
&&
value
!==
Infinity
&&
value
!==
-
Infinity
)
{
if
(
_
.
isString
(
value
)
)
{
if
(
typeof
value
===
'string'
)
{
if
(
value
.
toLowerCase
()
===
'infinity'
)
{
return
Infinity
;
}
...
...
@@ -174,12 +174,12 @@ module.exports = BaseTypes => {
value
=
value
[
0
];
}
if
(
_
.
isString
(
value
)
)
{
if
(
typeof
value
===
'string'
)
{
// Only take action on valid boolean strings.
return
value
===
'true'
||
value
===
't'
?
true
:
value
===
'false'
||
value
===
'f'
?
false
:
value
;
}
if
(
_
.
isNumber
(
value
)
)
{
if
(
typeof
value
===
'number'
)
{
// Only take action on valid boolean integers.
return
value
===
1
?
true
:
value
===
0
?
false
:
value
;
}
...
...
@@ -222,7 +222,7 @@ module.exports = BaseTypes => {
DATE
.
prototype
.
_sanitize
=
function
_sanitize
(
value
,
options
)
{
if
((
!
options
||
options
&&
!
options
.
raw
)
&&
!
(
value
instanceof
Date
)
&&
!!
value
&&
value
!==
Infinity
&&
value
!==
-
Infinity
)
{
if
(
_
.
isString
(
value
)
)
{
if
(
typeof
value
===
'string'
)
{
if
(
value
.
toLowerCase
()
===
'infinity'
)
{
return
Infinity
;
}
...
...
lib/dialects/postgres/query-generator.js
View file @
6b1ff3b
...
...
@@ -66,7 +66,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
const
quotedTable
=
this
.
quoteTable
(
tableName
);
if
(
options
.
comment
&&
_
.
isString
(
options
.
comment
)
)
{
if
(
options
.
comment
&&
typeof
options
.
comment
===
'string'
)
{
comments
+=
`; COMMENT ON TABLE
${
quotedTable
}
IS
${
this
.
escape
(
options
.
comment
)}
`
;
}
...
...
@@ -151,7 +151,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
* @throws {Error} throw if the statement looks like json function but has invalid token
*/
_checkValidJsonStatement
(
stmt
)
{
if
(
!
_
.
isString
(
stmt
)
)
{
if
(
typeof
stmt
!==
'string'
)
{
return
false
;
}
...
...
@@ -397,7 +397,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
showIndexesQuery
(
tableName
)
{
let
schemaJoin
=
''
;
let
schemaWhere
=
''
;
if
(
!
_
.
isString
(
tableName
)
)
{
if
(
typeof
tableName
!==
'string'
)
{
schemaJoin
=
', pg_namespace s'
;
schemaWhere
=
` AND s.oid = t.relnamespace AND s.nspname = '
${
tableName
.
schema
}
'`
;
tableName
=
tableName
.
tableName
;
...
...
@@ -535,7 +535,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
}
}
if
(
attribute
.
comment
&&
_
.
isString
(
attribute
.
comment
)
)
{
if
(
attribute
.
comment
&&
typeof
attribute
.
comment
===
'string'
)
{
if
(
options
&&
options
.
context
===
'addColumn'
)
{
const
quotedAttr
=
this
.
quoteIdentifier
(
options
.
key
);
const
escapedCommentText
=
this
.
escape
(
attribute
.
comment
);
...
...
@@ -638,16 +638,16 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
}
expandFunctionParamList
(
params
)
{
if
(
_
.
isUndefined
(
params
)
||
!
Array
.
isArray
(
params
))
{
if
(
params
===
undefined
||
!
Array
.
isArray
(
params
))
{
throw
new
Error
(
'expandFunctionParamList: function parameters array required, including an empty one for no arguments'
);
}
const
paramList
=
[];
params
.
forEach
(
curParam
=>
{
const
paramDef
=
[];
if
(
_
.
has
(
curParam
,
'type'
)
)
{
if
(
_
.
has
(
curParam
,
'direction'
)
)
{
paramDef
.
push
(
curParam
.
direction
);
}
if
(
_
.
has
(
curParam
,
'name'
)
)
{
paramDef
.
push
(
curParam
.
name
);
}
if
(
curParam
.
type
)
{
if
(
curParam
.
direction
)
{
paramDef
.
push
(
curParam
.
direction
);
}
if
(
curParam
.
name
)
{
paramDef
.
push
(
curParam
.
name
);
}
paramDef
.
push
(
curParam
.
type
);
}
else
{
throw
new
Error
(
'function or trigger used with a parameter without any type'
);
...
...
@@ -662,7 +662,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
}
expandOptions
(
options
)
{
return
_
.
isUndefined
(
options
)
||
_
.
isEmpty
(
options
)
?
return
options
===
undefined
||
_
.
isEmpty
(
options
)
?
''
:
options
.
join
(
' '
);
}
...
...
@@ -674,7 +674,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
'after_constraint'
:
'AFTER'
};
if
(
!
_
.
has
(
EVENT_DECODER
,
eventSpecifier
)
)
{
if
(
!
EVENT_DECODER
[
eventSpecifier
]
)
{
throw
new
Error
(
`Invalid trigger event specified:
${
eventSpecifier
}
`
);
}
...
...
@@ -698,7 +698,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
'truncate'
:
'TRUNCATE'
};
if
(
!
_
.
has
(
EVENT_MAP
,
fireValue
)
)
{
if
(
!
EVENT_MAP
[
fireValue
]
)
{
throw
new
Error
(
`parseTriggerEventSpec: undefined trigger event
${
fireKey
}
`
);
}
...
...
lib/dialects/postgres/query.js
View file @
6b1ff3b
...
...
@@ -100,7 +100,7 @@ class Query extends AbstractQuery {
:
queryResult
.
rows
;
const
rowCount
=
Array
.
isArray
(
queryResult
)
?
queryResult
.
reduce
(
(
count
,
r
)
=>
_
.
isFinite
(
r
.
rowCount
)
?
count
+
r
.
rowCount
:
count
,
(
count
,
r
)
=>
Number
.
isFinite
(
r
.
rowCount
)
?
count
+
r
.
rowCount
:
count
,
0
)
:
queryResult
.
rowCount
;
...
...
lib/dialects/sqlite/data-types.js
View file @
6b1ff3b
...
...
@@ -244,7 +244,7 @@ module.exports = BaseTypes => {
[
FLOAT
,
DOUBLE
,
REAL
].
forEach
(
floating
=>
{
floating
.
parse
=
function
parse
(
value
)
{
if
(
_
.
isString
(
value
)
)
{
if
(
typeof
value
===
'string'
)
{
if
(
value
===
'NaN'
)
{
return
NaN
;
}
...
...
lib/dialects/sqlite/query-generator.js
View file @
6b1ff3b
...
...
@@ -84,7 +84,7 @@ class SQLiteQueryGenerator extends MySqlQueryGenerator {
* @throws {Error} throw if the statement looks like json function but has invalid token
*/
_checkValidJsonStatement
(
stmt
)
{
if
(
!
_
.
isString
(
stmt
)
)
{
if
(
typeof
stmt
!==
'string'
)
{
return
false
;
}
...
...
lib/errors/index.js
View file @
6b1ff3b
...
...
@@ -355,8 +355,8 @@ class ValidationErrorItem {
* @private
*/
getValidatorKey
(
useTypeAsNS
,
NSSeparator
)
{
const
useTANS
=
typeof
useTypeAsNS
===
'undefined'
?
true
:
!!
useTypeAsNS
;
const
NSSep
=
typeof
NSSeparator
===
'undefined'
?
'.'
:
NSSeparator
;
const
useTANS
=
useTypeAsNS
===
undefined
||
!!
useTypeAsNS
;
const
NSSep
=
NSSeparator
===
undefined
?
'.'
:
NSSeparator
;
const
type
=
this
.
origin
;
const
key
=
this
.
validatorKey
||
this
.
validatorName
;
...
...
lib/instance-validator.js
View file @
6b1ff3b
...
...
@@ -182,7 +182,7 @@ class InstanceValidator {
*/
_builtinAttrValidate
(
value
,
field
)
{
// check if value is null (if null not allowed the Schema pass will capture it)
if
(
value
===
null
||
typeof
value
===
'undefined'
)
{
if
(
value
===
null
||
value
===
undefined
)
{
return
Promise
.
resolve
();
}
...
...
lib/model.js
View file @
6b1ff3b
...
...
@@ -267,7 +267,7 @@ class Model {
});
_
.
each
(
tail
,
(
value
,
attr
)
=>
{
if
(
_
.
isUndefined
(
this
.
rawAttributes
[
attr
])
)
{
if
(
this
.
rawAttributes
[
attr
]
===
undefined
)
{
this
.
rawAttributes
[
attr
]
=
value
;
}
});
...
...
@@ -965,7 +965,7 @@ class Model {
throw
new
Error
(
`A model validator function must not have the same name as a field. Model:
${
this
.
name
}
, field/validation name:
${
validatorType
}
`
);
}
if
(
!
_
.
isFunction
(
validator
)
)
{
if
(
typeof
validator
!==
'function'
)
{
throw
new
Error
(
`Members of the validate option must be functions. Model:
${
this
.
name
}
, error with validate member
${
validatorType
}
`
);
}
});
...
...
@@ -988,38 +988,37 @@ class Model {
return
attribute
;
});
const
tableName
=
this
.
getTableName
();
this
.
_indexes
=
this
.
options
.
indexes
.
map
(
index
=>
this
.
_conformIndex
(
index
))
.
map
(
index
=>
Utils
.
nameIndex
(
index
,
this
.
getTableName
()));
.
map
(
index
=>
Utils
.
nameIndex
(
this
.
_conformIndex
(
index
),
tableName
));
this
.
primaryKeys
=
{};
this
.
_readOnlyAttributes
=
[]
;
this
.
_readOnlyAttributes
=
new
Set
()
;
this
.
_timestampAttributes
=
{};
// setup names of timestamp attributes
if
(
this
.
options
.
timestamps
)
{
if
(
this
.
options
.
createdAt
!==
false
)
{
this
.
_timestampAttributes
.
createdAt
=
this
.
options
.
createdAt
||
'createdAt'
;
this
.
_readOnlyAttributes
.
push
(
this
.
_timestampAttributes
.
createdAt
);
this
.
_readOnlyAttributes
.
add
(
this
.
_timestampAttributes
.
createdAt
);
}
if
(
this
.
options
.
updatedAt
!==
false
)
{
this
.
_timestampAttributes
.
updatedAt
=
this
.
options
.
updatedAt
||
'updatedAt'
;
this
.
_readOnlyAttributes
.
push
(
this
.
_timestampAttributes
.
updatedAt
);
this
.
_readOnlyAttributes
.
add
(
this
.
_timestampAttributes
.
updatedAt
);
}
if
(
this
.
options
.
paranoid
&&
this
.
options
.
deletedAt
!==
false
)
{
this
.
_timestampAttributes
.
deletedAt
=
this
.
options
.
deletedAt
||
'deletedAt'
;
this
.
_readOnlyAttributes
.
push
(
this
.
_timestampAttributes
.
deletedAt
);
this
.
_readOnlyAttributes
.
add
(
this
.
_timestampAttributes
.
deletedAt
);
}
}
// setup name for version attribute
if
(
this
.
options
.
version
)
{
this
.
_versionAttribute
=
typeof
this
.
options
.
version
===
'string'
?
this
.
options
.
version
:
'version'
;
this
.
_readOnlyAttributes
.
push
(
this
.
_versionAttribute
);
this
.
_readOnlyAttributes
.
add
(
this
.
_versionAttribute
);
}
this
.
_hasReadOnlyAttributes
=
this
.
_readOnlyAttributes
.
length
>
0
;
this
.
_isReadOnlyAttribute
=
_
.
memoize
(
key
=>
this
.
_readOnlyAttributes
.
includes
(
key
));
this
.
_hasReadOnlyAttributes
=
this
.
_readOnlyAttributes
.
size
>
0
;
// Add head and tail default attributes (id, timestamps)
this
.
_addDefaultAttributes
();
...
...
@@ -1091,13 +1090,10 @@ class Model {
this
.
_dataTypeChanges
=
{};
this
.
_dataTypeSanitizers
=
{};
this
.
_booleanAttributes
=
[];
this
.
_dateAttributes
=
[];
this
.
_hstoreAttributes
=
[];
this
.
_rangeAttributes
=
[];
this
.
_jsonAttributes
=
[];
this
.
_geometryAttributes
=
[];
this
.
_virtualAttributes
=
[];
this
.
_hasBooleanAttributes
=
false
;
this
.
_hasDateAttributes
=
false
;
this
.
_jsonAttributes
=
new
Set
();
this
.
_virtualAttributes
=
new
Set
();
this
.
_defaultValues
=
{};
this
.
prototype
.
validators
=
{};
...
...
@@ -1123,7 +1119,6 @@ class Model {
this
.
fieldRawAttributesMap
[
definition
.
field
]
=
definition
;
if
(
definition
.
type
.
_sanitize
)
{
this
.
_dataTypeSanitizers
[
name
]
=
definition
.
type
.
_sanitize
;
}
...
...
@@ -1133,19 +1128,13 @@ class Model {
}
if
(
definition
.
type
instanceof
DataTypes
.
BOOLEAN
)
{
this
.
_
booleanAttributes
.
push
(
name
)
;
this
.
_
hasBooleanAttributes
=
true
;
}
else
if
(
definition
.
type
instanceof
DataTypes
.
DATE
||
definition
.
type
instanceof
DataTypes
.
DATEONLY
)
{
this
.
_dateAttributes
.
push
(
name
);
}
else
if
(
definition
.
type
instanceof
DataTypes
.
HSTORE
||
DataTypes
.
ARRAY
.
is
(
definition
.
type
,
DataTypes
.
HSTORE
))
{
this
.
_hstoreAttributes
.
push
(
name
);
}
else
if
(
definition
.
type
instanceof
DataTypes
.
RANGE
||
DataTypes
.
ARRAY
.
is
(
definition
.
type
,
DataTypes
.
RANGE
))
{
this
.
_rangeAttributes
.
push
(
name
);
this
.
_hasDateAttributes
=
true
;
}
else
if
(
definition
.
type
instanceof
DataTypes
.
JSON
)
{
this
.
_jsonAttributes
.
push
(
name
);
this
.
_jsonAttributes
.
add
(
name
);
}
else
if
(
definition
.
type
instanceof
DataTypes
.
VIRTUAL
)
{
this
.
_virtualAttributes
.
push
(
name
);
}
else
if
(
definition
.
type
instanceof
DataTypes
.
GEOMETRY
)
{
this
.
_geometryAttributes
.
push
(
name
);
this
.
_virtualAttributes
.
add
(
name
);
}
if
(
definition
.
hasOwnProperty
(
'defaultValue'
))
{
...
...
@@ -1203,30 +1192,13 @@ class Model {
return
map
;
},
{});
this
.
_hasBooleanAttributes
=
!!
this
.
_booleanAttributes
.
length
;
this
.
_isBooleanAttribute
=
_
.
memoize
(
key
=>
this
.
_booleanAttributes
.
includes
(
key
));
this
.
_hasDateAttributes
=
!!
this
.
_dateAttributes
.
length
;
this
.
_isDateAttribute
=
_
.
memoize
(
key
=>
this
.
_dateAttributes
.
includes
(
key
));
this
.
_hasHstoreAttributes
=
!!
this
.
_hstoreAttributes
.
length
;
this
.
_isHstoreAttribute
=
_
.
memoize
(
key
=>
this
.
_hstoreAttributes
.
includes
(
key
));
this
.
_hasRangeAttributes
=
!!
this
.
_rangeAttributes
.
length
;
this
.
_isRangeAttribute
=
_
.
memoize
(
key
=>
this
.
_rangeAttributes
.
includes
(
key
));
this
.
_hasJsonAttributes
=
!!
this
.
_jsonAttributes
.
size
;
this
.
_hasJsonAttributes
=
!!
this
.
_jsonAttributes
.
length
;
this
.
_isJsonAttribute
=
_
.
memoize
(
key
=>
this
.
_jsonAttributes
.
includes
(
key
));
this
.
_hasVirtualAttributes
=
!!
this
.
_virtualAttributes
.
length
;
this
.
_isVirtualAttribute
=
_
.
memoize
(
key
=>
this
.
_virtualAttributes
.
includes
(
key
));
this
.
_hasGeometryAttributes
=
!!
this
.
_geometryAttributes
.
length
;
this
.
_isGeometryAttribute
=
_
.
memoize
(
key
=>
this
.
_geometryAttributes
.
includes
(
key
));
this
.
_hasVirtualAttributes
=
!!
this
.
_virtualAttributes
.
size
;
this
.
_hasDefaultValues
=
!
_
.
isEmpty
(
this
.
_defaultValues
);
this
.
tableAttributes
=
_
.
omit
(
this
.
rawAttributes
,
this
.
_virtualAttributes
);
this
.
tableAttributes
=
_
.
omit
By
(
this
.
rawAttributes
,
(
_a
,
key
)
=>
this
.
_virtualAttributes
.
has
(
key
)
);
this
.
prototype
.
_hasCustomGetters
=
Object
.
keys
(
this
.
prototype
.
_customGetters
).
length
;
this
.
prototype
.
_hasCustomSetters
=
Object
.
keys
(
this
.
prototype
.
_customSetters
).
length
;
...
...
@@ -1557,8 +1529,7 @@ class Model {
}
else
{
scopeName
=
option
;
scope
=
self
.
options
.
scopes
[
scopeName
];
if
(
_
.
isFunction
(
scope
))
{
if
(
typeof
scope
===
'function'
)
{
scope
=
scope
();
}
}
...
...
@@ -1795,7 +1766,7 @@ class Model {
for
(
const
attribute
of
attributes
)
{
if
(
this
.
_
isVirtualAttribute
(
attribute
)
this
.
_
virtualAttributes
.
has
(
attribute
)
&&
this
.
rawAttributes
[
attribute
].
type
.
fields
)
{
attributes
=
attributes
.
concat
(
this
.
rawAttributes
[
attribute
].
type
.
fields
);
...
...
@@ -3138,7 +3109,7 @@ class Model {
const
where
=
Object
.
assign
({},
options
.
where
);
let
values
=
{};
if
(
_
.
isString
(
fields
)
)
{
if
(
typeof
fields
===
'string'
)
{
values
[
fields
]
=
options
.
by
;
}
else
if
(
Array
.
isArray
(
fields
))
{
_
.
each
(
fields
,
field
=>
{
...
...
@@ -3397,20 +3368,20 @@ class Model {
}
else
{
// Loop and call set
if
(
options
.
attributes
)
{
let
keys
=
options
.
attributes
;
const
setKeys
=
data
=>
{
for
(
const
k
of
data
)
{
if
(
values
[
k
]
===
undefined
)
{
continue
;
}
this
.
set
(
k
,
values
[
k
],
options
);
}
};
setKeys
(
options
.
attributes
);
if
(
this
.
constructor
.
_hasVirtualAttributes
)
{
keys
=
keys
.
concat
(
this
.
constructor
.
_virtualAttributes
);
setKeys
(
this
.
constructor
.
_virtualAttributes
);
}
if
(
this
.
_options
.
includeNames
)
{
keys
=
keys
.
concat
(
this
.
_options
.
includeNames
);
}
for
(
const
k
of
keys
)
{
if
(
values
[
k
]
===
undefined
)
{
continue
;
}
this
.
set
(
k
,
values
[
k
],
options
);
setKeys
(
this
.
_options
.
includeNames
);
}
}
else
{
for
(
const
key
in
values
)
{
...
...
@@ -3452,7 +3423,7 @@ class Model {
if
(
!
options
.
raw
)
{
// If attribute is not in model definition, return
if
(
!
this
.
_isAttribute
(
key
))
{
if
(
key
.
includes
(
'.'
)
&&
this
.
constructor
.
_
isJsonAttribute
(
key
.
split
(
'.'
)[
0
]))
{
if
(
key
.
includes
(
'.'
)
&&
this
.
constructor
.
_
jsonAttributes
.
has
(
key
.
split
(
'.'
)[
0
]))
{
const
previousNestedValue
=
Dottie
.
get
(
this
.
dataValues
,
key
);
if
(
!
_
.
isEqual
(
previousNestedValue
,
value
))
{
Dottie
.
set
(
this
.
dataValues
,
key
,
value
);
...
...
@@ -3468,7 +3439,7 @@ class Model {
}
// If attempting to set read only attributes, return
if
(
!
this
.
isNewRecord
&&
this
.
constructor
.
_hasReadOnlyAttributes
&&
this
.
constructor
.
_
isReadOnlyAttribute
(
key
))
{
if
(
!
this
.
isNewRecord
&&
this
.
constructor
.
_hasReadOnlyAttributes
&&
this
.
constructor
.
_
readOnlyAttributes
.
has
(
key
))
{
return
this
;
}
}
...
...
@@ -3743,7 +3714,7 @@ class Model {
return
instance
.
save
(
includeOptions
).
then
(()
=>
this
[
include
.
association
.
accessors
.
set
](
instance
,
{
save
:
false
,
logging
:
options
.
logging
}));
});
}).
then
(()
=>
{
const
realFields
=
options
.
fields
.
filter
(
field
=>
!
this
.
constructor
.
_
isVirtualAttribute
(
field
));
const
realFields
=
options
.
fields
.
filter
(
field
=>
!
this
.
constructor
.
_
virtualAttributes
.
has
(
field
));
if
(
!
realFields
.
length
)
return
this
;
if
(
!
this
.
changed
()
&&
!
this
.
isNewRecord
)
return
this
;
...
...
lib/query-interface.js
View file @
6b1ff3b
...
...
@@ -1165,7 +1165,7 @@ class QueryInterface {
const
dataType
=
options
.
dataType
;
if
(
dataType
instanceof
DataTypes
.
DECIMAL
||
dataType
instanceof
DataTypes
.
FLOAT
)
{
if
(
!
_
.
isNull
(
result
)
)
{
if
(
result
!==
null
)
{
return
parseFloat
(
result
);
}
}
...
...
@@ -1173,7 +1173,7 @@ class QueryInterface {
return
parseInt
(
result
,
10
);
}
if
(
dataType
instanceof
DataTypes
.
DATE
)
{
if
(
!
_
.
isNull
(
result
)
&&
!
_
.
isDate
(
result
))
{
if
(
result
!==
null
&&
!
(
result
instanceof
Date
))
{
return
new
Date
(
result
);
}
}
...
...
lib/sequelize.js
View file @
6b1ff3b
...
...
@@ -238,7 +238,7 @@ class Sequelize {
if
(
_
.
isPlainObject
(
this
.
options
.
operatorsAliases
))
{
logger
.
deprecate
(
'String based operators are deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators'
);
this
.
dialect
.
QueryGenerator
.
setOperatorsAliases
(
this
.
options
.
operatorsAliases
);
}
else
if
(
_
.
isBoolean
(
this
.
options
.
operatorsAliases
)
)
{
}
else
if
(
typeof
this
.
options
.
operatorsAliases
===
'boolean'
)
{
logger
.
warn
(
'A boolean value was passed to options.operatorsAliases. This is a no-op with v5 and should be removed.'
);
}
...
...
lib/transaction.js
View file @
6b1ff3b
...
...
@@ -108,7 +108,7 @@ class Transaction {
prepareEnvironment
(
useCLS
)
{
let
connectionPromise
;
if
(
typeof
useCLS
===
'undefined'
)
{
if
(
useCLS
===
undefined
)
{
useCLS
=
true
;
}
...
...
lib/utils.js
View file @
6b1ff3b
...
...
@@ -146,7 +146,7 @@ exports.cloneDeep = cloneDeep;
function
mapFinderOptions
(
options
,
Model
)
{
if
(
options
.
attributes
&&
Array
.
isArray
(
options
.
attributes
))
{
options
.
attributes
=
Model
.
_injectDependentVirtualAttributes
(
options
.
attributes
);
options
.
attributes
=
options
.
attributes
.
filter
(
v
=>
!
Model
.
_
isVirtualAttribute
(
v
));
options
.
attributes
=
options
.
attributes
.
filter
(
v
=>
!
Model
.
_
virtualAttributes
.
has
(
v
));
}
mapOptionFieldNames
(
options
,
Model
);
...
...
@@ -216,7 +216,7 @@ function mapValueFieldNames(dataValues, fields, Model) {
const
values
=
{};
for
(
const
attr
of
fields
)
{
if
(
dataValues
[
attr
]
!==
undefined
&&
!
Model
.
_
isVirtualAttribute
(
attr
))
{
if
(
dataValues
[
attr
]
!==
undefined
&&
!
Model
.
_
virtualAttributes
.
has
(
attr
))
{
// Field name mapping
if
(
Model
.
rawAttributes
[
attr
]
&&
Model
.
rawAttributes
[
attr
].
field
&&
Model
.
rawAttributes
[
attr
].
field
!==
attr
)
{
values
[
Model
.
rawAttributes
[
attr
].
field
]
=
dataValues
[
attr
];
...
...
@@ -278,7 +278,7 @@ exports.toDefaultValue = toDefaultValue;
* @private
*/
function
defaultValueSchemable
(
value
)
{
if
(
typeof
value
===
'undefined'
)
{
return
false
;
}
if
(
value
===
undefined
)
{
return
false
;
}
// TODO this will be schemable when all supported db
// have been normalized for this case
...
...
@@ -286,11 +286,7 @@ function defaultValueSchemable(value) {
if
(
value
instanceof
DataTypes
.
UUIDV1
||
value
instanceof
DataTypes
.
UUIDV4
)
{
return
false
;
}
if
(
_
.
isFunction
(
value
))
{
return
false
;
}
return
true
;
return
typeof
value
!==
'function'
;
}
exports
.
defaultValueSchemable
=
defaultValueSchemable
;
...
...
test/support.js
View file @
6b1ff3b
...
...
@@ -228,7 +228,7 @@ const Support = {
}
}
if
(
_
.
isError
(
query
)
)
{
if
(
query
instanceof
Error
)
{
expect
(
query
.
message
).
to
.
equal
(
expectation
.
message
);
}
else
{
expect
(
query
.
query
||
query
).
to
.
equal
(
expectation
);
...
...
@@ -241,7 +241,7 @@ const Support = {
}
};
if
(
typeof
beforeEach
!==
'undefined'
)
{
if
(
global
.
beforeEach
)
{
beforeEach
(
function
()
{
this
.
sequelize
=
Support
.
sequelize
;
});
...
...
test/unit/dialects/mysql/query-generator.test.js
View file @
6b1ff3b
...
...
@@ -731,8 +731,8 @@ if (dialect === 'mysql') {
const
title
=
test
.
title
||
`MySQL correctly returns
${
query
}
for
${
JSON
.
stringify
(
test
.
arguments
)}
`
;
it
(
title
,
function
()
{
if
(
test
.
needsSequelize
)
{
if
(
_
.
isFunction
(
test
.
arguments
[
1
])
)
test
.
arguments
[
1
]
=
test
.
arguments
[
1
](
this
.
sequelize
);
if
(
_
.
isFunction
(
test
.
arguments
[
2
])
)
test
.
arguments
[
2
]
=
test
.
arguments
[
2
](
this
.
sequelize
);
if
(
typeof
test
.
arguments
[
1
]
===
'function'
)
test
.
arguments
[
1
]
=
test
.
arguments
[
1
](
this
.
sequelize
);
if
(
typeof
test
.
arguments
[
2
]
===
'function'
)
test
.
arguments
[
2
]
=
test
.
arguments
[
2
](
this
.
sequelize
);
}
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
...
...
test/unit/dialects/postgres/query-generator.test.js
View file @
6b1ff3b
...
...
@@ -1224,8 +1224,8 @@ if (dialect.startsWith('postgres')) {
const
title
=
test
.
title
||
`Postgres correctly returns
${
query
}
for
${
JSON
.
stringify
(
test
.
arguments
)}
`
;
it
(
title
,
function
()
{
if
(
test
.
needsSequelize
)
{
if
(
_
.
isFunction
(
test
.
arguments
[
1
])
)
test
.
arguments
[
1
]
=
test
.
arguments
[
1
](
this
.
sequelize
);
if
(
_
.
isFunction
(
test
.
arguments
[
2
])
)
test
.
arguments
[
2
]
=
test
.
arguments
[
2
](
this
.
sequelize
);
if
(
typeof
test
.
arguments
[
1
]
===
'function'
)
test
.
arguments
[
1
]
=
test
.
arguments
[
1
](
this
.
sequelize
);
if
(
typeof
test
.
arguments
[
2
]
===
'function'
)
test
.
arguments
[
2
]
=
test
.
arguments
[
2
](
this
.
sequelize
);
}
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
...
...
test/unit/dialects/sqlite/query-generator.test.js
View file @
6b1ff3b
...
...
@@ -637,8 +637,8 @@ if (dialect === 'sqlite') {
const
title
=
test
.
title
||
`SQLite correctly returns
${
query
}
for
${
JSON
.
stringify
(
test
.
arguments
)}
`
;
it
(
title
,
function
()
{
if
(
test
.
needsSequelize
)
{
if
(
_
.
isFunction
(
test
.
arguments
[
1
])
)
test
.
arguments
[
1
]
=
test
.
arguments
[
1
](
this
.
sequelize
);
if
(
_
.
isFunction
(
test
.
arguments
[
2
])
)
test
.
arguments
[
2
]
=
test
.
arguments
[
2
](
this
.
sequelize
);
if
(
typeof
test
.
arguments
[
1
]
===
'function'
)
test
.
arguments
[
1
]
=
test
.
arguments
[
1
](
this
.
sequelize
);
if
(
typeof
test
.
arguments
[
2
]
===
'function'
)
test
.
arguments
[
2
]
=
test
.
arguments
[
2
](
this
.
sequelize
);
}
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
...
...
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