All files / src/storage/schema/entities TxLabelMap.ts

100% Statements 37/37
84.61% Branches 11/13
100% Functions 19/19
100% Lines 36/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102    57x 57x   57x   1370x 1370x                               821x     815x     821x     815x     4x     1x     547x     2x     3x     2x       1x     1x     1x       3x 3x   3x   2x       1356x 1356x 1356x 1356x               814x 814x 814x       543x 543x 1x 1x 1x 1x   543x      
/* eslint-disable @typescript-eslint/no-unused-vars */
import { MerklePath } from '@bsv/sdk'
import { arraysEqual, entity, sdk, table, verifyId, verifyOneOrNone } from '../../../index.client'
import { EntityBase } from '.'
 
export class TxLabelMap extends EntityBase<table.TxLabelMap> {
  constructor(api?: table.TxLabelMap) {
    const now = new Date()
    super(
      api || {
        created_at: now,
        updated_at: now,
        transactionId: 0,
        txLabelId: 0,
        isDeleted: false
      }
    )
  }
 
  override updateApi(): void {
    /* nothing needed yet... */
  }
 
  get txLabelId() {
    return this.api.txLabelId
  }
  set txLabelId(v: number) {
    this.api.txLabelId = v
  }
  get transactionId() {
    return this.api.transactionId
  }
  set transactionId(v: number) {
    this.api.transactionId = v
  }
  get created_at() {
    return this.api.created_at
  }
  set created_at(v: Date) {
    this.api.created_at = v
  }
  get updated_at() {
    return this.api.updated_at
  }
  set updated_at(v: Date) {
    this.api.updated_at = v
  }
  get isDeleted() {
    return this.api.isDeleted
  }
  set isDeleted(v: boolean) {
    this.api.isDeleted = v
  }
 
  override get id(): number {
    throw new sdk.WERR_INVALID_OPERATION('entity has no "id" value')
  } // entity does not have its own id.
  override get entityName(): string {
    return 'TxLabelMap'
  }
  override get entityTable(): string {
    return 'tx_labels_map'
  }
 
  override equals(ei: table.TxLabelMap, syncMap?: entity.SyncMap | undefined): boolean {
    const eo = this.toApi()
    console.log('Comparing objects:', { eo, ei })
 
    if (eo.transactionId !== (syncMap ? syncMap.transaction.idMap[verifyId(ei.transactionId)] : ei.transactionId) || eo.txLabelId !== (syncMap ? syncMap.txLabel.idMap[verifyId(ei.txLabelId)] : ei.txLabelId) || eo.isDeleted !== ei.isDeleted) return false
 
    return true
  }
 
  static async mergeFind(storage: entity.EntityStorage, userId: number, ei: table.TxLabelMap, syncMap: entity.SyncMap, trx?: sdk.TrxToken): Promise<{ found: boolean; eo: entity.TxLabelMap; eiId: number }> {
    const transactionId = syncMap.transaction.idMap[ei.transactionId]
    const txLabelId = syncMap.txLabel.idMap[ei.txLabelId]
    const ef = verifyOneOrNone(await storage.findTxLabelMaps({ partial: { transactionId, txLabelId }, trx }))
    return {
      found: !!ef,
      eo: new entity.TxLabelMap(ef || { ...ei }),
      eiId: -1
    }
  }
 
  override async mergeNew(storage: entity.EntityStorage, userId: number, syncMap: entity.SyncMap, trx?: sdk.TrxToken): Promise<void> {
    this.transactionId = syncMap.transaction.idMap[this.transactionId]
    this.txLabelId = syncMap.txLabel.idMap[this.txLabelId]
    await storage.insertTxLabelMap(this.toApi(), trx)
  }
 
  override async mergeExisting(storage: entity.EntityStorage, since: Date | undefined, ei: table.TxLabelMap, syncMap: entity.SyncMap, trx?: sdk.TrxToken): Promise<boolean> {
    let wasMerged = false
    if (ei.updated_at > this.updated_at) {
      this.isDeleted = ei.isDeleted
      this.updated_at = new Date()
      await storage.updateTxLabelMap(this.transactionId, this.txLabelId, this.toApi(), trx)
      wasMerged = true
    }
    return wasMerged
  }
}