index.js 814 Bytes
import { createActionPolicyRepo } from './action-policy-repo.js'
import { createLedgerRepo } from './ledger-repo.js'
import { createPaywallRepo } from './paywall-repo.js'
import { createProductRepo } from './product-repo.js'

export function createBillingRepo(db, runner = db) {
  if (typeof db.transaction !== 'function') throw new Error('[billing] createBillingRepo requires a Knex db')
  return {
    ...createLedgerRepo(db, runner),
    ...createProductRepo(db, runner),
    ...createActionPolicyRepo(db, runner),
    ...createPaywallRepo(db, runner),
    afterCommit(work) {
      if (runner === db) {
        globalThis.queueMicrotask(work)
        return
      }
      runner.executionPromise.then(work, () => undefined)
    },
    forTransaction(trx) {
      return createBillingRepo(db, trx)
    },
  }
}