add clear all

This commit is contained in:
DavidWells 2018-06-20 17:21:17 -07:00
parent 96854572a0
commit 28d647861a
10 changed files with 253 additions and 33 deletions

View file

@ -1,26 +1,47 @@
/* bootstrap database in your FaunaDB account */
const readline = require('readline');
const faunadb = require('faunadb');
const q = faunadb.query;
const readline = require('readline')
const faunadb = require('faunadb')
const chalk = require('chalk')
const insideNetlify = insideNetlifyBuildContext()
const q = faunadb.query
ask('Enter your faunaDB server key', function(err, answer) {
const key = answer || process.env.FAUNADB_SECRET
if (!key) {
console.log('Please set supply a faunaDB server key')
process.exit()
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 (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')
process.exit(1)
}
// Local machine warning
if (!insideNetlify) {
console.log()
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 (!answer) {
console.log('Please supply a faunaDB server key')
process.exit(1)
}
createFaunaDB(process.env.FAUNADB_SECRET).then(() => {
console.log('Database created')
})
});
}
}
const client = new faunadb.Client({
secret: answer
});
createFaunaDB(key).then(() => {
// Has var. Do the thing
if (process.env.FAUNADB_SECRET) {
createFaunaDB(process.env.FAUNADB_SECRET).then(() => {
console.log('Database created')
})
});
}
/* idempotent operation */
function createFaunaDB(key) {
console.log('Create the database!')
const client = new faunadb.Client({
secret: key
});
@ -33,9 +54,20 @@ function createFaunaDB(key) {
name: "all_todos",
source: q.Ref("classes/todos")
}))
}).catch((e) => {
// Database already exists
if (e.requestResult.statusCode === 400 && e.message === 'instance not unique') {
console.log('DB already exists')
throw e
}
})
.then(console.log.bind(console))
.catch(console.error.bind(console))
}
function insideNetlifyBuildContext() {
if (process.env.DEPLOY_PRIME_URL) {
return true
}
return false
}
// Readline util