Last active
April 23, 2020 19:46
-
-
Save TheStros/f8900764b01dba7124b912186b1447de to your computer and use it in GitHub Desktop.
Hasura, Algolia, and React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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