loopback-datasource-juggler/lib/connectors/http.js

191 lines
5.0 KiB
JavaScript
Raw Normal View History

exports.initialize = function initializeSchema(dataSource, callback) {
2014-01-24 17:09:53 +00:00
dataSource.connector = new WebService();
process.nextTick(callback);
2013-01-20 16:05:45 +00:00
};
function WebService() {
2014-01-24 17:09:53 +00:00
this._models = {};
this.cache = {};
this.ids = {};
2013-01-20 16:05:45 +00:00
}
WebService.prototype.installPostProcessor = function installPostProcessor(descr) {
2014-01-24 17:09:53 +00:00
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) {
2014-01-24 17:09:53 +00:00
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) {
2014-01-24 17:09:53 +00:00
var postProcessor = this._models[model].postProcessor;
if (postProcessor && data) {
postProcessor(data);
}
};
WebService.prototype.postProcessMultiple = function postProcessMultiple(model, data) {
2014-01-24 17:09:53 +00:00
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]);
}
}
2014-01-24 17:09:53 +00:00
;
}
};
2013-01-20 16:05:45 +00:00
WebService.prototype.define = function defineModel(descr) {
2014-01-24 17:09:53 +00:00
var m = descr.model.modelName;
this.installPostProcessor(descr);
this._models[m] = descr;
2013-01-20 16:05:45 +00:00
};
WebService.prototype.getResourceUrl = function getResourceUrl(model) {
2014-01-24 17:09:53 +00:00
var url = this._models[model].settings.restPath;
if (!url) throw new Error('Resource url (restPath) for ' + model + ' is not defined');
return url;
2013-01-20 16:05:45 +00:00
};
WebService.prototype.getBlankReq = function () {
2014-01-24 17:09:53 +00:00
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;
2013-01-20 16:05:45 +00:00
}
WebService.prototype.create = function create(model, data, callback) {
2014-01-24 17:09:53 +00:00
var req = this.getBlankReq();
req[model] = this.preProcess(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;
2013-01-20 16:05:45 +00:00
};
WebService.prototype.updateOrCreate = function (model, data, callback) {
2014-01-24 17:09:53 +00:00
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);
});
}
});
2013-01-20 16:05:45 +00:00
};
WebService.prototype.save = function save(model, data, callback) {
2014-01-24 17:09:53 +00:00
var _this = this;
var req = this.getBlankReq();
req._method = 'PUT';
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);
}
}, 'json');
2013-01-20 16:05:45 +00:00
};
WebService.prototype.exists = function exists(model, id, callback) {
2014-01-24 17:09:53 +00:00
$.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);
}
});
2013-01-20 16:05:45 +00:00
};
WebService.prototype.find = function find(model, id, callback) {
2014-01-24 17:09:53 +00:00
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);
}
});
2013-01-20 16:05:45 +00:00
};
WebService.prototype.destroy = function destroy(model, id, callback) {
2014-01-24 17:09:53 +00:00
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');
2013-01-20 16:05:45 +00:00
};
WebService.prototype.all = function all(model, filter, callback) {
2014-01-24 17:09:53 +00:00
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);
}
});
2013-01-20 16:05:45 +00:00
};
WebService.prototype.destroyAll = function destroyAll(model, callback) {
2014-01-24 17:09:53 +00:00
throw new Error('Not supported');
2013-01-20 16:05:45 +00:00
};
WebService.prototype.count = function count(model, callback, where) {
2014-01-24 17:09:53 +00:00
throw new Error('Not supported');
2013-01-20 16:05:45 +00:00
};
WebService.prototype.updateAttributes = function (model, id, data, callback) {
2014-01-24 17:09:53 +00:00
data.id = id;
this.save(model, data, callback);
2013-01-20 16:05:45 +00:00
};