-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor] [api] Upgrade to CouchDB 2.0
* Removes `jugglingdb-nano` #32 * Adds support for CouchDB Mango queries * Adds examples * Should still support JugglingDB interfaces
- Loading branch information
Showing
18 changed files
with
663 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.