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 toRow: (item: Partial<WriteModel>) => RowWriteModel;
protected fromRow: (item: RowReadModel) => ReadModel;
protected toRow: (item: Partial<WriteModel>) => Partial<RowWriteModel>;
protected fromRow: (item: Partial<RowReadModel>) => Partial<ReadModel>;
constructor(
tableName: string,
db: Db,
{ eventBus }: CrudStoreConfig,
options?: Partial<{
toRow: (item: WriteModel) => RowWriteModel;
fromRow: (item: RowReadModel) => ReadModel;
toRow: (item: Partial<WriteModel>) => Partial<RowWriteModel>;
fromRow: (item: RowReadModel) => Partial<ReadModel>;
}>,
) {
this.tableName = tableName;
@ -64,14 +64,14 @@ export abstract class CRUDStore<
allQuery = allQuery.where(this.toRow(query) as Record<string, any>);
}
const items = await allQuery;
return items.map(this.fromRow);
return items.map(this.fromRow) as ReadModel[];
}
async insert(item: WriteModel): Promise<ReadModel> {
const rows = await this.db(this.tableName)
.insert(this.toRow(item))
.returning('*');
return this.fromRow(rows[0]);
return this.fromRow(rows[0]) as ReadModel;
}
async bulkInsert(items: WriteModel[]): Promise<ReadModel[]> {
@ -81,7 +81,7 @@ export abstract class CRUDStore<
const rows = await this.db(this.tableName)
.insert(items.map(this.toRow))
.returning('*');
return rows.map(this.fromRow);
return rows.map(this.fromRow) as ReadModel[];
}
async update(id: IdType, item: Partial<WriteModel>): Promise<ReadModel> {
@ -89,7 +89,7 @@ export abstract class CRUDStore<
.where({ id })
.update(this.toRow(item))
.returning('*');
return this.fromRow(rows[0]);
return this.fromRow(rows[0]) as ReadModel;
}
async delete(id: IdType): Promise<void> {
@ -127,6 +127,6 @@ export abstract class CRUDStore<
if (!row) {
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
*/
export const defaultToRow = <WriteModel, WriteRow>(
item: WriteModel,
): WriteRow => {
const row: Partial<WriteRow> = {};
item: Partial<WriteModel>,
): Partial<WriteRow> => {
const row = {};
Object.entries(item as Record<string, any>).forEach(([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
* @returns a modified version of row with all fields in camelCase
*/
export const defaultFromRow = <ReadModel, ReadRow>(row: ReadRow): ReadModel => {
const model: Partial<ReadModel> = {};
export const defaultFromRow = <ReadModel, ReadRow>(
row: Partial<ReadRow>,
): Partial<ReadModel> => {
const model = {};
Object.entries(row as Record<string, any>).forEach(([key, value]) => {
model[snakeToCamelCase(key)] = value;
});
return model as ReadModel;
return model;
};