Skip to content

Commit 40984f8

Browse files
authored
Merge pull request #27 from cnbrown04/prerelease
Prerelease
2 parents 0299b0f + 0cc13bc commit 40984f8

File tree

4 files changed

+400
-177
lines changed

4 files changed

+400
-177
lines changed

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"typescript": "^5.8.3"
2929
},
3030
"devDependencies": {
31+
"@types/node": "^24.0.8",
3132
"semantic-release": "^24.2.5"
3233
},
3334
"files": [

src/abac/adapter.ts

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ interface AbacAdapterConfig {
3838
db: MysqlConfig | PostgresConfig | SqliteConfig;
3939
}
4040

41+
interface AbacAdapter extends Kysely<Database> {
42+
dispose(): Promise<void>;
43+
}
44+
4145
async function createDialect(
4246
config: MysqlConfig | PostgresConfig | SqliteConfig
4347
): Promise<Dialect> {
@@ -177,7 +181,7 @@ async function createDialect(
177181

178182
async function createAbacAdapter(
179183
config: AbacAdapterConfig
180-
): Promise<Kysely<Database>> {
184+
): Promise<AbacAdapter> {
181185
/*******************************************************************
182186
* Kysely ABAC Plugin DB Config
183187
* EDIT ONLY BELOW THIS LINE
@@ -192,9 +196,61 @@ async function createAbacAdapter(
192196
* EDIT ONLY ABOVE THIS LINE
193197
******************************************************************/
194198

195-
return new Kysely<Database>({
199+
const db = new Kysely<Database>({
196200
dialect,
197201
});
202+
203+
// Store reference to the raw database connection for cleanup
204+
let rawConnection: any = null;
205+
206+
// Extract raw connection based on database type
207+
if (config.db.type === 'mysql') {
208+
// @ts-ignore - accessing internal pool property
209+
rawConnection = (dialect as any).pool;
210+
} else if (config.db.type === 'postgres') {
211+
// @ts-ignore - accessing internal pool property
212+
rawConnection = (dialect as any).pool;
213+
} else if (config.db.type === 'sqlite') {
214+
// @ts-ignore - accessing internal database property
215+
rawConnection = (dialect as any).database;
216+
}
217+
218+
// Extend the Kysely instance with disposal method
219+
const adapter = Object.assign(db, {
220+
async dispose(): Promise<void> {
221+
try {
222+
// Destroy the Kysely instance
223+
if (typeof db.destroy === 'function') {
224+
await db.destroy();
225+
}
226+
227+
// Clean up raw connections
228+
if (rawConnection) {
229+
if (config.db.type === 'mysql' || config.db.type === 'postgres') {
230+
// For MySQL and PostgreSQL pools
231+
if (typeof rawConnection.end === 'function') {
232+
await rawConnection.end();
233+
} else if (typeof rawConnection.destroy === 'function') {
234+
await rawConnection.destroy();
235+
}
236+
} else if (config.db.type === 'sqlite') {
237+
// For SQLite database
238+
if (typeof rawConnection.close === 'function') {
239+
rawConnection.close();
240+
}
241+
}
242+
}
243+
244+
// Clear reference
245+
rawConnection = null;
246+
} catch (error) {
247+
console.error('Error disposing database adapter:', error);
248+
throw error;
249+
}
250+
}
251+
}) as AbacAdapter;
252+
253+
return adapter;
198254
}
199255

200256
// Usage examples:
@@ -219,4 +275,4 @@ async function createAbacAdapter(
219275
// }
220276
// });
221277

222-
export { createAbacAdapter };
278+
export { createAbacAdapter, type AbacAdapter };

0 commit comments

Comments
 (0)