Comparing version 0.0.17 to 0.0.18
@@ -14,3 +14,3 @@ /** | ||
if (schema.settings.url) { | ||
if (s.url) { | ||
var url = require('url').parse(schema.settings.url); | ||
@@ -54,5 +54,5 @@ s.host = url.hostname; | ||
this.server = arango.Connection(schema.settings.url); | ||
this.server.database.create(s.database).then(function(res) { | ||
this.server.database.create(s.database).then(function (res) { | ||
console.log("Database created: %j", res); | ||
}, function(err) { | ||
}, function (err) { | ||
if (err.errorNum !== 1207) { | ||
@@ -68,12 +68,13 @@ console.log("Failed to create database: %j", err); | ||
ArangoDB.prototype.define = function(descr) { | ||
if (!descr.settings) | ||
ArangoDB.prototype.define = function (descr) { | ||
if (!descr.settings) { | ||
descr.settings = {}; | ||
} | ||
var self = this; | ||
this._models[descr.model.modelName] = descr; | ||
self.client.collection.list().done(function(res) { | ||
self.client.collection.list().done(function (res) { | ||
var found = false; | ||
if (res && res.collections) { | ||
res.collections.forEach(function(collection) { | ||
res.collections.forEach(function (collection) { | ||
if (collection.name === descr.model.modelName) { | ||
@@ -84,5 +85,5 @@ found = true; | ||
if (!found) { | ||
self.client.collection.create(descr.model.modelName).then(function(res) { | ||
self.client.collection.create(descr.model.modelName).then(function (res) { | ||
console.log("result: %j", res); | ||
}, function(err) { | ||
}, function (err) { | ||
console.log("error: %j", err); | ||
@@ -95,14 +96,21 @@ }); | ||
ArangoDB.prototype.defineProperty = function(model, prop, params) { | ||
ArangoDB.prototype.defineProperty = function (model, prop, params) { | ||
this._models[model].properties[prop] = params; | ||
}; | ||
ArangoDB.prototype.collection = function(name) { | ||
if (!this.collections[name]) { | ||
this.collections[name] = new mongodb.Collection(this.client, name); | ||
ArangoDB.prototype.collection = function (name) { | ||
console.log("error: ", this.collections[name]); | ||
if (typeof this.collections[name] === 'undefined') { | ||
this.client.collection.create(name).then(function (res) { | ||
console.log("result: ", res); | ||
}, function (err) { | ||
console.log("error: ", err); | ||
}); | ||
// this.collections[name] = arango.collection(this.client, 'collection' ); | ||
} else { | ||
return this.collections[name]; | ||
} | ||
return this.collections[name]; | ||
}; | ||
ArangoDB.prototype.ensureIndex = function(model, fields, params, callback) { | ||
ArangoDB.prototype.ensureIndex = function (model, fields, params, callback) { | ||
this.collection(model).ensureIndex(fields, params); | ||
@@ -112,3 +120,3 @@ return callback(null); | ||
ArangoDB.prototype.create = function(model, data, callback) { | ||
ArangoDB.prototype.create = function (model, data, callback) { | ||
if (data.id === null) { | ||
@@ -118,12 +126,12 @@ delete data.id; | ||
this.client.document.create(model, data, {createCollection: true}) | ||
.then(function(res) { | ||
callback(null, res._key, res._rev); | ||
}, function(err) { | ||
callback(err, null); | ||
}); | ||
.then(function (res) { | ||
callback(null, res._key, res._rev); | ||
}, function (err) { | ||
callback(err, null); | ||
}); | ||
}; | ||
ArangoDB.prototype.save = function(model, data, callback) { | ||
ArangoDB.prototype.save = function (model, data, callback) { | ||
var id = data.id; | ||
this.collection(model).update({_id: id}, data, function(err) { | ||
this.collection(model).update({_id: id}, data, function (err) { | ||
callback(err); | ||
@@ -133,4 +141,4 @@ }); | ||
ArangoDB.prototype.exists = function(model, id, callback) { | ||
this._models[model].findById(id, function(err, data) { | ||
ArangoDB.prototype.exists = function (model, id, callback) { | ||
this._models[model].findById(id, function (err, data) { | ||
callback(err, !err && data); | ||
@@ -142,3 +150,3 @@ }); | ||
//query.count(1).exec({gender:"female",likes:"running"}).then(do_something); | ||
this.client.document.get(model + '/' + id, function(err, data) { | ||
this.client.document.get(model + '/' + id, function (err, data) { | ||
if (data) { | ||
@@ -158,3 +166,3 @@ data.id = id; | ||
return this.create(data, callback); | ||
this.find(model, data.id, function(err, inst) { | ||
this.find(model, data.id, function (err, inst) { | ||
if (err) | ||
@@ -166,3 +174,3 @@ return callback(err); | ||
delete data.id; | ||
adapter.create(model, data, function(err, id) { | ||
adapter.create(model, data, function (err, id) { | ||
if (err) | ||
@@ -201,80 +209,83 @@ return callback(err); | ||
} | ||
var querys = self.client.query.for('u').in(model) | ||
.filter('u._key > 0') | ||
// .sort('u.id DESC') | ||
.limit('0, 5') | ||
.return('{"id": _key}'); | ||
console.log(querys.string); | ||
self.client.query.count(1).exec("FOR u in " + model + " RETURN u", function(err, dts) { | ||
console.log("rest: %j", err, dts); | ||
callback(err, dts); | ||
}); | ||
/* | ||
self.client.document.list(model) | ||
.then(function(res) { | ||
var documents = [], docIds = []; | ||
if (res.documents) { | ||
docIds = res.documents.map(function(d) { | ||
return /([0-9]+)$/.exec(d)[0]; | ||
var querys = self.client.query.for('u').in(model) | ||
.filter('u._key > 0') | ||
// .sort('u.id DESC') | ||
.limit('0, 5') | ||
.return('{"id": _key}'); | ||
// console.log(querys.string); | ||
self.client.query.count(1).exec("FOR u in " + model + " RETURN u", function(err, dts) { | ||
console.log("rest: %j", err, dts); | ||
callback(err, dts); | ||
}); | ||
var count = docIds.length; | ||
if (count > 0) { | ||
docIds.forEach(function(id) { | ||
self.findById(model, id, function(err, data) { | ||
if (!err) | ||
documents.push(data) | ||
if (--count === 0) { | ||
return callback && callback(null, documents); | ||
} | ||
}); | ||
}); | ||
} else { | ||
return callback && callback(null, documents); | ||
} | ||
} else { | ||
return callback && callback(null, documents); | ||
} | ||
}, function(err) { | ||
return callback && callback(err, null); | ||
}); | ||
var cursor = this.collection(model).find(query); | ||
if (filter.order) { | ||
var keys = filter.order; | ||
if (typeof keys === 'string') { | ||
keys = keys.split(','); | ||
} | ||
var args = {}; | ||
for (index in keys) { | ||
var m = keys[index].match(/\s+(A|DE)SC$/); | ||
var key = keys[index]; | ||
key = key.replace(/\s+(A|DE)SC$/, '').trim(); | ||
if (m && m[1] === 'DE') { | ||
args[key] = -1; | ||
} else { | ||
args[key] = 1; | ||
} | ||
} | ||
cursor.sort(args); | ||
} | ||
if (filter.limit) { | ||
cursor.limit(filter.limit); | ||
} | ||
if (filter.skip) { | ||
cursor.skip(filter.skip); | ||
} else if (filter.offset) { | ||
cursor.skip(filter.offset); | ||
} | ||
cursor.toArray(function(err, data) { | ||
if (err) | ||
return callback(err); | ||
callback(null, data.map(function(o) { | ||
o.id = o._id; | ||
return o; | ||
})); | ||
}); | ||
*/ | ||
self.client.document.list(model) | ||
.then(function (res) { | ||
var documents = [], docIds = []; | ||
if (res.documents) { | ||
docIds = res.documents.map(function (d) { | ||
return /([0-9]+)$/.exec(d)[0]; | ||
}); | ||
var count = docIds.length; | ||
if (count > 0) { | ||
docIds.forEach(function (id) { | ||
self.findById(model, id, function (err, data) { | ||
if (!err) | ||
documents.push(data) | ||
if (--count === 0) { | ||
if(filter.where) { | ||
documents = documents.length ? documents.filter(helpers.applyFilter(filter.where)) : documents; | ||
} | ||
return callback && callback(null, documents); | ||
} | ||
}); | ||
}); | ||
} else { | ||
return callback && callback(null, documents); | ||
} | ||
} else { | ||
return callback && callback(null, documents); | ||
} | ||
}, function (err) { | ||
return callback && callback(err, null); | ||
}); | ||
/* | ||
var cursor = this.collection(model).find(query); | ||
console.log('cursor:',cursor) | ||
if (filter.order) { | ||
var keys = filter.order; | ||
if (typeof keys === 'string') { | ||
keys = keys.split(','); | ||
} | ||
var args = {}; | ||
for (index in keys) { | ||
var m = keys[index].match(/\s+(A|DE)SC$/); | ||
var key = keys[index]; | ||
key = key.replace(/\s+(A|DE)SC$/, '').trim(); | ||
if (m && m[1] === 'DE') { | ||
args[key] = -1; | ||
} else { | ||
args[key] = 1; | ||
} | ||
} | ||
cursor.sort(args); | ||
} | ||
if (filter.limit) { | ||
cursor.limit(filter.limit); | ||
} | ||
if (filter.skip) { | ||
cursor.skip(filter.skip); | ||
} else if (filter.offset) { | ||
cursor.skip(filter.offset); | ||
} | ||
cursor.toArray(function (err, data) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
callback(null, data.map(function (o) { | ||
o.id = o._id; | ||
return o; | ||
})); | ||
}); | ||
*/ | ||
}; | ||
@@ -292,6 +303,8 @@ | ||
ArangoDB.prototype.updateAttributes = function updateAttrs(model, id, data, callback) { | ||
this.collection(model).findAndModify({_id: id}, [['_id', 'asc']], {$set: data}, {}, callback); | ||
this.collection(model).findAndModify({_id: id}, [ | ||
['_id', 'asc'] | ||
], {$set: data}, {}, callback); | ||
}; | ||
ArangoDB.prototype.disconnect = function() { | ||
ArangoDB.prototype.disconnect = function () { | ||
this.client.close(); | ||
@@ -302,3 +315,3 @@ }; | ||
var query = {}; | ||
Object.keys(filter).forEach(function(k) { | ||
Object.keys(filter).forEach(function (k) { | ||
var cond = filter[k]; | ||
@@ -305,0 +318,0 @@ var spec = false; |
@@ -308,2 +308,7 @@ /** | ||
this.client.queryNodeIndex(model, 'id:*', function (err, nodes) { | ||
if(err && !nodes) { | ||
if(err.message && /NotFound/gi.test(err.message)) { | ||
err = null; | ||
} | ||
} | ||
if (nodes) { | ||
@@ -317,3 +322,3 @@ nodes = nodes.map(function (obj) { | ||
nodes = nodes ? nodes.filter(helpers.applyFilter(filter)) : nodes; | ||
if (filter.order) { | ||
if (nodes && filter.order) { | ||
var key = filter.order.split(' ')[0]; | ||
@@ -320,0 +325,0 @@ var dir = filter.order.split(' ')[1]; |
{ | ||
"name": "caminte", | ||
"description": "ORM for every database: redis, mysql, neo4j, mongodb, rethinkdb, postgres, sqlite, tingodb", | ||
"version": "0.0.17", | ||
"version": "0.0.18", | ||
"author": "Aleksej Gordejev <aleksej@gordejev.lv> (http://www.gordejev.lv)", | ||
@@ -6,0 +6,0 @@ "homepage": "http://camintejs.com/", |
Sorry, the diff of this file is not supported yet
605857
11260