forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
100 lines (88 loc) · 2.56 KB
/
index.js
File metadata and controls
100 lines (88 loc) · 2.56 KB
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
const { Database, aql } = require('arangojs')
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const { addOrganizationsDomains, alignOrganizationsDomains } = require('./src')
const path = require('path')
require('dotenv-safe').config({
path: path.join(__dirname, '/.env'),
example: path.join(__dirname, '/.env.example'),
})
const argv = yargs(hideBin(process.argv))
.usage('Usage: $0 <command> [options]')
.command(
'add',
'Add organizations and domains to the database from a JSON file',
)
.command(
'align',
'Add and remove organizations and domains to align with a JSON file',
)
.example(
'$0 add',
'add new organizations and domains to the database from a JSON file',
)
.demandCommand(
1,
1,
'You must include the command you wish to run.',
'You must include the command you wish to run.',
)
.help('h')
.alias('h', 'help')
.version(false).argv
if (!['add', 'align'].includes(argv._[0])) {
console.error('Incorrect command entered, use "node index.js -h" for help.')
process.exit(-1)
}
const { FILE, DB_USERNAME, DB_PASS, DB_URL, DB_NAME } = process.env
;(async () => {
let data
try {
data = require(FILE)
} catch (err) {
console.error(err)
return
}
const rootDb = new Database({
url: DB_URL,
auth: { username: DB_USERNAME, password: DB_PASS },
})
try {
await rootDb.exists()
} catch (err) {
console.error('Error while connecting to database\n', err)
return
}
console.log('Successfully created database connection')
// check if database exists
const databaseExists = (await rootDb.listDatabases()).includes(DB_NAME)
// create database if it does not exist
if (!databaseExists) {
console.log(`Database does not exist, creating database "${DB_NAME}"`)
await rootDb.createDatabase(DB_NAME)
} else {
console.log(`Database ${DB_NAME} already exists`)
}
const db = rootDb.database(DB_NAME)
db.useBasicAuth(DB_USERNAME, DB_PASS)
console.log(`Using database "${DB_NAME}"`)
const query = async function query(strings, ...vars) {
return db.query(aql(strings, ...vars), {
count: true,
})
}
switch (argv._[0]) {
case 'add':
console.log(
`Adding domains and organizations from "${FILE}" to "${DB_NAME}"`,
)
await addOrganizationsDomains({ db: db, query, data })
break
case 'align':
console.log(
`Aligning domains and organizations in "${DB_NAME}" with ${FILE}`,
)
await alignOrganizationsDomains({ db: db, query, data })
break
}
})()