@siglume/direct-request-payment 0.4.24 → 0.4.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -60,6 +60,12 @@ interface SqlParts {
60
60
  readonly qReviews: string;
61
61
  }
62
62
 
63
+ interface TypeOrmQueryRunner {
64
+ connect?: () => Promise<void>;
65
+ query?: (statement: string, params?: readonly unknown[], useStructuredResult?: boolean) => Promise<unknown>;
66
+ release?: () => Promise<void>;
67
+ }
68
+
63
69
  const CHECKOUT_CREATION_LEASE_MS = 30_000;
64
70
  const CHECKOUT_CREATION_WAIT_MS = 10_000;
65
71
  const CHECKOUT_CREATION_POLL_MS = 100;
@@ -216,6 +222,8 @@ export function createPrismaSiglumeSqlExecutor(prisma: unknown): SiglumeSqlExecu
216
222
  export function createTypeOrmSiglumeSqlExecutor(dataSource: unknown): SiglumeSqlExecutor {
217
223
  const source = dataSource as {
218
224
  query?: (statement: string, params?: readonly unknown[]) => Promise<unknown>;
225
+ queryRunner?: TypeOrmQueryRunner | null;
226
+ createQueryRunner?: () => TypeOrmQueryRunner;
219
227
  transaction?: <T>(handler: (manager: { query: (statement: string, params?: readonly unknown[]) => Promise<unknown> }) => Promise<T>) => Promise<T>;
220
228
  };
221
229
  return {
@@ -223,6 +231,15 @@ export function createTypeOrmSiglumeSqlExecutor(dataSource: unknown): SiglumeSql
223
231
  return normalizeRows(await source.query?.(statement, params)) as T[];
224
232
  },
225
233
  async execute(statement, params = []) {
234
+ const runner = source.queryRunner ?? source.createQueryRunner?.();
235
+ if (runner?.query) {
236
+ try {
237
+ if (!source.queryRunner) await runner.connect?.();
238
+ return normalizeTypeOrmExecuteResult(await runner.query(statement, params, true));
239
+ } finally {
240
+ if (!source.queryRunner) await runner.release?.();
241
+ }
242
+ }
226
243
  return source.query?.(statement, params);
227
244
  },
228
245
  async transaction(handler) {
@@ -371,7 +388,7 @@ class SqlSiglumeOrderStore implements SiglumeSdrpOrderStore {
371
388
  await this.executor().execute(
372
389
  `UPDATE ${parts.qAttempts}
373
390
  SET status = ${this.p(1)}, stable_nonce = ${this.p(2)}, challenge_hash = ${this.p(3)},
374
- checkout_session_id = ${this.p(4)}, checkout_url = ${this.p(5)}, expires_at = ${this.p(6)},
391
+ checkout_session_id = ${this.p(4)}, checkout_url = ${this.p(5)}, expires_at = ${timestampPlaceholder(6, this.options)},
375
392
  creation_owner_id = NULL, creation_lease_expires_at = NULL, error_message = NULL,
376
393
  updated_at = CURRENT_TIMESTAMP
377
394
  WHERE order_id = ${this.p(7)} AND attempt_id = ${this.p(8)} AND status = ${this.p(9)}`,
@@ -660,7 +677,7 @@ function insertAttemptSql(options: NormalizedOptions): string {
660
677
  VALUES (${placeholder(1, options)}, ${placeholder(2, options)}, ${placeholder(3, options)}, ${placeholder(4, options)}, ${placeholder(5, options)}, ${placeholder(6, options)}, ${placeholder(7, options)}, ${placeholder(8, options)})`;
661
678
  }
662
679
  return `INSERT INTO ${parts.qAttempts} (order_id, attempt_number, attempt_id, stable_nonce, active_key, status, creation_owner_id, creation_lease_expires_at)
663
- VALUES (${placeholder(1, options)}, ${placeholder(2, options)}, ${placeholder(3, options)}, ${placeholder(4, options)}, ${placeholder(5, options)}, ${placeholder(6, options)}, ${placeholder(7, options)}, ${placeholder(8, options)})
680
+ VALUES (${placeholder(1, options)}, ${placeholder(2, options)}, ${placeholder(3, options)}, ${placeholder(4, options)}, ${placeholder(5, options)}, ${placeholder(6, options)}, ${placeholder(7, options)}, ${timestampPlaceholder(8, options)})
664
681
  ON CONFLICT (active_key) DO NOTHING`;
665
682
  }
666
683
 
@@ -676,6 +693,11 @@ function placeholder(index: number, options: NormalizedOptions): string {
676
693
  return options.param_style === "numbered" ? `$${index}` : "?";
677
694
  }
678
695
 
696
+ function timestampPlaceholder(index: number, options: NormalizedOptions): string {
697
+ const value = placeholder(index, options);
698
+ return options.dialect === "postgres" ? `CAST(${value} AS TIMESTAMPTZ)` : value;
699
+ }
700
+
679
701
  function quoteIdentifier(identifier: string, dialect: SiglumeSqlDialect): string {
680
702
  const quote = dialect === "mysql" ? "`" : "\"";
681
703
  return identifier.split(".").map((part) => {
@@ -764,7 +786,10 @@ function sleep(ms: number): Promise<void> {
764
786
  }
765
787
 
766
788
  function normalizeRows(value: unknown): Record<string, unknown>[] {
767
- if (Array.isArray(value)) return value as Record<string, unknown>[];
789
+ if (Array.isArray(value)) {
790
+ if (Array.isArray(value[0])) return value[0] as Record<string, unknown>[];
791
+ return value as Record<string, unknown>[];
792
+ }
768
793
  if (value && typeof value === "object" && Array.isArray((value as { rows?: unknown[] }).rows)) {
769
794
  return (value as { rows: Record<string, unknown>[] }).rows;
770
795
  }
@@ -773,14 +798,30 @@ function normalizeRows(value: unknown): Record<string, unknown>[] {
773
798
 
774
799
  function affectedRows(value: unknown): number | null {
775
800
  if (typeof value === "number") return value;
801
+ if (Array.isArray(value)) {
802
+ for (const item of value) {
803
+ const changed = affectedRows(item);
804
+ if (changed !== null) return changed;
805
+ }
806
+ return null;
807
+ }
776
808
  if (!value || typeof value !== "object") return null;
777
809
  const record = value as Record<string, unknown>;
778
- for (const key of ["rowCount", "affectedRows", "changes"]) {
810
+ for (const key of ["rowCount", "affectedRows", "changes", "affected"]) {
779
811
  if (typeof record[key] === "number") return record[key] as number;
780
812
  }
781
813
  return null;
782
814
  }
783
815
 
816
+ function normalizeTypeOrmExecuteResult(value: unknown): unknown {
817
+ if (!value || typeof value !== "object" || Array.isArray(value)) return value;
818
+ const record = value as Record<string, unknown>;
819
+ if (typeof record.affected === "number" && typeof record.rowCount !== "number") {
820
+ return { ...record, rowCount: record.affected };
821
+ }
822
+ return value;
823
+ }
824
+
784
825
  function toDrizzleStatement(
785
826
  sqlTag: {
786
827
  (strings: TemplateStringsArray, ...params: unknown[]): unknown;