nemo64:query-builder

v1.1.1Published 10 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Meteor QueryBuilder package

This package will help you create reusable queries by defining them as filters which can be enabled or disabled for a query!

This way you avoid messy queries all over your project and changes in your collections (like adding user restrictions) are very easy.

View API Documentation on GitHub

Examples

soft-delete

// both/collections/person.js
Person = new Meteor.Collection('person');

// define query additions that are always added to every query
Person.defaultFilters({
  softDelete: { deletedAt: null }
});

Think now of a list of all users you application has:

// client/views/person/person_index.js
Template.PersonIndex.persons = function () {
  return Person.query().execute({ limit: 100 });
  // executes Person.find({ deletedAt: null }, { limit: 100 });
}

But now you are an admin and want to see deleted users:

// client/views/person/person_admin_index.js
Template.PersonAdminIndex.persons = function () {
  var query = Person.query();
  query.filter('softDelete', false); // disable for this query
  return query.execute({ limit: 100 });
  // executes Person.find({}, { limit: 100 });
}

Now you want to search a person (not admin anymore):

// client/views/person/person_search_index.js
Template.PersonSearchIndex.persons = function () {
  var query = Person.query({ name: "Max" });
  
  // you could disable/enable filters here
  
  return query.execute();
  // executes Person.find({ $and: [{ name: "Max" }, { deletedAt: null }] });
}

normal filters

// both/collections/person.js
Person = new Meteor.Collection('person');

// these filters are enabled by default
Person.defaultFilters({
  softDelete: { deletedAt: null }
});

// these filters are disabled by default
Person.filters({
  employedAt: function (company) {
    return { employedAt: company._id };
  }
  // heads up: normal filters can be simple object too
  // but in this example the filter is a function
});
// client/views/company/company_person_index.js
Template.CompanyPersonIndex.persons = function (company) {
  var query = Person.query();
  query.filter('employedAt', true, [company]);
  return query.execute();
  // executes Person.find({
  //   $and: [{ employedAt: "[company id]" }, { deletedAt: null }]
  // });
}

API

The following methods are the ones you should use. There are more methods available if you study the source, but only those listed here are safe to use and won't change in the near future. Methods you shouldn't use are marked with an _ prefix on there names.

All methods described here are available on both the client and the server so there is no restriction to it unless told otherwise.

Collection methods

These methods are added to the meteors Collection prototype so they are available on every collection you have in your app.

collection.filters(methods)

Adds filters to the collection which are by default disabled. Calling this method more then once will add the new filters, not replace them. However, if a filter is defined that already exists, it will be overwritten.

Arguments

collection.defaultFilters(methods)

The same as filter but by default enabled.

collection.query([selector])

Creates a Query object to build the query with.

Arguments

Query object

This object abstracts the query sent to the find method. It can be created with the collection method query and can be executed with the properly named method execute

Query.execute([options])

Builds the query and passes it to the find method. The option parameter is the same as the one of find.

Arguments
  • Options Object This argument is exactly the same as the one on the find method of meteor.

Query.filter(name, enable, [args])

This method can change which filter will be used for that query. If the name of the filter does not exist a warning will be given though console.warn

Arguments
  • name String The name of the filter which should be enabled or disabled.
  • enable Boolean A boolean to enable or disable this filter.
  • args Array These values will be passed to the filter function (if it is a function). It will be ignored if enable is false.

Query.condition(selector)

Adds another selector to the query. It will be executed just like a filter (with an $and).

Arguments

Contributing and Feedback

Every kind of Contributing and/or Feedback is welcome. If an issue or a pull request isn't what you need you can contact me at cheat2000-git [at] yahoo.de or talk about it on google groups