-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add execution context with switching functionality
- Loading branch information
1 parent
1158aa1
commit f2f0490
Showing
21 changed files
with
433 additions
and
235 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './knexion-execution-context-host'; |
95 changes: 95 additions & 0 deletions
95
packages/core/lib/helpers/knexion-execution-context-host.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,95 @@ | ||
import { Type } from '@nestjs/common/interfaces'; | ||
import { | ||
KnexArgumentsHost, | ||
KnexionContextType, | ||
KnexionExecutionContext, | ||
KnexionMethodType, | ||
} from '../interfaces'; | ||
import { DefaultRepositoryOptions } from '../repository'; | ||
|
||
export class KnexionExecutionContextHost< | ||
TRecord, | ||
TResult, | ||
IdType = DefaultRepositoryOptions['idType'], | ||
> implements KnexionExecutionContext<TRecord, TResult, IdType> | ||
{ | ||
private contextType = 'knex'; | ||
private methodType = null; | ||
|
||
constructor( | ||
private readonly args: any[], | ||
private readonly constructorRef: Type = null, | ||
private readonly handler: Function = null, | ||
) {} | ||
|
||
setType<TContext extends string = KnexionContextType>(type: TContext): void { | ||
type && (this.contextType = type); | ||
} | ||
|
||
getType<TContext extends string = KnexionContextType>(): TContext { | ||
return this.contextType as TContext; | ||
} | ||
|
||
setMethod<TMethod extends string = KnexionMethodType>(type: TMethod): void { | ||
type && (this.methodType = type); | ||
} | ||
|
||
getMethod<TMethod extends string = KnexionMethodType>(): TMethod { | ||
return this.methodType as TMethod; | ||
} | ||
|
||
getClass<T = any>(): Type<T> { | ||
return this.constructorRef; | ||
} | ||
|
||
getHandler(): Function { | ||
return this.handler; | ||
} | ||
|
||
getArgs<T extends Array<any> = any[]>(): T { | ||
return this.args as T; | ||
} | ||
|
||
getArgByIndex<T = any>(index: number): T { | ||
return this.args[index] as T; | ||
} | ||
|
||
switchToKnex(): KnexArgumentsHost<TRecord, TResult, IdType> { | ||
return Object.assign(this, { | ||
getQueryBuilder: () => this.getArgByIndex(0), | ||
getRawBuilder: () => this.getArgByIndex(1), | ||
createQueryBuilder: () => this.getArgByIndex(2)(), | ||
getOptions: () => this.getArgByIndex(3), | ||
switchToList: () => | ||
Object.assign(this, { | ||
getQueryBuilder: () => this.getArgByIndex(0), | ||
getOptions: () => this.getArgByIndex(3), | ||
}), | ||
switchToCreate: () => | ||
Object.assign(this, { | ||
getQueryBuilder: () => this.getArgByIndex(0), | ||
getOptions: () => this.getArgByIndex(3), | ||
getPayload: () => this.getArgByIndex(4), | ||
}), | ||
switchToRetrieve: () => | ||
Object.assign(this, { | ||
getQueryBuilder: () => this.getArgByIndex(0), | ||
getOptions: () => this.getArgByIndex(3), | ||
getId: () => this.getArgByIndex(4), | ||
}), | ||
switchToUpdate: () => | ||
Object.assign(this, { | ||
getQueryBuilder: () => this.getArgByIndex(0), | ||
getOptions: () => this.getArgByIndex(3), | ||
getId: () => this.getArgByIndex(4), | ||
getPayload: () => this.getArgByIndex(5), | ||
}), | ||
switchToDelete: () => | ||
Object.assign(this, { | ||
getQueryBuilder: () => this.getArgByIndex(0), | ||
getOptions: () => this.getArgByIndex(3), | ||
getId: () => this.getArgByIndex(4), | ||
}), | ||
}); | ||
} | ||
} |
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,9 +1,9 @@ | ||
export * from './decorators'; | ||
export * from './helpers'; | ||
export * from './indicators'; | ||
export * from './interfaces'; | ||
export * from './services'; | ||
export * from './utils'; | ||
export * from './interceptors-consumer'; | ||
export * from './knexion.module'; | ||
export * from './knexion-context'; | ||
export * from './repository'; |
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
118 changes: 118 additions & 0 deletions
118
packages/core/lib/interfaces/knexion-arguments-host.interface.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,118 @@ | ||
import { | ||
DatabaseOptions, | ||
SelectDatabaseOptions, | ||
} from './database-options.interface'; | ||
import { Knex } from 'knex'; | ||
|
||
export type KnexionContextType = 'knex'; | ||
|
||
export type KnexionMethodType = | ||
| 'list' | ||
| 'create' | ||
| 'retrieve' | ||
| 'update' | ||
| 'delete'; | ||
|
||
export interface KnexMethodArgumentsHost<TRecord, TResult> { | ||
getQueryBuilder(): Knex.QueryBuilder<TRecord, TResult>; | ||
getOptions(): unknown; | ||
} | ||
|
||
export interface ListKnexMethodArgumentsHost<TRecord, TResult> | ||
extends KnexMethodArgumentsHost<TRecord, TResult> { | ||
getOptions< | ||
Options extends SelectDatabaseOptions< | ||
TRecord, | ||
TResult | ||
> = SelectDatabaseOptions<TRecord, TResult>, | ||
>(): Options; | ||
} | ||
|
||
export interface CreateKnexMethodArgumentsHost<TRecord, TResult> | ||
extends KnexMethodArgumentsHost<TRecord, TResult> { | ||
getPayload<T = any>(): T; | ||
getOptions< | ||
Options extends DatabaseOptions<TRecord, TResult> = DatabaseOptions< | ||
TRecord, | ||
TResult | ||
>, | ||
>(): Options; | ||
} | ||
|
||
export interface RetrieveKnexMethodArgumentsHost<TRecord, TResult, IdType> | ||
extends KnexMethodArgumentsHost<TRecord, TResult> { | ||
getId(): IdType; | ||
getOptions< | ||
Options extends SelectDatabaseOptions< | ||
TRecord, | ||
TResult | ||
> = SelectDatabaseOptions<TRecord, TResult>, | ||
>(): Options; | ||
} | ||
|
||
export interface UpdateKnexMethodArgumentsHost<TRecord, TResult, IdType> | ||
extends KnexMethodArgumentsHost<TRecord, TResult> { | ||
getId(): IdType; | ||
getPayload<T = any>(): T; | ||
getOptions< | ||
Options extends DatabaseOptions<TRecord, TResult> = DatabaseOptions< | ||
TRecord, | ||
TResult | ||
>, | ||
>(): Options; | ||
} | ||
|
||
export interface DeleteKnexMethodArgumentsHost<TRecord, TResult, IdType> | ||
extends KnexMethodArgumentsHost<TRecord, TResult> { | ||
getId(): IdType; | ||
getOptions< | ||
Options extends DatabaseOptions<TRecord, TResult> = DatabaseOptions< | ||
TRecord, | ||
TResult | ||
>, | ||
>(): Options; | ||
} | ||
|
||
export interface KnexArgumentsHost<TRecord, TResult, IdType> { | ||
getQueryBuilder(): Knex.QueryBuilder<TRecord, TResult>; | ||
getRawBuilder(): Knex.RawBuilder<TRecord, TResult>; | ||
createQueryBuilder(): Knex.QueryBuilder<TRecord, TResult>; | ||
getOptions< | ||
Options extends DatabaseOptions<TRecord, TResult> = DatabaseOptions< | ||
TRecord, | ||
TResult | ||
>, | ||
>(): Options; | ||
switchToList(): ListKnexMethodArgumentsHost<TRecord, TResult>; | ||
switchToCreate(): CreateKnexMethodArgumentsHost<TRecord, TResult>; | ||
switchToRetrieve(): RetrieveKnexMethodArgumentsHost<TRecord, TResult, IdType>; | ||
switchToUpdate(): UpdateKnexMethodArgumentsHost<TRecord, TResult, IdType>; | ||
switchToDelete(): DeleteKnexMethodArgumentsHost<TRecord, TResult, IdType>; | ||
} | ||
|
||
/** | ||
* Provides methods for retrieving the arguments being passed to a handler. | ||
*/ | ||
export interface KnexionArgumentsHost<TRecord, TResult, IdType> { | ||
/** | ||
* Returns the array of arguments being passed to the handler. | ||
*/ | ||
getArgs<T extends Array<any> = any[]>(): T; | ||
/** | ||
* Returns a particular argument by index. | ||
* @param index index of argument to retrieve | ||
*/ | ||
getArgByIndex<T = any>(index: number): T; | ||
|
||
/** | ||
* Returns the current execution context type (string) | ||
*/ | ||
getType<TContext extends string = KnexionContextType>(): TContext; | ||
|
||
/** | ||
* Returns the current execution method type (string) | ||
*/ | ||
getMethod<TMethod extends string = KnexionMethodType>(): TMethod; | ||
|
||
switchToKnex(): KnexArgumentsHost<TRecord, TResult, IdType>; | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/core/lib/interfaces/knexion-execution-contex.interface.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,19 @@ | ||
import { Type } from '@nestjs/common'; | ||
import { KnexionArgumentsHost } from './knexion-arguments-host.interface'; | ||
import { DefaultRepositoryOptions } from '../repository'; | ||
|
||
export interface KnexionExecutionContext< | ||
TRecord, | ||
TResult = unknown, | ||
IdType = DefaultRepositoryOptions['idType'], | ||
> extends KnexionArgumentsHost<TRecord, TResult, IdType> { | ||
/** | ||
* Returns the *type* of the controller class which the current handler belongs to. | ||
*/ | ||
getClass<T = any>(): Type<T>; | ||
/** | ||
* Returns a reference to the handler (method) that will be invoked next in the | ||
* request pipeline. | ||
*/ | ||
getHandler(): Function; | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.