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 d42b47d1
authored
May 04, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
model.count
1 parent
00ebd92a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
0 deletions
lib/sequelize/model-definition.js
lib/sequelize/query-generator.js
test/Model/count.js
lib/sequelize/model-definition.js
View file @
d42b47d
...
...
@@ -81,6 +81,17 @@ ModelDefinition.prototype.__defineGetter__('all', function() {
return
this
.
query
(
QueryGenerator
.
selectQuery
(
this
.
tableName
))
})
ModelDefinition
.
prototype
.
count
=
function
(
options
)
{
var
self
=
this
var
emitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
self
.
query
(
QueryGenerator
.
countQuery
(
self
.
tableName
,
options
),
self
,
{
plain
:
true
}).
on
(
'success'
,
function
(
obj
)
{
emitter
.
emit
(
'success'
,
obj
[
'count(*)'
])
})
})
return
emitter
.
run
()
}
ModelDefinition
.
prototype
.
findAll
=
function
(
options
)
{
return
this
.
query
(
QueryGenerator
.
selectQuery
(
this
.
tableName
,
options
))
}
...
...
lib/sequelize/query-generator.js
View file @
d42b47d
...
...
@@ -75,6 +75,10 @@ var QueryGenerator = module.exports = {
return
Utils
.
_
.
template
(
query
)(
options
)
},
countQuery
:
function
(
tableName
,
options
)
{
return
QueryGenerator
.
selectQuery
(
tableName
,
options
).
replace
(
"*"
,
"count(*)"
)
},
/*
Returns an insert into command. Parameters: table name + hash of attribute-value-pairs.
*/
...
...
test/Model/count.js
0 → 100644
View file @
d42b47d
var
assert
=
require
(
"assert"
)
,
config
=
require
(
"./../config"
)
,
Sequelize
=
require
(
"./../../index"
)
,
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
{
logging
:
false
})
module
.
exports
=
{
'it should correctly count all objects'
:
function
(
exit
)
{
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
create
({
username
:
'user1'
}).
on
(
'success'
,
function
()
{
User
.
create
({
username
:
'user2'
}).
on
(
'success'
,
function
()
{
User
.
count
().
on
(
'success'
,
function
(
count
)
{
assert
.
eql
(
count
,
2
)
exit
(
function
(){})
})
})
})
})
},
'it should correctly count filtered objects'
:
function
(
exit
)
{
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
create
({
username
:
'user1'
}).
on
(
'success'
,
function
()
{
User
.
create
({
username
:
'foo'
}).
on
(
'success'
,
function
()
{
User
.
count
({
where
:
"username LIKE '%us%'"
}).
on
(
'success'
,
function
(
count
)
{
assert
.
eql
(
count
,
1
)
exit
(
function
(){})
})
})
})
})
}
}
\ No newline at end of file
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