u
This commit is contained in:
@@ -27,6 +27,19 @@ function printResult(label, value) {
|
||||
console.log();
|
||||
}
|
||||
|
||||
async function askText(message, { placeholder, validate } = {}) {
|
||||
const value = await p.text({ message, placeholder, validate });
|
||||
if (p.isCancel(value)) return null;
|
||||
return value.trim();
|
||||
}
|
||||
|
||||
async function askInt(message, placeholder = '1') {
|
||||
return askText(message, {
|
||||
placeholder,
|
||||
validate: (v) => (!/^\d+$/.test(v?.trim() ?? '') ? 'Integer required' : undefined),
|
||||
});
|
||||
}
|
||||
|
||||
async function shutdown(pool, code = 0) {
|
||||
try {
|
||||
await pool.close();
|
||||
@@ -36,6 +49,146 @@ async function shutdown(pool, code = 0) {
|
||||
process.exit(code);
|
||||
}
|
||||
|
||||
const METHODS = [
|
||||
{ value: 'company.create', label: 'company.create', hint: '{name} → id' },
|
||||
{ value: 'company.list', label: 'company.list', hint: '→ rows' },
|
||||
{ value: 'company.setDefaultLang', label: 'company.setDefaultLang', hint: 'companyId, langId' },
|
||||
{ value: 'company.setRootCategory', label: 'company.setRootCategory', hint: 'companyId, categoryId' },
|
||||
{ value: 'company.delete', label: 'company.delete', hint: 'companyId' },
|
||||
{ value: 'company.getByCompanyUserId', label: 'company.getByCompanyUserId', hint: 'id → tuple' },
|
||||
{ value: 'lang.create', label: 'lang.create', hint: 'iso639 → id' },
|
||||
{ value: 'lang.list', label: 'lang.list', hint: '→ rows' },
|
||||
{ value: 'lang.delete', label: 'lang.delete', hint: 'id' },
|
||||
{ value: 'category.create', label: 'category.create', hint: '{parent_id, sort} → id' },
|
||||
{ value: 'category.getTree', label: 'category.getTree', hint: 'rootId → rows' },
|
||||
{ value: 'companyuser.add', label: 'companyuser.add', hint: 'companyId, authId → id' },
|
||||
{ value: 'companyuser.getByEmail', label: 'companyuser.getByEmail', hint: 'email → ids' },
|
||||
{ value: 'google_auth.add', label: 'google_auth.add', hint: 'email → authId' },
|
||||
{ value: 'google_auth.list', label: 'google_auth.list', hint: '→ rows' },
|
||||
{ value: 'google_auth.del', label: 'google_auth.del', hint: 'id' },
|
||||
{ value: 'initialize', label: 'initialize', hint: 'purge + recreate' },
|
||||
{ value: 'purge', label: chalk.red('purge'), hint: 'drop all · exit' },
|
||||
{ value: 'exit', label: 'exit', hint: 'quit without purge' },
|
||||
];
|
||||
|
||||
async function runMethod(client, action) {
|
||||
switch (action) {
|
||||
case 'company.create': {
|
||||
const name = await askText('Name', {
|
||||
placeholder: 'Acme',
|
||||
validate: (v) => (!v?.trim() ? 'Required' : undefined),
|
||||
});
|
||||
if (name == null) return;
|
||||
return ['id', await client.company.create({ name })];
|
||||
}
|
||||
case 'company.list': {
|
||||
return ['rows', await client.company.list()];
|
||||
}
|
||||
case 'company.setDefaultLang': {
|
||||
const companyId = await askInt('Company id');
|
||||
if (companyId == null) return;
|
||||
const languageId = await askInt('Language id');
|
||||
if (languageId == null) return;
|
||||
await client.company.setDefaultLang(Number(companyId), Number(languageId));
|
||||
return ['ok', true];
|
||||
}
|
||||
case 'company.setRootCategory': {
|
||||
const companyId = await askInt('Company id');
|
||||
if (companyId == null) return;
|
||||
const categoryId = await askInt('Category id');
|
||||
if (categoryId == null) return;
|
||||
await client.company.setRootCategory(Number(companyId), Number(categoryId));
|
||||
return ['ok', true];
|
||||
}
|
||||
case 'company.delete': {
|
||||
const companyId = await askInt('Company id');
|
||||
if (companyId == null) return;
|
||||
await client.company.delete(Number(companyId));
|
||||
return ['ok', true];
|
||||
}
|
||||
case 'company.getByCompanyUserId': {
|
||||
const id = await askInt('Company user id');
|
||||
if (id == null) return;
|
||||
return [
|
||||
'[id, defaultLangId, rootCategoryId, name]',
|
||||
await client.company.getByCompanyUserId(Number(id)),
|
||||
];
|
||||
}
|
||||
case 'lang.create': {
|
||||
const iso639 = await askText('ISO 639', {
|
||||
placeholder: 'de',
|
||||
validate: (v) => (!v?.trim() ? 'Required' : undefined),
|
||||
});
|
||||
if (iso639 == null) return;
|
||||
return ['id', await client.lang.create(iso639)];
|
||||
}
|
||||
case 'lang.list': {
|
||||
return ['rows', await client.lang.list()];
|
||||
}
|
||||
case 'lang.delete': {
|
||||
const id = await askInt('Language id');
|
||||
if (id == null) return;
|
||||
await client.lang.delete(Number(id));
|
||||
return ['ok', true];
|
||||
}
|
||||
case 'category.create': {
|
||||
const parentRaw = await askText('parent_id (null as empty)', {
|
||||
placeholder: '',
|
||||
validate: (v) =>
|
||||
v?.trim() && !/^\d+$/.test(v.trim()) ? 'Integer or empty' : undefined,
|
||||
});
|
||||
if (parentRaw == null) return;
|
||||
const sortRaw = await askInt('sort', '0');
|
||||
if (sortRaw == null) return;
|
||||
const parent_id = parentRaw === '' ? null : Number(parentRaw);
|
||||
return ['id', await client.category.create({ parent_id, sort: Number(sortRaw) })];
|
||||
}
|
||||
case 'category.getTree': {
|
||||
const rootId = await askInt('Root category id');
|
||||
if (rootId == null) return;
|
||||
return ['rows', await client.category.getTree(Number(rootId))];
|
||||
}
|
||||
case 'companyuser.add': {
|
||||
const companyId = await askInt('Company id');
|
||||
if (companyId == null) return;
|
||||
const authId = await askInt('Auth id');
|
||||
if (authId == null) return;
|
||||
return ['id', await client.companyuser.add(Number(companyId), Number(authId))];
|
||||
}
|
||||
case 'companyuser.getByEmail': {
|
||||
const email = await askText('Email', {
|
||||
placeholder: 'user@example.com',
|
||||
validate: (v) => (!v?.trim() ? 'Required' : undefined),
|
||||
});
|
||||
if (email == null) return;
|
||||
return ['ids', await client.companyuser.getByEmail(email)];
|
||||
}
|
||||
case 'google_auth.add': {
|
||||
const email = await askText('Email', {
|
||||
placeholder: 'user@example.com',
|
||||
validate: (v) => (!v?.trim() ? 'Required' : undefined),
|
||||
});
|
||||
if (email == null) return;
|
||||
return ['authId', await client.google_auth.add(email)];
|
||||
}
|
||||
case 'google_auth.list': {
|
||||
return ['rows', await client.google_auth.list()];
|
||||
}
|
||||
case 'google_auth.del': {
|
||||
const id = await askInt('Auth id');
|
||||
if (id == null) return;
|
||||
await client.google_auth.del(Number(id));
|
||||
return ['ok', true];
|
||||
}
|
||||
case 'initialize': {
|
||||
await client.initialize();
|
||||
return ['ok', true];
|
||||
}
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.clear();
|
||||
p.intro(chalk.bgMagenta.black(' libdashstore ') + chalk.dim(' api tester'));
|
||||
@@ -75,34 +228,7 @@ async function main() {
|
||||
while (true) {
|
||||
const action = await p.select({
|
||||
message: 'API method',
|
||||
options: [
|
||||
{
|
||||
value: 'companyuser.getByEmail',
|
||||
label: 'companyuser.getByEmail',
|
||||
hint: 'email → id',
|
||||
},
|
||||
{
|
||||
value: 'company.getByCompanyUserId',
|
||||
label: 'company.getByCompanyUserId',
|
||||
hint: 'id → [id, lang, root, name]',
|
||||
},
|
||||
{
|
||||
value: 'category.getTree',
|
||||
label: 'category.getTree',
|
||||
hint: 'rootId → tree',
|
||||
},
|
||||
{
|
||||
value: 'initialize',
|
||||
label: 'initialize',
|
||||
hint: 'purge + recreate schema',
|
||||
},
|
||||
{
|
||||
value: 'purge',
|
||||
label: chalk.red('purge'),
|
||||
hint: 'drop all · exit',
|
||||
},
|
||||
{ value: 'exit', label: 'exit', hint: 'quit without purge' },
|
||||
],
|
||||
options: METHODS,
|
||||
});
|
||||
|
||||
if (p.isCancel(action) || action === 'exit') {
|
||||
@@ -110,74 +236,20 @@ async function main() {
|
||||
await shutdown(pool, 0);
|
||||
}
|
||||
|
||||
if (action === 'purge') {
|
||||
const run = p.spinner();
|
||||
run.start('Purging…');
|
||||
await client.purge();
|
||||
run.stop(chalk.green('purge'));
|
||||
p.outro(chalk.red('Database purged — exiting'));
|
||||
await shutdown(pool, 0);
|
||||
}
|
||||
|
||||
try {
|
||||
if (action === 'companyuser.getByEmail') {
|
||||
const email = await p.text({
|
||||
message: 'Email',
|
||||
placeholder: 'user@example.com',
|
||||
validate: (v) => (!v?.trim() ? 'Required' : undefined),
|
||||
});
|
||||
if (p.isCancel(email)) continue;
|
||||
|
||||
const run = p.spinner();
|
||||
run.start('Running…');
|
||||
const id = await client.companyuser.getByEmail(email.trim());
|
||||
run.stop(chalk.green('companyuser.getByEmail'));
|
||||
printResult('id', id);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (action === 'company.getByCompanyUserId') {
|
||||
const raw = await p.text({
|
||||
message: 'Company user id',
|
||||
placeholder: '1',
|
||||
validate: (v) =>
|
||||
!/^\d+$/.test(v?.trim() ?? '') ? 'Integer required' : undefined,
|
||||
});
|
||||
if (p.isCancel(raw)) continue;
|
||||
|
||||
const run = p.spinner();
|
||||
run.start('Running…');
|
||||
const row = await client.company.getByCompanyUserId(Number(raw.trim()));
|
||||
run.stop(chalk.green('company.getByCompanyUserId'));
|
||||
printResult('[id, defaultLangId, rootCategoryId, name]', row);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (action === 'category.getTree') {
|
||||
const raw = await p.text({
|
||||
message: 'Root category id',
|
||||
placeholder: '1',
|
||||
validate: (v) =>
|
||||
!/^\d+$/.test(v?.trim() ?? '') ? 'Integer required' : undefined,
|
||||
});
|
||||
if (p.isCancel(raw)) continue;
|
||||
|
||||
const run = p.spinner();
|
||||
run.start('Running…');
|
||||
const tree = await client.category.getTree(Number(raw.trim()));
|
||||
run.stop(chalk.green('category.getTree'));
|
||||
printResult('tree', tree);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (action === 'initialize') {
|
||||
const run = p.spinner();
|
||||
run.start('Initializing…');
|
||||
await client.initialize();
|
||||
run.stop(chalk.green('initialize'));
|
||||
p.log.success('Schema purged and recreated');
|
||||
console.log();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (action === 'purge') {
|
||||
const run = p.spinner();
|
||||
run.start('Purging…');
|
||||
await client.purge();
|
||||
run.stop(chalk.green('purge'));
|
||||
p.outro(chalk.red('Database purged — exiting'));
|
||||
await shutdown(pool, 0);
|
||||
const result = await runMethod(client, action);
|
||||
if (result) {
|
||||
p.log.success(action);
|
||||
printResult(result[0], result[1]);
|
||||
}
|
||||
} catch (err) {
|
||||
p.log.error(err.message);
|
||||
|
||||
Reference in New Issue
Block a user