This repository was archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathbootstrap-fauna-database.js
60 lines (53 loc) · 1.84 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
/* bootstrap database in your FaunaDB account */
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_SERVER_SECRET) {
console.log(chalk.yellow('Required FAUNADB_SERVER_SECRET enviroment variable not found.'))
console.log(`Make sure you have created your Fauna database with "netlify addons:create fauna"`)
console.log(`Then run "npm run bootstrap" to setup your database schema`)
if (insideNetlify) {
process.exit(1)
}
}
// Has var. Do the thing
if (process.env.FAUNADB_SERVER_SECRET) {
createFaunaDB(process.env.FAUNADB_SERVER_SECRET).then(() => {
console.log('Fauna Database schema has been created')
console.log('Claim your fauna database with "netlify addons:auth fauna"')
})
}
/* idempotent operation */
function createFaunaDB(key) {
console.log('Create the fauna database schema!')
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('Fauna already setup! Good to go')
console.log('Claim your fauna database with "netlify addons:auth fauna"')
throw e
}
})
}
/* util methods */
// Test if inside netlify build context
function insideNetlifyBuildContext() {
if (process.env.DEPLOY_PRIME_URL) {
return true
}
return false
}