mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	chore: rename parametric types to make them clearer (#6500)
## About the changes Just renaming to bring more clarity based on https://github.com/Unleash/unleash/pull/6436#discussion_r1517334796
This commit is contained in:
		
							parent
							
								
									160eec18a0
								
							
						
					
					
						commit
						977b0e4e28
					
				| @ -9,11 +9,13 @@ import { Row } from './row-type'; | ||||
| export type CrudStoreConfig = Pick<IUnleashConfig, 'eventBus'>; | ||||
| 
 | ||||
| /** | ||||
|  * This abstract class defines the basic operations for a CRUD store | ||||
|  * This abstract class defines the basic operations for a CRUD store. | ||||
|  * | ||||
|  * It accepts one model as input and one model as output that generally includes auto-generated properties such as the id or createdAt. | ||||
|  * | ||||
|  * Provides default types for: | ||||
|  * - RowReadModel turning the properties of ReadModel from camelCase to snake_case | ||||
|  * - RowWriteModel turning the properties of WriteModel from camelCase to snake_case | ||||
|  * - OutputRowModel turning the properties of OutputModel from camelCase to snake_case | ||||
|  * - InputRowModel turning the properties of InputModel from camelCase to snake_case | ||||
|  * - IdType assumming it's a number | ||||
|  * | ||||
|  * These types can be overridden to suit different needs. | ||||
| @ -21,12 +23,12 @@ export type CrudStoreConfig = Pick<IUnleashConfig, 'eventBus'>; | ||||
|  * Default implementations of toRow and fromRow are provided, but can be overridden. | ||||
|  */ | ||||
| export abstract class CRUDStore< | ||||
|     ReadModel extends { id: IdType }, | ||||
|     WriteModel, | ||||
|     RowReadModel = Row<ReadModel>, | ||||
|     RowWriteModel = Row<WriteModel>, | ||||
|     OutputModel extends { id: IdType }, | ||||
|     InputModel, | ||||
|     OutputRowModel = Row<OutputModel>, | ||||
|     InputRowModel = Row<InputModel>, | ||||
|     IdType = number, | ||||
| > implements Store<ReadModel, IdType> | ||||
| > implements Store<OutputModel, IdType> | ||||
| { | ||||
|     protected db: Db; | ||||
| 
 | ||||
| @ -34,16 +36,16 @@ export abstract class CRUDStore< | ||||
| 
 | ||||
|     protected readonly timer: (action: string) => Function; | ||||
| 
 | ||||
|     protected toRow: (item: Partial<WriteModel>) => Partial<RowWriteModel>; | ||||
|     protected fromRow: (item: Partial<RowReadModel>) => Partial<ReadModel>; | ||||
|     protected toRow: (item: Partial<InputModel>) => Partial<InputRowModel>; | ||||
|     protected fromRow: (item: Partial<OutputRowModel>) => Partial<OutputModel>; | ||||
| 
 | ||||
|     constructor( | ||||
|         tableName: string, | ||||
|         db: Db, | ||||
|         { eventBus }: CrudStoreConfig, | ||||
|         options?: Partial<{ | ||||
|             toRow: (item: Partial<WriteModel>) => Partial<RowWriteModel>; | ||||
|             fromRow: (item: RowReadModel) => Partial<ReadModel>; | ||||
|             toRow: (item: Partial<InputModel>) => Partial<InputRowModel>; | ||||
|             fromRow: (item: OutputRowModel) => Partial<OutputModel>; | ||||
|         }>, | ||||
|     ) { | ||||
|         this.tableName = tableName; | ||||
| @ -53,43 +55,43 @@ export abstract class CRUDStore< | ||||
|                 store: tableName, | ||||
|                 action, | ||||
|             }); | ||||
|         this.toRow = options?.toRow ?? defaultToRow<WriteModel, RowWriteModel>; | ||||
|         this.toRow = options?.toRow ?? defaultToRow<InputModel, InputRowModel>; | ||||
|         this.fromRow = | ||||
|             options?.fromRow ?? defaultFromRow<ReadModel, RowReadModel>; | ||||
|             options?.fromRow ?? defaultFromRow<OutputModel, OutputRowModel>; | ||||
|     } | ||||
| 
 | ||||
|     async getAll(query?: Partial<WriteModel>): Promise<ReadModel[]> { | ||||
|     async getAll(query?: Partial<InputModel>): Promise<OutputModel[]> { | ||||
|         let allQuery = this.db(this.tableName); | ||||
|         if (query) { | ||||
|             allQuery = allQuery.where(this.toRow(query) as Record<string, any>); | ||||
|         } | ||||
|         const items = await allQuery; | ||||
|         return items.map(this.fromRow) as ReadModel[]; | ||||
|         return items.map(this.fromRow) as OutputModel[]; | ||||
|     } | ||||
| 
 | ||||
|     async insert(item: WriteModel): Promise<ReadModel> { | ||||
|     async insert(item: InputModel): Promise<OutputModel> { | ||||
|         const rows = await this.db(this.tableName) | ||||
|             .insert(this.toRow(item)) | ||||
|             .returning('*'); | ||||
|         return this.fromRow(rows[0]) as ReadModel; | ||||
|         return this.fromRow(rows[0]) as OutputModel; | ||||
|     } | ||||
| 
 | ||||
|     async bulkInsert(items: WriteModel[]): Promise<ReadModel[]> { | ||||
|     async bulkInsert(items: InputModel[]): Promise<OutputModel[]> { | ||||
|         if (!items || items.length === 0) { | ||||
|             return []; | ||||
|         } | ||||
|         const rows = await this.db(this.tableName) | ||||
|             .insert(items.map(this.toRow)) | ||||
|             .returning('*'); | ||||
|         return rows.map(this.fromRow) as ReadModel[]; | ||||
|         return rows.map(this.fromRow) as OutputModel[]; | ||||
|     } | ||||
| 
 | ||||
|     async update(id: IdType, item: Partial<WriteModel>): Promise<ReadModel> { | ||||
|     async update(id: IdType, item: Partial<InputModel>): Promise<OutputModel> { | ||||
|         const rows = await this.db(this.tableName) | ||||
|             .where({ id }) | ||||
|             .update(this.toRow(item)) | ||||
|             .returning('*'); | ||||
|         return this.fromRow(rows[0]) as ReadModel; | ||||
|         return this.fromRow(rows[0]) as OutputModel; | ||||
|     } | ||||
| 
 | ||||
|     async delete(id: IdType): Promise<void> { | ||||
| @ -111,7 +113,7 @@ export abstract class CRUDStore< | ||||
|         return present; | ||||
|     } | ||||
| 
 | ||||
|     async count(query?: Partial<WriteModel>): Promise<number> { | ||||
|     async count(query?: Partial<InputModel>): Promise<number> { | ||||
|         let countQuery = this.db(this.tableName).count('*'); | ||||
|         if (query) { | ||||
|             countQuery = countQuery.where( | ||||
| @ -122,11 +124,11 @@ export abstract class CRUDStore< | ||||
|         return Number(count); | ||||
|     } | ||||
| 
 | ||||
|     async get(id: IdType): Promise<ReadModel> { | ||||
|     async get(id: IdType): Promise<OutputModel> { | ||||
|         const row = await this.db(this.tableName).where({ id }).first(); | ||||
|         if (!row) { | ||||
|             throw new NotFoundError(`No item with id ${id}`); | ||||
|         } | ||||
|         return this.fromRow(row) as ReadModel; | ||||
|         return this.fromRow(row) as OutputModel; | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user