-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbcli.js
83 lines (75 loc) · 1.68 KB
/
dbcli.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
const yargs = require('yargs');
const dotenv = require('dotenv')
dotenv.config()
const db = require('./database/connect');
function sync(parsed) {
(async function () {
await db.sync({force: parsed.force}).then(function () {
console.log('Sync complete');
});
})();
}
function create(parsed) {
(async function () {
switch (parsed.thing) {
case 'apikey':
await _create_apikey(parsed);
}
})();
}
async function _create_apikey(parsed) {
if(!parsed.name) {
console.log("Required args: --name <name>");
return;
}
const { randomUUID } = require('crypto');
const key = "cli:" + randomUUID();
await db.models.APIkey.create({name: parsed.name, apikey: key})
console.log("Created API key:\n" + key);
}
yargs
.scriptName('npm run db')
.command({
command: 'create [thing]',
aliases: ['c'],
describe: 'Create a [thing]',
handler: parsed => create(parsed),
builder: {
thing: {
demand: true,
choices: ['apikey']
}
},
})
.command({
command: 'sync',
describe: 'Pulls up-to-date the database',
handler: parsed => sync(parsed),
builder: {
force: {
description: 'Force it',
alias: 'f',
type: 'boolean'
}
},
})
.parse(process.argv.slice(2));
// const argv = yargs
// .command('sync', 'Pulls up-to-date the database.', {
// force: {
// description: 'Force it',
// alias: 'f',
// type: 'boolean'
// }
// })
// .command('create api-key', 'Create an API key in the database.', {
// name: {
// description: 'Key name',
// alias: 'n',
// type: 'string'
// }
// })
// .option('time', {})
// .help()
// .alias('help', 'h').argv;
// console.log(argv);