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

Commit bdc7a1f

Browse files
authored
Merge pull request #47 from netlify/fauna-crud
Fauna CRUD improvements
2 parents 9a822ee + f8a9927 commit bdc7a1f

File tree

7 files changed

+130
-56
lines changed

7 files changed

+130
-56
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env node
2+
3+
/* bootstrap database in your FaunaDB account - use with `netlify dev:exec <path-to-this-file>` */
4+
const faunadb = require('faunadb')
5+
6+
const q = faunadb.query
7+
8+
function createFaunaDB() {
9+
if (!process.env.FAUNADB_SERVER_SECRET) {
10+
console.log('No FAUNADB_SERVER_SECRET in environment, skipping DB setup')
11+
}
12+
console.log('Create the database!')
13+
const client = new faunadb.Client({
14+
secret: process.env.FAUNADB_SERVER_SECRET
15+
})
16+
17+
/* Based on your requirements, change the schema here */
18+
return client
19+
.query(q.Create(q.Ref('classes'), { name: 'items' }))
20+
.then(() => {
21+
console.log('Created items class')
22+
return client.query(
23+
q.Create(q.Ref('indexes'), {
24+
name: 'all_items',
25+
source: q.Ref('classes/items')
26+
})
27+
)
28+
})
29+
30+
.catch(e => {
31+
if (e.requestResult.statusCode === 400 && e.message === 'instance not unique') {
32+
console.log('DB already exists')
33+
}
34+
throw e
35+
})
36+
}
37+
38+
createFaunaDB()

src/functions-templates/js/fauna-crud/create.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,30 @@ const client = new faunadb.Client({
77
})
88

99
/* export our lambda function as named "handler" export */
10-
exports.handler = (event, context, callback) => {
10+
exports.handler = async (event, context) => {
1111
/* parse the string body into a useable JS object */
1212
const data = JSON.parse(event.body)
13-
console.log('Function `todo-create` invoked', data)
14-
const todoItem = {
13+
console.log('Function `create` invoked', data)
14+
const item = {
1515
data: data
1616
}
1717
/* construct the fauna query */
1818
return client
19-
.query(q.Create(q.Ref('classes/todos'), todoItem))
19+
.query(q.Create(q.Ref('classes/items'), item))
2020
.then(response => {
2121
console.log('success', response)
2222
/* Success! return the response with statusCode 200 */
23-
return callback(null, {
23+
return {
2424
statusCode: 200,
2525
body: JSON.stringify(response)
26-
})
26+
}
2727
})
2828
.catch(error => {
2929
console.log('error', error)
3030
/* Error! return the error with statusCode 400 */
31-
return callback(null, {
31+
return {
3232
statusCode: 400,
3333
body: JSON.stringify(error)
34-
})
34+
}
3535
})
3636
}
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
/* Import faunaDB sdk */
22
const faunadb = require('faunadb')
33

4-
function getId(urlPath) {
5-
return urlPath.match(/([^\/]*)\/*$/)[0]
6-
}
7-
84
const q = faunadb.query
95
const client = new faunadb.Client({
106
secret: process.env.FAUNADB_SERVER_SECRET
117
})
128

13-
exports.handler = (event, context, callback) => {
14-
const id = getId(event.path)
15-
console.log(`Function 'todo-delete' invoked. delete id: ${id}`)
9+
exports.handler = async (event, context) => {
10+
const id = event.id
11+
console.log(`Function 'delete' invoked. delete id: ${id}`)
1612
return client
17-
.query(q.Delete(q.Ref(`classes/todos/${id}`)))
13+
.query(q.Delete(q.Ref(`classes/items/${id}`)))
1814
.then(response => {
1915
console.log('success', response)
20-
return callback(null, {
16+
return {
2117
statusCode: 200,
2218
body: JSON.stringify(response)
23-
})
19+
}
2420
})
2521
.catch(error => {
2622
console.log('error', error)
27-
return callback(null, {
23+
return {
2824
statusCode: 400,
2925
body: JSON.stringify(error)
30-
})
26+
}
3127
})
3228
}
Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
exports.handler = async (event, context, callback) => {
2-
const { action } = event.queryStringParameters
3-
switch (action) {
4-
case 'create':
5-
return require('./create').handler(event, context, callback)
6-
case 'read':
7-
return require('./read').handler(event, context, callback)
8-
case 'update':
9-
return require('./update').handler(event, context, callback)
10-
case 'delete':
11-
return require('./delete').handler(event, context, callback)
1+
exports.handler = async (event, context) => {
2+
const path = event.path.replace(/\.netlify\/functions\/[^\/]+/, '')
3+
const segments = path.split('/').filter(e => e)
4+
5+
switch (event.httpMethod) {
6+
case 'GET':
7+
if (segments.length === 0) {
8+
return require('./read-all').handler(event, context)
9+
}
10+
if (segments.length === 1) {
11+
event.id = segments[0]
12+
return require('./read').handler(event, context)
13+
}
14+
case 'POST':
15+
return require('./create').handler(event, context)
16+
case 'PUT':
17+
if (segments.length === 1) {
18+
event.id = segments[0]
19+
return require('./update').handler(event, context)
20+
}
21+
case 'DELETE':
22+
if (segments.length === 1) {
23+
event.id = segments[0]
24+
return require('./delete').handler(event, context)
25+
}
1226
}
13-
return { statusCode: 500, body: 'unrecognized action ' + action }
27+
return { statusCode: 500, body: 'unrecognized HTTP Method, must be one of GET/POST/PUT/DELETE' }
1428
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* Import faunaDB sdk */
2+
const faunadb = require('faunadb')
3+
4+
const q = faunadb.query
5+
const client = new faunadb.Client({
6+
secret: process.env.FAUNADB_SERVER_SECRET
7+
})
8+
9+
exports.handler = async (event, context) => {
10+
console.log('Function `read-all` invoked')
11+
return client
12+
.query(q.Paginate(q.Match(q.Ref('indexes/all_items'))))
13+
.then(response => {
14+
const itemRefs = response.data
15+
// create new query out of item refs. http://bit.ly/2LG3MLg
16+
const getAllItemsDataQuery = itemRefs.map(ref => {
17+
return q.Get(ref)
18+
})
19+
// then query the refs
20+
return client.query(getAllItemsDataQuery).then(ret => {
21+
return {
22+
statusCode: 200,
23+
body: JSON.stringify(ret)
24+
}
25+
})
26+
})
27+
.catch(error => {
28+
console.log('error', error)
29+
return {
30+
statusCode: 400,
31+
body: JSON.stringify(error)
32+
}
33+
})
34+
}
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
/* Import faunaDB sdk */
22
const faunadb = require('faunadb')
33

4-
function getId(urlPath) {
5-
return urlPath.match(/([^\/]*)\/*$/)[0]
6-
}
7-
84
const q = faunadb.query
95
const client = new faunadb.Client({
106
secret: process.env.FAUNADB_SERVER_SECRET
117
})
128

13-
exports.handler = (event, context, callback) => {
14-
const id = getId(event.path)
15-
console.log(`Function 'todo-read' invoked. Read id: ${id}`)
9+
exports.handler = async (event, context) => {
10+
const id = event.id
11+
console.log(`Function 'read' invoked. Read id: ${id}`)
1612
return client
17-
.query(q.Get(q.Ref(`classes/todos/${id}`)))
13+
.query(q.Get(q.Ref(`classes/items/${id}`)))
1814
.then(response => {
1915
console.log('success', response)
20-
return callback(null, {
16+
return {
2117
statusCode: 200,
2218
body: JSON.stringify(response)
23-
})
19+
}
2420
})
2521
.catch(error => {
2622
console.log('error', error)
27-
return callback(null, {
23+
return {
2824
statusCode: 400,
2925
body: JSON.stringify(error)
30-
})
26+
}
3127
})
3228
}
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
/* Import faunaDB sdk */
22
const faunadb = require('faunadb')
33

4-
function getId(urlPath) {
5-
return urlPath.match(/([^\/]*)\/*$/)[0]
6-
}
7-
84
const q = faunadb.query
95
const client = new faunadb.Client({
106
secret: process.env.FAUNADB_SERVER_SECRET
117
})
128

13-
exports.handler = (event, context, callback) => {
9+
exports.handler = async (event, context) => {
1410
const data = JSON.parse(event.body)
15-
const id = getId(event.path)
16-
console.log(`Function 'todo-update' invoked. update id: ${id}`)
11+
const id = event.id
12+
console.log(`Function 'update' invoked. update id: ${id}`)
1713
return client
18-
.query(q.Update(q.Ref(`classes/todos/${id}`), { data }))
14+
.query(q.Update(q.Ref(`classes/items/${id}`), { data }))
1915
.then(response => {
2016
console.log('success', response)
21-
return callback(null, {
17+
return {
2218
statusCode: 200,
2319
body: JSON.stringify(response)
24-
})
20+
}
2521
})
2622
.catch(error => {
2723
console.log('error', error)
28-
return callback(null, {
24+
return {
2925
statusCode: 400,
3026
body: JSON.stringify(error)
31-
})
27+
}
3228
})
3329
}

0 commit comments

Comments
 (0)