ponder 0.9.4-debug.1 → 0.9.4

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.
@@ -1,27 +1,15 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
+ createQueue,
4
+ dedupe,
3
5
  getMetadataStore,
4
6
  never,
7
+ promiseWithResolvers,
5
8
  serialize
6
- } from "../chunk-DZFRP3KH.js";
9
+ } from "../chunk-LHCA5XFV.js";
7
10
  import {
8
- BuildError,
9
- NonRetryableError,
10
- ShutdownError,
11
- createHistoricalIndexingStore,
12
- createRealtimeIndexingStore,
13
- getColumnCasing,
14
- getPrimaryKeyColumns,
15
- getSql,
16
- getTableNames,
17
- prettyPrint
18
- } from "../chunk-6AOFLZJ4.js";
19
- import {
20
- createQueue,
21
- dedupe,
22
- promiseWithResolvers
23
- } from "../chunk-MJKRYIBO.js";
24
- import "../chunk-K2TLRLX3.js";
11
+ onchain
12
+ } from "../chunk-K2TLRLX3.js";
25
13
 
26
14
  // src/bin/ponder.ts
27
15
  import { readFileSync as readFileSync4 } from "node:fs";
@@ -689,6 +677,106 @@ function hrTimeToMs(diff) {
689
677
  // src/internal/telemetry.ts
690
678
  import Conf from "conf";
691
679
  import { detect, getNpmVersion } from "detect-package-manager";
680
+
681
+ // src/internal/errors.ts
682
+ var BaseError = class _BaseError extends Error {
683
+ name = "BaseError";
684
+ meta = [];
685
+ constructor(message) {
686
+ super(message);
687
+ Object.setPrototypeOf(this, _BaseError.prototype);
688
+ }
689
+ };
690
+ function getBaseError(err) {
691
+ if (err instanceof BaseError)
692
+ return err;
693
+ if (err instanceof Error)
694
+ return new BaseError(err.message);
695
+ if (typeof err?.message === "string")
696
+ return new BaseError(err.message);
697
+ if (typeof err === "string")
698
+ return new BaseError(err);
699
+ return new BaseError("unknown error");
700
+ }
701
+ var BuildError = class _BuildError extends BaseError {
702
+ name = "BuildError";
703
+ constructor(message) {
704
+ super(message);
705
+ Object.setPrototypeOf(this, _BuildError.prototype);
706
+ }
707
+ };
708
+ var NonRetryableError = class _NonRetryableError extends BaseError {
709
+ name = "NonRetryableError";
710
+ constructor(message) {
711
+ super(message);
712
+ Object.setPrototypeOf(this, _NonRetryableError.prototype);
713
+ }
714
+ };
715
+ var UniqueConstraintError = class _UniqueConstraintError extends NonRetryableError {
716
+ name = "UniqueConstraintError";
717
+ constructor(message) {
718
+ super(message);
719
+ Object.setPrototypeOf(this, _UniqueConstraintError.prototype);
720
+ }
721
+ };
722
+ var NotNullConstraintError = class _NotNullConstraintError extends NonRetryableError {
723
+ name = "NotNullConstraintError";
724
+ constructor(message) {
725
+ super(message);
726
+ Object.setPrototypeOf(this, _NotNullConstraintError.prototype);
727
+ }
728
+ };
729
+ var RecordNotFoundError = class _RecordNotFoundError extends NonRetryableError {
730
+ name = "RecordNotFoundError";
731
+ constructor(message) {
732
+ super(message);
733
+ Object.setPrototypeOf(this, _RecordNotFoundError.prototype);
734
+ }
735
+ };
736
+ var CheckConstraintError = class _CheckConstraintError extends NonRetryableError {
737
+ name = "CheckConstraintError";
738
+ constructor(message) {
739
+ super(message);
740
+ Object.setPrototypeOf(this, _CheckConstraintError.prototype);
741
+ }
742
+ };
743
+ var InvalidStoreMethodError = class _InvalidStoreMethodError extends NonRetryableError {
744
+ name = "InvalidStoreMethodError";
745
+ constructor(message) {
746
+ super(message);
747
+ Object.setPrototypeOf(this, _InvalidStoreMethodError.prototype);
748
+ }
749
+ };
750
+ var UndefinedTableError = class _UndefinedTableError extends NonRetryableError {
751
+ name = "UndefinedTableError";
752
+ constructor(message) {
753
+ super(message);
754
+ Object.setPrototypeOf(this, _UndefinedTableError.prototype);
755
+ }
756
+ };
757
+ var BigIntSerializationError = class _BigIntSerializationError extends NonRetryableError {
758
+ name = "BigIntSerializationError";
759
+ constructor(message) {
760
+ super(message);
761
+ Object.setPrototypeOf(this, _BigIntSerializationError.prototype);
762
+ }
763
+ };
764
+ var FlushError = class _FlushError extends NonRetryableError {
765
+ name = "FlushError";
766
+ constructor(message) {
767
+ super(message);
768
+ Object.setPrototypeOf(this, _FlushError.prototype);
769
+ }
770
+ };
771
+ var ShutdownError = class _ShutdownError extends NonRetryableError {
772
+ name = "ShutdownError";
773
+ constructor(message) {
774
+ super(message);
775
+ Object.setPrototypeOf(this, _ShutdownError.prototype);
776
+ }
777
+ };
778
+
779
+ // src/internal/telemetry.ts
692
780
  var HEARTBEAT_INTERVAL_MS = 6e4;
693
781
  function createTelemetry({
694
782
  options,
@@ -2484,22 +2572,617 @@ function safeBuildPre({
2484
2572
  }
2485
2573
  }
2486
2574
 
2575
+ // src/drizzle/kit/index.ts
2576
+ import { SQL, is } from "drizzle-orm";
2577
+ import { CasingCache, toCamelCase, toSnakeCase } from "drizzle-orm/casing";
2578
+ import {
2579
+ PgDialect,
2580
+ PgEnumColumn,
2581
+ PgMaterializedView,
2582
+ PgSchema,
2583
+ PgTable,
2584
+ PgView,
2585
+ getTableConfig,
2586
+ integer,
2587
+ isPgEnum,
2588
+ isPgSequence,
2589
+ pgTable,
2590
+ serial,
2591
+ varchar
2592
+ } from "drizzle-orm/pg-core";
2593
+ var sqlToReorgTableName = (tableName) => `_reorg__${tableName}`;
2594
+ var getSql = (schema) => {
2595
+ const { tables, enums, schemas } = prepareFromExports(schema);
2596
+ const json = generatePgSnapshot(tables, enums, schemas, "snake_case");
2597
+ const squashed = squashPgScheme(json);
2598
+ const jsonCreateIndexesForCreatedTables = Object.values(
2599
+ squashed.tables
2600
+ ).flatMap((it) => {
2601
+ return preparePgCreateIndexesJson(it.name, it.schema, it.indexes);
2602
+ });
2603
+ const jsonCreateEnums = Object.values(squashed.enums).map((it) => {
2604
+ return prepareCreateEnumJson(it.name, it.schema, it.values);
2605
+ }) ?? [];
2606
+ const jsonCreateTables = Object.values(squashed.tables).map((it) => {
2607
+ return preparePgCreateTableJson(it, json);
2608
+ });
2609
+ const fromJson = (statements) => statements.flatMap((statement) => {
2610
+ const filtered = convertors.filter((it) => {
2611
+ return it.can(statement, "postgresql");
2612
+ });
2613
+ const convertor = filtered.length === 1 ? filtered[0] : void 0;
2614
+ if (!convertor) {
2615
+ return "";
2616
+ }
2617
+ return convertor.convert(statement);
2618
+ }).filter((it) => it !== "");
2619
+ const combinedTables = jsonCreateTables.flatMap((statement) => [
2620
+ statement,
2621
+ createReorgTableStatement(statement)
2622
+ ]);
2623
+ return {
2624
+ tables: {
2625
+ sql: fromJson(combinedTables),
2626
+ json: combinedTables
2627
+ },
2628
+ enums: { sql: fromJson(jsonCreateEnums), json: jsonCreateEnums },
2629
+ indexes: {
2630
+ sql: fromJson(jsonCreateIndexesForCreatedTables),
2631
+ json: jsonCreateIndexesForCreatedTables
2632
+ }
2633
+ };
2634
+ };
2635
+ var createReorgTableStatement = (statement) => {
2636
+ const reorgStatement = structuredClone(statement);
2637
+ reorgStatement.compositePkName = void 0;
2638
+ reorgStatement.compositePKs = [];
2639
+ for (const column of reorgStatement.columns) {
2640
+ column.primaryKey = false;
2641
+ }
2642
+ const reorgColumns = Object.values(
2643
+ squashPgScheme(
2644
+ generatePgSnapshot(
2645
+ [
2646
+ pgTable("", {
2647
+ operation_id: serial().notNull().primaryKey(),
2648
+ operation: integer().notNull(),
2649
+ checkpoint: varchar({
2650
+ length: 75
2651
+ }).notNull()
2652
+ })
2653
+ ],
2654
+ [],
2655
+ [],
2656
+ "snake_case"
2657
+ )
2658
+ ).tables
2659
+ //@ts-ignore
2660
+ )[0].columns;
2661
+ reorgStatement.columns.push(...Object.values(reorgColumns));
2662
+ reorgStatement.tableName = sqlToReorgTableName(reorgStatement.tableName);
2663
+ return reorgStatement;
2664
+ };
2665
+ var PgSquasher = {
2666
+ squashIdx: (idx) => {
2667
+ return `${idx.name};${idx.columns.map(
2668
+ (c) => `${c.expression}--${c.isExpression}--${c.asc}--${c.nulls}--${c.opclass && ""}`
2669
+ ).join(
2670
+ ",,"
2671
+ )};${idx.isUnique};${idx.concurrently};${idx.method};${idx.where};${JSON.stringify(idx.with)}`;
2672
+ },
2673
+ unsquashIdx: (input) => {
2674
+ const [
2675
+ name,
2676
+ columnsString,
2677
+ isUnique,
2678
+ concurrently,
2679
+ method,
2680
+ where,
2681
+ idxWith
2682
+ ] = input.split(";");
2683
+ const columnString = columnsString.split(",,");
2684
+ const columns = [];
2685
+ for (const column of columnString) {
2686
+ const [expression, isExpression, asc, nulls, opclass] = column.split("--");
2687
+ columns.push({
2688
+ nulls,
2689
+ isExpression: isExpression === "true",
2690
+ asc: asc === "true",
2691
+ expression,
2692
+ opclass: opclass === "undefined" ? void 0 : opclass
2693
+ });
2694
+ }
2695
+ return {
2696
+ name,
2697
+ columns,
2698
+ isUnique: isUnique === "true",
2699
+ concurrently: concurrently === "true",
2700
+ method,
2701
+ where: where === "undefined" ? void 0 : where,
2702
+ with: !idxWith || idxWith === "undefined" ? void 0 : JSON.parse(idxWith)
2703
+ };
2704
+ },
2705
+ squashPK: (pk) => {
2706
+ return `${pk.columns.join(",")};${pk.name}`;
2707
+ },
2708
+ unsquashPK: (pk) => {
2709
+ const splitted = pk.split(";");
2710
+ return { name: splitted[1], columns: splitted[0].split(",") };
2711
+ }
2712
+ };
2713
+ var parseType = (schemaPrefix, type) => {
2714
+ const pgNativeTypes = [
2715
+ "uuid",
2716
+ "smallint",
2717
+ "integer",
2718
+ "bigint",
2719
+ "boolean",
2720
+ "text",
2721
+ "varchar",
2722
+ "serial",
2723
+ "bigserial",
2724
+ "decimal",
2725
+ "numeric",
2726
+ "real",
2727
+ "json",
2728
+ "jsonb",
2729
+ "time",
2730
+ "time with time zone",
2731
+ "time without time zone",
2732
+ "time",
2733
+ "timestamp",
2734
+ "timestamp with time zone",
2735
+ "timestamp without time zone",
2736
+ "date",
2737
+ "interval",
2738
+ "bigint",
2739
+ "bigserial",
2740
+ "double precision",
2741
+ "interval year",
2742
+ "interval month",
2743
+ "interval day",
2744
+ "interval hour",
2745
+ "interval minute",
2746
+ "interval second",
2747
+ "interval year to month",
2748
+ "interval day to hour",
2749
+ "interval day to minute",
2750
+ "interval day to second",
2751
+ "interval hour to minute",
2752
+ "interval hour to second",
2753
+ "interval minute to second"
2754
+ ];
2755
+ const arrayDefinitionRegex = /\[\d*(?:\[\d*\])*\]/g;
2756
+ const arrayDefinition = (type.match(arrayDefinitionRegex) ?? []).join("");
2757
+ const withoutArrayDefinition = type.replace(arrayDefinitionRegex, "");
2758
+ return pgNativeTypes.some((it) => type.startsWith(it)) ? `${withoutArrayDefinition}${arrayDefinition}` : `${schemaPrefix}"${withoutArrayDefinition}"${arrayDefinition}`;
2759
+ };
2760
+ var Convertor = class {
2761
+ };
2762
+ var PgCreateTableConvertor = class extends Convertor {
2763
+ can(statement, dialect) {
2764
+ return statement.type === "create_table" && dialect === "postgresql";
2765
+ }
2766
+ convert(st) {
2767
+ const { tableName, schema, columns, compositePKs } = st;
2768
+ let statement = "";
2769
+ const name = schema ? `"${schema}"."${tableName}"` : `"${tableName}"`;
2770
+ statement += `CREATE TABLE ${name} (
2771
+ `;
2772
+ for (let i = 0; i < columns.length; i++) {
2773
+ const column = columns[i];
2774
+ const primaryKeyStatement = column.primaryKey ? " PRIMARY KEY" : "";
2775
+ const notNullStatement = column.notNull && !column.identity ? " NOT NULL" : "";
2776
+ const defaultStatement = column.default !== void 0 ? ` DEFAULT ${column.default}` : "";
2777
+ const schemaPrefix = column.typeSchema && column.typeSchema !== "public" ? `"${column.typeSchema}".` : "";
2778
+ const type = parseType(schemaPrefix, column.type);
2779
+ statement += ` "${column.name}" ${type}${primaryKeyStatement}${defaultStatement}${notNullStatement}`;
2780
+ statement += i === columns.length - 1 ? "" : ",\n";
2781
+ }
2782
+ if (typeof compositePKs !== "undefined" && compositePKs.length > 0) {
2783
+ statement += ",\n";
2784
+ const compositePK = PgSquasher.unsquashPK(compositePKs[0]);
2785
+ statement += ` CONSTRAINT "${st.compositePkName}" PRIMARY KEY("${compositePK.columns.join(`","`)}")`;
2786
+ }
2787
+ statement += "\n);";
2788
+ statement += "\n";
2789
+ return statement;
2790
+ }
2791
+ };
2792
+ var CreateTypeEnumConvertor = class extends Convertor {
2793
+ can(statement) {
2794
+ return statement.type === "create_type_enum";
2795
+ }
2796
+ convert(st) {
2797
+ const { name, values, schema } = st;
2798
+ const enumNameWithSchema = schema ? `"${schema}"."${name}"` : `"${name}"`;
2799
+ let valuesStatement = "(";
2800
+ valuesStatement += values.map((it) => `'${it}'`).join(", ");
2801
+ valuesStatement += ")";
2802
+ const statement = `CREATE TYPE ${enumNameWithSchema} AS ENUM${valuesStatement};`;
2803
+ return statement;
2804
+ }
2805
+ };
2806
+ var CreatePgIndexConvertor = class extends Convertor {
2807
+ can(statement, dialect) {
2808
+ return statement.type === "create_index_pg" && dialect === "postgresql";
2809
+ }
2810
+ convert(statement) {
2811
+ const {
2812
+ name,
2813
+ columns,
2814
+ isUnique,
2815
+ concurrently,
2816
+ with: withMap,
2817
+ method,
2818
+ where
2819
+ } = statement.data;
2820
+ const indexPart = isUnique ? "UNIQUE INDEX" : "INDEX";
2821
+ const value = columns.map(
2822
+ (it) => `${it.isExpression ? it.expression : `"${it.expression}"`}${it.opclass ? ` ${it.opclass}` : it.asc ? "" : " DESC"}${it.asc && it.nulls && it.nulls === "last" || it.opclass ? "" : ` NULLS ${it.nulls.toUpperCase()}`}`
2823
+ ).join(",");
2824
+ const tableNameWithSchema = statement.schema ? `"${statement.schema}"."${statement.tableName}"` : `"${statement.tableName}"`;
2825
+ function reverseLogic(mappedWith) {
2826
+ let reversedString = "";
2827
+ for (const key in mappedWith) {
2828
+ if (mappedWith.hasOwnProperty(key)) {
2829
+ reversedString += `${key}=${mappedWith[key]},`;
2830
+ }
2831
+ }
2832
+ reversedString = reversedString.slice(0, -1);
2833
+ return reversedString;
2834
+ }
2835
+ return `CREATE ${indexPart}${concurrently ? " CONCURRENTLY" : ""} IF NOT EXISTS "${name}" ON ${tableNameWithSchema} USING ${method} (${value})${Object.keys(withMap).length !== 0 ? ` WITH (${reverseLogic(withMap)})` : ""}${where ? ` WHERE ${where}` : ""};`;
2836
+ }
2837
+ };
2838
+ var PgCreateSchemaConvertor = class extends Convertor {
2839
+ can(statement, dialect) {
2840
+ return statement.type === "create_schema" && dialect === "postgresql";
2841
+ }
2842
+ convert(statement) {
2843
+ const { name } = statement;
2844
+ return `CREATE SCHEMA IF NOT EXISTS"${name}";
2845
+ `;
2846
+ }
2847
+ };
2848
+ var convertors = [];
2849
+ convertors.push(new PgCreateTableConvertor());
2850
+ convertors.push(new CreateTypeEnumConvertor());
2851
+ convertors.push(new CreatePgIndexConvertor());
2852
+ convertors.push(new PgCreateSchemaConvertor());
2853
+ var preparePgCreateTableJson = (table, json) => {
2854
+ const { name, schema, columns, compositePrimaryKeys } = table;
2855
+ const tableKey = `${schema || "public"}.${name}`;
2856
+ const compositePkName = Object.values(compositePrimaryKeys).length > 0 ? json.tables[tableKey].compositePrimaryKeys[`${PgSquasher.unsquashPK(Object.values(compositePrimaryKeys)[0]).name}`].name : "";
2857
+ return {
2858
+ type: "create_table",
2859
+ tableName: name,
2860
+ schema,
2861
+ columns: Object.values(columns),
2862
+ compositePKs: Object.values(compositePrimaryKeys),
2863
+ compositePkName
2864
+ };
2865
+ };
2866
+ var preparePgCreateIndexesJson = (tableName, schema, indexes) => {
2867
+ return Object.values(indexes).map((indexData) => {
2868
+ return {
2869
+ type: "create_index_pg",
2870
+ tableName,
2871
+ data: PgSquasher.unsquashIdx(indexData),
2872
+ schema
2873
+ };
2874
+ });
2875
+ };
2876
+ var prepareCreateEnumJson = (name, schema, values) => {
2877
+ return {
2878
+ type: "create_type_enum",
2879
+ name,
2880
+ schema,
2881
+ values
2882
+ };
2883
+ };
2884
+ var prepareFromExports = (exports) => {
2885
+ const tables = [];
2886
+ const enums = [];
2887
+ const schemas = [];
2888
+ const sequences = [];
2889
+ const views = [];
2890
+ const matViews = [];
2891
+ const i0values = Object.values(exports);
2892
+ i0values.forEach((t) => {
2893
+ if (isPgEnum(t)) {
2894
+ enums.push(t);
2895
+ return;
2896
+ }
2897
+ if (is(t, PgTable)) {
2898
+ tables.push(t);
2899
+ }
2900
+ if (is(t, PgSchema)) {
2901
+ schemas.push(t);
2902
+ }
2903
+ if (is(t, PgView)) {
2904
+ views.push(t);
2905
+ }
2906
+ if (is(t, PgMaterializedView)) {
2907
+ matViews.push(t);
2908
+ }
2909
+ if (isPgSequence(t)) {
2910
+ sequences.push(t);
2911
+ }
2912
+ });
2913
+ return { tables, enums, schemas, sequences, views, matViews };
2914
+ };
2915
+ function getColumnCasing(column, casing) {
2916
+ if (!column.name)
2917
+ return "";
2918
+ return !column.keyAsName || casing === void 0 ? column.name : casing === "camelCase" ? toCamelCase(column.name) : toSnakeCase(column.name);
2919
+ }
2920
+ var sqlToStr = (sql5, casing) => {
2921
+ return sql5.toQuery({
2922
+ escapeName: () => {
2923
+ throw new Error("we don't support params for `sql` default values");
2924
+ },
2925
+ escapeParam: () => {
2926
+ throw new Error("we don't support params for `sql` default values");
2927
+ },
2928
+ escapeString: () => {
2929
+ throw new Error("we don't support params for `sql` default values");
2930
+ },
2931
+ casing: new CasingCache(casing)
2932
+ }).sql;
2933
+ };
2934
+ function isPgArrayType(sqlType) {
2935
+ return sqlType.match(/.*\[\d*\].*|.*\[\].*/g) !== null;
2936
+ }
2937
+ function buildArrayString(array, sqlType) {
2938
+ sqlType = sqlType.split("[")[0];
2939
+ const values = array.map((value) => {
2940
+ if (typeof value === "number" || typeof value === "bigint") {
2941
+ return value.toString();
2942
+ } else if (typeof value === "boolean") {
2943
+ return value ? "true" : "false";
2944
+ } else if (Array.isArray(value)) {
2945
+ return buildArrayString(value, sqlType);
2946
+ } else if (value instanceof Date) {
2947
+ if (sqlType === "date") {
2948
+ return `"${value.toISOString().split("T")[0]}"`;
2949
+ } else if (sqlType === "timestamp") {
2950
+ return `"${value.toISOString().replace("T", " ").slice(0, 23)}"`;
2951
+ } else {
2952
+ return `"${value.toISOString()}"`;
2953
+ }
2954
+ } else if (typeof value === "object") {
2955
+ return `"${JSON.stringify(value).replaceAll('"', '\\"')}"`;
2956
+ }
2957
+ return `"${value}"`;
2958
+ }).join(",");
2959
+ return `{${values}}`;
2960
+ }
2961
+ var indexName = (tableName, columns) => {
2962
+ return `${tableName}_${columns.join("_")}_index`;
2963
+ };
2964
+ var generatePgSnapshot = (tables, enums, schemas, casing) => {
2965
+ const dialect = new PgDialect({ casing });
2966
+ const result = {};
2967
+ const indexesInSchema = {};
2968
+ for (const table of tables) {
2969
+ const {
2970
+ name: tableName,
2971
+ columns,
2972
+ indexes,
2973
+ schema,
2974
+ primaryKeys
2975
+ } = getTableConfig(table);
2976
+ const columnsObject = {};
2977
+ const indexesObject = {};
2978
+ const primaryKeysObject = {};
2979
+ columns.forEach((column) => {
2980
+ const name = getColumnCasing(column, casing);
2981
+ const notNull = column.notNull;
2982
+ const primaryKey = column.primary;
2983
+ const sqlTypeLowered = column.getSQLType().toLowerCase();
2984
+ const typeSchema = is(column, PgEnumColumn) ? column.enum.schema || "public" : void 0;
2985
+ const columnToSet = {
2986
+ name,
2987
+ type: column.getSQLType(),
2988
+ typeSchema,
2989
+ primaryKey,
2990
+ notNull
2991
+ };
2992
+ if (column.default !== void 0) {
2993
+ if (is(column.default, SQL)) {
2994
+ columnToSet.default = sqlToStr(column.default, casing);
2995
+ } else {
2996
+ if (typeof column.default === "string") {
2997
+ columnToSet.default = `'${column.default}'`;
2998
+ } else {
2999
+ if (sqlTypeLowered === "jsonb" || sqlTypeLowered === "json") {
3000
+ columnToSet.default = `'${JSON.stringify(column.default)}'::${sqlTypeLowered}`;
3001
+ } else if (column.default instanceof Date) {
3002
+ if (sqlTypeLowered === "date") {
3003
+ columnToSet.default = `'${column.default.toISOString().split("T")[0]}'`;
3004
+ } else if (sqlTypeLowered === "timestamp") {
3005
+ columnToSet.default = `'${column.default.toISOString().replace("T", " ").slice(0, 23)}'`;
3006
+ } else {
3007
+ columnToSet.default = `'${column.default.toISOString()}'`;
3008
+ }
3009
+ } else if (isPgArrayType(sqlTypeLowered) && Array.isArray(column.default)) {
3010
+ columnToSet.default = `'${buildArrayString(column.default, sqlTypeLowered)}'`;
3011
+ } else {
3012
+ columnToSet.default = column.default;
3013
+ }
3014
+ }
3015
+ }
3016
+ }
3017
+ columnsObject[name] = columnToSet;
3018
+ });
3019
+ primaryKeys.map((pk) => {
3020
+ const originalColumnNames = pk.columns.map((c) => c.name);
3021
+ const columnNames = pk.columns.map((c) => getColumnCasing(c, casing));
3022
+ let name = pk.getName();
3023
+ if (casing !== void 0) {
3024
+ for (let i = 0; i < originalColumnNames.length; i++) {
3025
+ name = name.replace(originalColumnNames[i], columnNames[i]);
3026
+ }
3027
+ }
3028
+ primaryKeysObject[name] = {
3029
+ name,
3030
+ columns: columnNames
3031
+ };
3032
+ });
3033
+ indexes.forEach((value) => {
3034
+ const columns2 = value.config.columns;
3035
+ const indexColumnNames = [];
3036
+ columns2.forEach((it) => {
3037
+ const name2 = getColumnCasing(it, casing);
3038
+ indexColumnNames.push(name2);
3039
+ });
3040
+ const name = value.config.name ? value.config.name : indexName(tableName, indexColumnNames);
3041
+ const indexColumns = columns2.map(
3042
+ (it) => {
3043
+ if (is(it, SQL)) {
3044
+ return {
3045
+ expression: dialect.sqlToQuery(it, "indexes").sql,
3046
+ asc: true,
3047
+ isExpression: true,
3048
+ nulls: "last"
3049
+ };
3050
+ } else {
3051
+ it = it;
3052
+ return {
3053
+ expression: getColumnCasing(it, casing),
3054
+ isExpression: false,
3055
+ // @ts-ignore
3056
+ asc: it.indexConfig?.order === "asc",
3057
+ // @ts-ignore
3058
+ nulls: it.indexConfig?.nulls ? (
3059
+ // @ts-ignore
3060
+ it.indexConfig?.nulls
3061
+ ) : (
3062
+ // @ts-ignore
3063
+ it.indexConfig?.order === "desc" ? "first" : "last"
3064
+ ),
3065
+ // @ts-ignore
3066
+ opclass: it.indexConfig?.opClass
3067
+ };
3068
+ }
3069
+ }
3070
+ );
3071
+ if (typeof indexesInSchema[schema ?? "public"] !== "undefined") {
3072
+ indexesInSchema[schema ?? "public"].push(name);
3073
+ } else {
3074
+ indexesInSchema[schema ?? "public"] = [name];
3075
+ }
3076
+ indexesObject[name] = {
3077
+ name,
3078
+ columns: indexColumns,
3079
+ isUnique: value.config.unique ?? false,
3080
+ where: value.config.where ? dialect.sqlToQuery(value.config.where).sql : void 0,
3081
+ concurrently: value.config.concurrently ?? false,
3082
+ method: value.config.method ?? "btree",
3083
+ with: value.config.with ?? {}
3084
+ };
3085
+ });
3086
+ const tableKey = `${schema ?? "public"}.${tableName}`;
3087
+ result[tableKey] = {
3088
+ name: tableName,
3089
+ schema: schema ?? "",
3090
+ columns: columnsObject,
3091
+ indexes: indexesObject,
3092
+ compositePrimaryKeys: primaryKeysObject
3093
+ };
3094
+ }
3095
+ const enumsToReturn = enums.reduce((map, obj) => {
3096
+ const enumSchema = obj.schema || "public";
3097
+ const key = `${enumSchema}.${obj.enumName}`;
3098
+ map[key] = {
3099
+ name: obj.enumName,
3100
+ schema: enumSchema,
3101
+ values: obj.enumValues
3102
+ };
3103
+ return map;
3104
+ }, {});
3105
+ const schemasObject = Object.fromEntries(
3106
+ schemas.filter((it) => {
3107
+ return it.schemaName !== "public";
3108
+ }).map((it) => [it.schemaName, it.schemaName])
3109
+ );
3110
+ return {
3111
+ version: "7",
3112
+ dialect: "postgresql",
3113
+ tables: result,
3114
+ enums: enumsToReturn,
3115
+ schemas: schemasObject
3116
+ };
3117
+ };
3118
+ var mapValues = (obj, map) => {
3119
+ const result = Object.keys(obj).reduce(
3120
+ (result2, key) => {
3121
+ result2[key] = map(obj[key]);
3122
+ return result2;
3123
+ },
3124
+ {}
3125
+ );
3126
+ return result;
3127
+ };
3128
+ var squashPgScheme = (json) => {
3129
+ const mappedTables = Object.fromEntries(
3130
+ Object.entries(json.tables).map((it) => {
3131
+ const squashedIndexes = mapValues(it[1].indexes, (index) => {
3132
+ return PgSquasher.squashIdx(index);
3133
+ });
3134
+ const squashedPKs = mapValues(it[1].compositePrimaryKeys, (pk) => {
3135
+ return PgSquasher.squashPK(pk);
3136
+ });
3137
+ const mappedColumns = Object.fromEntries(
3138
+ Object.entries(it[1].columns).map((it2) => {
3139
+ return [
3140
+ it2[0],
3141
+ {
3142
+ ...it2[1],
3143
+ identity: void 0
3144
+ }
3145
+ ];
3146
+ })
3147
+ );
3148
+ return [
3149
+ it[0],
3150
+ {
3151
+ name: it[1].name,
3152
+ schema: it[1].schema,
3153
+ columns: mappedColumns,
3154
+ indexes: squashedIndexes,
3155
+ compositePrimaryKeys: squashedPKs
3156
+ }
3157
+ ];
3158
+ })
3159
+ );
3160
+ return {
3161
+ version: "7",
3162
+ dialect: json.dialect,
3163
+ tables: mappedTables,
3164
+ enums: json.enums,
3165
+ schemas: json.schemas,
3166
+ views: json.views
3167
+ };
3168
+ };
3169
+
2487
3170
  // src/build/schema.ts
2488
- import { SQL, getTableColumns, is } from "drizzle-orm";
3171
+ import { SQL as SQL2, getTableColumns, is as is2 } from "drizzle-orm";
2489
3172
  import {
2490
3173
  PgBigSerial53,
2491
3174
  PgBigSerial64,
2492
3175
  PgSequence,
2493
3176
  PgSerial,
2494
3177
  PgSmallSerial,
2495
- PgTable,
2496
- PgView,
2497
- getTableConfig
3178
+ PgTable as PgTable2,
3179
+ PgView as PgView2,
3180
+ getTableConfig as getTableConfig2
2498
3181
  } from "drizzle-orm/pg-core";
2499
3182
  var buildSchema = ({ schema }) => {
2500
3183
  const statements = getSql(schema);
2501
3184
  for (const [name, s] of Object.entries(schema)) {
2502
- if (is(s, PgTable)) {
3185
+ if (is2(s, PgTable2)) {
2503
3186
  let hasPrimaryKey = false;
2504
3187
  for (const [columnName, column] of Object.entries(getTableColumns(s))) {
2505
3188
  if (column.primary) {
@@ -2532,60 +3215,60 @@ var buildSchema = ({ schema }) => {
2532
3215
  );
2533
3216
  }
2534
3217
  if (column.hasDefault) {
2535
- if (column.default && column.default instanceof SQL) {
3218
+ if (column.default && column.default instanceof SQL2) {
2536
3219
  throw new Error(
2537
3220
  `Schema validation failed: '${name}.${columnName}' is a default column and default columns with raw sql are unsupported.`
2538
3221
  );
2539
3222
  }
2540
- if (column.defaultFn && column.defaultFn() instanceof SQL) {
3223
+ if (column.defaultFn && column.defaultFn() instanceof SQL2) {
2541
3224
  throw new Error(
2542
3225
  `Schema validation failed: '${name}.${columnName}' is a default column and default columns with raw sql are unsupported.`
2543
3226
  );
2544
3227
  }
2545
- if (column.onUpdateFn && column.onUpdateFn() instanceof SQL) {
3228
+ if (column.onUpdateFn && column.onUpdateFn() instanceof SQL2) {
2546
3229
  throw new Error(
2547
3230
  `Schema validation failed: '${name}.${columnName}' is a default column and default columns with raw sql are unsupported.`
2548
3231
  );
2549
3232
  }
2550
3233
  }
2551
3234
  }
2552
- if (getTableConfig(s).primaryKeys.length > 1) {
3235
+ if (getTableConfig2(s).primaryKeys.length > 1) {
2553
3236
  throw new Error(
2554
3237
  `Schema validation failed: '${name}' has multiple primary keys.`
2555
3238
  );
2556
3239
  }
2557
- if (getTableConfig(s).primaryKeys.length === 1 && hasPrimaryKey) {
3240
+ if (getTableConfig2(s).primaryKeys.length === 1 && hasPrimaryKey) {
2558
3241
  throw new Error(
2559
3242
  `Schema validation failed: '${name}' has multiple primary keys.`
2560
3243
  );
2561
3244
  }
2562
- if (getTableConfig(s).primaryKeys.length === 0 && hasPrimaryKey === false) {
3245
+ if (getTableConfig2(s).primaryKeys.length === 0 && hasPrimaryKey === false) {
2563
3246
  throw new Error(
2564
3247
  `Schema validation failed: '${name}' has no primary key. Declare one with ".primaryKey()".`
2565
3248
  );
2566
3249
  }
2567
- if (getTableConfig(s).foreignKeys.length > 0) {
3250
+ if (getTableConfig2(s).foreignKeys.length > 0) {
2568
3251
  throw new Error(
2569
3252
  `Schema validation failed: '${name}' has a foreign key constraint and foreign key constraints are unsupported.`
2570
3253
  );
2571
3254
  }
2572
- if (getTableConfig(s).checks.length > 0) {
3255
+ if (getTableConfig2(s).checks.length > 0) {
2573
3256
  throw new Error(
2574
3257
  `Schema validation failed: '${name}' has a check constraint and check constraints are unsupported.`
2575
3258
  );
2576
3259
  }
2577
- if (getTableConfig(s).uniqueConstraints.length > 0) {
3260
+ if (getTableConfig2(s).uniqueConstraints.length > 0) {
2578
3261
  throw new Error(
2579
3262
  `Schema validation failed: '${name}' has a unique constraint and unique constraints are unsupported.`
2580
3263
  );
2581
3264
  }
2582
3265
  }
2583
- if (is(s, PgSequence)) {
3266
+ if (is2(s, PgSequence)) {
2584
3267
  throw new Error(
2585
3268
  `Schema validation failed: '${name}' is a sequence and sequences are unsupported.`
2586
3269
  );
2587
3270
  }
2588
- if (is(s, PgView)) {
3271
+ if (is2(s, PgView2)) {
2589
3272
  throw new Error(
2590
3273
  `Schema validation failed: '${name}' is a view and views are unsupported.`
2591
3274
  );
@@ -2854,7 +3537,6 @@ var createBuild = async ({
2854
3537
  async executeApi({ indexingBuild, database }) {
2855
3538
  globalThis.PONDER_INDEXING_BUILD = indexingBuild;
2856
3539
  globalThis.PONDER_DATABASE = database;
2857
- globalThis.COMMON = common;
2858
3540
  if (!fs.existsSync(common.options.apiFile)) {
2859
3541
  const error = new BuildError(
2860
3542
  `API function file not found. Create a file at ${common.options.apiFile}. Read more: https://ponder-docs-git-v09-ponder-sh.vercel.app/docs/query/api-functions`
@@ -3097,6 +3779,49 @@ var createBuild = async ({
3097
3779
  // src/database/index.ts
3098
3780
  import { randomUUID } from "node:crypto";
3099
3781
 
3782
+ // src/drizzle/index.ts
3783
+ import { getTableColumns as getTableColumns2, getTableName, is as is3 } from "drizzle-orm";
3784
+ import { PgTable as PgTable3, getTableConfig as getTableConfig3 } from "drizzle-orm/pg-core";
3785
+ var getTableNames = (schema) => {
3786
+ const tableNames = Object.entries(schema).filter(([, table]) => is3(table, PgTable3)).map(([js, table]) => {
3787
+ const sql5 = getTableName(table);
3788
+ return {
3789
+ sql: sql5,
3790
+ reorg: sqlToReorgTableName(sql5),
3791
+ trigger: sqlToReorgTableName(sql5),
3792
+ triggerFn: `operation_reorg__${sql5}()`,
3793
+ js
3794
+ };
3795
+ });
3796
+ return tableNames;
3797
+ };
3798
+ var getPrimaryKeyColumns = (table) => {
3799
+ const primaryKeys = getTableConfig3(table).primaryKeys;
3800
+ const findJsName = (column) => {
3801
+ const name = column.name;
3802
+ for (const [js, column2] of Object.entries(getTableColumns2(table))) {
3803
+ if (column2.name === name)
3804
+ return js;
3805
+ }
3806
+ throw "unreachable";
3807
+ };
3808
+ if (primaryKeys.length > 0) {
3809
+ return primaryKeys[0].columns.map((column) => ({
3810
+ sql: getColumnCasing(column, "snake_case"),
3811
+ js: findJsName(column)
3812
+ }));
3813
+ }
3814
+ const pkColumn = Object.values(getTableColumns2(table)).find(
3815
+ (c) => c.primary
3816
+ );
3817
+ return [
3818
+ {
3819
+ sql: getColumnCasing(pkColumn, "snake_case"),
3820
+ js: findJsName(pkColumn)
3821
+ }
3822
+ ];
3823
+ };
3824
+
3100
3825
  // src/sync-store/migrations.ts
3101
3826
  import { sql } from "kysely";
3102
3827
  var migrations = {
@@ -3807,6 +4532,27 @@ var formatPercentage = (cacheRate) => {
3807
4532
 
3808
4533
  // src/utils/pg.ts
3809
4534
  import pg from "pg";
4535
+
4536
+ // src/utils/print.ts
4537
+ function prettyPrint(args) {
4538
+ if (args === void 0)
4539
+ return "(undefined)";
4540
+ const entries = Object.entries(args).map(([key, value]) => {
4541
+ if (value === void 0)
4542
+ return null;
4543
+ const trimmedValue = typeof value === "string" && value.length > 80 ? value.slice(0, 80).concat("...") : value;
4544
+ return [key, trimmedValue];
4545
+ }).filter(Boolean);
4546
+ if (entries.length === 0)
4547
+ return " (empty object)";
4548
+ const maxLength = entries.reduce(
4549
+ (acc, [key]) => Math.max(acc, key.length),
4550
+ 0
4551
+ );
4552
+ return entries.map(([key, value]) => ` ${`${key}`.padEnd(maxLength + 1)} ${value}`).join("\n");
4553
+ }
4554
+
4555
+ // src/utils/pg.ts
3810
4556
  var bigIntArrayParser = pg.types.getTypeParser(1016);
3811
4557
  pg.types.setTypeParser(1231, bigIntArrayParser);
3812
4558
  var originalClientQuery = pg.Client.prototype.query;
@@ -4002,7 +4748,7 @@ async function wait(milliseconds) {
4002
4748
  }
4003
4749
 
4004
4750
  // src/database/index.ts
4005
- import { getTableColumns as getTableColumns2 } from "drizzle-orm";
4751
+ import { getTableColumns as getTableColumns3 } from "drizzle-orm";
4006
4752
  import { drizzle as drizzleNodePg } from "drizzle-orm/node-postgres";
4007
4753
  import { drizzle as drizzlePglite } from "drizzle-orm/pglite";
4008
4754
  import {
@@ -4238,8 +4984,8 @@ var createDatabase = async ({
4238
4984
  for (const log of reversed) {
4239
4985
  if (log.operation === 0) {
4240
4986
  await tx.deleteFrom(tableName.sql).$call((qb2) => {
4241
- for (const { sql: sql4 } of primaryKeyColumns) {
4242
- qb2 = qb2.where(sql4, "=", log[sql4]);
4987
+ for (const { sql: sql5 } of primaryKeyColumns) {
4988
+ qb2 = qb2.where(sql5, "=", log[sql5]);
4243
4989
  }
4244
4990
  return qb2;
4245
4991
  }).execute();
@@ -4248,8 +4994,8 @@ var createDatabase = async ({
4248
4994
  log.checkpoint = void 0;
4249
4995
  log.operation = void 0;
4250
4996
  await tx.updateTable(tableName.sql).set(log).$call((qb2) => {
4251
- for (const { sql: sql4 } of primaryKeyColumns) {
4252
- qb2 = qb2.where(sql4, "=", log[sql4]);
4997
+ for (const { sql: sql5 } of primaryKeyColumns) {
4998
+ qb2 = qb2.where(sql5, "=", log[sql5]);
4253
4999
  }
4254
5000
  return qb2;
4255
5001
  }).execute();
@@ -4258,7 +5004,7 @@ var createDatabase = async ({
4258
5004
  log.checkpoint = void 0;
4259
5005
  log.operation = void 0;
4260
5006
  await tx.insertInto(tableName.sql).values(log).onConflict(
4261
- (oc) => oc.columns(primaryKeyColumns.map(({ sql: sql4 }) => sql4)).doNothing()
5007
+ (oc) => oc.columns(primaryKeyColumns.map(({ sql: sql5 }) => sql5)).doNothing()
4262
5008
  ).execute();
4263
5009
  }
4264
5010
  }
@@ -4513,7 +5259,7 @@ var createDatabase = async ({
4513
5259
  if (createdTables) {
4514
5260
  common.logger.info({
4515
5261
  service: "database",
4516
- msg: `Created tables [${getTableNames(schemaBuild.schema).map(({ sql: sql4 }) => sql4).join(", ")}]`
5262
+ msg: `Created tables [${getTableNames(schemaBuild.schema).map(({ sql: sql5 }) => sql5).join(", ")}]`
4517
5263
  });
4518
5264
  const newApp = {
4519
5265
  is_locked: 1,
@@ -4522,7 +5268,7 @@ var createDatabase = async ({
4522
5268
  build_id: buildId,
4523
5269
  checkpoint: ZERO_CHECKPOINT_STRING,
4524
5270
  table_names: getTableNames(schemaBuild.schema).map(
4525
- ({ sql: sql4 }) => sql4
5271
+ ({ sql: sql5 }) => sql5
4526
5272
  ),
4527
5273
  version: VERSION
4528
5274
  };
@@ -4643,7 +5389,7 @@ var createDatabase = async ({
4643
5389
  { method: "createTriggers", includeTraceLogs: true },
4644
5390
  async () => {
4645
5391
  for (const tableName of getTableNames(schemaBuild.schema)) {
4646
- const columns = getTableColumns2(
5392
+ const columns = getTableColumns3(
4647
5393
  schemaBuild.schema[tableName.js]
4648
5394
  );
4649
5395
  const columnNames = Object.values(columns).map(
@@ -4943,6 +5689,921 @@ var mergeResults = (results) => {
4943
5689
  return { status: "success", result: results.map((result) => result.result) };
4944
5690
  };
4945
5691
 
5692
+ // src/indexing-store/historical.ts
5693
+ import {
5694
+ and,
5695
+ eq,
5696
+ getTableColumns as getTableColumns4,
5697
+ getTableName as getTableName2,
5698
+ sql as sql3
5699
+ } from "drizzle-orm";
5700
+ import { getTableConfig as getTableConfig4 } from "drizzle-orm/pg-core";
5701
+ import { drizzle } from "drizzle-orm/pg-proxy";
5702
+
5703
+ // src/indexing-store/index.ts
5704
+ var parseSqlError = (e) => {
5705
+ let error = getBaseError(e);
5706
+ if (error?.message?.includes("violates not-null constraint")) {
5707
+ error = new NotNullConstraintError(error.message);
5708
+ } else if (error?.message?.includes("violates unique constraint")) {
5709
+ error = new UniqueConstraintError(error.message);
5710
+ } else if (error?.message.includes("violates check constraint")) {
5711
+ error = new CheckConstraintError(error.message);
5712
+ } else if (error?.message?.includes("Do not know how to serialize a BigInt")) {
5713
+ error = new BigIntSerializationError(error.message);
5714
+ error.meta.push(
5715
+ "Hint:\n The JSON column type does not support BigInt values. Use the replaceBigInts() helper function before inserting into the database. Docs: https://ponder.sh/docs/utilities/replace-bigints"
5716
+ );
5717
+ }
5718
+ return error;
5719
+ };
5720
+
5721
+ // src/indexing-store/historical.ts
5722
+ var checkOnchainTable = (table, method) => {
5723
+ if (table === void 0)
5724
+ throw new UndefinedTableError(
5725
+ `Table object passed to db.${method}() is undefined`
5726
+ );
5727
+ if (onchain in table)
5728
+ return;
5729
+ throw new InvalidStoreMethodError(
5730
+ method === "find" ? `db.find() can only be used with onchain tables, and '${getTableConfig4(table).name}' is an offchain table.` : `Indexing functions can only write to onchain tables, and '${getTableConfig4(table).name}' is an offchain table.`
5731
+ );
5732
+ };
5733
+ var hasEmptyValue = (column) => {
5734
+ return column.hasDefault;
5735
+ };
5736
+ var getEmptyValue = (column, type) => {
5737
+ if (type === 1 /* UPDATE */ && column.onUpdateFn) {
5738
+ return column.onUpdateFn();
5739
+ }
5740
+ if (column.default !== void 0)
5741
+ return column.default;
5742
+ if (column.defaultFn !== void 0)
5743
+ return column.defaultFn();
5744
+ if (column.onUpdateFn !== void 0)
5745
+ return column.onUpdateFn();
5746
+ return void 0;
5747
+ };
5748
+ var normalizeColumn = (column, value, type) => {
5749
+ if (value === void 0) {
5750
+ if (hasEmptyValue(column))
5751
+ return getEmptyValue(column, type);
5752
+ return null;
5753
+ }
5754
+ if (column.mapToDriverValue === void 0)
5755
+ return value;
5756
+ try {
5757
+ return column.mapFromDriverValue(column.mapToDriverValue(value));
5758
+ } catch (e) {
5759
+ if (e?.message?.includes("Do not know how to serialize a BigInt")) {
5760
+ const error = new BigIntSerializationError(e.message);
5761
+ error.meta.push(
5762
+ "Hint:\n The JSON column type does not support BigInt values. Use the replaceBigInts() helper function before inserting into the database. Docs: https://ponder.sh/docs/utilities/replace-bigints"
5763
+ );
5764
+ throw error;
5765
+ }
5766
+ }
5767
+ };
5768
+ var createHistoricalIndexingStore = ({
5769
+ common,
5770
+ schemaBuild: { schema },
5771
+ database,
5772
+ isDatabaseEmpty
5773
+ }) => {
5774
+ const queue = createQueue({
5775
+ browser: false,
5776
+ initialStart: true,
5777
+ concurrency: 1,
5778
+ worker: (fn) => {
5779
+ return fn();
5780
+ }
5781
+ });
5782
+ const primaryKeysCache = /* @__PURE__ */ new Map();
5783
+ const cache = /* @__PURE__ */ new Map();
5784
+ for (const tableName of getTableNames(schema)) {
5785
+ primaryKeysCache.set(
5786
+ schema[tableName.js],
5787
+ getPrimaryKeyColumns(schema[tableName.js])
5788
+ );
5789
+ cache.set(schema[tableName.js], /* @__PURE__ */ new Map());
5790
+ }
5791
+ const getCacheKey = (table, row) => {
5792
+ const primaryKeys = primaryKeysCache.get(table);
5793
+ return primaryKeys.map((pk) => normalizeColumn(table[pk.js], row[pk.js])).join("_");
5794
+ };
5795
+ const getCacheEntry = (table, row) => {
5796
+ return cache.get(table).get(getCacheKey(table, row));
5797
+ };
5798
+ const setCacheEntry = (table, userRow, entryType, existingRow) => {
5799
+ let row = structuredClone(userRow);
5800
+ if (existingRow) {
5801
+ for (const [key, value] of Object.entries(row)) {
5802
+ existingRow[key] = value;
5803
+ }
5804
+ existingRow = normalizeRow(table, existingRow, entryType);
5805
+ const bytes = getBytes(existingRow);
5806
+ cacheBytes += bytes;
5807
+ cache.get(table).set(getCacheKey(table, existingRow), {
5808
+ type: entryType,
5809
+ row: existingRow,
5810
+ operationIndex: totalCacheOps++,
5811
+ bytes
5812
+ });
5813
+ return structuredClone(existingRow);
5814
+ } else {
5815
+ row = normalizeRow(table, row, entryType);
5816
+ const bytes = getBytes(row);
5817
+ cacheBytes += bytes;
5818
+ cache.get(table).set(getCacheKey(table, row), {
5819
+ type: entryType,
5820
+ bytes,
5821
+ operationIndex: totalCacheOps++,
5822
+ row
5823
+ });
5824
+ return structuredClone(row);
5825
+ }
5826
+ };
5827
+ const deleteCacheEntry = (table, row) => {
5828
+ const entry = getCacheEntry(table, row);
5829
+ if (entry) {
5830
+ cacheBytes -= entry.bytes;
5831
+ }
5832
+ return cache.get(table).delete(getCacheKey(table, row));
5833
+ };
5834
+ const normalizeRow = (table, row, type) => {
5835
+ for (const [columnName, column] of Object.entries(getTableColumns4(table))) {
5836
+ if (type === 0 /* INSERT */ && (row[columnName] === void 0 || row[columnName] === null) && column.notNull && hasEmptyValue(column) === false) {
5837
+ const error = new NotNullConstraintError(
5838
+ `Column '${getTableName2(table)}.${columnName}' violates not-null constraint.`
5839
+ );
5840
+ error.meta.push(
5841
+ `db.${type === 0 /* INSERT */ ? "insert" : "update"} arguments:
5842
+ ${prettyPrint(row)}`
5843
+ );
5844
+ throw error;
5845
+ }
5846
+ row[columnName] = normalizeColumn(column, row[columnName], type);
5847
+ }
5848
+ return row;
5849
+ };
5850
+ const getBytes = (value) => {
5851
+ let size = 13;
5852
+ if (typeof value === "number") {
5853
+ size += 8;
5854
+ } else if (typeof value === "string") {
5855
+ size += 2 * value.length;
5856
+ } else if (typeof value === "boolean") {
5857
+ size += 4;
5858
+ } else if (typeof value === "bigint") {
5859
+ size += 48;
5860
+ } else if (value === null || value === void 0) {
5861
+ size += 8;
5862
+ } else if (Array.isArray(value)) {
5863
+ for (const e of value) {
5864
+ size += getBytes(e);
5865
+ }
5866
+ } else {
5867
+ for (const col of Object.values(value)) {
5868
+ size += getBytes(col);
5869
+ }
5870
+ }
5871
+ return size;
5872
+ };
5873
+ let cacheBytes = 0;
5874
+ let totalCacheOps = 0;
5875
+ const maxBytes = common.options.indexingCacheMaxBytes;
5876
+ common.logger.debug({
5877
+ service: "indexing",
5878
+ msg: `Using a ${Math.round(maxBytes / (1024 * 1024))} MB indexing cache`
5879
+ });
5880
+ const getWhereCondition = (table, key) => {
5881
+ primaryKeysCache.get(table);
5882
+ const conditions = [];
5883
+ for (const { js } of primaryKeysCache.get(table)) {
5884
+ conditions.push(eq(table[js], key[js]));
5885
+ }
5886
+ return and(...conditions);
5887
+ };
5888
+ const find = (table, key) => {
5889
+ return database.qb.drizzle.select().from(table).where(getWhereCondition(table, key)).then((res) => res.length === 0 ? null : res[0]);
5890
+ };
5891
+ const indexingStore = {
5892
+ // @ts-ignore
5893
+ find: (table, key) => queue.add(
5894
+ () => database.wrap(
5895
+ { method: `${getTableName2(table) ?? "unknown"}.find()` },
5896
+ async () => {
5897
+ checkOnchainTable(table, "find");
5898
+ const entry = getCacheEntry(table, key);
5899
+ if (entry) {
5900
+ getCacheEntry(table, key).operationIndex = totalCacheOps++;
5901
+ return entry.row;
5902
+ } else {
5903
+ if (isDatabaseEmpty)
5904
+ return null;
5905
+ const row = await find(table, key);
5906
+ const bytes = getBytes(row);
5907
+ cacheBytes += bytes;
5908
+ cache.get(table).set(getCacheKey(table, key), {
5909
+ type: 2 /* FIND */,
5910
+ bytes,
5911
+ operationIndex: totalCacheOps++,
5912
+ row
5913
+ });
5914
+ return find(table, key);
5915
+ }
5916
+ }
5917
+ )
5918
+ ),
5919
+ // @ts-ignore
5920
+ insert(table) {
5921
+ return {
5922
+ values: (values) => {
5923
+ const inner = {
5924
+ onConflictDoNothing: () => queue.add(
5925
+ () => database.wrap(
5926
+ {
5927
+ method: `${getTableName2(table) ?? "unknown"}.insert()`
5928
+ },
5929
+ async () => {
5930
+ checkOnchainTable(table, "insert");
5931
+ if (Array.isArray(values)) {
5932
+ const rows = [];
5933
+ for (const value of values) {
5934
+ const entry = getCacheEntry(table, value);
5935
+ let row;
5936
+ if (entry?.row) {
5937
+ row = entry.row;
5938
+ } else {
5939
+ if (isDatabaseEmpty)
5940
+ row = null;
5941
+ else
5942
+ row = await find(table, value);
5943
+ }
5944
+ if (row === null) {
5945
+ rows.push(
5946
+ setCacheEntry(table, value, 0 /* INSERT */)
5947
+ );
5948
+ } else {
5949
+ rows.push(null);
5950
+ }
5951
+ }
5952
+ return rows;
5953
+ } else {
5954
+ const entry = getCacheEntry(table, values);
5955
+ let row;
5956
+ if (entry?.row) {
5957
+ row = entry.row;
5958
+ } else {
5959
+ if (isDatabaseEmpty)
5960
+ row = null;
5961
+ else
5962
+ row = await find(table, values);
5963
+ }
5964
+ if (row === null) {
5965
+ return setCacheEntry(table, values, 0 /* INSERT */);
5966
+ }
5967
+ return null;
5968
+ }
5969
+ }
5970
+ )
5971
+ ),
5972
+ onConflictDoUpdate: (valuesU) => queue.add(
5973
+ () => database.wrap(
5974
+ {
5975
+ method: `${getTableName2(table) ?? "unknown"}.insert()`
5976
+ },
5977
+ async () => {
5978
+ checkOnchainTable(table, "insert");
5979
+ if (Array.isArray(values)) {
5980
+ const rows = [];
5981
+ for (const value of values) {
5982
+ const entry = getCacheEntry(table, value);
5983
+ deleteCacheEntry(table, value);
5984
+ let row;
5985
+ if (entry?.row) {
5986
+ row = entry.row;
5987
+ } else {
5988
+ if (isDatabaseEmpty)
5989
+ row = null;
5990
+ else
5991
+ row = await find(table, value);
5992
+ }
5993
+ if (row === null) {
5994
+ rows.push(
5995
+ setCacheEntry(table, value, 0 /* INSERT */)
5996
+ );
5997
+ } else {
5998
+ if (typeof valuesU === "function") {
5999
+ rows.push(
6000
+ setCacheEntry(
6001
+ table,
6002
+ valuesU(row),
6003
+ entry?.type === 0 /* INSERT */ ? 0 /* INSERT */ : 1 /* UPDATE */,
6004
+ row
6005
+ )
6006
+ );
6007
+ } else {
6008
+ rows.push(
6009
+ setCacheEntry(
6010
+ table,
6011
+ valuesU,
6012
+ entry?.type === 0 /* INSERT */ ? 0 /* INSERT */ : 1 /* UPDATE */,
6013
+ row
6014
+ )
6015
+ );
6016
+ }
6017
+ }
6018
+ }
6019
+ return rows;
6020
+ } else {
6021
+ const entry = getCacheEntry(table, values);
6022
+ deleteCacheEntry(table, values);
6023
+ let row;
6024
+ if (entry?.row) {
6025
+ row = entry.row;
6026
+ } else {
6027
+ if (isDatabaseEmpty)
6028
+ row = null;
6029
+ else
6030
+ row = await find(table, values);
6031
+ }
6032
+ if (row === null) {
6033
+ return setCacheEntry(table, values, 0 /* INSERT */);
6034
+ } else {
6035
+ if (typeof valuesU === "function") {
6036
+ return setCacheEntry(
6037
+ table,
6038
+ valuesU(row),
6039
+ entry?.type === 0 /* INSERT */ ? 0 /* INSERT */ : 1 /* UPDATE */,
6040
+ row
6041
+ );
6042
+ } else {
6043
+ return setCacheEntry(
6044
+ table,
6045
+ valuesU,
6046
+ entry?.type === 0 /* INSERT */ ? 0 /* INSERT */ : 1 /* UPDATE */,
6047
+ row
6048
+ );
6049
+ }
6050
+ }
6051
+ }
6052
+ }
6053
+ )
6054
+ ),
6055
+ // biome-ignore lint/suspicious/noThenProperty: <explanation>
6056
+ then: (onFulfilled, onRejected) => queue.add(
6057
+ () => database.wrap(
6058
+ {
6059
+ method: `${getTableName2(table) ?? "unknown"}.insert()`
6060
+ },
6061
+ async () => {
6062
+ checkOnchainTable(table, "insert");
6063
+ if (Array.isArray(values)) {
6064
+ const rows = [];
6065
+ for (const value of values) {
6066
+ if (getCacheEntry(table, value)?.row) {
6067
+ const error = new UniqueConstraintError(
6068
+ `Unique constraint failed for '${getTableName2(table)}'.`
6069
+ );
6070
+ error.meta.push(
6071
+ `db.insert arguments:
6072
+ ${prettyPrint(value)}`
6073
+ );
6074
+ throw error;
6075
+ } else if (isDatabaseEmpty === false) {
6076
+ const findResult = await find(table, value);
6077
+ if (findResult) {
6078
+ const error = new UniqueConstraintError(
6079
+ `Unique constraint failed for '${getTableName2(table)}'.`
6080
+ );
6081
+ error.meta.push(
6082
+ `db.insert arguments:
6083
+ ${prettyPrint(value)}`
6084
+ );
6085
+ throw error;
6086
+ }
6087
+ }
6088
+ rows.push(
6089
+ setCacheEntry(table, value, 0 /* INSERT */)
6090
+ );
6091
+ }
6092
+ return rows;
6093
+ } else {
6094
+ if (getCacheEntry(table, values)?.row) {
6095
+ const error = new UniqueConstraintError(
6096
+ `Unique constraint failed for '${getTableName2(table)}'.`
6097
+ );
6098
+ error.meta.push(
6099
+ `db.insert arguments:
6100
+ ${prettyPrint(values)}`
6101
+ );
6102
+ throw error;
6103
+ } else if (isDatabaseEmpty === false) {
6104
+ const findResult = await find(table, values);
6105
+ if (findResult) {
6106
+ const error = new UniqueConstraintError(
6107
+ `Unique constraint failed for '${getTableName2(table)}'.`
6108
+ );
6109
+ error.meta.push(
6110
+ `db.insert arguments:
6111
+ ${prettyPrint(values)}`
6112
+ );
6113
+ throw error;
6114
+ }
6115
+ }
6116
+ return setCacheEntry(table, values, 0 /* INSERT */);
6117
+ }
6118
+ }
6119
+ )
6120
+ ).then(onFulfilled, onRejected),
6121
+ catch: (onRejected) => inner.then(void 0, onRejected),
6122
+ finally: (onFinally) => inner.then(
6123
+ (value) => {
6124
+ onFinally?.();
6125
+ return value;
6126
+ },
6127
+ (reason) => {
6128
+ onFinally?.();
6129
+ throw reason;
6130
+ }
6131
+ )
6132
+ // @ts-ignore
6133
+ };
6134
+ return inner;
6135
+ }
6136
+ };
6137
+ },
6138
+ // @ts-ignore
6139
+ update(table, key) {
6140
+ return {
6141
+ set: (values) => queue.add(
6142
+ () => database.wrap(
6143
+ { method: `${getTableName2(table) ?? "unknown"}.update()` },
6144
+ async () => {
6145
+ checkOnchainTable(table, "update");
6146
+ const entry = getCacheEntry(table, key);
6147
+ deleteCacheEntry(table, key);
6148
+ let row;
6149
+ if (entry?.row) {
6150
+ row = entry.row;
6151
+ } else {
6152
+ if (isDatabaseEmpty) {
6153
+ const error = new RecordNotFoundError(
6154
+ `No existing record found in table '${getTableName2(table)}'`
6155
+ );
6156
+ error.meta.push(
6157
+ `db.update arguments:
6158
+ ${prettyPrint(key)}`
6159
+ );
6160
+ throw error;
6161
+ }
6162
+ const findResult = await find(table, key);
6163
+ if (findResult) {
6164
+ row = findResult;
6165
+ } else {
6166
+ const error = new RecordNotFoundError(
6167
+ `No existing record found in table '${getTableName2(table)}'`
6168
+ );
6169
+ error.meta.push(
6170
+ `db.update arguments:
6171
+ ${prettyPrint(key)}`
6172
+ );
6173
+ throw error;
6174
+ }
6175
+ }
6176
+ if (typeof values === "function") {
6177
+ return setCacheEntry(
6178
+ table,
6179
+ values(row),
6180
+ entry?.type === 0 /* INSERT */ ? 0 /* INSERT */ : 1 /* UPDATE */,
6181
+ row
6182
+ );
6183
+ } else {
6184
+ return setCacheEntry(
6185
+ table,
6186
+ values,
6187
+ entry?.type === 0 /* INSERT */ ? 0 /* INSERT */ : 1 /* UPDATE */,
6188
+ row
6189
+ );
6190
+ }
6191
+ }
6192
+ )
6193
+ )
6194
+ };
6195
+ },
6196
+ // @ts-ignore
6197
+ delete: (table, key) => queue.add(
6198
+ () => database.wrap(
6199
+ { method: `${getTableName2(table) ?? "unknown"}.delete()` },
6200
+ async () => {
6201
+ checkOnchainTable(table, "delete");
6202
+ const entry = getCacheEntry(table, key);
6203
+ deleteCacheEntry(table, key);
6204
+ if (entry?.row) {
6205
+ if (entry.type === 0 /* INSERT */) {
6206
+ return true;
6207
+ }
6208
+ await database.qb.drizzle.delete(table).where(getWhereCondition(table, key));
6209
+ return true;
6210
+ } else {
6211
+ if (isDatabaseEmpty) {
6212
+ return false;
6213
+ }
6214
+ const deleteResult = await database.qb.drizzle.delete(table).where(getWhereCondition(table, key)).returning();
6215
+ return deleteResult.length > 0;
6216
+ }
6217
+ }
6218
+ )
6219
+ ),
6220
+ // @ts-ignore
6221
+ sql: drizzle(
6222
+ async (_sql, params, method, typings) => {
6223
+ await database.createTriggers();
6224
+ await indexingStore.flush();
6225
+ await database.removeTriggers();
6226
+ isDatabaseEmpty = false;
6227
+ const query2 = { sql: _sql, params, typings };
6228
+ const res = await database.wrap({ method: "sql" }, async () => {
6229
+ try {
6230
+ return await database.qb.drizzle._.session.prepareQuery(query2, void 0, void 0, method === "all").execute();
6231
+ } catch (e) {
6232
+ throw parseSqlError(e);
6233
+ }
6234
+ });
6235
+ return { rows: res.rows.map((row) => Object.values(row)) };
6236
+ },
6237
+ { schema, casing: "snake_case" }
6238
+ ),
6239
+ async flush() {
6240
+ await queue.add(async () => {
6241
+ let cacheSize = 0;
6242
+ for (const c of cache.values())
6243
+ cacheSize += c.size;
6244
+ const flushIndex = totalCacheOps - cacheSize * (1 - common.options.indexingCacheFlushRatio);
6245
+ const shouldDelete = cacheBytes > maxBytes;
6246
+ if (shouldDelete)
6247
+ isDatabaseEmpty = false;
6248
+ const promises = [];
6249
+ for (const [table, tableCache] of cache) {
6250
+ const batchSize = Math.round(
6251
+ common.options.databaseMaxQueryParameters / Object.keys(getTableColumns4(table)).length
6252
+ );
6253
+ const insertValues = [];
6254
+ const updateValues = [];
6255
+ for (const [key, entry] of tableCache) {
6256
+ if (entry.type === 0 /* INSERT */) {
6257
+ insertValues.push(entry.row);
6258
+ }
6259
+ if (entry.type === 1 /* UPDATE */) {
6260
+ updateValues.push(entry.row);
6261
+ }
6262
+ if (shouldDelete && entry.operationIndex < flushIndex) {
6263
+ tableCache.delete(key);
6264
+ cacheBytes -= entry.bytes;
6265
+ }
6266
+ entry.type = 2 /* FIND */;
6267
+ }
6268
+ if (insertValues.length > 0) {
6269
+ common.logger.debug({
6270
+ service: "indexing",
6271
+ msg: `Inserting ${insertValues.length} cached '${getTableName2(table)}' rows into the database`
6272
+ });
6273
+ while (insertValues.length > 0) {
6274
+ const values = insertValues.splice(0, batchSize);
6275
+ promises.push(
6276
+ database.wrap(
6277
+ { method: `${getTableName2(table)}.flush()` },
6278
+ async () => {
6279
+ await database.qb.drizzle.insert(table).values(values).catch((_error) => {
6280
+ const error = _error;
6281
+ common.logger.error({
6282
+ service: "indexing",
6283
+ msg: "Internal error occurred while flushing cache. Please report this error here: https://github.com/ponder-sh/ponder/issues"
6284
+ });
6285
+ throw new FlushError(error.message);
6286
+ });
6287
+ }
6288
+ )
6289
+ );
6290
+ }
6291
+ }
6292
+ if (updateValues.length > 0) {
6293
+ common.logger.debug({
6294
+ service: "indexing",
6295
+ msg: `Updating ${updateValues.length} cached '${getTableName2(table)}' rows in the database`
6296
+ });
6297
+ const primaryKeys = primaryKeysCache.get(table);
6298
+ const set = {};
6299
+ for (const [columnName, column] of Object.entries(
6300
+ getTableColumns4(table)
6301
+ )) {
6302
+ set[columnName] = sql3.raw(
6303
+ `excluded."${getColumnCasing(column, "snake_case")}"`
6304
+ );
6305
+ }
6306
+ while (updateValues.length > 0) {
6307
+ const values = updateValues.splice(0, batchSize);
6308
+ promises.push(
6309
+ database.wrap(
6310
+ {
6311
+ method: `${getTableName2(table)}.flush()`
6312
+ },
6313
+ async () => {
6314
+ await database.qb.drizzle.insert(table).values(values).onConflictDoUpdate({
6315
+ // @ts-ignore
6316
+ target: primaryKeys.map(({ js }) => table[js]),
6317
+ set
6318
+ }).catch((_error) => {
6319
+ const error = _error;
6320
+ common.logger.error({
6321
+ service: "indexing",
6322
+ msg: "Internal error occurred while flushing cache. Please report this error here: https://github.com/ponder-sh/ponder/issues"
6323
+ });
6324
+ throw new FlushError(error.message);
6325
+ });
6326
+ }
6327
+ )
6328
+ );
6329
+ }
6330
+ }
6331
+ }
6332
+ await Promise.all(promises);
6333
+ });
6334
+ },
6335
+ isCacheFull() {
6336
+ return cacheBytes > maxBytes;
6337
+ }
6338
+ };
6339
+ return indexingStore;
6340
+ };
6341
+
6342
+ // src/indexing-store/realtime.ts
6343
+ import {
6344
+ and as and2,
6345
+ eq as eq2,
6346
+ getTableName as getTableName3
6347
+ } from "drizzle-orm";
6348
+ import { getTableConfig as getTableConfig5 } from "drizzle-orm/pg-core";
6349
+ import { drizzle as drizzle2 } from "drizzle-orm/pg-proxy";
6350
+ var checkOnchainTable2 = (table, method) => {
6351
+ if (table === void 0)
6352
+ throw new UndefinedTableError(
6353
+ `Table object passed to db.${method}() is undefined`
6354
+ );
6355
+ if (onchain in table)
6356
+ return;
6357
+ throw new InvalidStoreMethodError(
6358
+ method === "find" ? `db.find() can only be used with onchain tables, and '${getTableConfig5(table).name}' is an offchain table.` : `Indexing functions can only write to onchain tables, and '${getTableConfig5(table).name}' is an offchain table.`
6359
+ );
6360
+ };
6361
+ var createRealtimeIndexingStore = ({
6362
+ schemaBuild: { schema },
6363
+ database
6364
+ }) => {
6365
+ const queue = createQueue({
6366
+ browser: false,
6367
+ initialStart: true,
6368
+ concurrency: 1,
6369
+ worker: (fn) => {
6370
+ return fn();
6371
+ }
6372
+ });
6373
+ const primaryKeysCache = /* @__PURE__ */ new Map();
6374
+ for (const tableName of getTableNames(schema)) {
6375
+ primaryKeysCache.set(
6376
+ schema[tableName.js],
6377
+ getPrimaryKeyColumns(schema[tableName.js])
6378
+ );
6379
+ }
6380
+ const getCacheKey = (table, row) => {
6381
+ const primaryKeys = primaryKeysCache.get(table);
6382
+ return primaryKeys.map((pk) => normalizeColumn(table[pk.js], row[pk.js])).join("_");
6383
+ };
6384
+ const getWhereCondition = (table, key) => {
6385
+ primaryKeysCache.get(table);
6386
+ const conditions = [];
6387
+ for (const { js } of primaryKeysCache.get(table)) {
6388
+ conditions.push(eq2(table[js], key[js]));
6389
+ }
6390
+ return and2(...conditions);
6391
+ };
6392
+ const find = (table, key) => {
6393
+ return database.qb.drizzle.select().from(table).where(getWhereCondition(table, key)).then((res) => res.length === 0 ? null : res[0]);
6394
+ };
6395
+ const indexingStore = {
6396
+ // @ts-ignore
6397
+ find: (table, key) => queue.add(
6398
+ () => database.wrap(
6399
+ { method: `${getTableName3(table) ?? "unknown"}.find()` },
6400
+ async () => {
6401
+ checkOnchainTable2(table, "find");
6402
+ return find(table, key);
6403
+ }
6404
+ )
6405
+ ),
6406
+ // @ts-ignore
6407
+ insert(table) {
6408
+ return {
6409
+ values: (values) => {
6410
+ const inner = {
6411
+ onConflictDoNothing: () => queue.add(
6412
+ () => database.wrap(
6413
+ {
6414
+ method: `${getTableName3(table) ?? "unknown"}.insert()`
6415
+ },
6416
+ async () => {
6417
+ checkOnchainTable2(table, "insert");
6418
+ const parseResult = (result) => {
6419
+ if (Array.isArray(values) === false) {
6420
+ return result.length === 1 ? result[0] : null;
6421
+ }
6422
+ const rows = [];
6423
+ let resultIndex = 0;
6424
+ for (let i = 0; i < values.length; i++) {
6425
+ if (getCacheKey(table, values[i]) === getCacheKey(table, result[resultIndex])) {
6426
+ rows.push(result[resultIndex++]);
6427
+ } else {
6428
+ rows.push(null);
6429
+ }
6430
+ }
6431
+ return rows;
6432
+ };
6433
+ try {
6434
+ return await database.qb.drizzle.insert(table).values(values).onConflictDoNothing().returning().then(parseResult);
6435
+ } catch (e) {
6436
+ throw parseSqlError(e);
6437
+ }
6438
+ }
6439
+ )
6440
+ ),
6441
+ onConflictDoUpdate: (valuesU) => queue.add(
6442
+ () => database.wrap(
6443
+ {
6444
+ method: `${getTableName3(table) ?? "unknown"}.insert()`
6445
+ },
6446
+ async () => {
6447
+ checkOnchainTable2(table, "insert");
6448
+ if (typeof valuesU === "object") {
6449
+ try {
6450
+ return await database.qb.drizzle.insert(table).values(values).onConflictDoUpdate({
6451
+ target: primaryKeysCache.get(table).map(({ js }) => table[js]),
6452
+ set: valuesU
6453
+ }).returning().then(
6454
+ (res) => Array.isArray(values) ? res : res[0]
6455
+ );
6456
+ } catch (e) {
6457
+ throw parseSqlError(e);
6458
+ }
6459
+ }
6460
+ if (Array.isArray(values)) {
6461
+ const rows = [];
6462
+ for (const value of values) {
6463
+ const row = await find(table, value);
6464
+ if (row === null) {
6465
+ try {
6466
+ rows.push(
6467
+ await database.qb.drizzle.insert(table).values(value).returning().then((res) => res[0])
6468
+ );
6469
+ } catch (e) {
6470
+ throw parseSqlError(e);
6471
+ }
6472
+ } else {
6473
+ try {
6474
+ rows.push(
6475
+ await database.qb.drizzle.update(table).set(valuesU(row)).where(getWhereCondition(table, value)).returning().then((res) => res[0])
6476
+ );
6477
+ } catch (e) {
6478
+ throw parseSqlError(e);
6479
+ }
6480
+ }
6481
+ }
6482
+ return rows;
6483
+ } else {
6484
+ const row = await find(table, values);
6485
+ if (row === null) {
6486
+ try {
6487
+ return await database.qb.drizzle.insert(table).values(values).returning().then((res) => res[0]);
6488
+ } catch (e) {
6489
+ throw parseSqlError(e);
6490
+ }
6491
+ } else {
6492
+ try {
6493
+ return await database.qb.drizzle.update(table).set(valuesU(row)).where(getWhereCondition(table, values)).returning().then((res) => res[0]);
6494
+ } catch (e) {
6495
+ throw parseSqlError(e);
6496
+ }
6497
+ }
6498
+ }
6499
+ }
6500
+ )
6501
+ ),
6502
+ // biome-ignore lint/suspicious/noThenProperty: <explanation>
6503
+ then: (onFulfilled, onRejected) => queue.add(
6504
+ () => database.wrap(
6505
+ {
6506
+ method: `${getTableName3(table) ?? "unknown"}.insert()`
6507
+ },
6508
+ async () => {
6509
+ checkOnchainTable2(table, "insert");
6510
+ try {
6511
+ return await database.qb.drizzle.insert(table).values(values).returning().then(
6512
+ (res) => Array.isArray(values) ? res : res[0]
6513
+ );
6514
+ } catch (e) {
6515
+ throw parseSqlError(e);
6516
+ }
6517
+ }
6518
+ )
6519
+ ).then(onFulfilled, onRejected),
6520
+ catch: (onRejected) => inner.then(void 0, onRejected),
6521
+ finally: (onFinally) => inner.then(
6522
+ (value) => {
6523
+ onFinally?.();
6524
+ return value;
6525
+ },
6526
+ (reason) => {
6527
+ onFinally?.();
6528
+ throw reason;
6529
+ }
6530
+ )
6531
+ // @ts-ignore
6532
+ };
6533
+ return inner;
6534
+ }
6535
+ };
6536
+ },
6537
+ // @ts-ignore
6538
+ update(table, key) {
6539
+ return {
6540
+ set: (values) => queue.add(
6541
+ () => database.wrap(
6542
+ { method: `${getTableName3(table) ?? "unknown"}.update()` },
6543
+ async () => {
6544
+ checkOnchainTable2(table, "update");
6545
+ if (typeof values === "function") {
6546
+ const row = await find(table, key);
6547
+ if (row === null) {
6548
+ const error = new RecordNotFoundError(
6549
+ `No existing record found in table '${getTableName3(table)}'`
6550
+ );
6551
+ error.meta.push(
6552
+ `db.update arguments:
6553
+ ${prettyPrint(key)}`
6554
+ );
6555
+ throw error;
6556
+ }
6557
+ try {
6558
+ return await database.qb.drizzle.update(table).set(values(row)).where(getWhereCondition(table, key)).returning().then((res) => res[0]);
6559
+ } catch (e) {
6560
+ throw parseSqlError(e);
6561
+ }
6562
+ } else {
6563
+ try {
6564
+ return await database.qb.drizzle.update(table).set(values).where(getWhereCondition(table, key)).returning().then((res) => res[0]);
6565
+ } catch (e) {
6566
+ throw parseSqlError(e);
6567
+ }
6568
+ }
6569
+ }
6570
+ )
6571
+ )
6572
+ };
6573
+ },
6574
+ // @ts-ignore
6575
+ delete: (table, key) => queue.add(
6576
+ () => database.wrap(
6577
+ { method: `${getTableName3(table) ?? "unknown"}.delete()` },
6578
+ async () => {
6579
+ checkOnchainTable2(table, "delete");
6580
+ const deleted = await database.qb.drizzle.delete(table).where(getWhereCondition(table, key)).returning();
6581
+ return deleted.length > 0;
6582
+ }
6583
+ )
6584
+ ),
6585
+ // @ts-ignore
6586
+ sql: drizzle2(
6587
+ (_sql, params, method, typings) => (
6588
+ // @ts-ignore
6589
+ queue.add(async () => {
6590
+ const query2 = { sql: _sql, params, typings };
6591
+ const res = await database.wrap({ method: "sql" }, async () => {
6592
+ try {
6593
+ return await database.qb.drizzle._.session.prepareQuery(query2, void 0, void 0, method === "all").execute();
6594
+ } catch (e) {
6595
+ throw parseSqlError(e);
6596
+ }
6597
+ });
6598
+ return { rows: res.rows.map((row) => Object.values(row)) };
6599
+ })
6600
+ ),
6601
+ { schema, casing: "snake_case" }
6602
+ )
6603
+ };
6604
+ return indexingStore;
6605
+ };
6606
+
4946
6607
  // src/utils/extend.ts
4947
6608
  var extend = (create2, _methods) => {
4948
6609
  return (...params) => {
@@ -10855,7 +12516,7 @@ async function dev({ cliOptions }) {
10855
12516
  }
10856
12517
 
10857
12518
  // src/bin/commands/list.ts
10858
- import { sql as sql3 } from "kysely";
12519
+ import { sql as sql4 } from "kysely";
10859
12520
  var emptySchemaBuild = {
10860
12521
  schema: {},
10861
12522
  statements: {
@@ -10901,7 +12562,7 @@ async function list({ cliOptions }) {
10901
12562
  ).execute();
10902
12563
  let union;
10903
12564
  for (const row of ponderSchemas) {
10904
- const query2 = database.qb.internal.selectFrom(`${row.table_schema}._ponder_meta`).select(["value", sql3`${row.table_schema}`.as("schema")]).where("key", "=", "app");
12565
+ const query2 = database.qb.internal.selectFrom(`${row.table_schema}._ponder_meta`).select(["value", sql4`${row.table_schema}`.as("schema")]).where("key", "=", "app");
10905
12566
  if (union === void 0) {
10906
12567
  union = query2;
10907
12568
  } else {