@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

      • __namedParameters: {
            routeId: string;
            tx?: PgAsyncTransaction<
                PostgresJsQueryResultHKT,
                __module,
                EmptyRelations,
                ExtractTablesWithRelations<__module>,
            >;
        }

      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

      • __namedParameters: {
            bankId: string;
            tx?: PgAsyncTransaction<
                PostgresJsQueryResultHKT,
                __module,
                EmptyRelations,
                ExtractTablesWithRelations<__module>,
            >;
        }

      Returns SQL<number>

    • Returns a scalar subquery resolving a chain's internal id from its canonical lowercase key (e.g. "solana", "avalanche-c-chain"). Used inline in INSERT/UPDATE to populate chain_id int FK columns without a separate round trip.

      Parameters

      • __namedParameters: {
            key: string;
            tx?: PgAsyncTransaction<
                PostgresJsQueryResultHKT,
                __module,
                EmptyRelations,
                ExtractTablesWithRelations<__module>,
            >;
        }

      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

      • __namedParameters: {
            customerId: string;
            tx?: PgAsyncTransaction<
                PostgresJsQueryResultHKT,
                __module,
                EmptyRelations,
                ExtractTablesWithRelations<__module>,
            >;
        }

      Returns SQL<number>

    • Resolve the country of the owning entity (identity or organization) for a row that left-joins both identity and organization. Identities resolve to their country of residence, organizations to their country of incorporation. Both source columns are NOT NULL and the owner is mutually exclusive, so this is non-null whenever an owner is joined.

      Returns SQL<string>

    • Resolve an always-present display label for the owning entity (identity or organization) for a row that left-joins both identity and organization. Identities fall back full name → email → external id (mirrors the identity service's displayName); organizations use organization.name. Non-null whenever an owner is joined.

      Returns SQL<string>

    • 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?: PgAsyncTransaction<
                PostgresJsQueryResultHKT,
                __module,
                EmptyRelations,
                ExtractTablesWithRelations<__module>,
            >;
        }

      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?: PgAsyncTransaction<
                PostgresJsQueryResultHKT,
                __module,
                EmptyRelations,
                ExtractTablesWithRelations<__module>,
            >;
        }

      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?: PgAsyncTransaction<
                PostgresJsQueryResultHKT,
                __module,
                EmptyRelations,
                ExtractTablesWithRelations<__module>,
            >;
        }

      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?: PgAsyncTransaction<
                PostgresJsQueryResultHKT,
                __module,
                EmptyRelations,
                ExtractTablesWithRelations<__module>,
            >;
        }

      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?: PgAsyncTransaction<
                PostgresJsQueryResultHKT,
                __module,
                EmptyRelations,
                ExtractTablesWithRelations<__module>,
            >;
        }

      Returns SQL<number>

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

      Parameters

      • __namedParameters: {
            customerId: string;
            relatedPersonId: string;
            tx?: PgAsyncTransaction<
                PostgresJsQueryResultHKT,
                __module,
                EmptyRelations,
                ExtractTablesWithRelations<__module>,
            >;
        }

      Returns SQL<number>

    • Returns a scalar subquery resolving a virtual-account provider's internal id from its external id. Used inline in INSERT to populate the provider_id int FK on virtual_account without a separate round trip.

      Parameters

      • __namedParameters: {
            providerId: string;
            tx?: PgAsyncTransaction<
                PostgresJsQueryResultHKT,
                __module,
                EmptyRelations,
                ExtractTablesWithRelations<__module>,
            >;
        }

      Returns SQL<number>