Comparing version 2.0.2 to 3.0.0
{ | ||
"name": "percy", | ||
"version": "2.0.2", | ||
"version": "3.0.0", | ||
"description": "A persistance layer that plays nice with Couchbase", | ||
@@ -12,6 +12,6 @@ "main": "percy.js", | ||
"dependencies": { | ||
"kgo": "^1.0.0" | ||
"kgo": "^1.3.0" | ||
}, | ||
"devDependencies": { | ||
"grape": "^0.1.10" | ||
"tape": "^3.4.0" | ||
}, | ||
@@ -18,0 +18,0 @@ "directories": { |
123
percy.js
var kgo = require('kgo'); | ||
function get(id, callback) { | ||
var percy = this; | ||
kgo | ||
('bucket', this.connector) | ||
('entityKey', this.createKey.bind(this, id, null)) | ||
(['entityKey', 'bucket'], function(entityKey, bucket){ | ||
bucket.get(entityKey, function(error, result){ | ||
('entityKey', percy.createKey.bind(percy, id, null)) | ||
(['entityKey'], function(entityKey){ | ||
percy.bucket.get(entityKey, function(error, result){ | ||
// Error code 13 is No such keys | ||
@@ -27,18 +28,13 @@ if(error && error.code !== 13){ | ||
this.connector(function(error, bucket){ | ||
this.bucket.getMulti(keys, options, function(error, results){ | ||
if(error){ | ||
return callback(error); | ||
} | ||
bucket.getMulti(keys, options, function(errors, results){ | ||
if(errors){ | ||
return callback(error); | ||
} | ||
var actualResults = []; | ||
for(var key in results){ | ||
actualResults.push(results[key].value); | ||
} | ||
var actualResults = []; | ||
for(var key in results){ | ||
actualResults.push(results[key].value); | ||
} | ||
callback(null, actualResults); | ||
}); | ||
callback(null, actualResults); | ||
}); | ||
@@ -51,12 +47,12 @@ } | ||
kgo | ||
('bucket', this.connector) | ||
('model', function(done){ | ||
percy.validator.validate(data, done); | ||
}) | ||
('entityKey', ['model'], this.createKey.bind(this, id)) | ||
(['entityKey', 'bucket', 'model'], function(entityKey, bucket, model){ | ||
bucket.set(entityKey, model, function(error){ | ||
('entityKey', ['model'], percy.createKey.bind(percy, id)) | ||
(['entityKey', 'model'], function(entityKey, model){ | ||
percy.bucket.upsert(entityKey, model, function(error){ | ||
if(error){ | ||
return callback(error); | ||
} | ||
callback(null, model); | ||
@@ -72,10 +68,13 @@ }); | ||
kgo | ||
('bucket', this.connector) | ||
('model', function(done){ | ||
percy.validator.validate(data, done); | ||
}) | ||
('entityKey', ['model'], this.createKey.bind(this, null)) | ||
(['entityKey', 'bucket', 'model'], function(entityKey, bucket, model){ | ||
bucket.add(entityKey, model, function(error){ | ||
callback(error, error ? null : model); | ||
('entityKey', ['model'], percy.createKey.bind(percy, null)) | ||
(['entityKey', 'model'], function(entityKey, model){ | ||
percy.bucket.insert(entityKey, model, function(error){ | ||
if(error){ | ||
return callback(error); | ||
} | ||
callback(null, model); | ||
}); | ||
@@ -90,9 +89,8 @@ }) | ||
kgo | ||
('bucket', this.connector) | ||
('model', function(done){ | ||
percy.validator.validate(data, done); | ||
}) | ||
('entityKey', ['model'], this.createKey.bind(this, id)) | ||
(['entityKey', 'bucket', 'model'], function(entityKey, bucket, model){ | ||
bucket.replace(entityKey, model, function(error){ | ||
('entityKey', ['model'], percy.createKey.bind(percy, id)) | ||
(['entityKey', 'model'], function(entityKey, model){ | ||
percy.bucket.replace(entityKey, model, function(error){ | ||
callback(error, error ? null : model); | ||
@@ -107,3 +105,3 @@ }); | ||
this.get(id, function(error, model){ | ||
percy.get(id, function(error, model){ | ||
if(error){ | ||
@@ -124,7 +122,8 @@ return callback(error); | ||
function remove(id, callback){ | ||
var percy = this; | ||
kgo | ||
('bucket', this.connector) | ||
('entityKey', this.createKey.bind(this, id, null)) | ||
(['entityKey', 'bucket'], function(entityKey, bucket){ | ||
bucket.remove(entityKey, function(error){ | ||
('entityKey', percy.createKey.bind(percy, id, null)) | ||
(['entityKey'], function(entityKey){ | ||
percy.bucket.remove(entityKey, function(error){ | ||
callback(error); | ||
@@ -136,8 +135,9 @@ }); | ||
function touch(id, options, callback){ | ||
function touch(id, expiry, options, callback){ | ||
var percy = this; | ||
kgo | ||
('bucket', this.connector) | ||
('entityKey', this.createKey.bind(this, id, null)) | ||
(['entityKey', 'bucket'], function(entityKey, bucket){ | ||
bucket.touch(entityKey, options, callback); | ||
('entityKey', percy.createKey.bind(percy, id, null)) | ||
(['entityKey'], function(entityKey){ | ||
percy.bucket.touch(entityKey, expiry, options, callback); | ||
}) | ||
@@ -147,16 +147,2 @@ .on('error', callback); | ||
function exists(id, callback){ | ||
this.touch(id, null, function(error){ | ||
var exists = true; | ||
if(error){ | ||
if(error.message === 'No such key'){ | ||
exists = false; | ||
}else{ | ||
callback(error); | ||
} | ||
} | ||
callback(null, exists); | ||
}); | ||
} | ||
function createKey(id, data, callback){ | ||
@@ -192,31 +178,5 @@ var percy = this; | ||
function getView(viewName, callback){ | ||
var percy = this; | ||
this.connector(function(error, bucket){ | ||
if(error){ | ||
return callback(error); | ||
} | ||
callback(null, bucket.view(percy.entityType, viewName)); | ||
}); | ||
} | ||
function increment(key, options, callback){ | ||
if(typeof options === 'function'){ | ||
callback = options; | ||
options = {initial: 0, offset: 1}; | ||
} | ||
this.connector(function(error, bucket){ | ||
if(error){ | ||
return callback(error); | ||
} | ||
bucket.incr(key, options, callback); | ||
}); | ||
} | ||
function Percy(entityType, connector, validator){ | ||
function Percy(entityType, bucket, validator){ | ||
this.entityType = entityType; | ||
this.connector = connector; | ||
this.bucket = bucket; | ||
this.validator = validator; | ||
@@ -232,9 +192,6 @@ } | ||
Percy.prototype.touch = touch; | ||
Percy.prototype.exists = exists; | ||
Percy.prototype.createKey = createKey; | ||
Percy.prototype.createId = createId; | ||
Percy.prototype.getView = getView; | ||
Percy.prototype.getMulti = getMulti; | ||
Percy.prototype.increment = increment; | ||
module.exports = Percy; |
@@ -1,8 +0,8 @@ | ||
#percy | ||
# percy | ||
A persistance layer that plays nice with Couchbase via [Couchector](https://www.npmjs.org/package/couchector) | ||
A persistance layer that plays nice with couchbase@^2.0.0 | ||
##Usage | ||
## Usage | ||
var Percy = require('percy'), | ||
connection, // connection object such as a Couchector (https://www.npmjs.org/package/couchector) | ||
bucket, // bucket object from Couchbase | ||
validator, // object that has a validate function with a signature of function(model, callback) | ||
@@ -9,0 +9,0 @@ myCoolIdGenerator; // returns a unique id for this entity |
@@ -1,40 +0,47 @@ | ||
var test = require('grape'), | ||
var test = require('tape'), | ||
Percy = require('../'); | ||
function createMockConnector(){ | ||
function createMockBucket(){ | ||
var db = {}; | ||
return function(callback){ | ||
callback(null, { | ||
get: function(key, callback){ | ||
if(!(key in db)){ | ||
return callback(key + ' not in db'); | ||
} | ||
callback(null, db[key]); | ||
}, | ||
set: function(key, model, callback){ | ||
db[key] = {cas:{0:0,1:1}, value: model}; | ||
callback(null, {cas:{0:0,1:1}}); | ||
}, | ||
add: function(key, model, callback){ | ||
if(key in db){ | ||
return callback(key + ' already in db'); | ||
} | ||
db[key] = {value: model}; | ||
callback(null, {cas:{0:0,1:1}}); | ||
}, | ||
remove: function(key, callback){ | ||
if(!(key in db)){ | ||
return callback(key + ' not in db'); | ||
} | ||
delete db[key]; | ||
callback(null, {cas:{0:0,1:1}}); | ||
}, | ||
replace: function(key, model, callback){ | ||
if(!(key in db)){ | ||
return callback(key + ' not in db'); | ||
} | ||
db[key] = {value: model}; | ||
callback(null, {cas:{0:0,1:1}}); | ||
return { | ||
get: function(key, callback){ | ||
if(!(key in db)){ | ||
return callback(key + ' not in db'); | ||
} | ||
}); | ||
callback(null, db[key]); | ||
}, | ||
upsert: function(key, model, callback){ | ||
db[key] = {cas:{0:0,1:1}, value: model}; | ||
callback(null, {cas:{0:0,1:1}}); | ||
}, | ||
insert: function(key, model, callback){ | ||
if(key in db){ | ||
return callback(key + ' already in db'); | ||
} | ||
db[key] = {value: model}; | ||
callback(null, {cas:{0:0,1:1}}); | ||
}, | ||
remove: function(key, callback){ | ||
if(!(key in db)){ | ||
return callback(key + ' not in db'); | ||
} | ||
delete db[key]; | ||
callback(null, {cas:{0:0,1:1}}); | ||
}, | ||
replace: function(key, model, callback){ | ||
if(!(key in db)){ | ||
return callback(key + ' not in db'); | ||
} | ||
db[key] = {value: model}; | ||
callback(null, {cas:{0:0,1:1}}); | ||
}, | ||
getMulti: function(keys, options, callback){ | ||
var results = {}; | ||
for (var i = 0; i < keys.length; i++) { | ||
results[keys[i]] = db[keys[i]]; | ||
} | ||
callback(null, results); | ||
} | ||
}; | ||
@@ -53,3 +60,3 @@ } | ||
var itemIndex = 0, | ||
percy = new Percy('thing', createMockConnector(), createMockValidator()); | ||
percy = new Percy('thing', createMockBucket(), createMockValidator()); | ||
@@ -335,2 +342,25 @@ percy.createId = function(callback){ | ||
}); | ||
}); | ||
}); | ||
test('getMulti', function(t){ | ||
t.plan(6); | ||
var percy = createTestPercy(), | ||
testData1 = {foo: 'bar'}, | ||
testData2 = {stuff: 'meh'}; | ||
percy.set('testData1', testData1, function(error, model1){ | ||
t.notOk(error, 'no error'); | ||
t.equal(model1, testData1, 'model1 returned'); | ||
percy.set('testData2', testData2, function(error, model2){ | ||
t.notOk(error, 'no error'); | ||
t.equal(model2, testData2, 'model2 returned'); | ||
percy.getMulti(['thing:testData1', 'thing:testData2'], function(error, results){ | ||
t.notOk(error, 'no error'); | ||
t.deepEqual(results, [testData1, testData2], 'results returned'); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16116
425
Updatedkgo@^1.3.0