Compare commits

...

20 commits

Author SHA1 Message Date
DavidWells
271721bbc0 update env key to FAUNADB_SERVER_SECRET 2019-04-07 15:49:58 -07:00
David Wells
9b089e7c04
Merge pull request #6 from RyanNielson/patch-1
Fixed the link to the FaunaDB dashboard in the README
2019-02-07 11:35:21 -08:00
Ryan Nielson
abcb29741e
Fixed the link to the FaunaDB dashboard 2019-02-07 15:32:42 -04:00
David Wells
b8197dad6c
Update todos-update.js 2019-01-31 14:03:12 -08:00
David Wells
8ee6d32f05
Update todos-read.js 2019-01-31 14:03:05 -08:00
David Wells
20188a7982
Update todos-read-all.js 2019-01-31 14:02:58 -08:00
David Wells
42c6298127
Update todos-delete.js 2019-01-31 14:02:50 -08:00
David Wells
669355c7f4
Update todos-delete-batch.js 2019-01-31 14:02:42 -08:00
David Wells
36e12d4a79
Update todos-create.js 2019-01-31 14:02:16 -08:00
DavidWells
65522c08ec Merge branch 'master' of github.com:netlify/netlify-faunadb-example
# Conflicts:
#	package.json
#	src/App.js
#	src/setupProxy.js
2019-01-07 12:56:01 -08:00
shawn wang
62777cbd47
Merge pull request #1 from lambrospetrou/master
Upgrade to Create-React-App 2
2018-10-17 15:42:20 -04:00
shawn wang
245653049b
Delete package-lock.json 2018-10-17 15:41:58 -04:00
Laura Jodz
4cbcafa032 add headers 2018-10-17 10:22:28 -07:00
Lambros Petrou
0684e2df74 Upgrade netlify-lambda to 1.0.2 2018-10-12 20:13:30 +01:00
Lambros Petrou
6268e75b16 Merge branch 'feature-upgrade-to-CRA-2' 2018-10-06 13:06:47 +01:00
Lambros Petrou
fe4b9e43e9 Fix netlify-lambda build process due to webpack3 requirement 2018-10-06 13:05:22 +01:00
Lambros Petrou
397f8281a0 Upgrade to Create-React-App 2 2018-10-06 13:03:55 +01:00
Lambros Petrou
895c86b61a Fix faunadb @ref id access 2018-10-06 12:22:56 +01:00
Lambros Petrou
0f099d7cad Upgrade vulnerabilities 2018-10-06 12:18:46 +01:00
David Wells
a1fbcced32
Update todos-create.js 2018-08-30 10:20:30 -07:00
13 changed files with 18422 additions and 137 deletions

View file

@ -51,7 +51,7 @@ This application is using [React](https://reactjs.org/) for the frontend, [Netli
In your terminal run the following command:
```bash
export FAUNADB_SECRET=YourFaunaDBKeyHere
export FAUNADB_SERVER_SECRET=YourFaunaDBKeyHere
```
5. Run project locally
@ -140,9 +140,9 @@ Head over to [https://app.fauna.com/sign-up](https://app.fauna.com/sign-up) to c
```bash
# on mac
export FAUNADB_SECRET=YourFaunaDBKeyHere
export FAUNADB_SERVER_SECRET=YourFaunaDBKeyHere
# on windows
set FAUNADB_SECRET=YourFaunaDBKeyHere
set FAUNADB_SERVER_SECRET=YourFaunaDBKeyHere
```
Add the [/scripts/bootstrap-fauna-database.js](https://github.com/netlify/netlify-faunadb-example/blob/f965df497f0de507c2dfdb1a8a32a81bbd939314/scripts/bootstrap-fauna-database.js) to the root directory of the project. This is an idempotent script that you can run 1 million times and have the same result (one todos database)
@ -163,7 +163,7 @@ Head over to [https://app.fauna.com/sign-up](https://app.fauna.com/sign-up) to c
npm run bootstrap
```
If you login to the [FaunaDB dashboard](https://dashboard.fauna.com] you will see your todo database.
If you login to the [FaunaDB dashboard](https://dashboard.fauna.com) you will see your todo database.
### 3. Create a function
@ -324,7 +324,7 @@ Lets rock and roll.
/* configure faunaDB Client with our secret */
const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET
secret: process.env.FAUNADB_SERVER_SECRET
})
/* export our lambda function as named "handler" export */
@ -410,7 +410,7 @@ So far we have created our `todo-create` function done and we've seen how we mak
const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET
secret: process.env.FAUNADB_SERVER_SECRET
})
exports.handler = (event, context, callback) => {
@ -446,7 +446,7 @@ So far we have created our `todo-create` function done and we've seen how we mak
const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET
secret: process.env.FAUNADB_SERVER_SECRET
})
exports.handler = (event, context, callback) => {
@ -491,7 +491,7 @@ So far we have created our `todo-create` function done and we've seen how we mak
const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET
secret: process.env.FAUNADB_SERVER_SECRET
})
exports.handler = (event, context, callback) => {
@ -530,7 +530,7 @@ So far we have created our `todo-create` function done and we've seen how we mak
const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET
secret: process.env.FAUNADB_SERVER_SECRET
})
exports.handler = (event, context, callback) => {
@ -569,7 +569,7 @@ So far we have created our `todo-create` function done and we've seen how we mak
const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET
secret: process.env.FAUNADB_SERVER_SECRET
})
exports.handler = (event, context, callback) => {

View file

@ -3,32 +3,32 @@ import faunadb from 'faunadb' /* Import faunaDB sdk */
/* configure faunaDB Client with our secret */
const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET
secret: process.env.FAUNADB_SERVER_SECRET
})
/* export our lambda function as named "handler" export */
exports.handler = (event, context, callback) => {
/* parse the string body into a useable JS object */
const data = JSON.parse(event.body)
console.log("Function `todo-create` invoked", data)
console.log('Function `todo-create` invoked', data)
const todoItem = {
data: data
}
/* construct the fauna query */
return client.query(q.Create(q.Ref("classes/todos"), todoItem))
.then((response) => {
console.log("success", response)
/* Success! return the response with statusCode 200 */
return callback(null, {
statusCode: 200,
body: JSON.stringify(response)
return client.query(q.Create(q.Ref('classes/todos'), todoItem))
.then((response) => {
console.log('success', response)
/* Success! return the response with statusCode 200 */
return callback(null, {
statusCode: 200,
body: JSON.stringify(response)
})
}).catch((error) => {
console.log('error', error)
/* Error! return the error with statusCode 400 */
return callback(null, {
statusCode: 400,
body: JSON.stringify(error)
})
})
}).catch((error) => {
console.log("error", error)
/* Error! return the error with statusCode 400 */
return callback(null, {
statusCode: 400,
body: JSON.stringify(error)
})
})
}

View file

@ -1,32 +1,31 @@
import faunadb from 'faunadb'
import getId from './utils/getId'
const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET
secret: process.env.FAUNADB_SERVER_SECRET
})
exports.handler = (event, context, callback) => {
const data = JSON.parse(event.body)
console.log('data', data)
console.log("Function `todo-delete-batch` invoked", data.ids)
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 callback(null, {
statusCode: 200,
body: JSON.stringify(response)
.then((response) => {
console.log('success', response)
return callback(null, {
statusCode: 200,
body: JSON.stringify(response)
})
}).catch((error) => {
console.log('error', error)
return callback(null, {
statusCode: 400,
body: JSON.stringify(error)
})
})
}).catch((error) => {
console.log("error", error)
return callback(null, {
statusCode: 400,
body: JSON.stringify(error)
})
})
}

View file

@ -3,24 +3,24 @@ import getId from './utils/getId'
const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET
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}`)
return client.query(q.Delete(q.Ref(`classes/todos/${id}`)))
.then((response) => {
console.log("success", response)
return callback(null, {
statusCode: 200,
body: JSON.stringify(response)
.then((response) => {
console.log('success', response)
return callback(null, {
statusCode: 200,
body: JSON.stringify(response)
})
}).catch((error) => {
console.log('error', error)
return callback(null, {
statusCode: 400,
body: JSON.stringify(error)
})
})
}).catch((error) => {
console.log("error", error)
return callback(null, {
statusCode: 400,
body: JSON.stringify(error)
})
})
}

View file

@ -2,32 +2,32 @@ import faunadb from 'faunadb'
const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET
secret: process.env.FAUNADB_SERVER_SECRET
})
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) => {
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(getAllTodoDataQuery).then((ret) => {
console.log('Function `todo-read-all` invoked')
return client.query(q.Paginate(q.Match(q.Ref('indexes/all_todos'))))
.then((response) => {
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(getAllTodoDataQuery).then((ret) => {
return callback(null, {
statusCode: 200,
body: JSON.stringify(ret)
})
})
}).catch((error) => {
console.log('error', error)
return callback(null, {
statusCode: 200,
body: JSON.stringify(ret)
statusCode: 400,
body: JSON.stringify(error)
})
})
}).catch((error) => {
console.log("error", error)
return callback(null, {
statusCode: 400,
body: JSON.stringify(error)
})
})
}

View file

@ -3,24 +3,24 @@ import getId from './utils/getId'
const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET
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}`)
return client.query(q.Get(q.Ref(`classes/todos/${id}`)))
.then((response) => {
console.log("success", response)
return callback(null, {
statusCode: 200,
body: JSON.stringify(response)
.then((response) => {
console.log('success', response)
return callback(null, {
statusCode: 200,
body: JSON.stringify(response)
})
}).catch((error) => {
console.log('error', error)
return callback(null, {
statusCode: 400,
body: JSON.stringify(error)
})
})
}).catch((error) => {
console.log("error", error)
return callback(null, {
statusCode: 400,
body: JSON.stringify(error)
})
})
}

View file

@ -3,7 +3,7 @@ import getId from './utils/getId'
const q = faunadb.query
const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET
secret: process.env.FAUNADB_SERVER_SECRET
})
exports.handler = (event, context, callback) => {
@ -11,17 +11,17 @@ exports.handler = (event, context, callback) => {
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) => {
console.log("success", response)
return callback(null, {
statusCode: 200,
body: JSON.stringify(response)
.then((response) => {
console.log('success', response)
return callback(null, {
statusCode: 200,
body: JSON.stringify(response)
})
}).catch((error) => {
console.log('error', error)
return callback(null, {
statusCode: 400,
body: JSON.stringify(error)
})
})
}).catch((error) => {
console.log("error", error)
return callback(null, {
statusCode: 400,
body: JSON.stringify(error)
})
})
}

View file

@ -6,4 +6,4 @@
publish = "build"
[template.environment]
FAUNADB_SECRET = "Your FaunaDB Server Secret"
FAUNADB_SERVER_SECRET = "Your FaunaDB Server Secret"

18292
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -8,11 +8,11 @@ const q = faunadb.query
console.log(chalk.cyan('Creating your FaunaDB Database...\n'))
// 1. Check for required enviroment variables
if (!process.env.FAUNADB_SECRET) {
console.log(chalk.yellow('Required FAUNADB_SECRET enviroment variable not found.'))
if (!process.env.FAUNADB_SERVER_SECRET) {
console.log(chalk.yellow('Required FAUNADB_SERVER_SECRET enviroment variable not found.'))
if (insideNetlify) {
console.log(`Visit https://app.netlify.com/sites/YOUR_SITE_HERE/settings/deploys`)
console.log('and set a `FAUNADB_SECRET` value in the "Build environment variables" section')
console.log('and set a `FAUNADB_SERVER_SECRET` value in the "Build environment variables" section')
process.exit(1)
}
// Local machine warning
@ -21,20 +21,23 @@ if (!process.env.FAUNADB_SECRET) {
console.log('You can create fauna DB keys here: https://dashboard.fauna.com/db/keys')
console.log()
ask(chalk.bold('Enter your faunaDB server key'), (err, answer) => {
if (err) {
console.log('err', err)
}
if (!answer) {
console.log('Please supply a faunaDB server key')
process.exit(1)
}
createFaunaDB(process.env.FAUNADB_SECRET).then(() => {
createFaunaDB(process.env.FAUNADB_SERVER_SECRET).then(() => {
console.log('Database created')
})
});
})
}
}
// Has var. Do the thing
if (process.env.FAUNADB_SECRET) {
createFaunaDB(process.env.FAUNADB_SECRET).then(() => {
if (process.env.FAUNADB_SERVER_SECRET) {
createFaunaDB(process.env.FAUNADB_SERVER_SECRET).then(() => {
console.log('Database created')
})
}
@ -44,15 +47,15 @@ function createFaunaDB(key) {
console.log('Create the database!')
const client = new faunadb.Client({
secret: key
});
})
/* Based on your requirements, change the schema here */
return client.query(q.Create(q.Ref("classes"), { name: "todos" }))
.then(()=>{
return client.query(q.Create(q.Ref('classes'), { name: 'todos' }))
.then(() => {
return client.query(
q.Create(q.Ref("indexes"), {
name: "all_todos",
source: q.Ref("classes/todos")
q.Create(q.Ref('indexes'), {
name: 'all_todos',
source: q.Ref('classes/todos')
}))
}).catch((e) => {
// Database already exists
@ -63,7 +66,6 @@ function createFaunaDB(key) {
})
}
/* util methods */
// Test if inside netlify build context
@ -79,9 +81,9 @@ function ask(question, callback) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
})
rl.question(question + '\n', function(answer) {
rl.close();
callback(null, answer);
});
rl.close()
callback(null, answer)
})
}

View file

@ -1,15 +1,8 @@
const chalk = require('chalk')
var util = require('util');
var exec = require('child_process').exec;
function clear(){
exec('clear', function(error, stdout, stderr){
util.puts(stdout);
});
}
function checkForFaunaKey() {
if (!process.env.FAUNADB_SECRET) {
console.log(chalk.yellow('Required FAUNADB_SECRET enviroment variable not found.'))
if (!process.env.FAUNADB_SERVER_SECRET) {
console.log(chalk.yellow('Required FAUNADB_SERVER_SECRET enviroment variable not found.'))
console.log(`
=========================
@ -17,7 +10,7 @@ You can create fauna DB keys here: https://dashboard.fauna.com/db/keys
In your terminal run the following command:
export FAUNADB_SECRET=YourFaunaDBKeyHere
export FAUNADB_SERVER_SECRET=YourFaunaDBKeyHere
=========================
`)

View file

@ -20,7 +20,7 @@ export default class App extends Component {
if (isLocalHost()) {
alert('FaunaDB key is not unauthorized. Make sure you set it in terminal session where you ran `npm start`. Visit http://bit.ly/set-fauna-key for more info')
} else {
alert('FaunaDB key is not unauthorized. Verify the key `FAUNADB_SECRET` set in Netlify enviroment variables is correct')
alert('FaunaDB key is not unauthorized. Verify the key `FAUNADB_SERVER_SECRET` set in Netlify enviroment variables is correct')
}
return false
}
@ -206,7 +206,6 @@ export default class App extends Component {
console.log('An API error occurred', e)
})
})
}
closeModal = (e) => {
this.setState({

View file

@ -1,4 +1,4 @@
const proxy = require('http-proxy-middleware');
const proxy = require('http-proxy-middleware')
module.exports = function(app) {
app.use(
@ -8,5 +8,5 @@ module.exports = function(app) {
'^/\\.netlify/functions': ''
}
})
);
};
)
}