|
7 | 7 | type PluginRouteManager |
8 | 8 | } from '../../plugin-system/types'; |
9 | 9 |
|
10 | | -import { type AnyDatabase, getSchema } from '../../db'; // Import getSchema |
| 10 | +import { type AnyDatabase, type AnySchema, getSchema } from '../../db'; // Import getSchema and AnySchema |
11 | 11 | import { type BetterSQLite3Database } from 'drizzle-orm/better-sqlite3'; // For type guard |
12 | 12 | import { type NodePgDatabase } from 'drizzle-orm/node-postgres'; // For casting db |
13 | 13 | import { type SQLiteTable } from 'drizzle-orm/sqlite-core'; // For casting table from schema |
@@ -97,65 +97,54 @@ class ExamplePlugin implements Plugin { |
97 | 97 | tableDefinitions: examplePluginTableDefinitions, // Use tableDefinitions |
98 | 98 |
|
99 | 99 | // Optional initialization function |
100 | | - // Use arrow function to correctly capture 'this' for access to this.meta.id |
101 | | - onDatabaseInit: async (db: AnyDatabase, logger: FastifyBaseLogger) => { |
102 | | - logger.info({ |
103 | | - operation: 'plugin_database_init', |
104 | | - pluginId: this.meta.id |
105 | | - }, 'Initializing example plugin database...'); |
| 100 | + onDatabaseInit: async (db: AnyDatabase, schema: AnySchema) => { |
| 101 | + // Note: The function signature expects (db, schema) not (db, logger) |
| 102 | + // We'll use console.log for now since logger is not available |
| 103 | + console.log('Initializing example plugin database...'); |
106 | 104 |
|
107 | | - const currentSchema = getSchema(); |
108 | | - // 'this' here refers to the ExamplePlugin instance because of the arrow function |
109 | | - const tableNameInSchema = `${this.meta.id}_example_entities`; |
110 | | - const table = currentSchema[tableNameInSchema]; |
| 105 | + // Use hardcoded plugin ID since 'this' is not available in arrow function |
| 106 | + const tableNameInSchema = `example-plugin_example_entities`; |
| 107 | + const table = schema[tableNameInSchema]; |
111 | 108 |
|
112 | | - if (!table) { |
113 | | - logger.error({ |
114 | | - operation: 'plugin_database_init', |
115 | | - pluginId: this.meta.id, |
116 | | - tableNameInSchema, |
117 | | - error: 'Table not found in global schema' |
118 | | - }, 'Critical: Table not found in global schema! Cannot initialize database for plugin.'); |
119 | | - return; |
120 | | - } |
121 | | - |
122 | | - let currentCount = 0; |
123 | | - if (isSQLiteDB(db)) { |
124 | | - const result = await db |
125 | | - .select({ count: sql<number>`count(*)` }) |
126 | | - .from(table as SQLiteTable) |
127 | | - .get(); |
128 | | - currentCount = result?.count ?? 0; |
129 | | - } else { |
130 | | - // Assume NodePgDatabase-like behavior |
131 | | - const rows = await (db as NodePgDatabase) |
132 | | - .select({ count: sql<number>`count(*)` }) |
133 | | - .from(table as PgTable); |
134 | | - currentCount = rows[0]?.count ?? 0; |
135 | | - } |
136 | | - |
137 | | - if (currentCount === 0) { |
138 | | - logger.info({ |
139 | | - operation: 'plugin_database_seed', |
140 | | - pluginId: this.meta.id |
141 | | - }, 'Seeding initial data...'); |
142 | | - const dataToSeed = { |
143 | | - id: 'example1', |
144 | | - name: 'Example Entity', |
145 | | - description: 'This is an example entity created by the plugin', |
146 | | - }; |
| 109 | + if (!table) { |
| 110 | + console.error('Critical: Table not found in global schema! Cannot initialize database for plugin.', { |
| 111 | + tableNameInSchema, |
| 112 | + availableTables: Object.keys(schema) |
| 113 | + }); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + let currentCount = 0; |
147 | 118 | if (isSQLiteDB(db)) { |
148 | | - await db.insert(table as SQLiteTable).values(dataToSeed).run(); |
| 119 | + const result = await db |
| 120 | + .select({ count: sql<number>`count(*)` }) |
| 121 | + .from(table as SQLiteTable) |
| 122 | + .get(); |
| 123 | + currentCount = result?.count ?? 0; |
149 | 124 | } else { |
150 | 125 | // Assume NodePgDatabase-like behavior |
151 | | - await (db as NodePgDatabase).insert(table as PgTable).values(dataToSeed); |
| 126 | + const rows = await (db as NodePgDatabase) |
| 127 | + .select({ count: sql<number>`count(*)` }) |
| 128 | + .from(table as PgTable); |
| 129 | + currentCount = rows[0]?.count ?? 0; |
| 130 | + } |
| 131 | + |
| 132 | + if (currentCount === 0) { |
| 133 | + console.log('Seeding initial data for example plugin...'); |
| 134 | + const dataToSeed = { |
| 135 | + id: 'example1', |
| 136 | + name: 'Example Entity', |
| 137 | + description: 'This is an example entity created by the plugin', |
| 138 | + }; |
| 139 | + if (isSQLiteDB(db)) { |
| 140 | + await db.insert(table as SQLiteTable).values(dataToSeed).run(); |
| 141 | + } else { |
| 142 | + // Assume NodePgDatabase-like behavior |
| 143 | + await (db as NodePgDatabase).insert(table as PgTable).values(dataToSeed); |
| 144 | + } |
| 145 | + console.log('Seeded initial data for example plugin'); |
152 | 146 | } |
153 | | - logger.info({ |
154 | | - operation: 'plugin_database_seed', |
155 | | - pluginId: this.meta.id |
156 | | - }, 'Seeded initial data'); |
157 | | - } |
158 | | - }, |
| 147 | + }, |
159 | 148 | }; |
160 | 149 |
|
161 | 150 | // Initialize the plugin (non-route initialization only) |
|
0 commit comments