forked from fauna/fauna-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list-databases.js
50 lines (44 loc) · 1.2 KB
/
list-databases.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
const FaunaCommand = require('../lib/fauna-command.js')
const { errorOut } = require('../lib/misc.js')
const faunadb = require('faunadb')
const q = faunadb.query
/**
* Despite its name, returns the first 1000 databases defined.
* "1000 databases ought to be enough for anybody".
*/
function allDatabasesQuery(q) {
return q.Map(
q.Paginate(q.Databases(null), { size: 1000 }),
q.Lambda('x', q.Get(q.Var('x')))
)
}
class ListDatabasesCommand extends FaunaCommand {
async run() {
const log = this.log
return this.withClient(function (client, _) {
log('listing databases')
return client
.query(allDatabasesQuery(q))
.then(function (res) {
if (res.data.length > 0) {
res.data.forEach(function (el) {
log(el.ref.id)
})
} else {
log('No databases created')
}
})
.catch(function (err) {
errorOut(err.message)
})
})
}
}
ListDatabasesCommand.description = `
Lists child databases in the current database
`
ListDatabasesCommand.examples = ['$ fauna list-databases']
ListDatabasesCommand.flags = {
...FaunaCommand.flags,
}
module.exports = ListDatabasesCommand