File tree 13 files changed +93
-31
lines changed
migrations/20241102024042_account_username
src/example/custom/domain
13 files changed +93
-31
lines changed Original file line number Diff line number Diff line change @@ -5,11 +5,12 @@ export class PrismaDeleteProcess
5
5
extends PrismaProcess
6
6
implements DeleteProcess
7
7
{
8
- public identity ;
8
+ public identityData ;
9
+ public identityKey : string = 'id' ;
9
10
10
11
async process ( ) {
11
12
this . result = await this . getDelegate ( ) . delete ( {
12
- where : { id : this . identity } ,
13
+ where : { [ this . identityKey ] : this . identityData } ,
13
14
} ) ;
14
15
}
15
16
}
Original file line number Diff line number Diff line change @@ -2,11 +2,12 @@ import { ReadProcess } from '@aditama-labs/nest-autocrud/skeleton';
2
2
import { PrismaProcess } from './prisma.process' ;
3
3
4
4
export class PrismaReadProcess extends PrismaProcess implements ReadProcess {
5
- public identity ;
5
+ public identityData ;
6
+ public identityKey : string = 'id' ;
6
7
7
8
async process ( ) {
8
9
this . result = await this . getDelegate ( ) . findUnique ( {
9
- where : { id : this . identity } ,
10
+ where : { [ this . identityKey ] : this . identityData } ,
10
11
} ) ;
11
12
}
12
13
}
Original file line number Diff line number Diff line change @@ -5,13 +5,14 @@ export class PrismaUpdateProcess
5
5
extends PrismaProcess
6
6
implements UpdateProcess
7
7
{
8
- public identity ;
8
+ public identityData ;
9
+ public identityKey : string = 'id' ;
9
10
public payload ;
10
11
11
12
async process ( ) {
12
13
this . result = await this . getDelegate ( ) . update ( {
13
14
data : this . payload ,
14
- where : { id : this . identity } ,
15
+ where : { [ this . identityKey ] : this . identityData } ,
15
16
} ) ;
16
17
}
17
18
}
Original file line number Diff line number Diff line change @@ -3,14 +3,23 @@ import { DefaultExecutor } from './default.executor';
3
3
4
4
// @TODO : ReadExecutor and DeleteExecutor can be merged into one executor
5
5
export class DeleteExecutor extends DefaultExecutor {
6
- constructor ( process : DeleteProcess , id ) {
6
+ constructor (
7
+ process : DeleteProcess ,
8
+ identityData ,
9
+ identityKey : string = 'id' ,
10
+ ) {
7
11
super ( process ) ;
8
12
// Set the id of the data
9
- process . identity = id ;
13
+ process . identityData = identityData ;
14
+ process . identityKey = identityKey ;
10
15
}
11
16
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 ) ;
14
23
await executor . execute ( ) ;
15
24
return executor . getOutput ( ) ;
16
25
}
Original file line number Diff line number Diff line change @@ -2,14 +2,19 @@ import { ReadProcess } from '../processes';
2
2
import { DefaultExecutor } from './default.executor' ;
3
3
4
4
export class ReadExecutor extends DefaultExecutor {
5
- constructor ( process : ReadProcess , id ) {
5
+ constructor ( process : ReadProcess , identityData , identityKey : string = 'id' ) {
6
6
super ( process ) ;
7
7
// Set the id of the data
8
- process . identity = id ;
8
+ process . identityData = identityData ;
9
+ process . identityKey = identityKey ;
9
10
}
10
11
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 ) ;
13
18
await executor . execute ( ) ;
14
19
return executor . getOutput ( ) ;
15
20
}
Original file line number Diff line number Diff line change @@ -3,15 +3,31 @@ import { DefaultExecutor } from './default.executor';
3
3
4
4
// @TODO : This executor should be able to extend from ReadExecutor and CreateExecutor
5
5
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
+ ) {
7
12
super ( process ) ;
8
13
// Set the id and data to process
9
- process . identity = id ;
14
+ process . identityData = identityData ;
10
15
process . payload = data ;
16
+ process . identityKey = identityKey ;
11
17
}
12
18
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
+ ) ;
15
31
await executor . execute ( ) ;
16
32
return executor . getOutput ( ) ;
17
33
}
Original file line number Diff line number Diff line change @@ -2,5 +2,6 @@ import { DefaultProcess } from './default.process';
2
2
3
3
// @TODO : DeleteProcess and ReadProcess are the same, should be refactored next
4
4
export class DeleteProcess extends DefaultProcess {
5
- public identity ;
5
+ public identityData ;
6
+ public identityKey : string = 'id' ;
6
7
}
Original file line number Diff line number Diff line change 1
1
import { DefaultProcess } from './default.process' ;
2
2
3
3
export class ReadProcess extends DefaultProcess {
4
- public identity ;
4
+ public identityData ;
5
+ public identityKey : string = 'id' ;
5
6
}
Original file line number Diff line number Diff line change @@ -2,7 +2,8 @@ import { DefaultProcess } from './default.process';
2
2
3
3
export class UpdateProcess extends DefaultProcess {
4
4
// @TODO : The property of id can be take from ReadProcess which is extended
5
- public identity ;
5
+ public identityData ;
6
+ public identityKey : string = 'id' ;
6
7
// @TODO : The property of data can be take from CreateProcess which is extended
7
8
public payload ;
8
9
}
Original file line number Diff line number Diff line change @@ -95,8 +95,12 @@ export const CustomCRUDController = (options?: ControllerOption) => {
95
95
}
96
96
97
97
@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
+ ) ;
100
104
}
101
105
102
106
@Get ( 'list' )
@@ -111,13 +115,22 @@ export const CustomCRUDController = (options?: ControllerOption) => {
111
115
}
112
116
113
117
@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
+ ) ;
116
124
}
117
125
118
126
@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
+ ) ;
121
134
}
122
135
}
123
136
Original file line number Diff line number Diff line change
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" );
Original file line number Diff line number Diff line change @@ -16,9 +16,10 @@ datasource db {
16
16
}
17
17
18
18
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 []
22
23
23
24
@@map (" account " )
24
25
}
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ export class CustomReadProcess extends PrismaReadProcess {
4
4
customResult ;
5
5
6
6
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 ) ;
8
8
}
9
9
async after ( ) : Promise < any > {
10
10
this . customResult = {
You can’t perform that action at this time.
0 commit comments