Skip to content

Commit b0088e4

Browse files
committed
feat: implement unique identifier when searching to database
1 parent 606221d commit b0088e4

File tree

13 files changed

+93
-31
lines changed

13 files changed

+93
-31
lines changed

libs/prisma/src/processes/delete.process.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ export class PrismaDeleteProcess
55
extends PrismaProcess
66
implements DeleteProcess
77
{
8-
public identity;
8+
public identityData;
9+
public identityKey: string = 'id';
910

1011
async process() {
1112
this.result = await this.getDelegate().delete({
12-
where: { id: this.identity },
13+
where: { [this.identityKey]: this.identityData },
1314
});
1415
}
1516
}

libs/prisma/src/processes/read.process.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { ReadProcess } from '@aditama-labs/nest-autocrud/skeleton';
22
import { PrismaProcess } from './prisma.process';
33

44
export class PrismaReadProcess extends PrismaProcess implements ReadProcess {
5-
public identity;
5+
public identityData;
6+
public identityKey: string = 'id';
67

78
async process() {
89
this.result = await this.getDelegate().findUnique({
9-
where: { id: this.identity },
10+
where: { [this.identityKey]: this.identityData },
1011
});
1112
}
1213
}

libs/prisma/src/processes/update.process.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ export class PrismaUpdateProcess
55
extends PrismaProcess
66
implements UpdateProcess
77
{
8-
public identity;
8+
public identityData;
9+
public identityKey: string = 'id';
910
public payload;
1011

1112
async process() {
1213
this.result = await this.getDelegate().update({
1314
data: this.payload,
14-
where: { id: this.identity },
15+
where: { [this.identityKey]: this.identityData },
1516
});
1617
}
1718
}

libs/skeleton/src/executors/delete.executor.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@ import { DefaultExecutor } from './default.executor';
33

44
// @TODO: ReadExecutor and DeleteExecutor can be merged into one executor
55
export class DeleteExecutor extends DefaultExecutor {
6-
constructor(process: DeleteProcess, id) {
6+
constructor(
7+
process: DeleteProcess,
8+
identityData,
9+
identityKey: string = 'id',
10+
) {
711
super(process);
812
// Set the id of the data
9-
process.identity = id;
13+
process.identityData = identityData;
14+
process.identityKey = identityKey;
1015
}
1116

12-
static async bootstrap(process: DeleteProcess, id) {
13-
const executor = new DeleteExecutor(process, id);
17+
static async bootstrap(
18+
process: DeleteProcess,
19+
identityData,
20+
identityKey: string = 'id',
21+
) {
22+
const executor = new DeleteExecutor(process, identityData, identityKey);
1423
await executor.execute();
1524
return executor.getOutput();
1625
}

libs/skeleton/src/executors/read.executor.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@ import { ReadProcess } from '../processes';
22
import { DefaultExecutor } from './default.executor';
33

44
export class ReadExecutor extends DefaultExecutor {
5-
constructor(process: ReadProcess, id) {
5+
constructor(process: ReadProcess, identityData, identityKey: string = 'id') {
66
super(process);
77
// Set the id of the data
8-
process.identity = id;
8+
process.identityData = identityData;
9+
process.identityKey = identityKey;
910
}
1011

11-
static async bootstrap(process: ReadProcess, id) {
12-
const executor = new ReadExecutor(process, id);
12+
static async bootstrap(
13+
process: ReadProcess,
14+
identityData,
15+
identityKey: string = 'id',
16+
) {
17+
const executor = new ReadExecutor(process, identityData, identityKey);
1318
await executor.execute();
1419
return executor.getOutput();
1520
}

libs/skeleton/src/executors/update.executor.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,31 @@ import { DefaultExecutor } from './default.executor';
33

44
// @TODO: This executor should be able to extend from ReadExecutor and CreateExecutor
55
export class UpdateExecutor extends DefaultExecutor {
6-
constructor(process: UpdateProcess, id, data) {
6+
constructor(
7+
process: UpdateProcess,
8+
identityData,
9+
data,
10+
identityKey: string = 'id',
11+
) {
712
super(process);
813
// Set the id and data to process
9-
process.identity = id;
14+
process.identityData = identityData;
1015
process.payload = data;
16+
process.identityKey = identityKey;
1117
}
1218

13-
static async bootstrap(process: UpdateProcess, id, data) {
14-
const executor = new UpdateExecutor(process, id, data);
19+
static async bootstrap(
20+
process: UpdateProcess,
21+
identityData,
22+
data,
23+
identityKey: string = 'id',
24+
) {
25+
const executor = new UpdateExecutor(
26+
process,
27+
identityData,
28+
data,
29+
identityKey,
30+
);
1531
await executor.execute();
1632
return executor.getOutput();
1733
}

libs/skeleton/src/processes/delete.process.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ import { DefaultProcess } from './default.process';
22

33
// @TODO: DeleteProcess and ReadProcess are the same, should be refactored next
44
export class DeleteProcess extends DefaultProcess {
5-
public identity;
5+
public identityData;
6+
public identityKey: string = 'id';
67
}
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { DefaultProcess } from './default.process';
22

33
export class ReadProcess extends DefaultProcess {
4-
public identity;
4+
public identityData;
5+
public identityKey: string = 'id';
56
}

libs/skeleton/src/processes/update.process.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { DefaultProcess } from './default.process';
22

33
export class UpdateProcess extends DefaultProcess {
44
// @TODO: The property of id can be take from ReadProcess which is extended
5-
public identity;
5+
public identityData;
6+
public identityKey: string = 'id';
67
// @TODO: The property of data can be take from CreateProcess which is extended
78
public payload;
89
}

libs/skeleton/src/skeleton-crud.controller.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ export const CustomCRUDController = (options?: ControllerOption) => {
9595
}
9696

9797
@Delete(`:${uniqueIdentifier}`)
98-
async delete(@Param(uniqueIdentifier) id) {
99-
return await super.delete(id);
98+
async delete(@Param(uniqueIdentifier) identityData) {
99+
return await DeleteExecutor.bootstrap(
100+
this.deleteProcess,
101+
identityData,
102+
uniqueIdentifier,
103+
);
100104
}
101105

102106
@Get('list')
@@ -111,13 +115,22 @@ export const CustomCRUDController = (options?: ControllerOption) => {
111115
}
112116

113117
@Get(`:${uniqueIdentifier}`)
114-
async read(@Param(uniqueIdentifier) id) {
115-
return await super.read(id);
118+
async read(@Param(uniqueIdentifier) identityData) {
119+
return await ReadExecutor.bootstrap(
120+
this.readProcess,
121+
identityData,
122+
uniqueIdentifier,
123+
);
116124
}
117125

118126
@Patch(`:${uniqueIdentifier}`)
119-
async update(@Param(uniqueIdentifier) id, @Body() body) {
120-
return await super.update(id, body);
127+
async update(@Param(uniqueIdentifier) identityData, @Body() body) {
128+
return await UpdateExecutor.bootstrap(
129+
this.updateProcess,
130+
identityData,
131+
body,
132+
uniqueIdentifier,
133+
);
121134
}
122135
}
123136

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
Warnings:
3+
4+
- A unique constraint covering the columns `[username]` on the table `account` will be added. If there are existing duplicate values, this will fail.
5+
- Added the required column `username` to the `account` table without a default value. This is not possible if the table is not empty.
6+
7+
*/
8+
-- AlterTable
9+
ALTER TABLE "account" ADD COLUMN "username" TEXT NOT NULL;
10+
11+
-- CreateIndex
12+
CREATE UNIQUE INDEX "account_username_key" ON "account"("username");

prisma/schema.prisma

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ datasource db {
1616
}
1717

1818
model User {
19-
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
20-
name String
21-
Todo Todo[]
19+
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
20+
username String @unique
21+
name String
22+
Todo Todo[]
2223
2324
@@map("account")
2425
}

src/example/custom/domain/custom.read.process.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export class CustomReadProcess extends PrismaReadProcess {
44
customResult;
55

66
async before(): Promise<any> {
7-
console.log('The ID requested in path parameter', this.identity);
7+
console.log('The ID requested in path parameter', this.identityData);
88
}
99
async after(): Promise<any> {
1010
this.customResult = {

0 commit comments

Comments
 (0)