All files / src/storage StorageKnex.ts

91.6% Statements 502/548
74.38% Branches 151/203
96.87% Functions 124/128
95.75% Lines 451/471

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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971  50x 50x     50x 50x 50x 50x                   50x       72x 72x 72x       170x       89x 89x           89x 89x 70x 70x 22x 22x     89x       60x 60x       88x   88x 88x 30x 30x 30x 30x     30x 30x 30x 30x 2x       58x 58x 52x   88x       137x 137x 137x   137x 137x 137x   137x 137x     137x 137x 137x       42x 42x 42x   42x 42x 42x   42x 42x     42x 42x 42x       37x 37x 37x 37x 37x 37x   37x     37x 37x 37x       37x 37x 37x 37x 37x 37x   37x     37x 37x 37x       11x 11x 11x     20x 20x 20x       546x 546x 546x 545x 545x       101x 101x 101x 99x 99x       52x 52x 52x 51x 51x       1x   1x 1x       93x 93x 93x 93x 93x 92x   92x 1x 3x 3x 3x       92x       184x 184x       81x 81x 81x 80x 80x       630x 630x 630x 629x 629x       595x 595x 595x 594x 594x       1628x 1628x 1628x 1627x 1627x       254x 254x 254x 253x 253x       1321x 1321x       96x 96x 96x 95x 95x       841x 841x       8x 8x 8x 8x 8x       16x 16x 16x 16x 16x       2x 2x     13x 13x     4x 4x     10x 10x     34x 34x     4x 4x     3x 3x     89x   89x 1x 88x 88x       89x     39x 39x     47x 47x         31x   31x   31x 31x       31x             3x 3x     65x 65x     1x 1x       12363x 12363x 12363x 12363x 473x 473x   12363x       335x     174x 174x 174x 174x     1011x 1011x     214x     3775x 3775x 3775x 1x   3775x 864x 36x   3775x     2158x 2158x 2158x     437x     194x 194x 194x 194x 194x 194x     1028x 1028x 1028x     96x     1084x 1084x 1084x 1084x 1084x 221x 17x   1084x     1361x 1361x 1361x     163x     329x     4x                               1x   1x 1x       328x     167x 167x 167x     1010x 1010x 1010x     211x 211x 211x     3774x 3774x 3774x 3738x 5677x     3774x     2157x 2157x 2157x     436x 436x 436x     193x 193x 193x     1027x 1027x 1027x     95x 95x 95x     1083x 1083x 1083x 1066x 2937x     1083x     1360x 1360x 1360x     162x 162x 162x     328x 328x 328x     3x 3x 3x       36x 36x 36x       7x     7x     1x     3x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x       70x       71x     71x 71x 71x         16x 16x 16x 32x 32x 32x               50x   43x 43x 43x                   19988x 19988x 19988x         2937x       52x 52x 4x         5751x   5711x       30x 30x 2x     72x                     6791x 4x     6791x     51x 51x     51x     6791x               345x 345x 345x 345x 345x 345x   345x 47x 47x     345x 80x 80x     345x 2270x 2270x 108x 2162x 268x     345x 345x               6446x 6446x 6446x 6446x 6446x 6446x 6446x 16x 16x     6446x 2702x 2702x     6446x 78416x 78416x 4094x 74322x 8993x     6446x 6446x       59x 59x         59x     36x 36x 36x 36x 36x   36x       115x         115x                         7x 7x 21x 7x 7x 7x 7x                 6x 6x 18x   6x 6x     6x 6x 6x 6x 6x 6x     6x                           6x   6x                                       6x                                           6x   6x                 6x 6x     6x                       18950x 18950x 18950x 93x 93x 32x     18950x 16696x 22398x 22398x     18950x 257618x 257618x 31084x 226534x 12987x     18950x                 12761x 18780x   12761x        
import { ListActionsArgs, ListActionsResult, ListOutputsArgs, ListOutputsResult } from '@bsv/sdk'
import { sdk, verifyOne, verifyOneOrNone, verifyTruthy } from '../index.all'
import { KnexMigrations, table } from './index.all'
 
import { Knex } from 'knex'
import { StorageProvider, StorageProviderOptions } from './StorageProvider'
import { purgeData } from './methods/purgeData'
import { listActions } from './methods/listActions'
import { listOutputs } from './methods/listOutputs'
import { DBType } from './StorageReader'
 
export interface StorageKnexOptions extends StorageProviderOptions {
  /**
   * Knex database interface initialized with valid connection configuration.
   */
  knex: Knex
}
 
export class StorageKnex extends StorageProvider implements sdk.WalletStorageProvider {
  knex: Knex
 
  constructor(options: StorageKnexOptions) {
    super(options)
    Iif (!options.knex) throw new sdk.WERR_INVALID_PARAMETER('options.knex', `valid`)
    this.knex = options.knex
  }
 
  async readSettings(): Promise<table.Settings> {
    return this.validateEntity(verifyOne(await this.toDb(undefined)<table.Settings>('settings')))
  }
 
  override async getProvenOrRawTx(txid: string, trx?: sdk.TrxToken): Promise<sdk.ProvenOrRawTx> {
    const k = this.toDb(trx)
    const r: sdk.ProvenOrRawTx = {
      proven: undefined,
      rawTx: undefined,
      inputBEEF: undefined
    }
 
    r.proven = verifyOneOrNone(await this.findProvenTxs({ partial: { txid: txid } }))
    if (!r.proven) {
      const reqRawTx = verifyOneOrNone(await k('proven_tx_reqs').where('txid', txid).whereIn('status', ['unsent', 'unmined', 'unconfirmed', 'sending', 'nosend', 'completed']).select('rawTx', 'inputBEEF'))
      if (reqRawTx) {
        r.rawTx = Array.from(reqRawTx.rawTx)
        r.inputBEEF = Array.from(reqRawTx.inputBEEF)
      }
    }
    return r
  }
 
  dbTypeSubstring(source: string, fromOffset: number, forLength?: number) {
    Iif (this.dbtype === 'MySQL') return `substring(${source} from ${fromOffset} for ${forLength!})`
    return `substr(${source}, ${fromOffset}, ${forLength})`
  }
 
  override async getRawTxOfKnownValidTransaction(txid?: string, offset?: number, length?: number, trx?: sdk.TrxToken): Promise<number[] | undefined> {
    Iif (!txid) return undefined
 
    let rawTx: number[] | undefined = undefined
    if (Number.isInteger(offset) && Number.isInteger(length)) {
      let rs: { rawTx: Buffer | null }[] = await this.toDb(trx).raw(`select ${this.dbTypeSubstring('rawTx', offset! + 1, length)} as rawTx from proven_txs where txid = '${txid}'`)
      Iif (this.dbtype === 'MySQL') rs = (rs as unknown as { rawTx: Buffer | null }[][])[0]
      const r = verifyOneOrNone(rs)
      Iif (r && r.rawTx) {
        rawTx = Array.from(r.rawTx)
      } else {
        let rs: { rawTx: Buffer | null }[] = await this.toDb(trx).raw(`select ${this.dbTypeSubstring('rawTx', offset! + 1, length)} as rawTx from proven_tx_reqs where txid = '${txid}' and status in ('unsent', 'nosend', 'sending', 'unmined', 'completed')`)
        Iif (this.dbtype === 'MySQL') rs = (rs as unknown as { rawTx: Buffer | null }[][])[0]
        const r = verifyOneOrNone(rs)
        if (r && r.rawTx) {
          rawTx = Array.from(r.rawTx)
        }
      }
    } else {
      const r = await this.getProvenOrRawTx(txid, trx)
      if (r.proven) rawTx = r.proven.rawTx
      else rawTx = r.rawTx
    }
    return rawTx
  }
 
  getProvenTxsForUserQuery(args: sdk.FindForUserSincePagedArgs): Knex.QueryBuilder {
    const k = this.toDb(args.trx)
    let q = k('proven_txs').where(function () {
      this.whereExists(k.select('*').from('transactions').whereRaw(`proven_txs.provenTxId = transactions.provenTxId and transactions.userId = ${args.userId}`))
    })
    if (args.paged) {
      q = q.limit(args.paged.limit)
      q = q.offset(args.paged.offset || 0)
    }
    if (args.since) q = q.where('updated_at', '>=', args.since)
    return q
  }
  override async getProvenTxsForUser(args: sdk.FindForUserSincePagedArgs): Promise<table.ProvenTx[]> {
    const q = this.getProvenTxsForUserQuery(args)
    const rs = await q
    return this.validateEntities(rs)
  }
 
  getProvenTxReqsForUserQuery(args: sdk.FindForUserSincePagedArgs): Knex.QueryBuilder {
    const k = this.toDb(args.trx)
    let q = k('proven_tx_reqs').where(function () {
      this.whereExists(k.select('*').from('transactions').whereRaw(`proven_tx_reqs.txid = transactions.txid and transactions.userId = ${args.userId}`))
    })
    if (args.paged) {
      q = q.limit(args.paged.limit)
      q = q.offset(args.paged.offset || 0)
    }
    if (args.since) q = q.where('updated_at', '>=', args.since)
    return q
  }
  override async getProvenTxReqsForUser(args: sdk.FindForUserSincePagedArgs): Promise<table.ProvenTxReq[]> {
    const q = this.getProvenTxReqsForUserQuery(args)
    const rs = await q
    return this.validateEntities(rs, undefined, ['notified'])
  }
 
  getTxLabelMapsForUserQuery(args: sdk.FindForUserSincePagedArgs): Knex.QueryBuilder {
    const k = this.toDb(args.trx)
    let q = k('tx_labels_map').whereExists(k.select('*').from('tx_labels').whereRaw(`tx_labels.txLabelId = tx_labels_map.txLabelId and tx_labels.userId = ${args.userId}`))
    if (args.since) q = q.where('updated_at', '>=', this.validateDateForWhere(args.since))
    if (args.paged) {
      q = q.limit(args.paged.limit)
      q = q.offset(args.paged.offset || 0)
    }
    return q
  }
  override async getTxLabelMapsForUser(args: sdk.FindForUserSincePagedArgs): Promise<table.TxLabelMap[]> {
    const q = this.getTxLabelMapsForUserQuery(args)
    const rs = await q
    return this.validateEntities(rs, undefined, ['isDeleted'])
  }
 
  getOutputTagMapsForUserQuery(args: sdk.FindForUserSincePagedArgs): Knex.QueryBuilder {
    const k = this.toDb(args.trx)
    let q = k('output_tags_map').whereExists(k.select('*').from('output_tags').whereRaw(`output_tags.outputTagId = output_tags_map.outputTagId and output_tags.userId = ${args.userId}`))
    if (args.since) q = q.where('updated_at', '>=', this.validateDateForWhere(args.since))
    if (args.paged) {
      q = q.limit(args.paged.limit)
      q = q.offset(args.paged.offset || 0)
    }
    return q
  }
  override async getOutputTagMapsForUser(args: sdk.FindForUserSincePagedArgs): Promise<table.OutputTagMap[]> {
    const q = this.getOutputTagMapsForUserQuery(args)
    const rs = await q
    return this.validateEntities(rs, undefined, ['isDeleted'])
  }
 
  override async listActions(auth: sdk.AuthId, args: ListActionsArgs): Promise<ListActionsResult> {
    Iif (!auth.userId) throw new sdk.WERR_UNAUTHORIZED()
    const vargs = sdk.validateListActionsArgs(args)
    return await listActions(this, auth, vargs)
  }
  override async listOutputs(auth: sdk.AuthId, args: ListOutputsArgs): Promise<ListOutputsResult> {
    Iif (!auth.userId) throw new sdk.WERR_UNAUTHORIZED()
    const vargs = sdk.validateListOutputsArgs(args)
    return await listOutputs(this, auth, vargs)
  }
 
  override async insertProvenTx(tx: table.ProvenTx, trx?: sdk.TrxToken): Promise<number> {
    const e = await this.validateEntityForInsert(tx, trx)
    if (e.provenTxId === 0) delete e.provenTxId
    const [id] = await this.toDb(trx)<table.ProvenTx>('proven_txs').insert(e)
    tx.provenTxId = id
    return tx.provenTxId
  }
 
  override async insertProvenTxReq(tx: table.ProvenTxReq, trx?: sdk.TrxToken): Promise<number> {
    const e = await this.validateEntityForInsert(tx, trx)
    if (e.provenTxReqId === 0) delete e.provenTxReqId
    const [id] = await this.toDb(trx)<table.ProvenTxReq>('proven_tx_reqs').insert(e)
    tx.provenTxReqId = id
    return tx.provenTxReqId
  }
 
  override async insertUser(user: table.User, trx?: sdk.TrxToken): Promise<number> {
    const e = await this.validateEntityForInsert(user, trx)
    if (e.userId === 0) delete e.userId
    const [id] = await this.toDb(trx)<table.User>('users').insert(e)
    user.userId = id
    return user.userId
  }
 
  override async insertCertificateAuth(auth: sdk.AuthId, certificate: table.CertificateX): Promise<number> {
    Iif (!auth.userId || certificate.userId && certificate.userId !== auth.userId)
      throw new sdk.WERR_UNAUTHORIZED()
    certificate.userId = auth.userId
    return await this.insertCertificate(certificate)
  }
 
  override async insertCertificate(certificate: table.CertificateX, trx?: sdk.TrxToken): Promise<number> {
    const e = await this.validateEntityForInsert(certificate, trx, undefined, ['isDeleted'])
    const fields = e.fields
    if (e.fields) delete e.fields
    if (e.certificateId === 0) delete e.certificateId
    const [id] = await this.toDb(trx)<table.Certificate>('certificates').insert(e)
    certificate.certificateId = id
 
    if (fields) {
      for (const field of fields) {
        field.certificateId = id
        field.userId = certificate.userId
        await this.insertCertificateField(field, trx)
      }
    }
 
    return certificate.certificateId
  }
 
  override async insertCertificateField(certificateField: table.CertificateField, trx?: sdk.TrxToken): Promise<void> {
    const e = await this.validateEntityForInsert(certificateField, trx)
    await this.toDb(trx)<table.Certificate>('certificate_fields').insert(e)
  }
 
  override async insertOutputBasket(basket: table.OutputBasket, trx?: sdk.TrxToken): Promise<number> {
    const e = await this.validateEntityForInsert(basket, trx, undefined, ['isDeleted'])
    if (e.basketId === 0) delete e.basketId
    const [id] = await this.toDb(trx)<table.OutputBasket>('output_baskets').insert(e)
    basket.basketId = id
    return basket.basketId
  }
 
  override async insertTransaction(tx: table.Transaction, trx?: sdk.TrxToken): Promise<number> {
    const e = await this.validateEntityForInsert(tx, trx)
    if (e.transactionId === 0) delete e.transactionId
    const [id] = await this.toDb(trx)<table.Transaction>('transactions').insert(e)
    tx.transactionId = id
    return tx.transactionId
  }
 
  override async insertCommission(commission: table.Commission, trx?: sdk.TrxToken): Promise<number> {
    const e = await this.validateEntityForInsert(commission, trx)
    if (e.commissionId === 0) delete e.commissionId
    const [id] = await this.toDb(trx)<table.Commission>('commissions').insert(e)
    commission.commissionId = id
    return commission.commissionId
  }
 
  override async insertOutput(output: table.Output, trx?: sdk.TrxToken): Promise<number> {
    const e = await this.validateEntityForInsert(output, trx)
    if (e.outputId === 0) delete e.outputId
    const [id] = await this.toDb(trx)<table.Output>('outputs').insert(e)
    output.outputId = id
    return output.outputId
  }
 
  override async insertOutputTag(tag: table.OutputTag, trx?: sdk.TrxToken): Promise<number> {
    const e = await this.validateEntityForInsert(tag, trx, undefined, ['isDeleted'])
    if (e.outputTagId === 0) delete e.outputTagId
    const [id] = await this.toDb(trx)<table.OutputTag>('output_tags').insert(e)
    tag.outputTagId = id
    return tag.outputTagId
  }
 
  override async insertOutputTagMap(tagMap: table.OutputTagMap, trx?: sdk.TrxToken): Promise<void> {
    const e = await this.validateEntityForInsert(tagMap, trx, undefined, ['isDeleted'])
    const [id] = await this.toDb(trx)<table.OutputTagMap>('output_tags_map').insert(e)
  }
 
  override async insertTxLabel(label: table.TxLabel, trx?: sdk.TrxToken): Promise<number> {
    const e = await this.validateEntityForInsert(label, trx, undefined, ['isDeleted'])
    if (e.txLabelId === 0) delete e.txLabelId
    const [id] = await this.toDb(trx)<table.TxLabel>('tx_labels').insert(e)
    label.txLabelId = id
    return label.txLabelId
  }
 
  override async insertTxLabelMap(labelMap: table.TxLabelMap, trx?: sdk.TrxToken): Promise<void> {
    const e = await this.validateEntityForInsert(labelMap, trx, undefined, ['isDeleted'])
    const [id] = await this.toDb(trx)<table.TxLabelMap>('tx_labels_map').insert(e)
  }
 
  override async insertMonitorEvent(event: table.MonitorEvent, trx?: sdk.TrxToken): Promise<number> {
    const e = await this.validateEntityForInsert(event, trx)
    if (e.id === 0) delete e.id
    const [id] = await this.toDb(trx)<table.MonitorEvent>('monitor_events').insert(e)
    event.id = id
    return event.id
  }
 
  override async insertSyncState(syncState: table.SyncState, trx?: sdk.TrxToken): Promise<number> {
    const e = await this.validateEntityForInsert(syncState, trx, ['when'], ['init'])
    if (e.syncStateId === 0) delete e.syncStateId
    const [id] = await this.toDb(trx)<table.SyncState>('sync_states').insert(e)
    syncState.syncStateId = id
    return syncState.syncStateId
  }
 
  override async updateCertificateField(certificateId: number, fieldName: string, update: Partial<table.CertificateField>, trx?: sdk.TrxToken): Promise<number> {
    await this.verifyReadyForDatabaseAccess(trx)
    return await this.toDb(trx)<table.CertificateField>('certificate_fields').where({ certificateId, fieldName }).update(this.validatePartialForUpdate(update))
  }
  override async updateCertificate(id: number, update: Partial<table.Certificate>, trx?: sdk.TrxToken): Promise<number> {
    await this.verifyReadyForDatabaseAccess(trx)
    return await this.toDb(trx)<table.Certificate>('certificates').where({ certificateId: id }).update(this.validatePartialForUpdate(update, undefined, ['isDeleted']))
  }
  override async updateCommission(id: number, update: Partial<table.Commission>, trx?: sdk.TrxToken): Promise<number> {
    await this.verifyReadyForDatabaseAccess(trx)
    return await this.toDb(trx)<table.Commission>('commissions').where({ commissionId: id }).update(this.validatePartialForUpdate(update))
  }
  override async updateOutputBasket(id: number, update: Partial<table.OutputBasket>, trx?: sdk.TrxToken): Promise<number> {
    await this.verifyReadyForDatabaseAccess(trx)
    return await this.toDb(trx)<table.OutputBasket>('output_baskets').where({ basketId: id }).update(this.validatePartialForUpdate(update, undefined, ['isDeleted']))
  }
  override async updateOutput(id: number, update: Partial<table.Output>, trx?: sdk.TrxToken): Promise<number> {
    await this.verifyReadyForDatabaseAccess(trx)
    return await this.toDb(trx)<table.Output>('outputs').where({ outputId: id }).update(this.validatePartialForUpdate(update))
  }
  override async updateOutputTagMap(outputId: number, tagId: number, update: Partial<table.OutputTagMap>, trx?: sdk.TrxToken): Promise<number> {
    await this.verifyReadyForDatabaseAccess(trx)
    return await this.toDb(trx)<table.OutputTagMap>('output_tags_map').where({ outputId, outputTagId: tagId }).update(this.validatePartialForUpdate(update, undefined, ['isDeleted']))
  }
  override async updateOutputTag(id: number, update: Partial<table.OutputTag>, trx?: sdk.TrxToken): Promise<number> {
    await this.verifyReadyForDatabaseAccess(trx)
    return await this.toDb(trx)<table.OutputTag>('output_tags').where({ outputTagId: id }).update(this.validatePartialForUpdate(update, undefined, ['isDeleted']))
  }
  override async updateProvenTxReq(id: number | number[], update: Partial<table.ProvenTxReq>, trx?: sdk.TrxToken): Promise<number> {
    await this.verifyReadyForDatabaseAccess(trx)
    let r: number
    if (Array.isArray(id)) {
      r = await this.toDb(trx)<table.ProvenTxReq>('proven_tx_reqs').whereIn('provenTxReqId', id).update(this.validatePartialForUpdate(update))
    } else if (Number.isInteger(id)) {
      r = await this.toDb(trx)<table.ProvenTxReq>('proven_tx_reqs').where({ 'provenTxReqId': id }).update(this.validatePartialForUpdate(update))
    } else E{
      throw new sdk.WERR_INVALID_PARAMETER('id', 'transactionId or array of transactionId')
    }
    return r
  }
  override async updateProvenTx(id: number, update: Partial<table.ProvenTx>, trx?: sdk.TrxToken): Promise<number> {
    await this.verifyReadyForDatabaseAccess(trx)
    return await this.toDb(trx)<table.ProvenTx>('proven_txs').where({ provenTxId: id }).update(this.validatePartialForUpdate(update))
  }
  override async updateSyncState(id: number, update: Partial<table.SyncState>, trx?: sdk.TrxToken): Promise<number> {
    await this.verifyReadyForDatabaseAccess(trx)
    return await this.toDb(trx)<table.SyncState>('sync_states')
      .where({ syncStateId: id })
      .update(this.validatePartialForUpdate(update, ['when'], ['init']))
  }
  override async updateTransaction(id: number | number[], update: Partial<table.Transaction>, trx?: sdk.TrxToken): Promise<number> {
    await this.verifyReadyForDatabaseAccess(trx)
    let r: number
    Iif (Array.isArray(id)) {
      r = await this.toDb(trx)<table.Transaction>('transactions').whereIn('transactionId', id).update(await this.validatePartialForUpdate(update))
    } else if (Number.isInteger(id)) {
      r = await this.toDb(trx)<table.Transaction>('transactions').where({ transactionId: id }).update(await this.validatePartialForUpdate(update))
    } else E{
      throw new sdk.WERR_INVALID_PARAMETER('id', 'transactionId or array of transactionId')
    }
    return r
  }
  override async updateTxLabelMap(transactionId: number, txLabelId: number, update: Partial<table.TxLabelMap>, trx?: sdk.TrxToken): Promise<number> {
    await this.verifyReadyForDatabaseAccess(trx)
    return await this.toDb(trx)<table.TxLabelMap>('tx_labels_map').where({ transactionId, txLabelId }).update(this.validatePartialForUpdate(update, undefined, ['isDeleted']))
  }
  override async updateTxLabel(id: number, update: Partial<table.TxLabel>, trx?: sdk.TrxToken): Promise<number> {
    await this.verifyReadyForDatabaseAccess(trx)
    return await this.toDb(trx)<table.TxLabel>('tx_labels').where({ txLabelId: id }).update(this.validatePartialForUpdate(update, undefined, ['isDeleted']))
  }
  override async updateUser(id: number, update: Partial<table.User>, trx?: sdk.TrxToken): Promise<number> {
    await this.verifyReadyForDatabaseAccess(trx)
    return await this.toDb(trx)<table.User>('users').where({ userId: id }).update(this.validatePartialForUpdate(update))
  }
  override async updateMonitorEvent(id: number, update: Partial<table.MonitorEvent>, trx?: sdk.TrxToken): Promise<number> {
    await this.verifyReadyForDatabaseAccess(trx)
    return await this.toDb(trx)<table.MonitorEvent>('monitor_events').where({ id }).update(this.validatePartialForUpdate(update))
  }
 
  setupQuery<T extends object>(table: string, args: sdk.FindPartialSincePagedArgs<T>): Knex.QueryBuilder {
    let q = this.toDb(args.trx)<T>(table)
    if (args.partial && Object.keys(args.partial).length > 0) q.where(args.partial)
    if (args.since) q.where('updated_at', '>=', this.validateDateForWhere(args.since))
    if (args.paged) {
      q.limit(args.paged.limit)
      q.offset(args.paged.offset || 0)
    }
    return q
  }
 
  findCertificateFieldsQuery(args: sdk.FindCertificateFieldsArgs): Knex.QueryBuilder {
    return this.setupQuery('certificate_fields', args)
  }
  findCertificatesQuery(args: sdk.FindCertificatesArgs): Knex.QueryBuilder {
    const q = this.setupQuery('certificates', args)
    if (args.certifiers && args.certifiers.length > 0) q.whereIn('certifier', args.certifiers)
    if (args.types && args.types.length > 0) q.whereIn('type', args.types)
    return q
  }
  findCommissionsQuery(args: sdk.FindCommissionsArgs): Knex.QueryBuilder {
    Iif (args.partial.lockingScript) throw new sdk.WERR_INVALID_PARAMETER('partial.lockingScript', `undefined. Commissions may not be found by lockingScript value.`)
    return this.setupQuery('commissions', args)
  }
  findOutputBasketsQuery(args: sdk.FindOutputBasketsArgs): Knex.QueryBuilder {
    return this.setupQuery('output_baskets', args)
  }
  findOutputsQuery(args: sdk.FindOutputsArgs, count?: boolean): Knex.QueryBuilder {
    Iif (args.partial.lockingScript) throw new sdk.WERR_INVALID_PARAMETER('args.partial.lockingScript', `undefined. Outputs may not be found by lockingScript value.`)
    const q = this.setupQuery('outputs', args)
    if (args.txStatus && args.txStatus.length > 0) {
      q.whereRaw(`(select status from transactions where transactions.transactionId = outputs.transactionId) in (${args.txStatus.map(s => `'${s}'`).join(',')})`)
    }
    if (args.noScript && !count) {
      const columns = table.outputColumnsWithoutLockingScript.map(c => `outputs.${c}`)
      q.select(columns)
    }
    return q
  }
  findOutputTagMapsQuery(args: sdk.FindOutputTagMapsArgs): Knex.QueryBuilder {
    const q = this.setupQuery('output_tags_map', args)
    Iif (args.tagIds && args.tagIds.length > 0) q.whereIn('outputTagId', args.tagIds)
    return q
  }
  findOutputTagsQuery(args: sdk.FindOutputTagsArgs): Knex.QueryBuilder {
    return this.setupQuery('output_tags', args)
  }
  findProvenTxReqsQuery(args: sdk.FindProvenTxReqsArgs): Knex.QueryBuilder {
    Iif (args.partial.rawTx) throw new sdk.WERR_INVALID_PARAMETER('args.partial.rawTx', `undefined. ProvenTxReqs may not be found by rawTx value.`)
    Iif (args.partial.inputBEEF) throw new sdk.WERR_INVALID_PARAMETER('args.partial.inputBEEF', `undefined. ProvenTxReqs may not be found by inputBEEF value.`)
    const q = this.setupQuery('proven_tx_reqs', args)
    if (args.status && args.status.length > 0) q.whereIn('status', args.status)
    Iif (args.txids && args.txids.length > 0) q.whereIn('txid', args.txids)
    return q
  }
  findProvenTxsQuery(args: sdk.FindProvenTxsArgs): Knex.QueryBuilder {
    Iif (args.partial.rawTx) throw new sdk.WERR_INVALID_PARAMETER('args.partial.rawTx', `undefined. ProvenTxs may not be found by rawTx value.`)
    Iif (args.partial.merklePath) throw new sdk.WERR_INVALID_PARAMETER('args.partial.merklePath', `undefined. ProvenTxs may not be found by merklePath value.`)
    return this.setupQuery('proven_txs', args)
  }
  findSyncStatesQuery(args: sdk.FindSyncStatesArgs): Knex.QueryBuilder {
    return this.setupQuery('sync_states', args)
  }
  findTransactionsQuery(args: sdk.FindTransactionsArgs, count?: boolean): Knex.QueryBuilder {
    Iif (args.partial.rawTx) throw new sdk.WERR_INVALID_PARAMETER('args.partial.rawTx', `undefined. Transactions may not be found by rawTx value.`)
    Iif (args.partial.inputBEEF) throw new sdk.WERR_INVALID_PARAMETER('args.partial.inputBEEF', `undefined. Transactions may not be found by inputBEEF value.`)
    const q = this.setupQuery('transactions', args)
    Iif (args.status && args.status.length > 0) q.whereIn('status', args.status)
    if (args.noRawTx && !count) {
      const columns = table.transactionColumnsWithoutRawTx.map(c => `transactions.${c}`)
      q.select(columns)
    }
    return q
  }
  findTxLabelMapsQuery(args: sdk.FindTxLabelMapsArgs): Knex.QueryBuilder {
    const q = this.setupQuery('tx_labels_map', args)
    Iif (args.labelIds && args.labelIds.length > 0) q.whereIn('txLabelId', args.labelIds)
    return q
  }
  findTxLabelsQuery(args: sdk.FindTxLabelsArgs): Knex.QueryBuilder {
    return this.setupQuery('tx_labels', args)
  }
  findUsersQuery(args: sdk.FindUsersArgs): Knex.QueryBuilder {
    return this.setupQuery('users', args)
  }
  findMonitorEventsQuery(args: sdk.FindMonitorEventsArgs): Knex.QueryBuilder {
    return this.setupQuery('monitor_events', args)
  }
 
  override async findCertificatesAuth(auth: sdk.AuthId, args: sdk.FindCertificatesArgs): Promise<table.Certificate[]> {
    Iif (!auth.userId || args.partial.userId && args.partial.userId !== auth.userId)
      throw new sdk.WERR_UNAUTHORIZED()
    args.partial.userId = auth.userId
    return await this.findCertificates(args)
  }
  override async findOutputBasketsAuth(auth: sdk.AuthId, args: sdk.FindOutputBasketsArgs): Promise<table.OutputBasket[]> {
    Iif (!auth.userId || args.partial.userId && args.partial.userId !== auth.userId)
      throw new sdk.WERR_UNAUTHORIZED()
    args.partial.userId = auth.userId
    return await this.findOutputBaskets(args)
  }
  override async findOutputsAuth(auth: sdk.AuthId, args: sdk.FindOutputsArgs): Promise<table.Output[]> {
    Iif (!auth.userId || args.partial.userId && args.partial.userId !== auth.userId)
      throw new sdk.WERR_UNAUTHORIZED()
    args.partial.userId = auth.userId
    return await this.findOutputs(args)
  }
 
  override async findCertificateFields(args: sdk.FindCertificateFieldsArgs): Promise<table.CertificateField[]> {
    return this.validateEntities(await this.findCertificateFieldsQuery(args))
  }
  override async findCertificates(args: sdk.FindCertificatesArgs): Promise<table.Certificate[]> {
    const q = this.findCertificatesQuery(args)
    const r = await q
    return this.validateEntities(r, undefined, ['isDeleted'])
  }
  override async findCommissions(args: sdk.FindCommissionsArgs): Promise<table.Commission[]> {
    const q = this.findCommissionsQuery(args)
    const r = await q
    return this.validateEntities(r, undefined, ['isRedeemed'])
  }
  override async findOutputBaskets(args: sdk.FindOutputBasketsArgs): Promise<table.OutputBasket[]> {
    const q = this.findOutputBasketsQuery(args)
    const r = await q
    return this.validateEntities(r, undefined, ['isDeleted'])
  }
  override async findOutputs(args: sdk.FindOutputsArgs): Promise<table.Output[]> {
    const q = this.findOutputsQuery(args)
    const r = await q
    if (!args.noScript) {
      for (const o of r) {
        await this.validateOutputScript(o, args.trx)
      }
    }
    return this.validateEntities(r, undefined, ['spendable', 'change'])
  }
  override async findOutputTagMaps(args: sdk.FindOutputTagMapsArgs): Promise<table.OutputTagMap[]> {
    const q = this.findOutputTagMapsQuery(args)
    const r = await q
    return this.validateEntities(r, undefined, ['isDeleted'])
  }
  override async findOutputTags(args: sdk.FindOutputTagsArgs): Promise<table.OutputTag[]> {
    const q = this.findOutputTagsQuery(args)
    const r = await q
    return this.validateEntities(r, undefined, ['isDeleted'])
  }
  override async findProvenTxReqs(args: sdk.FindProvenTxReqsArgs): Promise<table.ProvenTxReq[]> {
    const q = this.findProvenTxReqsQuery(args)
    const r = await q
    return this.validateEntities(r, undefined, ['notified'])
  }
  override async findProvenTxs(args: sdk.FindProvenTxsArgs): Promise<table.ProvenTx[]> {
    const q = this.findProvenTxsQuery(args)
    const r = await q
    return this.validateEntities(r)
  }
  override async findSyncStates(args: sdk.FindSyncStatesArgs): Promise<table.SyncState[]> {
    const q = this.findSyncStatesQuery(args)
    const r = await q
    return this.validateEntities(r, ['when'], ['init'])
  }
  override async findTransactions(args: sdk.FindTransactionsArgs): Promise<table.Transaction[]> {
    const q = this.findTransactionsQuery(args)
    const r = await q
    if (!args.noRawTx) {
      for (const t of r) {
        await this.validateRawTransaction(t, args.trx)
      }
    }
    return this.validateEntities(r, undefined, ['isOutgoing'])
  }
  override async findTxLabelMaps(args: sdk.FindTxLabelMapsArgs): Promise<table.TxLabelMap[]> {
    const q = this.findTxLabelMapsQuery(args)
    const r = await q
    return this.validateEntities(r, undefined, ['isDeleted'])
  }
  override async findTxLabels(args: sdk.FindTxLabelsArgs): Promise<table.TxLabel[]> {
    const q = this.findTxLabelsQuery(args)
    const r = await q
    return this.validateEntities(r, undefined, ['isDeleted'])
  }
  override async findUsers(args: sdk.FindUsersArgs): Promise<table.User[]> {
    const q = this.findUsersQuery(args)
    const r = await q
    return this.validateEntities(r)
  }
  override async findMonitorEvents(args: sdk.FindMonitorEventsArgs): Promise<table.MonitorEvent[]> {
    const q = this.findMonitorEventsQuery(args)
    const r = await q
    return this.validateEntities(r, ['when'], undefined)
  }
 
  async getCount<T extends object>(q: Knex.QueryBuilder<T, T[]>): Promise<number> {
    q.count()
    const r = await q
    return r[0]['count(*)']
  }
 
  override async countCertificateFields(args: sdk.FindCertificateFieldsArgs): Promise<number> {
    return await this.getCount(this.findCertificateFieldsQuery(args))
  }
  override async countCertificates(args: sdk.FindCertificatesArgs): Promise<number> {
    return await this.getCount(this.findCertificatesQuery(args))
  }
  override async countCommissions(args: sdk.FindCommissionsArgs): Promise<number> {
    return await this.getCount(this.findCommissionsQuery(args))
  }
  override async countOutputBaskets(args: sdk.FindOutputBasketsArgs): Promise<number> {
    return await this.getCount(this.findOutputBasketsQuery(args))
  }
  override async countOutputs(args: sdk.FindOutputsArgs): Promise<number> {
    return await this.getCount(this.findOutputsQuery(args, true))
  }
  override async countOutputTagMaps(args: sdk.FindOutputTagMapsArgs): Promise<number> {
    return await this.getCount(this.findOutputTagMapsQuery(args))
  }
  override async countOutputTags(args: sdk.FindOutputTagsArgs): Promise<number> {
    return await this.getCount(this.findOutputTagsQuery(args))
  }
  override async countProvenTxReqs(args: sdk.FindProvenTxReqsArgs): Promise<number> {
    return await this.getCount(this.findProvenTxReqsQuery(args))
  }
  override async countProvenTxs(args: sdk.FindProvenTxsArgs): Promise<number> {
    return await this.getCount(this.findProvenTxsQuery(args))
  }
  override async countSyncStates(args: sdk.FindSyncStatesArgs): Promise<number> {
    return await this.getCount(this.findSyncStatesQuery(args))
  }
  override async countTransactions(args: sdk.FindTransactionsArgs): Promise<number> {
    return await this.getCount(this.findTransactionsQuery(args, true))
  }
  override async countTxLabelMaps(args: sdk.FindTxLabelMapsArgs): Promise<number> {
    return await this.getCount(this.findTxLabelMapsQuery(args))
  }
  override async countTxLabels(args: sdk.FindTxLabelsArgs): Promise<number> {
    return await this.getCount(this.findTxLabelsQuery(args))
  }
  override async countUsers(args: sdk.FindUsersArgs): Promise<number> {
    return await this.getCount(this.findUsersQuery(args))
  }
  override async countMonitorEvents(args: sdk.FindMonitorEventsArgs): Promise<number> {
    return await this.getCount(this.findMonitorEventsQuery(args))
  }
 
  override async destroy(): Promise<void> {
    await this.knex?.destroy()
  }
 
  override async migrate(storageName: string, storageIdentityKey: string): Promise<string> {
    const config = {
      migrationSource: new KnexMigrations(this.chain, storageName, storageIdentityKey, 1024)
    }
    await this.knex.migrate.latest(config)
    const version = await this.knex.migrate.currentVersion(config)
    return version
  }
 
  override async dropAllData(): Promise<void> {
    // Only using migrations to migrate down, don't need valid properties for settings table.
    const config = { migrationSource: new KnexMigrations('test', '', '', 1024) }
    const count = Object.keys(config.migrationSource.migrations).length
    for (let i = 0; i < count; i++) {
      try {
        const r = await this.knex.migrate.down(config)
        expect(r).toBeTruthy()
      } catch (eu: unknown) {
        break
      }
    }
  }
 
  override async transaction<T>(scope: (trx: sdk.TrxToken) => Promise<T>, trx?: sdk.TrxToken): Promise<T> {
    if (trx) return await scope(trx)
 
    return await this.knex.transaction<T>(async knextrx => {
      const trx = knextrx as sdk.TrxToken
      return await scope(trx)
    })
  }
 
  /**
   * Convert the standard optional `TrxToken` parameter into either a direct knex database instance,
   * or a Knex.Transaction as appropriate.
   */
  toDb(trx?: sdk.TrxToken) {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const db = !trx ? this.knex : <Knex.Transaction<any, any[]>>trx
    this.whenLastAccess = new Date()
    return db
  }
 
  async validateRawTransaction(t: table.Transaction, trx?: sdk.TrxToken): Promise<void> {
    // if there is no txid or there is a rawTransaction return what we have.
    if (t.rawTx || !t.txid) return
 
    // rawTransaction is missing, see if we moved it ...
 
    const rawTx = await this.getRawTxOfKnownValidTransaction(t.txid, undefined, undefined, trx)
    if (!rawTx) return
    t.rawTx = rawTx
  }
 
  async validateOutputScript(o: table.Output, trx?: sdk.TrxToken): Promise<void> {
    // without offset and length values return what we have (make no changes)
    if (!o.scriptLength || !o.scriptOffset || !o.txid) return
    // if there is an outputScript and its length is the expected length return what we have.
    if (o.lockingScript && o.lockingScript.length === o.scriptLength) return
 
    // outputScript is missing or has incorrect length...
 
    const script = await this.getRawTxOfKnownValidTransaction(o.txid, o.scriptOffset, o.scriptLength, trx)
    if (!script) return
    o.lockingScript = script
  }
 
  _verifiedReadyForDatabaseAccess: boolean = false
 
  /**
   * Make sure database is ready for access:
   *
   * - dateScheme is known
   * - foreign key constraints are enabled
   *
   * @param trx
   */
  async verifyReadyForDatabaseAccess(trx?: sdk.TrxToken): Promise<DBType> {
    if (!this._settings) {
      this._settings = await this.readSettings()
    }
 
    if (!this._verifiedReadyForDatabaseAccess) {
 
      // Make sure foreign key constraint checking is turned on in SQLite.
      if (this._settings.dbtype === 'SQLite') {
        await this.toDb(trx).raw('PRAGMA foreign_keys = ON;')
      }
 
      this._verifiedReadyForDatabaseAccess = true
    }
 
    return this._settings.dbtype
  }
 
  /**
   * Helper to force uniform behavior across database engines.
   * Use to process the update template for entities being updated.
   */
  validatePartialForUpdate<T extends sdk.EntityTimeStamp>(update: Partial<T>, dateFields?: string[], booleanFields?: string[]): Partial<T> {
    Iif (!this.dbtype) throw new sdk.WERR_INTERNAL('must call verifyReadyForDatabaseAccess first')
    const v: any = update
    if (v.created_at) v.created_at = this.validateEntityDate(v.created_at)
    if (v.updated_at) v.updated_at = this.validateEntityDate(v.updated_at)
    if (!v.created_at) delete v.created_at
    if (!v.updated_at) v.updated_at = this.validateEntityDate(new Date())
 
    if (dateFields) {
      for (const df of dateFields) {
        if (v[df]) v[df] = this.validateOptionalEntityDate(v[df])
      }
    }
    if (booleanFields) {
      for (const df of booleanFields) {
        if (update[df] !== undefined) update[df] = !!update[df] ? 1 : 0
      }
    }
    for (const key of Object.keys(v)) {
      const val = v[key]
      if (Array.isArray(val) && (val.length === 0 || typeof val[0] === 'number')) {
        v[key] = Buffer.from(val)
      } else if (val === undefined) {
        v[key] = null
      }
    }
    this.isDirty = true
    return v
  }
 
  /**
   * Helper to force uniform behavior across database engines.
   * Use to process new entities being inserted into the database.
   */
  async validateEntityForInsert<T extends sdk.EntityTimeStamp>(entity: T, trx?: sdk.TrxToken, dateFields?: string[], booleanFields?: string[]): Promise<any> {
    await this.verifyReadyForDatabaseAccess(trx)
    const v: any = { ...entity }
    v.created_at = this.validateOptionalEntityDate(v.created_at, true)!
    v.updated_at = this.validateOptionalEntityDate(v.updated_at, true)!
    Iif (!v.created_at) delete v.created_at
    Iif (!v.updated_at) delete v.updated_at
    if (dateFields) {
      for (const df of dateFields) {
        Iif (v[df]) v[df] = this.validateOptionalEntityDate(v[df])
      }
    }
    if (booleanFields) {
      for (const df of booleanFields) {
        if (entity[df] !== undefined) entity[df] = !!entity[df] ? 1 : 0
      }
    }
    for (const key of Object.keys(v)) {
      const val = v[key]
      if (Array.isArray(val) && (val.length === 0 || typeof val[0] === 'number')) {
        v[key] = Buffer.from(val)
      } else if (val === undefined) {
        v[key] = null
      }
    }
    this.isDirty = true
    return v
  }
 
  override async getLabelsForTransactionId(transactionId?: number, trx?: sdk.TrxToken): Promise<table.TxLabel[]> {
    Iif (transactionId === undefined) return []
    const labels = await this.toDb(trx)<table.TxLabel>('tx_labels')
      .join('tx_labels_map', 'tx_labels_map.txLabelId', 'tx_labels.txLabelId')
      .where('tx_labels_map.transactionId', transactionId)
      .whereNot('tx_labels_map.isDeleted', true)
      .whereNot('tx_labels.isDeleted', true)
    return this.validateEntities(labels, undefined, ['isDeleted'])
  }
 
  async extendOutput(o: table.Output, includeBasket = false, includeTags = false, trx?: sdk.TrxToken): Promise<table.OutputX> {
    const ox = o as table.OutputX
    if (includeBasket && ox.basketId) ox.basket = await this.findOutputBasketById(o.basketId!, trx)
    if (includeTags) {
      ox.tags = await this.getTagsForOutputId(o.outputId)
    }
    return o
  }
 
  override async getTagsForOutputId(outputId: number, trx?: sdk.TrxToken): Promise<table.OutputTag[]> {
    const tags = await this.toDb(trx)<table.OutputTag>('output_tags')
      .join('output_tags_map', 'output_tags_map.outputTagId', 'output_tags.outputTagId')
      .where('output_tags_map.outputId', outputId)
      .whereNot('output_tags_map.isDeleted', true)
      .whereNot('output_tags.isDeleted', true)
    return this.validateEntities(tags, undefined, ['isDeleted'])
  }
 
  override async purgeData(params: sdk.PurgeParams, trx?: sdk.TrxToken): Promise<sdk.PurgeResults> {
    return await purgeData(this, params, trx)
  }
 
  /**
   *  Finds closest matching available change output to use as input for new transaction.
   *
   * Transactionally allocate the output such that
   */
  async countChangeInputs(userId: number, basketId: number, excludeSending: boolean): Promise<number> {
    const status: sdk.TransactionStatus[] = ['completed', 'unproven']
    if (!excludeSending) status.push('sending')
    const statusText = status.map(s => `'${s}'`).join(',')
    const txStatusCondition = `(SELECT status FROM transactions WHERE outputs.transactionId = transactions.transactionId) in (${statusText})`
    let q = this.knex<table.Output>('outputs').where({ userId, spendable: true, basketId }).whereRaw(txStatusCondition)
    const count = await this.getCount(q)
    return count
  }
 
  /**
   *  Finds closest matching available change output to use as input for new transaction.
   *
   * Transactionally allocate the output such that
   */
  async allocateChangeInput(userId: number, basketId: number, targetSatoshis: number, exactSatoshis: number | undefined, excludeSending: boolean, transactionId: number): Promise<table.Output | undefined> {
    const status: sdk.TransactionStatus[] = ['completed', 'unproven']
    if (!excludeSending) status.push('sending')
    const statusText = status.map(s => `'${s}'`).join(',')
 
    const r: table.Output | undefined = await this.knex.transaction(async trx => {
      const txStatusCondition = `AND (SELECT status FROM transactions WHERE outputs.transactionId = transactions.transactionId) in (${statusText})`
 
      let outputId: number | undefined
      const setOutputId = async (rawQuery: string): Promise<void> => {
        let oidr = await trx.raw(rawQuery)
        outputId = undefined
        if (!oidr['outputId'] && oidr.length > 0) oidr = oidr[0]
        Iif (!oidr['outputId'] && oidr.length > 0) oidr = oidr[0]
        if (oidr['outputId']) outputId = Number(oidr['outputId'])
      }
 
      Iif (exactSatoshis !== undefined) {
        // Find outputId of output that with exactSatoshis
        await setOutputId(`
                SELECT outputId 
                FROM outputs
                WHERE userId = ${userId} 
                    AND spendable = 1 
                    AND basketId = ${basketId}
                    ${txStatusCondition}
                    AND satoshis = ${exactSatoshis}
                LIMIT 1;
                `)
      }
 
      if (outputId === undefined) {
        // Find outputId of output that would at least fund targetSatoshis
        await setOutputId(`
                    SELECT outputId 
                    FROM outputs
                    WHERE userId = ${userId} 
                        AND spendable = 1 
                        AND basketId = ${basketId}
                        ${txStatusCondition}
                        AND satoshis - ${targetSatoshis} = (
                            SELECT MIN(satoshis - ${targetSatoshis}) 
                            FROM outputs 
                            WHERE userId = ${userId} 
                            AND spendable = 1 
                            AND basketId = ${basketId}
                            ${txStatusCondition}
                            AND satoshis - ${targetSatoshis} >= 0
                        )
                    LIMIT 1;
                    `)
      }
 
      Iif (outputId === undefined) {
        // Find outputId of output that would add the most fund targetSatoshis
        await setOutputId(`
                    SELECT outputId 
                    FROM outputs
                    WHERE userId = ${userId} 
                        AND spendable = 1 
                        AND basketId = ${basketId}
                        ${txStatusCondition}
                        AND satoshis - ${targetSatoshis} = (
                            SELECT MAX(satoshis - ${targetSatoshis}) 
                            FROM outputs 
                            WHERE userId = ${userId} 
                            AND spendable = 1 
                            AND basketId = ${basketId}
                            ${txStatusCondition}
                            AND satoshis - ${targetSatoshis} < 0
                        )
                    LIMIT 1;
                    `)
      }
 
      Iif (outputId === undefined) return undefined
 
      await this.updateOutput(
        outputId,
        {
          spendable: false,
          spentBy: transactionId
        },
        trx
      )
 
      const r = verifyTruthy(await this.findOutputById(outputId, trx))
      return r
    })
 
    return r
  }
 
  /**
   * Helper to force uniform behavior across database engines.
   * Use to process all individual records with time stamps retreived from database.
   */
  validateEntity<T extends sdk.EntityTimeStamp>(
    entity: T,
    dateFields?: string[],
    booleanFields?: string[]
  ): T {
    entity.created_at = this.validateDate(entity.created_at)
    entity.updated_at = this.validateDate(entity.updated_at)
    if (dateFields) {
      for (const df of dateFields) {
        if (entity[df])
          entity[df] = this.validateDate(entity[df])
      }
    }
    if (booleanFields) {
      for (const df of booleanFields) {
        if (entity[df] !== undefined)
          entity[df] = !!(entity[df])
      }
    }
    for (const key of Object.keys(entity)) {
      const val = entity[key]
      if (val === null) {
        entity[key] = undefined
      } else if (Buffer.isBuffer(val)) {
        entity[key] = Array.from(val)
      }
    }
    return entity
  }
 
  /**
   * Helper to force uniform behavior across database engines.
   * Use to process all arrays of records with time stamps retreived from database.
   * @returns input `entities` array with contained values validated.
   */
  validateEntities<T extends sdk.EntityTimeStamp>(entities: T[], dateFields?: string[], booleanFields?: string[]): T[] {
    for (let i = 0; i < entities.length; i++) {
      entities[i] = this.validateEntity(entities[i], dateFields, booleanFields)
    }
    return entities
  }
 
}