Feathers: Database count operation

Created on 12 Dec 2016  路  8Comments  路  Source: feathersjs/feathers

I want to bootstrap my database with some data but I'm not sure how to query to see if the database is empty. I'm currently doing this:

module.exports = function(service) {
  service.find({})
    .then((results) => {
      if (results.length === 0) {
        return fsp.readFile('./articles.yaml')
          .then(YAML.parse)
          .then(R.map(R.evolve({
            date: formatISO,
            title: markdown,
            description: markdown,
          })))
          .then((posts) => {
            return Promise.all(posts.map((post) => {
              return service.create(post)
            }))
          })
      }
    })
}

But thats going to cause problems eventually...

Most helpful comment

You can actually make the query and set$limit to 0 and it will only run the count query itself (in the fasted way the supported database provides).

All 8 comments

You could limit that query to 1 record. Inside find: query:{$limit:1}

What problems do you foresee?

You can actually make the query and set$limit to 0 and it will only run the count query itself (in the fasted way the supported database provides).

interesting. so $limit: 0 will return a number?

What problems do you foresee?

querying a bunch of data that we dont need.

I'm just getting started with feathers -- i bet in 10 minutes you guys make the querying docs 100x better wtih some examples. ;)

@ccorcos

querying a bunch of data that we dont need.

Ok. That's the only one I saw, too. Just making sure.

in 10 minutes ...

More like 11. 馃槂

$limit : 0 will still return a normal paginated object with { total, limit, skip, data } but data will be an empty array (also, for this to work, pagination will have to be enabled which it normally is in any generated application).

@daffl

RFC - If the service method is called with $limit: 0, should the data query still be executed, or the operation not be executed and an empty array be returned immediately (depending on service method)?

 sequelize:sql:mysql executing(default) : SELECT count(DISTINCT(`organiser`.`id`)) AS `count` FROM `organiser` AS `organiser` WHERE `organiser`.`name` = 'Tim Limited'; +5s
  sequelize:sql:mysql executed(default) : SELECT count(DISTINCT(`organiser`.`id`)) AS `count` FROM `organiser` AS `organiser` WHERE `organiser`.`name` = 'Tim Limited'; +1ms
  sequelize:pool connection released +1ms
  sequelize:pool connection acquired +0ms
  sequelize:sql:mysql executing(default) : SELECT `id`, `name`, `description`, `type`, `creator_id`, `status`, `createdAt`, `updatedAt` FROM `organiser` AS `organiser` WHERE`organiser`.`name` = 'Tim Limited' LIMIT 0; +0ms
  sequelize:sql:mysql executed(default) : SELECT `id`, `name`, `description`, `type`, `creator_id`, `status`, `createdAt`, `updatedAt` FROM `organiser` AS `organiser` WHERE `organiser`.`name` = 'Tim Limited' LIMIT 0; +1ms

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NetOperatorWibby picture NetOperatorWibby  路  4Comments

davigmacode picture davigmacode  路  3Comments

eric-burel picture eric-burel  路  3Comments

perminder-klair picture perminder-klair  路  3Comments

rstegg picture rstegg  路  3Comments