Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First functions implemented (still incompleted and WIP!)
- Loading branch information
0 parents
commit f0ea163
Showing
6 changed files
with
218 additions
and
0 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 @@ | ||
node_modules |
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,56 @@ | ||
## JugglingDB-Arango | ||
|
||
Arango adapter for jugglingdb. | ||
|
||
## Usage | ||
|
||
To use it you need `jugglingdb@0.2.x`. | ||
|
||
1. Setup dependencies in `package.json`: | ||
|
||
```json | ||
{ | ||
... | ||
"dependencies": { | ||
"jugglingdb": "0.2.x", | ||
"jugglingdb-arango": "latest" | ||
}, | ||
... | ||
} | ||
``` | ||
|
||
2. Use: | ||
|
||
```javascript | ||
var Schema = require('jugglingdb').Schema; | ||
var schema = new Schema('arango'); | ||
``` | ||
|
||
## Running tests | ||
|
||
Make sure you have arango server running on default port, then run | ||
|
||
npm test | ||
|
||
## MIT License | ||
|
||
Copyright (C) 2012 by Andreas Streichardt | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
|
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 @@ | ||
module.exports = require('./lib/arango'); |
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,133 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
var arango = require('arango.client'); | ||
|
||
exports.initialize = function initializeSchema(schema, callback) { | ||
if (!arango) return; | ||
|
||
schema.client = new arango.Connection(); | ||
|
||
schema.adapter = new Arango(schema.client); | ||
callback(); | ||
}; | ||
|
||
ArangoId = function(fullId) { | ||
var parts = fullId.split('/'); | ||
this._id = parts[0]; | ||
this._rev = parts[1]; | ||
}; | ||
|
||
ArangoId.prototype.id = function() { | ||
return this._id; | ||
}; | ||
|
||
ArangoId.prototype.rev = function() { | ||
return this._rev; | ||
}; | ||
|
||
ArangoId.prototype.fullId = function() { | ||
return this._id + '/' + this._rev; | ||
}; | ||
|
||
ArangoId.prototype.setRev = function(rev) { | ||
this._rev = rev; | ||
}; | ||
|
||
function Arango(client) { | ||
this._client = client; | ||
this._models = {}; | ||
} | ||
|
||
Arango.prototype.define = function (descr) { | ||
if (!descr.settings) descr.settings = {}; | ||
this._models[descr.model.modelName] = descr; | ||
}; | ||
|
||
Arango.prototype.create = function (model, data, callback) { | ||
//console.log("CREATE", data); | ||
this._client.document.create(true, model, data, function(err, res, hdr) { | ||
callback(err, err ? null : res._id); | ||
}); | ||
}; | ||
|
||
Arango.prototype.find = function(model, id, callback) { | ||
this._client.document.get(id, function(err, data) { | ||
callback(err ? true : false, err ? null : data); | ||
}); | ||
}; | ||
|
||
Arango.prototype.exists = function (model, id, callback) { | ||
this._client.document.get(id, function (err, data) { | ||
callback(err, !err && data); | ||
}); | ||
}; | ||
|
||
Arango.prototype.all = function(model, filter, callback) { | ||
var _self = this; | ||
|
||
var func; | ||
var args = [model]; | ||
|
||
var resultFunction = function(err, res, hdr) { | ||
callback(err ? true : false, res.result.map(function(o) { o.id = o._id; delete o.id; return o; })); | ||
}; | ||
//console.log(filter); | ||
if (filter === null) { | ||
func = this._client.simple.list; | ||
args.push({}); | ||
} else { | ||
func = this._client.simple.example; | ||
args.push({}); | ||
args.push(filter.where); | ||
} | ||
args.push(resultFunction); | ||
func.apply(this, args); | ||
}; | ||
|
||
Arango.prototype.save = function(model, data, callback) { | ||
//console.log(data); | ||
var id = data.id; | ||
//console.log("SAVE", id); | ||
this._client.document.put(id, data, function(err, res) { | ||
if (!err) { | ||
var newId = new ArangoId(res._id); | ||
newId.setRev(res._rev); | ||
data.id = newId.fullId(); | ||
} | ||
//console.log("SAVE RESULT", arguments); | ||
callback(err); | ||
}); | ||
}; | ||
|
||
Arango.prototype.destroy = function(model, id, callback) { | ||
this._client.document.delete(id, function(err) { | ||
callback(err ? true : false); | ||
}); | ||
}; | ||
|
||
Arango.prototype.updateAttributes = function(model, id, newData, callback) { | ||
this.find(model, id, function(err, data) { | ||
if (err) { | ||
callback(err); | ||
} else { | ||
for (var key in newData) { | ||
if (newData.hasOwnProperty(key)) { | ||
data[key] = newData[key]; | ||
} | ||
} | ||
data.id = id; | ||
this.save(model, data, callback); | ||
} | ||
}.bind(this)); | ||
}; | ||
|
||
Arango.prototype.count = function(model, callback, where) { | ||
if (!where) { | ||
this._client.collection.count(model, function(err, result) { | ||
callback(err ? true : false, err ? null : result.count); | ||
}); | ||
} else { | ||
console.log(where); | ||
} | ||
}; |
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,19 @@ | ||
{ | ||
"name": "jugglingdb-arango", | ||
"version": "0.0.1", | ||
"description": "ArangoDb adapter for jugglingdb", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "nodeunit test/arango.js" | ||
}, | ||
"dependencies": { | ||
"arango.client": "latest" | ||
}, | ||
"devDependencies": { | ||
"jugglingdb": "= 0.2.x", | ||
"nodeunit": "latest" | ||
}, | ||
"repository": "", | ||
"author": "Andreas Streichardt <andreas.streichardt@gmail.com>", | ||
"license": "MIT" | ||
} |
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,8 @@ | ||
var jdb = require('jugglingdb'), | ||
Schema = jdb.Schema, | ||
test = jdb.test; | ||
|
||
var schema = new Schema(__dirname + '/..'); | ||
|
||
test(module.exports, schema); | ||
|