@gusto/baerly-storage 0.1.0 → 0.1.2
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.
- package/README.md +9 -5
- package/dist/API.md +161 -10
- package/dist/{app-config-Cb5dO2s9.d.ts → app-config-C0Bbw_Z6.d.ts} +18 -9
- package/dist/app-config.d.ts +1 -1
- package/dist/app-config.js.map +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/baerly.js +10 -10
- package/dist/baerly.js.map +1 -1
- package/dist/bytes-BXdamO3L.js +11 -0
- package/dist/bytes-BXdamO3L.js.map +1 -0
- package/dist/{client-BLf3Ae5s.d.ts → client-DYZfoZb9.d.ts} +3 -3
- package/dist/client-react.d.ts +3 -3
- package/dist/client.d.ts +3 -3
- package/dist/cloudflare.d.ts +31 -4
- package/dist/cloudflare.js +72 -6
- package/dist/cloudflare.js.map +1 -1
- package/dist/{config-Bsg8wgwo.js → config-DVfDGyw1.js} +82 -2
- package/dist/config-DVfDGyw1.js.map +1 -0
- package/dist/{context-Oj7ffwD-.js → context-DRZ3tZ5l.js} +3 -2
- package/dist/{context-Oj7ffwD-.js.map → context-DRZ3tZ5l.js.map} +1 -1
- package/dist/{cost-B1WiZxlf.js → cost-CQt1YRkA.js} +3 -4
- package/dist/{cost-B1WiZxlf.js.map → cost-CQt1YRkA.js.map} +1 -1
- package/dist/{current-json-BkatBzF7.js → current-json-C-EyJqs0.js} +107 -5
- package/dist/current-json-C-EyJqs0.js.map +1 -0
- package/dist/{db-D62JguBc.d.ts → db-BM3y2JQw.d.ts} +3 -3
- package/dist/{deploy-BB-wqbhf.js → deploy-CrAbfgCq.js} +2 -2
- package/dist/{deploy-BB-wqbhf.js.map → deploy-CrAbfgCq.js.map} +1 -1
- package/dist/dev-vite.d.ts +3 -3
- package/dist/dev-vite.js +3 -3
- package/dist/dev.d.ts +1 -1
- package/dist/dev.js +1 -1
- package/dist/{doctor-B8h9GCN9.js → doctor-Db-hzPmE.js} +144 -7
- package/dist/doctor-Db-hzPmE.js.map +1 -0
- package/dist/{dump-BHo_sIMZ.js → dump-BYA55NLP.js} +3 -4
- package/dist/{dump-BHo_sIMZ.js.map → dump-BYA55NLP.js.map} +1 -1
- package/dist/errors-DOdL95kK.js.map +1 -1
- package/dist/{export-Dz2PZKSR.js → export-Bhbz90-z.js} +4 -5
- package/dist/{export-Dz2PZKSR.js.map → export-Bhbz90-z.js.map} +1 -1
- package/dist/{fsck-DEz9lyCy.js → fsck-CPPZWt9p.js} +5 -8
- package/dist/fsck-CPPZWt9p.js.map +1 -0
- package/dist/{http-CjUO1Bec.js → http-Cw8p6y5b.js} +8 -8
- package/dist/{http-CjUO1Bec.js.map → http-Cw8p6y5b.js.map} +1 -1
- package/dist/http.d.ts +6 -6
- package/dist/http.js +1 -1
- package/dist/{index-BYv0hb8M.d.ts → index-C3JAH1Yo.d.ts} +14 -2
- package/dist/{index-BGC-ENnj.d.ts → index-DSvwEUeF.d.ts} +63 -54
- package/dist/{index-BquQkhJi.d.ts → index-DwDTWPSU.d.ts} +2 -2
- package/dist/{index-COsQ-Mlz.d.ts → index-cUY-36AN.d.ts} +3 -3
- package/dist/index.d.ts +5 -5
- package/dist/index.js +3 -3
- package/dist/{indexes-DwOXkn1Y.d.ts → indexes-DAu2AkTW.d.ts} +2 -2
- package/dist/{inspect-x02aFWnh.js → inspect-Bdbzvi-_.js} +5 -6
- package/dist/{inspect-x02aFWnh.js.map → inspect-Bdbzvi-_.js.map} +1 -1
- package/dist/maintenance-BRrThz8V.d.ts +449 -0
- package/dist/maintenance-BvOv6BMM.js +1295 -0
- package/dist/maintenance-BvOv6BMM.js.map +1 -0
- package/dist/maintenance-ClzBC1Vv.js +1895 -0
- package/dist/maintenance-ClzBC1Vv.js.map +1 -0
- package/dist/maintenance.d.ts +2 -323
- package/dist/maintenance.js +2 -2
- package/dist/node.d.ts +20 -79
- package/dist/node.js +2 -2
- package/dist/{observability-2IDYRuZK.js → observability-CpOiEN0k.js} +2 -2
- package/dist/{observability-2IDYRuZK.js.map → observability-CpOiEN0k.js.map} +1 -1
- package/dist/observability.d.ts +2 -2
- package/dist/observability.js +2 -2
- package/dist/{plan-Cs17VqjU.js → plan-DOTl21vq.js} +3 -4
- package/dist/{plan-Cs17VqjU.js.map → plan-DOTl21vq.js.map} +1 -1
- package/dist/{query-DcSbFLJt.js → query-BmiQy328.js} +58 -26
- package/dist/query-BmiQy328.js.map +1 -0
- package/dist/{rebuild-index-DwXM0WfA.js → rebuild-index-DC_nR6wP.js} +4 -5
- package/dist/{rebuild-index-DwXM0WfA.js.map → rebuild-index-DC_nR6wP.js.map} +1 -1
- package/dist/{restore-CbKVMMpm.js → restore-C6IcmLeA.js} +44 -120
- package/dist/restore-C6IcmLeA.js.map +1 -0
- package/dist/{log-walk-DIru96UT.js → snapshot-SS8USDUF.js} +187 -20
- package/dist/snapshot-SS8USDUF.js.map +1 -0
- package/dist/{src-DyET_VPP.js → src-DU1sCMlr.js} +315 -278
- package/dist/src-DU1sCMlr.js.map +1 -0
- package/dist/{src-n0Te1r98.js → src-QQ7jDeL2.js} +25 -6
- package/dist/src-QQ7jDeL2.js.map +1 -0
- package/dist/{src-Chafbw8r.js → src-zhEAmmF7.js} +35 -4
- package/dist/src-zhEAmmF7.js.map +1 -0
- package/dist/{bucket-uri-BrDEYvzy.js → subcommand-DZcHcsYp.js} +222 -22
- package/dist/subcommand-DZcHcsYp.js.map +1 -0
- package/dist/{usage-RVUQMLop.js → usage-CLjirdD-.js} +4 -4
- package/dist/{usage-RVUQMLop.js.map → usage-CLjirdD-.js.map} +1 -1
- package/dist/{validate-DhH1lO8I.js → validate-DCq_0DIn.js} +2 -2
- package/dist/validate-DCq_0DIn.js.map +1 -0
- package/package.json +13 -6
- package/dist/bucket-uri-BrDEYvzy.js.map +0 -1
- package/dist/config-Bsg8wgwo.js.map +0 -1
- package/dist/current-json-BkatBzF7.js.map +0 -1
- package/dist/doctor-B8h9GCN9.js.map +0 -1
- package/dist/fsck-DEz9lyCy.js.map +0 -1
- package/dist/indexes-BXn7a06a.js +0 -297
- package/dist/indexes-BXn7a06a.js.map +0 -1
- package/dist/log-walk-DIru96UT.js.map +0 -1
- package/dist/maintenance-Bn6ul9aV.js +0 -758
- package/dist/maintenance-Bn6ul9aV.js.map +0 -1
- package/dist/query-DcSbFLJt.js.map +0 -1
- package/dist/rebuild-index-C5gTXryd.js +0 -119
- package/dist/rebuild-index-C5gTXryd.js.map +0 -1
- package/dist/restore-CbKVMMpm.js.map +0 -1
- package/dist/snapshot-D7C4_FSl.js +0 -71
- package/dist/snapshot-D7C4_FSl.js.map +0 -1
- package/dist/snapshot-z_N9hpTe.js +0 -917
- package/dist/snapshot-z_N9hpTe.js.map +0 -1
- package/dist/src-Chafbw8r.js.map +0 -1
- package/dist/src-DyET_VPP.js.map +0 -1
- package/dist/src-n0Te1r98.js.map +0 -1
- package/dist/subcommand-BWs6K7zb.js +0 -110
- package/dist/subcommand-BWs6K7zb.js.map +0 -1
- package/dist/validate-DhH1lO8I.js.map +0 -1
package/dist/observability.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { t as MetricsRecorder } from "./index-
|
|
2
|
-
import { _ as getCurrentContext, a as flushCanonicalLine, c as CATEGORY, d as ObservabilityConfig, f as configureObservability, g as createObservabilityContext, h as ObservabilityContextInit, i as FlushCanonicalLineOptions, l as CategoryName, m as ObservabilityContext, n as deriveOutcome, o as flushUnauthorizedAndRespond, p as getLogger, r as observableStorage, s as withHttpObservability, t as Outcome, u as FriendlyLogLevel, v as runWithContext } from "./index-
|
|
1
|
+
import { t as MetricsRecorder } from "./index-DSvwEUeF.js";
|
|
2
|
+
import { _ as getCurrentContext, a as flushCanonicalLine, c as CATEGORY, d as ObservabilityConfig, f as configureObservability, g as createObservabilityContext, h as ObservabilityContextInit, i as FlushCanonicalLineOptions, l as CategoryName, m as ObservabilityContext, n as deriveOutcome, o as flushUnauthorizedAndRespond, p as getLogger, r as observableStorage, s as withHttpObservability, t as Outcome, u as FriendlyLogLevel, v as runWithContext } from "./index-C3JAH1Yo.js";
|
|
3
3
|
export { CATEGORY, type CategoryName, type FlushCanonicalLineOptions, type FriendlyLogLevel, type MetricsRecorder, type ObservabilityConfig, type ObservabilityContext, type ObservabilityContextInit, type Outcome, configureObservability, createObservabilityContext, deriveOutcome, flushCanonicalLine, flushUnauthorizedAndRespond, getCurrentContext, getLogger, observableStorage, runWithContext, withHttpObservability };
|
package/dist/observability.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { n as getCurrentContext, r as runWithContext, t as createObservabilityContext } from "./context-
|
|
2
|
-
import { a as withHttpObservability, c as configureObservability, l as getLogger, n as flushCanonicalLine, o as deriveOutcome, r as flushUnauthorizedAndRespond, s as CATEGORY, t as observableStorage } from "./observability-
|
|
1
|
+
import { n as getCurrentContext, r as runWithContext, t as createObservabilityContext } from "./context-DRZ3tZ5l.js";
|
|
2
|
+
import { a as withHttpObservability, c as configureObservability, l as getLogger, n as flushCanonicalLine, o as deriveOutcome, r as flushUnauthorizedAndRespond, s as CATEGORY, t as observableStorage } from "./observability-CpOiEN0k.js";
|
|
3
3
|
export { CATEGORY, configureObservability, createObservabilityContext, deriveOutcome, flushCanonicalLine, flushUnauthorizedAndRespond, getCurrentContext, getLogger, observableStorage, runWithContext, withHttpObservability };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as loadSnapshotAsMap } from "./snapshot-D7C4_FSl.js";
|
|
1
|
+
import { f as readCurrentJson, n as loadSnapshotAsMap, o as walkLogRange } from "./snapshot-SS8USDUF.js";
|
|
3
2
|
import { o as BaerlyError } from "./baerly.js";
|
|
4
3
|
//#region src/export/sql-escape.ts
|
|
5
4
|
/**
|
|
@@ -213,7 +212,7 @@ const loadMaterialisedView = async (params) => {
|
|
|
213
212
|
if (entry.collection !== collection) continue;
|
|
214
213
|
if (entry.doc_id === void 0) continue;
|
|
215
214
|
if (entry.op === "I" || entry.op === "U") {
|
|
216
|
-
if (entry.
|
|
215
|
+
if (entry.after !== void 0) base.set(entry.doc_id, entry.after);
|
|
217
216
|
} else if (entry.op === "D") base.delete(entry.doc_id);
|
|
218
217
|
}
|
|
219
218
|
return base;
|
|
@@ -221,4 +220,4 @@ const loadMaterialisedView = async (params) => {
|
|
|
221
220
|
//#endregion
|
|
222
221
|
export { loadMaterialisedView as n, quoteValue as r, inferPlanForCollection as t };
|
|
223
222
|
|
|
224
|
-
//# sourceMappingURL=plan-
|
|
223
|
+
//# sourceMappingURL=plan-DOTl21vq.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plan-Cs17VqjU.js","names":[],"sources":["../packages/cli/src/export/sql-escape.ts","../packages/cli/src/export/plan.ts"],"sourcesContent":["import { BaerlyError, type DocumentValue } from \"@baerly/protocol\";\nimport type { SqlTarget } from \"./types.ts\";\n\n/**\n * Quote an SQL identifier (table or column name) per the target's\n * lexical rules. All three targets accept double-quoted identifiers\n * with embedded `\"\"` for a literal quote. SQLite and Postgres are\n * case-preserving inside double quotes; D1 (sqlite-flavoured)\n * matches.\n *\n * @throws BaerlyError code=\"SchemaError\" — name is empty or contains\n * a NUL byte.\n */\nexport const quoteIdentifier = (name: string, _target: SqlTarget): string => {\n if (name.length === 0) {\n throw new BaerlyError(\"SchemaError\", \"quoteIdentifier: empty identifier\");\n }\n if (name.includes(\"\\0\")) {\n throw new BaerlyError(\n \"SchemaError\",\n `quoteIdentifier: NUL byte in identifier ${JSON.stringify(name)}`,\n );\n }\n return `\"${name.replace(/\"/g, '\"\"')}\"`;\n};\n\n/**\n * Quote a value for inline embedding in `INSERT INTO … VALUES (…)`.\n * Targets agree on `'…'` with doubled `''` for embedded apostrophes\n * and `NULL` for SQL null. JSONB / JSON-encoded text values are\n * passed in as JSON-stringified text by the caller; this function\n * sees them as plain strings.\n *\n * @throws BaerlyError code=\"SchemaError\" — value is an unsupported\n * shape (e.g. raw `undefined` from a missing-field, which\n * {@link emitInsertStatements} pre-filters into `NULL` before\n * calling this).\n */\nexport const quoteValue = (value: DocumentValue | null, target: SqlTarget): string => {\n if (value === null) {\n return \"NULL\";\n }\n if (typeof value === \"string\") {\n return `'${value.replace(/'/g, \"''\")}'`;\n }\n if (typeof value === \"number\") {\n if (!Number.isFinite(value)) {\n throw new BaerlyError(\n \"SchemaError\",\n `quoteValue: ${String(value)} is not finite — JSON doesn't survive round-trip`,\n );\n }\n return String(value);\n }\n if (typeof value === \"boolean\") {\n if (target === \"postgres\") {\n return value ? \"true\" : \"false\";\n }\n return value ? \"1\" : \"0\";\n }\n // Nested object: caller is responsible for setting the column's\n // `jsonEncoded` flag and pre-stringifying before reaching here.\n throw new BaerlyError(\n \"SchemaError\",\n `quoteValue: nested-object reached the quoter without pre-encoding — caller bug`,\n );\n};\n","import { BaerlyError, readCurrentJson, type DocumentValue, type Storage } from \"@baerly/protocol\";\nimport { loadSnapshotAsMap, walkLogRange } from \"@baerly/server\";\nimport type { ColumnPlan, ExportPlan, ExportRow, SqlTarget, SqlType } from \"./types.ts\";\nimport { quoteIdentifier } from \"./sql-escape.ts\";\n\n/**\n * Worst-case observed shape for one column across all scanned rows.\n * Drives the {@link ColumnPlan.sqlType} pick in §3's table.\n */\ninterface ColumnObservation {\n hasString: boolean;\n hasBoolean: boolean;\n hasInteger: boolean;\n /** A floating number, OR an integer that doesn't fit 32-bit signed. */\n hasNonInteger: boolean;\n hasNestedObject: boolean;\n /** Any row missing this field → column is nullable. */\n hasNull: boolean;\n}\n\nconst INT32_MIN = -(2 ** 31);\nconst INT32_MAX = 2 ** 31 - 1;\n\nconst observe = (obs: ColumnObservation, value: DocumentValue): void => {\n if (typeof value === \"string\") {\n obs.hasString = true;\n } else if (typeof value === \"boolean\") {\n obs.hasBoolean = true;\n } else if (typeof value === \"number\") {\n if (Number.isInteger(value) && value >= INT32_MIN && value <= INT32_MAX) {\n obs.hasInteger = true;\n } else {\n obs.hasNonInteger = true;\n }\n } else if (value !== null && typeof value === \"object\") {\n obs.hasNestedObject = true;\n }\n};\n\nconst pickSqlType = (\n obs: ColumnObservation,\n target: SqlTarget,\n): { sqlType: SqlType; jsonEncoded: boolean } => {\n // Nested object present, in any combination → JSON.\n if (obs.hasNestedObject) {\n if (target === \"postgres\") {\n return { sqlType: \"jsonb\", jsonEncoded: true };\n }\n return { sqlType: \"TEXT\", jsonEncoded: true };\n }\n const primitiveKinds =\n (obs.hasString ? 1 : 0) +\n (obs.hasBoolean ? 1 : 0) +\n (obs.hasInteger || obs.hasNonInteger ? 1 : 0);\n // Mixed primitives → safest superset = text.\n if (primitiveKinds > 1) {\n if (target === \"postgres\") {\n return { sqlType: \"text\", jsonEncoded: false };\n }\n return { sqlType: \"TEXT\", jsonEncoded: false };\n }\n if (obs.hasString) {\n if (target === \"postgres\") {\n return { sqlType: \"text\", jsonEncoded: false };\n }\n return { sqlType: \"TEXT\", jsonEncoded: false };\n }\n if (obs.hasBoolean) {\n if (target === \"postgres\") {\n return { sqlType: \"boolean\", jsonEncoded: false };\n }\n return { sqlType: \"INTEGER\", jsonEncoded: false };\n }\n if (obs.hasNonInteger) {\n if (target === \"postgres\") {\n return { sqlType: \"double precision\", jsonEncoded: false };\n }\n return { sqlType: \"REAL\", jsonEncoded: false };\n }\n if (obs.hasInteger) {\n if (target === \"postgres\") {\n return { sqlType: \"integer\", jsonEncoded: false };\n }\n return { sqlType: \"INTEGER\", jsonEncoded: false };\n }\n // No observed value at all (column present-but-null on every row).\n // Fall back to text — won't matter because every insert will\n // emit NULL for it.\n if (target === \"postgres\") {\n return { sqlType: \"text\", jsonEncoded: false };\n }\n return { sqlType: \"TEXT\", jsonEncoded: false };\n};\n\n/**\n * Build an {@link ExportPlan} by scanning every row in the\n * materialised view. Column order is `_id` first, then every other\n * field sorted by first-appearance order (deterministic for a given\n * input map — JavaScript Maps preserve insertion order).\n *\n * @throws BaerlyError code=\"SchemaError\" — a row body is not a\n * `DocumentData` (defensive — the type system enforces it,\n * but the snapshot reader hands us `unknown`-shaped JSON).\n */\nexport const inferPlanForCollection = (params: {\n rows: ReadonlyMap<string, ExportRow>;\n target: SqlTarget;\n table: string;\n}): ExportPlan => {\n const { rows, target, table } = params;\n const obsByField = new Map<string, ColumnObservation>();\n // Track first-appearance order so the column list is stable.\n const orderedFields: string[] = [];\n // Per-field count of rows that carried this field (non-undefined).\n // Compared against `rows.size` in the second pass — any column\n // observed on fewer than every row is nullable.\n const rowsWithField = new Map<string, number>();\n\n // First pass — accumulate observations.\n for (const [id, body] of rows) {\n if (body === null || typeof body !== \"object\") {\n throw new BaerlyError(\n \"SchemaError\",\n `inferPlanForCollection: row ${JSON.stringify(id)} body is not an object`,\n );\n }\n for (const [field, value] of Object.entries(body)) {\n if (value === undefined) {\n continue;\n }\n let obs = obsByField.get(field);\n if (obs === undefined) {\n obs = {\n hasString: false,\n hasBoolean: false,\n hasInteger: false,\n hasNonInteger: false,\n hasNestedObject: false,\n hasNull: false,\n };\n obsByField.set(field, obs);\n orderedFields.push(field);\n }\n observe(obs, value);\n rowsWithField.set(field, (rowsWithField.get(field) ?? 0) + 1);\n }\n }\n\n // Second pass — any column observed on fewer than every row is\n // nullable. A single full sweep at the end captures both \"field\n // disappears after appearing\" AND \"field first appears on the last\n // row\" (the latter was a regression hole when the back-fill was\n // inlined into the per-row loop).\n const totalRows = rows.size;\n for (const [field, obs] of obsByField) {\n if ((rowsWithField.get(field) ?? 0) < totalRows) {\n obs.hasNull = true;\n }\n }\n\n // Build the column list. `_id` first (always — protocol-locked).\n // `_id` is the map key, not a body field, so its observation comes\n // implicitly: every row has a non-empty string id → text / TEXT.\n const columns: ColumnPlan[] = [];\n const idSqlType: SqlType = target === \"postgres\" ? \"text\" : \"TEXT\";\n columns.push({\n source: \"_id\",\n identifier: quoteIdentifier(\"_id\", target),\n sqlType: idSqlType,\n // _id is special: ALWAYS NOT NULL (it's the PK).\n nullable: false,\n jsonEncoded: false,\n });\n for (const field of orderedFields) {\n if (field === \"_id\") {\n continue;\n } // _id from the body is ignored; the map key wins\n const obs = obsByField.get(field);\n if (obs === undefined) {\n continue;\n } // never observed → skip\n const { sqlType, jsonEncoded } = pickSqlType(obs, target);\n columns.push({\n source: field,\n identifier: quoteIdentifier(field, target),\n sqlType,\n nullable: obs.hasNull,\n jsonEncoded,\n });\n }\n\n return {\n target,\n table,\n tableIdentifier: quoteIdentifier(table, target),\n columns,\n rowCount: rows.size,\n };\n};\n\n/**\n * Read the live materialised view of one collection — snapshot\n * folded with the live log tail. See `loadSnapshotAsMap` in\n * `packages/server/src/snapshot.ts` for the hash-checked snapshot\n * load this builds on.\n *\n * Returns `null` when `current.json` is missing (collection not\n * provisioned). Returns an empty map when the collection is\n * provisioned but holds no rows (every doc tombstoned, or no\n * inserts yet).\n */\nexport const loadMaterialisedView = async (params: {\n storage: Storage;\n currentJsonKey: string;\n collection: string;\n signal?: AbortSignal;\n}): Promise<ReadonlyMap<string, ExportRow> | null> => {\n const { storage, currentJsonKey, collection } = params;\n const read = await readCurrentJson(\n storage,\n currentJsonKey,\n params.signal !== undefined ? { signal: params.signal } : undefined,\n );\n if (read === null) {\n return null;\n }\n const collectionPrefix = currentJsonKey.slice(0, currentJsonKey.lastIndexOf(\"/\"));\n const base =\n read.json.snapshot === null\n ? new Map<string, ExportRow>()\n : await loadSnapshotAsMap(storage, read.json.snapshot, collection, params.signal);\n const entries = await walkLogRange(\n storage,\n collectionPrefix,\n read.json.log_seq_start ?? 0,\n read.json.next_seq,\n );\n for (const entry of entries) {\n if (entry.collection !== collection) {\n continue;\n }\n if (entry.doc_id === undefined) {\n continue;\n }\n if (entry.op === \"I\" || entry.op === \"U\") {\n if (entry.new !== undefined) {\n base.set(entry.doc_id, entry.new);\n }\n } else if (entry.op === \"D\") {\n base.delete(entry.doc_id);\n }\n }\n return base;\n};\n"],"mappings":";;;;;;;;;;;;;;AAaA,MAAa,mBAAmB,MAAc,YAA+B;CAC3E,IAAI,KAAK,WAAW,GAClB,MAAM,IAAI,YAAY,eAAe,mCAAmC;C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AAa,uBACnB,IAAI,IAAuB,IAC3B,MAAM,kBAAkB,SAAS,KAAK,KAAK,UAAU,YAAY,OAAO,MAAM;CACpF,MAAM,UAAU,MAAM,aACpB,SACA,kBACA,KAAK,KAAK,iBAAiB,GAC3B,KAAK,KAAK,QACZ;CACA,KAAK,MAAM,SAAS,SAAS;EAC3B,IAAI,MAAM,eAAe,YACvB;EAEF,IAAI,MAAM,WAAW,KAAA,GACnB;EAEF,IAAI,MAAM,OAAO,OAAO,MAAM,OAAO;OAC/B,MAAM,QAAQ,KAAA,GAChB,KAAK,IAAI,MAAM,QAAQ,MAAM,GAAG;EAAA,OAE7B,IAAI,MAAM,OAAO,KACtB,KAAK,OAAO,MAAM,MAAM;CAE5B;CACA,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"plan-DOTl21vq.js","names":[],"sources":["../packages/cli/src/export/sql-escape.ts","../packages/cli/src/export/plan.ts"],"sourcesContent":["import { BaerlyError, type DocumentValue } from \"@baerly/protocol\";\nimport type { SqlTarget } from \"./types.ts\";\n\n/**\n * Quote an SQL identifier (table or column name) per the target's\n * lexical rules. All three targets accept double-quoted identifiers\n * with embedded `\"\"` for a literal quote. SQLite and Postgres are\n * case-preserving inside double quotes; D1 (sqlite-flavoured)\n * matches.\n *\n * @throws BaerlyError code=\"SchemaError\" — name is empty or contains\n * a NUL byte.\n */\nexport const quoteIdentifier = (name: string, _target: SqlTarget): string => {\n if (name.length === 0) {\n throw new BaerlyError(\"SchemaError\", \"quoteIdentifier: empty identifier\");\n }\n if (name.includes(\"\\0\")) {\n throw new BaerlyError(\n \"SchemaError\",\n `quoteIdentifier: NUL byte in identifier ${JSON.stringify(name)}`,\n );\n }\n return `\"${name.replace(/\"/g, '\"\"')}\"`;\n};\n\n/**\n * Quote a value for inline embedding in `INSERT INTO … VALUES (…)`.\n * Targets agree on `'…'` with doubled `''` for embedded apostrophes\n * and `NULL` for SQL null. JSONB / JSON-encoded text values are\n * passed in as JSON-stringified text by the caller; this function\n * sees them as plain strings.\n *\n * @throws BaerlyError code=\"SchemaError\" — value is an unsupported\n * shape (e.g. raw `undefined` from a missing-field, which\n * {@link emitInsertStatements} pre-filters into `NULL` before\n * calling this).\n */\nexport const quoteValue = (value: DocumentValue | null, target: SqlTarget): string => {\n if (value === null) {\n return \"NULL\";\n }\n if (typeof value === \"string\") {\n return `'${value.replace(/'/g, \"''\")}'`;\n }\n if (typeof value === \"number\") {\n if (!Number.isFinite(value)) {\n throw new BaerlyError(\n \"SchemaError\",\n `quoteValue: ${String(value)} is not finite — JSON doesn't survive round-trip`,\n );\n }\n return String(value);\n }\n if (typeof value === \"boolean\") {\n if (target === \"postgres\") {\n return value ? \"true\" : \"false\";\n }\n return value ? \"1\" : \"0\";\n }\n // Nested object: caller is responsible for setting the column's\n // `jsonEncoded` flag and pre-stringifying before reaching here.\n throw new BaerlyError(\n \"SchemaError\",\n `quoteValue: nested-object reached the quoter without pre-encoding — caller bug`,\n );\n};\n","import { BaerlyError, readCurrentJson, type DocumentValue, type Storage } from \"@baerly/protocol\";\nimport { loadSnapshotAsMap, walkLogRange } from \"@baerly/server\";\nimport type { ColumnPlan, ExportPlan, ExportRow, SqlTarget, SqlType } from \"./types.ts\";\nimport { quoteIdentifier } from \"./sql-escape.ts\";\n\n/**\n * Worst-case observed shape for one column across all scanned rows.\n * Drives the {@link ColumnPlan.sqlType} pick in §3's table.\n */\ninterface ColumnObservation {\n hasString: boolean;\n hasBoolean: boolean;\n hasInteger: boolean;\n /** A floating number, OR an integer that doesn't fit 32-bit signed. */\n hasNonInteger: boolean;\n hasNestedObject: boolean;\n /** Any row missing this field → column is nullable. */\n hasNull: boolean;\n}\n\nconst INT32_MIN = -(2 ** 31);\nconst INT32_MAX = 2 ** 31 - 1;\n\nconst observe = (obs: ColumnObservation, value: DocumentValue): void => {\n if (typeof value === \"string\") {\n obs.hasString = true;\n } else if (typeof value === \"boolean\") {\n obs.hasBoolean = true;\n } else if (typeof value === \"number\") {\n if (Number.isInteger(value) && value >= INT32_MIN && value <= INT32_MAX) {\n obs.hasInteger = true;\n } else {\n obs.hasNonInteger = true;\n }\n } else if (value !== null && typeof value === \"object\") {\n obs.hasNestedObject = true;\n }\n};\n\nconst pickSqlType = (\n obs: ColumnObservation,\n target: SqlTarget,\n): { sqlType: SqlType; jsonEncoded: boolean } => {\n // Nested object present, in any combination → JSON.\n if (obs.hasNestedObject) {\n if (target === \"postgres\") {\n return { sqlType: \"jsonb\", jsonEncoded: true };\n }\n return { sqlType: \"TEXT\", jsonEncoded: true };\n }\n const primitiveKinds =\n (obs.hasString ? 1 : 0) +\n (obs.hasBoolean ? 1 : 0) +\n (obs.hasInteger || obs.hasNonInteger ? 1 : 0);\n // Mixed primitives → safest superset = text.\n if (primitiveKinds > 1) {\n if (target === \"postgres\") {\n return { sqlType: \"text\", jsonEncoded: false };\n }\n return { sqlType: \"TEXT\", jsonEncoded: false };\n }\n if (obs.hasString) {\n if (target === \"postgres\") {\n return { sqlType: \"text\", jsonEncoded: false };\n }\n return { sqlType: \"TEXT\", jsonEncoded: false };\n }\n if (obs.hasBoolean) {\n if (target === \"postgres\") {\n return { sqlType: \"boolean\", jsonEncoded: false };\n }\n return { sqlType: \"INTEGER\", jsonEncoded: false };\n }\n if (obs.hasNonInteger) {\n if (target === \"postgres\") {\n return { sqlType: \"double precision\", jsonEncoded: false };\n }\n return { sqlType: \"REAL\", jsonEncoded: false };\n }\n if (obs.hasInteger) {\n if (target === \"postgres\") {\n return { sqlType: \"integer\", jsonEncoded: false };\n }\n return { sqlType: \"INTEGER\", jsonEncoded: false };\n }\n // No observed value at all (column present-but-null on every row).\n // Fall back to text — won't matter because every insert will\n // emit NULL for it.\n if (target === \"postgres\") {\n return { sqlType: \"text\", jsonEncoded: false };\n }\n return { sqlType: \"TEXT\", jsonEncoded: false };\n};\n\n/**\n * Build an {@link ExportPlan} by scanning every row in the\n * materialised view. Column order is `_id` first, then every other\n * field sorted by first-appearance order (deterministic for a given\n * input map — JavaScript Maps preserve insertion order).\n *\n * @throws BaerlyError code=\"SchemaError\" — a row body is not a\n * `DocumentData` (defensive — the type system enforces it,\n * but the snapshot reader hands us `unknown`-shaped JSON).\n */\nexport const inferPlanForCollection = (params: {\n rows: ReadonlyMap<string, ExportRow>;\n target: SqlTarget;\n table: string;\n}): ExportPlan => {\n const { rows, target, table } = params;\n const obsByField = new Map<string, ColumnObservation>();\n // Track first-appearance order so the column list is stable.\n const orderedFields: string[] = [];\n // Per-field count of rows that carried this field (non-undefined).\n // Compared against `rows.size` in the second pass — any column\n // observed on fewer than every row is nullable.\n const rowsWithField = new Map<string, number>();\n\n // First pass — accumulate observations.\n for (const [id, body] of rows) {\n if (body === null || typeof body !== \"object\") {\n throw new BaerlyError(\n \"SchemaError\",\n `inferPlanForCollection: row ${JSON.stringify(id)} body is not an object`,\n );\n }\n for (const [field, value] of Object.entries(body)) {\n if (value === undefined) {\n continue;\n }\n let obs = obsByField.get(field);\n if (obs === undefined) {\n obs = {\n hasString: false,\n hasBoolean: false,\n hasInteger: false,\n hasNonInteger: false,\n hasNestedObject: false,\n hasNull: false,\n };\n obsByField.set(field, obs);\n orderedFields.push(field);\n }\n observe(obs, value);\n rowsWithField.set(field, (rowsWithField.get(field) ?? 0) + 1);\n }\n }\n\n // Second pass — any column observed on fewer than every row is\n // nullable. A single full sweep at the end captures both \"field\n // disappears after appearing\" AND \"field first appears on the last\n // row\" (the latter was a regression hole when the back-fill was\n // inlined into the per-row loop).\n const totalRows = rows.size;\n for (const [field, obs] of obsByField) {\n if ((rowsWithField.get(field) ?? 0) < totalRows) {\n obs.hasNull = true;\n }\n }\n\n // Build the column list. `_id` first (always — protocol-locked).\n // `_id` is the map key, not a body field, so its observation comes\n // implicitly: every row has a non-empty string id → text / TEXT.\n const columns: ColumnPlan[] = [];\n const idSqlType: SqlType = target === \"postgres\" ? \"text\" : \"TEXT\";\n columns.push({\n source: \"_id\",\n identifier: quoteIdentifier(\"_id\", target),\n sqlType: idSqlType,\n // _id is special: ALWAYS NOT NULL (it's the PK).\n nullable: false,\n jsonEncoded: false,\n });\n for (const field of orderedFields) {\n if (field === \"_id\") {\n continue;\n } // _id from the body is ignored; the map key wins\n const obs = obsByField.get(field);\n if (obs === undefined) {\n continue;\n } // never observed → skip\n const { sqlType, jsonEncoded } = pickSqlType(obs, target);\n columns.push({\n source: field,\n identifier: quoteIdentifier(field, target),\n sqlType,\n nullable: obs.hasNull,\n jsonEncoded,\n });\n }\n\n return {\n target,\n table,\n tableIdentifier: quoteIdentifier(table, target),\n columns,\n rowCount: rows.size,\n };\n};\n\n/**\n * Read the live materialised view of one collection — snapshot\n * folded with the live log tail. See `loadSnapshotAsMap` in\n * `packages/server/src/snapshot.ts` for the hash-checked snapshot\n * load this builds on.\n *\n * Returns `null` when `current.json` is missing (collection not\n * provisioned). Returns an empty map when the collection is\n * provisioned but holds no rows (every doc tombstoned, or no\n * inserts yet).\n */\nexport const loadMaterialisedView = async (params: {\n storage: Storage;\n currentJsonKey: string;\n collection: string;\n signal?: AbortSignal;\n}): Promise<ReadonlyMap<string, ExportRow> | null> => {\n const { storage, currentJsonKey, collection } = params;\n const read = await readCurrentJson(\n storage,\n currentJsonKey,\n params.signal !== undefined ? { signal: params.signal } : undefined,\n );\n if (read === null) {\n return null;\n }\n const collectionPrefix = currentJsonKey.slice(0, currentJsonKey.lastIndexOf(\"/\"));\n const base =\n read.json.snapshot === null\n ? new Map<string, ExportRow>()\n : await loadSnapshotAsMap(storage, read.json.snapshot, collection, params.signal);\n const entries = await walkLogRange(\n storage,\n collectionPrefix,\n read.json.log_seq_start ?? 0,\n read.json.next_seq,\n );\n for (const entry of entries) {\n if (entry.collection !== collection) {\n continue;\n }\n if (entry.doc_id === undefined) {\n continue;\n }\n if (entry.op === \"I\" || entry.op === \"U\") {\n if (entry.after !== undefined) {\n base.set(entry.doc_id, entry.after);\n }\n } else if (entry.op === \"D\") {\n base.delete(entry.doc_id);\n }\n }\n return base;\n};\n"],"mappings":";;;;;;;;;;;;;AAaA,MA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ET,MAAM,mBAAmB,eAAe,MAAM,GAAG,eAAe,YAAY,GAAG,CAAC;CAChF,MAAM,OACJ,KAAK,KAAK,aAAa,uBACnB,IAAI,IAAuB,IAC3B,MAAM,kBAAkB,SAAS,KAAK,KAAK,UAAU,YAAY,OAAO,MAAM;CACpF,MAAM,UAAU,MAAM,aACpB,SACA,kBACA,KAAK,KAAK,iBAAiB,GAC3B,KAAK,KAAK,QACZ;CACA,KAAK,MAAM,SAAS,SAAS;EAC3B,IAAI,MAAM,eAAe,YACvB;EAEF,IAAI,MAAM,WAAW,KAAA,GACnB;EAEF,IAAI,MAAM,OAAO,OAAO,MAAM,OAAO;OAC/B,MAAM,UAAU,KAAA,GAClB,KAAK,IAAI,MAAM,QAAQ,MAAM,KAAK;EAAA,OAE/B,IAAI,MAAM,OAAO,KACtB,KAAK,OAAO,MAAM,MAAM;CAE5B;CACA,OAAO;AACT"}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as readCurrentJson, m as MANIFEST_POINTER_EMPTY_SNAPSHOT, n as createCurrentJson, o as FORBIDDEN_MERGE_KEYS, r as logSeqStartOf, v as decodeJsonBytes, y as encodeJsonBytes } from "./current-json-C-EyJqs0.js";
|
|
2
2
|
import { t as BaerlyError } from "./errors-DOdL95kK.js";
|
|
3
|
-
import {
|
|
3
|
+
import { C as matchesWire, S as assertWireSatisfiable, _ as encodeIndexValue, b as versionFromContent, d as loadSnapshotAsMap, g as allIndexKeysFor, h as walkLogRange, i as runBoundedMaintenance, m as readLogEntry, o as shouldFireMaintenance, p as foldLogEntriesOnto, r as dispatchInlineAwaited, v as validateIndexDefinition, w as deepEqualDocumentValue, x as validateWire } from "./maintenance-ClzBC1Vv.js";
|
|
4
4
|
import { t as normalizePredicateArg } from "./normalize-XWY25_sG.js";
|
|
5
|
-
import { i as noopMetricsRecorder, n as getCurrentContext } from "./context-
|
|
5
|
+
import { i as noopMetricsRecorder, n as getCurrentContext } from "./context-DRZ3tZ5l.js";
|
|
6
6
|
//#region packages/protocol/src/json.ts
|
|
7
7
|
const isPlainObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
8
8
|
/**
|
|
9
|
-
* JSON Merge Patch (RFC
|
|
9
|
+
* JSON Merge Patch (RFC 7386).
|
|
10
10
|
*
|
|
11
11
|
* Update `target` with a merge patch. Plain-object values recurse
|
|
12
12
|
* key-by-key; primitives, arrays, and type-mismatched values replace
|
|
13
13
|
* the target wholesale. A `null` value in `patch` deletes the key.
|
|
14
14
|
*
|
|
15
|
-
* RFC
|
|
15
|
+
* RFC 7386 explicitly treats arrays as opaque values: an array in a
|
|
16
16
|
* patch replaces the target array — there is no element-wise merge.
|
|
17
17
|
*/
|
|
18
18
|
function merge(target, patch) {
|
|
@@ -157,7 +157,23 @@ const str2uintDesc = (str, bits) => {
|
|
|
157
157
|
};
|
|
158
158
|
//#endregion
|
|
159
159
|
//#region packages/protocol/src/time.ts
|
|
160
|
-
|
|
160
|
+
/**
|
|
161
|
+
* Encode an epoch-millis instant as a lexicographically-DEScending
|
|
162
|
+
* base-32 key (newer sorts first). The load-bearing LSN ordering
|
|
163
|
+
* primitive — see {@link uint2strDesc}.
|
|
164
|
+
*
|
|
165
|
+
* `epoch` is REQUIRED and validated. There is no default: an argless
|
|
166
|
+
* call used to encode `0`, which descending-sorts as the most-ancient
|
|
167
|
+
* instant and would silently invert ordering. A non-finite, negative,
|
|
168
|
+
* or out-of-range `epoch` (e.g. a broken `Date.now()` returning `NaN`)
|
|
169
|
+
* would corrupt the key silently rather than fail loud, so it throws
|
|
170
|
+
* `Internal` instead — this is an invariant violation, not caller config
|
|
171
|
+
* (the only legitimate argument is `Date.now()`).
|
|
172
|
+
*/
|
|
173
|
+
const timestamp = (epoch) => {
|
|
174
|
+
if (!Number.isInteger(epoch) || epoch < 0 || epoch >= 2 ** 42) throw new BaerlyError("Internal", `timestamp(): epoch must be a non-negative integer < 2^42 (got ${epoch})`);
|
|
175
|
+
return uint2strDesc(epoch, 42);
|
|
176
|
+
};
|
|
161
177
|
/**
|
|
162
178
|
* Delay for `ms` milliseconds, with optional cancellation. If
|
|
163
179
|
* `signal` aborts before the delay elapses, the returned promise
|
|
@@ -469,23 +485,22 @@ var Writer = class {
|
|
|
469
485
|
const baseEtag = read.etag;
|
|
470
486
|
const expectedEpoch = current.writer_fence.epoch;
|
|
471
487
|
if (this.#verifyLogIntegrityOnCommit) await this.#walkLog(logPrefix, logSeqStartOf(current), current.next_seq);
|
|
488
|
+
const commitNowMs = Date.now();
|
|
489
|
+
const commitTs = new Date(commitNowMs).toISOString();
|
|
490
|
+
const lsnTimestamp = timestamp(commitNowMs);
|
|
472
491
|
const entries = [];
|
|
473
492
|
for (let i = 0; i < inputs.length; i++) {
|
|
474
493
|
const input = inputs[i];
|
|
475
494
|
const seq = current.next_seq + i;
|
|
476
495
|
const entry = {
|
|
477
|
-
lsn: `${
|
|
478
|
-
commit_ts:
|
|
496
|
+
lsn: `${lsnTimestamp}_${session}_${countKey(seq)}`,
|
|
497
|
+
commit_ts: commitTs,
|
|
479
498
|
op: input.op,
|
|
480
499
|
collection: input.collection,
|
|
481
500
|
doc_id: input.docId,
|
|
482
|
-
schema_version: 0,
|
|
483
501
|
session,
|
|
484
502
|
seq,
|
|
485
|
-
...input.op !== "D" && input.body !== void 0 ? {
|
|
486
|
-
new: input.body,
|
|
487
|
-
patch: input.body
|
|
488
|
-
} : {},
|
|
503
|
+
...input.op !== "D" && input.body !== void 0 ? { after: input.body } : {},
|
|
489
504
|
...input.origin !== void 0 ? { origin: input.origin } : {}
|
|
490
505
|
};
|
|
491
506
|
entries.push(entry);
|
|
@@ -572,10 +587,14 @@ var Writer = class {
|
|
|
572
587
|
else throw new BaerlyError("Conflict", `${errorPrefix}: log entry already exists at ${logEntryKey}; ${decision.reason}`);
|
|
573
588
|
} else throw new BaerlyError("Conflict", `${errorPrefix}: log entry already exists at ${logEntryKey}; peer wrote our seq`);
|
|
574
589
|
}
|
|
575
|
-
const
|
|
590
|
+
const batchLogBytes = committedEntries.reduce((sum, e) => sum + encodeJsonBytes(e).byteLength, 0);
|
|
591
|
+
const next = {
|
|
576
592
|
...current,
|
|
577
|
-
next_seq: current.next_seq + inputs.length
|
|
578
|
-
|
|
593
|
+
next_seq: current.next_seq + inputs.length,
|
|
594
|
+
tail_bytes: current.tail_bytes + batchLogBytes
|
|
595
|
+
};
|
|
596
|
+
if (next.next_seq - current.next_seq !== inputs.length) throw new BaerlyError("Internal", `${errorPrefix}: GC-cadence invariant violated — next_seq advanced by ${next.next_seq - current.next_seq} but ${inputs.length} input(s) were committed on ${this.#currentJsonKey}`);
|
|
597
|
+
const nextBody = encodeJsonBytes(next);
|
|
579
598
|
const putOpts = {
|
|
580
599
|
ifMatch: baseEtag,
|
|
581
600
|
contentType: APPLICATION_JSON
|
|
@@ -594,6 +613,16 @@ var Writer = class {
|
|
|
594
613
|
}
|
|
595
614
|
throw error;
|
|
596
615
|
}
|
|
616
|
+
const prevSeq = current.next_seq;
|
|
617
|
+
const maint = getCurrentContext()?.maintenance;
|
|
618
|
+
if (maint?.disabled !== true) {
|
|
619
|
+
if (shouldFireMaintenance(next, prevSeq, maint?.options?.gcInterval ?? 4)) await (maint?.dispatch ?? dispatchInlineAwaited)(() => runBoundedMaintenance({
|
|
620
|
+
storage: this.#storage,
|
|
621
|
+
currentJsonKey: this.#currentJsonKey,
|
|
622
|
+
prevSeq,
|
|
623
|
+
...maint?.maxFoldBytes !== void 0 && { maxFoldBytes: maint.maxFoldBytes }
|
|
624
|
+
}, maint?.options).then(() => {}, () => {}));
|
|
625
|
+
}
|
|
597
626
|
const classAOps = contentPutCount + entries.length + indexClassA + 1;
|
|
598
627
|
return {
|
|
599
628
|
entries: committedEntries,
|
|
@@ -651,7 +680,7 @@ var Writer = class {
|
|
|
651
680
|
*/
|
|
652
681
|
async #provisionCurrentJson() {
|
|
653
682
|
const initial = {
|
|
654
|
-
schema_version:
|
|
683
|
+
schema_version: 2,
|
|
655
684
|
snapshot: null,
|
|
656
685
|
next_seq: 0,
|
|
657
686
|
log_seq_start: 0,
|
|
@@ -659,7 +688,10 @@ var Writer = class {
|
|
|
659
688
|
epoch: 0,
|
|
660
689
|
owner: "",
|
|
661
690
|
claimed_at: ""
|
|
662
|
-
}
|
|
691
|
+
},
|
|
692
|
+
tail_bytes: 0,
|
|
693
|
+
snapshot_bytes: 0,
|
|
694
|
+
snapshot_rows: 0
|
|
663
695
|
};
|
|
664
696
|
try {
|
|
665
697
|
return await createCurrentJson(this.#storage, this.#currentJsonKey, initial);
|
|
@@ -683,7 +715,7 @@ var Writer = class {
|
|
|
683
715
|
}
|
|
684
716
|
if (entry.collection !== collection || entry.doc_id !== docId) continue;
|
|
685
717
|
if (entry.op === "D") return;
|
|
686
|
-
if ((entry.op === "I" || entry.op === "U") && entry.
|
|
718
|
+
if ((entry.op === "I" || entry.op === "U") && entry.after !== void 0) return entry.after;
|
|
687
719
|
}
|
|
688
720
|
}
|
|
689
721
|
/**
|
|
@@ -1446,8 +1478,8 @@ const writerFor = (ctx) => new Writer({
|
|
|
1446
1478
|
* — matches the locked `Collection.insert` contract
|
|
1447
1479
|
* (`packages/protocol/src/collection-api.ts:123–125`).
|
|
1448
1480
|
*
|
|
1449
|
-
* The emitted `LogEntry` has `op:"I"` and `
|
|
1450
|
-
* (today's per-doc-replace model
|
|
1481
|
+
* The emitted `LogEntry` has `op:"I"` and `after === {...doc, _id}`
|
|
1482
|
+
* (today's per-doc-replace model).
|
|
1451
1483
|
*
|
|
1452
1484
|
* @throws BaerlyError code="Conflict" — `_id` collision (pre-commit check) or
|
|
1453
1485
|
* CAS retry budget exhausted inside `Writer.commit()`.
|
|
@@ -1507,8 +1539,8 @@ const runInsert = async (ctx, doc) => {
|
|
|
1507
1539
|
* `db.transaction(...)` exists to deliver.
|
|
1508
1540
|
*
|
|
1509
1541
|
* `replica_identity` defaults to `PATCH_ONLY` for every collection
|
|
1510
|
-
* today — emitted `U` entries carry `{
|
|
1511
|
-
* neither `
|
|
1542
|
+
* today — emitted `U` entries carry `{ after }` (the full post-image)
|
|
1543
|
+
* and neither `before` nor `key_old`. Consumers rebuilding pre-images
|
|
1512
1544
|
* under `PATCH_ONLY` need to maintain a shadow table — see
|
|
1513
1545
|
* `packages/protocol/src/log.ts:102–118`.
|
|
1514
1546
|
*
|
|
@@ -1600,8 +1632,8 @@ const runReplaceById = async (ctx, id, doc) => {
|
|
|
1600
1632
|
/**
|
|
1601
1633
|
* `Query.delete` implementation. Tombstones every matched row with a
|
|
1602
1634
|
* single `op:"D"` `LogEntry` per `doc_id`. `replica_identity` defaults
|
|
1603
|
-
* to `PATCH_ONLY`, so emitted `D` entries carry
|
|
1604
|
-
*
|
|
1635
|
+
* to `PATCH_ONLY`, so emitted `D` entries carry no body fields
|
|
1636
|
+
* (no `after`, no `before`/`key_old`).
|
|
1605
1637
|
* Consumers rebuilding pre-images under `PATCH_ONLY` need to
|
|
1606
1638
|
* maintain a shadow table — see `packages/protocol/src/log.ts:102–118`.
|
|
1607
1639
|
*
|
|
@@ -1877,4 +1909,4 @@ const sortByOrderSpec = (rows, spec) => {
|
|
|
1877
1909
|
//#endregion
|
|
1878
1910
|
export { runReplaceById as a, lsnParts as c, runInsert as i, delay as l, runAllWithMeta as n, validateOrThrow as o, runByIdWithMeta as r, Writer as s, makeQuery as t };
|
|
1879
1911
|
|
|
1880
|
-
//# sourceMappingURL=query-
|
|
1912
|
+
//# sourceMappingURL=query-BmiQy328.js.map
|