@cfxlabsinc/b2b-services
    Preparing search index...

    A collection of helpers for aggressively common patterns used in our services. Instantiated automatically by createDrizzle and attached as db.helpers.

    Index

    Constructors

    • Creates a new DbHelper instance

      Parameters

      • params: { db: PostgresJsDatabase<__module> }

        Configuration object

        • db: PostgresJsDatabase<__module>

          The B2B database instance to use for queries

      Returns DbHelper

    Properties

    db: PostgresJsDatabase<__module>

    Methods

    • Returns a scalar subquery resolving an ACTIVE, non-deleted product route's internal id from its external id. Used by rule-admin services to attach rules to routes inline in INSERT — if no row matches, the subquery yields NULL and the FK on route_id rejects the insert.

      Parameters

      Returns SQL<number>

    • Returns a scalar subquery resolving a bank's internal id from its external id (the literal BankId). Used inline in INSERT/UPDATE to populate bank_id int FK columns without a separate round trip.

      Parameters

      Returns SQL<number>

    • Returns a scalar subquery (SELECT id FROM customer WHERE external_id = ?) wrapped in parens, usable anywhere an SQL<number> is accepted — including insert values, eq(col, ...) conditions, and inArray(col, ...).

      Parameters

      Returns SQL<number>

    • Resolve the external id of the owning entity (identity or organization) for a row that left-joins both identity and organization. Returns NULL when neither side is joined.

      Caller is responsible for the joins — see LedgerAccountService.search for the canonical pattern.

      Returns SQL<string>

    • Resolve the display name of the owning entity (identity or organization) for a row that left-joins both identity and organization. Identity names are formatted via identityFullName; organization names use organization.name directly. Returns NULL when neither side is joined.

      Returns SQL<string>

    • Resolve the customer-supplied reference id of the owning entity (identity or organization) for a row that left-joins both identity and organization. Returns NULL when neither side has a reference id set.

      Returns SQL<string | null>

    • Resolve the type tag ('IDENTITY' | 'ORGANIZATION') of the owning entity for a row that left-joins both identity and organization. Returns NULL when neither side is joined.

      Returns SQL<"IDENTITY" | "ORGANIZATION">

    • Format the jsonb identity.name column ({firstName, middleName?, lastName}) as a single text value: "First [Middle] Last". Returns NULL when the column is NULL or the resulting trimmed string is empty.

      Pair with coalesce() to derive an "owning entity name" projection that falls back to organization / customer names — see LedgerAccountService.search for the canonical pattern.

      Parameters

      • nameColumn: SQLWrapper

      Returns SQL

    • Returns a scalar subquery resolving an identity's internal id from its external id, scoped to the owning customer.

      Parameters

      • __namedParameters: { customerId: string; identityId: string; tx?: B2bPgTransaction }

      Returns SQL<number>

    • Returns a scalar subquery resolving a ledger account's internal id from its external id, optionally scoped to the owning customer.

      Ledger accounts are owned by exactly one customer — pass customerId whenever the caller knows the owning customer, for defense-in-depth against cross-tenant IDOR. Omit customerId only for system-managed accounts (e.g. master fee accounts owned by the platform) where the caller doesn't naturally know the owning customer.

      Parameters

      • __namedParameters: { customerId?: string; ledgerAccountId: string; tx?: B2bPgTransaction }

      Returns SQL<number>

    • Returns a scalar subquery resolving an organization's internal id from its external id, scoped to the owning customer.

      Parameters

      • __namedParameters: { customerId: string; organizationId: string; tx?: B2bPgTransaction }

      Returns SQL<number>

    • Returns a scalar subquery resolving a product's internal id from its external id. Accepts any ProductName, including the routing wildcards (deposit.* / withdraw.*), all of which exist as rows in b2b.product.

      Parameters

      • __namedParameters: {
            productName:
                | "organization.v1"
                | "identity.v1"
                | "card.physical_card.v1"
                | "card.virtual_card.v1"
                | "deposit.us_cash.v1"
                | "deposit.rtp.v1"
                | "deposit.us_bank_ach.v1"
                | "deposit.ach_credit.v1"
                | "deposit.us_wire.v1"
                | "deposit.swift_wire.v1"
                | "transfer.redemption.v1"
                | "swap.v1"
                | "withdraw.blockchain.v1"
                | "withdraw.ke_bank.v1"
                | "withdraw.ke_momo.v1"
                | "withdraw.mx_bank_spei.v1"
                | "withdraw.swift_wire.v1"
                | "withdraw.tg_momo.v1"
                | "withdraw.us_bank_ach.v1"
                | "withdraw.us_instant.v1"
                | "withdraw.us_wire.v1"
                | "withdraw.ach_pull.v1"
                | "withdraw.us_wire_drawdown.v1"
                | "account.virtual-account.v1"
                | "deposit.*"
                | "withdraw.*";
            tx?: B2bPgTransaction;
        }

      Returns SQL<number>

    • Returns a scalar subquery resolving a product_quote's internal id from its external id (the public-facing quoteId nanoid). Used by rail-quote services (deposit/withdrawal) to populate product_quote_id int FK columns from the string quoteId returned by ProductQuoteService.quote(), without an extra round trip. Optional customerId adds defense-in-depth cross-tenant scoping when the caller knows the owning customer.

      Parameters

      • __namedParameters: { customerId?: string; quoteId: string; tx?: B2bPgTransaction }

      Returns SQL<number>