Skip to content

Commit

Permalink
refactor: automate database committing
Browse files Browse the repository at this point in the history
  • Loading branch information
Char2sGu committed May 25, 2022
1 parent 522b710 commit c4e11c9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export class AuthController {
this.db.add(user);
const refreshToken = await this.tokenService.signRefresh(user);
const accessToken = await this.tokenService.signAccess(refreshToken);
await this.db.commit();
return { user, refreshToken, accessToken };
}

Expand Down
2 changes: 1 addition & 1 deletion src/database/database-event.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventDispatcher, EventToken } from "@deepkit/event";
import { UnitOfWorkEvent } from "@deepkit/orm";

import { AppDatabase } from "./database.service";
import { AppDatabase } from "./database.provider";

export function forwardDatabaseEvents(
database: AppDatabase,
Expand Down
16 changes: 16 additions & 0 deletions src/database/database.listener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { eventDispatcher } from "@deepkit/event";
import { httpWorkflow } from "@deepkit/http";
import { DatabaseSession } from "@deepkit/orm";

import { DATABASE_SESSION } from "./database.module";

export class DatabaseListener {
@eventDispatcher.listen(httpWorkflow.onController, 1000)
async afterHttpController(
event: typeof httpWorkflow.onController.event,
): Promise<void> {
const session =
event.injectorContext.get<DatabaseSession<any>>(DATABASE_SESSION);
await session.commit();
}
}
5 changes: 3 additions & 2 deletions src/database/database.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { ClassType } from "@deepkit/core";
import { Inject } from "@deepkit/injector";
import * as orm from "@deepkit/orm"; // 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.

import { AppDatabase, SQLiteDatabase } from "./database.service";
import { DatabaseListener } from "./database.listener";
import { AppDatabase, SQLiteDatabase } from "./database.provider";

// Temporary workaround due to a bug related to @deepkit/injector, will be
// fixed in the next release.
Expand All @@ -27,13 +28,13 @@ export class DatabaseModule extends createModule(
scope: "http",
},
],
listeners: [DatabaseListener],
exports: [AppDatabase, DATABASE_SESSION],
config: DatabaseConfig,
},
"database",
) {
protected entities = new DatabaseEntitySet();

withEntities(...entities: ClassType[]): this {
entities.forEach((entity) => this.entities.add(entity));
return this;
Expand Down
File renamed without changes.

0 comments on commit c4e11c9

Please sign in to comment.