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 596d78f3
authored
Apr 10, 2013
by
Jochem Maas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement "sugar" wrapper method for findAll() followed by count()
1 parent
f54644e6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
99 additions
and
0 deletions
lib/dao-factory.js
spec-jasmine/dao-factory.spec.js
lib/dao-factory.js
View file @
596d78f
...
...
@@ -249,6 +249,48 @@ module.exports = (function() {
return
this
.
QueryInterface
.
rawSelect
(
this
.
tableName
,
options
,
'count'
)
}
DAOFactory
.
prototype
.
findAndCount
=
function
(
options
)
{
var
self
=
this
,
copts
=
Utils
.
_
.
extend
({},
options
||
{})
// no limit or offset for the options given to count()
copts
.
offset
=
0
;
copts
.
limit
=
0
;
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
emit
=
{
err
:
function
(
e
)
{
// emit error
emitter
.
emit
(
'error'
,
e
);
}
,
okay
:
function
(
c
,
r
)
{
// emit success
emitter
.
emit
(
'success'
,
{
count
:
c
||
0
,
rows
:
(
r
&&
Array
.
isArray
(
r
)
?
r
:
[])
});
}
,
sql
:
function
(
s
)
{
// emit SQL
emitter
.
emit
(
'sql'
,
s
);
}
};
self
.
count
(
copts
)
.
on
(
'sql'
,
emit
.
sql
)
.
error
(
emit
.
err
)
.
success
(
function
(
cnt
)
{
if
(
cnt
===
0
)
return
emit
.
okay
(
cnt
);
// no records, no need for another query
self
.
findAll
(
options
)
.
on
(
'sql'
,
emit
.
sql
)
.
error
(
emit
.
err
)
.
success
(
function
(
rows
)
{
emit
.
okay
(
cnt
,
rows
)
})
});
}).
run
();
}
DAOFactory
.
prototype
.
max
=
function
(
field
,
options
)
{
options
=
Utils
.
_
.
extend
({
attributes
:
[]
},
options
||
{})
options
.
attributes
.
push
([
'max('
+
field
+
')'
,
'max'
])
...
...
spec-jasmine/dao-factory.spec.js
View file @
596d78f
...
...
@@ -227,6 +227,63 @@ describe('DAOFactory', function() {
})
})
describe
(
'findAndCount'
,
function
()
{
var
users
=
[],
fullcount
=
3
beforeEach
(
function
()
{
Helpers
.
Factories
.
User
({
name
:
'user'
,
bio
:
'foobar'
},
function
(
_users
)
{
users
=
_users
},
fullcount
)
})
it
(
"handles where clause [only]"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
.
findAndCount
({
where
:
"id != "
+
users
[
0
].
id
}).
success
(
function
(
info
)
{
expect
(
info
.
count
).
toEqual
(
fullcount
-
1
)
expect
(
Array
.
isArray
(
info
.
rows
)).
toBeTruthy
()
expect
(
info
.
rows
.
length
).
toEqual
(
fullcount
-
1
)
done
()
})
})
})
/*
// at time of writing (v1.6.0) Sequelize does not seem to support 'offset' on it's own consistently (goes wrong for PostGRES and SQLite)
it("handles offset", function() {
Helpers.async(function(done) {
User.findAndCount({offset: 1}).success(function(info) {
expect(info.count).toEqual(fullcount)
expect(Array.isArray(info.rows)).toBeTruthy()
expect(info.rows.length).toEqual(fullcount - 1)
done()
})
})
})
*/
it
(
"handles limit"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
.
findAndCount
({
limit
:
1
})
/*.on('sql', console.log)*/
.
success
(
function
(
info
)
{
expect
(
info
.
count
).
toEqual
(
fullcount
)
expect
(
Array
.
isArray
(
info
.
rows
)).
toBeTruthy
()
expect
(
info
.
rows
.
length
).
toEqual
(
1
)
done
()
})
})
})
it
(
"handles offset and limit"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
.
findAndCount
({
offset
:
1
,
limit
:
1
}).
success
(
function
(
info
)
{
expect
(
info
.
count
).
toEqual
(
fullcount
)
expect
(
Array
.
isArray
(
info
.
rows
)).
toBeTruthy
()
expect
(
info
.
rows
.
length
).
toEqual
(
1
)
done
()
})
})
})
})
describe
(
'all'
,
function
()
{
beforeEach
(
function
()
{
Helpers
.
Factories
.
User
({
name
:
'user'
,
bio
:
'foobar'
},
null
,
2
)
...
...
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