Merge branch 'master' of github1602:1602/jugglingdb

This commit is contained in:
Anatoliy Chakkaev 2013-03-19 14:19:39 +04:00
commit a90fe8da41
3 changed files with 95 additions and 29 deletions

View File

@ -43,66 +43,66 @@ check following list of available adapters
<!-- MongoDB -->
<tr>
<td><a href="http://www.mongodb.org"><img src="http://mongodb.ru/favicon.ico" alt="MongoDB" /></a> MongoDB</td>
<td><a href="https://github.com/1602/jugglingdb-mongodb">jugglingdb-mongodb</a></td>
<td><a href="/anatoliychakkaev">Anatoliy Chakkaev</a></td>
<td><a href="https://travis-ci.org/1602/jugglingdb-mongodb"><img src="https://travis-ci.org/1602/jugglingdb-mongodb.png?branch=master" alt="Build Status" /></a></td>
<td><a href="https://github.com/jugglingdb/mongodb-adapter">jugglingdb/mongodb-adapter</a></td>
<td><a href="https://github.com/anatoliychakkaev">Anatoliy Chakkaev</a></td>
<td><a href="https://travis-ci.org/jugglingdb/mongodb-adapter"><img src="https://travis-ci.org/jugglingdb/mongodb-adapter.png?branch=master" alt="Build Status" /></a></td>
</tr>
<!-- MySQL -->
<tr>
<td><a href="http://www.mysql.com/"><img src="https://github.com/1602/jugglingdb/raw/master/media/mysql.ico" style="vertical-align:middle"" alt="MySQL" /></a> MySQL</td>
<td><a href="https://github.com/1602/jugglingdb-mysql">jugglingdb-mysql</a></td>
<td><a href="/anatoliychakkaev">Anatoliy Chakkaev</a></td>
<td><a href="https://travis-ci.org/1602/jugglingdb-mysql"><img src="https://travis-ci.org/1602/jugglingdb-mysql.png?branch=master" alt="Build Status" /></a></td>
<td><a href="https://github.com/jugglingdb/mysql-adapter">jugglingdb/mysql</a></td>
<td><a href="https://github.com/dgsan">dgsan</a></td>
<td><a href="https://travis-ci.org/jugglingdb/mysql-adapter"><img src="https://travis-ci.org/jugglingdb/mysql-adapter.png?branch=master" alt="Build Status" /></a></td>
</tr>
<!-- CouchDB / nano -->
<tr>
<td><a href="http://couchdb.apache.org/"><img width="16" src="http://couchdb.apache.org/favicon.ico" style="vertical-align:middle"" alt="CouchDB" /></a> CouchDB / nano</td>
<td><a href="https://github.com/1602/jugglingdb-nano">jugglingdb-nano</a></td>
<td><a href="/nrw">Nicholas Westlake</a></td>
<td><a href="https://travis-ci.org/1602/jugglingdb-nano"><img src="https://travis-ci.org/1602/jugglingdb-nano.png?branch=master" alt="Build Status" /></a></td>
<td><a href="https://github.com/jugglingdb/nano-adapter">jugglingdb/nano-adapter</a></td>
<td><a href="https://github.com/nrw">Nicholas Westlake</a></td>
<td><a href="https://travis-ci.org/jugglingdb/nano-adapter"><img src="https://travis-ci.org/jugglingdb/nano-adapter.png?branch=master" alt="Build Status" /></a></td>
</tr>
<!-- PostgreSQL -->
<tr>
<td><a href="http://www.postgresql.org/"><img src="http://www.postgresql.org/favicon.ico" style="vertical-align:middle"" alt="PostgreSQL" /></a> PostgreSQL</td>
<td><a href="https://github.com/1602/jugglingdb-postgres">jugglingdb-postgres</a></td>
<td><a href="/anatoliychakkaev">Anatoliy Chakkaev</a></td>
<td><a href="https://travis-ci.org/1602/jugglingdb-postgres"><img src="https://travis-ci.org/1602/jugglingdb-postgres.png?branch=master" alt="Build Status" /></a></td>
<td><a href="https://github.com/jugglingdb/postgres-adapter">jugglingdb/postgres-adapter</a></td>
<td><a href="https://github.com/anatoliychakkaev">Anatoliy Chakkaev</a></td>
<td><a href="https://travis-ci.org/jugglingdb/postgres-adapter"><img src="https://travis-ci.org/jugglingdb/postgres-adapter.png?branch=master" alt="Build Status" /></a></td>
</tr>
<!-- Redis -->
<tr>
<td><a href="http://redis.io/"><img src="http://redis.io/images/favicon.png" alt="Redis" /></a> Redis</td>
<td><a href="https://github.com/1602/jugglingdb-redis">jugglingdb-redis</a></td>
<td><a href="/anatoliychakkaev">Anatoliy Chakkaev</a></td>
<td><a href="https://travis-ci.org/1602/jugglingdb-redis"><img src="https://travis-ci.org/1602/jugglingdb-redis.png?branch=master" alt="Build Status" /></a></td>
<td><a href="https://github.com/jugglingdb/redis-adapter">jugglingdb-redis</a></td>
<td><a href="https://github.com/anatoliychakkaev">Anatoliy Chakkaev</a></td>
<td><a href="https://travis-ci.org/jugglingdb/redis-adapter"><img src="https://travis-ci.org/jugglingdb/redis-adapter.png?branch=master" alt="Build Status" /></a></td>
</tr>
<!-- SQLite -->
<tr>
<td><a href="http://www.sqlite.org/"><img width="16" src="https://github.com/1602/jugglingdb/raw/master/media/sqlite.png" style="vertical-align:middle"" alt="SQLite" /></a> SQLite</td>
<td><a href="https://github.com/1602/jugglingdb-sqlite3">jugglingdb-sqlite3</a></td>
<td><a href="/anatoliychakkaev">Anatoliy Chakkaev</a></td>
<td><a href="https://travis-ci.org/1602/jugglingdb-sqlite3"><img src="https://travis-ci.org/1602/jugglingdb-sqlite3.png?branch=master" alt="Build Status" /></a></td>
<td><a href="https://github.com/jugglingdb/sqlite3-adapter">jugglingdb/sqlite3-adapter</a></td>
<td><a href="https://github.com/anatoliychakkaev">Anatoliy Chakkaev</a></td>
<td><a href="https://travis-ci.org/jugglingdb/sqlite3-adapter"><img src="https://travis-ci.org/jugglingdb/sqlite3-adapter.png?branch=master" alt="Build Status" /></a></td>
</tr>
<tr>
<td>WebService</td>
<td>built-in</td>
<td><a href="/anatoliychakkaev">Anatoliy Chakkaev</a></td>
<td><a href="https://github.com/anatoliychakkaev">Anatoliy Chakkaev</a></td>
<td>n/a</td>
</tr>
<tr>
<td>Memory (bogus)</td>
<td>built-in</td>
<td><a href="/anatoliychakkaev">Anatoliy Chakkaev</a></td>
<td><a href="https://github.com/anatoliychakkaev">Anatoliy Chakkaev</a></td>
<td>n/a</td>
</tr>
<tr>
<td>Neo4j</td>
<td>built-in</td>
<td><a href="/anatoliychakkaev"><strike>Anatoliy Chakkaev</strike></a> /!\ Looking for
<td>built-in <i>TODO: move</i></td>
<td><a href="https://github.com/anatoliychakkaev"><strike>Anatoliy Chakkaev</strike></a> <u>/!\</u> Looking for
maintainer</td>
<td>n/a</td>
</tr>
@ -118,7 +118,7 @@ check following list of available adapters
If you want to create your own jugglingdb adapter, you should publish your
adapter package with name `jugglingdb-ADAPTERNAME`. Creating adapter is simple,
check [jugglingdb-redis](https://github.com/1602/jugglingdb-redis) for example. JugglingDB core
check [jugglingdb/redis-adapter](https://github.com/jugglingdb/redis-adapter) for example. JugglingDB core
exports common tests each adapter should pass, you could create your adapter in
TDD style, check that adapter pass all tests defined in `test/common_test.js`.

View File

@ -91,7 +91,7 @@ AbstractClass.prototype._initProperties = function (data, applySetters) {
try {
self.__data[attr] = JSON.parse(self.__data[attr] + '');
} catch (e) {
console.log(e.stack);
self.__data[attr] = String(self.__data[attr]);
}
}
if (type.name === 'Array' || typeof type === 'object' && type.constructor.name === 'Array') {

View File

@ -9,8 +9,59 @@ function WebService() {
this.ids = {};
}
WebService.prototype.installPostProcessor = function installPostProcessor(descr) {
var dates = [];
Object.keys(descr.properties).forEach(function(column) {
if (descr.properties[column].type.name === 'Date') {
dates.push(column);
}
});
var postProcessor = function(model) {
var max = dates.length;
for (var i = 0; i < max; i++) {
var column = dates[i];
if (model[column]) {
model[column] = new Date(model[column]);
}
};
};
descr.postProcessor = postProcessor;
};
WebService.prototype.preProcess = function preProcess(data) {
var result = {};
Object.keys(data).forEach(function(key) {
if (data[key] != null) {
result[key] = data[key];
}
})
return result;
};
WebService.prototype.postProcess = function postProcess(model, data) {
var postProcessor = this._models[model].postProcessor;
if (postProcessor && data) {
postProcessor(data);
}
};
WebService.prototype.postProcessMultiple = function postProcessMultiple(model, data) {
var postProcessor = this._models[model].postProcessor;
if (postProcessor) {
var max = data.length;
for (var i = 0; i < max; i++) {
if (data[i]) {
postProcessor(data[i]);
}
};
}
};
WebService.prototype.define = function defineModel(descr) {
var m = descr.model.modelName;
this.installPostProcessor(descr);
this._models[m] = descr;
};
@ -32,7 +83,7 @@ WebService.prototype.getBlankReq = function () {
WebService.prototype.create = function create(model, data, callback) {
var req = this.getBlankReq();
req[model] = data;
req[model] = this.preProcess(data);
$.post(this.getResourceUrl(model) + '.json', req, function (res) {
if (res.code === 200) {
callback(null, res.data.id);
@ -58,11 +109,13 @@ WebService.prototype.updateOrCreate = function (model, data, callback) {
};
WebService.prototype.save = function save(model, data, callback) {
var _this = this;
var req = this.getBlankReq();
req._method = 'PUT';
req[model] = data;
req[model] = this.preProcess(data);
$.post(this.getResourceUrl(model) + '/' + data.id + '.json', req, function (res) {
if (res.code === 200) {
_this.postProcess(model, res.data);
callback(null, res.data);
} else {
callback(res.error);
@ -83,8 +136,10 @@ WebService.prototype.exists = function exists(model, id, callback) {
};
WebService.prototype.find = function find(model, id, callback) {
var _this = this;
$.getJSON(this.getResourceUrl(model) + '/' + id + '.json', function (res) {
if (res.code === 200) {
_this.postProcess(model, res.data);
callback(null, res.data);
} else {
callback(res.error);
@ -93,13 +148,24 @@ WebService.prototype.find = function find(model, id, callback) {
};
WebService.prototype.destroy = function destroy(model, id, callback) {
delete this.cache[model][id];
callback();
var _this = this;
var req = this.getBlankReq();
req._method = 'DELETE';
$.post(this.getResourceUrl(model) + '/' + id + '.json', req, function (res) {
if (res.code === 200) {
//delete _this.cache[model][id];
callback(null, res.data);
} else {
callback(res.error);
}
}, 'json');
};
WebService.prototype.all = function all(model, filter, callback) {
$.getJSON(this.getResourceUrl(model) + '.json?query=' + JSON.stringify(filter), function (res) {
var _this = this;
$.getJSON(this.getResourceUrl(model) + '.json?query=' + encodeURIComponent(JSON.stringify(filter)), function (res) {
if (res.code === 200) {
_this.postProcessMultiple(model, res.data);
callback(null, res.data);
} else {
callback(res.error);