-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from skgndi12/feature/issue-53/design-reposito…
…ry-layer [#53] Design repository layer
- Loading branch information
Showing
23 changed files
with
323 additions
and
43 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
import { UUID } from 'crypto'; | ||
|
||
export interface Profile { | ||
id: UUID; | ||
id: string; | ||
name: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
import { UUID } from 'crypto'; | ||
|
||
export interface User { | ||
id: UUID; | ||
id: string; | ||
name: string; | ||
email: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export enum PrismaErrorCode { | ||
TRANSACTION_CONFLICT = 'P2034' | ||
} | ||
|
||
export interface ErrorWithCode { | ||
code: string; | ||
} | ||
|
||
export function isErrorWithCode(error: unknown): error is ErrorWithCode { | ||
return ( | ||
typeof error === 'object' && | ||
error !== null && | ||
'code' in error && | ||
typeof (error as Record<string, unknown>).code === '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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
import { DatabaseConfig } from '@src/infrastructure/repositories/types'; | ||
|
||
export function generatePrismaClient(config: DatabaseConfig): PrismaClient { | ||
return new PrismaClient({ | ||
datasources: { | ||
db: { | ||
url: `postgresql://${config.user}:${config.password}@${config.host}:${config.port}/mrc` | ||
} | ||
} | ||
}); | ||
} |
44 changes: 44 additions & 0 deletions
44
api/src/infrastructure/prisma/prisma.transaction.manager.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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Prisma, PrismaClient } from '@prisma/client'; | ||
|
||
import { | ||
PrismaErrorCode, | ||
isErrorWithCode | ||
} from '@src/infrastructure/prisma/errors'; | ||
import { RepositoryError } from '@src/infrastructure/repositories/errors'; | ||
import { IsolationLevel } from '@src/infrastructure/repositories/types'; | ||
import { TransactionManager } from '@src/ports/transaction.manager'; | ||
import { getErrorMessage } from '@src/util/error'; | ||
|
||
export class PrismaTransactionManager implements TransactionManager { | ||
constructor(private readonly client: PrismaClient) {} | ||
|
||
public runInTransaction = async <T>( | ||
callback: (tx: Prisma.TransactionClient) => Promise<T>, | ||
isolationLevel: IsolationLevel, | ||
maxRetries = 3 | ||
): Promise<T | null> => { | ||
let retries = 0; | ||
let result: T | null = null; | ||
|
||
while (retries < maxRetries) { | ||
try { | ||
result = await this.client.$transaction(callback, { | ||
isolationLevel | ||
}); | ||
break; | ||
} catch (error: unknown) { | ||
if ( | ||
isErrorWithCode(error) && | ||
error.code === PrismaErrorCode.TRANSACTION_CONFLICT | ||
) { | ||
retries++; | ||
continue; | ||
} | ||
|
||
throw new RepositoryError(getErrorMessage(error)); | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
} |
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,5 @@ | ||
export class RepositoryError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
api/src/infrastructure/repositories/postgresql/profile.repository.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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Prisma, PrismaClient } from '@prisma/client'; | ||
|
||
import { Profile } from '@src/core/entities/profile.entity'; | ||
import { RepositoryError } from '@src/infrastructure/repositories/errors'; | ||
import { ProfileRepository } from '@src/ports/profile.repository'; | ||
import { getErrorMessage } from '@src/util/error'; | ||
|
||
export class PostgresqlProfileRepository implements ProfileRepository { | ||
constructor(private readonly client: PrismaClient) {} | ||
|
||
public create = async ( | ||
name: string, | ||
transactionClient?: Prisma.TransactionClient | ||
): Promise<Profile> => { | ||
try { | ||
const client = transactionClient ?? this.client; | ||
return client.profile.create({ data: { name } }); | ||
} catch (error: unknown) { | ||
throw new RepositoryError(getErrorMessage(error)); | ||
} | ||
}; | ||
|
||
public findById = async ( | ||
id: string, | ||
transactionClient?: Prisma.TransactionClient | ||
): Promise<Profile> => { | ||
try { | ||
const client = transactionClient ?? this.client; | ||
const profile = client.profile.findFirstOrThrow({ | ||
where: { id } | ||
}); | ||
return profile; | ||
} catch (error: unknown) { | ||
throw new RepositoryError(getErrorMessage(error)); | ||
} | ||
}; | ||
|
||
public updateById = async ( | ||
id: string, | ||
name: string, | ||
transactionClient?: Prisma.TransactionClient | ||
): Promise<Profile> => { | ||
try { | ||
const client = transactionClient ?? this.client; | ||
return client.profile.update({ | ||
where: { id }, | ||
data: { name } | ||
}); | ||
} catch (error: unknown) { | ||
throw new RepositoryError(getErrorMessage(error)); | ||
} | ||
}; | ||
} |
Oops, something went wrong.