Comparing version 0.0.3 to 0.0.4
30
index.js
@@ -84,3 +84,6 @@ var fs = require('fs'), | ||
} else if ((!_.has(query, 'id') && !_.isEmpty(query)) && _.isFunction(callback)) { | ||
callback(new Error('Get using query object not yet implemented.')); | ||
this.getByQuery(query, function(error, records) { | ||
if (error) return callback(error); | ||
callback(null, records); | ||
}); | ||
@@ -124,2 +127,27 @@ // Invalid arguments | ||
Connection.prototype.getByQuery = function(query, callback) { | ||
var matchedRecords = []; | ||
var keysToMatch = _.keys(query); | ||
function hasKeysAndValues(keys, source, target) { | ||
var isMatch = _.all(keys, function(key) { | ||
return source[key] === target[key]; | ||
}); | ||
return isMatch; | ||
}; | ||
this.connection.createValueStream() | ||
.on('error', function(error) { | ||
callback(error); | ||
}) | ||
.on('data', function(record) { | ||
if (hasKeysAndValues(keysToMatch, query, record)) { | ||
matchedRecords.push(record); | ||
} | ||
}) | ||
.on('end', function() { | ||
callback(null, matchedRecords); | ||
}); | ||
}; | ||
Connection.prototype.update = function(record, callback) { | ||
@@ -126,0 +154,0 @@ if (!_.isObject(record) || !record.id) { |
{ | ||
"name": "magpie", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Simple persistence for rapid prototyping. Powered by levelup.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -74,7 +74,7 @@ Magpie | ||
Get a record by ID using a query object: | ||
Get records by author using a query object: | ||
``` | ||
```js | ||
var query = { | ||
id: "7f5d532b-8bfe-42fd-a1d3-8272e8aa7e3f" | ||
author: "TheOatmeal.com" | ||
}; | ||
@@ -123,2 +123,4 @@ db.get(query, function(error, record) { | ||
### db.getByQuery(query _object_, callback); | ||
License | ||
@@ -125,0 +127,0 @@ ------- |
Sorry, the diff of this file is not supported yet
17333
412
148