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 0af885ec
authored
Jun 19, 2013
by
Jochem Maas
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'iamjochem/feature/findAndCountSugar'
2 parents
f6f9a41f
6d499699
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
113 additions
and
0 deletions
lib/dao-factory.js
spec-jasmine/dao-factory.spec.js
lib/dao-factory.js
View file @
0af885e
...
@@ -297,6 +297,51 @@ module.exports = (function() {
...
@@ -297,6 +297,51 @@ module.exports = (function() {
return
this
.
QueryInterface
.
rawSelect
(
this
.
getTableName
(),
options
,
'count'
)
return
this
.
QueryInterface
.
rawSelect
(
this
.
getTableName
(),
options
,
'count'
)
}
}
DAOFactory
.
prototype
.
findAndCountAll
=
function
(
options
)
{
var
self
=
this
,
opts
=
Utils
.
_
.
cloneDeep
(
options
)
,
copts
=
Utils
.
_
.
extend
({},
Utils
.
_
.
cloneDeep
(
options
)
||
{},
{
// no limit, offset, order or include for the options given to count()
offset
:
0
,
limit
:
0
,
order
:
null
,
include
:
null
})
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
)
{
DAOFactory
.
prototype
.
max
=
function
(
field
,
options
)
{
options
=
Utils
.
_
.
extend
({
attributes
:
[]
},
options
||
{})
options
=
Utils
.
_
.
extend
({
attributes
:
[]
},
options
||
{})
options
.
attributes
.
push
([
'max('
+
field
+
')'
,
'max'
])
options
.
attributes
.
push
([
'max('
+
field
+
')'
,
'max'
])
...
...
spec-jasmine/dao-factory.spec.js
View file @
0af885e
...
@@ -208,6 +208,74 @@ describe('DAOFactory', function() {
...
@@ -208,6 +208,74 @@ describe('DAOFactory', function() {
})
})
})
})
describe
(
'findAndCountAll'
,
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
.
findAndCountAll
({
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
()
})
})
})
it
(
"handles where clause with ordering [only]"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
.
findAndCountAll
({
where
:
"id != "
+
users
[
0
].
id
,
order
:
'id ASC'
}).
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.findAndCountAll({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
.
findAndCountAll
({
limit
:
1
}).
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
.
findAndCountAll
({
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
()
{
describe
(
'all'
,
function
()
{
beforeEach
(
function
()
{
beforeEach
(
function
()
{
Helpers
.
Factories
.
User
({
name
:
'user'
,
bio
:
'foobar'
},
null
,
2
)
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