@lpdjs/firestore-repo-service 2.3.6 → 2.4.1

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 +1 @@
1
- {"version":3,"sources":["../../src/sync/constants.ts","../../src/sync/adapters/bigquery-types.ts","../../src/sync/adapters/bigquery.ts"],"names":["SYNC_VERSION_COLUMN","normalizeBigQueryType","type","upper","isConcurrentUpdateError","err","e","x","withRetry","fn","maxRetries","baseMs","attempt","cap","delay","res","BigQueryDialect","logical","id","bigqueryDialect","_BigQueryAdapter","options","tableName","exists","metadata","f","fields","result","table","qi","cols","c","notNull","ddl","columns","stmt","rows","primaryKey","allKeys","nonPkCols","k","source","row","i","updateSet","insertCols","insertVals","versionGuard","query","ids","escaped","v","sql","BigQueryAdapter"],"mappings":"AAaO,IAAMA,EAAsB,gBAAA,CCI5B,SAASC,CAAAA,CAAsBC,CAAAA,CAAsB,CAC1D,IAAMC,CAAAA,CAAQD,CAAAA,CAAK,WAAA,GACnB,OAAQC,CAAAA,EACN,KAAK,SAAA,CACH,OAAO,OAAA,CACT,KAAK,OAAA,CACH,OAAO,UACT,KAAK,SAAA,CACH,OAAO,MAAA,CACT,QACE,OAAOA,CACX,CACF,CCVA,SAASC,CAAAA,CAAwBC,CAAAA,CAAuB,CACtD,GAAI,CAACA,GAAO,OAAOA,CAAAA,EAAQ,QAAA,CAAU,OAAO,OAC5C,IAAMC,CAAAA,CAAID,CAAAA,CACV,OAAIC,EAAE,IAAA,GAAY,GAAA,CAAY,KAAA,CAAA,CACf,KAAA,CAAM,QAAQA,CAAAA,CAAE,MAAS,EAAIA,CAAAA,CAAE,MAAA,CAAY,EAAC,EAC7C,IAAA,CACXC,CAAAA,EACC,OAAOA,GAAG,OAAA,EAAY,QAAA,EACtBA,CAAAA,CAAE,OAAA,CAAQ,aAAY,CAAE,QAAA,CAAS,kBAAkB,CACvD,CACF,CAMA,eAAeC,EACbC,CAAAA,CACAC,CAAAA,CAAa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,OAAOA,CAAAA,EAAM,UAAY,OAAOA,CAAAA,EAAM,SAAiB,MAAA,CAAOA,CAAC,EAC/D,OAAOA,CAAAA,EAAM,SAEX/B,CAAAA,CAAgB,gBAAA,CAAiB,KAAK+B,CAAC,CAAA,CAClC,cAAcA,CAAC,CAAA,EAAA,CAAA,CAIrBA,EAAE,UAAA,CAAW,GAAG,GAAKA,CAAAA,CAAE,QAAA,CAAS,GAAG,CAAA,EACnCA,CAAAA,CAAE,WAAW,GAAG,CAAA,EAAKA,EAAE,QAAA,CAAS,GAAG,EAE7B,CAAA,YAAA,EAAeA,CAAAA,CAAE,QAAQ,IAAA,CAAM,KAAK,CAAC,CAAA,EAAA,CAAA,CAEvC,CAAA,CAAA,EAAIA,EAAE,OAAA,CAAQ,IAAA,CAAM,KAAK,CAAC,CAAA,CAAA,CAAA,CAG5B,eAAe,IAAA,CAAK,SAAA,CAAUA,CAAC,CAAA,CAAE,OAAA,CAAQ,KAAM,KAAK,CAAC,IAC9D,CACF,EAtMa/B,EA6Ka,gBAAA,CACtB,sCAAA,KA9KSiC,CAAAA,CAANjC","file":"bigquery.js","sourcesContent":["/**\n * Internal constants shared between the worker, queue, schema mapper and\n * SQL adapters.\n */\n\n/**\n * Name of the SQL column that stores the publish-time `version` of each\n * sync event. Used by the worker to discard out-of-order PubSub deliveries\n * (the MERGE only updates rows when the incoming version is strictly\n * greater than the stored one).\n *\n * Two underscores prefix avoids collisions with user-defined fields.\n */\nexport const SYNC_VERSION_COLUMN = \"__sync_version\";\n","/**\n * BigQuery type-name utilities shared by the legacy `BigQueryAdapter` (DML\n * MERGE) and the new `BigQueryStorageAdapter` (Storage Write API).\n *\n * - {@link normalizeBigQueryType} canonicalises BigQuery type strings so that\n * `INTEGER`/`INT64`, `FLOAT`/`FLOAT64`, `BOOLEAN`/`BOOL`, etc. compare\n * equal during schema-drift detection.\n * - {@link isBigQueryTypeCompatible} returns whether a desired type can\n * safely keep an existing column of another type — only widenings that\n * BigQuery accepts via `ALTER COLUMN SET DATA TYPE` are allowed.\n */\n\n/**\n * Canonicalise a BigQuery type name returned by `getMetadata().schema`\n * (which may use legacy aliases like `INTEGER` or `FLOAT`) so that it can\n * be compared against the type produced by `BigQueryDialect.mapType()`.\n */\nexport function normalizeBigQueryType(type: string): string {\n const upper = type.toUpperCase();\n switch (upper) {\n case \"INTEGER\":\n return \"INT64\";\n case \"FLOAT\":\n return \"FLOAT64\";\n case \"BOOLEAN\":\n return \"BOOL\";\n default:\n return upper;\n }\n}\n\n/**\n * Whether `desired` is the same as, or a safe widening of, `existing`.\n * The widenings mirror what BigQuery allows via\n * `ALTER COLUMN x SET DATA TYPE …` — see\n * https://cloud.google.com/bigquery/docs/managing-table-schemas#change_column_types\n */\nexport function isBigQueryTypeCompatible(\n existing: string,\n desired: string,\n): boolean {\n const a = normalizeBigQueryType(existing);\n const b = normalizeBigQueryType(desired);\n if (a === b) return true;\n\n const widenings: Record<string, string[]> = {\n INT64: [\"NUMERIC\", \"BIGNUMERIC\", \"FLOAT64\"],\n NUMERIC: [\"BIGNUMERIC\", \"FLOAT64\"],\n DATE: [\"DATETIME\", \"TIMESTAMP\"],\n DATETIME: [\"TIMESTAMP\"],\n };\n return widenings[a]?.includes(b) ?? false;\n}\n","import { SYNC_VERSION_COLUMN } from \"../constants\";\nimport type {\n LogicalType,\n SqlAdapter,\n SqlColumn,\n SqlDialect,\n SqlTableDef,\n} from \"../types\";\nimport { normalizeBigQueryType } from \"./bigquery-types\";\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\n/**\n * Returns true when the BigQuery error is a \"concurrent update\" serialization\n * conflict (code 400, reason \"invalidQuery\" containing \"serialize access\").\n * These are safe to retry after a brief back-off.\n */\nfunction isConcurrentUpdateError(err: unknown): boolean {\n if (!err || typeof err !== \"object\") return false;\n const e = err as Record<string, unknown>;\n if (e[\"code\"] !== 400) return false;\n const errors = Array.isArray(e[\"errors\"]) ? e[\"errors\"] : [];\n return errors.some(\n (x: any) =>\n typeof x?.message === \"string\" &&\n x.message.toLowerCase().includes(\"serialize access\"),\n );\n}\n\n/**\n * Execute `fn`, retrying up to `maxRetries` times when BigQuery returns a\n * concurrent-update error. Uses full-jitter exponential back-off.\n */\nasync function withRetry<T>(\n fn: () => Promise<T>,\n maxRetries = 10,\n baseMs = 500,\n): Promise<T> {\n let attempt = 0;\n while (true) {\n try {\n return await fn();\n } catch (err) {\n attempt++;\n if (!isConcurrentUpdateError(err) || attempt > maxRetries) throw err;\n const cap = baseMs * Math.pow(2, attempt);\n const delay = Math.random() * cap; // full jitter\n await new Promise((res) => setTimeout(res, delay));\n }\n }\n}\n\n// ---------------------------------------------------------------------------\n// Dialect (internal — used only by BigQueryAdapter)\n// ---------------------------------------------------------------------------\n\n/** BigQuery SQL dialect mapping. */\nclass BigQueryDialect implements SqlDialect {\n readonly name = \"bigquery\";\n\n mapType(logical: LogicalType): string {\n switch (logical) {\n case \"string\":\n return \"STRING\";\n case \"number\":\n return \"FLOAT64\";\n case \"bigint\":\n return \"INT64\";\n case \"boolean\":\n return \"BOOL\";\n case \"timestamp\":\n return \"TIMESTAMP\";\n case \"json\":\n return \"JSON\";\n case \"text\":\n return \"STRING\";\n }\n }\n\n quoteIdentifier(id: string): string {\n return `\\`${id}\\``;\n }\n}\n\n/** Shared BigQuery dialect singleton. */\nexport const bigqueryDialect: SqlDialect = new BigQueryDialect();\n\n// ---------------------------------------------------------------------------\n// Adapter\n// ---------------------------------------------------------------------------\n\n/**\n * BigQuery implementation of {@link SqlAdapter}.\n *\n * Accepts an already-configured BigQuery client so the library does not pull\n * in `@google-cloud/bigquery` as a hard dependency.\n *\n * @example\n * ```ts\n * import { BigQuery } from \"@google-cloud/bigquery\";\n * import { BigQueryAdapter } from \"./adapters/bigquery\";\n *\n * const adapter = new BigQueryAdapter({\n * bigquery: new BigQuery({ projectId: \"my-project\" }),\n * datasetId: \"my_dataset\",\n * });\n * ```\n */\nexport class BigQueryAdapter implements SqlAdapter {\n private readonly bigquery: any;\n private readonly datasetId: string;\n\n constructor(options: { bigquery: any; datasetId: string }) {\n this.bigquery = options.bigquery;\n this.datasetId = options.datasetId;\n }\n\n /** The BigQuery SQL dialect. */\n get dialect(): SqlDialect {\n return bigqueryDialect;\n }\n\n /** Check whether a table exists in the dataset. */\n async tableExists(tableName: string): Promise<boolean> {\n const [exists] = await this.dataset.table(tableName).exists();\n return exists;\n }\n\n /** Return the column names currently present in the table. */\n async getTableColumns(tableName: string): Promise<string[]> {\n const [metadata] = await this.dataset.table(tableName).getMetadata();\n const fields: Array<{ name: string }> = metadata.schema?.fields ?? [];\n return fields.map((f) => f.name);\n }\n\n /**\n * Return existing columns with their normalized BigQuery type strings.\n * Used by the worker to detect type drift before applying schema changes.\n */\n async getTableColumnsWithTypes(\n tableName: string,\n ): Promise<Map<string, string>> {\n const [metadata] = await this.dataset.table(tableName).getMetadata();\n const fields: Array<{ name: string; type: string }> =\n metadata.schema?.fields ?? [];\n const result = new Map<string, string>();\n for (const f of fields) {\n result.set(f.name, normalizeBigQueryType(f.type));\n }\n return result;\n }\n\n /** Create a table using a fully-qualified name. */\n async createTable(table: SqlTableDef): Promise<void> {\n const qi = (id: string) => this.dialect.quoteIdentifier(id);\n const cols = table.columns\n .map((c) => {\n const notNull = c.isPrimaryKey ? \" NOT NULL\" : \"\";\n return ` ${qi(c.name)} ${c.sqlType}${notNull}`;\n })\n .join(\",\\n\");\n\n const ddl = `CREATE TABLE IF NOT EXISTS ${this.fqn(table.tableName)} (\\n${cols}\\n);`;\n await this.bigquery.query({ query: ddl });\n }\n\n /** Add columns to an existing table using a fully-qualified name. */\n async addColumns(tableName: string, columns: SqlColumn[]): Promise<void> {\n const qi = (id: string) => this.dialect.quoteIdentifier(id);\n for (const c of columns) {\n const stmt = `ALTER TABLE ${this.fqn(tableName)} ADD COLUMN ${qi(c.name)} ${c.sqlType};`;\n await this.bigquery.query({ query: stmt });\n }\n }\n\n /** Append rows via BigQuery streaming insert. */\n async insertRows(\n tableName: string,\n rows: Record<string, unknown>[],\n ): Promise<void> {\n if (rows.length === 0) return;\n await this.dataset.table(tableName).insert(rows);\n }\n\n /**\n * Upsert rows using a MERGE DML statement.\n *\n * Builds a source table from inline SELECT … UNION ALL rows and merges\n * into the target on the given primary key.\n */\n async upsertRows(\n tableName: string,\n rows: Record<string, unknown>[],\n primaryKey: string,\n ): Promise<void> {\n if (rows.length === 0) return;\n\n const allKeys = Object.keys(rows[0]!);\n const nonPkCols = allKeys.filter((k) => k !== primaryKey);\n const qi = (id: string) => this.dialect.quoteIdentifier(id);\n\n // Build inline source: SELECT val AS col, … UNION ALL SELECT …\n const selects = rows.map((row, i) => {\n const values = allKeys\n .map((k) => {\n const aliased =\n i === 0\n ? `${this.escapeValue(row[k])} AS ${qi(k)}`\n : this.escapeValue(row[k]);\n return aliased;\n })\n .join(\", \");\n return `SELECT ${values}`;\n });\n\n const source = selects.join(\" UNION ALL\\n \");\n\n // UPDATE SET clause (non-PK columns).\n // Note: when __sync_version is present we still update it so the row\n // tracks the latest applied version.\n const updateSet = nonPkCols\n .map((c) => `T.${qi(c)} = S.${qi(c)}`)\n .join(\", \");\n\n // INSERT columns / values\n const insertCols = allKeys.map((c) => qi(c)).join(\", \");\n const insertVals = allKeys.map((c) => `S.${qi(c)}`).join(\", \");\n\n // Out-of-order protection: only UPDATE when the incoming version is\n // strictly greater than the stored one (NULL stored version means the\n // row pre-dates versioning → always update).\n const versionGuard = allKeys.includes(SYNC_VERSION_COLUMN)\n ? ` AND (T.${qi(SYNC_VERSION_COLUMN)} IS NULL OR S.${qi(SYNC_VERSION_COLUMN)} > T.${qi(SYNC_VERSION_COLUMN)})`\n : \"\";\n\n const query = [\n `MERGE ${this.fqn(tableName)} AS T`,\n `USING (\\n ${source}\\n ) AS S`,\n `ON T.${qi(primaryKey)} = S.${qi(primaryKey)}`,\n `WHEN MATCHED${versionGuard} THEN UPDATE SET ${updateSet}`,\n `WHEN NOT MATCHED THEN INSERT (${insertCols}) VALUES (${insertVals});`,\n ].join(\"\\n\");\n\n await withRetry(() => this.bigquery.query({ query }));\n }\n\n /** Delete rows by primary-key values. */\n async deleteRows(\n tableName: string,\n primaryKey: string,\n ids: string[],\n ): Promise<void> {\n if (ids.length === 0) return;\n\n const qi = (id: string) => this.dialect.quoteIdentifier(id);\n const escaped = ids.map((v) => this.escapeValue(v)).join(\", \");\n const query = `DELETE FROM ${this.fqn(tableName)} WHERE ${qi(primaryKey)} IN (${escaped});`;\n\n await withRetry(() => this.bigquery.query({ query }));\n }\n\n /** Execute a raw SQL statement (used by the migration manager). */\n async executeRaw(sql: string): Promise<void> {\n await this.bigquery.query({ query: sql });\n }\n\n // -------------------------------------------------------------------------\n // Private helpers\n // -------------------------------------------------------------------------\n\n /** The BigQuery Dataset handle. */\n private get dataset() {\n return this.bigquery.dataset(this.datasetId);\n }\n\n /** Return the fully-qualified table reference (`` `dataset.table` ``). */\n private fqn(tableName: string): string {\n return `\\`${this.datasetId}.${tableName}\\``;\n }\n\n /** ISO 8601 timestamp pattern (e.g. 2026-03-29T20:59:27.394Z) */\n private static readonly ISO_TIMESTAMP_RE =\n /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}/;\n\n /** Escape a value for use as a SQL literal. */\n private escapeValue(v: unknown): string {\n if (v === null || v === undefined) return \"NULL\";\n if (typeof v === \"boolean\") return v ? \"TRUE\" : \"FALSE\";\n if (typeof v === \"number\" || typeof v === \"bigint\") return String(v);\n if (typeof v === \"string\") {\n // ISO 8601 timestamps → TIMESTAMP literal (keeps type-safety with BQ TIMESTAMP columns)\n if (BigQueryAdapter.ISO_TIMESTAMP_RE.test(v)) {\n return `TIMESTAMP('${v}')`;\n }\n // Detect JSON strings (arrays/objects) → use PARSE_JSON for native JSON columns\n if (\n (v.startsWith(\"[\") && v.endsWith(\"]\")) ||\n (v.startsWith(\"{\") && v.endsWith(\"}\"))\n ) {\n return `PARSE_JSON('${v.replace(/'/g, \"\\\\'\")}')`;\n }\n return `'${v.replace(/'/g, \"\\\\'\")}'`;\n }\n // Objects / arrays → JSON\n return `PARSE_JSON('${JSON.stringify(v).replace(/'/g, \"\\\\'\")}')`;\n }\n}\n"]}
1
+ {"version":3,"sources":["../../src/sync/constants.ts","../../src/sync/adapters/bigquery-types.ts","../../src/sync/adapters/bigquery.ts"],"names":["SYNC_VERSION_COLUMN","normalizeBigQueryType","type","upper","BigQueryDialect","logical","id","bigqueryDialect","storageNsCache","loadStorageNs","formatChangeSequenceNumber","version","n","isRetryableStorageError","err","e","withRetry","fn","maxRetries","baseMs","attempt","cap","delay","res","_BigQueryAdapter","options","tableName","exists","metadata","f","fields","result","table","qi","pk","c","cols","notNull","opts","optionsClause","ddl","columns","stmt","sql","cached","rows","writer","cdc","row","primaryKey","ids","d","ms","out","k","v","ns","bqSchema","storageSchema","protoDescriptor","destinationTable","connection","BigQueryAdapter"],"mappings":"yPAaO,IAAMA,CAAAA,CAAsB,iBCG5B,SAASC,CAAAA,CAAsBC,EAAsB,CAC1D,IAAMC,EAAQD,CAAAA,CAAK,WAAA,GACnB,OAAQC,CAAAA,EACN,KAAK,SAAA,CACH,OAAO,OAAA,CACT,KAAK,OAAA,CACH,OAAO,SAAA,CACT,KAAK,UACH,OAAO,MAAA,CACT,QACE,OAAOA,CACX,CACF,CC8BA,IAAMC,CAAAA,CAAN,KAA4C,CAA5C,WAAA,EAAA,CACE,KAAS,IAAA,CAAO,WAAA,CAEhB,QAAQC,CAAAA,CAA8B,CACpC,OAAQA,CAAAA,EACN,KAAK,QAAA,CACH,OAAO,QAAA,CACT,KAAK,QAAA,CACH,OAAO,UACT,KAAK,QAAA,CACH,OAAO,OAAA,CACT,KAAK,SAAA,CACH,OAAO,MAAA,CACT,KAAK,YACH,OAAO,WAAA,CACT,KAAK,MAAA,CACH,OAAO,OACT,KAAK,MAAA,CACH,OAAO,QACX,CACF,CAEA,gBAAgBC,CAAAA,CAAoB,CAClC,OAAO,CAAA,EAAA,EAAKA,CAAE,IAChB,CACF,C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AF,CAAAA,CAAG,MAAM,cAAA,EAAe,CACxBA,CAAAA,CAAG,KAAA,CAAM,0BACX,CAAA,CAEMI,CAAAA,CAAmB,CAAA,SAAA,EAAY,KAAK,SAAS,CAAA,UAAA,EAAa,IAAA,CAAK,SAAS,CAAA,QAAA,EAAWlC,CAAS,CAAA,CAAA,CAM5FmC,CAAAA,CAAa,MAAM,IAAA,CAAK,YAAA,CAAa,sBAAA,CAAuB,CAChE,SAAUL,CAAAA,CAAG,aAAA,CAAc,aAAA,CAC3B,gBAAA,CAAAI,CACF,CAAC,CAAA,CAEKd,CAAAA,CAAS,IAAIU,CAAAA,CAAG,aAAA,CAAc,UAAA,CAAW,CAC7C,WAAAK,CAAAA,CACA,eAAA,CAAAF,CACF,CAAC,CAAA,CAED,OAAA,IAAA,CAAK,OAAA,CAAQ,GAAA,CAAIjC,EAAW,CAAE,MAAA,CAAAoB,CAAAA,CAAQ,UAAA,CAAYG,CAAAA,EAAc,EAAG,CAAC,CAAA,CAC7DH,CACT,CAGA,MAAM,KAAA,EAAuB,CAC3B,OAAW,CAAE,MAAA,CAAAA,CAAO,CAAA,GAAK,KAAK,OAAA,CAAQ,MAAA,EAAO,CAC3C,GAAI,CACFA,CAAAA,CAAO,KAAA,GACT,MAAQ,CAER,CAGF,GADA,IAAA,CAAK,QAAQ,KAAA,EAAM,CACf,IAAA,CAAK,YAAA,EAAgB,OAAO,IAAA,CAAK,YAAA,CAAa,KAAA,EAAU,UAAA,CAC1D,GAAI,CACF,IAAA,CAAK,YAAA,CAAa,QACpB,CAAA,KAAQ,CAER,CAEJ,CACF,CAAA,CArUatB,CAAAA,CAsLa,gBAAA,CACtB,sCAAA,KAvLSsC,CAAAA,CAANtC","file":"bigquery.js","sourcesContent":["/**\n * Internal constants shared between the worker, queue, schema mapper and\n * SQL adapters.\n */\n\n/**\n * Name of the SQL column that stores the publish-time `version` of each\n * sync event. Used by the worker to discard out-of-order PubSub deliveries\n * (the MERGE only updates rows when the incoming version is strictly\n * greater than the stored one).\n *\n * Two underscores prefix avoids collisions with user-defined fields.\n */\nexport const SYNC_VERSION_COLUMN = \"__sync_version\";\n","/**\n * BigQuery type-name utilities used by the {@link BigQueryAdapter}.\n *\n * - {@link normalizeBigQueryType} canonicalises BigQuery type strings so that\n * `INTEGER`/`INT64`, `FLOAT`/`FLOAT64`, `BOOLEAN`/`BOOL`, etc. compare\n * equal during schema-drift detection.\n * - {@link isBigQueryTypeCompatible} returns whether a desired type can\n * safely keep an existing column of another type — only widenings that\n * BigQuery accepts via `ALTER COLUMN SET DATA TYPE` are allowed.\n */\n\n/**\n * Canonicalise a BigQuery type name returned by `getMetadata().schema`\n * (which may use legacy aliases like `INTEGER` or `FLOAT`) so that it can\n * be compared against the type produced by `BigQueryDialect.mapType()`.\n */\nexport function normalizeBigQueryType(type: string): string {\n const upper = type.toUpperCase();\n switch (upper) {\n case \"INTEGER\":\n return \"INT64\";\n case \"FLOAT\":\n return \"FLOAT64\";\n case \"BOOLEAN\":\n return \"BOOL\";\n default:\n return upper;\n }\n}\n\n/**\n * Whether `desired` is the same as, or a safe widening of, `existing`.\n * The widenings mirror what BigQuery allows via\n * `ALTER COLUMN x SET DATA TYPE …` — see\n * https://cloud.google.com/bigquery/docs/managing-table-schemas#change_column_types\n */\nexport function isBigQueryTypeCompatible(\n existing: string,\n desired: string,\n): boolean {\n const a = normalizeBigQueryType(existing);\n const b = normalizeBigQueryType(desired);\n if (a === b) return true;\n\n const widenings: Record<string, string[]> = {\n INT64: [\"NUMERIC\", \"BIGNUMERIC\", \"FLOAT64\"],\n NUMERIC: [\"BIGNUMERIC\", \"FLOAT64\"],\n DATE: [\"DATETIME\", \"TIMESTAMP\"],\n DATETIME: [\"TIMESTAMP\"],\n };\n return widenings[a]?.includes(b) ?? false;\n}\n","/**\n * BigQuery adapter — streams Firestore changes to BigQuery via the\n * **Storage Write API** in CDC (Change Data Capture) mode.\n *\n * Why CDC over the legacy MERGE approach:\n *\n * - **No DML concurrency limit.** MERGE/DELETE DML is bounded by ≈ 2\n * concurrent statements per table; busy collections triggered\n * `Could not serialize access … due to concurrent update` errors.\n * The Storage Write API has no such bound, so multiple Cloud Function\n * instances can flush in parallel without conflicts.\n * - **Cheaper at scale.** Storage Write is roughly ~50% cheaper than\n * legacy streaming inserts, and free for the first 2 TiB / month.\n * - **Same idempotency model.** Each row carries `_CHANGE_SEQUENCE_NUMBER`\n * built from the existing `__sync_version` column, so out-of-order\n * PubSub deliveries are merged correctly by BigQuery.\n *\n * Requirements:\n *\n * - Destination tables **must** declare a `PRIMARY KEY (...) NOT ENFORCED`\n * constraint and be clustered on that key. {@link BigQueryAdapter.createTable}\n * handles both for tables managed by this library.\n * - Tables should be created with `OPTIONS(max_staleness = INTERVAL …)`\n * so the CDC merge runs in the background instead of on every read\n * — see {@link BigQueryAdapterOptions.maxStaleness}.\n * - Service account needs `bigquery.tables.updateData` (e.g. via the\n * `roles/bigquery.dataEditor` role).\n *\n * @example\n * ```ts\n * import { BigQuery } from \"@google-cloud/bigquery\";\n * import { BigQueryAdapter } from \"@lpdjs/firestore-repo-service/sync/bigquery\";\n *\n * const adapter = new BigQueryAdapter({\n * projectId: \"my-project\",\n * datasetId: \"firestore_sync\",\n * bigquery: new BigQuery({ projectId: \"my-project\" }),\n * maxStaleness: \"INTERVAL 15 MINUTE\",\n * });\n * ```\n */\n\nimport type { BigQuery } from \"@google-cloud/bigquery\";\nimport { SYNC_VERSION_COLUMN } from \"../constants\";\nimport type {\n LogicalType,\n SqlAdapter,\n SqlColumn,\n SqlDialect,\n SqlTableDef,\n} from \"../types\";\nimport { normalizeBigQueryType } from \"./bigquery-types\";\n\n// ---------------------------------------------------------------------------\n// Dialect\n// ---------------------------------------------------------------------------\n\n/** BigQuery SQL dialect mapping. */\nclass BigQueryDialect implements SqlDialect {\n readonly name = \"bigquery\";\n\n mapType(logical: LogicalType): string {\n switch (logical) {\n case \"string\":\n return \"STRING\";\n case \"number\":\n return \"FLOAT64\";\n case \"bigint\":\n return \"INT64\";\n case \"boolean\":\n return \"BOOL\";\n case \"timestamp\":\n return \"TIMESTAMP\";\n case \"json\":\n return \"JSON\";\n case \"text\":\n return \"STRING\";\n }\n }\n\n quoteIdentifier(id: string): string {\n return `\\`${id}\\``;\n }\n}\n\n/** Shared BigQuery dialect singleton. */\nexport const bigqueryDialect: SqlDialect = new BigQueryDialect();\n\n// ---------------------------------------------------------------------------\n// Storage Write API loader (kept loose so `@google-cloud/bigquery-storage`\n// stays an OPTIONAL peer dep)\n// ---------------------------------------------------------------------------\n\ntype StorageNs = {\n managedwriter: {\n WriterClient: new (opts?: any) => any;\n JSONWriter: new (params: { connection: any; protoDescriptor: any }) => any;\n DefaultStream: any;\n };\n adapt: {\n convertBigQuerySchemaToStorageTableSchema(schema: any): any;\n convertStorageSchemaToProto2Descriptor(\n schema: any,\n scope: string,\n ...opts: any[]\n ): any;\n withChangeType(): any;\n withChangeSequenceNumber(): any;\n };\n};\n\nlet storageNsCache: StorageNs | null = null;\nfunction loadStorageNs(): StorageNs {\n if (storageNsCache) return storageNsCache;\n // Lazy require so the library does not pull `@google-cloud/bigquery-storage`\n // unless this adapter is actually instantiated.\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n const mod = require(\"@google-cloud/bigquery-storage\");\n storageNsCache = mod as StorageNs;\n return storageNsCache;\n}\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\n/**\n * Format `__sync_version` (Firestore commit timestamp in microseconds since\n * epoch) as the 16-character hex string required by `_CHANGE_SEQUENCE_NUMBER`.\n *\n * BigQuery compares sequence numbers lexicographically, so a fixed-width hex\n * representation is mandatory for correct ordering.\n */\nfunction formatChangeSequenceNumber(version: unknown): string {\n let n: bigint;\n if (typeof version === \"bigint\") n = version;\n else if (typeof version === \"number\") n = BigInt(version);\n else if (typeof version === \"string\") n = BigInt(version);\n else n = 0n;\n if (n < 0n) n = 0n;\n return n.toString(16).padStart(16, \"0\");\n}\n\n/**\n * Returns true when the Storage Write append failed with a transient gRPC\n * status (UNAVAILABLE, DEADLINE_EXCEEDED, INTERNAL, ABORTED). Caller can\n * safely retry these.\n */\nfunction isRetryableStorageError(err: unknown): boolean {\n if (!err || typeof err !== \"object\") return false;\n const e = err as { code?: number };\n // gRPC codes: 4=DEADLINE_EXCEEDED, 10=ABORTED, 13=INTERNAL, 14=UNAVAILABLE\n return e.code === 4 || e.code === 10 || e.code === 13 || e.code === 14;\n}\n\nasync function withRetry<T>(\n fn: () => Promise<T>,\n maxRetries = 6,\n baseMs = 200,\n): Promise<T> {\n let attempt = 0;\n // eslint-disable-next-line no-constant-condition\n while (true) {\n try {\n return await fn();\n } catch (err) {\n attempt++;\n if (!isRetryableStorageError(err) || attempt > maxRetries) throw err;\n const cap = baseMs * Math.pow(2, attempt);\n const delay = Math.random() * cap;\n await new Promise((res) => setTimeout(res, delay));\n }\n }\n}\n\n// ---------------------------------------------------------------------------\n// Adapter\n// ---------------------------------------------------------------------------\n\nexport interface BigQueryAdapterOptions {\n /** GCP project id that owns the dataset. */\n projectId: string;\n /** BigQuery dataset id. */\n datasetId: string;\n /**\n * BigQuery client used for DDL operations (createTable, addColumns,\n * getMetadata, executeRaw). Storage Write only handles inserts.\n *\n * Typed as `BigQuery` from `@google-cloud/bigquery` (peer dependency —\n * the type-only import is erased at compile time so the runtime stays\n * free of that module unless the consumer instantiates a client).\n */\n bigquery: BigQuery;\n /**\n * Optional pre-built Storage Write `WriterClient`. When omitted a new\n * one is created lazily on first use.\n */\n writerClient?: any;\n /**\n * Value passed to `OPTIONS(max_staleness = ...)` on `createTable`.\n *\n * **Why this matters**: in BigQuery CDC mode, Storage Write API rows\n * land in a delta buffer. They become visible to queries only after a\n * MERGE applies them to the base table. Two paths exist:\n *\n * - **Background MERGE** — runs periodically in the background, paid\n * for as part of CDC pricing, and never blocks your readers.\n * - **Read-time MERGE** — every `SELECT` query merges the delta on the\n * fly before returning. Always-fresh reads but every query pays for\n * the merge work.\n *\n * `max_staleness` is the tolerated lag between a write and what reads\n * see. Setting it tells BigQuery: \"background MERGE is fine if reads\n * are at most this stale\". When the threshold is exceeded, the next\n * read triggers a one-shot merge.\n *\n * **The default `INTERVAL 0` means \"always read-time merge\"** — every\n * query forces a full merge of pending CDC writes. That makes reads\n * dramatically slower and more expensive on busy tables, so this\n * library defaults to a 15 minute window in production.\n *\n * Recommended:\n * - `INTERVAL 15 MINUTE` in production (good cost/freshness trade-off).\n * - `INTERVAL 1 MINUTE` in dev for quick visibility.\n * - Set to `null` to omit the option entirely (only do this when you\n * know what you are doing — you'll likely hit slow & expensive reads).\n *\n * @default \"INTERVAL 15 MINUTE\"\n */\n maxStaleness?: string | null;\n}\n\n/**\n * BigQuery implementation of {@link SqlAdapter} using the Storage Write API\n * in CDC mode. See module-level docstring for the rationale.\n */\nexport class BigQueryAdapter implements SqlAdapter {\n private readonly bigquery: BigQuery;\n private readonly projectId: string;\n private readonly datasetId: string;\n private readonly maxStaleness: string | null;\n private writerClient: any;\n /** Cache of `{ writer, primaryKey }` per table name. */\n private readonly writers = new Map<\n string,\n { writer: any; primaryKey: string }\n >();\n\n constructor(options: BigQueryAdapterOptions) {\n this.bigquery = options.bigquery;\n this.projectId = options.projectId;\n this.datasetId = options.datasetId;\n this.maxStaleness =\n options.maxStaleness === undefined\n ? \"INTERVAL 15 MINUTE\"\n : options.maxStaleness;\n this.writerClient = options.writerClient;\n }\n\n get dialect(): SqlDialect {\n return bigqueryDialect;\n }\n\n // -------- DDL (delegated to @google-cloud/bigquery) ----------------------\n\n async tableExists(tableName: string): Promise<boolean> {\n const [exists] = await this.dataset.table(tableName).exists();\n return exists;\n }\n\n async getTableColumns(tableName: string): Promise<string[]> {\n const [metadata] = await this.dataset.table(tableName).getMetadata();\n const fields: Array<{ name: string }> = metadata.schema?.fields ?? [];\n return fields.map((f) => f.name);\n }\n\n async getTableColumnsWithTypes(\n tableName: string,\n ): Promise<Map<string, string>> {\n const [metadata] = await this.dataset.table(tableName).getMetadata();\n const fields: Array<{ name: string; type: string }> =\n metadata.schema?.fields ?? [];\n const result = new Map<string, string>();\n for (const f of fields) {\n result.set(f.name, normalizeBigQueryType(f.type));\n }\n return result;\n }\n\n async createTable(table: SqlTableDef): Promise<void> {\n const qi = (id: string) => this.dialect.quoteIdentifier(id);\n const pk = table.columns.find((c) => c.isPrimaryKey)?.name;\n if (!pk) {\n throw new Error(\n `BigQueryAdapter requires a primary key on table \\`${table.tableName}\\` ` +\n `(Storage Write CDC mode needs PRIMARY KEY NOT ENFORCED).`,\n );\n }\n const cols = table.columns\n .map((c) => {\n const notNull = c.isPrimaryKey ? \" NOT NULL\" : \"\";\n return ` ${qi(c.name)} ${c.sqlType}${notNull}`;\n })\n .join(\",\\n\");\n\n const opts: string[] = [];\n if (this.maxStaleness !== null) {\n opts.push(`max_staleness = ${this.maxStaleness}`);\n }\n const optionsClause =\n opts.length > 0 ? `\\nOPTIONS(${opts.join(\", \")})` : \"\";\n\n const ddl =\n `CREATE TABLE IF NOT EXISTS ${this.fqn(table.tableName)} (\\n${cols},\\n` +\n ` PRIMARY KEY (${qi(pk)}) NOT ENFORCED\\n)` +\n `\\nCLUSTER BY ${qi(pk)}` +\n `${optionsClause};`;\n\n await this.bigquery.query({ query: ddl });\n }\n\n async addColumns(tableName: string, columns: SqlColumn[]): Promise<void> {\n const qi = (id: string) => this.dialect.quoteIdentifier(id);\n for (const c of columns) {\n const stmt = `ALTER TABLE ${this.fqn(tableName)} ADD COLUMN ${qi(c.name)} ${c.sqlType};`;\n await this.bigquery.query({ query: stmt });\n }\n }\n\n async executeRaw(sql: string): Promise<void> {\n await this.bigquery.query({ query: sql });\n }\n\n /**\n * Invalidate the cached writer for a table. Called by the worker after\n * `addColumns` so the next append rebuilds the proto descriptor against\n * the new schema.\n */\n onSchemaChange(tableName: string): void {\n const cached = this.writers.get(tableName);\n if (cached) {\n try {\n cached.writer.close();\n } catch {\n // ignore — connection may already be torn down\n }\n this.writers.delete(tableName);\n }\n }\n\n // -------- Inserts (Storage Write API) -----------------------------------\n\n async insertRows(\n tableName: string,\n rows: Record<string, unknown>[],\n ): Promise<void> {\n if (rows.length === 0) return;\n // Plain inserts have no PK matching, but in CDC mode every row needs a\n // _CHANGE_TYPE. We treat them as UPSERT.\n const writer = await this.getOrCreateWriter(tableName);\n const cdc = rows.map((row) => ({\n ...this.normalizeRow(row),\n _CHANGE_TYPE: \"UPSERT\",\n _CHANGE_SEQUENCE_NUMBER: formatChangeSequenceNumber(\n row[SYNC_VERSION_COLUMN],\n ),\n }));\n await this.appendWithRetry(tableName, writer, cdc);\n }\n\n async upsertRows(\n tableName: string,\n rows: Record<string, unknown>[],\n primaryKey: string,\n ): Promise<void> {\n if (rows.length === 0) return;\n const writer = await this.getOrCreateWriter(tableName, primaryKey);\n const cdc = rows.map((row) => ({\n ...this.normalizeRow(row),\n _CHANGE_TYPE: \"UPSERT\",\n _CHANGE_SEQUENCE_NUMBER: formatChangeSequenceNumber(\n row[SYNC_VERSION_COLUMN],\n ),\n }));\n await this.appendWithRetry(tableName, writer, cdc);\n }\n\n async deleteRows(\n tableName: string,\n primaryKey: string,\n ids: string[],\n ): Promise<void> {\n if (ids.length === 0) return;\n const writer = await this.getOrCreateWriter(tableName, primaryKey);\n // Storage Write CDC requires a value for every NOT-NULL column (the PK)\n // and a `_CHANGE_TYPE`. Other columns may be omitted; missing values are\n // interpreted as NULL. We pass the maximum sequence number so the DELETE\n // wins over any concurrent UPSERT for the same key — tombstones from the\n // queue carry no `__sync_version` of their own.\n const cdc = ids.map((id) => ({\n [primaryKey]: id,\n _CHANGE_TYPE: \"DELETE\",\n _CHANGE_SEQUENCE_NUMBER: \"ffffffffffffffff\",\n }));\n await this.appendWithRetry(tableName, writer, cdc);\n }\n\n // -------- Internal helpers ----------------------------------------------\n\n private get dataset() {\n return this.bigquery.dataset(this.datasetId);\n }\n\n private fqn(tableName: string): string {\n return `\\`${this.datasetId}.${tableName}\\``;\n }\n\n /** ISO 8601 timestamp pattern (e.g. 2026-03-29T20:59:27.394Z) */\n private static readonly ISO_TIMESTAMP_RE =\n /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}/;\n\n /**\n * Convert a Date or epoch-millis number into the epoch-microseconds string\n * required by the Storage Write API for TIMESTAMP columns.\n *\n * The JSONWriter encodes through protobuf and a TIMESTAMP field is an\n * `int64`. Passing an ISO string would make `Long.fromString` throw\n * `interior hyphen` on the `-` characters.\n */\n private static toEpochMicros(d: Date): string {\n const ms = d.getTime();\n return (BigInt(ms) * 1000n).toString();\n }\n\n /** Convert JS values into shapes accepted by JSONWriter. */\n private normalizeRow(row: Record<string, unknown>): Record<string, unknown> {\n const out: Record<string, unknown> = {};\n for (const [k, v] of Object.entries(row)) {\n if (v === undefined) continue;\n if (v instanceof Date) {\n // TIMESTAMP column → int64 epoch micros (as string to preserve precision)\n out[k] = BigQueryAdapter.toEpochMicros(v);\n } else if (\n typeof v === \"string\" &&\n BigQueryAdapter.ISO_TIMESTAMP_RE.test(v)\n ) {\n // ISO timestamp produced upstream by `serializeDocument` (Firestore\n // Timestamp → ISO string) — convert to epoch micros for protobuf.\n const d = new Date(v);\n if (!Number.isNaN(d.getTime())) {\n out[k] = BigQueryAdapter.toEpochMicros(d);\n } else {\n out[k] = v;\n }\n } else if (typeof v === \"object\" && v !== null) {\n // JSON columns: serialise nested objects/arrays\n out[k] = JSON.stringify(v);\n } else if (typeof v === \"bigint\") {\n out[k] = v.toString();\n } else {\n out[k] = v;\n }\n }\n return out;\n }\n\n private async appendWithRetry(\n tableName: string,\n writer: any,\n rows: Record<string, unknown>[],\n ): Promise<void> {\n await withRetry(async () => {\n try {\n const pending = writer.appendRows(rows);\n await pending.getResult();\n } catch (err) {\n // On certain errors (schema drift, broken connection) drop the\n // cached writer so the next call rebuilds it.\n this.onSchemaChange(tableName);\n throw err;\n }\n });\n }\n\n private async getOrCreateWriter(\n tableName: string,\n primaryKey?: string,\n ): Promise<any> {\n const cached = this.writers.get(tableName);\n if (cached) {\n if (primaryKey && cached.primaryKey !== primaryKey) {\n // PK changed — invalidate and rebuild\n this.onSchemaChange(tableName);\n } else {\n return cached.writer;\n }\n }\n\n const ns = loadStorageNs();\n if (!this.writerClient) {\n this.writerClient = new ns.managedwriter.WriterClient({\n projectId: this.projectId,\n });\n }\n\n const [metadata] = await this.dataset.table(tableName).getMetadata();\n const bqSchema = { fields: metadata.schema?.fields ?? [] };\n if (!primaryKey) {\n // Try to recover PK from table constraints if available\n const tableConstraintsPK =\n metadata.tableConstraints?.primaryKey?.columns?.[0];\n primaryKey = tableConstraintsPK ?? bqSchema.fields[0]?.name;\n }\n\n const storageSchema =\n ns.adapt.convertBigQuerySchemaToStorageTableSchema(bqSchema);\n const protoDescriptor = ns.adapt.convertStorageSchemaToProto2Descriptor(\n storageSchema,\n \"Row\",\n ns.adapt.withChangeType(),\n ns.adapt.withChangeSequenceNumber(),\n );\n\n const destinationTable = `projects/${this.projectId}/datasets/${this.datasetId}/tables/${tableName}`;\n // Pass `DefaultStream` as `streamId` (not `streamType`): the `_default`\n // stream is implicit on every table, so the client must short-circuit to\n // `${destinationTable}/streams/_default` instead of calling\n // `createWriteStream({ type: DEFAULT })` which BigQuery rejects with\n // `Unable to create a stream with type TYPE_UNSPECIFIED`.\n const connection = await this.writerClient.createStreamConnection({\n streamId: ns.managedwriter.DefaultStream,\n destinationTable,\n });\n\n const writer = new ns.managedwriter.JSONWriter({\n connection,\n protoDescriptor,\n });\n\n this.writers.set(tableName, { writer, primaryKey: primaryKey ?? \"\" });\n return writer;\n }\n\n /** Close all open writer connections. Useful for graceful shutdown. */\n async close(): Promise<void> {\n for (const { writer } of this.writers.values()) {\n try {\n writer.close();\n } catch {\n /* ignore */\n }\n }\n this.writers.clear();\n if (this.writerClient && typeof this.writerClient.close === \"function\") {\n try {\n this.writerClient.close();\n } catch {\n /* ignore */\n }\n }\n }\n}\n"]}