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
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
16 deletions
changelog.md
lib/model.js
test/model/update.test.js → test/model/upsert.test.js
changelog.md
View file @
6817aad
# Next
-
[
BUG
]
Fixed issue with paranoid deletes and
`deletedAt`
with a custom field.
-
[
BUG
]
No longer crahes on
`where: []`
-
[
FEATURE
]
Validations are now enabled by default for upsert.
# 2.0.0-rc7
-
[
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() {
*
* @param {Object} values
* @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
*
* @alias insertOrUpdate
...
...
@@ -1211,25 +1212,30 @@ module.exports = (function() {
var
createdAtAttr
=
this
.
_timestampAttributes
.
createdAt
,
updatedAtAttr
=
this
.
_timestampAttributes
.
updatedAt
,
hadPrimary
=
this
.
primaryKeyField
in
values
;
,
hadPrimary
=
this
.
primaryKeyField
in
values
,
instance
;
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
);
if
(
createdAtAttr
&&
!
values
[
createdAtAttr
])
{
values
[
createdAtAttr
]
=
this
.
__getTimestamp
(
createdAtAttr
);
}
if
(
updatedAtAttr
&&
!
values
[
updatedAtAttr
])
{
values
[
updatedAtAttr
]
=
this
.
__getTimestamp
(
updatedAtAttr
);
}
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
// Build adds a null value for the primary key, if none was given by the user.
// We need to remove that because of some Postgres technicalities.
if
(
!
hadPrimary
)
{
delete
values
[
this
.
primaryKeyField
];
}
if
(
createdAtAttr
&&
!
values
[
createdAtAttr
])
{
values
[
createdAtAttr
]
=
this
.
__getTimestamp
(
createdAtAttr
);
}
if
(
updatedAtAttr
&&
!
values
[
updatedAtAttr
])
{
values
[
updatedAtAttr
]
=
this
.
__getTimestamp
(
updatedAtAttr
);
}
// Build adds a null value for the primary key, if none was given by the user.
// We need to remove that because of some Postgres technicalities.
if
(
!
hadPrimary
)
{
delete
values
[
this
.
primaryKeyField
];
}
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
;
...
...
test/model/up
date
.test.js
→
test/model/up
sert
.test.js
View file @
6817aad
...
...
@@ -40,7 +40,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
if
(
current
.
dialect
.
supports
.
upserts
)
{
describe
(
'upsert'
,
function
()
{
describe
.
only
(
'upsert'
,
function
()
{
it
(
'works with upsert on id'
,
function
()
{
return
this
.
User
.
upsert
({
id
:
42
,
username
:
'john'
}).
bind
(
this
).
then
(
function
(
created
)
{
if
(
dialect
===
'sqlite'
)
{
...
...
@@ -90,7 +90,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
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