initial version
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import Database from 'better-sqlite3';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
const dataDir = path.join(process.cwd(), 'data');
|
||||
if (!fs.existsSync(dataDir)) {
|
||||
fs.mkdirSync(dataDir, { recursive: true });
|
||||
}
|
||||
|
||||
const dbPath = path.join(dataDir, 'app.db');
|
||||
const isNewDb = !fs.existsSync(dbPath);
|
||||
|
||||
export const db = new Database(dbPath);
|
||||
db.pragma('foreign_keys = ON');
|
||||
db.pragma('journal_mode = WAL');
|
||||
|
||||
if (isNewDb) {
|
||||
const schema = fs.readFileSync(path.join(__dirname, 'schema.sql'), 'utf-8');
|
||||
db.exec(schema);
|
||||
}
|
||||
|
||||
const FIRST_ORDER_NUMBER = 8246;
|
||||
|
||||
/** Allocates the next order number inside the caller's transaction. */
|
||||
export function nextOrderNumber(): number {
|
||||
const row = db
|
||||
.prepare(`SELECT COALESCE(MAX(order_number), ?) + 1 AS next FROM service_orders`)
|
||||
.get(FIRST_ORDER_NUMBER) as { next: number };
|
||||
return row.next;
|
||||
}
|
||||
Reference in New Issue
Block a user