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 6817aad8
authored
Jan 14, 2015
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validation for upsert. Closes #2676
1 parent
f5e3448a
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
4 deletions
changelog.md
lib/model.js
test/model/update.test.js → test/model/upsert.test.js
changelog.md
View file @
6817aad
# Next
# Next
-
[
BUG
]
Fixed issue with paranoid deletes and
`deletedAt`
with a custom field.
-
[
BUG
]
Fixed issue with paranoid deletes and
`deletedAt`
with a custom field.
-
[
BUG
]
No longer crahes on
`where: []`
-
[
BUG
]
No longer crahes on
`where: []`
-
[
FEATURE
]
Validations are now enabled by default for upsert.
# 2.0.0-rc7
# 2.0.0-rc7
-
[
FEATURE
]
Throw an error if no where clause is given to
`Model.destroy()`
.
-
[
FEATURE
]
Throw an error if no where clause is given to
`Model.destroy()`
.
...
...
lib/model.js
View file @
6817aad
...
@@ -1197,6 +1197,7 @@ module.exports = (function() {
...
@@ -1197,6 +1197,7 @@ module.exports = (function() {
*
*
* @param {Object} values
* @param {Object} values
* @param {Object} [options]
* @param {Object} [options]
* @param {Boolean}[options.validate=true] Run validations before the row is insertedF
* @param {Array} [options.fields=Object.keys(this.attributes)] The fields to insert / update. Defaults to all fields
* @param {Array} [options.fields=Object.keys(this.attributes)] The fields to insert / update. Defaults to all fields
*
*
* @alias insertOrUpdate
* @alias insertOrUpdate
...
@@ -1211,10 +1212,14 @@ module.exports = (function() {
...
@@ -1211,10 +1212,14 @@ module.exports = (function() {
var
createdAtAttr
=
this
.
_timestampAttributes
.
createdAt
var
createdAtAttr
=
this
.
_timestampAttributes
.
createdAt
,
updatedAtAttr
=
this
.
_timestampAttributes
.
updatedAt
,
updatedAtAttr
=
this
.
_timestampAttributes
.
updatedAt
,
hadPrimary
=
this
.
primaryKeyField
in
values
;
,
hadPrimary
=
this
.
primaryKeyField
in
values
,
instance
;
values
=
Utils
.
_
.
pick
(
values
,
options
.
fields
);
values
=
Utils
.
_
.
pick
(
values
,
options
.
fields
);
values
=
this
.
build
(
values
).
get
();
// Get default values - this also adds a null value for the PK if none is given
instance
=
this
.
build
(
values
);
return
instance
.
hookValidate
(
options
).
bind
(
this
).
then
(
function
()
{
values
=
instance
.
get
();
// Get default values - this also adds a null value for the PK if none is given
if
(
createdAtAttr
&&
!
values
[
createdAtAttr
])
{
if
(
createdAtAttr
&&
!
values
[
createdAtAttr
])
{
values
[
createdAtAttr
]
=
this
.
__getTimestamp
(
createdAtAttr
);
values
[
createdAtAttr
]
=
this
.
__getTimestamp
(
createdAtAttr
);
...
@@ -1230,6 +1235,7 @@ module.exports = (function() {
...
@@ -1230,6 +1235,7 @@ module.exports = (function() {
}
}
return
this
.
QueryInterface
.
upsert
(
this
.
getTableName
(
options
),
values
,
this
,
options
);
return
this
.
QueryInterface
.
upsert
(
this
.
getTableName
(
options
),
values
,
this
,
options
);
});
};
};
Model
.
prototype
.
insertOrUpdate
=
Model
.
prototype
.
upsert
;
Model
.
prototype
.
insertOrUpdate
=
Model
.
prototype
.
upsert
;
...
...
test/model/up
date
.test.js
→
test/model/up
sert
.test.js
View file @
6817aad
...
@@ -40,7 +40,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
...
@@ -40,7 +40,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
if
(
current
.
dialect
.
supports
.
upserts
)
{
if
(
current
.
dialect
.
supports
.
upserts
)
{
describe
(
'upsert'
,
function
()
{
describe
.
only
(
'upsert'
,
function
()
{
it
(
'works with upsert on id'
,
function
()
{
it
(
'works with upsert on id'
,
function
()
{
return
this
.
User
.
upsert
({
id
:
42
,
username
:
'john'
}).
bind
(
this
).
then
(
function
(
created
)
{
return
this
.
User
.
upsert
({
id
:
42
,
username
:
'john'
}).
bind
(
this
).
then
(
function
(
created
)
{
if
(
dialect
===
'sqlite'
)
{
if
(
dialect
===
'sqlite'
)
{
...
@@ -90,7 +90,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
...
@@ -90,7 +90,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect
(
user
.
updatedAt
).
to
.
be
.
afterTime
(
user
.
createdAt
);
expect
(
user
.
updatedAt
).
to
.
be
.
afterTime
(
user
.
createdAt
);
});
});
});
});
});
it
(
'supports validations'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'user'
,
{
email
:
{
type
:
Sequelize
.
STRING
,
validate
:
{
isEmail
:
true
}
}
}
});
return
expect
(
User
.
upsert
({
email
:
'notanemail'
})).
to
.
eventually
.
be
.
rejectedWith
(
this
.
sequelize
.
ValidationError
);
});
});
}
});
});
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