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 3e048c74
authored
May 03, 2012
by
sdepold
Browse files
Options
Browse Files
Download
Plain Diff
merge: added selective save
2 parents
b928d775
3c4cbad3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
63 additions
and
3 deletions
lib/associations/belongs-to.js
lib/dao.js
spec-jasmine/dao.spec.js
lib/associations/belongs-to.js
View file @
3e048c7
...
@@ -44,7 +44,9 @@ module.exports = (function() {
...
@@ -44,7 +44,9 @@ module.exports = (function() {
obj
[
accessor
]
=
function
(
associatedObject
)
{
obj
[
accessor
]
=
function
(
associatedObject
)
{
obj
[
self
.
identifier
]
=
associatedObject
?
associatedObject
.
id
:
null
obj
[
self
.
identifier
]
=
associatedObject
?
associatedObject
.
id
:
null
return
obj
.
save
()
// passes the changed field to save, so only that field get updated.
return
obj
.
save
([
self
.
identifier
])
}
}
return
this
return
this
...
...
lib/dao.js
View file @
3e048c7
...
@@ -81,7 +81,20 @@ module.exports = (function() {
...
@@ -81,7 +81,20 @@ module.exports = (function() {
}
}
})
})
DAO
.
prototype
.
save
=
function
()
{
// if an array with field names is passed to save()
// only those fields will be updated
DAO
.
prototype
.
save
=
function
(
fields
)
{
var
self
=
this
,
values
=
fields
?
{}
:
this
.
values
;
if
(
fields
)
{
fields
.
forEach
(
function
(
field
)
{
if
(
self
.
values
[
field
])
{
values
[
field
]
=
self
.
values
[
field
]
}
});
}
var
updatedAtAttr
=
this
.
__options
.
underscored
?
'updated_at'
:
'updatedAt'
var
updatedAtAttr
=
this
.
__options
.
underscored
?
'updated_at'
:
'updatedAt'
if
(
this
.
__options
.
timestamps
&&
this
.
hasOwnProperty
(
updatedAtAttr
))
if
(
this
.
__options
.
timestamps
&&
this
.
hasOwnProperty
(
updatedAtAttr
))
...
@@ -92,7 +105,7 @@ module.exports = (function() {
...
@@ -92,7 +105,7 @@ module.exports = (function() {
}
else
{
}
else
{
var
identifier
=
this
.
__options
.
hasPrimaryKeys
?
this
.
primaryKeyValues
:
this
.
id
var
identifier
=
this
.
__options
.
hasPrimaryKeys
?
this
.
primaryKeyValues
:
this
.
id
,
tableName
=
this
.
__factory
.
tableName
,
tableName
=
this
.
__factory
.
tableName
,
query
=
this
.
QueryInterface
.
update
(
this
,
tableName
,
this
.
values
,
identifier
)
,
query
=
this
.
QueryInterface
.
update
(
this
,
tableName
,
values
,
identifier
)
return
query
return
query
}
}
...
...
spec-jasmine/dao.spec.js
View file @
3e048c7
...
@@ -333,6 +333,51 @@ describe('DAO', function() {
...
@@ -333,6 +333,51 @@ describe('DAO', function() {
})
})
describe
(
'save'
,
function
()
{
describe
(
'save'
,
function
()
{
it
(
'only updates fields in passed array'
,
function
()
{
var
user
=
null
,
user2
=
null
,
userId
=
null
,
date
=
new
Date
(
1990
,
01
,
01
)
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
username
:
'foo'
,
birthDate
:
new
Date
()
}).
success
(
function
(
_user
)
{
user
=
_user
done
()
}).
error
(
function
(
err
)
{
console
.
log
(
err
)
})
})
Helpers
.
async
(
function
(
done
)
{
user
.
username
=
'fizz'
user
.
birthDate
=
date
done
()
})
Helpers
.
async
(
function
(
done
)
{
user
.
save
([
'username'
]).
success
(
function
(){
// re-select user
User
.
find
(
user
.
id
).
success
(
function
(
_user2
)
{
user2
=
_user2
done
()
})
})
})
Helpers
.
async
(
function
(
done
)
{
// name should have changed
expect
(
user2
.
username
).
toEqual
(
'fizz'
)
// bio should be unchanged
expect
(
user2
.
birthDate
).
toNotEqual
(
date
)
done
()
})
})
it
(
"stores an entry in the database"
,
function
()
{
it
(
"stores an entry in the database"
,
function
()
{
var
username
=
'user'
var
username
=
'user'
,
user
=
User
.
build
({
,
user
=
User
.
build
({
...
...
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