Skip to content

Commit 62fc5bc

Browse files
author
Lasim
committed
Refactor database handling and plugin system to improve type safety and clarity
1 parent 2c8f040 commit 62fc5bc

File tree

8 files changed

+15
-4
lines changed

8 files changed

+15
-4
lines changed

services/backend/src/db/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fs from 'node:fs/promises';
22
import path from 'node:path';
3-
import { type SQL } from 'drizzle-orm'; // Removed 'sql' import as we'll use strings for raw exec
43
import { type Plugin, type DatabaseExtension } from '../plugin-system/types'; // Added DatabaseExtension
54

65
// Config
@@ -327,6 +326,7 @@ export function getDbStatus() {
327326

328327
// Define a more specific type for DatabaseExtension if possible, or use 'any' for now.
329328
interface DatabaseExtensionWithTables extends DatabaseExtension {
329+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
330330
tableDefinitions?: Record<string, Record<string, (columnBuilder: any) => any>>;
331331
onDatabaseInit?: (db: AnyDatabase) => Promise<void>; // Ensure onDatabaseInit accepts AnyDatabase
332332
}
@@ -359,7 +359,7 @@ export async function createPluginTables(_db: AnyDatabase, plugins: Plugin[]) {
359359
const ext = plugin.databaseExtension as DatabaseExtensionWithTables | undefined; // Cast here
360360
if (!ext || !ext.tableDefinitions) continue;
361361

362-
for (const [defName, _tableDef] of Object.entries(ext.tableDefinitions)) { // _tableDef not used
362+
for (const [defName] of Object.entries(ext.tableDefinitions)) {
363363
const fullTableName = `${plugin.meta.id}_${defName}`;
364364
if (dbSchema && dbSchema[fullTableName]) {
365365
console.log(`[INFO] Table ${fullTableName} already defined in schema. Creation should be handled by migrations.`);

services/backend/src/db/schema.pg.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { pgTable, text as pgText, integer as pgInteger, timestamp as pgTimestamp } from 'drizzle-orm/pg-core';
66
import { baseTableDefinitions, pluginTableDefinitions } from './schema'; // Central definitions
77

8+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
89
const tables: Record<string, any> = {};
910

1011
// Helper to get the correct PG column builder based on a simple type string
@@ -18,6 +19,7 @@ function getPgColumnBuilder(type: 'text' | 'integer' | 'timestamp') {
1819

1920
// Instantiate base tables for PostgreSQL
2021
for (const [tableName, tableColumnDefinitions] of Object.entries(baseTableDefinitions)) {
22+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2123
const columns: Record<string, any> = {};
2224
for (const [columnName, columnDefFunc] of Object.entries(tableColumnDefinitions)) {
2325
// Determine builder type (heuristic, same as in db/index.ts)
@@ -38,6 +40,7 @@ for (const [tableName, tableColumnDefinitions] of Object.entries(baseTableDefini
3840

3941
// Instantiate plugin tables for PostgreSQL (similar logic)
4042
for (const [tableName, tableColumnDefinitions] of Object.entries(pluginTableDefinitions)) {
43+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4144
const columns: Record<string, any> = {};
4245
for (const [columnName, columnDefFunc] of Object.entries(tableColumnDefinitions)) {
4346
let builderType: 'text' | 'integer' | 'timestamp' = 'text';
@@ -58,6 +61,7 @@ for (const [tableName, tableColumnDefinitions] of Object.entries(pluginTableDefi
5861
export const { users, ...otherBaseTables } = tables; // Assuming 'users' is a key in tables
5962
// For plugin tables, they would also need to be destructured and exported if `tables` contains them directly.
6063
// Or, more robustly:
64+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6165
const allExports: Record<string, any> = {};
6266
for(const key in tables) {
6367
allExports[key] = tables[key];

services/backend/src/db/schema.sqlite.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { sqliteTable, text as sqliteText, integer as sqliteInteger } from 'drizzle-orm/sqlite-core';
66
import { baseTableDefinitions, pluginTableDefinitions } from './schema'; // Central definitions
77

8+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
89
const tables: Record<string, any> = {};
910

1011
// Helper to get the correct SQLite column builder
@@ -20,6 +21,7 @@ function getSqliteColumnBuilder(type: 'text' | 'integer' | 'timestamp') {
2021

2122
// Instantiate base tables for SQLite
2223
for (const [tableName, tableColumnDefinitions] of Object.entries(baseTableDefinitions)) {
24+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2325
const columns: Record<string, any> = {};
2426
for (const [columnName, columnDefFunc] of Object.entries(tableColumnDefinitions)) {
2527
// Determine builder type (heuristic, same as in db/index.ts and schema.pg.ts)
@@ -40,6 +42,7 @@ for (const [tableName, tableColumnDefinitions] of Object.entries(baseTableDefini
4042

4143
// Instantiate plugin tables for SQLite (similar logic)
4244
for (const [tableName, tableColumnDefinitions] of Object.entries(pluginTableDefinitions)) {
45+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4346
const columns: Record<string, any> = {};
4447
for (const [columnName, columnDefFunc] of Object.entries(tableColumnDefinitions)) {
4548
let builderType: 'text' | 'integer' | 'timestamp' = 'text';

services/backend/src/db/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
// This file exports table column definitions.
23
// The actual Drizzle table objects (sqliteTable, pgTable) will be constructed
34
// in db/index.ts based on the selected database dialect.

services/backend/src/plugin-system/plugin-manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from './types';
1414
import {
1515
PluginLoadError,
16+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1617
PluginInitializeError,
1718
PluginDuplicateError,
1819
PluginNotFoundError

services/backend/src/plugin-system/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface DatabaseExtension {
2525
* Value: Column definitions object, similar to baseTableDefinitions in schema.ts.
2626
* e.g., { id: (b:any)=>b('id'), name: (b:any)=>b('name') }
2727
*/
28+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2829
tableDefinitions?: Record<string, Record<string, (columnBuilder: any) => any>>;
2930

3031
/**

services/backend/src/plugins/example-plugin/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
import { type Plugin, type DatabaseExtension } from '../../plugin-system/types';
23
import { type FastifyInstance } from 'fastify';
34
import { type AnyDatabase, getSchema } from '../../db'; // Import getSchema

services/backend/src/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
import fastify from 'fastify'
23
import path from 'node:path'
34
import { loggerConfig } from './fastify/config/logger'
@@ -12,8 +13,7 @@ import {
1213
createPluginTables,
1314
getDb,
1415
getDbConnection,
15-
getDbStatus,
16-
type AnyDatabase // Make sure AnyDatabase is exported from db/index.ts
16+
getDbStatus
1717
} from './db'
1818
import type SqliteDriver from 'better-sqlite3'; // For type checking in onClose
1919
import type { Pool as PgPool } from 'pg'; // For type checking in onClose

0 commit comments

Comments
 (0)