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 b61b3e83
authored
Jan 10, 2015
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: make paranoid destroy work with field, fixes #2879
1 parent
dad54ad6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
87 additions
and
69 deletions
lib/instance.js
lib/model.js
test/model/attributes/field.test.js
lib/instance.js
View file @
b61b3e8
...
@@ -483,9 +483,9 @@ module.exports = (function() {
...
@@ -483,9 +483,9 @@ module.exports = (function() {
}
}
var
self
=
this
var
self
=
this
,
values
=
{}
,
updatedAtAttr
=
this
.
Model
.
_timestampAttributes
.
updatedAt
,
updatedAtAttr
=
this
.
Model
.
_timestampAttributes
.
updatedAt
,
createdAtAttr
=
this
.
Model
.
_timestampAttributes
.
createdAt
;
,
createdAtAttr
=
this
.
Model
.
_timestampAttributes
.
createdAt
,
hook
=
self
.
isNewRecord
?
'Create'
:
'Update'
;
if
(
updatedAtAttr
&&
options
.
fields
.
indexOf
(
updatedAtAttr
)
===
-
1
)
{
if
(
updatedAtAttr
&&
options
.
fields
.
indexOf
(
updatedAtAttr
)
===
-
1
)
{
options
.
fields
.
push
(
updatedAtAttr
);
options
.
fields
.
push
(
updatedAtAttr
);
...
@@ -512,54 +512,6 @@ module.exports = (function() {
...
@@ -512,54 +512,6 @@ module.exports = (function() {
});
});
}
}
}).
then
(
function
()
{
}).
then
(
function
()
{
options
.
fields
.
forEach
(
function
(
field
)
{
if
(
self
.
dataValues
[
field
]
!==
undefined
)
{
values
[
field
]
=
self
.
dataValues
[
field
];
}
});
if
(
updatedAtAttr
&&
!
options
.
silent
)
{
values
[
updatedAtAttr
]
=
self
.
Model
.
__getTimestamp
(
updatedAtAttr
);
}
if
(
self
.
isNewRecord
&&
createdAtAttr
&&
!
values
[
createdAtAttr
])
{
values
[
createdAtAttr
]
=
self
.
Model
.
__getTimestamp
(
createdAtAttr
);
}
var
query
=
null
,
args
=
[]
,
hook
=
''
;
if
(
self
.
isNewRecord
)
{
query
=
'insert'
;
args
=
[
self
,
self
.
Model
.
getTableName
(
options
),
values
,
options
];
hook
=
'Create'
;
}
else
{
var
identifier
=
self
.
primaryKeyValues
;
if
(
identifier
)
{
for
(
var
attrName
in
identifier
)
{
// Field name mapping
var
rawAttribute
=
self
.
Model
.
rawAttributes
[
attrName
];
if
(
rawAttribute
.
field
&&
rawAttribute
.
field
!==
rawAttribute
.
fieldName
)
{
identifier
[
self
.
Model
.
rawAttributes
[
attrName
].
field
]
=
identifier
[
attrName
];
delete
identifier
[
attrName
];
}
}
}
if
(
identifier
===
null
&&
self
.
__options
.
whereCollection
!==
null
)
{
identifier
=
self
.
__options
.
whereCollection
;
}
query
=
'update'
;
args
=
[
self
,
self
.
Model
.
getTableName
(
options
),
values
,
identifier
,
options
];
hook
=
'Update'
;
}
// Add the values to the Instance
self
.
dataValues
=
_
.
extend
(
self
.
dataValues
,
values
);
return
Promise
.
bind
(
this
).
then
(
function
()
{
return
Promise
.
bind
(
this
).
then
(
function
()
{
// Run before hook
// Run before hook
if
(
options
.
hooks
)
{
if
(
options
.
hooks
)
{
...
@@ -584,24 +536,7 @@ module.exports = (function() {
...
@@ -584,24 +536,7 @@ module.exports = (function() {
});
});
options
.
fields
=
_
.
unique
(
options
.
fields
.
concat
(
hookChanged
));
options
.
fields
=
_
.
unique
(
options
.
fields
.
concat
(
hookChanged
));
}
}
// dataValues might have changed inside the hook, rebuild the values hash
values
=
{};
options
.
fields
.
forEach
(
function
(
attr
)
{
if
(
this
.
dataValues
[
attr
]
!==
undefined
&&
!
this
.
Model
.
_isVirtualAttribute
(
attr
))
{
values
[
attr
]
=
this
.
dataValues
[
attr
];
}
// Field name mapping
if
(
this
.
Model
.
rawAttributes
[
attr
]
&&
this
.
Model
.
rawAttributes
[
attr
].
field
&&
this
.
Model
.
rawAttributes
[
attr
].
field
!==
attr
)
{
values
[
this
.
Model
.
rawAttributes
[
attr
].
field
]
=
values
[
attr
];
delete
values
[
attr
];
}
},
this
);
args
[
2
]
=
values
;
if
(
hookChanged
)
{
if
(
hookChanged
)
{
return
Promise
.
bind
(
this
).
then
(
function
()
{
return
Promise
.
bind
(
this
).
then
(
function
()
{
...
@@ -619,6 +554,67 @@ module.exports = (function() {
...
@@ -619,6 +554,67 @@ module.exports = (function() {
}).
then
(
function
()
{
}).
then
(
function
()
{
if
(
!
options
.
fields
.
length
)
return
this
;
if
(
!
options
.
fields
.
length
)
return
this
;
var
values
=
{}
,
query
=
null
,
args
=
[];
options
.
fields
.
forEach
(
function
(
attr
)
{
if
(
this
.
dataValues
[
attr
]
!==
undefined
&&
!
this
.
Model
.
_isVirtualAttribute
(
attr
))
{
values
[
attr
]
=
this
.
dataValues
[
attr
];
}
// Field name mapping
if
(
this
.
Model
.
rawAttributes
[
attr
]
&&
this
.
Model
.
rawAttributes
[
attr
].
field
&&
this
.
Model
.
rawAttributes
[
attr
].
field
!==
attr
)
{
values
[
this
.
Model
.
rawAttributes
[
attr
].
field
]
=
values
[
attr
];
delete
values
[
attr
];
}
},
this
);
if
(
updatedAtAttr
&&
!
options
.
silent
)
{
self
.
dataValues
[
updatedAtAttr
]
=
values
[
self
.
Model
.
rawAttributes
[
updatedAtAttr
].
field
||
updatedAtAttr
]
=
self
.
Model
.
__getTimestamp
(
updatedAtAttr
);
}
if
(
self
.
isNewRecord
&&
createdAtAttr
&&
!
values
[
createdAtAttr
])
{
self
.
dataValues
[
createdAtAttr
]
=
values
[
self
.
Model
.
rawAttributes
[
createdAtAttr
].
field
||
createdAtAttr
]
=
self
.
Model
.
__getTimestamp
(
createdAtAttr
);
}
options
.
fields
.
forEach
(
function
(
attr
)
{
if
(
this
.
dataValues
[
attr
]
!==
undefined
&&
!
this
.
Model
.
_isVirtualAttribute
(
attr
))
{
values
[
attr
]
=
this
.
dataValues
[
attr
];
}
// Field name mapping
if
(
this
.
Model
.
rawAttributes
[
attr
]
&&
this
.
Model
.
rawAttributes
[
attr
].
field
&&
this
.
Model
.
rawAttributes
[
attr
].
field
!==
attr
)
{
values
[
this
.
Model
.
rawAttributes
[
attr
].
field
]
=
values
[
attr
];
delete
values
[
attr
];
}
},
this
);
if
(
self
.
isNewRecord
)
{
query
=
'insert'
;
args
=
[
self
,
self
.
Model
.
getTableName
(
options
),
values
,
options
];
}
else
{
var
identifier
=
self
.
primaryKeyValues
;
if
(
identifier
)
{
for
(
var
attrName
in
identifier
)
{
// Field name mapping
var
rawAttribute
=
self
.
Model
.
rawAttributes
[
attrName
];
if
(
rawAttribute
.
field
&&
rawAttribute
.
field
!==
rawAttribute
.
fieldName
)
{
identifier
[
self
.
Model
.
rawAttributes
[
attrName
].
field
]
=
identifier
[
attrName
];
delete
identifier
[
attrName
];
}
}
}
if
(
identifier
===
null
&&
self
.
__options
.
whereCollection
!==
null
)
{
identifier
=
self
.
__options
.
whereCollection
;
}
query
=
'update'
;
args
=
[
self
,
self
.
Model
.
getTableName
(
options
),
values
,
identifier
,
options
];
}
return
self
.
QueryInterface
[
query
].
apply
(
self
.
QueryInterface
,
args
)
return
self
.
QueryInterface
[
query
].
apply
(
self
.
QueryInterface
,
args
)
.
then
(
function
(
result
)
{
.
then
(
function
(
result
)
{
// Transfer database generated values (defaults, autoincrement, etc)
// Transfer database generated values (defaults, autoincrement, etc)
...
...
lib/model.js
View file @
b61b3e8
...
@@ -1805,7 +1805,7 @@ module.exports = (function() {
...
@@ -1805,7 +1805,7 @@ module.exports = (function() {
var
deletedAtCol
=
this
.
_timestampAttributes
.
deletedAt
var
deletedAtCol
=
this
.
_timestampAttributes
.
deletedAt
,
deletedAtObject
=
{};
,
deletedAtObject
=
{};
deletedAtObject
[
deletedAtCol
]
=
null
;
deletedAtObject
[
this
.
rawAttributes
[
deletedAtCol
].
field
||
deletedAtCol
]
=
null
;
// detect emptiness
// detect emptiness
if
(
if
(
...
...
test/model/attributes/field.test.js
View file @
b61b3e8
...
@@ -499,6 +499,28 @@ describe(Support.getTestDialectTeaser('Model'), function() {
...
@@ -499,6 +499,28 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect
(
userB
.
get
(
'id'
)).
to
.
equal
(
userId
);
expect
(
userB
.
get
(
'id'
)).
to
.
equal
(
userId
);
});
});
});
});
it
(
'should work with paranoid destroy'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
deletedAt
:
{
type
:
DataTypes
.
DATE
,
field
:
'deleted_at'
}
},
{
timestamps
:
true
,
paranoid
:
true
});
return
User
.
sync
({
force
:
true
}).
then
(
function
()
{
return
User
.
create
().
then
(
function
(
user
)
{
return
user
.
destroy
();
}).
then
(
function
()
{
return
User
.
findAll
().
then
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
0
);
});
});
});
});
});
});
});
});
});
});
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