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 3be596cf
authored
Jan 18, 2013
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add key value pairs to increment
1 parent
260fc6b4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
8 deletions
lib/dao.js
spec/dao.spec.js
lib/dao.js
View file @
3be596c
...
@@ -241,17 +241,28 @@ module.exports = (function() {
...
@@ -241,17 +241,28 @@ module.exports = (function() {
var
identifier
=
this
.
__options
.
hasPrimaryKeys
?
this
.
primaryKeyValues
:
this
.
id
,
var
identifier
=
this
.
__options
.
hasPrimaryKeys
?
this
.
primaryKeyValues
:
this
.
id
,
values
=
{}
values
=
{}
if
(
!
Utils
.
_
.
isArray
(
fields
))
fields
=
[
fields
];
if
(
count
===
undefined
)
count
=
1
;
if
(
count
===
undefined
)
count
=
1
;
Utils
.
_
.
each
(
fields
,
function
(
field
)
{
if
(
Utils
.
_
.
isString
(
fields
))
{
values
[
field
]
=
count
values
[
fields
]
=
count
;
})
}
else
if
(
Utils
.
_
.
isArray
(
fields
))
{
Utils
.
_
.
each
(
fields
,
function
(
field
)
{
values
[
field
]
=
count
})
}
else
{
// Assume fields is key-value pairs
values
=
fields
;
}
return
this
.
QueryInterface
.
increment
(
this
,
this
.
__factory
.
tableName
,
values
,
identifier
)
return
this
.
QueryInterface
.
increment
(
this
,
this
.
__factory
.
tableName
,
values
,
identifier
)
}
}
DAO
.
prototype
.
decrement
=
function
(
fields
,
count
)
{
DAO
.
prototype
.
decrement
=
function
(
fields
,
count
)
{
if
(
!
Utils
.
_
.
isString
(
fields
)
&&
!
Utils
.
_
.
isArray
(
fields
))
{
// Assume fields is key-value pairs
Utils
.
_
.
each
(
fields
,
function
(
value
,
field
)
{
fields
[
field
]
=
-
value
;
});
}
return
this
.
increment
(
fields
,
0
-
count
);
return
this
.
increment
(
fields
,
0
-
count
);
}
}
...
...
spec/dao.spec.js
View file @
3be596c
...
@@ -18,7 +18,8 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
...
@@ -18,7 +18,8 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
self
.
User
=
sequelize
.
define
(
'User'
,
{
self
.
User
=
sequelize
.
define
(
'User'
,
{
username
:
{
type
:
DataTypes
.
STRING
},
username
:
{
type
:
DataTypes
.
STRING
},
touchedAt
:
{
type
:
DataTypes
.
DATE
,
defaultValue
:
DataTypes
.
NOW
},
touchedAt
:
{
type
:
DataTypes
.
DATE
,
defaultValue
:
DataTypes
.
NOW
},
aNumber
:
{
type
:
DataTypes
.
INTEGER
}
aNumber
:
{
type
:
DataTypes
.
INTEGER
},
bNumber
:
{
type
:
DataTypes
.
INTEGER
}
})
})
self
.
HistoryLog
=
sequelize
.
define
(
'HistoryLog'
,
{
self
.
HistoryLog
=
sequelize
.
define
(
'HistoryLog'
,
{
...
@@ -37,7 +38,7 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
...
@@ -37,7 +38,7 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
describe
(
'increment'
,
function
()
{
describe
(
'increment'
,
function
()
{
before
(
function
(
done
)
{
before
(
function
(
done
)
{
this
.
User
.
create
({
id
:
1
,
aNumber
:
0
}).
done
(
done
)
this
.
User
.
create
({
id
:
1
,
aNumber
:
0
,
bNumber
:
0
}).
done
(
done
)
});
});
it
(
'with array'
,
function
(
done
)
{
it
(
'with array'
,
function
(
done
)
{
...
@@ -107,11 +108,27 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
...
@@ -107,11 +108,27 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
user1
.
increment
([
'aNumber'
],
2
).
done
(
_done
);
user1
.
increment
([
'aNumber'
],
2
).
done
(
_done
);
});
});
});
});
it
(
'with key value pair'
,
function
(
done
)
{
var
self
=
this
;
// Select something
this
.
User
.
find
(
1
).
done
(
function
(
err
,
user1
)
{
user1
.
increment
({
'aNumber'
:
1
,
'bNumber'
:
2
}).
done
(
function
(
err
,
user2
)
{
self
.
User
.
find
(
1
).
done
(
function
(
err
,
user3
)
{
expect
(
user3
.
aNumber
).
toBe
(
1
);
expect
(
user3
.
bNumber
).
toBe
(
2
);
done
();
});
});
});
});
});
});
describe
(
'decrement'
,
function
()
{
describe
(
'decrement'
,
function
()
{
before
(
function
(
done
)
{
before
(
function
(
done
)
{
this
.
User
.
create
({
id
:
1
,
aNumber
:
0
}).
done
(
done
)
this
.
User
.
create
({
id
:
1
,
aNumber
:
0
,
bNumber
:
0
}).
done
(
done
)
});
});
it
(
'with array'
,
function
(
done
)
{
it
(
'with array'
,
function
(
done
)
{
...
@@ -181,8 +198,24 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
...
@@ -181,8 +198,24 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
user1
.
decrement
([
'aNumber'
],
2
).
done
(
_done
);
user1
.
decrement
([
'aNumber'
],
2
).
done
(
_done
);
});
});
});
});
});
it
(
'with key value pair'
,
function
(
done
)
{
var
self
=
this
;
// Select something
this
.
User
.
find
(
1
).
done
(
function
(
err
,
user1
)
{
user1
.
decrement
({
'aNumber'
:
1
,
'bNumber'
:
2
}).
done
(
function
(
err
,
user2
)
{
self
.
User
.
find
(
1
).
done
(
function
(
err
,
user3
)
{
expect
(
user3
.
aNumber
).
toBe
(
-
1
);
expect
(
user3
.
bNumber
).
toBe
(
-
2
);
done
();
});
});
});
});
});
/*
describe('default values', function() {
describe('default values', function() {
describe('current date', function() {
describe('current date', function() {
it('should store a date in touchedAt', function() {
it('should store a date in touchedAt', function() {
...
@@ -318,4 +351,5 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
...
@@ -318,4 +351,5 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
}.bind(this))
}.bind(this))
})
})
})
})
*/
})
})
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