Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let users define their own database as easily as possible. (#10209)
* Let users define their own client token. * Make sure users know that GraphQL schema can also be done via the UI * Update README.md * Update setup.js Co-authored-by: Matthew Sweeney <mail@msweeneydev.com> Co-authored-by: Joe Haddad <timer150@gmail.com>
- Loading branch information
1 parent
f24aa41
commit 7d419f8
Showing
4 changed files
with
126 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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`) | ||
}) | ||
}) |