diff --git a/src/lib/db/crud/crud-store.ts b/src/lib/db/crud/crud-store.ts index ed6f88eb1f..5653a69fd7 100644 --- a/src/lib/db/crud/crud-store.ts +++ b/src/lib/db/crud/crud-store.ts @@ -34,16 +34,16 @@ export abstract class CRUDStore< protected readonly timer: (action: string) => Function; - protected toRow: (item: Partial) => RowWriteModel; - protected fromRow: (item: RowReadModel) => ReadModel; + protected toRow: (item: Partial) => Partial; + protected fromRow: (item: Partial) => Partial; constructor( tableName: string, db: Db, { eventBus }: CrudStoreConfig, options?: Partial<{ - toRow: (item: WriteModel) => RowWriteModel; - fromRow: (item: RowReadModel) => ReadModel; + toRow: (item: Partial) => Partial; + fromRow: (item: RowReadModel) => Partial; }>, ) { this.tableName = tableName; @@ -64,14 +64,14 @@ export abstract class CRUDStore< allQuery = allQuery.where(this.toRow(query) as Record); } const items = await allQuery; - return items.map(this.fromRow); + return items.map(this.fromRow) as ReadModel[]; } async insert(item: WriteModel): Promise { 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 { @@ -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): Promise { @@ -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 { @@ -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; } } diff --git a/src/lib/db/crud/default-mappings.ts b/src/lib/db/crud/default-mappings.ts index 5bd7bc8aab..39d132c76f 100644 --- a/src/lib/db/crud/default-mappings.ts +++ b/src/lib/db/crud/default-mappings.ts @@ -11,13 +11,13 @@ const snakeToCamelCase = (str: string) => * @returns a modified version of item with all fields in snake_case */ export const defaultToRow = ( - item: WriteModel, -): WriteRow => { - const row: Partial = {}; + item: Partial, +): Partial => { + const row = {}; Object.entries(item as Record).forEach(([key, value]) => { row[camelToSnakeCase(key)] = value; }); - return row as WriteRow; + return row; }; /** @@ -25,10 +25,12 @@ export const defaultToRow = ( * @param row is the input object * @returns a modified version of row with all fields in camelCase */ -export const defaultFromRow = (row: ReadRow): ReadModel => { - const model: Partial = {}; +export const defaultFromRow = ( + row: Partial, +): Partial => { + const model = {}; Object.entries(row as Record).forEach(([key, value]) => { model[snakeToCamelCase(key)] = value; }); - return model as ReadModel; + return model; };