1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-06-18 01:18:23 +02: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:
Gastón Fournier 2024-03-11 15:41:34 +01:00 committed by GitHub
parent 160eec18a0
commit 977b0e4e28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,11 +9,13 @@ import { Row } from './row-type';
export type CrudStoreConfig = Pick<IUnleashConfig, 'eventBus'>; 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: * Provides default types for:
* - RowReadModel turning the properties of ReadModel from camelCase to snake_case * - OutputRowModel turning the properties of OutputModel from camelCase to snake_case
* - RowWriteModel turning the properties of WriteModel from camelCase to snake_case * - InputRowModel turning the properties of InputModel from camelCase to snake_case
* - IdType assumming it's a number * - IdType assumming it's a number
* *
* These types can be overridden to suit different needs. * 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. * Default implementations of toRow and fromRow are provided, but can be overridden.
*/ */
export abstract class CRUDStore< export abstract class CRUDStore<
ReadModel extends { id: IdType }, OutputModel extends { id: IdType },
WriteModel, InputModel,
RowReadModel = Row<ReadModel>, OutputRowModel = Row<OutputModel>,
RowWriteModel = Row<WriteModel>, InputRowModel = Row<InputModel>,
IdType = number, IdType = number,
> implements Store<ReadModel, IdType> > implements Store<OutputModel, IdType>
{ {
protected db: Db; protected db: Db;
@ -34,16 +36,16 @@ export abstract class CRUDStore<
protected readonly timer: (action: string) => Function; protected readonly timer: (action: string) => Function;
protected toRow: (item: Partial<WriteModel>) => Partial<RowWriteModel>; protected toRow: (item: Partial<InputModel>) => Partial<InputRowModel>;
protected fromRow: (item: Partial<RowReadModel>) => Partial<ReadModel>; protected fromRow: (item: Partial<OutputRowModel>) => Partial<OutputModel>;
constructor( constructor(
tableName: string, tableName: string,
db: Db, db: Db,
{ eventBus }: CrudStoreConfig, { eventBus }: CrudStoreConfig,
options?: Partial<{ options?: Partial<{
toRow: (item: Partial<WriteModel>) => Partial<RowWriteModel>; toRow: (item: Partial<InputModel>) => Partial<InputRowModel>;
fromRow: (item: RowReadModel) => Partial<ReadModel>; fromRow: (item: OutputRowModel) => Partial<OutputModel>;
}>, }>,
) { ) {
this.tableName = tableName; this.tableName = tableName;
@ -53,43 +55,43 @@ export abstract class CRUDStore<
store: tableName, store: tableName,
action, action,
}); });
this.toRow = options?.toRow ?? defaultToRow<WriteModel, RowWriteModel>; this.toRow = options?.toRow ?? defaultToRow<InputModel, InputRowModel>;
this.fromRow = 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); let allQuery = this.db(this.tableName);
if (query) { if (query) {
allQuery = allQuery.where(this.toRow(query) as Record<string, any>); allQuery = allQuery.where(this.toRow(query) as Record<string, any>);
} }
const items = await allQuery; 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) const rows = await this.db(this.tableName)
.insert(this.toRow(item)) .insert(this.toRow(item))
.returning('*'); .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) { if (!items || items.length === 0) {
return []; return [];
} }
const rows = await this.db(this.tableName) const rows = await this.db(this.tableName)
.insert(items.map(this.toRow)) .insert(items.map(this.toRow))
.returning('*'); .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) const rows = await this.db(this.tableName)
.where({ id }) .where({ id })
.update(this.toRow(item)) .update(this.toRow(item))
.returning('*'); .returning('*');
return this.fromRow(rows[0]) as ReadModel; return this.fromRow(rows[0]) as OutputModel;
} }
async delete(id: IdType): Promise<void> { async delete(id: IdType): Promise<void> {
@ -111,7 +113,7 @@ export abstract class CRUDStore<
return present; return present;
} }
async count(query?: Partial<WriteModel>): Promise<number> { async count(query?: Partial<InputModel>): Promise<number> {
let countQuery = this.db(this.tableName).count('*'); let countQuery = this.db(this.tableName).count('*');
if (query) { if (query) {
countQuery = countQuery.where( countQuery = countQuery.where(
@ -122,11 +124,11 @@ export abstract class CRUDStore<
return Number(count); 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(); const row = await this.db(this.tableName).where({ id }).first();
if (!row) { if (!row) {
throw new NotFoundError(`No item with id ${id}`); throw new NotFoundError(`No item with id ${id}`);
} }
return this.fromRow(row) as ReadModel; return this.fromRow(row) as OutputModel;
} }
} }