Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Factor query stuff into it's own module.

  • Loading branch information...
commit 796d6264065263d0951d91fae66e6b7c26db7220 1 parent eca6711
@creationix authored
View
35 lib/nstore.js
@@ -229,41 +229,6 @@ var nStore = module.exports = Class.extend({
}
},
- // 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 = Utils.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);
- },
-
clear: function clearAll(callback) {
if (this.busy) {
var self = this;
View
72 lib/nstore/query.js
@@ -0,0 +1,72 @@
+// This plugin adds simple queries to nStore
+var nStore = require('../nstore');
+
+// Get all the records as either a single object or a document/key stream
+nStore.all = function all(callback) {
+ return this.find(undefined, callback);
+};
+
+// Find records using a query
+nStore.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);
+};
+
+function compileQuery(query) {
+ if (typeof query === 'function') {
+ return query;
+ }
+ var exp;
+ if (Array.isArray(query)) {
+ exp = query.map(compileSection).join(" || ") || "true";
+ } else if (typeof query === 'object') {
+ exp = compileSection(query);
+ } else {
+ exp = "true";
+ }
+ var filter = new Function("doc", "key", "return " + exp + ";");
+ return filter;
+}
+
+function compileSection(obj) {
+ var parts = Object.keys(obj).map(function (key) {
+ var p = key.indexOf(" ");
+ var name = key;
+ var operator = "=";
+ if (p > 0) {
+ name = key.substr(0, p);
+ operator = key.substr(p + 1);
+ }
+ if (operator === '=') operator = '===';
+ if (operator === '<>') operator = '!==';
+ if (name !== "key") {
+ name = "doc." + name;
+ }
+ return "(" + name + " " + operator + " " + JSON.stringify(obj[key]) + ")";
+ })
+ return "(" + parts.join(" && ") + ")";
+}
View
39 lib/utils.js
@@ -90,45 +90,8 @@ function safe3(fn) {
};
}
-
-function compileQuery(query) {
- if (typeof query === 'function') {
- return query;
- }
- var exp;
- if (Array.isArray(query)) {
- exp = query.map(compileSection).join(" || ") || "true";
- } else if (typeof query === 'object') {
- exp = compileSection(query);
- } else {
- exp = "true";
- }
- var filter = new Function("doc", "key", "return " + exp + ";");
- return filter;
-}
-
-function compileSection(obj) {
- var parts = Object.keys(obj).map(function (key) {
- var p = key.indexOf(" ");
- var name = key;
- var operator = "=";
- if (p > 0) {
- name = key.substr(0, p);
- operator = key.substr(p + 1);
- }
- if (operator === '=') operator = '===';
- if (operator === '<>') operator = '!==';
- if (name !== "key") {
- name = "doc." + name;
- }
- return "(" + name + " " + operator + " " + JSON.stringify(obj[key]) + ")";
- })
- return "(" + parts.join(" && ") + ")";
-}
-
module.exports = {
fsRead: safe3(fsRead),
fsWrite: fsWrite,
- makeUUID: makeUUID,
- compileQuery: compileQuery
+ makeUUID: makeUUID
};
View
3  test/findTest.js
@@ -1,5 +1,8 @@
require('./helper');
+// Enable the query plugin
+require('../lib/nstore/query');
+
var queries = [
[{age: 50}, 2],
[{"age =": 50}, 2],
Please sign in to comment.
Something went wrong with that request. Please try again.