forked from fauna-labs/fwitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
destroy.js
122 lines (110 loc) · 3.7 KB
/
destroy.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
require('dotenv').config({ path: '.env.' + process.argv[2] })
const faunadb = require('faunadb')
const readline = require('readline-promise').default
const { handleSetupError } = require('../src/fauna/helpers/errors')
const {
Exists,
If,
Database,
Map,
Collections,
Documents,
Paginate,
Lambda,
Functions,
Roles,
Indexes,
Delete,
Var,
Tokens,
Let,
Get,
Select,
Equals,
Keys
} = faunadb.query
const keyQuestion = `----- Please provide the FaunaDB admin key) -----
An admin key is powerful, it should only be used for the setup script, not to run your application!
At the end of the script a key with limited privileges will be returned that should be used to run your application
Enter your key:`
const main = async () => {
let serverKey = process.env.REACT_APP_LOCAL___ADMIN
if (!serverKey) {
const interactiveSession = readline.createInterface({
input: process.stdin,
output: process.stdout
})
await interactiveSession.questionAsync(keyQuestion).then(key => {
serverKey = key
interactiveSession.close()
})
}
const client = new faunadb.Client({ secret: serverKey })
await deleteAll(client)
}
const deleteAll = async client => {
// If this option is provided, the db will be created as a child db of the database
// that the above admin key belongs to. This is useful to destroy/recreate a database
// easily without having to wait for cache invalidation of collection/index names.
// In this case, we can just nuke the database completely.
const childDbName = process.env.REACT_APP_LOCAL___CHILD_DB_NAME
if (typeof childDbName !== 'undefined') {
// clean keys that are linked to this database
await handleSetupError(
client.query(
Map(Paginate(Documents(Keys())), x =>
Let(
{
key: Get(x),
ref: Select(['ref'], Var('key')),
db: Select(['database'], Var('key'), 'none')
},
If(Equals(Var('db'), Database(childDbName)), Delete(Var('ref')), false)
)
)
),
'delete keys - delete keys linked to database'
)
await handleSetupError(
client.query(If(Exists(Database(childDbName)), Delete(Database(childDbName)), false)),
'database - delete child database'
)
} else {
try {
const collections = await deleteAllCollections(client)
const functions = await deleteAllFunctions(client)
const roles = await deleteAllRoles(client)
const indexes = await deleteIndexes(client)
const tokens = await deleteTokens(client)
// We only delete tokens, not keys in order to keep the admin key.
console.log(`Deleted:
1. collections: ${collections.data.length}
2. functions: ${functions.data.length}
3. roles: ${roles.data.length},
4. indexes: ${indexes.data.length},
5. tokens: ${tokens.data.length}`)
console.log(
'\x1b[32m',
`In case you want to recreate the database, please wait 60 seconds to invalidate collection/index name caches`
)
} catch (err) {
console.log('Error', err)
}
}
}
const deleteAllCollections = async client => {
return client.query(Map(Paginate(Collections()), Lambda('ref', Delete(Var('ref')))))
}
const deleteAllFunctions = async client => {
return client.query(Map(Paginate(Functions()), Lambda('ref', Delete(Var('ref')))))
}
const deleteAllRoles = async client => {
return client.query(Map(Paginate(Roles()), Lambda('ref', Delete(Var('ref')))))
}
const deleteIndexes = async client => {
return client.query(Map(Paginate(Indexes()), Lambda('ref', Delete(Var('ref')))))
}
const deleteTokens = async client => {
return client.query(Map(Paginate(Documents(Tokens())), Lambda('ref', Delete(Var('ref')))))
}
main()