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 b7c8e85a
authored
Sep 02, 2015
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(model/attributes): return type and dependency fields for VIRTUAL
1 parent
ad7f882b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
3 deletions
lib/data-types.js
lib/model.js
test/integration/model/findAll.test.js
lib/data-types.js
View file @
b7c8e85
...
@@ -658,13 +658,29 @@ UUIDV4.prototype.validate = function(value) {
...
@@ -658,13 +658,29 @@ UUIDV4.prototype.validate = function(value) {
* }
* }
* })
* })
* ```
* ```
*
* VIRTUAL also takes a return type and dependency fields as arguments
* If a virtual attribute is present in `attributes` it will automatically pull in the extra fields aswell.
* Return type is mostly usefull for setups that rely on types like GraphQL.
* ```js
* {
* active: {
* type: new DataTypes.VIRTUAL(DataTypes.BOOLEAN, ['createdAt']),
* get: function() {
* return this.get('createdAt') > Date.now() - (7 * 24 * 60 * 60 * 1000)
* }
* }
* }
*
* In the above code the password is stored plainly in the password field so it can be validated, but is never stored in the DB.
* In the above code the password is stored plainly in the password field so it can be validated, but is never stored in the DB.
* @property VIRTUAL
* @property VIRTUAL
* @alias NONE
* @alias NONE
*/
*/
var
VIRTUAL
=
function
()
{
var
VIRTUAL
=
function
(
returnType
,
fields
)
{
if
(
!
(
this
instanceof
VIRTUAL
))
return
new
VIRTUAL
();
if
(
!
(
this
instanceof
VIRTUAL
))
return
new
VIRTUAL
(
returnType
,
fields
);
ABSTRACT
.
apply
(
this
,
arguments
);
this
.
returnType
=
returnType
;
this
.
fields
=
fields
;
};
};
util
.
inherits
(
VIRTUAL
,
ABSTRACT
);
util
.
inherits
(
VIRTUAL
,
ABSTRACT
);
...
...
lib/model.js
View file @
b7c8e85
...
@@ -1326,7 +1326,16 @@ Model.prototype.findAll = function(options) {
...
@@ -1326,7 +1326,16 @@ 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
.
mapOptionFieldNames
(
options
,
this
);
Utils
.
mapOptionFieldNames
(
options
,
this
);
options
=
paranoidClause
(
this
,
options
);
options
=
paranoidClause
(
this
,
options
);
...
...
test/integration/model/findAll.test.js
View file @
b7c8e85
...
@@ -1185,6 +1185,30 @@ describe(Support.getTestDialectTeaser('Model'), function() {
...
@@ -1185,6 +1185,30 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
});
});
});
});
it
(
'should pull in dependent fields for a VIRTUAL'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
active
:
{
type
:
new
Sequelize
.
VIRTUAL
(
Sequelize
.
BOOLEAN
,
[
'createdAt'
]),
get
:
function
()
{
return
this
.
get
(
'createdAt'
)
>
Date
.
now
()
-
(
7
*
24
*
60
*
60
*
1000
);
}
}
},
{
timestamps
:
true
});
return
User
.
create
().
then
(
function
()
{
return
User
.
findAll
({
attributes
:
[
'active'
]
}).
then
(
function
(
users
)
{
users
.
forEach
(
function
(
user
)
{
expect
(
user
.
get
(
'createdAt'
)).
to
.
be
.
ok
;
expect
(
user
.
get
(
'active'
)).
to
.
equal
(
true
);
});
});
});
});
});
});
});
});
...
...
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