-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: decouple database initialization from the provided implemen…
…tation
- Loading branch information
Showing
7 changed files
with
83 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { EventDispatcher } from "@deepkit/event"; | ||
import { SQLiteDatabaseAdapter } from "@deepkit/sqlite"; | ||
|
||
import { DatabaseConfig } from "./database.config"; | ||
import { InjectDatabase } from "./database.tokens"; | ||
import { forwardDatabaseEvents } from "./database-event"; | ||
|
||
export class DatabaseInitializer { | ||
constructor( | ||
private database: InjectDatabase, | ||
private config: Pick<DatabaseConfig, "logging">, | ||
private eventDispatcher: EventDispatcher, | ||
) {} | ||
|
||
async initialize(): Promise<void> { | ||
await this.fixConnectionLogger(); | ||
if (this.config.logging) this.database.logger.enableLogging(); | ||
forwardDatabaseEvents(this.database, this.eventDispatcher); | ||
} | ||
|
||
/** | ||
* Temporary workaround for SQLite connection logger. | ||
* | ||
* SQLite uses single connection, thus once a connection is created, all the | ||
* following operations will reuse that connection. | ||
* | ||
* The first connection is created during migration, which accidentally | ||
* doesn't have access to our logger and created a new one, thus all the | ||
* operations will not use our logger. | ||
* | ||
* Should be removed once fixed. | ||
*/ | ||
private async fixConnectionLogger() { | ||
const adapter = this.database.adapter as SQLiteDatabaseAdapter; | ||
const connection = await adapter.connectionPool.getConnection(); | ||
connection["logger"] = this.database.logger; | ||
connection.release(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,13 @@ | ||
import { EventDispatcher } from "@deepkit/event"; | ||
import { Database } from "@deepkit/orm"; | ||
import { SQLiteDatabaseAdapter } from "@deepkit/sqlite"; | ||
|
||
import { DatabaseConfig } from "./database.config"; | ||
import { DatabaseEntitySet } from "./database.module"; | ||
import { forwardDatabaseEvents } from "./database-event"; | ||
|
||
export class SQLiteDatabase extends Database { | ||
override name = "default"; | ||
|
||
constructor( | ||
url: DatabaseConfig["url"], | ||
logging: DatabaseConfig["logging"], | ||
entities: DatabaseEntitySet, | ||
eventDispatcher: EventDispatcher, | ||
) { | ||
constructor(url: DatabaseConfig["url"], entities: DatabaseEntitySet) { | ||
super(new SQLiteDatabaseAdapter(url), [...entities]); | ||
forwardDatabaseEvents(this, eventDispatcher); | ||
this.fixConnectionLogger(); | ||
if (logging) this.logger.enableLogging(); | ||
} | ||
|
||
/** | ||
* Temporary workaround for SQLite connection logger. | ||
* | ||
* SQLite uses single connection, thus once a connection is created, all the | ||
* following operations will reuse that connection. | ||
* | ||
* The first connection is created during migration, which accidentally | ||
* doesn't have access to our logger and created a new one, thus all the | ||
* operations will not use our logger. | ||
* | ||
* Should be removed once fixed. | ||
*/ | ||
private async fixConnectionLogger() { | ||
const adapter = this.adapter as SQLiteDatabaseAdapter; | ||
const connection = await adapter.connectionPool.getConnection(); | ||
connection["logger"] = this.logger; | ||
connection.release(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Inject } from "@deepkit/injector"; | ||
import * as orm from "@deepkit/orm"; // temporary workaround: we have to use namespace import here as a temporary workaround, otherwise the application will not be able to bootstrap. This will be fixed in the next release | ||
|
||
// temporary workaround: when database is provided using a class extending | ||
// `Database`, and some entities is extending a base class and passing generic | ||
// type arguments, the debugger will fail to work | ||
// https://github.com/deepkit/deepkit-framework/issues/241 | ||
export const DATABASE = "token:database"; // temporary workaround: type `symbol` is missing in type `ExportType`, so we use string instead | ||
export class DatabaseFactoryToken {} // temporary workaround for https://github.com/deepkit/deepkit-framework/issues/240 | ||
export type InjectDatabase = Inject<orm.Database, typeof DATABASE>; | ||
|
||
// temporary workaround: `Database` cannot be used as a token when framework | ||
// debug mode is enabled | ||
export const DATABASE_SESSION = "token:database-session"; // temporary workaround: type `symbol` is missing in type `ExportType`, so we use string instead | ||
export type InjectDatabaseSession = Inject< | ||
orm.DatabaseSession<orm.DatabaseAdapter>, | ||
typeof DATABASE_SESSION | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters