@lpdjs/firestore-repo-service 2.3.7 → 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,9 +1,8 @@
1
- 'use strict';var u="__sync_version";function T(o){let t=o.toUpperCase();switch(t){case "INTEGER":return "INT64";case "FLOAT":return "FLOAT64";case "BOOLEAN":return "BOOL";default:return t}}function N(o){if(!o||typeof o!="object")return false;let t=o;return t.code!==400?false:(Array.isArray(t.errors)?t.errors:[]).some(n=>typeof n?.message=="string"&&n.message.toLowerCase().includes("serialize access"))}async function y(o,t=10,s=500){let n=0;for(;;)try{return await o()}catch(e){if(n++,!N(e)||n>t)throw e;let r=s*Math.pow(2,n),i=Math.random()*r;await new Promise(c=>setTimeout(c,i));}}var g=class{constructor(){this.name="bigquery";}mapType(t){switch(t){case "string":return "STRING";case "number":return "FLOAT64";case "bigint":return "INT64";case "boolean":return "BOOL";case "timestamp":return "TIMESTAMP";case "json":return "JSON";case "text":return "STRING"}}quoteIdentifier(t){return `\`${t}\``}},A=new g,l=class l{constructor(t){this.bigquery=t.bigquery,this.datasetId=t.datasetId;}get dialect(){return A}async tableExists(t){let[s]=await this.dataset.table(t).exists();return s}async getTableColumns(t){let[s]=await this.dataset.table(t).getMetadata();return (s.schema?.fields??[]).map(e=>e.name)}async getTableColumnsWithTypes(t){let[s]=await this.dataset.table(t).getMetadata(),n=s.schema?.fields??[],e=new Map;for(let r of n)e.set(r.name,T(r.type));return e}async createTable(t){let s=r=>this.dialect.quoteIdentifier(r),n=t.columns.map(r=>{let i=r.isPrimaryKey?" NOT NULL":"";return ` ${s(r.name)} ${r.sqlType}${i}`}).join(`,
2
- `),e=`CREATE TABLE IF NOT EXISTS ${this.fqn(t.tableName)} (
3
- ${n}
4
- );`;await this.bigquery.query({query:e});}async addColumns(t,s){let n=e=>this.dialect.quoteIdentifier(e);for(let e of s){let r=`ALTER TABLE ${this.fqn(t)} ADD COLUMN ${n(e.name)} ${e.sqlType};`;await this.bigquery.query({query:r});}}async insertRows(t,s){s.length!==0&&await this.dataset.table(t).insert(s);}async upsertRows(t,s,n){if(s.length===0)return;let e=Object.keys(s[0]),r=e.filter(a=>a!==n),i=a=>this.dialect.quoteIdentifier(a),m=s.map((a,I)=>`SELECT ${e.map(d=>I===0?`${this.escapeValue(a[d])} AS ${i(d)}`:this.escapeValue(a[d])).join(", ")}`).join(` UNION ALL
5
- `),f=r.map(a=>`T.${i(a)} = S.${i(a)}`).join(", "),E=e.map(a=>i(a)).join(", "),S=e.map(a=>`S.${i(a)}`).join(", "),q=e.includes(u)?` AND (T.${i(u)} IS NULL OR S.${i(u)} > T.${i(u)})`:"",h=[`MERGE ${this.fqn(t)} AS T`,`USING (
6
- ${m}
7
- ) AS S`,`ON T.${i(n)} = S.${i(n)}`,`WHEN MATCHED${q} THEN UPDATE SET ${f}`,`WHEN NOT MATCHED THEN INSERT (${E}) VALUES (${S});`].join(`
8
- `);await y(()=>this.bigquery.query({query:h}));}async deleteRows(t,s,n){if(n.length===0)return;let e=c=>this.dialect.quoteIdentifier(c),r=n.map(c=>this.escapeValue(c)).join(", "),i=`DELETE FROM ${this.fqn(t)} WHERE ${e(s)} IN (${r});`;await y(()=>this.bigquery.query({query:i}));}async executeRaw(t){await this.bigquery.query({query:t});}get dataset(){return this.bigquery.dataset(this.datasetId)}fqn(t){return `\`${this.datasetId}.${t}\``}escapeValue(t){return t==null?"NULL":typeof t=="boolean"?t?"TRUE":"FALSE":typeof t=="number"||typeof t=="bigint"?String(t):typeof t=="string"?l.ISO_TIMESTAMP_RE.test(t)?`TIMESTAMP('${t}')`:t.startsWith("[")&&t.endsWith("]")||t.startsWith("{")&&t.endsWith("}")?`PARSE_JSON('${t.replace(/'/g,"\\'")}')`:`'${t.replace(/'/g,"\\'")}'`:`PARSE_JSON('${JSON.stringify(t).replace(/'/g,"\\'")}')`}};l.ISO_TIMESTAMP_RE=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/;var p=l;exports.BigQueryAdapter=p;exports.bigqueryDialect=A;//# sourceMappingURL=bigquery.cjs.map
1
+ 'use strict';var S=(s=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(s,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):s)(function(s){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+s+'" is not supported')});var u="__sync_version";function m(s){let t=s.toUpperCase();switch(t){case "INTEGER":return "INT64";case "FLOAT":return "FLOAT64";case "BOOLEAN":return "BOOL";default:return t}}var p=class{constructor(){this.name="bigquery";}mapType(t){switch(t){case "string":return "STRING";case "number":return "FLOAT64";case "bigint":return "INT64";case "boolean":return "BOOL";case "timestamp":return "TIMESTAMP";case "json":return "JSON";case "text":return "STRING"}}quoteIdentifier(t){return `\`${t}\``}},T=new p,g=null;function E(){return g||(g=S("@google-cloud/bigquery-storage"),g)}function h(s){let t;return typeof s=="bigint"?t=s:typeof s=="number"||typeof s=="string"?t=BigInt(s):t=0n,t<0n&&(t=0n),t.toString(16).padStart(16,"0")}function C(s){if(!s||typeof s!="object")return false;let t=s;return t.code===4||t.code===10||t.code===13||t.code===14}async function I(s,t=6,e=200){let n=0;for(;;)try{return await s()}catch(r){if(n++,!C(r)||n>t)throw r;let i=e*Math.pow(2,n),a=Math.random()*i;await new Promise(l=>setTimeout(l,a));}}var c=class c{constructor(t){this.writers=new Map;this.bigquery=t.bigquery,this.projectId=t.projectId,this.datasetId=t.datasetId,this.maxStaleness=t.maxStaleness===void 0?"INTERVAL 15 MINUTE":t.maxStaleness,this.writerClient=t.writerClient;}get dialect(){return T}async tableExists(t){let[e]=await this.dataset.table(t).exists();return e}async getTableColumns(t){let[e]=await this.dataset.table(t).getMetadata();return (e.schema?.fields??[]).map(r=>r.name)}async getTableColumnsWithTypes(t){let[e]=await this.dataset.table(t).getMetadata(),n=e.schema?.fields??[],r=new Map;for(let i of n)r.set(i.name,m(i.type));return r}async createTable(t){let e=o=>this.dialect.quoteIdentifier(o),n=t.columns.find(o=>o.isPrimaryKey)?.name;if(!n)throw new Error(`BigQueryAdapter requires a primary key on table \`${t.tableName}\` (Storage Write CDC mode needs PRIMARY KEY NOT ENFORCED).`);let r=t.columns.map(o=>{let d=o.isPrimaryKey?" NOT NULL":"";return ` ${e(o.name)} ${o.sqlType}${d}`}).join(`,
2
+ `),i=[];this.maxStaleness!==null&&i.push(`max_staleness = ${this.maxStaleness}`);let a=i.length>0?`
3
+ OPTIONS(${i.join(", ")})`:"",l=`CREATE TABLE IF NOT EXISTS ${this.fqn(t.tableName)} (
4
+ ${r},
5
+ PRIMARY KEY (${e(n)}) NOT ENFORCED
6
+ )
7
+ CLUSTER BY ${e(n)}${a};`;await this.bigquery.query({query:l});}async addColumns(t,e){let n=r=>this.dialect.quoteIdentifier(r);for(let r of e){let i=`ALTER TABLE ${this.fqn(t)} ADD COLUMN ${n(r.name)} ${r.sqlType};`;await this.bigquery.query({query:i});}}async executeRaw(t){await this.bigquery.query({query:t});}onSchemaChange(t){let e=this.writers.get(t);if(e){try{e.writer.close();}catch{}this.writers.delete(t);}}async insertRows(t,e){if(e.length===0)return;let n=await this.getOrCreateWriter(t),r=e.map(i=>({...this.normalizeRow(i),_CHANGE_TYPE:"UPSERT",_CHANGE_SEQUENCE_NUMBER:h(i[u])}));await this.appendWithRetry(t,n,r);}async upsertRows(t,e,n){if(e.length===0)return;let r=await this.getOrCreateWriter(t,n),i=e.map(a=>({...this.normalizeRow(a),_CHANGE_TYPE:"UPSERT",_CHANGE_SEQUENCE_NUMBER:h(a[u])}));await this.appendWithRetry(t,r,i);}async deleteRows(t,e,n){if(n.length===0)return;let r=await this.getOrCreateWriter(t,e),i=n.map(a=>({[e]:a,_CHANGE_TYPE:"DELETE",_CHANGE_SEQUENCE_NUMBER:"ffffffffffffffff"}));await this.appendWithRetry(t,r,i);}get dataset(){return this.bigquery.dataset(this.datasetId)}fqn(t){return `\`${this.datasetId}.${t}\``}static toEpochMicros(t){let e=t.getTime();return (BigInt(e)*1000n).toString()}normalizeRow(t){let e={};for(let[n,r]of Object.entries(t))if(r!==void 0)if(r instanceof Date)e[n]=c.toEpochMicros(r);else if(typeof r=="string"&&c.ISO_TIMESTAMP_RE.test(r)){let i=new Date(r);Number.isNaN(i.getTime())?e[n]=r:e[n]=c.toEpochMicros(i);}else typeof r=="object"&&r!==null?e[n]=JSON.stringify(r):typeof r=="bigint"?e[n]=r.toString():e[n]=r;return e}async appendWithRetry(t,e,n){await I(async()=>{try{await e.appendRows(n).getResult();}catch(r){throw this.onSchemaChange(t),r}});}async getOrCreateWriter(t,e){let n=this.writers.get(t);if(n)if(e&&n.primaryKey!==e)this.onSchemaChange(t);else return n.writer;let r=E();this.writerClient||(this.writerClient=new r.managedwriter.WriterClient({projectId:this.projectId}));let[i]=await this.dataset.table(t).getMetadata(),a={fields:i.schema?.fields??[]};e||(e=i.tableConstraints?.primaryKey?.columns?.[0]??a.fields[0]?.name);let l=r.adapt.convertBigQuerySchemaToStorageTableSchema(a),o=r.adapt.convertStorageSchemaToProto2Descriptor(l,"Row",r.adapt.withChangeType(),r.adapt.withChangeSequenceNumber()),d=`projects/${this.projectId}/datasets/${this.datasetId}/tables/${t}`,w=await this.writerClient.createStreamConnection({streamId:r.managedwriter.DefaultStream,destinationTable:d}),y=new r.managedwriter.JSONWriter({connection:w,protoDescriptor:o});return this.writers.set(t,{writer:y,primaryKey:e??""}),y}async close(){for(let{writer:t}of this.writers.values())try{t.close();}catch{}if(this.writers.clear(),this.writerClient&&typeof this.writerClient.close=="function")try{this.writerClient.close();}catch{}}};c.ISO_TIMESTAMP_RE=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/;var f=c;exports.BigQueryAdapter=f;exports.bigqueryDialect=T;//# sourceMappingURL=bigquery.cjs.map
9
8
  //# sourceMappingURL=bigquery.cjs.map
@@ -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,GACbC,CAAAA,CAAS,GAAA,CACG,CACZ,IAAIC,EAAU,CAAA,CACd,OACE,GAAI,CACF,OAAO,MAAMH,CAAAA,EACf,CAAA,MAASJ,EAAK,CAEZ,GADAO,IACI,CAACR,CAAAA,CAAwBC,CAAG,CAAA,EAAKO,CAAAA,CAAUF,CAAAA,CAAY,MAAML,EAC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,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.cjs","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":"sQAaO,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,CAAA,CAGaC,CAAAA,CAA8B,IAAIH,CAAAA,CAyB3CI,CAAAA,CAAmC,KACvC,SAASC,CAAAA,EAA2B,CAClC,OAAID,CAAAA,GAKJA,EADY,CAAA,CAAQ,gCAAgC,CAAA,CAE7CA,CAAAA,CACT,CAaA,SAASE,EAA2BC,CAAAA,CAA0B,CAC5D,I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,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.cjs","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"]}
@@ -1,71 +1,154 @@
1
- import { b as SqlDialect, a as SqlAdapter, c as SqlTableDef, d as SqlColumn } from '../types-_JSH59Iy.cjs';
1
+ import { BigQuery } from '@google-cloud/bigquery';
2
+ import { b as SqlDialect, a as SqlAdapter, c as SqlTableDef, d as SqlColumn } from '../types-BgIGWlR1.cjs';
2
3
  import '../firebase-auth-D1APf9PA.cjs';
3
4
  import 'firebase-functions/v2/firestore';
4
5
  import 'firebase-functions/v2/https';
5
6
  import 'firebase-functions/v2/pubsub';
6
7
 
7
- /** Shared BigQuery dialect singleton. */
8
- declare const bigqueryDialect: SqlDialect;
9
8
  /**
10
- * BigQuery implementation of {@link SqlAdapter}.
9
+ * BigQuery adapter streams Firestore changes to BigQuery via the
10
+ * **Storage Write API** in CDC (Change Data Capture) mode.
11
+ *
12
+ * Why CDC over the legacy MERGE approach:
13
+ *
14
+ * - **No DML concurrency limit.** MERGE/DELETE DML is bounded by ≈ 2
15
+ * concurrent statements per table; busy collections triggered
16
+ * `Could not serialize access … due to concurrent update` errors.
17
+ * The Storage Write API has no such bound, so multiple Cloud Function
18
+ * instances can flush in parallel without conflicts.
19
+ * - **Cheaper at scale.** Storage Write is roughly ~50% cheaper than
20
+ * legacy streaming inserts, and free for the first 2 TiB / month.
21
+ * - **Same idempotency model.** Each row carries `_CHANGE_SEQUENCE_NUMBER`
22
+ * built from the existing `__sync_version` column, so out-of-order
23
+ * PubSub deliveries are merged correctly by BigQuery.
24
+ *
25
+ * Requirements:
11
26
  *
12
- * Accepts an already-configured BigQuery client so the library does not pull
13
- * in `@google-cloud/bigquery` as a hard dependency.
27
+ * - Destination tables **must** declare a `PRIMARY KEY (...) NOT ENFORCED`
28
+ * constraint and be clustered on that key. {@link BigQueryAdapter.createTable}
29
+ * handles both for tables managed by this library.
30
+ * - Tables should be created with `OPTIONS(max_staleness = INTERVAL …)`
31
+ * so the CDC merge runs in the background instead of on every read
32
+ * — see {@link BigQueryAdapterOptions.maxStaleness}.
33
+ * - Service account needs `bigquery.tables.updateData` (e.g. via the
34
+ * `roles/bigquery.dataEditor` role).
14
35
  *
15
36
  * @example
16
37
  * ```ts
17
38
  * import { BigQuery } from "@google-cloud/bigquery";
18
- * import { BigQueryAdapter } from "./adapters/bigquery";
39
+ * import { BigQueryAdapter } from "@lpdjs/firestore-repo-service/sync/bigquery";
19
40
  *
20
41
  * const adapter = new BigQueryAdapter({
42
+ * projectId: "my-project",
43
+ * datasetId: "firestore_sync",
21
44
  * bigquery: new BigQuery({ projectId: "my-project" }),
22
- * datasetId: "my_dataset",
45
+ * maxStaleness: "INTERVAL 15 MINUTE",
23
46
  * });
24
47
  * ```
25
48
  */
49
+
50
+ /** Shared BigQuery dialect singleton. */
51
+ declare const bigqueryDialect: SqlDialect;
52
+ interface BigQueryAdapterOptions {
53
+ /** GCP project id that owns the dataset. */
54
+ projectId: string;
55
+ /** BigQuery dataset id. */
56
+ datasetId: string;
57
+ /**
58
+ * BigQuery client used for DDL operations (createTable, addColumns,
59
+ * getMetadata, executeRaw). Storage Write only handles inserts.
60
+ *
61
+ * Typed as `BigQuery` from `@google-cloud/bigquery` (peer dependency —
62
+ * the type-only import is erased at compile time so the runtime stays
63
+ * free of that module unless the consumer instantiates a client).
64
+ */
65
+ bigquery: BigQuery;
66
+ /**
67
+ * Optional pre-built Storage Write `WriterClient`. When omitted a new
68
+ * one is created lazily on first use.
69
+ */
70
+ writerClient?: any;
71
+ /**
72
+ * Value passed to `OPTIONS(max_staleness = ...)` on `createTable`.
73
+ *
74
+ * **Why this matters**: in BigQuery CDC mode, Storage Write API rows
75
+ * land in a delta buffer. They become visible to queries only after a
76
+ * MERGE applies them to the base table. Two paths exist:
77
+ *
78
+ * - **Background MERGE** — runs periodically in the background, paid
79
+ * for as part of CDC pricing, and never blocks your readers.
80
+ * - **Read-time MERGE** — every `SELECT` query merges the delta on the
81
+ * fly before returning. Always-fresh reads but every query pays for
82
+ * the merge work.
83
+ *
84
+ * `max_staleness` is the tolerated lag between a write and what reads
85
+ * see. Setting it tells BigQuery: "background MERGE is fine if reads
86
+ * are at most this stale". When the threshold is exceeded, the next
87
+ * read triggers a one-shot merge.
88
+ *
89
+ * **The default `INTERVAL 0` means "always read-time merge"** — every
90
+ * query forces a full merge of pending CDC writes. That makes reads
91
+ * dramatically slower and more expensive on busy tables, so this
92
+ * library defaults to a 15 minute window in production.
93
+ *
94
+ * Recommended:
95
+ * - `INTERVAL 15 MINUTE` in production (good cost/freshness trade-off).
96
+ * - `INTERVAL 1 MINUTE` in dev for quick visibility.
97
+ * - Set to `null` to omit the option entirely (only do this when you
98
+ * know what you are doing — you'll likely hit slow & expensive reads).
99
+ *
100
+ * @default "INTERVAL 15 MINUTE"
101
+ */
102
+ maxStaleness?: string | null;
103
+ }
104
+ /**
105
+ * BigQuery implementation of {@link SqlAdapter} using the Storage Write API
106
+ * in CDC mode. See module-level docstring for the rationale.
107
+ */
26
108
  declare class BigQueryAdapter implements SqlAdapter {
27
109
  private readonly bigquery;
110
+ private readonly projectId;
28
111
  private readonly datasetId;
29
- constructor(options: {
30
- bigquery: any;
31
- datasetId: string;
32
- });
33
- /** The BigQuery SQL dialect. */
112
+ private readonly maxStaleness;
113
+ private writerClient;
114
+ /** Cache of `{ writer, primaryKey }` per table name. */
115
+ private readonly writers;
116
+ constructor(options: BigQueryAdapterOptions);
34
117
  get dialect(): SqlDialect;
35
- /** Check whether a table exists in the dataset. */
36
118
  tableExists(tableName: string): Promise<boolean>;
37
- /** Return the column names currently present in the table. */
38
119
  getTableColumns(tableName: string): Promise<string[]>;
39
- /**
40
- * Return existing columns with their normalized BigQuery type strings.
41
- * Used by the worker to detect type drift before applying schema changes.
42
- */
43
120
  getTableColumnsWithTypes(tableName: string): Promise<Map<string, string>>;
44
- /** Create a table using a fully-qualified name. */
45
121
  createTable(table: SqlTableDef): Promise<void>;
46
- /** Add columns to an existing table using a fully-qualified name. */
47
122
  addColumns(tableName: string, columns: SqlColumn[]): Promise<void>;
48
- /** Append rows via BigQuery streaming insert. */
49
- insertRows(tableName: string, rows: Record<string, unknown>[]): Promise<void>;
123
+ executeRaw(sql: string): Promise<void>;
50
124
  /**
51
- * Upsert rows using a MERGE DML statement.
52
- *
53
- * Builds a source table from inline SELECT … UNION ALL rows and merges
54
- * into the target on the given primary key.
125
+ * Invalidate the cached writer for a table. Called by the worker after
126
+ * `addColumns` so the next append rebuilds the proto descriptor against
127
+ * the new schema.
55
128
  */
129
+ onSchemaChange(tableName: string): void;
130
+ insertRows(tableName: string, rows: Record<string, unknown>[]): Promise<void>;
56
131
  upsertRows(tableName: string, rows: Record<string, unknown>[], primaryKey: string): Promise<void>;
57
- /** Delete rows by primary-key values. */
58
132
  deleteRows(tableName: string, primaryKey: string, ids: string[]): Promise<void>;
59
- /** Execute a raw SQL statement (used by the migration manager). */
60
- executeRaw(sql: string): Promise<void>;
61
- /** The BigQuery Dataset handle. */
62
133
  private get dataset();
63
- /** Return the fully-qualified table reference (`` `dataset.table` ``). */
64
134
  private fqn;
65
135
  /** ISO 8601 timestamp pattern (e.g. 2026-03-29T20:59:27.394Z) */
66
136
  private static readonly ISO_TIMESTAMP_RE;
67
- /** Escape a value for use as a SQL literal. */
68
- private escapeValue;
137
+ /**
138
+ * Convert a Date or epoch-millis number into the epoch-microseconds string
139
+ * required by the Storage Write API for TIMESTAMP columns.
140
+ *
141
+ * The JSONWriter encodes through protobuf and a TIMESTAMP field is an
142
+ * `int64`. Passing an ISO string would make `Long.fromString` throw
143
+ * `interior hyphen` on the `-` characters.
144
+ */
145
+ private static toEpochMicros;
146
+ /** Convert JS values into shapes accepted by JSONWriter. */
147
+ private normalizeRow;
148
+ private appendWithRetry;
149
+ private getOrCreateWriter;
150
+ /** Close all open writer connections. Useful for graceful shutdown. */
151
+ close(): Promise<void>;
69
152
  }
70
153
 
71
- export { BigQueryAdapter, bigqueryDialect };
154
+ export { BigQueryAdapter, type BigQueryAdapterOptions, bigqueryDialect };
@@ -1,71 +1,154 @@
1
- import { b as SqlDialect, a as SqlAdapter, c as SqlTableDef, d as SqlColumn } from '../types-Bhy7MfCj.js';
1
+ import { BigQuery } from '@google-cloud/bigquery';
2
+ import { b as SqlDialect, a as SqlAdapter, c as SqlTableDef, d as SqlColumn } from '../types-ChzVPw4k.js';
2
3
  import '../firebase-auth-D1APf9PA.js';
3
4
  import 'firebase-functions/v2/firestore';
4
5
  import 'firebase-functions/v2/https';
5
6
  import 'firebase-functions/v2/pubsub';
6
7
 
7
- /** Shared BigQuery dialect singleton. */
8
- declare const bigqueryDialect: SqlDialect;
9
8
  /**
10
- * BigQuery implementation of {@link SqlAdapter}.
9
+ * BigQuery adapter streams Firestore changes to BigQuery via the
10
+ * **Storage Write API** in CDC (Change Data Capture) mode.
11
+ *
12
+ * Why CDC over the legacy MERGE approach:
13
+ *
14
+ * - **No DML concurrency limit.** MERGE/DELETE DML is bounded by ≈ 2
15
+ * concurrent statements per table; busy collections triggered
16
+ * `Could not serialize access … due to concurrent update` errors.
17
+ * The Storage Write API has no such bound, so multiple Cloud Function
18
+ * instances can flush in parallel without conflicts.
19
+ * - **Cheaper at scale.** Storage Write is roughly ~50% cheaper than
20
+ * legacy streaming inserts, and free for the first 2 TiB / month.
21
+ * - **Same idempotency model.** Each row carries `_CHANGE_SEQUENCE_NUMBER`
22
+ * built from the existing `__sync_version` column, so out-of-order
23
+ * PubSub deliveries are merged correctly by BigQuery.
24
+ *
25
+ * Requirements:
11
26
  *
12
- * Accepts an already-configured BigQuery client so the library does not pull
13
- * in `@google-cloud/bigquery` as a hard dependency.
27
+ * - Destination tables **must** declare a `PRIMARY KEY (...) NOT ENFORCED`
28
+ * constraint and be clustered on that key. {@link BigQueryAdapter.createTable}
29
+ * handles both for tables managed by this library.
30
+ * - Tables should be created with `OPTIONS(max_staleness = INTERVAL …)`
31
+ * so the CDC merge runs in the background instead of on every read
32
+ * — see {@link BigQueryAdapterOptions.maxStaleness}.
33
+ * - Service account needs `bigquery.tables.updateData` (e.g. via the
34
+ * `roles/bigquery.dataEditor` role).
14
35
  *
15
36
  * @example
16
37
  * ```ts
17
38
  * import { BigQuery } from "@google-cloud/bigquery";
18
- * import { BigQueryAdapter } from "./adapters/bigquery";
39
+ * import { BigQueryAdapter } from "@lpdjs/firestore-repo-service/sync/bigquery";
19
40
  *
20
41
  * const adapter = new BigQueryAdapter({
42
+ * projectId: "my-project",
43
+ * datasetId: "firestore_sync",
21
44
  * bigquery: new BigQuery({ projectId: "my-project" }),
22
- * datasetId: "my_dataset",
45
+ * maxStaleness: "INTERVAL 15 MINUTE",
23
46
  * });
24
47
  * ```
25
48
  */
49
+
50
+ /** Shared BigQuery dialect singleton. */
51
+ declare const bigqueryDialect: SqlDialect;
52
+ interface BigQueryAdapterOptions {
53
+ /** GCP project id that owns the dataset. */
54
+ projectId: string;
55
+ /** BigQuery dataset id. */
56
+ datasetId: string;
57
+ /**
58
+ * BigQuery client used for DDL operations (createTable, addColumns,
59
+ * getMetadata, executeRaw). Storage Write only handles inserts.
60
+ *
61
+ * Typed as `BigQuery` from `@google-cloud/bigquery` (peer dependency —
62
+ * the type-only import is erased at compile time so the runtime stays
63
+ * free of that module unless the consumer instantiates a client).
64
+ */
65
+ bigquery: BigQuery;
66
+ /**
67
+ * Optional pre-built Storage Write `WriterClient`. When omitted a new
68
+ * one is created lazily on first use.
69
+ */
70
+ writerClient?: any;
71
+ /**
72
+ * Value passed to `OPTIONS(max_staleness = ...)` on `createTable`.
73
+ *
74
+ * **Why this matters**: in BigQuery CDC mode, Storage Write API rows
75
+ * land in a delta buffer. They become visible to queries only after a
76
+ * MERGE applies them to the base table. Two paths exist:
77
+ *
78
+ * - **Background MERGE** — runs periodically in the background, paid
79
+ * for as part of CDC pricing, and never blocks your readers.
80
+ * - **Read-time MERGE** — every `SELECT` query merges the delta on the
81
+ * fly before returning. Always-fresh reads but every query pays for
82
+ * the merge work.
83
+ *
84
+ * `max_staleness` is the tolerated lag between a write and what reads
85
+ * see. Setting it tells BigQuery: "background MERGE is fine if reads
86
+ * are at most this stale". When the threshold is exceeded, the next
87
+ * read triggers a one-shot merge.
88
+ *
89
+ * **The default `INTERVAL 0` means "always read-time merge"** — every
90
+ * query forces a full merge of pending CDC writes. That makes reads
91
+ * dramatically slower and more expensive on busy tables, so this
92
+ * library defaults to a 15 minute window in production.
93
+ *
94
+ * Recommended:
95
+ * - `INTERVAL 15 MINUTE` in production (good cost/freshness trade-off).
96
+ * - `INTERVAL 1 MINUTE` in dev for quick visibility.
97
+ * - Set to `null` to omit the option entirely (only do this when you
98
+ * know what you are doing — you'll likely hit slow & expensive reads).
99
+ *
100
+ * @default "INTERVAL 15 MINUTE"
101
+ */
102
+ maxStaleness?: string | null;
103
+ }
104
+ /**
105
+ * BigQuery implementation of {@link SqlAdapter} using the Storage Write API
106
+ * in CDC mode. See module-level docstring for the rationale.
107
+ */
26
108
  declare class BigQueryAdapter implements SqlAdapter {
27
109
  private readonly bigquery;
110
+ private readonly projectId;
28
111
  private readonly datasetId;
29
- constructor(options: {
30
- bigquery: any;
31
- datasetId: string;
32
- });
33
- /** The BigQuery SQL dialect. */
112
+ private readonly maxStaleness;
113
+ private writerClient;
114
+ /** Cache of `{ writer, primaryKey }` per table name. */
115
+ private readonly writers;
116
+ constructor(options: BigQueryAdapterOptions);
34
117
  get dialect(): SqlDialect;
35
- /** Check whether a table exists in the dataset. */
36
118
  tableExists(tableName: string): Promise<boolean>;
37
- /** Return the column names currently present in the table. */
38
119
  getTableColumns(tableName: string): Promise<string[]>;
39
- /**
40
- * Return existing columns with their normalized BigQuery type strings.
41
- * Used by the worker to detect type drift before applying schema changes.
42
- */
43
120
  getTableColumnsWithTypes(tableName: string): Promise<Map<string, string>>;
44
- /** Create a table using a fully-qualified name. */
45
121
  createTable(table: SqlTableDef): Promise<void>;
46
- /** Add columns to an existing table using a fully-qualified name. */
47
122
  addColumns(tableName: string, columns: SqlColumn[]): Promise<void>;
48
- /** Append rows via BigQuery streaming insert. */
49
- insertRows(tableName: string, rows: Record<string, unknown>[]): Promise<void>;
123
+ executeRaw(sql: string): Promise<void>;
50
124
  /**
51
- * Upsert rows using a MERGE DML statement.
52
- *
53
- * Builds a source table from inline SELECT … UNION ALL rows and merges
54
- * into the target on the given primary key.
125
+ * Invalidate the cached writer for a table. Called by the worker after
126
+ * `addColumns` so the next append rebuilds the proto descriptor against
127
+ * the new schema.
55
128
  */
129
+ onSchemaChange(tableName: string): void;
130
+ insertRows(tableName: string, rows: Record<string, unknown>[]): Promise<void>;
56
131
  upsertRows(tableName: string, rows: Record<string, unknown>[], primaryKey: string): Promise<void>;
57
- /** Delete rows by primary-key values. */
58
132
  deleteRows(tableName: string, primaryKey: string, ids: string[]): Promise<void>;
59
- /** Execute a raw SQL statement (used by the migration manager). */
60
- executeRaw(sql: string): Promise<void>;
61
- /** The BigQuery Dataset handle. */
62
133
  private get dataset();
63
- /** Return the fully-qualified table reference (`` `dataset.table` ``). */
64
134
  private fqn;
65
135
  /** ISO 8601 timestamp pattern (e.g. 2026-03-29T20:59:27.394Z) */
66
136
  private static readonly ISO_TIMESTAMP_RE;
67
- /** Escape a value for use as a SQL literal. */
68
- private escapeValue;
137
+ /**
138
+ * Convert a Date or epoch-millis number into the epoch-microseconds string
139
+ * required by the Storage Write API for TIMESTAMP columns.
140
+ *
141
+ * The JSONWriter encodes through protobuf and a TIMESTAMP field is an
142
+ * `int64`. Passing an ISO string would make `Long.fromString` throw
143
+ * `interior hyphen` on the `-` characters.
144
+ */
145
+ private static toEpochMicros;
146
+ /** Convert JS values into shapes accepted by JSONWriter. */
147
+ private normalizeRow;
148
+ private appendWithRetry;
149
+ private getOrCreateWriter;
150
+ /** Close all open writer connections. Useful for graceful shutdown. */
151
+ close(): Promise<void>;
69
152
  }
70
153
 
71
- export { BigQueryAdapter, bigqueryDialect };
154
+ export { BigQueryAdapter, type BigQueryAdapterOptions, bigqueryDialect };
@@ -1,9 +1,8 @@
1
- var u="__sync_version";function T(o){let t=o.toUpperCase();switch(t){case "INTEGER":return "INT64";case "FLOAT":return "FLOAT64";case "BOOLEAN":return "BOOL";default:return t}}function N(o){if(!o||typeof o!="object")return false;let t=o;return t.code!==400?false:(Array.isArray(t.errors)?t.errors:[]).some(n=>typeof n?.message=="string"&&n.message.toLowerCase().includes("serialize access"))}async function y(o,t=10,s=500){let n=0;for(;;)try{return await o()}catch(e){if(n++,!N(e)||n>t)throw e;let r=s*Math.pow(2,n),i=Math.random()*r;await new Promise(c=>setTimeout(c,i));}}var g=class{constructor(){this.name="bigquery";}mapType(t){switch(t){case "string":return "STRING";case "number":return "FLOAT64";case "bigint":return "INT64";case "boolean":return "BOOL";case "timestamp":return "TIMESTAMP";case "json":return "JSON";case "text":return "STRING"}}quoteIdentifier(t){return `\`${t}\``}},A=new g,l=class l{constructor(t){this.bigquery=t.bigquery,this.datasetId=t.datasetId;}get dialect(){return A}async tableExists(t){let[s]=await this.dataset.table(t).exists();return s}async getTableColumns(t){let[s]=await this.dataset.table(t).getMetadata();return (s.schema?.fields??[]).map(e=>e.name)}async getTableColumnsWithTypes(t){let[s]=await this.dataset.table(t).getMetadata(),n=s.schema?.fields??[],e=new Map;for(let r of n)e.set(r.name,T(r.type));return e}async createTable(t){let s=r=>this.dialect.quoteIdentifier(r),n=t.columns.map(r=>{let i=r.isPrimaryKey?" NOT NULL":"";return ` ${s(r.name)} ${r.sqlType}${i}`}).join(`,
2
- `),e=`CREATE TABLE IF NOT EXISTS ${this.fqn(t.tableName)} (
3
- ${n}
4
- );`;await this.bigquery.query({query:e});}async addColumns(t,s){let n=e=>this.dialect.quoteIdentifier(e);for(let e of s){let r=`ALTER TABLE ${this.fqn(t)} ADD COLUMN ${n(e.name)} ${e.sqlType};`;await this.bigquery.query({query:r});}}async insertRows(t,s){s.length!==0&&await this.dataset.table(t).insert(s);}async upsertRows(t,s,n){if(s.length===0)return;let e=Object.keys(s[0]),r=e.filter(a=>a!==n),i=a=>this.dialect.quoteIdentifier(a),m=s.map((a,I)=>`SELECT ${e.map(d=>I===0?`${this.escapeValue(a[d])} AS ${i(d)}`:this.escapeValue(a[d])).join(", ")}`).join(` UNION ALL
5
- `),f=r.map(a=>`T.${i(a)} = S.${i(a)}`).join(", "),E=e.map(a=>i(a)).join(", "),S=e.map(a=>`S.${i(a)}`).join(", "),q=e.includes(u)?` AND (T.${i(u)} IS NULL OR S.${i(u)} > T.${i(u)})`:"",h=[`MERGE ${this.fqn(t)} AS T`,`USING (
6
- ${m}
7
- ) AS S`,`ON T.${i(n)} = S.${i(n)}`,`WHEN MATCHED${q} THEN UPDATE SET ${f}`,`WHEN NOT MATCHED THEN INSERT (${E}) VALUES (${S});`].join(`
8
- `);await y(()=>this.bigquery.query({query:h}));}async deleteRows(t,s,n){if(n.length===0)return;let e=c=>this.dialect.quoteIdentifier(c),r=n.map(c=>this.escapeValue(c)).join(", "),i=`DELETE FROM ${this.fqn(t)} WHERE ${e(s)} IN (${r});`;await y(()=>this.bigquery.query({query:i}));}async executeRaw(t){await this.bigquery.query({query:t});}get dataset(){return this.bigquery.dataset(this.datasetId)}fqn(t){return `\`${this.datasetId}.${t}\``}escapeValue(t){return t==null?"NULL":typeof t=="boolean"?t?"TRUE":"FALSE":typeof t=="number"||typeof t=="bigint"?String(t):typeof t=="string"?l.ISO_TIMESTAMP_RE.test(t)?`TIMESTAMP('${t}')`:t.startsWith("[")&&t.endsWith("]")||t.startsWith("{")&&t.endsWith("}")?`PARSE_JSON('${t.replace(/'/g,"\\'")}')`:`'${t.replace(/'/g,"\\'")}'`:`PARSE_JSON('${JSON.stringify(t).replace(/'/g,"\\'")}')`}};l.ISO_TIMESTAMP_RE=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/;var p=l;export{p as BigQueryAdapter,A as bigqueryDialect};//# sourceMappingURL=bigquery.js.map
1
+ var S=(s=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(s,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):s)(function(s){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+s+'" is not supported')});var u="__sync_version";function m(s){let t=s.toUpperCase();switch(t){case "INTEGER":return "INT64";case "FLOAT":return "FLOAT64";case "BOOLEAN":return "BOOL";default:return t}}var p=class{constructor(){this.name="bigquery";}mapType(t){switch(t){case "string":return "STRING";case "number":return "FLOAT64";case "bigint":return "INT64";case "boolean":return "BOOL";case "timestamp":return "TIMESTAMP";case "json":return "JSON";case "text":return "STRING"}}quoteIdentifier(t){return `\`${t}\``}},T=new p,g=null;function E(){return g||(g=S("@google-cloud/bigquery-storage"),g)}function h(s){let t;return typeof s=="bigint"?t=s:typeof s=="number"||typeof s=="string"?t=BigInt(s):t=0n,t<0n&&(t=0n),t.toString(16).padStart(16,"0")}function C(s){if(!s||typeof s!="object")return false;let t=s;return t.code===4||t.code===10||t.code===13||t.code===14}async function I(s,t=6,e=200){let n=0;for(;;)try{return await s()}catch(r){if(n++,!C(r)||n>t)throw r;let i=e*Math.pow(2,n),a=Math.random()*i;await new Promise(l=>setTimeout(l,a));}}var c=class c{constructor(t){this.writers=new Map;this.bigquery=t.bigquery,this.projectId=t.projectId,this.datasetId=t.datasetId,this.maxStaleness=t.maxStaleness===void 0?"INTERVAL 15 MINUTE":t.maxStaleness,this.writerClient=t.writerClient;}get dialect(){return T}async tableExists(t){let[e]=await this.dataset.table(t).exists();return e}async getTableColumns(t){let[e]=await this.dataset.table(t).getMetadata();return (e.schema?.fields??[]).map(r=>r.name)}async getTableColumnsWithTypes(t){let[e]=await this.dataset.table(t).getMetadata(),n=e.schema?.fields??[],r=new Map;for(let i of n)r.set(i.name,m(i.type));return r}async createTable(t){let e=o=>this.dialect.quoteIdentifier(o),n=t.columns.find(o=>o.isPrimaryKey)?.name;if(!n)throw new Error(`BigQueryAdapter requires a primary key on table \`${t.tableName}\` (Storage Write CDC mode needs PRIMARY KEY NOT ENFORCED).`);let r=t.columns.map(o=>{let d=o.isPrimaryKey?" NOT NULL":"";return ` ${e(o.name)} ${o.sqlType}${d}`}).join(`,
2
+ `),i=[];this.maxStaleness!==null&&i.push(`max_staleness = ${this.maxStaleness}`);let a=i.length>0?`
3
+ OPTIONS(${i.join(", ")})`:"",l=`CREATE TABLE IF NOT EXISTS ${this.fqn(t.tableName)} (
4
+ ${r},
5
+ PRIMARY KEY (${e(n)}) NOT ENFORCED
6
+ )
7
+ CLUSTER BY ${e(n)}${a};`;await this.bigquery.query({query:l});}async addColumns(t,e){let n=r=>this.dialect.quoteIdentifier(r);for(let r of e){let i=`ALTER TABLE ${this.fqn(t)} ADD COLUMN ${n(r.name)} ${r.sqlType};`;await this.bigquery.query({query:i});}}async executeRaw(t){await this.bigquery.query({query:t});}onSchemaChange(t){let e=this.writers.get(t);if(e){try{e.writer.close();}catch{}this.writers.delete(t);}}async insertRows(t,e){if(e.length===0)return;let n=await this.getOrCreateWriter(t),r=e.map(i=>({...this.normalizeRow(i),_CHANGE_TYPE:"UPSERT",_CHANGE_SEQUENCE_NUMBER:h(i[u])}));await this.appendWithRetry(t,n,r);}async upsertRows(t,e,n){if(e.length===0)return;let r=await this.getOrCreateWriter(t,n),i=e.map(a=>({...this.normalizeRow(a),_CHANGE_TYPE:"UPSERT",_CHANGE_SEQUENCE_NUMBER:h(a[u])}));await this.appendWithRetry(t,r,i);}async deleteRows(t,e,n){if(n.length===0)return;let r=await this.getOrCreateWriter(t,e),i=n.map(a=>({[e]:a,_CHANGE_TYPE:"DELETE",_CHANGE_SEQUENCE_NUMBER:"ffffffffffffffff"}));await this.appendWithRetry(t,r,i);}get dataset(){return this.bigquery.dataset(this.datasetId)}fqn(t){return `\`${this.datasetId}.${t}\``}static toEpochMicros(t){let e=t.getTime();return (BigInt(e)*1000n).toString()}normalizeRow(t){let e={};for(let[n,r]of Object.entries(t))if(r!==void 0)if(r instanceof Date)e[n]=c.toEpochMicros(r);else if(typeof r=="string"&&c.ISO_TIMESTAMP_RE.test(r)){let i=new Date(r);Number.isNaN(i.getTime())?e[n]=r:e[n]=c.toEpochMicros(i);}else typeof r=="object"&&r!==null?e[n]=JSON.stringify(r):typeof r=="bigint"?e[n]=r.toString():e[n]=r;return e}async appendWithRetry(t,e,n){await I(async()=>{try{await e.appendRows(n).getResult();}catch(r){throw this.onSchemaChange(t),r}});}async getOrCreateWriter(t,e){let n=this.writers.get(t);if(n)if(e&&n.primaryKey!==e)this.onSchemaChange(t);else return n.writer;let r=E();this.writerClient||(this.writerClient=new r.managedwriter.WriterClient({projectId:this.projectId}));let[i]=await this.dataset.table(t).getMetadata(),a={fields:i.schema?.fields??[]};e||(e=i.tableConstraints?.primaryKey?.columns?.[0]??a.fields[0]?.name);let l=r.adapt.convertBigQuerySchemaToStorageTableSchema(a),o=r.adapt.convertStorageSchemaToProto2Descriptor(l,"Row",r.adapt.withChangeType(),r.adapt.withChangeSequenceNumber()),d=`projects/${this.projectId}/datasets/${this.datasetId}/tables/${t}`,w=await this.writerClient.createStreamConnection({streamId:r.managedwriter.DefaultStream,destinationTable:d}),y=new r.managedwriter.JSONWriter({connection:w,protoDescriptor:o});return this.writers.set(t,{writer:y,primaryKey:e??""}),y}async close(){for(let{writer:t}of this.writers.values())try{t.close();}catch{}if(this.writers.clear(),this.writerClient&&typeof this.writerClient.close=="function")try{this.writerClient.close();}catch{}}};c.ISO_TIMESTAMP_RE=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/;var f=c;export{f as BigQueryAdapter,T as bigqueryDialect};//# sourceMappingURL=bigquery.js.map
9
8
  //# sourceMappingURL=bigquery.js.map