@proteinjs/db 1.34.3 → 1.35.0

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.
Files changed (43) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/generated/index.js +1 -1
  3. package/dist/generated/index.js.map +1 -1
  4. package/dist/generated/test/index.d.ts.map +1 -1
  5. package/dist/generated/test/index.js +3 -1
  6. package/dist/generated/test/index.js.map +1 -1
  7. package/dist/src/Db.d.ts +10 -0
  8. package/dist/src/Db.d.ts.map +1 -1
  9. package/dist/src/Db.js +42 -2
  10. package/dist/src/Db.js.map +1 -1
  11. package/dist/src/MigrationRunner.d.ts +26 -0
  12. package/dist/src/MigrationRunner.d.ts.map +1 -1
  13. package/dist/src/MigrationRunner.js +82 -0
  14. package/dist/src/MigrationRunner.js.map +1 -1
  15. package/dist/src/source/SourceRecord.d.ts +35 -2
  16. package/dist/src/source/SourceRecord.d.ts.map +1 -1
  17. package/dist/src/source/SourceRecord.js +8 -1
  18. package/dist/src/source/SourceRecord.js.map +1 -1
  19. package/dist/src/source/SourceRecordLoader.d.ts +105 -7
  20. package/dist/src/source/SourceRecordLoader.d.ts.map +1 -1
  21. package/dist/src/source/SourceRecordLoader.js +358 -94
  22. package/dist/src/source/SourceRecordLoader.js.map +1 -1
  23. package/dist/src/tables/MigrationTable.d.ts +26 -0
  24. package/dist/src/tables/MigrationTable.d.ts.map +1 -1
  25. package/dist/src/tables/MigrationTable.js +1 -0
  26. package/dist/src/tables/MigrationTable.js.map +1 -1
  27. package/dist/test/reusable/SourceRecordSyncTests.d.ts.map +1 -1
  28. package/dist/test/reusable/SourceRecordSyncTests.js +803 -27
  29. package/dist/test/reusable/SourceRecordSyncTests.js.map +1 -1
  30. package/dist/test/util/tables/sourceRecordSyncTestTables.d.ts +16 -0
  31. package/dist/test/util/tables/sourceRecordSyncTestTables.d.ts.map +1 -1
  32. package/dist/test/util/tables/sourceRecordSyncTestTables.js +23 -1
  33. package/dist/test/util/tables/sourceRecordSyncTestTables.js.map +1 -1
  34. package/generated/index.ts +1 -1
  35. package/generated/test/index.ts +3 -1
  36. package/package.json +4 -4
  37. package/src/Db.ts +19 -0
  38. package/src/MigrationRunner.ts +62 -0
  39. package/src/source/SourceRecord.ts +42 -4
  40. package/src/source/SourceRecordLoader.ts +342 -48
  41. package/src/tables/MigrationTable.ts +24 -0
  42. package/test/reusable/SourceRecordSyncTests.ts +462 -11
  43. package/test/util/tables/sourceRecordSyncTestTables.ts +20 -0
@@ -1,14 +1,74 @@
1
+ import { Table } from '../Table';
2
+ /** What one boot of the sync did to one source-record table. */
3
+ export type SourceRecordTableLoadSummary = {
4
+ inserts: number;
5
+ updates: number;
6
+ unchanged: number;
7
+ adopted: number;
8
+ deletes: number;
9
+ removedUpdates: number;
10
+ /** Rows stamped by a newer version of their declaring package — left exactly as they are. */
11
+ skippedNewer: number;
12
+ /** Source-loaded rows with no owner stamp that no declaration in this build claims. */
13
+ unowned: number;
14
+ };
15
+ export type SourceRecordLoadSummary = {
16
+ [tableName: string]: SourceRecordTableLoadSummary;
17
+ };
1
18
  export declare class SourceRecordLoader {
2
19
  private logger;
3
- load(): Promise<void>;
20
+ /** Memo of resolved package versions — one resolution (and at most one warning) per source. */
21
+ private sourceVersions;
4
22
  /**
5
- * The removed-reconcile leg: rows previously loaded from source whose declaration no longer
6
- * exists (`is_loaded_from_source = true AND <key> NOT IN declared`), handled per the table's
7
- * `onSourceRemoved` policy — delete (default), keep, or update with a patch. The update leg
8
- * applies the patch only to rows whose fields actually differ (idempotent boots), through
9
- * `Db.update` so table watchers observe each write.
23
+ * One boot of the source-record sync, per source-record table in this build. Returns what it
24
+ * did to each table (also logged).
25
+ *
26
+ * Ownership model every row has one owner, the package that declares it, ordered by that
27
+ * package's version:
28
+ * - A boot speaks only for the packages it carries. Rows owned by a package this build does
29
+ * not carry are never touched: several servers running different builds against one shared
30
+ * database coexist without pruning each other's rows.
31
+ * - Within a package, a boot speaks only for its own version or older. A row stamped by a
32
+ * NEWER version of the same package is left exactly as it is — neither pruned nor rewritten —
33
+ * so ordinary version skew (an older build restarting against rows a newer build added or
34
+ * redefined) cannot delete or churn data. The newest version stays authoritative for
35
+ * genuine removals and redefinitions.
36
+ * - A package in the build is authoritative for all of its rows on every table, including
37
+ * tables it no longer declares anything for: dropping the last declaration is a removal.
38
+ * - Rows with no owner stamp (written before `source_package` existed) are claimed by the
39
+ * declaration that still matches them; the rest are reported as unowned and left alone.
40
+ *
41
+ * Accepted residuals: a package removed from every build leaves its rows behind (no surviving
42
+ * boot carries its authority — clean up explicitly); two builds of one package at the SAME
43
+ * version with differing sets (uncommitted local skew) are last-writer-wins, since versions
44
+ * cannot order them.
45
+ *
46
+ * With no argument, every source-record table is synced (the `Db.init` full pass). Passing
47
+ * `onlyTable` scopes the sync to that one table — the pre-schema-sync migration phase uses
48
+ * this to land the migration ledger's rows before the full schema sync has run (see
49
+ * {@link MigrationRunner.runPreSchemaSyncMigrations}); the later full pass re-reconciles the
50
+ * same rows idempotently under the same ownership model.
51
+ */
52
+ load(onlyTable?: Table<any>): Promise<SourceRecordLoadSummary>;
53
+ /**
54
+ * The removed-reconcile leg: rows owned by a package this build carries, at that package's
55
+ * version or older, that NO package in this build still declares
56
+ * (`is_loaded_from_source = true AND source_package IN <build's packages> AND <key> NOT IN
57
+ * <build's declared keys for the table>`, minus rows stamped by a newer version of their
58
+ * package) are handled per the table's `onSourceRemoved` policy — delete (default), keep, or
59
+ * update with a patch. The update leg applies the patch only to rows whose fields actually
60
+ * differ (idempotent boots), through `Db.update` so table watchers observe each write.
61
+ * See {@link load} for the ownership model.
10
62
  */
11
63
  private reconcileRemoved;
64
+ /**
65
+ * Source-loaded rows with no owner stamp that no declaration in this build claimed on this
66
+ * boot (the stamp leg has already run). They predate `source_package`, or their declaring
67
+ * package is not in this build; nobody prunes them. Reported so they are visible, never acted
68
+ * on. Tables that keep removed rows (`onSourceRemoved: 'keep'`) opted out of removal
69
+ * semantics altogether, so there is nothing to explain there.
70
+ */
71
+ private countUnowned;
12
72
  /**
13
73
  * Resolve and validate the property the sync keys on: `id` unless the table declares
14
74
  * `sourceRecordOptions.naturalKey`. A natural key must be schema-unique (a `ColumnOptions.unique`
@@ -29,7 +89,11 @@ export declare class SourceRecordLoader {
29
89
  * triggers a rewrite. Primitive columns retain their existing semantics.
30
90
  */
31
91
  private hasChanges;
32
- private getSourceRecordsMap;
92
+ /**
93
+ * Every source-record table in this build with the records declared for it, plus the set of
94
+ * packages that declare ANY source record in this build — the packages this boot speaks for.
95
+ */
96
+ private getDeclarations;
33
97
  /**
34
98
  * Find the first point of divergence between source and existing values.
35
99
  * Returns a description of the mismatch path, or null if they match.
@@ -58,5 +122,39 @@ export declare class SourceRecordLoader {
58
122
  * are sorted.
59
123
  */
60
124
  private canonicalStringify;
125
+ /** Memoized {@link resolveSourceVersion} — one resolution (and at most one warning) per source. */
126
+ private sourceVersion;
127
+ /**
128
+ * The declaring package's version, read from its own package.json at runtime. Resolution runs
129
+ * from the process cwd — the booting server's package, the one dependency tree every declaring
130
+ * package is reachable from (`@proteinjs/db` itself does not depend on the packages that
131
+ * declare records, so module-relative resolution could never find them). The package's entry
132
+ * is resolved (honoring exports maps, where `<pkg>/package.json` is usually not requireable),
133
+ * then the nearest package.json whose `name` matches is read walking up from it.
134
+ *
135
+ * Returns undefined where resolution is impossible — no Node `require` (browser bundles), or a
136
+ * package not resolvable from cwd. The sync then has no place in the version order for that
137
+ * package: it never touches its version-stamped rows, and what it writes is unversioned
138
+ * (see {@link isNewerStamp}). Logged once per boot so a misconfigured cwd is visible.
139
+ */
140
+ private resolveSourceVersion;
141
+ /**
142
+ * Whether a row's version stamp is strictly newer than the reconciling build's version of the
143
+ * same package — the guard that makes version skew of one package safe on a shared database:
144
+ * an older build never prunes (or flags) rows a newer build of the same package declared.
145
+ *
146
+ * Unversioned stamps (NULL — legacy rows, or builds whose version could not be resolved)
147
+ * carry no ordering and stay reconcilable (last-writer-wins, the pre-version behavior). A
148
+ * build whose OWN version is unresolvable cannot place itself in the order, so it never
149
+ * touches a version-stamped row — conservative: prefer leaving a removed row behind over
150
+ * deleting one that might be newer.
151
+ */
152
+ private isNewerStamp;
153
+ /**
154
+ * Semver comparison (major.minor.patch, prerelease-aware): negative when a < b, 0 when equal,
155
+ * positive when a > b, and undefined when either side does not parse as semver (unorderable —
156
+ * the caller treats that conservatively).
157
+ */
158
+ private compareVersions;
61
159
  }
62
160
  //# sourceMappingURL=SourceRecordLoader.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"SourceRecordLoader.d.ts","sourceRoot":"","sources":["../../../src/source/SourceRecordLoader.ts"],"names":[],"mappings":"AAYA,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,MAAM,CAA+C;IAEvD,IAAI;IAiEV;;;;;;OAMG;YACW,gBAAgB;IAoC9B;;;;;OAKG;IACH,OAAO,CAAC,eAAe;IA+CvB;;;;;;;;;;;OAWG;YACW,UAAU;YAmBV,mBAAmB;IAwBjC;;;;;;;;;;;;OAYG;IACH,OAAO,CAAC,gBAAgB;IAkDxB;;;;;;;;;;;;OAYG;IACH,OAAO,CAAC,kBAAkB;CAkB3B"}
1
+ {"version":3,"file":"SourceRecordLoader.d.ts","sourceRoot":"","sources":["../../../src/source/SourceRecordLoader.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAejC,gEAAgE;AAChE,MAAM,MAAM,4BAA4B,GAAG;IACzC,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,cAAc,EAAE,MAAM,CAAC;IACvB,6FAA6F;IAC7F,YAAY,EAAE,MAAM,CAAC;IACrB,uFAAuF;IACvF,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAAG;IAAE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