Add PostgreSQL support with SQLite/Postgres database compatibility layer
- Add dbGet/dbAll helper functions for database-agnostic queries - Add toDbBool/convertBooleansForDb for boolean type conversion - Add toDbDate/getNow for timestamp type handling - Add generateId that returns UUID for Postgres, nanoid for SQLite - Update all routes to use compatibility helpers - Add normalizeEvent to return clean number types from Postgres decimal - Add formatPrice utility for consistent price display - Add legal pages admin interface with RichTextEditor - Update carousel images - Add drizzle migration files for PostgreSQL
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'dotenv/config';
|
||||
import { drizzle as drizzleSqlite } from 'drizzle-orm/better-sqlite3';
|
||||
import { drizzle as drizzlePg } from 'drizzle-orm/node-postgres';
|
||||
import Database from 'better-sqlite3';
|
||||
@@ -29,5 +30,51 @@ if (dbType === 'postgres') {
|
||||
db = drizzleSqlite(sqlite, { schema });
|
||||
}
|
||||
|
||||
export { db };
|
||||
// ==================== Database Compatibility Helpers ====================
|
||||
// These functions abstract the differences between SQLite and PostgreSQL Drizzle drivers:
|
||||
// - SQLite uses .get() for single result, .all() for multiple
|
||||
// - PostgreSQL returns arrays directly (no .get()/.all() methods)
|
||||
|
||||
/**
|
||||
* Get a single result from a query (works with both SQLite and PostgreSQL)
|
||||
* @param query - A Drizzle query builder (e.g., db.select().from(table).where(...))
|
||||
* @returns The first result or null
|
||||
*/
|
||||
export async function dbGet<T>(query: any): Promise<T | null> {
|
||||
if (dbType === 'postgres') {
|
||||
const results = await query;
|
||||
return results[0] || null;
|
||||
}
|
||||
// SQLite - use .get()
|
||||
return query.get() || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all results from a query (works with both SQLite and PostgreSQL)
|
||||
* @param query - A Drizzle query builder (e.g., db.select().from(table).where(...))
|
||||
* @returns Array of results
|
||||
*/
|
||||
export async function dbAll<T>(query: any): Promise<T[]> {
|
||||
if (dbType === 'postgres') {
|
||||
return await query;
|
||||
}
|
||||
// SQLite - use .all()
|
||||
return query.all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if using PostgreSQL
|
||||
*/
|
||||
export function isPostgres(): boolean {
|
||||
return dbType === 'postgres';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if using SQLite
|
||||
*/
|
||||
export function isSqlite(): boolean {
|
||||
return dbType === 'sqlite';
|
||||
}
|
||||
|
||||
export { db, dbType };
|
||||
export * from './schema.js';
|
||||
|
||||
Reference in New Issue
Block a user