-
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.
- Loading branch information
1 parent
815d649
commit 6a8ccdc
Showing
15 changed files
with
123 additions
and
113 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 @@ | ||
export * from './sync.injection.token'; |
2 changes: 1 addition & 1 deletion
2
...ema/schema.enum/schema.injection.token.ts → ...ce/sync/sync.enum/sync.injection.token.ts
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,4 +1,4 @@ | ||
export enum SchemaInjectionToken { | ||
export enum SyncInjectionToken { | ||
MODULE_ID = 'MODULE_ID', | ||
MODULE_OPTIONS = 'MODULE_OPTIONS', | ||
} |
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 @@ | ||
export * from './sync.module.options'; |
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,12 @@ | ||
import { ModuleMetadata } from '@bechara/nestjs-core'; | ||
|
||
export interface SyncAsyncModuleOptions extends Pick<ModuleMetadata, 'imports'> { | ||
inject?: any[]; | ||
useFactory?: (...args: any[]) => Promise<SyncModuleOptions> | SyncModuleOptions; | ||
} | ||
|
||
export interface SyncModuleOptions { | ||
enable?: boolean; | ||
safe?: boolean; | ||
blacklist?: string[]; | ||
} |
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,70 @@ | ||
import { Inject, Injectable, LoggerService } from '@bechara/nestjs-core'; | ||
import { MikroORM } from '@mikro-orm/core'; | ||
|
||
import { SyncInjectionToken } from './sync.enum'; | ||
import { SyncModuleOptions } from './sync.interface'; | ||
|
||
@Injectable() | ||
export class SyncService { | ||
|
||
public constructor( | ||
@Inject(SyncInjectionToken.MODULE_OPTIONS) | ||
private readonly syncModuleOptions: SyncModuleOptions, | ||
private readonly mikroOrm: MikroORM, | ||
private readonly loggerService: LoggerService, | ||
) { | ||
const options = this.syncModuleOptions; | ||
|
||
if (options.enable) { | ||
void this.syncSync(options); | ||
} | ||
} | ||
|
||
/** | ||
* Remove from schema queries that has been blacklisted. | ||
* @param queries | ||
* @param options | ||
*/ | ||
private removeBlacklistedQueries(queries: string, options: SyncModuleOptions): string { | ||
options.blacklist ??= [ ]; | ||
queries = queries.replace(/\n+/g, '\n'); | ||
return queries.split('\n').filter((q) => !options.blacklist.includes(q)).join('\n'); | ||
} | ||
|
||
/** | ||
* Automatically sync current database schema with | ||
* configured entities. | ||
* @param options | ||
*/ | ||
public async syncSync(options: SyncModuleOptions): Promise<void> { | ||
this.loggerService.info('[OrmService] Starting database schema sync...'); | ||
|
||
const generator = this.mikroOrm.getSchemaGenerator(); | ||
let syncDump = await generator.getUpdateSchemaSQL(false, options.safe); | ||
syncDump = this.removeBlacklistedQueries(syncDump, options); | ||
|
||
if (syncDump.length === 0) { | ||
return this.loggerService.notice('[OrmService] Database schema is up to date'); | ||
} | ||
|
||
let syncQueries = await generator.getUpdateSchemaSQL(true, options.safe); | ||
syncQueries = this.removeBlacklistedQueries(syncQueries, options); | ||
await generator.execute(syncQueries); | ||
|
||
this.loggerService.notice('[OrmService] Database schema successfully updated'); | ||
} | ||
|
||
/** | ||
* Erase current database schema and recreate it. | ||
*/ | ||
public async resetSync(): Promise<void> { | ||
this.loggerService.info('[OrmService] Starting database schema reset...'); | ||
|
||
const generator = this.mikroOrm.getSchemaGenerator(); | ||
await generator.dropSchema(); | ||
await generator.createSchema(); | ||
|
||
this.loggerService.notice('[OrmService] Database schema successfully reset'); | ||
} | ||
|
||
} |
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