From 5a0a7e495c282dd5e67a7e1494916187ca696ce4 Mon Sep 17 00:00:00 2001 From: davidwells Date: Mon, 11 Jun 2018 21:26:54 -0700 Subject: [PATCH] add path id util --- functions/todos-delete.js | 3 ++- functions/todos-read-all.js | 10 ++++++---- functions/todos-read.js | 3 ++- functions/todos-update.js | 3 ++- functions/utils/getId.js | 4 ++++ 5 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 functions/utils/getId.js diff --git a/functions/todos-delete.js b/functions/todos-delete.js index 84d04e2..122f028 100644 --- a/functions/todos-delete.js +++ b/functions/todos-delete.js @@ -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) => { diff --git a/functions/todos-read-all.js b/functions/todos-read-all.js index 662995f..05e81a8 100644 --- a/functions/todos-read-all.js +++ b/functions/todos-read-all.js @@ -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) diff --git a/functions/todos-read.js b/functions/todos-read.js index c07666d..f5b388b 100644 --- a/functions/todos-read.js +++ b/functions/todos-read.js @@ -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) => { diff --git a/functions/todos-update.js b/functions/todos-update.js index 90f5ee0..76486fc 100644 --- a/functions/todos-update.js +++ b/functions/todos-update.js @@ -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) => { diff --git a/functions/utils/getId.js b/functions/utils/getId.js new file mode 100644 index 0000000..c61a209 --- /dev/null +++ b/functions/utils/getId.js @@ -0,0 +1,4 @@ + +export default function getId(urlPath) { + return urlPath.match(/([^\/]*)\/*$/)[0] +}