Skip to content
Aabha AI Academy
Skip to reading

SQL

Run

// Practice runner: fresh synthetic database for every invocation.
import {PGlite} from '@electric-sql/pglite';
import fs from 'node:fs/promises';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..');
const input=process.argv[2];
if (!input) {console.error('Usage: node tools/run.mjs sql/examples/02_select.sql');process.exit(2);}
const db=new PGlite();
try {
 await db.exec(await fs.readFile(path.join(root,'sql/00_setup.sql'),'utf8'));
 await db.exec("BEGIN READ ONLY; SET LOCAL statement_timeout = '5s';");
 const results=await db.exec(await fs.readFile(path.resolve(input),'utf8'));
 for (const r of results) {
   if (r.rows) console.table(r.rows);
 }
 await db.exec('ROLLBACK;');
} catch(e) {console.error('Query failed:',e.message);process.exitCode=1;}
finally {await db.close();}

Download this source file