import 'dotenv/config';
import sqlite3 from 'sqlite3';
import { open, Database } from 'sqlite';
let dbInstance: Database | null = null;
export async function getDb(): Promise<Database> {
if (!dbInstance) {
dbInstance = await open({
filename: 'C:\\temp\\ecommerce.db',
driver: sqlite3.Database
});
// Create products table
await dbInstance.exec(`
CREATE TABLE IF NOT EXISTS products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sku TEXT UNIQUE NOT NULL,
name TEXT NOT NULL,
description TEXT,
price REAL NOT NULL,
quantity INTEGER NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
console.log('✅ SQLite database initialized');
}
return dbInstance;
}
// SQLite result types
export interface RunResult {
lastID?: number;
changes?: number;
}