From 7a59a7395262d658e1808e28eb9d1e2172a9b6b6 Mon Sep 17 00:00:00 2001 From: stephhuynh18 Date: Fri, 10 Nov 2023 12:43:20 -0800 Subject: [PATCH] Remove second retrieval --- src/repositories/request-repository.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/repositories/request-repository.ts b/src/repositories/request-repository.ts index 6fcd9ad54..8471fd740 100644 --- a/src/repositories/request-repository.ts +++ b/src/repositories/request-repository.ts @@ -1,6 +1,12 @@ import { CID } from 'multiformats/cid' import type { Knex } from 'knex' -import { DATABASE_FIELDS, Request, RequestStatus, RequestUpdateFields } from '../models/request.js' +import { + DATABASE_FIELDS, + Request, + RequestStatus, + RequestUpdateFields, + RequestCodec, +} from '../models/request.js' import { logEvent, logger } from '../logger/index.js' import { Config } from 'node-config-ts' import { Utils } from '../utils.js' @@ -109,9 +115,11 @@ export class RequestRepository { */ async createOrUpdate(request: Request): Promise { const keys = Object.keys(request).filter((key) => key !== 'id') // all keys except ID - const [{ id }] = await this.table.insert(request.toDB(), ['id']).onConflict('cid').merge(keys) - - const created = await this.table.where({ id }).first() + const [created] = await this.table + .insert(request.toDB(), ['id']) + .returning(Object.keys(RequestCodec.props)) + .onConflict('cid') + .merge(keys) logEvent.db({ type: 'request',