added function
This commit is contained in:
parent
bde4779b1b
commit
5486dc0784
4 changed files with 46 additions and 0 deletions
36
functions/todos-create.js
Normal file
36
functions/todos-create.js
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* code from functions/todos-create.js */
|
||||
/* Import faunaDB sdk */
|
||||
const faunadb = require('faunadb')
|
||||
|
||||
/* configure faunaDB Client with our secret */
|
||||
const q = faunadb.query
|
||||
const client = new faunadb.Client({
|
||||
secret: process.env.FAUNADB_SERVER_SECRET
|
||||
})
|
||||
|
||||
/* export our lambda function as named "handler" export */
|
||||
exports.handler = async (event, context) => {
|
||||
/* parse the string body into a useable JS object */
|
||||
const data = JSON.parse(event.body)
|
||||
console.log('Function `todo-create` invoked', data)
|
||||
const todoItem = {
|
||||
data: data
|
||||
}
|
||||
/* construct the fauna query */
|
||||
return client.query(q.Create(q.Ref('classes/todos'), todoItem))
|
||||
.then((response) => {
|
||||
console.log('success', response)
|
||||
/* Success! return the response with statusCode 200 */
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: JSON.stringify(response)
|
||||
}
|
||||
}).catch((error) => {
|
||||
console.log('error', error)
|
||||
/* Error! return the error with statusCode 400 */
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: JSON.stringify(error)
|
||||
}
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue