Files
Krikorios a0b297285a Add self-hosted auth, admin API, and owner web dashboard
- Replace Clerk with self-hosted JWT auth (register/login/verify, bcrypt
  passwords, Gmail OTP with console fallback)
- Add lib/db.ts pg pool + transaction helpers; seed script migrates legacy
  Clerk-era schema (drop clerk_id, enforce UUID ids and unique email)
- Add owner-gated admin API: stats, users, drivers CRUD, rides
- Add dashboard/ Vite React owner dashboard (login, overview, users,
  fleet, rides) with dev-server proxy to avoid Expo CORS middleware
- Add scripts/set-owner.mjs for role management
2026-08-23 16:38:41 +03:00

68 lines
1.5 KiB
TypeScript

import { Pool, type QueryResultRow } from "pg";
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
max: 10,
idleTimeoutMillis: 30_000,
connectionTimeoutMillis: 10_000,
});
type SqlValue = string | number | boolean | null | Date;
export async function sql<R extends QueryResultRow = QueryResultRow>(
strings: TemplateStringsArray,
...values: SqlValue[]
): Promise<R[]> {
const text = strings.reduce(
(acc, chunk, i) =>
acc + chunk + (i < values.length ? `$${i + 1}` : ""),
"",
);
const result = await pool.query<R>(text, values);
return result.rows;
}
export async function transaction<T>(
callback: (
tx: <R extends QueryResultRow = QueryResultRow>(
strings: TemplateStringsArray,
...values: SqlValue[]
) => Promise<R[]>,
) => Promise<T>,
): Promise<T> {
const client = await pool.connect();
try {
await client.query("BEGIN");
const tx = async <R extends QueryResultRow = QueryResultRow>(
strings: TemplateStringsArray,
...values: SqlValue[]
) => {
const text = strings.reduce(
(acc, chunk, i) =>
acc + chunk + (i < values.length ? `$${i + 1}` : ""),
"",
);
const result = await client.query<R>(text, values);
return result.rows;
};
const out = await callback(tx);
await client.query("COMMIT");
return out;
} catch (error) {
await client.query("ROLLBACK");
throw error;
} finally {
client.release();
}
}