forked from sebastien-lb/japcalculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap-fauna-database.js
87 lines (78 loc) · 2.37 KB
/
bootstrap-fauna-database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* bootstrap database in your FaunaDB account */
const readline = require('readline')
const faunadb = require('faunadb')
const chalk = require('chalk')
const insideNetlify = insideNetlifyBuildContext()
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 (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')
})
});
}
}
// 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
});
/* 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("indexes"), {
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
}
})
}
/* util methods */
// Test if inside netlify build context
function insideNetlifyBuildContext() {
if (process.env.DEPLOY_PRIME_URL) {
return true
}
return false
}
// Readline util
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);
});
}