Drizzle the db

This commit is contained in:
Owen Schwartz 2024-09-28 12:42:38 -04:00
parent 584a4f28dc
commit db3ce357df
No known key found for this signature in database
GPG key ID: 8271FDFFD9E0CCBD
2 changed files with 26 additions and 14 deletions

View file

@ -7,6 +7,7 @@ import helmet from "helmet";
import cors from "cors"; import cors from "cors";
import unauth from "@server/routers/unauth"; import unauth from "@server/routers/unauth";
import Database from 'better-sqlite3'; import Database from 'better-sqlite3';
import { drizzle } from 'drizzle-orm/better-sqlite3';
const dev = environment.ENVIRONMENT !== "prod"; const dev = environment.ENVIRONMENT !== "prod";
const app = next({ dev }); const app = next({ dev });
@ -17,22 +18,29 @@ let db: Database.Database;
app.prepare().then(() => { app.prepare().then(() => {
// Open the SQLite database connection // Open the SQLite database connection
db = new Database(`${environment.CONFIG_PATH}/db/db.sqlite`, { verbose: console.log }); const sqlite = new Database(`${environment.CONFIG_PATH}/db/db.sqlite`, { verbose: console.log });
const db = drizzle(sqlite);
const server = express(); const server = express();
server.use(helmet()); server.use(helmet());
server.use(cors()); server.use(cors());
const prefix = `/api/${environment.API_VERSION}`; // Run migrations (if you're using Drizzle's migration system)
// migrate(db, { migrationsFolder: './drizzle' });
// Middleware to attach db to req object
server.use((req: Request & { db?: Database.Database }, res: Response, next) => { // Middleware to attach the database to the request
req.db = db; server.use((req, res, next) => {
next(); (req as any).db = db;
next();
}); });
const prefix = `/api/${environment.API_VERSION}`;
server.use(prefix, express.json(), unauth); server.use(prefix, express.json(), unauth);
// We are using NEXT from here on
server.all("*", (req: Request, res: Response) => { server.all("*", (req: Request, res: Response) => {
const parsedUrl = parse(req.url!, true); const parsedUrl = parse(req.url!, true);
handle(req, res, parsedUrl); handle(req, res, parsedUrl);

View file

@ -1,11 +1,13 @@
import { Request, Response, NextFunction } from 'express'; import { Request, Response, NextFunction } from 'express';
import { Database } from 'better-sqlite3'; import { DrizzleError } from 'drizzle-orm';
import { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';
import { sites, Site } from '../../db/schema';
interface CustomRequest extends Request { interface CustomRequest extends Request {
db?: Database; db?: BetterSQLite3Database;
} }
export const getConfig = (req: Request, res: Response, next: NextFunction): void => { export const getConfig = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
try { try {
const customReq = req as CustomRequest; const customReq = req as CustomRequest;
const db = customReq.db; const db = customReq.db;
@ -14,13 +16,15 @@ export const getConfig = (req: Request, res: Response, next: NextFunction): void
throw new Error('Database is not attached to the request'); throw new Error('Database is not attached to the request');
} }
const query = 'SELECT * FROM sites'; const results: Site[] = db.select().from(sites).all();
const statement = db.prepare(query);
const results = statement.all();
res.json(results); res.json(results);
} catch (error) { } catch (error) {
console.error('Error querying database:', error); console.error('Error querying database:', error);
next(error); if (error instanceof DrizzleError) {
res.status(500).json({ error: 'Database query error', message: error.message });
} else {
next(error);
}
} }
}; };