Permalink
Browse files

Allow for instances on plugins.

  • Loading branch information...
1 parent 5e83526 commit 61b6112a75ae4bcb54f0bbc928323bb3179634f3 @creationix committed Aug 19, 2010
Showing with 37 additions and 35 deletions.
  1. +36 −34 lib/nstore/query.js
  2. +1 −1 test/findTest.js
View
@@ -1,42 +1,44 @@
// This plugin adds simple queries to nStore
-module.exports = {
+module.exports = function QueryPlugin() {
+ return {
- // Get all the records as either a single object or a document/key stream
- all: function all(callback) {
- return this.find(undefined, callback);
- },
+ // Get all the records as either a single object or a document/key stream
+ all: function all(callback) {
+ return this.find(undefined, callback);
+ },
- // Find records using a query
- find: function find(query, callback) {
- var filter = compileQuery(query);
- var stream;
- var results = {};
- if (!callback) {
- stream = new EventEmitter();
+ // Find records using a query
+ find: function find(query, callback) {
+ var filter = compileQuery(query);
+ var stream;
+ var results = {};
+ if (!callback) {
+ stream = new EventEmitter();
+ }
+ var keys = Object.keys(this.index);
+ var counter = keys.length;
+ keys.forEach(function (key) {
+ this.get(key, function (err, doc, key) {
+ if (err) {
+ if (stream) stream.emit('error', err);
+ if (callback) callback(err);
+ return;
+ }
+ if (filter(doc, key)) {
+ if (stream) stream.emit('document', doc, key);
+ if (callback) results[key] = doc;
+ };
+ counter--;
+ if (counter === 0) {
+ if (stream) stream.emit('end');
+ if (callback) callback(null, results);
+ }
+ });
+ }, this);
}
- var keys = Object.keys(this.index);
- var counter = keys.length;
- keys.forEach(function (key) {
- this.get(key, function (err, doc, key) {
- if (err) {
- if (stream) stream.emit('error', err);
- if (callback) callback(err);
- return;
- }
- if (filter(doc, key)) {
- if (stream) stream.emit('document', doc, key);
- if (callback) results[key] = doc;
- };
- counter--;
- if (counter === 0) {
- if (stream) stream.emit('end');
- if (callback) callback(null, results);
- }
- });
- }, this);
- }
-};
+ };
+}
function compileQuery(query) {
if (typeof query === 'function') {
View
@@ -1,7 +1,7 @@
require('./helper');
// Enable the query plugin
-nStore = nStore.extend(require('nstore/query'));
+nStore = nStore.extend(require('nstore/query')());
var queries = [
[{age: 50}, 2],

0 comments on commit 61b6112

Please sign in to comment.