diff --git a/api/src/services/items.ts b/api/src/services/items.ts index 344692bb2e..1791badc47 100644 --- a/api/src/services/items.ts +++ b/api/src/services/items.ts @@ -46,9 +46,11 @@ export class ItemsService implements AbstractService { return this; } - async create(data: Partial[]): Promise; - async create(data: Partial): Promise; - async create(data: Partial | Partial[]): Promise { + async create>(data: T[]): Promise; + async create>(data: T): Promise; + async create>( + data: T | T[] + ): Promise { const primaryKeyField = await this.schemaInspector.primary(this.collection); const columns = await this.schemaInspector.columns(this.collection); @@ -194,7 +196,7 @@ export class ItemsService implements AbstractService { return Array.isArray(data) ? savedPrimaryKeys : savedPrimaryKeys[0]; } - async readByQuery(query: Query): Promise { + async readByQuery(query: Query): Promise { const authorizationService = new AuthorizationService({ accountability: this.accountability, knex: this.knex, @@ -210,20 +212,24 @@ export class ItemsService implements AbstractService { } const records = await runAST(ast, { knex: this.knex }); - return records as Item | Item[] | null; + return records as T | T[] | null; } - readByKey( + readByKey( keys: PrimaryKey[], query?: Query, action?: PermissionsAction - ): Promise; - readByKey(key: PrimaryKey, query?: Query, action?: PermissionsAction): Promise; - async readByKey( + ): Promise; + readByKey( + key: PrimaryKey, + query?: Query, + action?: PermissionsAction + ): Promise; + async readByKey( key: PrimaryKey | PrimaryKey[], query: Query = {}, action: PermissionsAction = 'read' - ): Promise { + ): Promise { query = clone(query); const primaryKeyField = await this.schemaInspector.primary(this.collection); const keys = toArray(key); @@ -260,14 +266,14 @@ export class ItemsService implements AbstractService { if (result === null) throw new ForbiddenException(); - return result as Item | Item[] | null; + return result as T | T[] | null; } - update(data: Partial, keys: PrimaryKey[]): Promise; - update(data: Partial, key: PrimaryKey): Promise; - update(data: Partial[]): Promise; - async update( - data: Partial | Partial[], + update>(data: T, keys: PrimaryKey[]): Promise; + update>(data: T, key: PrimaryKey): Promise; + update>(data: T[]): Promise; + async update>( + data: T | T[], key?: PrimaryKey | PrimaryKey[] ): Promise { const primaryKeyField = await this.schemaInspector.primary(this.collection); @@ -428,7 +434,10 @@ export class ItemsService implements AbstractService { return keys; } - async updateByQuery(data: Partial, query: Query): Promise { + async updateByQuery>( + data: T, + query: Query + ): Promise { const primaryKeyField = await this.schemaInspector.primary(this.collection); const readQuery = cloneDeep(query); readQuery.fields = [primaryKeyField]; @@ -446,9 +455,11 @@ export class ItemsService implements AbstractService { return await this.update(data, keys); } - upsert(data: Partial[]): Promise; - upsert(data: Partial): Promise; - async upsert(data: Partial | Partial[]): Promise { + upsert>(data: T[]): Promise; + upsert>(data: T): Promise; + async upsert>( + data: T | T[] + ): Promise { const primaryKeyField = await this.schemaInspector.primary(this.collection); const payloads = toArray(data); const primaryKeys: PrimaryKey[] = []; @@ -570,7 +581,7 @@ export class ItemsService implements AbstractService { return record; } - async upsertSingleton(data: Partial) { + async upsertSingleton>(data: T) { const primaryKeyField = await this.schemaInspector.primary(this.collection); const record = await this.knex