1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-11 00:08:30 +01:00

fix: mapper function should be partial (#6475)

Small fix to make mapper function partial as it should be
This commit is contained in:
Gastón Fournier 2024-03-08 11:20:41 +01:00 committed by GitHub
parent 82f4093c04
commit 1949d0134f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 16 deletions

View File

@ -34,16 +34,16 @@ export abstract class CRUDStore<
protected readonly timer: (action: string) => Function; protected readonly timer: (action: string) => Function;
protected toRow: (item: Partial<WriteModel>) => RowWriteModel; protected toRow: (item: Partial<WriteModel>) => Partial<RowWriteModel>;
protected fromRow: (item: RowReadModel) => ReadModel; protected fromRow: (item: Partial<RowReadModel>) => Partial<ReadModel>;
constructor( constructor(
tableName: string, tableName: string,
db: Db, db: Db,
{ eventBus }: CrudStoreConfig, { eventBus }: CrudStoreConfig,
options?: Partial<{ options?: Partial<{
toRow: (item: WriteModel) => RowWriteModel; toRow: (item: Partial<WriteModel>) => Partial<RowWriteModel>;
fromRow: (item: RowReadModel) => ReadModel; fromRow: (item: RowReadModel) => Partial<ReadModel>;
}>, }>,
) { ) {
this.tableName = tableName; this.tableName = tableName;
@ -64,14 +64,14 @@ export abstract class CRUDStore<
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); return items.map(this.fromRow) as ReadModel[];
} }
async insert(item: WriteModel): Promise<ReadModel> { async insert(item: WriteModel): Promise<ReadModel> {
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]); return this.fromRow(rows[0]) as ReadModel;
} }
async bulkInsert(items: WriteModel[]): Promise<ReadModel[]> { async bulkInsert(items: WriteModel[]): Promise<ReadModel[]> {
@ -81,7 +81,7 @@ export abstract class CRUDStore<
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); return rows.map(this.fromRow) as ReadModel[];
} }
async update(id: IdType, item: Partial<WriteModel>): Promise<ReadModel> { async update(id: IdType, item: Partial<WriteModel>): Promise<ReadModel> {
@ -89,7 +89,7 @@ export abstract class CRUDStore<
.where({ id }) .where({ id })
.update(this.toRow(item)) .update(this.toRow(item))
.returning('*'); .returning('*');
return this.fromRow(rows[0]); return this.fromRow(rows[0]) as ReadModel;
} }
async delete(id: IdType): Promise<void> { async delete(id: IdType): Promise<void> {
@ -127,6 +127,6 @@ export abstract class CRUDStore<
if (!row) { if (!row) {
throw new NotFoundError(`No item with id ${id}`); throw new NotFoundError(`No item with id ${id}`);
} }
return this.fromRow(row); return this.fromRow(row) as ReadModel;
} }
} }

View File

@ -11,13 +11,13 @@ const snakeToCamelCase = (str: string) =>
* @returns a modified version of item with all fields in snake_case * @returns a modified version of item with all fields in snake_case
*/ */
export const defaultToRow = <WriteModel, WriteRow>( export const defaultToRow = <WriteModel, WriteRow>(
item: WriteModel, item: Partial<WriteModel>,
): WriteRow => { ): Partial<WriteRow> => {
const row: Partial<WriteRow> = {}; const row = {};
Object.entries(item as Record<string, any>).forEach(([key, value]) => { Object.entries(item as Record<string, any>).forEach(([key, value]) => {
row[camelToSnakeCase(key)] = value; row[camelToSnakeCase(key)] = value;
}); });
return row as WriteRow; return row;
}; };
/** /**
@ -25,10 +25,12 @@ export const defaultToRow = <WriteModel, WriteRow>(
* @param row is the input object * @param row is the input object
* @returns a modified version of row with all fields in camelCase * @returns a modified version of row with all fields in camelCase
*/ */
export const defaultFromRow = <ReadModel, ReadRow>(row: ReadRow): ReadModel => { export const defaultFromRow = <ReadModel, ReadRow>(
const model: Partial<ReadModel> = {}; row: Partial<ReadRow>,
): Partial<ReadModel> => {
const model = {};
Object.entries(row as Record<string, any>).forEach(([key, value]) => { Object.entries(row as Record<string, any>).forEach(([key, value]) => {
model[snakeToCamelCase(key)] = value; model[snakeToCamelCase(key)] = value;
}); });
return model as ReadModel; return model;
}; };