Skip to content

Commit

Permalink
[refactor] [api] Upgrade to CouchDB 2.0
Browse files Browse the repository at this point in the history
  * Removes `jugglingdb-nano` #32
  * Adds support for CouchDB Mango queries
  * Adds examples
  * Should still support JugglingDB interfaces
  • Loading branch information
Marak committed Dec 30, 2016
1 parent 7211a32 commit 4ce1857
Show file tree
Hide file tree
Showing 18 changed files with 663 additions and 89 deletions.
21 changes: 21 additions & 0 deletions examples/datasource-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var resource = require('../'),
creature = resource.define('creature');

creature.persist('couchdb');

// add some properties
creature.property('name');
creature.property('type');

// add ctime and mtime timestamps
creature.timestamps();

creature.all(function (err, results) {
console.log(err);
console.log(results);
});

//
// PROTIP: Try console.log(creature.methods)
//
// console.log(creature.methods);
31 changes: 31 additions & 0 deletions examples/datasource-create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var resource = require('../'),
hook = resource.define('hook');

hook.persist({
type: 'couch2'
});

// add some properties
hook.property('name', {
unique: true
});
hook.property('type');

// add ctime and mtime timestamps
hook.timestamps();


hook.create({ name: 'foo2', owner: 'marak' }, function (err, result) {
if (err) {
throw err;
}
//console.log(err);
console.log(result);
console.log(result.id);
console.log(result.type);
});

//
// PROTIP: Try console.log(hook.methods)
//
// console.log(hook.methods);
16 changes: 16 additions & 0 deletions examples/datasource-createIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var resource = require('../'),
creature = resource.define('hook');

creature.persist({
type: 'couch2'
});

creature.model.createIndex({ name: 'generic', index: { fields: ['model', 'owner', 'name'] }}, function (err, result) {
console.log(err);
console.log(result);
});

//
// PROTIP: Try console.log(creature.methods)
//
// console.log(creature.methods);
35 changes: 35 additions & 0 deletions examples/datasource-destroy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var resource = require('../'),
creature = resource.define('creature');

creature.persist('couch2');

// add some properties
creature.property('name');
creature.property('type');

// add ctime and mtime timestamps
creature.timestamps();

console.log(creature.model)

creature.create({ name: 'bobby', type: 'dragon' }, function (err, result) {
console.log('created', err);
console.log(result);
console.log(result.id);
console.log(result.type);
creature.destroy(result.id, function (err, _deleted) {
console.log('destroyed', err, _deleted);
creature.get(result.id, function (err, _result) {
console.log(err);
console.log(_result);
console.log(_result.id);
console.log(_result.type);
});
});
});


//
// PROTIP: Try console.log(creature.methods)
//
// console.log(creature.methods);
28 changes: 28 additions & 0 deletions examples/datasource-find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var resource = require('../'),
hook = resource.define('hook');

hook.persist({
type: 'couch2',
database: 'hook'
}); // could also try, hook.persist('fs')

// add some properties
hook.property('name');
hook.property('owner');

// add ctime and mtime timestamps
hook.timestamps();

console.log(hook.model)

hook.find({ owner: 'marak', name: "echo" }, function (err, result) {
console.log(err);
console.log(result);
console.log(result.id);
console.log(result.type);
});

//
// PROTIP: Try console.log(hook.methods)
//
// console.log(hook.methods);
34 changes: 34 additions & 0 deletions examples/datasource-update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var resource = require('../'),
creature = resource.define('creature');

creature.persist('couch2');

// add some properties
creature.property('name');
creature.property('type');

// add ctime and mtime timestamps
creature.timestamps();

console.log(creature.model)

creature.create({ name: 'bobby', type: 'dragon' }, function (err, result) {
console.log(err);
console.log(result);
console.log(result.id);
console.log(result.type);

creature.update({ id: result.id, name: 'bobby', type: 'unicorn' }, function (err, result) {
console.log(err);
console.log(result);
console.log(result.id);
console.log(result.type);
});

});


//
// PROTIP: Try console.log(creature.methods)
//
// console.log(creature.methods);
4 changes: 3 additions & 1 deletion examples/persistence.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var resource = require('../'),
creature = resource.define('creature');

creature.persist('memory'); // could also try, creature.persist('fs')
creature.persist('couchdb'); // could also try, creature.persist('fs')

// add some properties
creature.property('name');
Expand All @@ -10,6 +10,8 @@ creature.property('type');
// add ctime and mtime timestamps
creature.timestamps();

console.log(creature.model)

creature.create({ name: 'bobby', type: 'dragon' }, function (err, result) {
console.log(err);
console.log(result);
Expand Down
94 changes: 94 additions & 0 deletions lib/couch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
var comfy = require('../vendor/Comfy');
var checkUniqueKey = require('./datasource/checkUniqueKey');

module.exports = function (opts) {
var couch = {};
var dbname = opts.db || "resource";
var model = opts.model || "defaultModel";
var resource = opts.resource || {
schema: {
properties: {}
}
};

var db = comfy(opts.url, {
user: opts.username,
password: opts.password
});

couch.create = function (entry, next) {
entry.model = model;
db.insert(dbname, entry, next);
};

couch.all = function (query, next) {
if (typeof query.where === "undefined") {
query.where = {
model: 'creature'
};
}
query.where.model = model;
// console.log('performing query', dbname, query.where)
db.find(dbname, { selector: query.where, limit: 10000 }, function(err, body){
if (err) {
return next(err);
}
// console.log('just found', query, err, body)
body.docs = body.docs.map(function(doc){
doc.id = doc._id;
doc.save = function (cb) {
checkUniqueKey(resource, doc, function (err, _data){
if (err) {
return cb(err);
}
db.edit(dbname, doc.id, doc, cb);
});
}
doc.destroy = function (cb) {
db.remove(dbname, doc.id, doc._rev, cb);
}
delete doc._id;
return doc;
})
next(null, body.docs);
});
};

couch.find = function (id, next) {
db.get(dbname, id, function(err, doc){
if (err) {
return next(err)
}
doc.save = function (cb) {
doc.id = doc._id;
checkUniqueKey(resource, doc, function (err, _data){
if (err) {
return cb(err);
}
db.edit(dbname, id, doc, cb);
});
delete doc._id;
}
doc.destroy = function (cb) {
db.remove(dbname, id, doc._rev, cb);
}
next(null, doc);
});
};

/*
couch.destroy = function (id, next) {
db.remove(dbname, id, undefined, cb);
};
*/
couch.updateOrCreate = function (doc, next) {
db.edit(dbname, doc.id, doc, next);
}
couch.update = function (opts, next) {
db.update(dbname, opts.id, opts, next);
};
couch.createIndex = function (opts, next) {
db.create_index(dbname, opts, next);
}
return couch;
};
Loading

0 comments on commit 4ce1857

Please sign in to comment.