|
| 1 | +// This script sets up the database to be used for this example application. |
| 2 | +// Look at the code to see what is behind the magic |
| 3 | +const faunadb = require('faunadb') |
| 4 | +const q = faunadb.query |
| 5 | +const request = require('request') |
| 6 | +const fs = require('fs') |
| 7 | +const streamToPromise = require('stream-to-promise') |
| 8 | + |
| 9 | +const readline = require('readline').createInterface({ |
| 10 | + input: process.stdin, |
| 11 | + output: process.stdout, |
| 12 | +}) |
| 13 | + |
| 14 | +// In order to set up a database, we need a server key, so let's ask the user for a key. |
| 15 | +readline.question(`Please provide the FaunaDB server key\n`, serverKey => { |
| 16 | + // A graphql schema can be imported in override or merge mode: 'https://docs.fauna.com/fauna/current/api/graphql/endpoints#import' |
| 17 | + const options = { |
| 18 | + model: 'merge', |
| 19 | + uri: 'https://graphql.fauna.com/import', |
| 20 | + headers: { Authorization: `Bearer ${serverKey}` }, |
| 21 | + } |
| 22 | + const stream = fs.createReadStream('./schema.gql').pipe(request.post(options)) |
| 23 | + |
| 24 | + streamToPromise(stream) |
| 25 | + .then(res => { |
| 26 | + const readableResult = res.toString() |
| 27 | + if (readableResult.startsWith('Invalid authorization header')) { |
| 28 | + console.error('You need to provide a secret, closing. Try again') |
| 29 | + return readline.close() |
| 30 | + } else if (readableResult.startsWith('Invalid database secret')) { |
| 31 | + console.error( |
| 32 | + 'The secret you have provided is not valid, closing. Try again' |
| 33 | + ) |
| 34 | + return readline.close() |
| 35 | + } else if (readableResult.includes('success')) { |
| 36 | + console.log('1. Successfully imported schema') |
| 37 | + return readline.close() |
| 38 | + } |
| 39 | + }) |
| 40 | + .catch(err => { |
| 41 | + console.error(err) |
| 42 | + console.error(`Could not import schema, closing`) |
| 43 | + }) |
| 44 | + .then(res => { |
| 45 | + // The GraphQL schema is important, this means that we now have a GuestbookEntry Colleciton and an entries index. |
| 46 | + // Then we create a token that can only read and write to that index and collection |
| 47 | + var client = new faunadb.Client({ secret: serverKey }) |
| 48 | + return client |
| 49 | + .query( |
| 50 | + q.CreateRole({ |
| 51 | + name: 'GuestbookRole', |
| 52 | + privileges: [ |
| 53 | + { |
| 54 | + resource: q.Collection('GuestbookEntry'), |
| 55 | + actions: { read: true, write: true }, |
| 56 | + }, |
| 57 | + { |
| 58 | + resource: q.Index('entries'), |
| 59 | + actions: { read: true }, |
| 60 | + }, |
| 61 | + ], |
| 62 | + }) |
| 63 | + ) |
| 64 | + .then(res => { |
| 65 | + console.log( |
| 66 | + '2. Successfully created role to read and write guestbook entries' |
| 67 | + ) |
| 68 | + }) |
| 69 | + .catch(err => { |
| 70 | + if (err.toString().includes('instance already exists')) { |
| 71 | + console.log('2. Role already exists.') |
| 72 | + } else { |
| 73 | + throw err |
| 74 | + } |
| 75 | + }) |
| 76 | + }) |
| 77 | + .catch(err => { |
| 78 | + console.error(err) |
| 79 | + console.error(`Failed to create role, closing`) |
| 80 | + }) |
| 81 | + .then(res => { |
| 82 | + // The GraphQL schema is important, this means that we now have a GuestbookEntry Colleciton and an entries index. |
| 83 | + // Then we create a token that can only read and write to that index and collection |
| 84 | + var client = new faunadb.Client({ secret: serverKey }) |
| 85 | + return client |
| 86 | + .query( |
| 87 | + q.CreateKey({ |
| 88 | + role: q.Role('GuestbookRole'), |
| 89 | + }) |
| 90 | + ) |
| 91 | + .then(res => { |
| 92 | + console.log('3. Created key to use in client') |
| 93 | + console.log( |
| 94 | + 'Replace the < GRAPHQL_SECRET > placehold in next.config.js with:' |
| 95 | + ) |
| 96 | + console.log(res.secret) |
| 97 | + }) |
| 98 | + }) |
| 99 | + .catch(err => { |
| 100 | + console.error(err) |
| 101 | + console.error(`Failed to create key, closing`) |
| 102 | + }) |
| 103 | +}) |
0 commit comments