Skip to content

Instantly share code, notes, and snippets.

@TheStros
Last active April 23, 2020 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheStros/f8900764b01dba7124b912186b1447de to your computer and use it in GitHub Desktop.
Save TheStros/f8900764b01dba7124b912186b1447de to your computer and use it in GitHub Desktop.
Hasura, Algolia, and React
addMessage example:
const algoliasearch = require('algoliasearch');
const client = algoliasearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_API_KEY
);
const index = client.initIndex(process.env.ALGOLIA_INDEX);
exports.handler = async (event, context, body) => {
let request;
try {
request = JSON.parse(event.body);
} catch (e) {
return { statusCode: 400, body: 'cannot parse hasura event' };
}
try {
const record = await index.addObject(request.event.data.new);
return { statusCode: 200, body: JSON.stringify(record) };
} catch (err) {
return { statusCode: 500, body: err.toString() };
}
};
deleteMessage:
const algoliasearch = require('algoliasearch');
const client = algoliasearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_API_KEY
);
const index = client.initIndex(process.env.ALGOLIA_INDEX);
exports.handler = async (event, context, body) => {
let request;
try {
request = JSON.parse(event.body);
} catch (e) {
return { statusCode: 400, body: 'cannot parse hasura event' };
}
try {
const record = await index.deleteObject(request.event.data.old.objectID);
console.log(request.event.data.old)
return { statusCode: 200, body: JSON.stringify(record) };
} catch (err) {
return { statusCode: 500, body: err.toString() };
}
};
updateMessage:
const algoliasearch = require('algoliasearch');
const client = algoliasearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_API_KEY
);
const index = client.initIndex(process.env.ALGOLIA_INDEX);
exports.handler = async (event, context, body) => {
let request;
try {
request = JSON.parse(event.body);
} catch (e) {
return { statusCode: 400, body: 'cannot parse hasura event' };
}
try {
const record = await index.partialUpdateObject(
request.event.data.old,
(err, content) => {
if (err) throw err;
console.log(content);
}
);
return { statusCode: 200, body: JSON.stringify(record) };
} catch (err) {
return { statusCode: 500, body: err.toString() };
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment