This commit is contained in:
seb
2026-07-31 00:08:50 +02:00
parent 93e52cd513
commit 6bdac7e543
17 changed files with 520 additions and 24 deletions

25
scripts/init-schema.js Normal file
View File

@@ -0,0 +1,25 @@
import 'dotenv/config';
import sql from 'mssql';
import { SCHEMA_SCRIPTS } from '../src/schema.js';
const config = {
server: process.env.MSSQL_SERVER,
database: process.env.MSSQL_DATABASE,
user: process.env.MSSQL_USER,
password: process.env.MSSQL_PASSWORD,
options: {
encrypt: false,
trustServerCertificate: true,
},
};
const pool = await sql.connect(config);
try {
for (const script of SCHEMA_SCRIPTS) {
await pool.request().query(script);
}
console.log(`Schema init OK (${SCHEMA_SCRIPTS.length} scripts)`);
} finally {
await pool.close();
}

193
scripts/test-api.js Normal file
View File

@@ -0,0 +1,193 @@
import 'dotenv/config';
import sql from 'mssql';
import chalk from 'chalk';
import * as p from '@clack/prompts';
import LibDashStore from '../src/index.js';
const config = {
server: process.env.MSSQL_SERVER,
database: process.env.MSSQL_DATABASE,
user: process.env.MSSQL_USER,
password: process.env.MSSQL_PASSWORD,
options: {
encrypt: false,
trustServerCertificate: true,
},
};
function printResult(label, value) {
p.log.step(chalk.cyan(label));
const text =
value === undefined
? chalk.dim('undefined')
: typeof value === 'string'
? value
: JSON.stringify(value, null, 2);
console.log(chalk.white(text));
console.log();
}
async function shutdown(pool, code = 0) {
try {
await pool.close();
} catch {
// ignore
}
process.exit(code);
}
async function main() {
console.clear();
p.intro(chalk.bgMagenta.black(' libdashstore ') + chalk.dim(' api tester'));
const spin = p.spinner();
spin.start('Connecting to MSSQL…');
let pool;
try {
pool = await sql.connect(config);
} catch (err) {
spin.stop(chalk.red('Connection failed'));
p.log.error(err.message);
p.outro(chalk.red('Aborted'));
process.exit(1);
}
spin.message('Initializing schema…');
let client;
try {
client = await new LibDashStore(pool);
spin.stop(chalk.green('Ready'));
} catch (err) {
spin.stop(chalk.red('Initialize failed'));
p.log.error(err.message);
await shutdown(pool, 1);
}
p.note(
[
chalk.dim(`${config.server} / ${config.database}`),
chalk.dim('purge ends the session'),
].join('\n'),
'Connected',
);
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' },
],
});
if (p.isCancel(action) || action === 'exit') {
p.outro(chalk.dim('Bye'));
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);
}
} catch (err) {
p.log.error(err.message);
console.log(chalk.dim(err.stack?.split('\n').slice(1, 4).join('\n') ?? ''));
console.log();
}
}
}
main().catch(async (err) => {
console.error(chalk.red(err.stack ?? err.message));
process.exit(1);
});