added functions
This commit is contained in:
parent
262502bff8
commit
2bae6e9269
6 changed files with 191 additions and 0 deletions
33
functions/todos-delete-batch.js
Normal file
33
functions/todos-delete-batch.js
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/* code from functions/todos-delete-batch.js */
|
||||
/* Import faunaDB sdk */
|
||||
const faunadb = require('faunadb')
|
||||
|
||||
const q = faunadb.query
|
||||
const client = new faunadb.Client({
|
||||
secret: process.env.FAUNADB_SERVER_SECRET
|
||||
})
|
||||
|
||||
exports.handler = async (event, context) => {
|
||||
const data = JSON.parse(event.body)
|
||||
console.log('data', data)
|
||||
console.log('Function `todo-delete-batch` invoked', data.ids)
|
||||
// construct batch query from IDs
|
||||
const deleteAllCompletedTodoQuery = data.ids.map((id) => {
|
||||
return q.Delete(q.Ref(`classes/todos/${id}`))
|
||||
})
|
||||
// Hit fauna with the query to delete the completed items
|
||||
return client.query(deleteAllCompletedTodoQuery)
|
||||
.then((response) => {
|
||||
console.log('success', response)
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: JSON.stringify(response)
|
||||
}
|
||||
}).catch((error) => {
|
||||
console.log('error', error)
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: JSON.stringify(error)
|
||||
}
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue