Skip to content

Commit

Permalink
Fix clearCache
Browse files Browse the repository at this point in the history
  • Loading branch information
biggora committed Feb 12, 2013
1 parent 74bf017 commit f79da3a
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/abstract-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ AbstractClass.remove = function remove(params, cb) {
if (stillConnecting(this.schema, this, arguments)) return;

this.schema.adapter.remove(this.modelName, params, function (err) {
clearCache(this);
// clearCache(this);
cb(err);
}.bind(this));
};
Expand Down Expand Up @@ -638,7 +638,7 @@ AbstractClass.update = function update(params, data, options, cb) {
if (stillConnecting(this.schema, this, arguments)) return;

this.schema.adapter.update(this.modelName, params, data, options, function (err, affected) {
clearCache(this);
// clearCache(this);
cb(err, affected);
}.bind(this));
};
Expand Down
122 changes: 122 additions & 0 deletions lib/adapters/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
exports.initialize = function initializeSchema(schema, callback) {
schema.adapter = new WebService();
process.nextTick(callback);
};

function WebService() {
this._models = {};
this.cache = {};
this.ids = {};
}

WebService.prototype.define = function defineModel(descr) {
var m = descr.model.modelName;
this._models[m] = descr;
};

WebService.prototype.getResourceUrl = function getResourceUrl(model) {
var url = this._models[model].settings.restPath;
if (!url) throw new Error('Resource url (restPath) for ' + model + ' is not defined');
return url;
};

WebService.prototype.getBlankReq = function () {
if (!this.csrfToken) {
this.csrfToken = $('meta[name=csrf-token]').attr('content');
this.csrfParam = $('meta[name=csrf-param]').attr('content');
}
var req = {};
req[this.csrfParam] = this.csrfToken;
return req;
}

WebService.prototype.create = function create(model, data, callback) {
var req = this.getBlankReq();
req[model] = data;
$.post(this.getResourceUrl(model) + '.json', req, function (res) {
if (res.code === 200) {
callback(null, res.data.id);
} else {
callback(res.error);
}
}, 'json');
// this.cache[model][id] = data;
};

WebService.prototype.updateOrCreate = function (model, data, callback) {
var mem = this;
this.exists(model, data.id, function (err, exists) {
if (exists) {
mem.save(model, data, callback);
} else {
mem.create(model, data, function (err, id) {
data.id = id;
callback(err, data);
});
}
});
};

WebService.prototype.save = function save(model, data, callback) {
var req = this.getBlankReq();
req._method = 'PUT';
req[model] = data;
$.post(this.getResourceUrl(model) + '/' + data.id + '.json', req, function (res) {
if (res.code === 200) {
callback(null, res.data);
} else {
callback(res.error);
}
}, 'json');
};

WebService.prototype.exists = function exists(model, id, callback) {
$.getJSON(this.getResourceUrl(model) + '/' + id + '.json', function (res) {
if (res.code === 200) {
callback(null, true);
} else if (res.code === 404) {
callback(null, false);
} else {
callback(res.error);
}
});
};

WebService.prototype.find = function find(model, id, callback) {
$.getJSON(this.getResourceUrl(model) + '/' + id + '.json', function (res) {
if (res.code === 200) {
callback(null, res.data);
} else {
callback(res.error);
}
});
};

WebService.prototype.destroy = function destroy(model, id, callback) {
delete this.cache[model][id];
callback();
};

WebService.prototype.all = function all(model, filter, callback) {
$.getJSON(this.getResourceUrl(model) + '.json?query=' + JSON.stringify(filter), function (res) {
if (res.code === 200) {
callback(null, res.data);
} else {
callback(res.error);
}
});
};

WebService.prototype.destroyAll = function destroyAll(model, callback) {
throw new Error('Not supported');
};

WebService.prototype.count = function count(model, callback, where) {
throw new Error('Not supported');
};

WebService.prototype.updateAttributes = function (model, id, data, callback) {
data.id = id;
this.save(model, data, callback);
};

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "caminte",
"description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite",
"version": "0.0.1",
"version": "0.0.2",
"author": "Aleksej Gordejev <[email protected]> (http://www.gordejev.lv)",
"keywords": [
"caminte",
Expand Down

0 comments on commit f79da3a

Please sign in to comment.