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

    A collection of helpers for aggressively common patterns used in our services

    Instantiate this in your service class, do not expose to outside

    Index

    Constructors

    Properties

    Methods

    • Usage:

      db.select().from(myTable).where(
      eq(
      myTable.customerId,
      dbHelper.customerInternalId({ customerId })
      )
      )

      Parameters

      • params: { customerId: string; tx?: B2bPgTransaction }

        Query parameters

        • customerId: string

          Customer external ID

        • Optionaltx?: B2bPgTransaction

          Optional database transaction to use

      Returns Omit<
          PgSelectBase<
              "customer",
              {
                  id: PgColumn<
                      {
                          baseColumn: never;
                          columnType: "PgInteger";
                          data: number;
                          dataType: "number";
                          driverParam: string
                          | number;
                          enumValues: undefined;
                          generated: undefined;
                          hasDefault: true;
                          hasRuntimeDefault: false;
                          identity: "byDefault";
                          isAutoincrement: false;
                          isPrimaryKey: true;
                          name: "id";
                          notNull: true;
                          tableName: "customer";
                      },
                      {},
                      {},
                  >;
              },
              "partial",
              Record<"customer", "not-null">,
              false,
              "where",
              { id: number }[],
              {
                  id: PgColumn<
                      {
                          baseColumn: never;
                          columnType: "PgInteger";
                          data: number;
                          dataType: "number";
                          driverParam: string
                          | number;
                          enumValues: undefined;
                          generated: undefined;
                          hasDefault: true;
                          hasRuntimeDefault: false;
                          identity: "byDefault";
                          isAutoincrement: false;
                          isPrimaryKey: true;
                          name: "id";
                          notNull: true;
                          tableName: "customer";
                      },
                      {},
                      {},
                  >;
              },
          >,
          "where",
      >

      Query that returns internal ID for customer

    • Helper for "with ... insert" where we need to provide identity or organization id

      This is suitable for tables that store entity references as mutually-exclusive populated identity ID and organization ID.

      Usage:

      const { entityWith, entityInsertFields } = dbHelper.entityInsertWith({
      entityId,
      customerId,
      });
      const results = await this.db
      .with(entityWith)
      .insert(myTable)
      .values({
      ...input,
      ...entityInsertFields,
      });

      You may need to rename entityInsertFields if you are not using the standard identity_id and organization_id columns in your table, or omit customer_id if this is not used in your table.

      Parameters

      • params: { customerId: string; entityId: string; name?: string; tx?: B2bPgTransaction }

        Query parameters

        • customerId: string

          Customer external ID that owns the entity

        • entityId: string

          Either identity or organization external ID

        • Optionalname?: string

          Name for the WITH clause (defaults to "entity")

        • Optionaltx?: B2bPgTransaction

          Optional database transaction to use

      Returns {
          entityInsertFields: {
              customerId: SQL<unknown>;
              identityId: SQL<unknown> | null;
              organizationId: SQL<unknown> | null;
          };
          entityWith: WithSubqueryWithSelection<any, string>;
      }

      Object containing entityWith that can be plugged into WITH clause and entityInsertFields fields that can be used in the VALUES of INSERT

    • Usage:

      db.select().from(myTable).where(
      eq(
      myTable.identityId,
      dbHelper.identityInternalId({ identityId, customerId })
      )
      )

      Parameters

      • params: {
            customerId: string;
            identityId: string;
            includeCustomerInternalId?: boolean;
            tx?: B2bPgTransaction;
        }

        Query parameters

        • customerId: string

          Customer external ID that owns the identity

        • identityId: string

          Identity external ID

        • OptionalincludeCustomerInternalId?: boolean

          Whether to include customer internal ID in result

        • Optionaltx?: B2bPgTransaction

          Optional database transaction to use

      Returns Omit<
          PgSelectBase<
              "identity",
              {
                  customerId?: PgColumn<
                      {
                          baseColumn: never;
                          columnType: "PgInteger";
                          data: number;
                          dataType: "number";
                          driverParam: string
                          | number;
                          enumValues: undefined;
                          generated: undefined;
                          hasDefault: false;
                          hasRuntimeDefault: false;
                          identity: undefined;
                          isAutoincrement: false;
                          isPrimaryKey: false;
                          name: "customerId";
                          notNull: true;
                          tableName: "identity";
                      },
                      {},
                      {},
                  >;
                  id: PgColumn<
                      {
                          baseColumn: never;
                          columnType: "PgInteger";
                          data: number;
                          dataType: "number";
                          driverParam: string
                          | number;
                          enumValues: undefined;
                          generated: undefined;
                          hasDefault: true;
                          hasRuntimeDefault: false;
                          identity: "byDefault";
                          isAutoincrement: false;
                          isPrimaryKey: true;
                          name: "id";
                          notNull: true;
                          tableName: "identity";
                      },
                      {},
                      {},
                  >;
              },
              "partial",
              Record<"identity", "not-null"> & { customer: "not-null" },
              false,
              "where",
              { customerId?: number; id: number }[],
              {
                  customerId?: undefined;
                  id: PgColumn<
                      {
                          baseColumn: never;
                          columnType: "PgInteger";
                          data: number;
                          dataType: "number";
                          driverParam: string
                          | number;
                          enumValues: undefined;
                          generated: undefined;
                          hasDefault: true;
                          hasRuntimeDefault: false;
                          identity: "byDefault";
                          isAutoincrement: false;
                          isPrimaryKey: true;
                          name: "id";
                          notNull: true;
                          tableName: "identity";
                      },
                      {},
                      {},
                  >;
              },
          >,
          "where",
      >

      Query that returns internal ID for identity, belonging to given customer

    • Usage:

      db.select().from(myTable).where(
      eq(
      myTable.organizationId,
      dbHelper.organizationInternalId({ organizationId, customerId })
      )
      )

      Parameters

      • params: {
            customerId: string;
            includeCustomerInternalId?: boolean;
            organizationId: string;
            tx?: B2bPgTransaction;
        }

        Query parameters

        • customerId: string

          Customer external ID that owns the organization

        • OptionalincludeCustomerInternalId?: boolean

          Whether to include customer internal ID in result

        • organizationId: string

          Organization external ID

        • Optionaltx?: B2bPgTransaction

          Optional database transaction to use

      Returns Omit<
          PgSelectBase<
              "organization",
              {
                  customerId?: PgColumn<
                      {
                          baseColumn: never;
                          columnType: "PgInteger";
                          data: number;
                          dataType: "number";
                          driverParam: string
                          | number;
                          enumValues: undefined;
                          generated: undefined;
                          hasDefault: false;
                          hasRuntimeDefault: false;
                          identity: undefined;
                          isAutoincrement: false;
                          isPrimaryKey: false;
                          name: "customerId";
                          notNull: true;
                          tableName: "organization";
                      },
                      {},
                      {},
                  >;
                  id: PgColumn<
                      {
                          baseColumn: never;
                          columnType: "PgInteger";
                          data: number;
                          dataType: "number";
                          driverParam: string
                          | number;
                          enumValues: undefined;
                          generated: undefined;
                          hasDefault: true;
                          hasRuntimeDefault: false;
                          identity: "byDefault";
                          isAutoincrement: false;
                          isPrimaryKey: true;
                          name: "id";
                          notNull: true;
                          tableName: "organization";
                      },
                      {},
                      {},
                  >;
              },
              "partial",
              Record<"organization", "not-null"> & { customer: "not-null" },
              false,
              "where",
              { customerId?: number; id: number }[],
              {
                  customerId?: undefined;
                  id: PgColumn<
                      {
                          baseColumn: never;
                          columnType: "PgInteger";
                          data: number;
                          dataType: "number";
                          driverParam: string
                          | number;
                          enumValues: undefined;
                          generated: undefined;
                          hasDefault: true;
                          hasRuntimeDefault: false;
                          identity: "byDefault";
                          isAutoincrement: false;
                          isPrimaryKey: true;
                          name: "id";
                          notNull: true;
                          tableName: "organization";
                      },
                      {},
                      {},
                  >;
              },
          >,
          "where",
      >

      Query that returns internal ID for organization