Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Fauna CRUD improvements #47

Merged
merged 4 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/functions-templates/js/fauna-crud/create-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env node

/* bootstrap database in your FaunaDB account - use with `netlify dev:exec <path-to-this-file>` */
const faunadb = require('faunadb')

const q = faunadb.query

function createFaunaDB() {
if (!process.env.FAUNADB_SERVER_SECRET) {
console.log('No FAUNADB_SERVER_SECRET in environment, skipping DB setup')
}
console.log('Create the database!')
const client = new faunadb.Client({
secret: process.env.FAUNADB_SERVER_SECRET
})

/* Based on your requirements, change the schema here */
return client
.query(q.Create(q.Ref('classes'), { name: 'items' }))
.then(() => {
console.log('Created items class')
return client.query(
q.Create(q.Ref('indexes'), {
name: 'all_items',
source: q.Ref('classes/items')
})
)
})

.catch(e => {
if (e.requestResult.statusCode === 400 && e.message === 'instance not unique') {
console.log('DB already exists')
}
throw e
})
}

createFaunaDB()
16 changes: 8 additions & 8 deletions src/functions-templates/js/fauna-crud/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@ const client = new faunadb.Client({
})

/* export our lambda function as named "handler" export */
exports.handler = (event, context, callback) => {
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 = {
console.log('Function `create` invoked', data)
const item = {
data: data
}
/* construct the fauna query */
return client
.query(q.Create(q.Ref('classes/todos'), todoItem))
.query(q.Create(q.Ref('classes/items'), item))
.then(response => {
console.log('success', response)
/* Success! return the response with statusCode 200 */
return callback(null, {
return {
statusCode: 200,
body: JSON.stringify(response)
})
}
})
.catch(error => {
console.log('error', error)
/* Error! return the error with statusCode 400 */
return callback(null, {
return {
statusCode: 400,
body: JSON.stringify(error)
})
}
})
}
20 changes: 8 additions & 12 deletions src/functions-templates/js/fauna-crud/delete.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
/* Import faunaDB sdk */
const faunadb = require('faunadb')

function getId(urlPath) {
return urlPath.match(/([^\/]*)\/*$/)[0]
}

const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SERVER_SECRET
})

exports.handler = (event, context, callback) => {
const id = getId(event.path)
console.log(`Function 'todo-delete' invoked. delete id: ${id}`)
exports.handler = async (event, context) => {
const id = event.id
console.log(`Function 'delete' invoked. delete id: ${id}`)
return client
.query(q.Delete(q.Ref(`classes/todos/${id}`)))
.query(q.Delete(q.Ref(`classes/items/${id}`)))
.then(response => {
console.log('success', response)
return callback(null, {
return {
statusCode: 200,
body: JSON.stringify(response)
})
}
})
.catch(error => {
console.log('error', error)
return callback(null, {
return {
statusCode: 400,
body: JSON.stringify(error)
})
}
})
}
38 changes: 26 additions & 12 deletions src/functions-templates/js/fauna-crud/fauna-crud.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
exports.handler = async (event, context, callback) => {
const { action } = event.queryStringParameters
switch (action) {
case 'create':
return require('./create').handler(event, context, callback)
case 'read':
return require('./read').handler(event, context, callback)
case 'update':
return require('./update').handler(event, context, callback)
case 'delete':
return require('./delete').handler(event, context, callback)
exports.handler = async (event, context) => {
const path = event.path.replace(/\.netlify\/functions\/[^\/]+/, '')
const segments = path.split('/').filter(e => e)

switch (event.httpMethod) {
case 'GET':
if (segments.length === 0) {
return require('./read-all').handler(event, context)
}
if (segments.length === 1) {
event.id = segments[0]
return require('./read').handler(event, context)
}
case 'POST':
return require('./create').handler(event, context)
case 'PUT':
if (segments.length === 1) {
event.id = segments[0]
return require('./update').handler(event, context)
}
case 'DELETE':
if (segments.length === 1) {
event.id = segments[0]
return require('./delete').handler(event, context)
}
}
return { statusCode: 500, body: 'unrecognized action ' + action }
return { statusCode: 500, body: 'unrecognized HTTP Method, must be one of GET/POST/PUT/DELETE' }
}
34 changes: 34 additions & 0 deletions src/functions-templates/js/fauna-crud/read-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* 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) => {
console.log('Function `read-all` invoked')
return client
.query(q.Paginate(q.Match(q.Ref('indexes/all_items'))))
.then(response => {
const itemRefs = response.data
// create new query out of item refs. http://bit.ly/2LG3MLg
const getAllItemsDataQuery = itemRefs.map(ref => {
return q.Get(ref)
})
// then query the refs
return client.query(getAllItemsDataQuery).then(ret => {
return {
statusCode: 200,
body: JSON.stringify(ret)
}
})
})
.catch(error => {
console.log('error', error)
return {
statusCode: 400,
body: JSON.stringify(error)
}
})
}
20 changes: 8 additions & 12 deletions src/functions-templates/js/fauna-crud/read.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
/* Import faunaDB sdk */
const faunadb = require('faunadb')

function getId(urlPath) {
return urlPath.match(/([^\/]*)\/*$/)[0]
}

const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SERVER_SECRET
})

exports.handler = (event, context, callback) => {
const id = getId(event.path)
console.log(`Function 'todo-read' invoked. Read id: ${id}`)
exports.handler = async (event, context) => {
const id = event.id
console.log(`Function 'read' invoked. Read id: ${id}`)
return client
.query(q.Get(q.Ref(`classes/todos/${id}`)))
.query(q.Get(q.Ref(`classes/items/${id}`)))
.then(response => {
console.log('success', response)
return callback(null, {
return {
statusCode: 200,
body: JSON.stringify(response)
})
}
})
.catch(error => {
console.log('error', error)
return callback(null, {
return {
statusCode: 400,
body: JSON.stringify(error)
})
}
})
}
20 changes: 8 additions & 12 deletions src/functions-templates/js/fauna-crud/update.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
/* Import faunaDB sdk */
const faunadb = require('faunadb')

function getId(urlPath) {
return urlPath.match(/([^\/]*)\/*$/)[0]
}

const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SERVER_SECRET
})

exports.handler = (event, context, callback) => {
exports.handler = async (event, context) => {
const data = JSON.parse(event.body)
const id = getId(event.path)
console.log(`Function 'todo-update' invoked. update id: ${id}`)
const id = event.id
console.log(`Function 'update' invoked. update id: ${id}`)
return client
.query(q.Update(q.Ref(`classes/todos/${id}`), { data }))
.query(q.Update(q.Ref(`classes/items/${id}`), { data }))
.then(response => {
console.log('success', response)
return callback(null, {
return {
statusCode: 200,
body: JSON.stringify(response)
})
}
})
.catch(error => {
console.log('error', error)
return callback(null, {
return {
statusCode: 400,
body: JSON.stringify(error)
})
}
})
}