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 6d37b085
authored
Mar 06, 2017
by
Sushant
Committed by
GitHub
Mar 06, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
restore setDataValue, fixed set issue with customGetter/Setter (#7328)
1 parent
8fd3fb65
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
64 deletions
lib/model.js
test/integration/model/update.test.js
test/unit/instance/previous.test.js
lib/model.js
View file @
6d37b08
...
@@ -853,7 +853,7 @@ class Model {
...
@@ -853,7 +853,7 @@ class Model {
}
}
}
}
}
}
return
index
;
return
index
;
});
});
...
@@ -2993,12 +2993,11 @@ class Model {
...
@@ -2993,12 +2993,11 @@ class Model {
* @param {any} value
* @param {any} value
*/
*/
setDataValue
(
key
,
value
)
{
setDataValue
(
key
,
value
)
{
const
originalValue
=
this
.
d
ataValues
[
key
];
const
originalValue
=
this
.
_previousD
ataValues
[
key
];
if
(
!
Utils
.
isPrimitive
(
value
)
||
value
!==
originalValue
)
{
if
(
!
Utils
.
isPrimitive
(
value
)
||
value
!==
originalValue
)
{
this
.
changed
(
key
,
true
);
this
.
changed
(
key
,
true
);
}
}
this
.
_previousDataValues
[
key
]
=
originalValue
;
this
.
dataValues
[
key
]
=
value
;
this
.
dataValues
[
key
]
=
value
;
}
}
...
@@ -3142,6 +3141,10 @@ class Model {
...
@@ -3142,6 +3141,10 @@ class Model {
// If not raw, and there's a custom setter
// If not raw, and there's a custom setter
if
(
!
options
.
raw
&&
this
.
_customSetters
[
key
])
{
if
(
!
options
.
raw
&&
this
.
_customSetters
[
key
])
{
this
.
_customSetters
[
key
].
call
(
this
,
value
,
key
);
this
.
_customSetters
[
key
].
call
(
this
,
value
,
key
);
if
(
!
Utils
.
isPrimitive
(
value
)
&&
value
!==
null
||
value
!==
originalValue
)
{
this
.
_previousDataValues
[
key
]
=
originalValue
;
this
.
changed
(
key
,
true
);
}
}
else
{
}
else
{
// Check if we have included models, and if this key matches the include model names/aliases
// Check if we have included models, and if this key matches the include model names/aliases
...
...
test/integration/model/update.test.js
View file @
6d37b08
'use strict'
;
'use strict'
;
/* jshint -W030 */
/* jshint -W030 */
var
Support
=
require
(
__dirname
+
'/../support'
)
const
Support
=
require
(
__dirname
+
'/../support'
);
,
DataTypes
=
require
(
__dirname
+
'/../../../lib/data-types'
)
const
DataTypes
=
require
(
__dirname
+
'/../../../lib/data-types'
);
,
chai
=
require
(
'chai'
)
const
chai
=
require
(
'chai'
);
,
expect
=
chai
.
expect
const
expect
=
chai
.
expect
;
,
Support
=
require
(
__dirname
+
'/../support'
)
const
current
=
Support
.
sequelize
;
,
current
=
Support
.
sequelize
const
_
=
require
(
'lodash'
);
,
_
=
require
(
'lodash'
);
describe
(
Support
.
getTestDialectTeaser
(
'Model'
),
function
()
{
describe
(
Support
.
getTestDialectTeaser
(
'Model'
),
function
()
{
describe
(
'update'
,
function
()
{
describe
(
'update'
,
function
()
{
var
Account
;
beforeEach
(
function
()
{
beforeEach
(
function
()
{
Account
=
this
.
sequelize
.
define
(
'Account'
,
{
this
.
Account
=
this
.
sequelize
.
define
(
'Account'
,
{
ownerId
:
{
ownerId
:
{
type
:
DataTypes
.
INTEGER
,
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
allowNull
:
false
,
...
@@ -25,59 +21,53 @@ describe(Support.getTestDialectTeaser('Model'), function() {
...
@@ -25,59 +21,53 @@ describe(Support.getTestDialectTeaser('Model'), function() {
type
:
DataTypes
.
STRING
type
:
DataTypes
.
STRING
}
}
});
});
return
Account
.
sync
({
force
:
true
});
return
this
.
Account
.
sync
({
force
:
true
});
});
});
it
(
'should only update the passed fields'
,
function
()
{
it
(
'should only update the passed fields'
,
function
()
{
return
Account
.
create
({
return
this
.
Account
ownerId
:
2
.
create
({
ownerId
:
2
})
}).
then
(
function
(
account
)
{
.
then
(
account
=>
this
.
Account
.
update
({
return
Account
.
update
({
name
:
Math
.
random
().
toString
()
name
:
Math
.
random
().
toString
()
},
{
},
{
where
:
{
where
:
{
id
:
account
.
get
(
'id'
)
id
:
account
.
get
(
'id'
)
}
}
});
}));
});
});
});
if
(
_
.
get
(
current
.
dialect
.
supports
,
'returnValues.returning'
))
{
if
(
_
.
get
(
current
.
dialect
.
supports
,
'returnValues.returning'
))
{
it
(
'should return the updated record'
,
function
()
{
it
(
'should return the updated record'
,
function
()
{
return
Account
.
create
({
return
this
.
Account
.
create
({
ownerId
:
2
}).
then
(
account
=>
{
ownerId
:
2
return
this
.
Account
.
update
({
name
:
'FooBar'
},
{
}).
then
(
function
(
account
)
{
return
Account
.
update
({
name
:
'FooBar'
},
{
where
:
{
where
:
{
id
:
account
.
get
(
'id'
)
id
:
account
.
get
(
'id'
)
},
},
returning
:
true
returning
:
true
}).
spread
(
function
(
count
,
accounts
)
{
}).
spread
(
(
count
,
accounts
)
=>
{
var
account
=
accounts
[
0
];
const
firstAcc
=
accounts
[
0
];
expect
(
account
.
ownerId
).
to
.
be
.
equal
(
2
);
expect
(
firstAcc
.
ownerId
).
to
.
be
.
equal
(
2
);
expect
(
account
.
name
).
to
.
be
.
equal
(
'FooBar'
);
expect
(
firstAcc
.
name
).
to
.
be
.
equal
(
'FooBar'
);
});
});
});
});
});
});
}
}
if
(
current
.
dialect
.
supports
[
'LIMIT ON UPDATE'
])
{
if
(
current
.
dialect
.
supports
[
'LIMIT ON UPDATE'
])
{
it
(
'
S
hould only update one row'
,
function
()
{
it
(
'
s
hould only update one row'
,
function
()
{
return
Account
.
create
({
return
this
.
Account
.
create
({
ownerId
:
2
,
ownerId
:
2
,
name
:
'Account Name 1'
name
:
'Account Name 1'
})
})
.
then
(()
=>
{
.
then
(()
=>
{
return
Account
.
create
({
return
this
.
Account
.
create
({
ownerId
:
2
,
ownerId
:
2
,
name
:
'Account Name 2'
name
:
'Account Name 2'
});
});
})
})
.
then
(()
=>
{
.
then
(()
=>
{
return
Account
.
create
({
return
this
.
Account
.
create
({
ownerId
:
2
,
ownerId
:
2
,
name
:
'Account Name 3'
name
:
'Account Name 3'
});
});
...
@@ -89,13 +79,12 @@ describe(Support.getTestDialectTeaser('Model'), function() {
...
@@ -89,13 +79,12 @@ describe(Support.getTestDialectTeaser('Model'), function() {
},
},
limit
:
1
limit
:
1
};
};
return
Account
.
update
({
name
:
'New Name'
},
options
);
return
this
.
Account
.
update
({
name
:
'New Name'
},
options
);
})
})
.
then
(
account
=>
{
.
then
(
account
=>
{
expect
(
account
[
0
]).
to
.
equal
(
1
);
expect
(
account
[
0
]).
to
.
equal
(
1
);
});
});
});
});
}
}
});
});
});
});
test/unit/instance/previous.test.js
View file @
6d37b08
'use strict'
;
'use strict'
;
/* jshint -W030 */
/* jshint -W030 */
var
chai
=
require
(
'chai'
)
const
chai
=
require
(
'chai'
);
,
expect
=
chai
.
expect
const
expect
=
chai
.
expect
;
,
Support
=
require
(
__dirname
+
'/../support'
)
const
Support
=
require
(
__dirname
+
'/../support'
);
,
DataTypes
=
require
(
__dirname
+
'/../../../lib/data-types'
)
const
DataTypes
=
require
(
__dirname
+
'/../../../lib/data-types'
);
,
current
=
Support
.
sequelize
;
const
current
=
Support
.
sequelize
;
describe
(
Support
.
getTestDialectTeaser
(
'Instance'
),
function
()
{
describe
(
Support
.
getTestDialectTeaser
(
'Instance'
),
function
()
{
describe
(
'previous'
,
function
()
{
describe
(
'previous'
,
function
()
{
it
(
'should return correct previous value'
,
function
()
{
it
(
'should return correct previous value'
,
function
()
{
var
Model
=
current
.
define
(
'Model'
,
{
const
Model
=
current
.
define
(
'Model'
,
{
text
:
{
text
:
DataTypes
.
STRING
,
type
:
DataTypes
.
STRING
,
textCustom
:
{
get
:
function
(
name
)
{
type
:
DataTypes
.
STRING
,
return
this
.
getDataValue
(
name
);
set
(
val
)
{
},
this
.
setDataValue
(
'textCustom'
,
val
);
set
:
function
(
value
,
name
)
{
},
this
.
setDataValue
(
name
,
value
);
get
()
{
}
this
.
getDataValue
(
'textCustom'
);
}
}
})
}
,
instance
,
shouldBeEmpty
,
shouldBeA
;
instance
=
Model
.
build
({
text
:
'a'
},
{
isNewRecord
:
false
});
});
shouldBeEmpty
=
instance
.
previous
(
'text'
);
const
instance
=
Model
.
build
({
text
:
'a'
,
textCustom
:
'abc'
});
expect
(
instance
.
previous
(
'text'
)).
to
.
be
.
not
.
ok
;
expect
(
instance
.
previous
(
'textCustom'
)).
to
.
be
.
not
.
ok
;
instance
.
set
(
'text'
,
'b'
);
instance
.
set
(
'text'
,
'b'
);
instance
.
set
(
'textCustom'
,
'def'
);
shouldBeA
=
instance
.
previous
(
'text'
);
expect
(
instance
.
previous
(
'text'
)).
to
.
be
.
equal
(
'a'
);
expect
(
instance
.
previous
(
'textCustom'
)).
to
.
be
.
equal
(
'abc'
);
expect
(
shouldBeEmpty
).
to
.
be
.
not
.
ok
;
expect
(
shouldBeA
).
to
.
be
.
equal
(
'a'
);
});
});
});
});
});
});
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