From 977b0e4e2898f6e182603267f23a87c203421aed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Mon, 11 Mar 2024 15:41:34 +0100 Subject: [PATCH] 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 --- src/lib/db/crud/crud-store.ts | 52 ++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/src/lib/db/crud/crud-store.ts b/src/lib/db/crud/crud-store.ts index 5653a69fd7..a1f6f36c3f 100644 --- a/src/lib/db/crud/crud-store.ts +++ b/src/lib/db/crud/crud-store.ts @@ -9,11 +9,13 @@ import { Row } from './row-type'; export type CrudStoreConfig = Pick; /** - * 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; * Default implementations of toRow and fromRow are provided, but can be overridden. */ export abstract class CRUDStore< - ReadModel extends { id: IdType }, - WriteModel, - RowReadModel = Row, - RowWriteModel = Row, + OutputModel extends { id: IdType }, + InputModel, + OutputRowModel = Row, + InputRowModel = Row, IdType = number, -> implements Store +> implements Store { protected db: Db; @@ -34,16 +36,16 @@ export abstract class CRUDStore< protected readonly timer: (action: string) => Function; - protected toRow: (item: Partial) => Partial; - protected fromRow: (item: Partial) => Partial; + protected toRow: (item: Partial) => Partial; + protected fromRow: (item: Partial) => Partial; constructor( tableName: string, db: Db, { eventBus }: CrudStoreConfig, options?: Partial<{ - toRow: (item: Partial) => Partial; - fromRow: (item: RowReadModel) => Partial; + toRow: (item: Partial) => Partial; + fromRow: (item: OutputRowModel) => Partial; }>, ) { this.tableName = tableName; @@ -53,43 +55,43 @@ export abstract class CRUDStore< store: tableName, action, }); - this.toRow = options?.toRow ?? defaultToRow; + this.toRow = options?.toRow ?? defaultToRow; this.fromRow = - options?.fromRow ?? defaultFromRow; + options?.fromRow ?? defaultFromRow; } - async getAll(query?: Partial): Promise { + async getAll(query?: Partial): Promise { let allQuery = this.db(this.tableName); if (query) { allQuery = allQuery.where(this.toRow(query) as Record); } const items = await allQuery; - return items.map(this.fromRow) as ReadModel[]; + return items.map(this.fromRow) as OutputModel[]; } - async insert(item: WriteModel): Promise { + async insert(item: InputModel): Promise { 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 { + async bulkInsert(items: InputModel[]): Promise { 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): Promise { + async update(id: IdType, item: Partial): Promise { 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 { @@ -111,7 +113,7 @@ export abstract class CRUDStore< return present; } - async count(query?: Partial): Promise { + async count(query?: Partial): Promise { 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 { + async get(id: IdType): Promise { 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; } }