add path id util

This commit is contained in:
davidwells 2018-06-11 21:26:54 -07:00
parent 4c794b23f3
commit 5a0a7e495c
5 changed files with 16 additions and 7 deletions

View file

@ -1,4 +1,5 @@
import faunadb from 'faunadb'
import getId from './utils/getId'
const q = faunadb.query
const client = new faunadb.Client({
@ -6,7 +7,7 @@ const client = new faunadb.Client({
})
exports.handler = (event, context, callback) => {
const id = event.path.replace(/\/\.netlify\/functions\/todos-delete\//, "")
const id = getId(event.path)
console.log(`Function 'todo-delete' invoked. delete id: ${id}`)
return client.query(q.Delete(q.Ref(`classes/todos/${id}`)))
.then((response) => {

View file

@ -9,13 +9,15 @@ exports.handler = (event, context, callback) => {
console.log("Function `todo-read-all` invoked")
return client.query(q.Paginate(q.Match(q.Ref("indexes/all_todos"))))
.then((response) => {
console.log("success", response)
const newQuery = response.data.map((ref) => {
console.log('ref', ref)
const todoRefs = response.data
console.log("Todo refs", todoRefs)
console.log(`${todoRefs.length} todos found`)
// create new query out of todo refs. http://bit.ly/2LG3MLg
const getAllTodoDataQuery = todoRefs.map((ref) => {
return q.Get(ref)
})
// then query the refs
return client.query(newQuery).then((ret) => {
return client.query(getAllTodoDataQuery).then((ret) => {
return callback(null, {
statusCode: 200,
body: JSON.stringify(ret)

View file

@ -1,4 +1,5 @@
import faunadb from 'faunadb'
import getId from './utils/getId'
const q = faunadb.query
const client = new faunadb.Client({
@ -6,7 +7,7 @@ const client = new faunadb.Client({
})
exports.handler = (event, context, callback) => {
const id = event.path.replace(/\/\.netlify\/functions\/todos-read\//, "")
const id = getId(event.path)
console.log(`Function 'todo-read' invoked. Read id: ${id}`)
return client.query(q.Get(q.Ref(`classes/todos/${id}`)))
.then((response) => {

View file

@ -1,4 +1,5 @@
import faunadb from 'faunadb'
import getId from './utils/getId'
const q = faunadb.query
const client = new faunadb.Client({
@ -7,7 +8,7 @@ const client = new faunadb.Client({
exports.handler = (event, context, callback) => {
const data = JSON.parse(event.body)
const id = event.path.replace(/\/\.netlify\/functions\/todos-update\//, "")
const id = getId(event.path)
console.log(`Function 'todo-update' invoked. update id: ${id}`)
return client.query(q.Update(q.Ref(`classes/todos/${id}`), {data}))
.then((response) => {

4
functions/utils/getId.js Normal file
View file

@ -0,0 +1,4 @@
export default function getId(urlPath) {
return urlPath.match(/([^\/]*)\/*$/)[0]
}