Skip to content

Commit

Permalink
Remove second retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
stephhuynh18 committed Nov 10, 2023
1 parent 2061e07 commit 7a59a73
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/repositories/request-repository.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -109,9 +115,11 @@ export class RequestRepository {
*/
async createOrUpdate(request: Request): Promise<Request> {
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',
Expand Down

0 comments on commit 7a59a73

Please sign in to comment.