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 8813622a
authored
Sep 03, 2015
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: introduce Utils.mapFinderOptions
1 parent
b7c8e85a
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
210 additions
and
189 deletions
lib/data-types.js
lib/model.js
lib/utils.js
test/unit/utils.js
test/unit/utils.test.js
lib/data-types.js
View file @
8813622
...
@@ -676,10 +676,11 @@ UUIDV4.prototype.validate = function(value) {
...
@@ -676,10 +676,11 @@ UUIDV4.prototype.validate = function(value) {
* @property VIRTUAL
* @property VIRTUAL
* @alias NONE
* @alias NONE
*/
*/
var
VIRTUAL
=
function
(
returnType
,
fields
)
{
var
VIRTUAL
=
function
(
ReturnType
,
fields
)
{
if
(
!
(
this
instanceof
VIRTUAL
))
return
new
VIRTUAL
(
returnType
,
fields
);
if
(
!
(
this
instanceof
VIRTUAL
))
return
new
VIRTUAL
(
ReturnType
,
fields
);
if
(
typeof
ReturnType
===
'function'
)
ReturnType
=
new
ReturnType
();
this
.
returnType
=
returnType
;
this
.
returnType
=
ReturnType
;
this
.
fields
=
fields
;
this
.
fields
=
fields
;
};
};
util
.
inherits
(
VIRTUAL
,
ABSTRACT
);
util
.
inherits
(
VIRTUAL
,
ABSTRACT
);
...
...
lib/model.js
View file @
8813622
...
@@ -538,7 +538,7 @@ validateIncludedElement = function(include, tableNames, options) {
...
@@ -538,7 +538,7 @@ validateIncludedElement = function(include, tableNames, options) {
// pseudo include just needed the attribute logic, return
// pseudo include just needed the attribute logic, return
if
(
include
.
_pseudo
)
{
if
(
include
.
_pseudo
)
{
return
Utils
.
map
OptionFieldName
s
(
include
,
include
.
model
);
return
Utils
.
map
FinderOption
s
(
include
,
include
.
model
);
}
}
// check if the current Model is actually associated with the passed Model - or it's a pseudo include
// check if the current Model is actually associated with the passed Model - or it's a pseudo include
...
@@ -595,7 +595,7 @@ validateIncludedElement = function(include, tableNames, options) {
...
@@ -595,7 +595,7 @@ validateIncludedElement = function(include, tableNames, options) {
Model
.
$injectScope
(
model
.
$scope
,
include
);
Model
.
$injectScope
(
model
.
$scope
,
include
);
include
=
Utils
.
map
OptionFieldName
s
(
include
,
include
.
model
);
include
=
Utils
.
map
FinderOption
s
(
include
,
include
.
model
);
if
(
include
.
required
===
undefined
)
{
if
(
include
.
required
===
undefined
)
{
include
.
required
=
!!
include
.
where
;
include
.
required
=
!!
include
.
where
;
...
@@ -1326,17 +1326,9 @@ Model.prototype.findAll = function(options) {
...
@@ -1326,17 +1326,9 @@ Model.prototype.findAll = function(options) {
if
(
options
.
attributes
===
undefined
)
{
if
(
options
.
attributes
===
undefined
)
{
options
.
attributes
=
Object
.
keys
(
this
.
tableAttributes
);
options
.
attributes
=
Object
.
keys
(
this
.
tableAttributes
);
}
else
if
(
this
.
_hasVirtualAttributes
)
{
options
.
attributes
.
forEach
(
function
(
attribute
)
{
if
(
this
.
_isVirtualAttribute
(
attribute
)
&&
this
.
rawAttributes
[
attribute
].
type
.
fields
)
{
options
.
attributes
=
options
.
attributes
.
concat
(
this
.
rawAttributes
[
attribute
].
type
.
fields
);
}
}.
bind
(
this
));
options
.
attributes
=
_
.
without
.
apply
(
this
,
[
options
.
attributes
].
concat
(
this
.
_virtualAttributes
));
options
.
attributes
=
_
.
unique
(
options
.
attributes
);
}
}
Utils
.
map
OptionFieldName
s
(
options
,
this
);
Utils
.
map
FinderOption
s
(
options
,
this
);
options
=
paranoidClause
(
this
,
options
);
options
=
paranoidClause
(
this
,
options
);
...
...
lib/utils.js
View file @
8813622
...
@@ -76,6 +76,23 @@ var Utils = module.exports = {
...
@@ -76,6 +76,23 @@ var Utils = module.exports = {
});
});
},
},
/* Expand and normalize finder options */
mapFinderOptions
:
function
(
options
,
Model
)
{
if
(
Model
.
_hasVirtualAttributes
)
{
options
.
attributes
.
forEach
(
function
(
attribute
)
{
if
(
Model
.
_isVirtualAttribute
(
attribute
)
&&
Model
.
rawAttributes
[
attribute
].
type
.
fields
)
{
options
.
attributes
=
options
.
attributes
.
concat
(
Model
.
rawAttributes
[
attribute
].
type
.
fields
);
}
}.
bind
(
Model
));
options
.
attributes
=
_
.
without
.
apply
(
_
,
[
options
.
attributes
].
concat
(
Model
.
_virtualAttributes
));
options
.
attributes
=
_
.
unique
(
options
.
attributes
);
}
Utils
.
mapOptionFieldNames
(
options
,
Model
);
return
options
;
},
/* Used to map field names in attributes and where conditions */
/* Used to map field names in attributes and where conditions */
mapOptionFieldNames
:
function
(
options
,
Model
)
{
mapOptionFieldNames
:
function
(
options
,
Model
)
{
if
(
options
.
attributes
)
{
if
(
options
.
attributes
)
{
...
...
test/unit/utils.js
deleted
100644 → 0
View file @
b7c8e85
'use strict'
;
var
chai
=
require
(
'chai'
)
,
expect
=
chai
.
expect
,
Support
=
require
(
__dirname
+
'/support'
)
,
DataTypes
=
require
(
__dirname
+
'/../../lib/data-types'
)
,
Utils
=
require
(
__dirname
+
'/../../lib/utils'
);
// Notice: [] will be replaced by dialect specific tick/quote character when there is not dialect specific expectation but only a default expectation
suite
(
Support
.
getTestDialectTeaser
(
'Utils'
),
function
()
{
suite
(
'toDefaultValue'
,
function
()
{
test
(
'return plain data types'
,
function
()
{
expect
(
Utils
.
toDefaultValue
(
DataTypes
.
UUIDV4
)).
to
.
equal
(
'UUIDV4'
);
});
test
(
'return uuid v1'
,
function
()
{
expect
(
/^
[
a-z0-9
\-]{36}
$/
.
test
(
Utils
.
toDefaultValue
(
DataTypes
.
UUIDV1
()))).
to
.
be
.
equal
(
true
);
});
test
(
'return uuid v4'
,
function
()
{
expect
(
/^
[
a-z0-9
\-]{36}
/
.
test
(
Utils
.
toDefaultValue
(
DataTypes
.
UUIDV4
()))).
to
.
be
.
equal
(
true
);
});
test
(
'return now'
,
function
()
{
expect
(
Object
.
prototype
.
toString
.
call
(
Utils
.
toDefaultValue
(
DataTypes
.
NOW
()))).
to
.
be
.
equal
(
'[object Date]'
);
});
test
(
'return plain string'
,
function
()
{
expect
(
Utils
.
toDefaultValue
(
'Test'
)).
to
.
equal
(
'Test'
);
});
test
(
'return plain object'
,
function
()
{
chai
.
assert
.
deepEqual
({},
Utils
.
toDefaultValue
({}));
});
});
suite
(
'mapOptionFieldNames'
,
function
()
{
test
(
'plain where'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
where
:
{
firstName
:
'Paul'
,
lastName
:
'Atreides'
}
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
,
field
:
'first_name'
},
lastName
:
{
type
:
DataTypes
.
STRING
,
field
:
'last_name'
}
}))).
to
.
eql
({
where
:
{
first_name
:
'Paul'
,
last_name
:
'Atreides'
}
});
});
test
(
'$or where'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
where
:
{
$or
:
{
firstName
:
'Paul'
,
lastName
:
'Atreides'
}
}
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
,
field
:
'first_name'
},
lastName
:
{
type
:
DataTypes
.
STRING
,
field
:
'last_name'
}
}))).
to
.
eql
({
where
:
{
$or
:
{
first_name
:
'Paul'
,
last_name
:
'Atreides'
}
}
});
});
test
(
'$or[] where'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
where
:
{
$or
:
[
{
firstName
:
'Paul'
},
{
lastName
:
'Atreides'
}
]
}
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
,
field
:
'first_name'
},
lastName
:
{
type
:
DataTypes
.
STRING
,
field
:
'last_name'
}
}))).
to
.
eql
({
where
:
{
$or
:
[
{
first_name
:
'Paul'
},
{
last_name
:
'Atreides'
}
]
}
});
});
test
(
'$and where'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
where
:
{
$and
:
{
firstName
:
'Paul'
,
lastName
:
'Atreides'
}
}
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
,
field
:
'first_name'
},
lastName
:
{
type
:
DataTypes
.
STRING
,
field
:
'last_name'
}
}))).
to
.
eql
({
where
:
{
$and
:
{
first_name
:
'Paul'
,
last_name
:
'Atreides'
}
}
});
});
});
suite
(
'stack'
,
function
()
{
test
(
'stack trace starts after call to Util.stack()'
,
function
this_here_test
()
{
function
a
()
{
return
b
();
}
function
b
()
{
return
c
();
}
function
c
()
{
return
Utils
.
stack
();
}
var
stack
=
a
();
expect
(
stack
[
0
].
getFunctionName
()).
to
.
eql
(
'c'
);
expect
(
stack
[
1
].
getFunctionName
()).
to
.
eql
(
'b'
);
expect
(
stack
[
2
].
getFunctionName
()).
to
.
eql
(
'a'
);
expect
(
stack
[
3
].
getFunctionName
()).
to
.
eql
(
'this_here_test'
);
});
});
});
\ No newline at end of file
test/unit/utils.test.js
View file @
8813622
'use strict'
;
'use strict'
;
/* jshint -W030 */
/* jshint -W110 */
var
chai
=
require
(
'chai'
)
var
chai
=
require
(
'chai'
)
,
expect
=
chai
.
expect
,
expect
=
chai
.
expect
,
Utils
=
require
(
__dirname
+
'/../../lib/utils'
)
,
Support
=
require
(
__dirname
+
'/support'
)
,
Support
=
require
(
__dirname
+
'/support'
);
,
DataTypes
=
require
(
__dirname
+
'/../../lib/data-types'
)
,
Utils
=
require
(
__dirname
+
'/../../lib/utils'
);
describe
(
Support
.
getTestDialectTeaser
(
'Utils'
),
function
()
{
// Notice: [] will be replaced by dialect specific tick/quote character when there is not dialect specific expectation but only a default expectation
describe
(
'formatReferences'
,
function
()
{
suite
(
Support
.
getTestDialectTeaser
(
'Utils'
),
function
()
{
suite
(
'toDefaultValue'
,
function
()
{
test
(
'return plain data types'
,
function
()
{
expect
(
Utils
.
toDefaultValue
(
DataTypes
.
UUIDV4
)).
to
.
equal
(
'UUIDV4'
);
});
test
(
'return uuid v1'
,
function
()
{
expect
(
/^
[
a-z0-9
\-]{36}
$/
.
test
(
Utils
.
toDefaultValue
(
DataTypes
.
UUIDV1
()))).
to
.
be
.
equal
(
true
);
});
test
(
'return uuid v4'
,
function
()
{
expect
(
/^
[
a-z0-9
\-]{36}
/
.
test
(
Utils
.
toDefaultValue
(
DataTypes
.
UUIDV4
()))).
to
.
be
.
equal
(
true
);
});
test
(
'return now'
,
function
()
{
expect
(
Object
.
prototype
.
toString
.
call
(
Utils
.
toDefaultValue
(
DataTypes
.
NOW
()))).
to
.
be
.
equal
(
'[object Date]'
);
});
test
(
'return plain string'
,
function
()
{
expect
(
Utils
.
toDefaultValue
(
'Test'
)).
to
.
equal
(
'Test'
);
});
test
(
'return plain object'
,
function
()
{
chai
.
assert
.
deepEqual
({},
Utils
.
toDefaultValue
({}));
});
});
suite
(
'mapFinderOptions'
,
function
()
{
test
(
'virtual attribute dependencies'
,
function
()
{
expect
(
Utils
.
mapFinderOptions
({
attributes
:
[
'active'
]
},
this
.
sequelize
.
define
(
'User'
,
{
createdAt
:
{
type
:
DataTypes
.
DATE
,
field
:
'created_at'
},
active
:
{
type
:
new
DataTypes
.
VIRTUAL
(
DataTypes
.
BOOLEAN
,
[
'createdAt'
])
}
})).
attributes
).
to
.
eql
([
[
'created_at'
,
'createdAt'
]
]);
});
});
suite
(
'mapOptionFieldNames'
,
function
()
{
test
(
'plain where'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
where
:
{
firstName
:
'Paul'
,
lastName
:
'Atreides'
}
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
,
field
:
'first_name'
},
lastName
:
{
type
:
DataTypes
.
STRING
,
field
:
'last_name'
}
}))).
to
.
eql
({
where
:
{
first_name
:
'Paul'
,
last_name
:
'Atreides'
}
});
});
test
(
'$or where'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
where
:
{
$or
:
{
firstName
:
'Paul'
,
lastName
:
'Atreides'
}
}
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
,
field
:
'first_name'
},
lastName
:
{
type
:
DataTypes
.
STRING
,
field
:
'last_name'
}
}))).
to
.
eql
({
where
:
{
$or
:
{
first_name
:
'Paul'
,
last_name
:
'Atreides'
}
}
});
});
test
(
'$or[] where'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
where
:
{
$or
:
[
{
firstName
:
'Paul'
},
{
lastName
:
'Atreides'
}
]
}
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
,
field
:
'first_name'
},
lastName
:
{
type
:
DataTypes
.
STRING
,
field
:
'last_name'
}
}))).
to
.
eql
({
where
:
{
$or
:
[
{
first_name
:
'Paul'
},
{
last_name
:
'Atreides'
}
]
}
});
});
test
(
'$and where'
,
function
()
{
expect
(
Utils
.
mapOptionFieldNames
({
where
:
{
$and
:
{
firstName
:
'Paul'
,
lastName
:
'Atreides'
}
}
},
this
.
sequelize
.
define
(
'User'
,
{
firstName
:
{
type
:
DataTypes
.
STRING
,
field
:
'first_name'
},
lastName
:
{
type
:
DataTypes
.
STRING
,
field
:
'last_name'
}
}))).
to
.
eql
({
where
:
{
$and
:
{
first_name
:
'Paul'
,
last_name
:
'Atreides'
}
}
});
});
});
suite
(
'stack'
,
function
()
{
test
(
'stack trace starts after call to Util.stack()'
,
function
this_here_test
()
{
function
a
()
{
return
b
();
}
function
b
()
{
return
c
();
}
function
c
()
{
return
Utils
.
stack
();
}
var
stack
=
a
();
expect
(
stack
[
0
].
getFunctionName
()).
to
.
eql
(
'c'
);
expect
(
stack
[
1
].
getFunctionName
()).
to
.
eql
(
'b'
);
expect
(
stack
[
2
].
getFunctionName
()).
to
.
eql
(
'a'
);
expect
(
stack
[
3
].
getFunctionName
()).
to
.
eql
(
'this_here_test'
);
});
});
suite
(
'formatReferences'
,
function
()
{
([
([
[{
referencesKey
:
1
},
{
references
:
{
model
:
undefined
,
key
:
1
,
deferrable
:
undefined
},
referencesKey
:
undefined
,
referencesDeferrable
:
undefined
}],
[{
referencesKey
:
1
},
{
references
:
{
model
:
undefined
,
key
:
1
,
deferrable
:
undefined
},
referencesKey
:
undefined
,
referencesDeferrable
:
undefined
}],
[{
references
:
'a'
},
{
references
:
{
model
:
'a'
,
key
:
undefined
,
deferrable
:
undefined
},
referencesKey
:
undefined
,
referencesDeferrable
:
undefined
}],
[{
references
:
'a'
},
{
references
:
{
model
:
'a'
,
key
:
undefined
,
deferrable
:
undefined
},
referencesKey
:
undefined
,
referencesDeferrable
:
undefined
}],
...
@@ -19,9 +195,9 @@ describe(Support.getTestDialectTeaser('Utils'), function() {
...
@@ -19,9 +195,9 @@ describe(Support.getTestDialectTeaser('Utils'), function() {
var
input
=
test
[
0
];
var
input
=
test
[
0
];
var
output
=
test
[
1
];
var
output
=
test
[
1
];
it
(
'converts '
+
JSON
.
stringify
(
input
)
+
' to '
+
JSON
.
stringify
(
output
),
function
()
{
it
(
JSON
.
stringify
(
input
)
+
' to '
+
JSON
.
stringify
(
output
),
function
()
{
expect
(
Utils
.
formatReferences
(
input
)).
to
.
deep
.
equal
(
output
);
expect
(
Utils
.
formatReferences
(
input
)).
to
.
deep
.
equal
(
output
);
});
});
});
});
});
});
});
});
\ 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