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 7d6d2cce
authored
Jun 23, 2015
by
Fred K. Schott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create upsert updateValues using information about changed fields
1 parent
fffd1a39
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
21 deletions
lib/model.js
lib/query-interface.js
test/integration/model/upsert.test.js
lib/model.js
View file @
7d6d2cc
...
@@ -1706,24 +1706,27 @@ Model.prototype.upsert = function (values, options) {
...
@@ -1706,24 +1706,27 @@ Model.prototype.upsert = function (values, options) {
return
instance
.
hookValidate
(
options
).
bind
(
this
).
then
(
function
()
{
return
instance
.
hookValidate
(
options
).
bind
(
this
).
then
(
function
()
{
// Map field names
// Map field names
values
=
Utils
.
mapValueFieldNames
(
instance
.
dataValues
,
options
.
fields
,
this
);
var
updatedDataValues
=
_
.
pick
(
instance
.
dataValues
,
Object
.
keys
(
instance
.
_changed
))
,
insertValues
=
Utils
.
mapValueFieldNames
(
instance
.
dataValues
,
options
.
fields
,
this
)
,
updateValues
=
Utils
.
mapValueFieldNames
(
updatedDataValues
,
options
.
fields
,
this
)
,
now
=
Utils
.
now
(
this
.
sequelize
.
options
.
dialect
);
var
now
=
Utils
.
now
(
this
.
sequelize
.
options
.
dialect
);
// Attach createdAt
if
(
createdAtAttr
&&
!
updateValues
[
createdAtAttr
])
{
if
(
createdAtAttr
&&
!
values
[
createdAtAttr
])
{
insertValues
[
createdAtAttr
]
=
this
.
$getDefaultTimestamp
(
createdAtAttr
)
||
now
;
values
[
createdAtAttr
]
=
this
.
$getDefaultTimestamp
(
createdAtAttr
)
||
now
;
}
}
if
(
updatedAtAttr
&&
!
v
alues
[
updatedAtAttr
])
{
if
(
updatedAtAttr
&&
!
insertV
alues
[
updatedAtAttr
])
{
v
alues
[
updatedAtAttr
]
=
this
.
$getDefaultTimestamp
(
updatedAtAttr
)
||
now
;
insertValues
[
updatedAtAttr
]
=
updateV
alues
[
updatedAtAttr
]
=
this
.
$getDefaultTimestamp
(
updatedAtAttr
)
||
now
;
}
}
// Build adds a null value for the primary key, if none was given by the user.
// 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.
// We need to remove that because of some Postgres technicalities.
if
(
!
hadPrimary
&&
this
.
primaryKeyAttribute
&&
!
this
.
rawAttributes
[
this
.
primaryKeyAttribute
].
defaultValue
)
{
if
(
!
hadPrimary
&&
this
.
primaryKeyAttribute
&&
!
this
.
rawAttributes
[
this
.
primaryKeyAttribute
].
defaultValue
)
{
delete
values
[
this
.
primaryKeyField
];
delete
insertValues
[
this
.
primaryKeyField
];
delete
updateValues
[
this
.
primaryKeyField
];
}
}
return
this
.
QueryInterface
.
upsert
(
this
.
getTableName
(
options
),
v
alues
,
this
,
options
);
return
this
.
QueryInterface
.
upsert
(
this
.
getTableName
(
options
),
insertValues
,
updateV
alues
,
this
,
options
);
});
});
};
};
...
...
lib/query-interface.js
View file @
7d6d2cc
...
@@ -507,12 +507,11 @@ QueryInterface.prototype.insert = function(instance, tableName, values, options)
...
@@ -507,12 +507,11 @@ QueryInterface.prototype.insert = function(instance, tableName, values, options)
});
});
};
};
QueryInterface
.
prototype
.
upsert
=
function
(
tableName
,
values
,
model
,
options
)
{
QueryInterface
.
prototype
.
upsert
=
function
(
tableName
,
values
,
updateValues
,
model
,
options
)
{
var
wheres
=
[]
var
wheres
=
[]
,
where
,
where
,
indexFields
,
indexFields
,
indexes
=
[]
,
indexes
=
[]
,
updateValues
,
attributes
=
Object
.
keys
(
values
);
,
attributes
=
Object
.
keys
(
values
);
where
=
{};
where
=
{};
...
@@ -560,16 +559,6 @@ QueryInterface.prototype.upsert = function(tableName, values, model, options) {
...
@@ -560,16 +559,6 @@ QueryInterface.prototype.upsert = function(tableName, values, model, options) {
options
.
type
=
QueryTypes
.
UPSERT
;
options
.
type
=
QueryTypes
.
UPSERT
;
options
.
raw
=
true
;
options
.
raw
=
true
;
if
(
model
.
_timestampAttributes
.
createdAt
)
{
// If we are updating an existing row, we shouldn't set createdAt
updateValues
=
Utils
.
cloneDeep
(
values
);
delete
updateValues
[
model
.
_timestampAttributes
.
createdAt
];
}
else
{
updateValues
=
values
;
}
var
sql
=
this
.
QueryGenerator
.
upsertQuery
(
tableName
,
values
,
updateValues
,
where
,
model
.
rawAttributes
,
options
);
var
sql
=
this
.
QueryGenerator
.
upsertQuery
(
tableName
,
values
,
updateValues
,
where
,
model
.
rawAttributes
,
options
);
return
this
.
sequelize
.
query
(
sql
,
options
).
then
(
function
(
rowCount
)
{
return
this
.
sequelize
.
query
(
sql
,
options
).
then
(
function
(
rowCount
)
{
if
(
rowCount
===
undefined
)
{
if
(
rowCount
===
undefined
)
{
...
...
test/integration/model/upsert.test.js
View file @
7d6d2cc
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
/* jshint -W030 */
/* jshint -W030 */
var
chai
=
require
(
'chai'
)
var
chai
=
require
(
'chai'
)
,
sinon
=
require
(
'sinon'
)
,
Sequelize
=
require
(
'../../../index'
)
,
Sequelize
=
require
(
'../../../index'
)
,
Promise
=
Sequelize
.
Promise
,
Promise
=
Sequelize
.
Promise
,
expect
=
chai
.
expect
,
expect
=
chai
.
expect
...
@@ -249,6 +250,26 @@ describe(Support.getTestDialectTeaser('Model'), function() {
...
@@ -249,6 +250,26 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
});
});
it
(
'does not overwrite createdAt time on update'
,
function
()
{
var
originalCreatedAt
;
var
originalUpdatedAt
;
var
clock
=
sinon
.
useFakeTimers
();
return
this
.
User
.
create
({
id
:
42
,
username
:
'john'
}).
bind
(
this
).
then
(
function
()
{
return
this
.
User
.
findById
(
42
);
}).
then
(
function
(
user
)
{
originalCreatedAt
=
user
.
createdAt
;
originalUpdatedAt
=
user
.
updatedAt
;
clock
.
tick
(
5000
);
return
this
.
User
.
upsert
({
id
:
42
,
username
:
'doe'
});
}).
then
(
function
()
{
return
this
.
User
.
findById
(
42
);
}).
then
(
function
(
user
)
{
expect
(
user
.
updatedAt
).
to
.
be
.
gt
(
originalUpdatedAt
);
expect
(
user
.
createdAt
).
to
.
deep
.
equal
(
originalCreatedAt
);
clock
.
restore
();
});
});
it
(
'does not update using default values'
,
function
()
{
it
(
'does not update using default values'
,
function
()
{
return
this
.
User
.
create
({
id
:
42
,
username
:
'john'
,
baz
:
'new baz value'
}).
bind
(
this
).
then
(
function
()
{
return
this
.
User
.
create
({
id
:
42
,
username
:
'john'
,
baz
:
'new baz value'
}).
bind
(
this
).
then
(
function
()
{
return
this
.
User
.
findById
(
42
);
return
this
.
User
.
findById
(
42
);
...
...
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