Skip to content

Commit

Permalink
refactor: use database unit of work
Browse files Browse the repository at this point in the history
  • Loading branch information
Char2sGu committed May 19, 2022
1 parent 70dda70 commit 0237235
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { http, HttpQueries } from "@deepkit/http";
import { AppDatabase } from "src/database/database.service";
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 { ResourceService } from "src/resource/resource.service";
import { ResourceCrud } from "src/resource/resource-crud.typings";
import { ResourceFilterMap } from "src/resource/resource-filter.typings";
Expand All @@ -10,27 +11,32 @@ import {
import { ResourceOrderMap } from "src/resource/resource-order.typings";

import { User } from "./user.entity";
import { DATABASE_SESSION } from "./user.module";

// Temporary workaround due to a bug related to @deepkit/injector, will be
// fixed in the next release.
type InjectDatabaseSession = Inject<
orm.DatabaseSession<orm.DatabaseAdapter>,
typeof DATABASE_SESSION
>;

@http.controller("users")
export class UserController implements Partial<ResourceCrud<User>> {
private query = this.database.query(User);

constructor(
private database: AppDatabase,
private db: InjectDatabaseSession,
private res: ResourceService<User>,
) {}

@http.GET()
async list(
// HttpQueries and HttpQuery cannot exist at the same time, so this might be the best solution
{ filter, order, ...pagination }: HttpQueries<UserListQuery>,
{ filter, order, ...pagination }: HttpQueries<UserListQuery>, // HttpQueries and HttpQuery cannot exist at the same time currently, but this feature might be available in a future release.
): Promise<ResourceList<User>> {
return this.res.list(this.query, pagination, filter, order);
return this.res.list(this.db.query(), pagination, filter, order);
}

@http.GET(":uuid")
async retrieve(uuid: string): Promise<User> {
return this.res.retrieve(this.query, { uuid });
return this.res.retrieve(this.db.query(), { uuid });
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { createModule } from "@deepkit/app";
import { AppDatabase } from "src/database/database.service";

import { UserController } from "./user.controller";
import { UserEventListener } from "./user.entity";

// Temporary workaround due to a bug related to @deepkit/injector, will be
// fixed in the next release.
export const DATABASE_SESSION = Symbol();

export class UserModule extends createModule({
controllers: [UserController],
providers: [
{
provide: DATABASE_SESSION,
useFactory: (db: AppDatabase) => db.createSession(),
scope: "http",
},
],
listeners: [UserEventListener],
}) {}

0 comments on commit 0237235

Please sign in to comment.