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 5ce5890b
authored
Jan 17, 2015
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(*): fixes a few bugs with validate and hooks, closes #2934
1 parent
64d13eaf
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
72 additions
and
31 deletions
lib/instance.js
lib/model.js
test/instance.test.js
test/model/create.test.js
lib/instance.js
View file @
5ce5890
...
...
@@ -507,7 +507,15 @@ module.exports = (function() {
// Validate
if
(
options
.
validate
)
{
options
.
skip
=
_
.
difference
(
Object
.
keys
(
this
.
Model
.
rawAttributes
),
options
.
fields
);
return
(
options
.
hooks
?
this
.
hookValidate
(
options
)
:
this
.
validate
(
options
)).
then
(
function
()
{
return
Promise
.
bind
(
this
).
then
(
function
()
{
// hookValidate rejects with errors, validate returns with errors
if
(
options
.
hooks
)
return
this
.
hookValidate
(
options
);
return
this
.
validate
(
options
).
then
(
function
(
err
)
{
if
(
err
)
throw
err
;
});
}).
then
(
function
()
{
delete
options
.
skip
;
});
}
...
...
@@ -539,15 +547,21 @@ module.exports = (function() {
}
if
(
hookChanged
)
{
return
Promise
.
bind
(
this
).
then
(
function
(
)
{
if
(
options
.
validate
)
{
// Validate again
if
(
options
.
validate
)
{
options
.
skip
=
_
.
difference
(
Object
.
keys
(
this
.
Model
.
rawAttributes
),
hookChanged
);
return
(
options
.
hooks
?
this
.
hookValidate
(
options
)
:
this
.
validate
(
options
)).
then
(
function
()
{
delete
options
.
skip
;
options
.
skip
=
_
.
difference
(
Object
.
keys
(
this
.
Model
.
rawAttributes
),
hookChanged
);
return
Promise
.
bind
(
this
).
then
(
function
()
{
// hookValidate rejects with errors, validate returns with errors
if
(
options
.
hooks
)
return
this
.
hookValidate
(
options
);
return
this
.
validate
(
options
).
then
(
function
(
err
)
{
if
(
err
)
throw
err
;
});
}
});
}).
then
(
function
()
{
delete
options
.
skip
;
});
}
}
});
}
...
...
lib/model.js
View file @
5ce5890
...
...
@@ -1303,21 +1303,21 @@ module.exports = (function() {
,
now
=
Utils
.
now
(
self
.
modelManager
.
sequelize
.
options
.
dialect
);
// build DAOs
var
dao
s
=
records
.
map
(
function
(
values
)
{
var
instance
s
=
records
.
map
(
function
(
values
)
{
return
self
.
build
(
values
,
{
isNewRecord
:
true
});
});
return
Promise
.
try
(
function
()
{
// Run before hook
if
(
options
.
hooks
)
{
return
self
.
runHooks
(
'beforeBulkCreate'
,
dao
s
,
options
);
return
self
.
runHooks
(
'beforeBulkCreate'
,
instance
s
,
options
);
}
}).
then
(
function
()
{
daos
.
forEach
(
function
(
dao
)
{
instances
.
forEach
(
function
(
instance
)
{
// Filter dataValues by options.fields
var
values
=
{};
options
.
fields
.
forEach
(
function
(
field
)
{
values
[
field
]
=
dao
.
dataValues
[
field
];
values
[
field
]
=
instance
.
dataValues
[
field
];
});
// set createdAt/updatedAt attributes
...
...
@@ -1328,7 +1328,7 @@ module.exports = (function() {
values
[
updatedAtAttr
]
=
now
;
}
dao
.
dataValues
=
values
;
instance
.
dataValues
=
values
;
});
// Validate
...
...
@@ -1336,13 +1336,17 @@ module.exports = (function() {
options
.
skip
=
Utils
.
_
.
difference
(
Object
.
keys
(
self
.
attributes
),
options
.
fields
);
var
errors
=
[];
return
Promise
.
map
(
daos
,
function
(
dao
)
{
var
fn
=
options
.
individualHooks
?
'hookValidate'
:
'validate'
;
return
dao
[
fn
](
options
).
then
(
function
(
err
)
{
if
(
!!
err
)
{
errors
.
push
({
record
:
dao
,
errors
:
err
});
}
});
return
Promise
.
map
(
instances
,
function
(
instance
)
{
// hookValidate rejects with errors, validate returns with errors
if
(
options
.
individualHooks
)
{
return
instance
.
hookValidate
(
options
);
}
else
{
return
instance
.
validate
(
options
).
then
(
function
(
err
)
{
if
(
err
)
{
errors
.
push
({
record
:
instance
,
errors
:
err
});
}
});
}
}).
then
(
function
()
{
delete
options
.
skip
;
if
(
errors
.
length
)
{
...
...
@@ -1352,8 +1356,8 @@ module.exports = (function() {
}
}).
then
(
function
()
{
if
(
options
.
individualHooks
)
{
// Create each
dao
individually
return
Promise
.
map
(
daos
,
function
(
dao
)
{
// Create each
instance
individually
return
Promise
.
map
(
instances
,
function
(
instance
)
{
var
individualOptions
=
Utils
.
_
.
clone
(
options
);
delete
individualOptions
.
fields
;
delete
individualOptions
.
individualHooks
;
...
...
@@ -1361,15 +1365,15 @@ module.exports = (function() {
individualOptions
.
validate
=
false
;
individualOptions
.
hooks
=
true
;
return
dao
.
save
(
individualOptions
);
}).
then
(
function
(
_
dao
s
)
{
daos
=
_dao
s
;
return
instance
.
save
(
individualOptions
);
}).
then
(
function
(
_
instance
s
)
{
instances
=
_instance
s
;
});
}
else
{
// Create all in one query
// Recreate records from
dao
s to represent any changes made in hooks or validation
records
=
daos
.
map
(
function
(
dao
)
{
return
Utils
.
_
.
omit
(
dao
.
dataValues
,
self
.
_virtualAttributes
);
// Recreate records from
instance
s to represent any changes made in hooks or validation
records
=
instances
.
map
(
function
(
instance
)
{
return
Utils
.
_
.
omit
(
instance
.
dataValues
,
self
.
_virtualAttributes
);
});
var
rawAttribute
;
...
...
@@ -1400,7 +1404,7 @@ module.exports = (function() {
return
self
.
QueryInterface
.
bulkInsert
(
self
.
getTableName
(
options
),
records
,
options
,
attributes
).
then
(
function
(
results
)
{
if
(
Array
.
isArray
(
results
))
{
results
.
forEach
(
function
(
result
,
i
)
{
dao
s
[
i
].
set
(
self
.
primaryKeyAttribute
,
result
[
self
.
rawAttributes
[
self
.
primaryKeyAttribute
].
field
],
{
raw
:
true
});
instance
s
[
i
].
set
(
self
.
primaryKeyAttribute
,
result
[
self
.
rawAttributes
[
self
.
primaryKeyAttribute
].
field
],
{
raw
:
true
});
});
}
return
results
;
...
...
@@ -1409,10 +1413,10 @@ module.exports = (function() {
}).
then
(
function
()
{
// Run after hook
if
(
options
.
hooks
)
{
return
self
.
runHooks
(
'afterBulkCreate'
,
dao
s
,
options
);
return
self
.
runHooks
(
'afterBulkCreate'
,
instance
s
,
options
);
}
}).
then
(
function
()
{
return
dao
s
;
return
instance
s
;
});
};
...
...
test/instance.test.js
View file @
5ce5890
...
...
@@ -1121,6 +1121,17 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
});
});
it
(
'should fail a validation upon creating with hooks false'
,
function
(
done
)
{
this
.
User
.
create
({
aNumber
:
0
,
validateTest
:
'hello'
},
{
hooks
:
false
}).
error
(
function
(
err
)
{
expect
(
err
).
to
.
exist
;
expect
(
err
).
to
.
be
.
instanceof
(
Object
);
expect
(
err
.
get
(
'validateTest'
)).
to
.
be
.
instanceof
(
Array
);
expect
(
err
.
get
(
'validateTest'
)[
0
]).
to
.
exist
;
expect
(
err
.
get
(
'validateTest'
)[
0
].
message
).
to
.
equal
(
'Validation isInt failed'
);
done
();
});
});
it
(
'should fail a validation upon building'
,
function
(
done
)
{
this
.
User
.
build
({
aNumber
:
0
,
validateCustom
:
'aaaaaaaaaaaaaaaaaaaaaaaaaa'
}).
save
()
.
error
(
function
(
err
)
{
...
...
test/model/create.test.js
View file @
5ce5890
...
...
@@ -1191,6 +1191,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
it
(
'should not fail on validate: true and individualHooks: true'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'user'
,
{
name
:
Sequelize
.
STRING
});
return
User
.
sync
({
force
:
true
}).
then
(
function
()
{
return
User
.
bulkCreate
([
{
name
:
'James'
}
],
{
validate
:
true
,
individualHooks
:
true
});
});
});
it
(
'properly handles disparate field lists'
,
function
(
done
)
{
var
self
=
this
,
data
=
[{
username
:
'Peter'
,
secretValue
:
'42'
,
uniqueName
:
'1'
},
...
...
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