/
setup.js
103 lines (100 loc) · 3.55 KB
/
setup.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// This script sets up the database to be used for this example application.
// Look at the code to see what is behind the magic
const faunadb = require('faunadb')
const q = faunadb.query
const request = require('request')
const fs = require('fs')
const streamToPromise = require('stream-to-promise')
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
})
// In order to set up a database, we need a server key, so let's ask the user for a key.
readline.question(`Please provide the FaunaDB server key\n`, serverKey => {
// A graphql schema can be imported in override or merge mode: 'https://docs.fauna.com/fauna/current/api/graphql/endpoints#import'
const options = {
model: 'merge',
uri: 'https://graphql.fauna.com/import',
headers: { Authorization: `Bearer ${serverKey}` },
}
const stream = fs.createReadStream('./schema.gql').pipe(request.post(options))
streamToPromise(stream)
.then(res => {
const readableResult = res.toString()
if (readableResult.startsWith('Invalid authorization header')) {
console.error('You need to provide a secret, closing. Try again')
return readline.close()
} else if (readableResult.startsWith('Invalid database secret')) {
console.error(
'The secret you have provided is not valid, closing. Try again'
)
return readline.close()
} else if (readableResult.includes('success')) {
console.log('1. Successfully imported schema')
return readline.close()
}
})
.catch(err => {
console.error(err)
console.error(`Could not import schema, closing`)
})
.then(res => {
// The GraphQL schema is important, this means that we now have a GuestbookEntry Colleciton and an entries index.
// Then we create a token that can only read and write to that index and collection
var client = new faunadb.Client({ secret: serverKey })
return client
.query(
q.CreateRole({
name: 'GuestbookRole',
privileges: [
{
resource: q.Collection('GuestbookEntry'),
actions: { read: true, write: true },
},
{
resource: q.Index('entries'),
actions: { read: true },
},
],
})
)
.then(res => {
console.log(
'2. Successfully created role to read and write guestbook entries'
)
})
.catch(err => {
if (err.toString().includes('instance already exists')) {
console.log('2. Role already exists.')
} else {
throw err
}
})
})
.catch(err => {
console.error(err)
console.error(`Failed to create role, closing`)
})
.then(res => {
// The GraphQL schema is important, this means that we now have a GuestbookEntry Colleciton and an entries index.
// Then we create a token that can only read and write to that index and collection
var client = new faunadb.Client({ secret: serverKey })
return client
.query(
q.CreateKey({
role: q.Role('GuestbookRole'),
})
)
.then(res => {
console.log('3. Created key to use in client')
console.log(
'Replace the < GRAPHQL_SECRET > placehold in next.config.js with:'
)
console.log(res.secret)
})
})
.catch(err => {
console.error(err)
console.error(`Failed to create key, closing`)
})
})