@dxos/migrations 0.8.4-main.fffef41 → 0.8.4-staging.60fe92afc8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"inputs":{"src/migration-builder.ts":{"bytes":22035,"imports":[{"path":"@automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","kind":"import-statement","external":true},{"path":"@dxos/echo/internal","kind":"import-statement","external":true},{"path":"@dxos/echo-db","kind":"import-statement","external":true},{"path":"@dxos/echo-protocol","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"src/migrations.ts":{"bytes":9709,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"}],"format":"esm"},"src/index.ts":{"bytes":653,"imports":[{"path":"src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"},{"path":"src/migrations.ts","kind":"import-statement","original":"./migrations"}],"format":"esm"}},"outputs":{"dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":15612},"dist/lib/browser/index.mjs":{"imports":[{"path":"@automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","kind":"import-statement","external":true},{"path":"@dxos/echo/internal","kind":"import-statement","external":true},{"path":"@dxos/echo-db","kind":"import-statement","external":true},{"path":"@dxos/echo-protocol","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"exports":["MigrationBuilder","Migrations"],"entryPoint":"src/index.ts","inputs":{"src/migration-builder.ts":{"bytesInOutput":4851},"src/index.ts":{"bytesInOutput":0},"src/migrations.ts":{"bytesInOutput":2727}},"bytes":7708}}}
1
+ {"inputs":{"src/annotations.ts":{"bytes":1241,"imports":[{"path":"effect/Schema","kind":"import-statement","external":true},{"path":"@dxos/echo","kind":"import-statement","external":true}],"format":"esm"},"src/migration-builder.ts":{"bytes":28104,"imports":[{"path":"@automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","kind":"import-statement","external":true},{"path":"@dxos/echo-client/internal","kind":"import-statement","external":true},{"path":"@dxos/echo-protocol","kind":"import-statement","external":true},{"path":"@dxos/echo/internal","kind":"import-statement","external":true},{"path":"@dxos/echo/Type","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/keys","kind":"import-statement","external":true}],"format":"esm"},"src/document-compaction.ts":{"bytes":4389,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"}],"format":"esm"},"src/migrations.ts":{"bytes":11492,"imports":[{"path":"@effect-atom/atom","kind":"import-statement","external":true},{"path":"@effect-atom/atom/Registry","kind":"import-statement","external":true},{"path":"effect/Option","kind":"import-statement","external":true},{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"src/annotations.ts","kind":"import-statement","original":"./annotations"},{"path":"src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"}],"format":"esm"},"src/index.ts":{"bytes":789,"imports":[{"path":"src/annotations.ts","kind":"import-statement","original":"./annotations"},{"path":"src/document-compaction.ts","kind":"import-statement","original":"./document-compaction"},{"path":"src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"},{"path":"src/migrations.ts","kind":"import-statement","original":"./migrations"}],"format":"esm"}},"outputs":{"dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":21776},"dist/lib/browser/index.mjs":{"imports":[{"path":"effect/Schema","kind":"import-statement","external":true},{"path":"@dxos/echo","kind":"import-statement","external":true},{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","kind":"import-statement","external":true},{"path":"@dxos/echo-client/internal","kind":"import-statement","external":true},{"path":"@dxos/echo-protocol","kind":"import-statement","external":true},{"path":"@dxos/echo/internal","kind":"import-statement","external":true},{"path":"@dxos/echo/Type","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"@effect-atom/atom","kind":"import-statement","external":true},{"path":"@effect-atom/atom/Registry","kind":"import-statement","external":true},{"path":"effect/Option","kind":"import-statement","external":true},{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"exports":["MigrationBuilder","MigrationVersionAnnotation","Migrations","compactDocumentsEpochMigration"],"entryPoint":"src/index.ts","inputs":{"src/annotations.ts":{"bytesInOutput":198},"src/index.ts":{"bytesInOutput":0},"src/document-compaction.ts":{"bytesInOutput":936},"src/migration-builder.ts":{"bytesInOutput":7116},"src/migrations.ts":{"bytesInOutput":2999}},"bytes":11528}}}
@@ -1,26 +1,42 @@
1
1
  import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
2
 
3
+ // src/annotations.ts
4
+ import * as Schema from "effect/Schema";
5
+ import { Annotation } from "@dxos/echo";
6
+ var MigrationVersionAnnotation = Annotation.make({
7
+ id: "org.dxos.migrations.version",
8
+ schema: Schema.String
9
+ });
10
+
11
+ // src/document-compaction.ts
12
+ import { SpaceState } from "@dxos/client/echo";
13
+ import { invariant as invariant2 } from "@dxos/invariant";
14
+
3
15
  // src/migration-builder.ts
4
- import { next as A } from "@automerge/automerge";
16
+ import { next as A, toJS } from "@automerge/automerge";
5
17
  import { CreateEpochRequest } from "@dxos/client/halo";
6
- import { requireTypeReference } from "@dxos/echo/internal";
7
- import { ObjectCore, migrateDocument } from "@dxos/echo-db";
8
- import { Reference, SpaceDocVersion, encodeReference } from "@dxos/echo-protocol";
18
+ import { ObjectCore, migrateDocument } from "@dxos/echo-client/internal";
19
+ import { EncodedReference, SpaceDocVersion } from "@dxos/echo-protocol";
20
+ import { getSchemaURI } from "@dxos/echo/internal";
21
+ import * as Type from "@dxos/echo/Type";
9
22
  import { invariant } from "@dxos/invariant";
23
+ import { EID, EntityId } from "@dxos/keys";
10
24
  var __dxlog_file = "/__w/dxos/dxos/packages/sdk/migrations/src/migration-builder.ts";
11
25
  var MigrationBuilder = class {
12
26
  _space;
13
27
  _repo;
14
28
  _rootDoc;
15
- // echoId -> automergeUrl
29
+ // echoUri -> automergeUrl
16
30
  _newLinks = {};
17
31
  _flushIds = [];
18
32
  _deleteObjects = [];
19
33
  _newRoot = void 0;
20
34
  constructor(_space) {
21
35
  this._space = _space;
22
- this._repo = this._space.db.coreDatabase._repo;
23
- this._rootDoc = this._space.db.coreDatabase._automergeDocLoader.getSpaceRootDocHandle().doc();
36
+ this._repo = this._space.internal.db._repo;
37
+ const rootDoc = this._space.internal.db._getSpaceRootDocHandle().doc();
38
+ invariant(rootDoc, "Space root document must be available when creating MigrationBuilder", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 41, S: this, A: ["rootDoc", "'Space root document must be available when creating MigrationBuilder'"] });
39
+ this._rootDoc = rootDoc;
24
40
  }
25
41
  async findObject(id) {
26
42
  const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString();
@@ -37,17 +53,10 @@ var MigrationBuilder = class {
37
53
  if (!objectStructure) {
38
54
  return;
39
55
  }
40
- const { schema, props } = await migrate(objectStructure);
56
+ const { type, props } = await migrate(objectStructure);
57
+ const schema = Type.getSchema(type);
41
58
  const oldHandle = await this._findObjectContainingHandle(id);
42
- invariant(oldHandle, void 0, {
43
- F: __dxlog_file,
44
- L: 84,
45
- S: this,
46
- A: [
47
- "oldHandle",
48
- ""
49
- ]
50
- });
59
+ invariant(oldHandle, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 62, S: this, A: ["oldHandle", ""] });
51
60
  const newState = {
52
61
  version: SpaceDocVersion.CURRENT,
53
62
  access: {
@@ -56,7 +65,7 @@ var MigrationBuilder = class {
56
65
  objects: {
57
66
  [id]: {
58
67
  system: {
59
- type: encodeReference(requireTypeReference(schema))
68
+ type: EncodedReference.fromURI(getSchemaURI(schema))
60
69
  },
61
70
  data: props,
62
71
  meta: {
@@ -67,58 +76,78 @@ var MigrationBuilder = class {
67
76
  };
68
77
  const migratedDoc = migrateDocument(oldHandle.doc(), newState);
69
78
  const newHandle = this._repo.import(A.save(migratedDoc));
79
+ await newHandle.whenReady();
80
+ invariant(newHandle.url, "Migrated document URL not available after whenReady", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 83, S: this, A: ["newHandle.url", "'Migrated document URL not available after whenReady'"] });
70
81
  this._newLinks[id] = newHandle.url;
71
- this._addHandleToFlushList(newHandle);
82
+ this._addHandleToFlushList(newHandle.documentId);
72
83
  }
73
- async addObject(schema, props) {
74
- const core = this._createObject({
75
- schema,
84
+ async addObject(type, props) {
85
+ const resolved = Type.getSchema(type);
86
+ const core = await this._createObject({
87
+ schema: resolved,
76
88
  props
77
89
  });
78
90
  return core.id;
79
91
  }
80
92
  createReference(id) {
81
- return encodeReference(Reference.localObjectReference(id));
93
+ invariant(EntityId.isValid(id), "Invalid EntityId.", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 96, S: this, A: ["EntityId.isValid(id)", "'Invalid EntityId.'"] });
94
+ return EncodedReference.fromURI(EID.make({
95
+ entityId: id
96
+ }));
82
97
  }
83
98
  deleteObject(id) {
84
99
  this._deleteObjects.push(id);
85
100
  }
86
- changeProperties(changeFn) {
101
+ /**
102
+ * Re-materializes linked object documents into fresh Automerge docs without history.
103
+ * Call {@link _commit} to publish a new space epoch with updated root links.
104
+ */
105
+ async compactLinkedDocuments(objectIds) {
106
+ const linkIds = objectIds ?? Object.keys(this._rootDoc.links ?? {});
107
+ const compacted = [];
108
+ const skipped = [];
109
+ for (const id of linkIds) {
110
+ const oldHandle = await this._findObjectContainingHandle(id);
111
+ if (!oldHandle) {
112
+ skipped.push(id);
113
+ continue;
114
+ }
115
+ await oldHandle.whenReady();
116
+ const materialized = toJS(oldHandle.doc());
117
+ const newHandle = this._repo.create(materialized);
118
+ await newHandle.whenReady();
119
+ invariant(newHandle.url, "Compacted document URL not available after whenReady", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 121, S: this, A: ["newHandle.url", "'Compacted document URL not available after whenReady'"] });
120
+ this._newLinks[id] = newHandle.url;
121
+ this._addHandleToFlushList(newHandle.documentId);
122
+ compacted.push(id);
123
+ }
124
+ return {
125
+ compacted,
126
+ skipped
127
+ };
128
+ }
129
+ async changeProperties(changeFn) {
87
130
  if (!this._newRoot) {
88
- this._buildNewRoot();
131
+ await this._buildNewRoot();
89
132
  }
90
- invariant(this._newRoot, "New root not created", {
91
- F: __dxlog_file,
92
- L: 126,
93
- S: this,
94
- A: [
95
- "this._newRoot",
96
- "'New root not created'"
97
- ]
98
- });
133
+ invariant(this._newRoot, "New root not created", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 135, S: this, A: ["this._newRoot", "'New root not created'"] });
99
134
  this._newRoot.change((doc) => {
100
135
  const propertiesStructure = doc.objects?.[this._space.properties.id];
101
136
  propertiesStructure && changeFn(propertiesStructure);
102
137
  });
103
- this._addHandleToFlushList(this._newRoot);
138
+ await this._newRoot.whenReady();
139
+ this._addHandleToFlushList(this._newRoot.documentId);
104
140
  }
105
141
  /**
106
142
  * @internal
107
143
  */
108
144
  async _commit() {
109
145
  if (!this._newRoot) {
110
- this._buildNewRoot();
146
+ await this._buildNewRoot();
111
147
  }
112
- invariant(this._newRoot, "New root not created", {
113
- F: __dxlog_file,
114
- L: 142,
115
- S: this,
116
- A: [
117
- "this._newRoot",
118
- "'New root not created'"
119
- ]
120
- });
148
+ invariant(this._newRoot, "New root not created", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 149, S: this, A: ["this._newRoot", "'New root not created'"] });
121
149
  await this._space.db.flush();
150
+ invariant(this._newRoot.url, "New root URL not available", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 152, S: this, A: ["this._newRoot.url", "'New root URL not available'"] });
122
151
  await this._space.internal.createEpoch({
123
152
  migration: CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT,
124
153
  automergeRootUrl: this._newRoot.url
@@ -133,7 +162,7 @@ var MigrationBuilder = class {
133
162
  await docHandle.whenReady();
134
163
  return docHandle;
135
164
  }
136
- _buildNewRoot() {
165
+ async _buildNewRoot() {
137
166
  const links = {
138
167
  ...this._rootDoc.links ?? {}
139
168
  };
@@ -151,15 +180,16 @@ var MigrationBuilder = class {
151
180
  objects: this._rootDoc.objects,
152
181
  links
153
182
  });
154
- this._addHandleToFlushList(this._newRoot);
183
+ await this._newRoot.whenReady();
184
+ this._addHandleToFlushList(this._newRoot.documentId);
155
185
  }
156
- _createObject({ id, schema, props }) {
186
+ async _createObject({ id, schema, props }) {
157
187
  const core = new ObjectCore();
158
188
  if (id) {
159
189
  core.id = id;
160
190
  }
161
191
  core.initNewObject(props);
162
- core.setType(requireTypeReference(schema));
192
+ core.setType(EncodedReference.fromURI(getSchemaURI(schema)));
163
193
  const newHandle = this._repo.create({
164
194
  version: SpaceDocVersion.CURRENT,
165
195
  access: {
@@ -169,25 +199,51 @@ var MigrationBuilder = class {
169
199
  [core.id]: core.getDoc()
170
200
  }
171
201
  });
202
+ await newHandle.whenReady();
172
203
  this._newLinks[core.id] = newHandle.url;
173
- this._addHandleToFlushList(newHandle);
204
+ this._addHandleToFlushList(newHandle.documentId);
174
205
  return core;
175
206
  }
176
- _addHandleToFlushList(handle) {
177
- this._flushIds.push(handle.documentId);
207
+ _addHandleToFlushList(id) {
208
+ this._flushIds.push(id);
178
209
  }
179
210
  };
180
211
 
212
+ // src/document-compaction.ts
213
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/sdk/migrations/src/document-compaction.ts";
214
+ var compactDocumentsEpochMigration = async (space, options = {}) => {
215
+ invariant2(space.state.get() === SpaceState.SPACE_READY, "Space must be open and ready before compaction.", { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 11, S: void 0, A: ["space.state.get() === SpaceState.SPACE_READY", "'Space must be open and ready before compaction.'"] });
216
+ const builder = new MigrationBuilder(space);
217
+ const { compacted, skipped } = await builder.compactLinkedDocuments(options.objectIds);
218
+ await builder._commit();
219
+ const epochs = await space.internal.getEpochs();
220
+ const lastEpoch = epochs[epochs.length - 1];
221
+ const epochNumber = lastEpoch?.subject.assertion.number ?? 0;
222
+ return {
223
+ compacted,
224
+ skipped,
225
+ epochNumber
226
+ };
227
+ };
228
+
181
229
  // src/migrations.ts
182
- import { SpaceState, live } from "@dxos/client/echo";
183
- import { invariant as invariant2 } from "@dxos/invariant";
184
- var __dxlog_file2 = "/__w/dxos/dxos/packages/sdk/migrations/src/migrations.ts";
230
+ import { Atom } from "@effect-atom/atom";
231
+ import * as Registry from "@effect-atom/atom/Registry";
232
+ import * as Option from "effect/Option";
233
+ import { SpaceState as SpaceState2 } from "@dxos/client/echo";
234
+ import { Annotation as Annotation2, Obj } from "@dxos/echo";
235
+ import { invariant as invariant3 } from "@dxos/invariant";
236
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/sdk/migrations/src/migrations.ts";
185
237
  var Migrations = class {
186
238
  static namespace;
187
239
  static migrations = [];
188
- static _state = live({
240
+ static _registry = Registry.make();
241
+ static _stateAtom = Atom.make({
189
242
  running: []
190
- });
243
+ }).pipe(Atom.keepAlive);
244
+ /**
245
+ * @deprecated Use `MigrationVersionAnnotation` via `Annotation.get/set` on space properties.
246
+ */
191
247
  static get versionProperty() {
192
248
  return this.namespace && `${this.namespace}.version`;
193
249
  }
@@ -195,77 +251,59 @@ var Migrations = class {
195
251
  return this.migrations[this.migrations.length - 1]?.version;
196
252
  }
197
253
  static running(space) {
198
- return this._state.running.includes(space.key.toHex());
254
+ const state = this._registry.get(this._stateAtom);
255
+ return state.running.includes(space.key.toHex());
199
256
  }
200
257
  static define(namespace, migrations) {
201
258
  this.namespace = namespace;
202
259
  this.migrations = migrations;
203
260
  }
204
261
  static async migrate(space, targetVersion) {
205
- invariant2(!this.running(space), "Migration already running", {
206
- F: __dxlog_file2,
207
- L: 44,
208
- S: this,
209
- A: [
210
- "!this.running(space)",
211
- "'Migration already running'"
212
- ]
213
- });
214
- invariant2(this.versionProperty, "Migrations namespace not set", {
215
- F: __dxlog_file2,
216
- L: 45,
217
- S: this,
218
- A: [
219
- "this.versionProperty",
220
- "'Migrations namespace not set'"
221
- ]
222
- });
223
- invariant2(space.state.get() === SpaceState.SPACE_READY, "Space not ready", {
224
- F: __dxlog_file2,
225
- L: 46,
226
- S: this,
227
- A: [
228
- "space.state.get() === SpaceState.SPACE_READY",
229
- "'Space not ready'"
230
- ]
231
- });
232
- const currentVersion = space.properties[this.versionProperty];
262
+ invariant3(!this.running(space), "Migration already running", { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 36, S: this, A: ["!this.running(space)", "'Migration already running'"] });
263
+ invariant3(space.state.get() === SpaceState2.SPACE_READY, "Space not ready", { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 37, S: this, A: ["space.state.get() === SpaceState.SPACE_READY", "'Space not ready'"] });
264
+ const currentVersion = Annotation2.get(space.properties, MigrationVersionAnnotation).pipe(Option.getOrUndefined);
233
265
  const currentIndex = this.migrations.findIndex((m) => m.version === currentVersion) + 1;
234
266
  const i = this.migrations.findIndex((m) => m.version === targetVersion);
235
267
  const targetIndex = i === -1 ? this.migrations.length : i + 1;
236
268
  if (currentIndex === targetIndex) {
237
269
  return false;
238
270
  }
239
- this._state.running.push(space.key.toHex());
240
- if (targetIndex > currentIndex) {
241
- const migrations = this.migrations.slice(currentIndex, targetIndex);
242
- for (const migration of migrations) {
243
- const builder = new MigrationBuilder(space);
244
- await migration.next({
245
- space,
246
- builder
247
- });
248
- builder.changeProperties((propertiesStructure) => {
249
- invariant2(this.versionProperty, "Migrations namespace not set", {
250
- F: __dxlog_file2,
251
- L: 62,
252
- S: this,
253
- A: [
254
- "this.versionProperty",
255
- "'Migrations namespace not set'"
256
- ]
271
+ const spaceKey = space.key.toHex();
272
+ const currentState = this._registry.get(this._stateAtom);
273
+ this._registry.set(this._stateAtom, {
274
+ running: [
275
+ ...currentState.running,
276
+ spaceKey
277
+ ]
278
+ });
279
+ try {
280
+ if (targetIndex > currentIndex) {
281
+ const migrations = this.migrations.slice(currentIndex, targetIndex);
282
+ for (const migration of migrations) {
283
+ const builder = new MigrationBuilder(space);
284
+ await migration.next({
285
+ space,
286
+ builder
287
+ });
288
+ await builder._commit();
289
+ Obj.update(space.properties, (properties) => {
290
+ Annotation2.set(properties, MigrationVersionAnnotation, migration.version);
257
291
  });
258
- propertiesStructure.data[this.versionProperty] = migration.version;
259
- });
260
- await builder._commit();
292
+ }
261
293
  }
294
+ } finally {
295
+ const finalState = this._registry.get(this._stateAtom);
296
+ this._registry.set(this._stateAtom, {
297
+ running: finalState.running.filter((key) => key !== spaceKey)
298
+ });
262
299
  }
263
- this._state.running.splice(this._state.running.indexOf(space.key.toHex()), 1);
264
300
  return true;
265
301
  }
266
302
  };
267
303
  export {
268
304
  MigrationBuilder,
269
- Migrations
305
+ MigrationVersionAnnotation,
306
+ Migrations,
307
+ compactDocumentsEpochMigration
270
308
  };
271
309
  //# sourceMappingURL=index.mjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../../../src/migration-builder.ts", "../../../src/migrations.ts"],
4
- "sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { next as A, type Doc } from '@automerge/automerge';\nimport { type AnyDocumentId, type DocumentId } from '@automerge/automerge-repo';\nimport type * as Schema from 'effect/Schema';\n\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { requireTypeReference } from '@dxos/echo/internal';\nimport { type DocHandleProxy, ObjectCore, type RepoProxy, migrateDocument } from '@dxos/echo-db';\nimport {\n type DatabaseDirectory,\n type ObjectStructure,\n Reference,\n SpaceDocVersion,\n encodeReference,\n} from '@dxos/echo-protocol';\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\n/*\n\nConsidering a better API for this:\n\n```ts\nconst migration = space.db.beginMigration(); // all actions are not visible to queries and are only applied once you call `apply`\n\nmigration.applyObjectMigration(defineMigration(From, To, { ... }));\n\nmigration.delete(id);\nmigration.add(obj);\n\nawait migration.apply(); // Will create new epoch.\n```\n\n*/\n\n// TODO(dmaretskyi): We no longer need to hook into ECHO internals, with the changes to echo APIs.\nexport class MigrationBuilder {\n private readonly _repo: RepoProxy;\n private readonly _rootDoc: Doc<DatabaseDirectory>;\n\n // echoId -> automergeUrl\n private readonly _newLinks: Record<string, string> = {};\n private readonly _flushIds: DocumentId[] = [];\n private readonly _deleteObjects: string[] = [];\n\n private _newRoot?: DocHandleProxy<DatabaseDirectory> = undefined;\n\n constructor(private readonly _space: Space) {\n this._repo = this._space.db.coreDatabase._repo;\n // TODO(wittjosiah): Accessing private API.\n this._rootDoc = (this._space.db.coreDatabase as any)._automergeDocLoader\n .getSpaceRootDocHandle()\n .doc() as Doc<DatabaseDirectory>;\n }\n\n async findObject(id: string): Promise<ObjectStructure | undefined> {\n const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString() as AnyDocumentId | undefined;\n const docHandle = documentId && this._repo.find(documentId);\n if (!docHandle) {\n return undefined;\n }\n\n await docHandle.whenReady();\n const doc = docHandle.doc() as Doc<DatabaseDirectory>;\n return doc.objects?.[id];\n }\n\n async migrateObject(\n id: string,\n migrate: (objectStructure: ObjectStructure) => MaybePromise<{ schema: Schema.Schema.AnyNoContext; props: any }>,\n ): Promise<void> {\n const objectStructure = await this.findObject(id);\n if (!objectStructure) {\n return;\n }\n\n const { schema, props } = await migrate(objectStructure);\n\n const oldHandle = await this._findObjectContainingHandle(id);\n invariant(oldHandle);\n\n const newState: DatabaseDirectory = {\n version: SpaceDocVersion.CURRENT,\n access: {\n spaceKey: this._space.key.toHex(),\n },\n objects: {\n [id]: {\n system: {\n type: encodeReference(requireTypeReference(schema)),\n },\n data: props,\n meta: {\n keys: [],\n },\n },\n },\n };\n const migratedDoc = migrateDocument(oldHandle.doc() as Doc<DatabaseDirectory>, newState);\n const newHandle = this._repo.import<DatabaseDirectory>(A.save(migratedDoc));\n this._newLinks[id] = newHandle.url;\n this._addHandleToFlushList(newHandle);\n }\n\n async addObject(schema: Schema.Schema.AnyNoContext, props: any): Promise<string> {\n const core = this._createObject({ schema, props });\n return core.id;\n }\n\n createReference(id: string) {\n return encodeReference(Reference.localObjectReference(id));\n }\n\n deleteObject(id: string): void {\n this._deleteObjects.push(id);\n }\n\n changeProperties(changeFn: (properties: ObjectStructure) => void): void {\n if (!this._newRoot) {\n this._buildNewRoot();\n }\n invariant(this._newRoot, 'New root not created');\n\n this._newRoot.change((doc: DatabaseDirectory) => {\n const propertiesStructure = doc.objects?.[this._space.properties.id];\n propertiesStructure && changeFn(propertiesStructure);\n });\n this._addHandleToFlushList(this._newRoot);\n }\n\n /**\n * @internal\n */\n async _commit(): Promise<void> {\n if (!this._newRoot) {\n this._buildNewRoot();\n }\n invariant(this._newRoot, 'New root not created');\n\n await this._space.db.flush();\n\n // Create new epoch.\n await this._space.internal.createEpoch({\n migration: CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT,\n automergeRootUrl: this._newRoot.url,\n });\n }\n\n private async _findObjectContainingHandle(id: string): Promise<DocHandleProxy<DatabaseDirectory> | undefined> {\n const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString() as AnyDocumentId | undefined;\n const docHandle = documentId && this._repo.find(documentId);\n if (!docHandle) {\n return undefined;\n }\n\n await docHandle.whenReady();\n return docHandle;\n }\n\n private _buildNewRoot(): void {\n const links = { ...(this._rootDoc.links ?? {}) };\n for (const id of this._deleteObjects) {\n delete links[id];\n }\n\n for (const [id, url] of Object.entries(this._newLinks)) {\n links[id] = new A.RawString(url);\n }\n\n this._newRoot = this._repo.create<DatabaseDirectory>({\n version: SpaceDocVersion.CURRENT,\n access: {\n spaceKey: this._space.key.toHex(),\n },\n objects: this._rootDoc.objects,\n links,\n });\n this._addHandleToFlushList(this._newRoot);\n }\n\n private _createObject({\n id,\n schema,\n props,\n }: {\n id?: string;\n schema: Schema.Schema.AnyNoContext;\n props: any;\n }): ObjectCore {\n const core = new ObjectCore();\n if (id) {\n core.id = id;\n }\n\n core.initNewObject(props);\n core.setType(requireTypeReference(schema));\n const newHandle = this._repo.create<DatabaseDirectory>({\n version: SpaceDocVersion.CURRENT,\n access: {\n spaceKey: this._space.key.toHex(),\n },\n objects: {\n [core.id]: core.getDoc() as ObjectStructure,\n },\n });\n this._newLinks[core.id] = newHandle.url;\n this._addHandleToFlushList(newHandle);\n\n return core;\n }\n\n private _addHandleToFlushList(handle: DocHandleProxy<any>): void {\n this._flushIds.push(handle.documentId);\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { type Space, SpaceState, live } from '@dxos/client/echo';\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\nimport { MigrationBuilder } from './migration-builder';\n\nexport type MigrationContext = {\n space: Space;\n builder: MigrationBuilder;\n};\n\nexport type Migration = {\n version: string;\n next: (context: MigrationContext) => MaybePromise<void>;\n};\n\nexport class Migrations {\n static namespace?: string;\n static migrations: Migration[] = [];\n private static _state = live<{ running: string[] }>({ running: [] });\n\n static get versionProperty() {\n return this.namespace && `${this.namespace}.version`;\n }\n\n static get targetVersion() {\n return this.migrations[this.migrations.length - 1]?.version;\n }\n\n static running(space: Space): boolean {\n return this._state.running.includes(space.key.toHex());\n }\n\n static define(namespace: string, migrations: Migration[]): void {\n this.namespace = namespace;\n this.migrations = migrations;\n }\n\n static async migrate(space: Space, targetVersion?: string | number): Promise<boolean> {\n invariant(!this.running(space), 'Migration already running');\n invariant(this.versionProperty, 'Migrations namespace not set');\n invariant(space.state.get() === SpaceState.SPACE_READY, 'Space not ready');\n const currentVersion = space.properties[this.versionProperty];\n const currentIndex = this.migrations.findIndex((m) => m.version === currentVersion) + 1;\n const i = this.migrations.findIndex((m) => m.version === targetVersion);\n const targetIndex = i === -1 ? this.migrations.length : i + 1;\n if (currentIndex === targetIndex) {\n return false;\n }\n\n this._state.running.push(space.key.toHex());\n if (targetIndex > currentIndex) {\n const migrations = this.migrations.slice(currentIndex, targetIndex);\n for (const migration of migrations) {\n const builder = new MigrationBuilder(space);\n await migration.next({ space, builder });\n builder.changeProperties((propertiesStructure) => {\n invariant(this.versionProperty, 'Migrations namespace not set');\n propertiesStructure.data[this.versionProperty] = migration.version;\n });\n await builder._commit();\n }\n }\n this._state.running.splice(this._state.running.indexOf(space.key.toHex()), 1);\n\n return true;\n }\n}\n"],
5
- "mappings": ";;;AAIA,SAASA,QAAQC,SAAmB;AAKpC,SAASC,0BAA0B;AACnC,SAASC,4BAA4B;AACrC,SAA8BC,YAA4BC,uBAAuB;AACjF,SAGEC,WACAC,iBACAC,uBACK;AACP,SAASC,iBAAiB;;AAqBnB,IAAMC,mBAAN,MAAMA;;EACMC;EACAC;;EAGAC,YAAoC,CAAC;EACrCC,YAA0B,CAAA;EAC1BC,iBAA2B,CAAA;EAEpCC,WAA+CC;EAEvD,YAA6BC,QAAe;SAAfA,SAAAA;AAC3B,SAAKP,QAAQ,KAAKO,OAAOC,GAAGC,aAAaT;AAEzC,SAAKC,WAAY,KAAKM,OAAOC,GAAGC,aAAqBC,oBAClDC,sBAAqB,EACrBC,IAAG;EACR;EAEA,MAAMC,WAAWC,IAAkD;AACjE,UAAMC,cAAc,KAAKd,SAASe,QAAQF,EAAAA,KAAO,KAAKZ,UAAUY,EAAAA,IAAMG,SAAAA;AACtE,UAAMC,YAAYH,cAAc,KAAKf,MAAMmB,KAAKJ,UAAAA;AAChD,QAAI,CAACG,WAAW;AACd,aAAOZ;IACT;AAEA,UAAMY,UAAUE,UAAS;AACzB,UA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Version", "encodeReference", "invariant", "MigrationBuilder", "_repo", "_rootDoc", "_newLinks", "_flushIds", "_deleteObjects", "_newRoot", "undefined", "_space", "db", "coreDatabase", "_automergeDocLoader", "getSpaceRootDocHandle", "doc", "findObject", "id", "documentId", "links", "toString", "docHandle", "find", "whenReady", "objects", "migrateObject", "migrate", "objectStructure", "schema", "props", "oldHandle", "_findObjectContainingHandle", "newState", "version", "CURRENT", "access", "spaceKey", "key", "toHex", "system", "type", "data", "meta", "keys", "migratedDoc", "newHandle", "import", "save", "url", "_addHandleToFlushList", "addObject", "core", "_createObject", "createReference", "localObjectReference", "deleteObject", "push", "changeProperties", "changeFn", "_buildNewRoot", "change", "propertiesStructure", "properties", "_commit", "flush", "internal", "createEpoch", "migration", "Migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "Object", "entries", "RawString", "create", "initNewObject", "setType", "getDoc", "handle", "SpaceState", "live", "invariant", "Migrations", "namespace", "migrations", "_state", "live", "running", "versionProperty", "targetVersion", "length", "version", "space", "includes", "key", "toHex", "define", "migrate", "invariant", "state", "get", "SpaceState", "SPACE_READY", "currentVersion", "properties", "currentIndex", "findIndex", "m", "i", "targetIndex", "push", "slice", "migration", "builder", "MigrationBuilder", "next", "changeProperties", "propertiesStructure", "data", "_commit", "splice", "indexOf"]
3
+ "sources": ["../../../src/annotations.ts", "../../../src/document-compaction.ts", "../../../src/migration-builder.ts", "../../../src/migrations.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2026 DXOS.org\n//\n\nimport * as Schema from 'effect/Schema';\n\nimport { Annotation } from '@dxos/echo';\n\n/** Migration version stored on space properties meta. */\nexport const MigrationVersionAnnotation = Annotation.make({\n id: 'org.dxos.migrations.version',\n schema: Schema.String,\n});\n", "//\n// Copyright 2026 DXOS.org\n//\n\nimport { type Space, SpaceState } from '@dxos/client/echo';\nimport { invariant } from '@dxos/invariant';\n\nimport { MigrationBuilder } from './migration-builder';\n\nexport type CompactDocumentsOptions = {\n /**\n * Entity ids whose linked Automerge documents should be compacted.\n * Defaults to all ids in the space root `links` map.\n */\n objectIds?: string[];\n};\n\nexport type CompactDocumentsResult = {\n compacted: string[];\n skipped: string[];\n epochNumber: number;\n};\n\n/**\n * Re-materializes linked object documents into fresh Automerge docs (no history) and commits\n * a new space epoch with {@link CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT}.\n */\nexport const compactDocumentsEpochMigration = async (\n space: Space,\n options: CompactDocumentsOptions = {},\n): Promise<CompactDocumentsResult> => {\n invariant(space.state.get() === SpaceState.SPACE_READY, 'Space must be open and ready before compaction.');\n\n const builder = new MigrationBuilder(space);\n const { compacted, skipped } = await builder.compactLinkedDocuments(options.objectIds);\n await builder._commit();\n\n const epochs = await space.internal.getEpochs();\n const lastEpoch = epochs[epochs.length - 1];\n const epochNumber = lastEpoch?.subject.assertion.number ?? 0;\n\n return { compacted, skipped, epochNumber };\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { next as A, type Doc, toJS } from '@automerge/automerge';\nimport { type AnyDocumentId, type DocumentId } from '@automerge/automerge-repo';\nimport type * as Schema from 'effect/Schema';\n\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { type DocHandleProxy, ObjectCore, type RepoProxy, migrateDocument } from '@dxos/echo-client/internal';\nimport { type DatabaseDirectory, EncodedReference, type EntityStructure, SpaceDocVersion } from '@dxos/echo-protocol';\nimport { getSchemaURI } from '@dxos/echo/internal';\nimport * as Type from '@dxos/echo/Type';\nimport { invariant } from '@dxos/invariant';\nimport { EID, EntityId } from '@dxos/keys';\nimport { type MaybePromise } from '@dxos/util';\n\n/*\n\nConsidering a better API for this:\n\n```ts\nconst migration = space.db.beginMigration(); // all actions are not visible to queries and are only applied once you call `apply`\n\nmigration.applyObjectMigration(defineMigration(From, To, { ... }));\n\nmigration.delete(id);\nmigration.add(obj);\n\nawait migration.apply(); // Will create new epoch.\n```\n\n*/\n\n// TODO(dmaretskyi): We no longer need to hook into ECHO internals, with the changes to echo APIs.\nexport class MigrationBuilder {\n private readonly _repo: RepoProxy;\n private readonly _rootDoc: Doc<DatabaseDirectory>;\n\n // echoUri -> automergeUrl\n private readonly _newLinks: Record<string, string> = {};\n private readonly _flushIds: DocumentId[] = [];\n private readonly _deleteObjects: string[] = [];\n\n private _newRoot?: DocHandleProxy<DatabaseDirectory> = undefined;\n\n constructor(private readonly _space: Space) {\n this._repo = this._space.internal.db._repo;\n const rootDoc = this._space.internal.db._getSpaceRootDocHandle().doc();\n invariant(rootDoc, 'Space root document must be available when creating MigrationBuilder');\n this._rootDoc = rootDoc;\n }\n\n async findObject(id: string): Promise<EntityStructure | undefined> {\n const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString() as AnyDocumentId | undefined;\n const docHandle = documentId && this._repo.find(documentId);\n if (!docHandle) {\n return undefined;\n }\n\n await docHandle.whenReady();\n const doc = docHandle.doc() as Doc<DatabaseDirectory>;\n return doc.objects?.[id];\n }\n\n async migrateObject(\n id: string,\n migrate: (objectStructure: EntityStructure) => MaybePromise<{ type: Type.AnyEntity; props: any }>,\n ): Promise<void> {\n const objectStructure = await this.findObject(id);\n if (!objectStructure) {\n return;\n }\n\n const { type, props } = await migrate(objectStructure);\n const schema = Type.getSchema(type);\n\n const oldHandle = await this._findObjectContainingHandle(id);\n invariant(oldHandle);\n\n const newState: DatabaseDirectory = {\n version: SpaceDocVersion.CURRENT,\n access: {\n spaceKey: this._space.key.toHex(),\n },\n objects: {\n [id]: {\n system: {\n type: EncodedReference.fromURI(getSchemaURI(schema)!),\n },\n data: props,\n meta: {\n keys: [],\n },\n },\n },\n };\n const migratedDoc = migrateDocument(oldHandle.doc() as Doc<DatabaseDirectory>, newState);\n const newHandle = this._repo.import<DatabaseDirectory>(A.save(migratedDoc));\n await newHandle.whenReady();\n invariant(newHandle.url, 'Migrated document URL not available after whenReady');\n this._newLinks[id] = newHandle.url;\n this._addHandleToFlushList(newHandle.documentId!);\n }\n\n async addObject(type: Type.AnyEntity, props: any): Promise<string> {\n const resolved = Type.getSchema(type);\n const core = await this._createObject({ schema: resolved, props });\n return core.id;\n }\n\n createReference(id: string) {\n invariant(EntityId.isValid(id), 'Invalid EntityId.');\n return EncodedReference.fromURI(EID.make({ entityId: id }));\n }\n\n deleteObject(id: string): void {\n this._deleteObjects.push(id);\n }\n\n /**\n * Re-materializes linked object documents into fresh Automerge docs without history.\n * Call {@link _commit} to publish a new space epoch with updated root links.\n */\n async compactLinkedDocuments(objectIds?: string[]): Promise<{ compacted: string[]; skipped: string[] }> {\n const linkIds = objectIds ?? Object.keys(this._rootDoc.links ?? {});\n const compacted: string[] = [];\n const skipped: string[] = [];\n\n for (const id of linkIds) {\n const oldHandle = await this._findObjectContainingHandle(id);\n if (!oldHandle) {\n skipped.push(id);\n continue;\n }\n\n await oldHandle.whenReady();\n const materialized = toJS(oldHandle.doc()!) as DatabaseDirectory;\n const newHandle = this._repo.create<DatabaseDirectory>(materialized);\n await newHandle.whenReady();\n invariant(newHandle.url, 'Compacted document URL not available after whenReady');\n this._newLinks[id] = newHandle.url;\n this._addHandleToFlushList(newHandle.documentId!);\n compacted.push(id);\n }\n\n return { compacted, skipped };\n }\n\n async changeProperties(changeFn: (properties: EntityStructure) => void): Promise<void> {\n if (!this._newRoot) {\n await this._buildNewRoot();\n }\n invariant(this._newRoot, 'New root not created');\n\n this._newRoot.change((doc: DatabaseDirectory) => {\n const propertiesStructure = doc.objects?.[this._space.properties.id];\n propertiesStructure && changeFn(propertiesStructure);\n });\n await this._newRoot.whenReady();\n this._addHandleToFlushList(this._newRoot.documentId!);\n }\n\n /**\n * @internal\n */\n async _commit(): Promise<void> {\n if (!this._newRoot) {\n await this._buildNewRoot();\n }\n invariant(this._newRoot, 'New root not created');\n\n await this._space.db.flush();\n\n // Create new epoch.\n invariant(this._newRoot.url, 'New root URL not available');\n await this._space.internal.createEpoch({\n migration: CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT,\n automergeRootUrl: this._newRoot.url,\n });\n }\n\n private async _findObjectContainingHandle(id: string): Promise<DocHandleProxy<DatabaseDirectory> | undefined> {\n const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString() as AnyDocumentId | undefined;\n const docHandle = documentId && this._repo.find(documentId);\n if (!docHandle) {\n return undefined;\n }\n\n await docHandle.whenReady();\n return docHandle;\n }\n\n private async _buildNewRoot(): Promise<void> {\n const links = { ...(this._rootDoc.links ?? {}) };\n for (const id of this._deleteObjects) {\n delete links[id];\n }\n\n for (const [id, url] of Object.entries(this._newLinks)) {\n links[id] = new A.RawString(url);\n }\n\n this._newRoot = this._repo.create<DatabaseDirectory>({\n version: SpaceDocVersion.CURRENT,\n access: {\n spaceKey: this._space.key.toHex(),\n },\n objects: this._rootDoc.objects,\n links,\n });\n await this._newRoot.whenReady();\n this._addHandleToFlushList(this._newRoot.documentId!);\n }\n\n private async _createObject({\n id,\n schema,\n props,\n }: {\n id?: string;\n schema: Schema.Schema.AnyNoContext;\n props: any;\n }): Promise<ObjectCore> {\n const core = new ObjectCore();\n if (id) {\n core.id = id;\n }\n\n core.initNewObject(props);\n core.setType(EncodedReference.fromURI(getSchemaURI(schema)!));\n const newHandle = this._repo.create<DatabaseDirectory>({\n version: SpaceDocVersion.CURRENT,\n access: {\n spaceKey: this._space.key.toHex(),\n },\n objects: {\n [core.id]: core.getDoc() as EntityStructure,\n },\n });\n await newHandle.whenReady();\n this._newLinks[core.id] = newHandle.url!;\n this._addHandleToFlushList(newHandle.documentId!);\n\n return core;\n }\n\n private _addHandleToFlushList(id: DocumentId): void {\n this._flushIds.push(id);\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { Atom } from '@effect-atom/atom';\nimport * as Registry from '@effect-atom/atom/Registry';\nimport * as Option from 'effect/Option';\n\nimport { type Space, SpaceState } from '@dxos/client/echo';\nimport { Annotation, Obj } from '@dxos/echo';\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\nimport { MigrationVersionAnnotation } from './annotations';\nimport { MigrationBuilder } from './migration-builder';\n\nexport type MigrationContext = {\n space: Space;\n builder: MigrationBuilder;\n};\n\nexport type Migration = {\n version: string;\n next: (context: MigrationContext) => MaybePromise<void>;\n};\n\nexport class Migrations {\n static namespace?: string;\n static migrations: Migration[] = [];\n private static _registry = Registry.make();\n private static _stateAtom = Atom.make<{ running: string[] }>({ running: [] }).pipe(Atom.keepAlive);\n\n /**\n * @deprecated Use `MigrationVersionAnnotation` via `Annotation.get/set` on space properties.\n */\n static get versionProperty() {\n return this.namespace && `${this.namespace}.version`;\n }\n\n static get targetVersion() {\n return this.migrations[this.migrations.length - 1]?.version;\n }\n\n static running(space: Space): boolean {\n const state = this._registry.get(this._stateAtom);\n return state.running.includes(space.key.toHex());\n }\n\n static define(namespace: string, migrations: Migration[]): void {\n this.namespace = namespace;\n this.migrations = migrations;\n }\n\n static async migrate(space: Space, targetVersion?: string | number): Promise<boolean> {\n invariant(!this.running(space), 'Migration already running');\n invariant(space.state.get() === SpaceState.SPACE_READY, 'Space not ready');\n const currentVersion = Annotation.get(space.properties, MigrationVersionAnnotation).pipe(Option.getOrUndefined);\n const currentIndex = this.migrations.findIndex((m) => m.version === currentVersion) + 1;\n const i = this.migrations.findIndex((m) => m.version === targetVersion);\n const targetIndex = i === -1 ? this.migrations.length : i + 1;\n if (currentIndex === targetIndex) {\n return false;\n }\n\n const spaceKey = space.key.toHex();\n const currentState = this._registry.get(this._stateAtom);\n this._registry.set(this._stateAtom, { running: [...currentState.running, spaceKey] });\n try {\n if (targetIndex > currentIndex) {\n const migrations = this.migrations.slice(currentIndex, targetIndex);\n for (const migration of migrations) {\n const builder = new MigrationBuilder(space);\n await migration.next({ space, builder });\n await builder._commit();\n Obj.update(space.properties, (properties) => {\n Annotation.set(properties, MigrationVersionAnnotation, migration.version);\n });\n }\n }\n } finally {\n const finalState = this._registry.get(this._stateAtom);\n this._registry.set(this._stateAtom, { running: finalState.running.filter((key) => key !== spaceKey) });\n }\n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A,cAAWG,cAAa7B;cACtB,aAAM8B,KAAcC,WAAAA,MAAiBxB,cAAAA,WAAAA;mBAC/BsB,aAAc,YAAC;gBAAEtB,UAAAA,IAAAA,iBAAAA,KAAAA;gBAAOuB,UAAAA,KAAAA;YAAQ;YACtC;UACAE,CAAAA;gBACEjB,QAAAA,QAAekB;AACjB,cAAA,OAAA,MAAA,YAAA,CAAA,eAAA;AACF,YAAAlB,YAAA,IAAA,YAAA,4BAAA,UAAA,OAAA;UACF,CAAA;QACF;MACE;;YACsCX,aAAS8B,KAAW9B,UAAQ+B,IAAM,KAAEC,UAAQA;AAAkB,WAAA,UAAA,IAAA,KAAA,YAAA;QACtG,SAAA,WAAA,QAAA,OAAA,CAAA,QAAA,QAAA,QAAA;MAEA,CAAA;IACF;AACF,WAAA;;;",
6
+ "names": ["Schema", "Annotation", "MigrationVersionAnnotation", "make", "id", "schema", "String", "SpaceState", "invariant", "next", "A", "toJS", "CreateEpochRequest", "ObjectCore", "migrateDocument", "EncodedReference", "SpaceDocVersion", "getSchemaURI", "Type", "invariant", "EID", "EntityId", "_repo", "_rootDoc", "_newLinks", "_deleteObjects", "_newRoot", "undefined", "_space", "rootDoc", "findObject", "docHandle", "objects", "migrateObject", "migrate", "objectStructure", "oldHandle", "_findObjectContainingHandle", "newState", "version", "access", "spaceKey", "system", "data", "props", "meta", "keys", "newHandle", "whenReady", "id", "_addHandleToFlushList", "addObject", "core", "_createObject", "schema", "resolved", "createReference", "entityId", "deleteObject", "skipped", "linkIds", "compacted", "_buildNewRoot", "propertiesStructure", "properties", "changeFn", "migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "documentId", "url", "create", "links", "initNewObject", "CURRENT", "__dxlog_file", "builder", "space", "invariant", "compacted", "skipped", "compactLinkedDocuments", "_commit", "epochs", "internal", "getEpochs", "lastEpoch", "epochNumber", "subject", "assertion", "Atom", "Registry", "Option", "SpaceState", "Annotation", "Obj", "invariant", "__dxlog_file", "Migrations", "migrations", "_registry", "_stateAtom", "Atom", "running", "keepAlive", "targetVersion", "space", "state", "define", "namespace", "invariant", "get", "SpaceState", "currentVersion", "Annotation", "MigrationVersionAnnotation", "pipe", "getOrUndefined", "currentIndex", "findIndex", "version", "i", "m", "targetIndex", "length", "spaceKey", "set", "currentState", "migration", "builder", "MigrationBuilder", "Obj", "properties", "finalState", "filter", "key"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"src/migration-builder.ts":{"bytes":22035,"imports":[{"path":"@automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","kind":"import-statement","external":true},{"path":"@dxos/echo/internal","kind":"import-statement","external":true},{"path":"@dxos/echo-db","kind":"import-statement","external":true},{"path":"@dxos/echo-protocol","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"src/migrations.ts":{"bytes":9709,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"}],"format":"esm"},"src/index.ts":{"bytes":653,"imports":[{"path":"src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"},{"path":"src/migrations.ts","kind":"import-statement","original":"./migrations"}],"format":"esm"}},"outputs":{"dist/lib/node-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":15614},"dist/lib/node-esm/index.mjs":{"imports":[{"path":"@automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","kind":"import-statement","external":true},{"path":"@dxos/echo/internal","kind":"import-statement","external":true},{"path":"@dxos/echo-db","kind":"import-statement","external":true},{"path":"@dxos/echo-protocol","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"exports":["MigrationBuilder","Migrations"],"entryPoint":"src/index.ts","inputs":{"src/migration-builder.ts":{"bytesInOutput":4851},"src/index.ts":{"bytesInOutput":0},"src/migrations.ts":{"bytesInOutput":2727}},"bytes":7801}}}
1
+ {"inputs":{"src/annotations.ts":{"bytes":1241,"imports":[{"path":"effect/Schema","kind":"import-statement","external":true},{"path":"@dxos/echo","kind":"import-statement","external":true}],"format":"esm"},"src/migration-builder.ts":{"bytes":28104,"imports":[{"path":"@automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","kind":"import-statement","external":true},{"path":"@dxos/echo-client/internal","kind":"import-statement","external":true},{"path":"@dxos/echo-protocol","kind":"import-statement","external":true},{"path":"@dxos/echo/internal","kind":"import-statement","external":true},{"path":"@dxos/echo/Type","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/keys","kind":"import-statement","external":true}],"format":"esm"},"src/document-compaction.ts":{"bytes":4389,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"}],"format":"esm"},"src/migrations.ts":{"bytes":11492,"imports":[{"path":"@effect-atom/atom","kind":"import-statement","external":true},{"path":"@effect-atom/atom/Registry","kind":"import-statement","external":true},{"path":"effect/Option","kind":"import-statement","external":true},{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"src/annotations.ts","kind":"import-statement","original":"./annotations"},{"path":"src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"}],"format":"esm"},"src/index.ts":{"bytes":789,"imports":[{"path":"src/annotations.ts","kind":"import-statement","original":"./annotations"},{"path":"src/document-compaction.ts","kind":"import-statement","original":"./document-compaction"},{"path":"src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"},{"path":"src/migrations.ts","kind":"import-statement","original":"./migrations"}],"format":"esm"}},"outputs":{"dist/lib/node-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":21778},"dist/lib/node-esm/index.mjs":{"imports":[{"path":"effect/Schema","kind":"import-statement","external":true},{"path":"@dxos/echo","kind":"import-statement","external":true},{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","kind":"import-statement","external":true},{"path":"@dxos/echo-client/internal","kind":"import-statement","external":true},{"path":"@dxos/echo-protocol","kind":"import-statement","external":true},{"path":"@dxos/echo/internal","kind":"import-statement","external":true},{"path":"@dxos/echo/Type","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"@effect-atom/atom","kind":"import-statement","external":true},{"path":"@effect-atom/atom/Registry","kind":"import-statement","external":true},{"path":"effect/Option","kind":"import-statement","external":true},{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"exports":["MigrationBuilder","MigrationVersionAnnotation","Migrations","compactDocumentsEpochMigration"],"entryPoint":"src/index.ts","inputs":{"src/annotations.ts":{"bytesInOutput":198},"src/index.ts":{"bytesInOutput":0},"src/document-compaction.ts":{"bytesInOutput":936},"src/migration-builder.ts":{"bytesInOutput":7116},"src/migrations.ts":{"bytesInOutput":2999}},"bytes":11621}}}
@@ -0,0 +1,4 @@
1
+ import { Annotation } from '@dxos/echo';
2
+ /** Migration version stored on space properties meta. */
3
+ export declare const MigrationVersionAnnotation: Annotation.Annotation<string>;
4
+ //# sourceMappingURL=annotations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"annotations.d.ts","sourceRoot":"","sources":["../../../src/annotations.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAExC,yDAAyD;AACzD,eAAO,MAAM,0BAA0B,+BAGrC,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { type Space } from '@dxos/client/echo';
2
+ export type CompactDocumentsOptions = {
3
+ /**
4
+ * Entity ids whose linked Automerge documents should be compacted.
5
+ * Defaults to all ids in the space root `links` map.
6
+ */
7
+ objectIds?: string[];
8
+ };
9
+ export type CompactDocumentsResult = {
10
+ compacted: string[];
11
+ skipped: string[];
12
+ epochNumber: number;
13
+ };
14
+ /**
15
+ * Re-materializes linked object documents into fresh Automerge docs (no history) and commits
16
+ * a new space epoch with {@link CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT}.
17
+ */
18
+ export declare const compactDocumentsEpochMigration: (space: Space, options?: CompactDocumentsOptions) => Promise<CompactDocumentsResult>;
19
+ //# sourceMappingURL=document-compaction.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"document-compaction.d.ts","sourceRoot":"","sources":["../../../src/document-compaction.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,KAAK,EAAc,MAAM,mBAAmB,CAAC;AAK3D,MAAM,MAAM,uBAAuB,GAAG;IACpC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG;IACnC,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,8BAA8B,UAClC,KAAK,YACH,uBAAuB,KAC/B,OAAO,CAAC,sBAAsB,CAYhC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=document-compaction.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"document-compaction.test.d.ts","sourceRoot":"","sources":["../../../src/document-compaction.test.ts"],"names":[],"mappings":""}
@@ -1,4 +1,6 @@
1
- export type { ObjectStructure } from '@dxos/echo-protocol';
1
+ export type { EntityStructure } from '@dxos/echo-protocol';
2
+ export * from './annotations';
3
+ export * from './document-compaction';
2
4
  export * from './migration-builder';
3
5
  export * from './migrations';
4
6
  //# sourceMappingURL=index.d.ts.map