Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "percy", | ||
"version": "1.0.1", | ||
"description": "a persistence layer", | ||
"version": "1.0.2", | ||
"description": "A persistance layer that plays nice with Couchbase", | ||
"main": "percy.js", | ||
@@ -10,5 +10,5 @@ "scripts": { | ||
"author": "Maurice Butler <maurice.butler@gmail.com>", | ||
"license": "ISC", | ||
"license": "MIT", | ||
"dependencies": { | ||
"kgo": "^0.1.1" | ||
"kgo": "^1.0.0" | ||
}, | ||
@@ -28,3 +28,12 @@ "devDependencies": { | ||
}, | ||
"homepage": "https://github.com/yoik/percy" | ||
"homepage": "https://github.com/yoik/percy", | ||
"keywords": [ | ||
"couchbase", | ||
"persistance", | ||
"layer", | ||
"validate", | ||
"validator", | ||
"couch", | ||
"base" | ||
] | ||
} |
139
percy.js
var kgo = require('kgo'), | ||
arrayProto = []; | ||
function get(key, callback) { | ||
var entityKey = this.createKey(key); | ||
function get(id, callback) { | ||
var percy = this; | ||
this.connector(function(error, bucket){ | ||
if(error){ | ||
return callback(error); | ||
} | ||
kgo | ||
('bucket', this.connector) | ||
('entityKey', this.createKey.bind(this, id, null)) | ||
(['entityKey', 'bucket'], function(entityKey, bucket){ | ||
bucket.get(entityKey, function(error, result){ | ||
@@ -19,3 +19,4 @@ // Error code 13 is No such keys | ||
}); | ||
}); | ||
}) | ||
.on('error', callback); | ||
} | ||
@@ -49,5 +50,4 @@ | ||
function set(key, data, callback) { | ||
var percy = this, | ||
entityKey = this.createKey(key); | ||
function set(id, data, callback) { | ||
var percy = this; | ||
@@ -59,3 +59,4 @@ kgo | ||
}) | ||
(['bucket', 'model'], function(bucket, model){ | ||
('entityKey', ['model'], this.createKey.bind(this, id)) | ||
(['entityKey', 'bucket', 'model'], function(entityKey, bucket, model){ | ||
bucket.set(entityKey, model, function(error, result){ | ||
@@ -67,11 +68,8 @@ if(error){ | ||
}); | ||
}).errors({ | ||
bucket: callback, | ||
model: callback | ||
}); | ||
}) | ||
.on('error', callback); | ||
} | ||
function add(key, data, callback){ | ||
var percy = this, | ||
entityKey = this.createKey(key); | ||
function add(data, callback){ | ||
var percy = this; | ||
@@ -83,18 +81,13 @@ kgo | ||
}) | ||
(['bucket', 'model'], function(bucket, model){ | ||
('entityKey', ['model'], this.createKey.bind(this, null)) | ||
(['entityKey', 'bucket', 'model'], function(entityKey, bucket, model){ | ||
bucket.add(entityKey, model, function(error, result){ | ||
if(error){ | ||
return callback(error); | ||
} | ||
callback(null, model); | ||
callback(error, error ? null : model); | ||
}); | ||
}).errors({ | ||
bucket: callback, | ||
model: callback | ||
}); | ||
}) | ||
.on('error', callback); | ||
} | ||
function replace(key, data, callback){ | ||
var percy = this, | ||
entityKey = this.createKey(key); | ||
function replace(id, data, callback){ | ||
var percy = this; | ||
@@ -106,19 +99,15 @@ kgo | ||
}) | ||
(['bucket', 'model'], function(bucket, model){ | ||
('entityKey', ['model'], this.createKey.bind(this, id)) | ||
(['entityKey', 'bucket', 'model'], function(entityKey, bucket, model){ | ||
bucket.replace(entityKey, model, function(error, result){ | ||
if(error){ | ||
return callback(error); | ||
} | ||
callback(null, model); | ||
callback(error, error ? null : model); | ||
}); | ||
}).errors({ | ||
bucket: callback, | ||
model: callback | ||
}); | ||
}) | ||
.on('error', callback); | ||
} | ||
function update(key, data, callback){ | ||
function update(id, data, callback){ | ||
var percy = this; | ||
this.get(key, function(error, model){ | ||
this.get(id, function(error, model){ | ||
if(error){ | ||
@@ -134,26 +123,23 @@ return callback(error); | ||
} | ||
percy.replace(key, model, callback); | ||
percy.replace(id, model, callback); | ||
}); | ||
} | ||
function remove(key, callback){ | ||
var entityKey = this.createKey(key); | ||
this.connector(function(error, bucket){ | ||
if(error){ | ||
return callback(error); | ||
} | ||
function remove(id, callback){ | ||
kgo | ||
('bucket', this.connector) | ||
('entityKey', this.createKey.bind(this, id, null)) | ||
(['entityKey', 'bucket'], function(entityKey, bucket){ | ||
bucket.remove(entityKey, function(error, result){ | ||
if(error){ | ||
return callback(error); | ||
} | ||
callback(null); | ||
callback(error); | ||
}); | ||
}); | ||
}) | ||
.on('error', callback); | ||
} | ||
function touch(key, options, callback){ | ||
var entityKey = this.createKey(key); | ||
this.connector(function(error, bucket){ | ||
function touch(id, options, callback){ | ||
kgo | ||
('bucket', this.connector) | ||
('entityKey', this.createKey.bind(this, id, null)) | ||
(['entityKey', 'bucket'], function(entityKey, bucket){ | ||
if(error){ | ||
@@ -163,7 +149,8 @@ return callback(error); | ||
bucket.touch(entityKey, options, callback); | ||
}); | ||
}) | ||
.on('error', callback); | ||
} | ||
function exists(key, callback){ | ||
this.touch(key, null, function(error){ | ||
function exists(id, callback){ | ||
this.touch(id, null, function(error){ | ||
var exists = true; | ||
@@ -181,10 +168,26 @@ if(error){ | ||
function createKey(key){ | ||
if(!Array.isArray(key)){ | ||
key = [key]; | ||
function createKey(id, data, callback){ | ||
var percy = this; | ||
if(id == null){ | ||
if(data && 'id' in data){ | ||
return callback("object already has an ID"); | ||
} | ||
percy.createId(function(error, id){ | ||
if(error){ | ||
return callback(error); | ||
} | ||
data.id = id; | ||
callback(null, percy.entityType + ':' + id); | ||
}); | ||
} | ||
key.unshift(this.entityType); | ||
return key.join(':'); | ||
callback(null, percy.entityType + ':' + id); | ||
} | ||
function createId(callback){ | ||
throw new Error("Not Implemented"); | ||
} | ||
function getView(viewName, callback){ | ||
@@ -197,3 +200,3 @@ var percy = this; | ||
} | ||
callback(null, bucket.view(percy.designName, viewName)); | ||
callback(null, bucket.view(percy.entityType, viewName)); | ||
}); | ||
@@ -219,3 +222,2 @@ } | ||
this.entityType = entityType; | ||
this.designName = entityType; | ||
this.connector = connector; | ||
@@ -234,2 +236,3 @@ this.validator = validator; | ||
Percy.prototype.createKey = createKey; | ||
Percy.prototype.createId = createId; | ||
Percy.prototype.getView = getView; | ||
@@ -236,0 +239,0 @@ Percy.prototype.getMulti = getMulti; |
@@ -8,8 +8,13 @@ #percy | ||
connection, // connection object such as a Couchector (https://www.npmjs.org/package/couchector) | ||
validator; // object that has a validate function with a signature of function(model, callback) | ||
validator, // object that has a validate function with a signature of function(model, callback) | ||
myCoolIdGenerator; // returns a unique id for this entity | ||
var persistance = new Percy('user', connection, validator); | ||
persistance.add('1234', { userName: 'bob' }, function(error, user){ | ||
// createId must be set so Percy knows how to generate unique ids | ||
persistance.createId = function(callback){ | ||
callback(null, myCoolIdGenerator()); | ||
}; | ||
persistance.add({ userName: 'bob' }, function(error, user){ | ||
if(error){ | ||
@@ -20,3 +25,12 @@ console.log(error); | ||
console.log(user); | ||
}); | ||
console.log(user); // { id: 'myCoolId', userName: 'bob'} | ||
}); | ||
// Results in the following db record | ||
KEY VALUE | ||
user:myCoolId { | ||
id: 'myCoolId' | ||
userName: 'bob' | ||
} |
@@ -20,3 +20,3 @@ var test = require('grape'), | ||
if(key in db){ | ||
return callback(true); | ||
return callback(key + ' already in db'); | ||
} | ||
@@ -28,3 +28,3 @@ db[key] = {value: model}; | ||
if(!(key in db)){ | ||
return callback(true); | ||
return callback(key + ' not in db'); | ||
} | ||
@@ -36,3 +36,3 @@ delete db[key]; | ||
if(!(key in db)){ | ||
return callback(true); | ||
return callback(key + ' not in db'); | ||
} | ||
@@ -54,2 +54,13 @@ db[key] = {value: model}; | ||
function createTestPercy(){ | ||
var itemIndex = 0; | ||
percy = new Percy('thing', createMockConnector(), createMockValidator()); | ||
percy.createId = function(callback){ | ||
callback(null, (itemIndex++).toString()); | ||
}; | ||
return percy; | ||
} | ||
test('create percy', function(t){ | ||
@@ -59,3 +70,3 @@ | ||
var percy = new Percy('thing', createMockConnector(), createMockValidator()); | ||
var percy = createTestPercy(); | ||
@@ -69,3 +80,3 @@ t.pass('Percy was created'); | ||
var percy = new Percy('thing', createMockConnector(), createMockValidator()); | ||
var percy = createTestPercy(); | ||
@@ -81,3 +92,3 @@ percy.set('abc', {}, function(error, model){ | ||
var percy = new Percy('thing', createMockConnector(), createMockValidator()); | ||
var percy = createTestPercy(); | ||
@@ -97,3 +108,3 @@ percy.set('abc', {}, function(error, model){ | ||
var percy = new Percy('thing', createMockConnector(), createMockValidator()); | ||
var percy = createTestPercy(); | ||
@@ -113,3 +124,3 @@ percy.set('abc', {}, function(error, model){ | ||
var percy = new Percy('thing', createMockConnector(), createMockValidator()); | ||
var percy = createTestPercy(); | ||
@@ -125,5 +136,5 @@ percy.get('abc', function(error, model){ | ||
var percy = new Percy('thing', createMockConnector(), createMockValidator()); | ||
var percy = createTestPercy(); | ||
percy.add('abc', {}, function(error, model){ | ||
percy.add({}, function(error, model){ | ||
t.pass('model added'); | ||
@@ -137,10 +148,10 @@ }); | ||
var percy = new Percy('thing', createMockConnector(), createMockValidator()); | ||
var percy = createTestPercy(); | ||
percy.add('abc', {}, function(error, model){ | ||
percy.add({}, function(error, model){ | ||
t.pass('model added'); | ||
}); | ||
percy.add('abc', {}, function(error, model){ | ||
t.ok(error, 'error thrown as expected'); | ||
percy.add(model, function(error, model){ | ||
t.ok(error, 'error thrown as expected'); | ||
}); | ||
}); | ||
@@ -153,8 +164,8 @@ }); | ||
var percy = new Percy('thing', createMockConnector(), createMockValidator()); | ||
var percy = createTestPercy(); | ||
percy.add('abc', {}, function(error, model){ | ||
percy.add({}, function(error, model){ | ||
t.pass('model added'); | ||
percy.remove('abc', function(error, model){ | ||
percy.remove(model.id, function(error, model){ | ||
t.equal(error, null, 'model removed'); | ||
@@ -169,3 +180,3 @@ }); | ||
var percy = new Percy('thing', createMockConnector(), createMockValidator()); | ||
var percy = createTestPercy(); | ||
@@ -181,8 +192,8 @@ percy.remove('abc', function(error, model){ | ||
var percy = new Percy('thing', createMockConnector(), createMockValidator()); | ||
var percy = createTestPercy(); | ||
percy.add(['abc', '123'], {a:1}, function(error, model){ | ||
percy.add({a:1}, function(error, model){ | ||
t.pass('model added'); | ||
percy.replace(['abc', '123'], {b:2}, function(error, model){ | ||
percy.replace(model.id, {b:2}, function(error, model){ | ||
t.deepEqual(model, {b:2}, 'replace succeded'); | ||
@@ -197,3 +208,3 @@ }); | ||
var percy = new Percy('thing', createMockConnector(), createMockValidator()); | ||
var percy = createTestPercy(); | ||
@@ -209,8 +220,11 @@ percy.replace('abc', {b:2}, function(error, model){ | ||
var percy = new Percy('thing', createMockConnector(), createMockValidator()); | ||
var percy = createTestPercy(); | ||
percy.add('abc', {a:1}, function(error, model){ | ||
percy.add({a:1}, function(error, model){ | ||
t.pass('model added'); | ||
percy.update('abc', {b:2}, function(error, model){ | ||
percy.update(model.id, {b:2}, function(error, model){ | ||
// remove the id for comparison | ||
delete model.id; | ||
t.deepEqual(model, {a:1, b:2}, 'update succeded'); | ||
@@ -225,3 +239,3 @@ }); | ||
var percy = new Percy('thing', createMockConnector(), createMockValidator()); | ||
var percy = createTestPercy(); | ||
@@ -231,2 +245,19 @@ percy.update('abc', {b:2}, function(error, model){ | ||
}); | ||
}); | ||
test('handels error from createKey', function(t){ | ||
t.plan(2); | ||
var percy = createTestPercy(), | ||
testError = new Error('BOOM!!'); | ||
percy.createId = function(callback){ | ||
callback(testError); | ||
}; | ||
percy.add({foo: 'bar'}, function(error){ | ||
t.ok(error, 'error passed as expected'); | ||
t.equal(error, testError, 'correct error passed'); | ||
}); | ||
}); |
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
14002
6
372
34
+ Addedkgo@1.3.0(transitive)
- Removedkgo@0.1.2(transitive)
Updatedkgo@^1.0.0