Merge pull request #844 from ParsePlatform/nlutsenko.hooks

Move HooksController to use MongoCollection instead of direct Mongo access.
This commit is contained in:
Nikita Lutsenko
2016-03-07 13:20:41 -08:00
2 changed files with 86 additions and 99 deletions

View File

@@ -1,4 +1,3 @@
let mongodb = require('mongodb'); let mongodb = require('mongodb');
let Collection = mongodb.Collection; let Collection = mongodb.Collection;
@@ -18,8 +17,7 @@ export default class MongoCollection {
return this._rawFind(query, { skip, limit, sort }) return this._rawFind(query, { skip, limit, sort })
.catch(error => { .catch(error => {
// Check for "no geoindex" error // Check for "no geoindex" error
if (error.code != 17007 || if (error.code != 17007 || !error.message.match(/unable to find index for .geoNear/)) {
!error.message.match(/unable to find index for .geoNear/)) {
throw error; throw error;
} }
// Figure out what key needs an index // Figure out what key needs an index
@@ -59,6 +57,13 @@ export default class MongoCollection {
}) })
} }
// Atomically updates data in the database for a single (first) object that matched the query
// If there is nothing that matches the query - does insert
// Postgres Note: `INSERT ... ON CONFLICT UPDATE` that is available since 9.5.
upsertOne(query, update) {
return this._mongoCollection.update(query, update, { upsert: true });
}
// Atomically find and delete an object based on query. // Atomically find and delete an object based on query.
// The result is the promise with an object that was in the database before deleting. // The result is the promise with an object that was in the database before deleting.
// Postgres Note: Translates directly to `DELETE * FROM ... RETURNING *`, which will return data after delete is done. // Postgres Note: Translates directly to `DELETE * FROM ... RETURNING *`, which will return data after delete is done.
@@ -70,6 +75,10 @@ export default class MongoCollection {
}); });
} }
remove(query) {
return this._mongoCollection.remove(query);
}
drop() { drop() {
return this._mongoCollection.drop(); return this._mongoCollection.drop();
} }

View File

@@ -8,104 +8,91 @@ import * as request from "request";
const DefaultHooksCollectionName = "_Hooks"; const DefaultHooksCollectionName = "_Hooks";
export class HooksController { export class HooksController {
_applicationId: string; _applicationId:string;
_collectionPrefix: string; _collectionPrefix:string;
_collection; _collection;
constructor(applicationId: string, collectionPrefix: string = '') { constructor(applicationId:string, collectionPrefix:string = '') {
this._applicationId = applicationId; this._applicationId = applicationId;
this._collectionPrefix = collectionPrefix; this._collectionPrefix = collectionPrefix;
} }
database() { load() {
return DatabaseAdapter.getDatabaseConnection(this._applicationId, this._collectionPrefix); return this._getHooks().then(hooks => {
hooks = hooks || [];
hooks.forEach((hook) => {
this.addHookToTriggers(hook);
});
});
} }
collection() { getCollection() {
if (this._collection) { if (this._collection) {
return Promise.resolve(this._collection) return Promise.resolve(this._collection)
} }
return this.database().rawCollection(DefaultHooksCollectionName).then((collection) => {
let database = DatabaseAdapter.getDatabaseConnection(this._applicationId, this._collectionPrefix);
return database.adaptiveCollection(DefaultHooksCollectionName).then(collection => {
this._collection = collection; this._collection = collection;
return collection; return collection;
}); });
} }
getFunction(functionName) { getFunction(functionName) {
return this.getOne({functionName: functionName}) return this._getHooks({ functionName: functionName }, 1).then(results => results[0]);
} }
getFunctions() { getFunctions() {
return this.get({functionName: { $exists: true }}) return this._getHooks({ functionName: { $exists: true } });
} }
getTrigger(className, triggerName) { getTrigger(className, triggerName) {
return this.getOne({className: className, triggerName: triggerName }) return this._getHooks({ className: className, triggerName: triggerName }, 1).then(results => results[0]);
} }
getTriggers() { getTriggers() {
return this.get({className: { $exists: true }, triggerName: { $exists: true }}) return this._getHooks({ className: { $exists: true }, triggerName: { $exists: true } });
} }
deleteFunction(functionName) { deleteFunction(functionName) {
triggers.removeFunction(functionName, this._applicationId); triggers.removeFunction(functionName, this._applicationId);
return this.delete({functionName: functionName}); return this._removeHooks({ functionName: functionName });
} }
deleteTrigger(className, triggerName) { deleteTrigger(className, triggerName) {
triggers.removeTrigger(triggerName, className, this._applicationId); triggers.removeTrigger(triggerName, className, this._applicationId);
return this.delete({className: className, triggerName: triggerName}); return this._removeHooks({ className: className, triggerName: triggerName });
} }
delete(query) { _getHooks(query, limit) {
return this.collection().then((collection) => { let options = limit ? { limit: limit } : undefined;
return collection.remove(query) return this.getCollection().then(collection => collection.find(query, options));
}).then( (res) => { }
_removeHooks(query) {
return this.getCollection().then(collection => {
return collection.remove(query);
}).then(() => {
return {}; return {};
}, 1);
}
getOne(query) {
return this.collection()
.then(coll => coll.findOne(query, {_id: 0}))
.then(hook => {
return hook;
}); });
} }
get(query) {
return this.collection()
.then(coll => coll.find(query, {_id: 0}).toArray())
.then(hooks => {
return hooks;
});
}
getHooks() {
return this.collection()
.then(coll => coll.find({}, {_id: 0}).toArray())
.then(hooks => {
return hooks;
}, () => ([]))
}
saveHook(hook) { saveHook(hook) {
var query; var query;
if (hook.functionName && hook.url) { if (hook.functionName && hook.url) {
query = {functionName: hook.functionName } query = { functionName: hook.functionName }
} else if (hook.triggerName && hook.className && hook.url) { } else if (hook.triggerName && hook.className && hook.url) {
query = { className: hook.className, triggerName: hook.triggerName } query = { className: hook.className, triggerName: hook.triggerName }
} else { } else {
throw new Parse.Error(143, "invalid hook declaration"); throw new Parse.Error(143, "invalid hook declaration");
} }
return this.collection().then((collection) => { return this.getCollection()
return collection.update(query, hook, {upsert: true}) .then(collection => collection.upsertOne(query, hook))
}).then(function(res){ .then(() => {
return hook; return hook;
}) });
} }
addHookToTriggers(hook) { addHookToTriggers(hook) {
var wrappedFunction = wrapToHTTPRequest(hook); var wrappedFunction = wrapToHTTPRequest(hook);
wrappedFunction.url = hook.url; wrappedFunction.url = hook.url;
@@ -114,13 +101,13 @@ export class HooksController {
} else { } else {
triggers.addFunction(hook.functionName, wrappedFunction, null, this._applicationId); triggers.addFunction(hook.functionName, wrappedFunction, null, this._applicationId);
} }
} }
addHook(hook) { addHook(hook) {
this.addHookToTriggers(hook); this.addHookToTriggers(hook);
return this.saveHook(hook); return this.saveHook(hook);
} }
createOrUpdateHook(aHook) { createOrUpdateHook(aHook) {
var hook; var hook;
if (aHook && aHook.functionName && aHook.url) { if (aHook && aHook.functionName && aHook.url) {
@@ -132,19 +119,19 @@ export class HooksController {
hook.className = aHook.className; hook.className = aHook.className;
hook.url = aHook.url; hook.url = aHook.url;
hook.triggerName = aHook.triggerName; hook.triggerName = aHook.triggerName;
} else { } else {
throw new Parse.Error(143, "invalid hook declaration"); throw new Parse.Error(143, "invalid hook declaration");
} }
return this.addHook(hook); return this.addHook(hook);
}; };
createHook(aHook) { createHook(aHook) {
if (aHook.functionName) { if (aHook.functionName) {
return this.getFunction(aHook.functionName).then((result) => { return this.getFunction(aHook.functionName).then((result) => {
if (result) { if (result) {
throw new Parse.Error(143,`function name: ${aHook.functionName} already exits`); throw new Parse.Error(143, `function name: ${aHook.functionName} already exits`);
} else { } else {
return this.createOrUpdateHook(aHook); return this.createOrUpdateHook(aHook);
} }
@@ -152,49 +139,39 @@ export class HooksController {
} else if (aHook.className && aHook.triggerName) { } else if (aHook.className && aHook.triggerName) {
return this.getTrigger(aHook.className, aHook.triggerName).then((result) => { return this.getTrigger(aHook.className, aHook.triggerName).then((result) => {
if (result) { if (result) {
throw new Parse.Error(143,`class ${aHook.className} already has trigger ${aHook.triggerName}`); throw new Parse.Error(143, `class ${aHook.className} already has trigger ${aHook.triggerName}`);
} }
return this.createOrUpdateHook(aHook); return this.createOrUpdateHook(aHook);
}); });
} }
throw new Parse.Error(143, "invalid hook declaration"); throw new Parse.Error(143, "invalid hook declaration");
}; };
updateHook(aHook) { updateHook(aHook) {
if (aHook.functionName) { if (aHook.functionName) {
return this.getFunction(aHook.functionName).then((result) => { return this.getFunction(aHook.functionName).then((result) => {
if (result) { if (result) {
return this.createOrUpdateHook(aHook); return this.createOrUpdateHook(aHook);
} }
throw new Parse.Error(143,`no function named: ${aHook.functionName} is defined`); throw new Parse.Error(143, `no function named: ${aHook.functionName} is defined`);
}); });
} else if (aHook.className && aHook.triggerName) { } else if (aHook.className && aHook.triggerName) {
return this.getTrigger(aHook.className, aHook.triggerName).then((result) => { return this.getTrigger(aHook.className, aHook.triggerName).then((result) => {
if (result) { if (result) {
return this.createOrUpdateHook(aHook); return this.createOrUpdateHook(aHook);
} }
throw new Parse.Error(143,`class ${aHook.className} does not exist`); throw new Parse.Error(143, `class ${aHook.className} does not exist`);
}); });
} }
throw new Parse.Error(143, "invalid hook declaration"); throw new Parse.Error(143, "invalid hook declaration");
}; };
load() {
return this.getHooks().then((hooks) => {
hooks = hooks || [];
hooks.forEach((hook) => {
this.addHookToTriggers(hook);
});
});
}
} }
function wrapToHTTPRequest(hook) { function wrapToHTTPRequest(hook) {
return function(req, res) { return (req, res) => {
var jsonBody = {}; let jsonBody = {};
for(var i in req) { for (var i in req) {
jsonBody[i] = req[i]; jsonBody[i] = req[i];
} }
if (req.object) { if (req.object) {
@@ -205,30 +182,31 @@ function wrapToHTTPRequest(hook) {
jsonBody.original = req.original.toJSON(); jsonBody.original = req.original.toJSON();
jsonBody.original.className = req.original.className; jsonBody.original.className = req.original.className;
} }
var jsonRequest = {}; let jsonRequest = {
jsonRequest.headers = { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} },
jsonRequest.body = JSON.stringify(jsonBody); body: JSON.stringify(jsonBody)
};
request.post(hook.url, jsonRequest, function(err, httpResponse, body){
request.post(hook.url, jsonRequest, function (err, httpResponse, body) {
var result; var result;
if (body) { if (body) {
if (typeof body == "string") { if (typeof body == "string") {
try { try {
body = JSON.parse(body); body = JSON.parse(body);
} catch(e) { } catch (e) {
err = {error: "Malformed response", code: -1}; err = { error: "Malformed response", code: -1 };
} }
} }
if (!err) { if (!err) {
result = body.success; result = body.success;
err = body.error; err = body.error;
} }
} }
if (err) { if (err) {
return res.error(err); return res.error(err);
} else { } else {
return res.success(result); return res.success(result);
} }
}); });