Skip to content

Commit

Permalink
fix: deep merge of MikroORM options
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastien Gatellier committed Jul 4, 2023
1 parent cc23c84 commit 145274a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
18 changes: 8 additions & 10 deletions modules/common/src/module/listener/ModuleListenerDatabaseConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import { ServiceEntity } from "../../entities/ServiceEntity.js"
const DB_NAME = "heart"

export function createDatabaseConfig<D extends IDatabaseDriver>(options: Options<D>): Options<D> {
return {
...options,
...{
dbName: DB_NAME,
entities: [ReportEntity, ServiceEntity],
metadataProvider: TsMorphMetadataProvider,
migrations: {
snapshot: false,
},
},
options.dbName = DB_NAME
options.entities = [ReportEntity, ServiceEntity]
options.metadataProvider = TsMorphMetadataProvider

if (options.migrations) {
options.migrations.snapshot = false
}

return options
}
8 changes: 4 additions & 4 deletions modules/mysql/src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class MySQLClient {
#orm: MikroORM<MySqlDriver> | undefined

public async getMigrator(): Promise<IMigrator> {
const orm = await this.getOrCreateDatabaseClient()
const orm = await this.getOrCreateOrm()

return orm.getMigrator()
}
Expand All @@ -20,7 +20,7 @@ export class MySQLClient {
const reportEntity = await this.createReportEntity(report)

const em = await this.getOrCreateEntityManager()
const orm = await this.getOrCreateDatabaseClient()
const orm = await this.getOrCreateOrm()

await em.persistAndFlush(reportEntity)

Expand All @@ -40,7 +40,7 @@ export class MySQLClient {
* Get or create the ORM: avoid the opening of several DB connection at the same or in a very short interval of time.
* As Heart is a CLI tool, having the DB connection opened during a single analysis that longs a couple of minutes is acceptable.
*/
private async getOrCreateDatabaseClient(): Promise<MikroORM<MySqlDriver>> {
private async getOrCreateOrm(): Promise<MikroORM<MySqlDriver>> {
if (this.#orm === undefined) {
this.#orm = await MikroORM.init<MySqlDriver>(databaseConfig)
}
Expand All @@ -53,7 +53,7 @@ export class MySQLClient {
* @link https://mikro-orm.io/docs/identity-map
*/
private async getOrCreateEntityManager(): Promise<SqlEntityManager> {
const orm = await this.getOrCreateDatabaseClient()
const orm = await this.getOrCreateOrm()

if (this.#em === undefined) {
this.#em = orm.em.fork()
Expand Down
4 changes: 2 additions & 2 deletions modules/mysql/src/config/mikro-orm.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createDatabaseConfig } from "@fabernovel/heart-common"
import { defineConfig } from "@mikro-orm/mysql"
import { MySqlDriver, defineConfig } from "@mikro-orm/mysql"
import { env } from "node:process"
import { Migration20230702150637 } from "../migrations/Migration20230702150637.js"

export default defineConfig(
createDatabaseConfig({
createDatabaseConfig<MySqlDriver>({
clientUrl: env.HEART_MYSQL_DATABASE_URL as string,

Check warning on line 8 in modules/mysql/src/config/mikro-orm.config.ts

View workflow job for this annotation

GitHub Actions / Syntax & changelog

Use a ! assertion to more succinctly remove null and undefined from the type
migrations: {
migrationsList: [
Expand Down

0 comments on commit 145274a

Please sign in to comment.