@dxos/migrations 0.8.1 → 0.8.2-main.10c050d

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,5 +1,5 @@
1
1
  // packages/sdk/migrations/src/migration-builder.ts
2
- import { next as am } from "@dxos/automerge/automerge";
2
+ import { next as am } from "@automerge/automerge";
3
3
  import { CreateEpochRequest } from "@dxos/client/halo";
4
4
  import { ObjectCore, migrateDocument } from "@dxos/echo-db";
5
5
  import { SpaceDocVersion, encodeReference, Reference } from "@dxos/echo-protocol";
@@ -14,7 +14,7 @@ var MigrationBuilder = class {
14
14
  this._deleteObjects = [];
15
15
  this._newRoot = void 0;
16
16
  this._repo = this._space.db.coreDatabase._repo;
17
- this._rootDoc = this._space.db.coreDatabase._automergeDocLoader.getSpaceRootDocHandle().docSync();
17
+ this._rootDoc = this._space.db.coreDatabase._automergeDocLoader.getSpaceRootDocHandle().doc();
18
18
  }
19
19
  async findObject(id) {
20
20
  const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString();
@@ -23,7 +23,7 @@ var MigrationBuilder = class {
23
23
  return void 0;
24
24
  }
25
25
  await docHandle.whenReady();
26
- const doc = docHandle.docSync();
26
+ const doc = docHandle.doc();
27
27
  return doc.objects?.[id];
28
28
  }
29
29
  async migrateObject(id, migrate) {
@@ -35,7 +35,7 @@ var MigrationBuilder = class {
35
35
  const oldHandle = await this._findObjectContainingHandle(id);
36
36
  invariant(oldHandle, void 0, {
37
37
  F: __dxlog_file,
38
- L: 58,
38
+ L: 66,
39
39
  S: this,
40
40
  A: [
41
41
  "oldHandle",
@@ -59,7 +59,7 @@ var MigrationBuilder = class {
59
59
  }
60
60
  }
61
61
  };
62
- const migratedDoc = migrateDocument(oldHandle.docSync(), newState);
62
+ const migratedDoc = migrateDocument(oldHandle.doc(), newState);
63
63
  const newHandle = this._repo.import(am.save(migratedDoc));
64
64
  this._newLinks[id] = newHandle.url;
65
65
  this._addHandleToFlushList(newHandle);
@@ -83,7 +83,7 @@ var MigrationBuilder = class {
83
83
  }
84
84
  invariant(this._newRoot, "New root not created", {
85
85
  F: __dxlog_file,
86
- L: 100,
86
+ L: 108,
87
87
  S: this,
88
88
  A: [
89
89
  "this._newRoot",
@@ -105,7 +105,7 @@ var MigrationBuilder = class {
105
105
  }
106
106
  invariant(this._newRoot, "New root not created", {
107
107
  F: __dxlog_file,
108
- L: 116,
108
+ L: 124,
109
109
  S: this,
110
110
  A: [
111
111
  "this._newRoot",
@@ -173,7 +173,7 @@ var MigrationBuilder = class {
173
173
  };
174
174
 
175
175
  // packages/sdk/migrations/src/migrations.ts
176
- import { create, SpaceState } from "@dxos/client/echo";
176
+ import { live, SpaceState } from "@dxos/client/echo";
177
177
  import { invariant as invariant2 } from "@dxos/invariant";
178
178
  var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migrations.ts";
179
179
  var Migrations = class {
@@ -181,7 +181,7 @@ var Migrations = class {
181
181
  this.migrations = [];
182
182
  }
183
183
  static {
184
- this._state = create({
184
+ this._state = live({
185
185
  running: []
186
186
  });
187
187
  }
@@ -189,7 +189,7 @@ var Migrations = class {
189
189
  return this.namespace && `${this.namespace}.version`;
190
190
  }
191
191
  static get targetVersion() {
192
- return this.migrations[this.migrations.length - 1].version;
192
+ return this.migrations[this.migrations.length - 1]?.version;
193
193
  }
194
194
  static running(space) {
195
195
  return this._state.running.includes(space.key.toHex());
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/migration-builder.ts", "../../../src/migrations.ts"],
4
- "sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Doc, next as am } from '@dxos/automerge/automerge';\nimport { type AnyDocumentId, type DocumentId } from '@dxos/automerge/automerge-repo';\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { ObjectCore, migrateDocument, type RepoProxy, type DocHandleProxy } from '@dxos/echo-db';\nimport { SpaceDocVersion, encodeReference, type ObjectStructure, type SpaceDoc, Reference } from '@dxos/echo-protocol';\nimport { requireTypeReference, type S } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\nexport class MigrationBuilder {\n private readonly _repo: RepoProxy;\n private readonly _rootDoc: Doc<SpaceDoc>;\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<SpaceDoc> = 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 .docSync() as Doc<SpaceDoc>;\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.docSync() as Doc<SpaceDoc>;\n return doc.objects?.[id];\n }\n\n async migrateObject(\n id: string,\n migrate: (objectStructure: ObjectStructure) => MaybePromise<{ schema: S.Schema.AnyNoContext; props: any }>,\n ) {\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: SpaceDoc = {\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.docSync() as Doc<SpaceDoc>, newState);\n const newHandle = this._repo.import<SpaceDoc>(am.save(migratedDoc));\n this._newLinks[id] = newHandle.url;\n this._addHandleToFlushList(newHandle);\n }\n\n async addObject(schema: S.Schema.AnyNoContext, props: any) {\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) {\n this._deleteObjects.push(id);\n }\n\n changeProperties(changeFn: (properties: ObjectStructure) => void) {\n if (!this._newRoot) {\n this._buildNewRoot();\n }\n invariant(this._newRoot, 'New root not created');\n\n this._newRoot.change((doc: SpaceDoc) => {\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() {\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<SpaceDoc> | 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() {\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 am.RawString(url);\n }\n\n this._newRoot = this._repo.create<SpaceDoc>({\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({ id, schema, props }: { id?: string; schema: S.Schema.AnyNoContext; props: any }) {\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<SpaceDoc>({\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>) {\n this._flushIds.push(handle.documentId);\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { type Space, create, SpaceState } 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 = create<{ 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) {\n return this._state.running.includes(space.key.toHex());\n }\n\n static define(namespace: string, migrations: Migration[]) {\n this.namespace = namespace;\n this.migrations = migrations;\n }\n\n static async migrate(space: Space, targetVersion?: string | number) {\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,SAAmBA,QAAQC,UAAU;AAGrC,SAASC,0BAA0B;AACnC,SAASC,YAAYC,uBAA4D;AACjF,SAASC,iBAAiBC,iBAAsDC,iBAAiB;AACjG,SAASC,4BAAoC;AAC7C,SAASC,iBAAiB;;AAGnB,IAAMC,mBAAN,MAAMA;EAWXC,YAA6BC,QAAe;SAAfA,SAAAA;SANZC,YAAoC,CAAC;SACrCC,YAA0B,CAAA;SAC1BC,iBAA2B,CAAA;SAEpCC,WAAsCC;AAG5C,SAAKC,QAAQ,KAAKN,OAAOO,GAAGC,aAAaF;AAEzC,SAAKG,WAAY,KAAKT,OAAOO,GAAGC,aAAqBE,oBAClDC,sBAAqB,EACrBC,QAAO;EACZ;EAEA,MAAMC,WAAWC,IAAkD;AACjE,UAAMC,cAAc,KAAKN,SAASO,QAAQF,EAAAA,KAAO,KAAKb,UAAUa,EAAAA,IAAMG,SAAAA;AACtE,UAAMC,YAAYH,cAAc,KAAKT,MAAMa,KAAKJ,UAAAA;AAChD,QAAI,CAACG,WAAW;AACd,aAAOb;IACT;AAEA,UAAMa,UAAUE,UAAS;AACzB,UAAMC,MAAMH,UAAUN,QAAO;AAC7B,WAAOS,IAAIC,UAAUR,EAAAA;EACvB;EAEA,MAAMS,cACJT,IACAU,SACA;AACA,UAAMC,kBAAkB,MAAM,KAAKZ,WAAWC,EAAAA;AAC9C,QAAI,CAACW,iBAAiB;AACpB;IACF;AAEA,UAAM,EAAEC,QAAQC,MAAK,IAAK,MAAMH,QAAQC,eAAAA;AAExC,UAAMG,YAAY,MAAM,KAAKC,4BAA4Bf,EAAAA;AACzDjB,cAAU+B,WAAAA,QAAAA;;;;;;;;;AAEV,UAAME,WAAqB;MACzBC,S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zB,WAAWuB,UAAU,CAACC,MAAMA,EAAEhB,YAAYF,aAAAA;AACzD,UAAMoB,cAAcD,MAAM,KAAK,KAAKzB,WAAWO,SAASkB,IAAI;AAC5D,QAAIH,iBAAiBI,aAAa;AAChC,aAAO;IACT;AAEA,SAAKzB,OAAOE,QAAQwB,KAAKlB,MAAME,IAAIC,MAAK,CAAA;AACxC,QAAIc,cAAcJ,cAAc;AAC9B,YAAMtB,aAAa,KAAKA,WAAW4B,MAAMN,cAAcI,WAAAA;AACvD,iBAAWG,aAAa7B,YAAY;AAClC,cAAM8B,UAAU,IAAIC,iBAAiBtB,KAAAA;AACrC,cAAMoB,UAAUG,KAAK;UAAEvB;UAAOqB;QAAQ,CAAA;AACtCA,gBAAQG,iBAAiB,CAACC,wBAAAA;AACxBnB,UAAAA,WAAU,KAAKX,iBAAiB,gCAAA;;;;;;;;;AAChC8B,8BAAoBC,KAAK,KAAK/B,eAAe,IAAIyB,UAAUrB;QAC7D,CAAA;AACA,cAAMsB,QAAQM,QAAO;MACvB;IACF;AACA,SAAKnC,OAAOE,QAAQkC,OAAO,KAAKpC,OAAOE,QAAQmC,QAAQ7B,MAAME,IAAIC,MAAK,CAAA,GAAK,CAAA;AAE3E,WAAO;EACT;AACF;",
6
- "names": ["next", "am", "CreateEpochRequest", "ObjectCore", "migrateDocument", "SpaceDocVersion", "encodeReference", "Reference", "requireTypeReference", "invariant", "MigrationBuilder", "constructor", "_space", "_newLinks", "_flushIds", "_deleteObjects", "_newRoot", "undefined", "_repo", "db", "coreDatabase", "_rootDoc", "_automergeDocLoader", "getSpaceRootDocHandle", "docSync", "findObject", "id", "documentId", "links", "toString", "docHandle", "find", "whenReady", "doc", "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", "create", "SpaceState", "invariant", "Migrations", "migrations", "_state", "create", "running", "versionProperty", "namespace", "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"]
4
+ "sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Doc, next as am } from '@automerge/automerge';\nimport { type AnyDocumentId, type DocumentId } from '@automerge/automerge-repo';\nimport { type Schema } from 'effect';\n\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { ObjectCore, migrateDocument, type RepoProxy, type DocHandleProxy } from '@dxos/echo-db';\nimport {\n SpaceDocVersion,\n encodeReference,\n type ObjectStructure,\n type DatabaseDirectory,\n Reference,\n} from '@dxos/echo-protocol';\nimport { requireTypeReference } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\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>(am.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 am.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, live, SpaceState } 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,SAAmBA,QAAQC,UAAU;AAKrC,SAASC,0BAA0B;AACnC,SAASC,YAAYC,uBAA4D;AACjF,SACEC,iBACAC,iBAGAC,iBACK;AACP,SAASC,4BAA4B;AACrC,SAASC,iBAAiB;;AAGnB,IAAMC,mBAAN,MAAMA;EAWX,YAA6BC,QAAe;SAAfA,SAAAA;SANZC,YAAoC,CAAC;SACr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AEvB;UAAOqB;QAAQ,CAAA;AACtCA,gBAAQG,iBAAiB,CAACC,wBAAAA;AACxBnB,UAAAA,WAAU,KAAKX,iBAAiB,gCAAA;;;;;;;;;AAChC8B,8BAAoBC,KAAK,KAAK/B,eAAe,IAAIyB,UAAUrB;QAC7D,CAAA;AACA,cAAMsB,QAAQM,QAAO;MACvB;IACF;AACA,SAAKnC,OAAOE,QAAQkC,OAAO,KAAKpC,OAAOE,QAAQmC,QAAQ7B,MAAME,IAAIC,MAAK,CAAA,GAAK,CAAA;AAE3E,WAAO;EACT;AACF;",
6
+ "names": ["next", "am", "CreateEpochRequest", "ObjectCore", "migrateDocument", "SpaceDocVersion", "encodeReference", "Reference", "requireTypeReference", "invariant", "MigrationBuilder", "_space", "_newLinks", "_flushIds", "_deleteObjects", "_newRoot", "undefined", "_repo", "db", "coreDatabase", "_rootDoc", "_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", "live", "SpaceState", "invariant", "Migrations", "migrations", "_state", "live", "running", "versionProperty", "namespace", "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"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytes":21027,"imports":[{"path":"@dxos/automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","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/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/sdk/migrations/src/migrations.ts":{"bytes":9845,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"}],"format":"esm"},"packages/sdk/migrations/src/index.ts":{"bytes":698,"imports":[{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"},{"path":"packages/sdk/migrations/src/migrations.ts","kind":"import-statement","original":"./migrations"}],"format":"esm"}},"outputs":{"packages/sdk/migrations/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":14843},"packages/sdk/migrations/dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","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/echo-schema","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":"packages/sdk/migrations/src/index.ts","inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytesInOutput":4851},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0},"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2757}},"bytes":7786}}}
1
+ {"inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytes":21062,"imports":[{"path":"@automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","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/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/sdk/migrations/src/migrations.ts":{"bytes":9722,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"}],"format":"esm"},"packages/sdk/migrations/src/index.ts":{"bytes":666,"imports":[{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"},{"path":"packages/sdk/migrations/src/migrations.ts","kind":"import-statement","original":"./migrations"}],"format":"esm"}},"outputs":{"packages/sdk/migrations/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":15113},"packages/sdk/migrations/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-db","kind":"import-statement","external":true},{"path":"@dxos/echo-protocol","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","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":"packages/sdk/migrations/src/index.ts","inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytesInOutput":4834},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0},"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2754}},"bytes":7766}}}
@@ -22,7 +22,7 @@ __export(node_exports, {
22
22
  Migrations: () => Migrations
23
23
  });
24
24
  module.exports = __toCommonJS(node_exports);
25
- var import_automerge = require("@dxos/automerge/automerge");
25
+ var import_automerge = require("@automerge/automerge");
26
26
  var import_halo = require("@dxos/client/halo");
27
27
  var import_echo_db = require("@dxos/echo-db");
28
28
  var import_echo_protocol = require("@dxos/echo-protocol");
@@ -39,7 +39,7 @@ var MigrationBuilder = class {
39
39
  this._deleteObjects = [];
40
40
  this._newRoot = void 0;
41
41
  this._repo = this._space.db.coreDatabase._repo;
42
- this._rootDoc = this._space.db.coreDatabase._automergeDocLoader.getSpaceRootDocHandle().docSync();
42
+ this._rootDoc = this._space.db.coreDatabase._automergeDocLoader.getSpaceRootDocHandle().doc();
43
43
  }
44
44
  async findObject(id) {
45
45
  const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString();
@@ -48,7 +48,7 @@ var MigrationBuilder = class {
48
48
  return void 0;
49
49
  }
50
50
  await docHandle.whenReady();
51
- const doc = docHandle.docSync();
51
+ const doc = docHandle.doc();
52
52
  return doc.objects?.[id];
53
53
  }
54
54
  async migrateObject(id, migrate) {
@@ -60,7 +60,7 @@ var MigrationBuilder = class {
60
60
  const oldHandle = await this._findObjectContainingHandle(id);
61
61
  (0, import_invariant.invariant)(oldHandle, void 0, {
62
62
  F: __dxlog_file,
63
- L: 58,
63
+ L: 66,
64
64
  S: this,
65
65
  A: [
66
66
  "oldHandle",
@@ -84,7 +84,7 @@ var MigrationBuilder = class {
84
84
  }
85
85
  }
86
86
  };
87
- const migratedDoc = (0, import_echo_db.migrateDocument)(oldHandle.docSync(), newState);
87
+ const migratedDoc = (0, import_echo_db.migrateDocument)(oldHandle.doc(), newState);
88
88
  const newHandle = this._repo.import(import_automerge.next.save(migratedDoc));
89
89
  this._newLinks[id] = newHandle.url;
90
90
  this._addHandleToFlushList(newHandle);
@@ -108,7 +108,7 @@ var MigrationBuilder = class {
108
108
  }
109
109
  (0, import_invariant.invariant)(this._newRoot, "New root not created", {
110
110
  F: __dxlog_file,
111
- L: 100,
111
+ L: 108,
112
112
  S: this,
113
113
  A: [
114
114
  "this._newRoot",
@@ -130,7 +130,7 @@ var MigrationBuilder = class {
130
130
  }
131
131
  (0, import_invariant.invariant)(this._newRoot, "New root not created", {
132
132
  F: __dxlog_file,
133
- L: 116,
133
+ L: 124,
134
134
  S: this,
135
135
  A: [
136
136
  "this._newRoot",
@@ -202,7 +202,7 @@ var Migrations = class {
202
202
  this.migrations = [];
203
203
  }
204
204
  static {
205
- this._state = (0, import_echo.create)({
205
+ this._state = (0, import_echo.live)({
206
206
  running: []
207
207
  });
208
208
  }
@@ -210,7 +210,7 @@ var Migrations = class {
210
210
  return this.namespace && `${this.namespace}.version`;
211
211
  }
212
212
  static get targetVersion() {
213
- return this.migrations[this.migrations.length - 1].version;
213
+ return this.migrations[this.migrations.length - 1]?.version;
214
214
  }
215
215
  static running(space) {
216
216
  return this._state.running.includes(space.key.toHex());
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/migration-builder.ts", "../../../src/migrations.ts"],
4
- "sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Doc, next as am } from '@dxos/automerge/automerge';\nimport { type AnyDocumentId, type DocumentId } from '@dxos/automerge/automerge-repo';\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { ObjectCore, migrateDocument, type RepoProxy, type DocHandleProxy } from '@dxos/echo-db';\nimport { SpaceDocVersion, encodeReference, type ObjectStructure, type SpaceDoc, Reference } from '@dxos/echo-protocol';\nimport { requireTypeReference, type S } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\nexport class MigrationBuilder {\n private readonly _repo: RepoProxy;\n private readonly _rootDoc: Doc<SpaceDoc>;\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<SpaceDoc> = 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 .docSync() as Doc<SpaceDoc>;\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.docSync() as Doc<SpaceDoc>;\n return doc.objects?.[id];\n }\n\n async migrateObject(\n id: string,\n migrate: (objectStructure: ObjectStructure) => MaybePromise<{ schema: S.Schema.AnyNoContext; props: any }>,\n ) {\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: SpaceDoc = {\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.docSync() as Doc<SpaceDoc>, newState);\n const newHandle = this._repo.import<SpaceDoc>(am.save(migratedDoc));\n this._newLinks[id] = newHandle.url;\n this._addHandleToFlushList(newHandle);\n }\n\n async addObject(schema: S.Schema.AnyNoContext, props: any) {\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) {\n this._deleteObjects.push(id);\n }\n\n changeProperties(changeFn: (properties: ObjectStructure) => void) {\n if (!this._newRoot) {\n this._buildNewRoot();\n }\n invariant(this._newRoot, 'New root not created');\n\n this._newRoot.change((doc: SpaceDoc) => {\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() {\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<SpaceDoc> | 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() {\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 am.RawString(url);\n }\n\n this._newRoot = this._repo.create<SpaceDoc>({\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({ id, schema, props }: { id?: string; schema: S.Schema.AnyNoContext; props: any }) {\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<SpaceDoc>({\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>) {\n this._flushIds.push(handle.documentId);\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { type Space, create, SpaceState } 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 = create<{ 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) {\n return this._state.running.includes(space.key.toHex());\n }\n\n static define(namespace: string, migrations: Migration[]) {\n this.namespace = namespace;\n this.migrations = migrations;\n }\n\n static async migrate(space: Space, targetVersion?: string | number) {\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,uBAAqC;AAGrC,kBAAmC;AACnC,qBAAiF;AACjF,2BAAiG;AACjG,yBAA6C;AAC7C,uBAA0B;ACP1B,kBAA+C;AAC/C,IAAAA,oBA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;AAClC,cAAMqB,UAAU,IAAI9G,iBAAiBgG,KAAAA;AACrC,cAAMtB,UAAUqC,KAAK;UAAEf;UAAOc;QAAQ,CAAA;AACtCA,gBAAQ9C,iBAAiB,CAACI,wBAAAA;AACxBpC,gCAAAA,WAAU,KAAK4D,iBAAiB,gCAAA;;;;;;;;;AAChCxB,8BAAoBvB,KAAK,KAAK+C,eAAe,IAAIlB,UAAUxC;QAC7D,CAAA;AACA,cAAM4E,QAAQxC,QAAO;MACvB;IACF;AACA,SAAKoB,OAAOC,QAAQqB,OAAO,KAAKtB,OAAOC,QAAQsB,QAAQjB,MAAMzD,IAAIC,MAAK,CAAA,GAAK,CAAA;AAE3E,WAAO;EACT;AACF;",
6
- "names": ["import_invariant", "MigrationBuilder", "constructor", "_space", "_newLinks", "_flushIds", "_deleteObjects", "_newRoot", "undefined", "_repo", "db", "coreDatabase", "_rootDoc", "_automergeDocLoader", "getSpaceRootDocHandle", "docSync", "findObject", "id", "documentId", "links", "toString", "docHandle", "find", "whenReady", "doc", "objects", "migrateObject", "migrate", "objectStructure", "schema", "props", "oldHandle", "_findObjectContainingHandle", "invariant", "newState", "version", "SpaceDocVersion", "CURRENT", "access", "spaceKey", "key", "toHex", "system", "type", "encodeReference", "requireTypeReference", "data", "meta", "keys", "migratedDoc", "migrateDocument", "newHandle", "import", "am", "save", "url", "_addHandleToFlushList", "addObject", "core", "_createObject", "createReference", "Reference", "localObjectReference", "deleteObject", "push", "changeProperties", "changeFn", "_buildNewRoot", "change", "propertiesStructure", "properties", "_commit", "flush", "internal", "createEpoch", "migration", "CreateEpochRequest", "Migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "Object", "entries", "RawString", "create", "ObjectCore", "initNewObject", "setType", "getDoc", "handle", "Migrations", "migrations", "_state", "running", "versionProperty", "namespace", "targetVersion", "length", "space", "includes", "define", "state", "get", "SpaceState", "SPACE_READY", "currentVersion", "currentIndex", "findIndex", "m", "i", "targetIndex", "slice", "builder", "next", "splice", "indexOf"]
4
+ "sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Doc, next as am } from '@automerge/automerge';\nimport { type AnyDocumentId, type DocumentId } from '@automerge/automerge-repo';\nimport { type Schema } from 'effect';\n\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { ObjectCore, migrateDocument, type RepoProxy, type DocHandleProxy } from '@dxos/echo-db';\nimport {\n SpaceDocVersion,\n encodeReference,\n type ObjectStructure,\n type DatabaseDirectory,\n Reference,\n} from '@dxos/echo-protocol';\nimport { requireTypeReference } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\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>(am.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 am.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, live, SpaceState } 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,uBAAqC;AAKrC,kBAAmC;AACnC,qBAAiF;AACjF,2BAMO;AACP,yBAAqC;AACrC,uBAA0B;ACf1B,kBAA6C;AAC7C,IAAAA,oBAA0B;;ADiBnB,IAAMC,mBAAN,MAAMA;EAWX,YAA6BC,QAAe;SAAfA,SAAAA;SANZC,YAAoC,CAAC;SACrCC,YAA0B,CAAA;SAC1BC,iBAA2B,CAAA;SAEpCC,WAA+CC;AAGrD,SAAKC,QAAQ,KAAKN,OAAOO,GAAGC,aAAaF;AAEzC,SAAKG,WAAY,KAAKT,OAAOO,GAAGC,aAAqBE,oBAClDC,sBAAqB,EACrBC,IAAG;EACR;EAEA,MAAMC,WAAWC,IAAkD;AACjE,UAAMC,cAAc,KAAKN,SAASO,QAAQF,EAAAA,KAAO,KAAKb,UAAUa,EAAAA,IAAMG,SAAAA;AACtE,UAAMC,YAAYH,cAAc,KAAKT,MAAMa,KAAKJ,UAAAA;AAChD,QAAI,CAACG,WAAW;AACd,aAAOb;IACT;AAEA,UAAMa,UAAUE,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"undefined", "_repo", "db", "coreDatabase", "_rootDoc", "_automergeDocLoader", "getSpaceRootDocHandle", "doc", "findObject", "id", "documentId", "links", "toString", "docHandle", "find", "whenReady", "objects", "migrateObject", "migrate", "objectStructure", "schema", "props", "oldHandle", "_findObjectContainingHandle", "invariant", "newState", "version", "SpaceDocVersion", "CURRENT", "access", "spaceKey", "key", "toHex", "system", "type", "encodeReference", "requireTypeReference", "data", "meta", "keys", "migratedDoc", "migrateDocument", "newHandle", "import", "am", "save", "url", "_addHandleToFlushList", "addObject", "core", "_createObject", "createReference", "Reference", "localObjectReference", "deleteObject", "push", "changeProperties", "changeFn", "_buildNewRoot", "change", "propertiesStructure", "properties", "_commit", "flush", "internal", "createEpoch", "migration", "CreateEpochRequest", "Migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "Object", "entries", "RawString", "create", "ObjectCore", "initNewObject", "setType", "getDoc", "handle", "Migrations", "migrations", "_state", "live", "running", "versionProperty", "namespace", "targetVersion", "length", "space", "includes", "define", "state", "get", "SpaceState", "SPACE_READY", "currentVersion", "currentIndex", "findIndex", "m", "i", "targetIndex", "slice", "builder", "next", "splice", "indexOf"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytes":21027,"imports":[{"path":"@dxos/automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","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/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/sdk/migrations/src/migrations.ts":{"bytes":9845,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"}],"format":"esm"},"packages/sdk/migrations/src/index.ts":{"bytes":698,"imports":[{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"},{"path":"packages/sdk/migrations/src/migrations.ts","kind":"import-statement","original":"./migrations"}],"format":"esm"}},"outputs":{"packages/sdk/migrations/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":14843},"packages/sdk/migrations/dist/lib/node/index.cjs":{"imports":[{"path":"@dxos/automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","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/echo-schema","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":"packages/sdk/migrations/src/index.ts","inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytesInOutput":4851},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0},"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2757}},"bytes":7786}}}
1
+ {"inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytes":21062,"imports":[{"path":"@automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","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/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/sdk/migrations/src/migrations.ts":{"bytes":9722,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"}],"format":"esm"},"packages/sdk/migrations/src/index.ts":{"bytes":666,"imports":[{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"},{"path":"packages/sdk/migrations/src/migrations.ts","kind":"import-statement","original":"./migrations"}],"format":"esm"}},"outputs":{"packages/sdk/migrations/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":15113},"packages/sdk/migrations/dist/lib/node/index.cjs":{"imports":[{"path":"@automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","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/echo-schema","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":"packages/sdk/migrations/src/index.ts","inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytesInOutput":4834},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0},"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2754}},"bytes":7766}}}
@@ -1,7 +1,7 @@
1
1
  import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
2
 
3
3
  // packages/sdk/migrations/src/migration-builder.ts
4
- import { next as am } from "@dxos/automerge/automerge";
4
+ import { next as am } from "@automerge/automerge";
5
5
  import { CreateEpochRequest } from "@dxos/client/halo";
6
6
  import { ObjectCore, migrateDocument } from "@dxos/echo-db";
7
7
  import { SpaceDocVersion, encodeReference, Reference } from "@dxos/echo-protocol";
@@ -16,7 +16,7 @@ var MigrationBuilder = class {
16
16
  this._deleteObjects = [];
17
17
  this._newRoot = void 0;
18
18
  this._repo = this._space.db.coreDatabase._repo;
19
- this._rootDoc = this._space.db.coreDatabase._automergeDocLoader.getSpaceRootDocHandle().docSync();
19
+ this._rootDoc = this._space.db.coreDatabase._automergeDocLoader.getSpaceRootDocHandle().doc();
20
20
  }
21
21
  async findObject(id) {
22
22
  const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString();
@@ -25,7 +25,7 @@ var MigrationBuilder = class {
25
25
  return void 0;
26
26
  }
27
27
  await docHandle.whenReady();
28
- const doc = docHandle.docSync();
28
+ const doc = docHandle.doc();
29
29
  return doc.objects?.[id];
30
30
  }
31
31
  async migrateObject(id, migrate) {
@@ -37,7 +37,7 @@ var MigrationBuilder = class {
37
37
  const oldHandle = await this._findObjectContainingHandle(id);
38
38
  invariant(oldHandle, void 0, {
39
39
  F: __dxlog_file,
40
- L: 58,
40
+ L: 66,
41
41
  S: this,
42
42
  A: [
43
43
  "oldHandle",
@@ -61,7 +61,7 @@ var MigrationBuilder = class {
61
61
  }
62
62
  }
63
63
  };
64
- const migratedDoc = migrateDocument(oldHandle.docSync(), newState);
64
+ const migratedDoc = migrateDocument(oldHandle.doc(), newState);
65
65
  const newHandle = this._repo.import(am.save(migratedDoc));
66
66
  this._newLinks[id] = newHandle.url;
67
67
  this._addHandleToFlushList(newHandle);
@@ -85,7 +85,7 @@ var MigrationBuilder = class {
85
85
  }
86
86
  invariant(this._newRoot, "New root not created", {
87
87
  F: __dxlog_file,
88
- L: 100,
88
+ L: 108,
89
89
  S: this,
90
90
  A: [
91
91
  "this._newRoot",
@@ -107,7 +107,7 @@ var MigrationBuilder = class {
107
107
  }
108
108
  invariant(this._newRoot, "New root not created", {
109
109
  F: __dxlog_file,
110
- L: 116,
110
+ L: 124,
111
111
  S: this,
112
112
  A: [
113
113
  "this._newRoot",
@@ -175,7 +175,7 @@ var MigrationBuilder = class {
175
175
  };
176
176
 
177
177
  // packages/sdk/migrations/src/migrations.ts
178
- import { create, SpaceState } from "@dxos/client/echo";
178
+ import { live, SpaceState } from "@dxos/client/echo";
179
179
  import { invariant as invariant2 } from "@dxos/invariant";
180
180
  var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migrations.ts";
181
181
  var Migrations = class {
@@ -183,7 +183,7 @@ var Migrations = class {
183
183
  this.migrations = [];
184
184
  }
185
185
  static {
186
- this._state = create({
186
+ this._state = live({
187
187
  running: []
188
188
  });
189
189
  }
@@ -191,7 +191,7 @@ var Migrations = class {
191
191
  return this.namespace && `${this.namespace}.version`;
192
192
  }
193
193
  static get targetVersion() {
194
- return this.migrations[this.migrations.length - 1].version;
194
+ return this.migrations[this.migrations.length - 1]?.version;
195
195
  }
196
196
  static running(space) {
197
197
  return this._state.running.includes(space.key.toHex());
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/migration-builder.ts", "../../../src/migrations.ts"],
4
- "sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Doc, next as am } from '@dxos/automerge/automerge';\nimport { type AnyDocumentId, type DocumentId } from '@dxos/automerge/automerge-repo';\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { ObjectCore, migrateDocument, type RepoProxy, type DocHandleProxy } from '@dxos/echo-db';\nimport { SpaceDocVersion, encodeReference, type ObjectStructure, type SpaceDoc, Reference } from '@dxos/echo-protocol';\nimport { requireTypeReference, type S } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\nexport class MigrationBuilder {\n private readonly _repo: RepoProxy;\n private readonly _rootDoc: Doc<SpaceDoc>;\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<SpaceDoc> = 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 .docSync() as Doc<SpaceDoc>;\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.docSync() as Doc<SpaceDoc>;\n return doc.objects?.[id];\n }\n\n async migrateObject(\n id: string,\n migrate: (objectStructure: ObjectStructure) => MaybePromise<{ schema: S.Schema.AnyNoContext; props: any }>,\n ) {\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: SpaceDoc = {\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.docSync() as Doc<SpaceDoc>, newState);\n const newHandle = this._repo.import<SpaceDoc>(am.save(migratedDoc));\n this._newLinks[id] = newHandle.url;\n this._addHandleToFlushList(newHandle);\n }\n\n async addObject(schema: S.Schema.AnyNoContext, props: any) {\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) {\n this._deleteObjects.push(id);\n }\n\n changeProperties(changeFn: (properties: ObjectStructure) => void) {\n if (!this._newRoot) {\n this._buildNewRoot();\n }\n invariant(this._newRoot, 'New root not created');\n\n this._newRoot.change((doc: SpaceDoc) => {\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() {\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<SpaceDoc> | 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() {\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 am.RawString(url);\n }\n\n this._newRoot = this._repo.create<SpaceDoc>({\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({ id, schema, props }: { id?: string; schema: S.Schema.AnyNoContext; props: any }) {\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<SpaceDoc>({\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>) {\n this._flushIds.push(handle.documentId);\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { type Space, create, SpaceState } 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 = create<{ 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) {\n return this._state.running.includes(space.key.toHex());\n }\n\n static define(namespace: string, migrations: Migration[]) {\n this.namespace = namespace;\n this.migrations = migrations;\n }\n\n static async migrate(space: Space, targetVersion?: string | number) {\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,SAAmBA,QAAQC,UAAU;AAGrC,SAASC,0BAA0B;AACnC,SAASC,YAAYC,uBAA4D;AACjF,SAASC,iBAAiBC,iBAAsDC,iBAAiB;AACjG,SAASC,4BAAoC;AAC7C,SAASC,iBAAiB;;AAGnB,IAAMC,mBAAN,MAAMA;EAWXC,YAA6BC,QAAe;SAAfA,SAAAA;SANZC,YAAoC,CAAC;SACrCC,YAA0B,CAAA;SAC1BC,iBAA2B,CAAA;SAEpCC,WAAsCC;AAG5C,SAAKC,QAAQ,KAAKN,OAAOO,GAAGC,aAAaF;AAEzC,SAAKG,WAAY,KAAKT,OAAOO,GAAGC,aAAqBE,oBAClDC,sBAAqB,EACrBC,QAAO;EACZ;EAEA,MAAMC,WAAWC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B;IACF;AACA,SAAKnC,OAAOE,QAAQkC,OAAO,KAAKpC,OAAOE,QAAQmC,QAAQ7B,MAAME,IAAIC,MAAK,CAAA,GAAK,CAAA;AAE3E,WAAO;EACT;AACF;",
6
- "names": ["next", "am", "CreateEpochRequest", "ObjectCore", "migrateDocument", "SpaceDocVersion", "encodeReference", "Reference", "requireTypeReference", "invariant", "MigrationBuilder", "constructor", "_space", "_newLinks", "_flushIds", "_deleteObjects", "_newRoot", "undefined", "_repo", "db", "coreDatabase", "_rootDoc", "_automergeDocLoader", "getSpaceRootDocHandle", "docSync", "findObject", "id", "documentId", "links", "toString", "docHandle", "find", "whenReady", "doc", "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", "create", "SpaceState", "invariant", "Migrations", "migrations", "_state", "create", "running", "versionProperty", "namespace", "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"]
4
+ "sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Doc, next as am } from '@automerge/automerge';\nimport { type AnyDocumentId, type DocumentId } from '@automerge/automerge-repo';\nimport { type Schema } from 'effect';\n\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { ObjectCore, migrateDocument, type RepoProxy, type DocHandleProxy } from '@dxos/echo-db';\nimport {\n SpaceDocVersion,\n encodeReference,\n type ObjectStructure,\n type DatabaseDirectory,\n Reference,\n} from '@dxos/echo-protocol';\nimport { requireTypeReference } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\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>(am.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 am.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, live, SpaceState } 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,SAAmBA,QAAQC,UAAU;AAKrC,SAASC,0BAA0B;AACnC,SAASC,YAAYC,uBAA4D;AACjF,SACEC,iBACAC,iBAGAC,iBACK;AACP,SAASC,4BAA4B;AACrC,SAASC,iBAAiB;;AAGnB,IAAMC,mBAAN,MAAMA;EAWX,YAA6BC,QAAe;SAAfA,SAAAA;SANZC,YAAoC,CAAC;SACrCC,YAA0B,CAAA;SAC1BC,iBAA2B,CAAA;SAEpCC,WAA+CC;AAGrD,SAAKC,QAAQ,KAAKN,OAAOO,GAAGC,aAAaF;AAEzC,SAAKG,WAAY,KAAKT,OAAOO,GAAGC,aAAqBE,oBAClDC,sBAAqB,EACrBC,IAAG;EACR;EAEA,MAAMC,WAAWC,IAAkD;AACjE,UAAMC,cAAc,KAAKN,SAASO,QAAQF,EAAAA,KAAO,KAAKb,UAAUa,EAAAA,IAAMG,SAAAA;AACtE,UAAMC,YAAYH,cAAc,KAAKT,MAAMa,KAAKJ,UAAAA;AAChD,QAAI,CAACG,WAAW;AACd,aAAOb;IACT;AAEA,UAAMa,UAAUE,UAAS;AACzB,UAAMR,MAAMM,UAAUN,IAAG;AACzB,WAAOA,IAAIS,UAAUP,EAAAA;EACvB;EAEA,MAAMQ,cACJR,IACAS,SACe;AACf,UAAMC,kBAAkB,MAAM,KAAKX,WAAWC,EAAAA;AAC9C,QAAI,CAACU,iBAAiB;AACpB;IACF;AAEA,UAA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BAAA;;;;;;;;;AACxD,UAAMC,iBAAiBX,MAAMY,WAAW,KAAKjB,eAAe;AAC5D,UAAMkB,eAAe,KAAKtB,WAAWuB,UAAU,CAACC,MAAMA,EAAEhB,YAAYY,cAAAA,IAAkB;AACtF,UAAMK,IAAI,KAAKzB,WAAWuB,UAAU,CAACC,MAAMA,EAAEhB,YAAYF,aAAAA;AACzD,UAAMoB,cAAcD,MAAM,KAAK,KAAKzB,WAAWO,SAASkB,IAAI;AAC5D,QAAIH,iBAAiBI,aAAa;AAChC,aAAO;IACT;AAEA,SAAKzB,OAAOE,QAAQwB,KAAKlB,MAAME,IAAIC,MAAK,CAAA;AACxC,QAAIc,cAAcJ,cAAc;AAC9B,YAAMtB,aAAa,KAAKA,WAAW4B,MAAMN,cAAcI,WAAAA;AACvD,iBAAWG,aAAa7B,YAAY;AAClC,cAAM8B,UAAU,IAAIC,iBAAiBtB,KAAAA;AACrC,cAAMoB,UAAUG,KAAK;UAAEvB;UAAOqB;QAAQ,CAAA;AACtCA,gBAAQG,iBAAiB,CAACC,wBAAAA;AACxBnB,UAAAA,WAAU,KAAKX,iBAAiB,gCAAA;;;;;;;;;AAChC8B,8BAAoBC,KAAK,KAAK/B,eAAe,IAAIyB,UAAUrB;QAC7D,CAAA;AACA,cAAMsB,QAAQM,QAAO;MACvB;IACF;AACA,SAAKnC,OAAOE,QAAQkC,OAAO,KAAKpC,OAAOE,QAAQmC,QAAQ7B,MAAME,IAAIC,MAAK,CAAA,GAAK,CAAA;AAE3E,WAAO;EACT;AACF;",
6
+ "names": ["next", "am", "CreateEpochRequest", "ObjectCore", "migrateDocument", "SpaceDocVersion", "encodeReference", "Reference", "requireTypeReference", "invariant", "MigrationBuilder", "_space", "_newLinks", "_flushIds", "_deleteObjects", "_newRoot", "undefined", "_repo", "db", "coreDatabase", "_rootDoc", "_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", "live", "SpaceState", "invariant", "Migrations", "migrations", "_state", "live", "running", "versionProperty", "namespace", "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"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytes":21027,"imports":[{"path":"@dxos/automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","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/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/sdk/migrations/src/migrations.ts":{"bytes":9845,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"}],"format":"esm"},"packages/sdk/migrations/src/index.ts":{"bytes":698,"imports":[{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"},{"path":"packages/sdk/migrations/src/migrations.ts","kind":"import-statement","original":"./migrations"}],"format":"esm"}},"outputs":{"packages/sdk/migrations/dist/lib/node-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":14845},"packages/sdk/migrations/dist/lib/node-esm/index.mjs":{"imports":[{"path":"@dxos/automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","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/echo-schema","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":"packages/sdk/migrations/src/index.ts","inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytesInOutput":4851},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0},"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2757}},"bytes":7879}}}
1
+ {"inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytes":21062,"imports":[{"path":"@automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","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/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/sdk/migrations/src/migrations.ts":{"bytes":9722,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"}],"format":"esm"},"packages/sdk/migrations/src/index.ts":{"bytes":666,"imports":[{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"},{"path":"packages/sdk/migrations/src/migrations.ts","kind":"import-statement","original":"./migrations"}],"format":"esm"}},"outputs":{"packages/sdk/migrations/dist/lib/node-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":15115},"packages/sdk/migrations/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-db","kind":"import-statement","external":true},{"path":"@dxos/echo-protocol","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","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":"packages/sdk/migrations/src/index.ts","inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytesInOutput":4834},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0},"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2754}},"bytes":7859}}}
@@ -1,6 +1,6 @@
1
+ import { type Schema } from 'effect';
1
2
  import { type Space } from '@dxos/client/echo';
2
3
  import { type ObjectStructure } from '@dxos/echo-protocol';
3
- import { type S } from '@dxos/echo-schema';
4
4
  import { type MaybePromise } from '@dxos/util';
5
5
  export declare class MigrationBuilder {
6
6
  private readonly _space;
@@ -13,10 +13,10 @@ export declare class MigrationBuilder {
13
13
  constructor(_space: Space);
14
14
  findObject(id: string): Promise<ObjectStructure | undefined>;
15
15
  migrateObject(id: string, migrate: (objectStructure: ObjectStructure) => MaybePromise<{
16
- schema: S.Schema.AnyNoContext;
16
+ schema: Schema.Schema.AnyNoContext;
17
17
  props: any;
18
18
  }>): Promise<void>;
19
- addObject(schema: S.Schema.AnyNoContext, props: any): Promise<string>;
19
+ addObject(schema: Schema.Schema.AnyNoContext, props: any): Promise<string>;
20
20
  createReference(id: string): import("@dxos/echo-protocol").EncodedReference;
21
21
  deleteObject(id: string): void;
22
22
  changeProperties(changeFn: (properties: ObjectStructure) => void): void;
@@ -1 +1 @@
1
- {"version":3,"file":"migration-builder.d.ts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AK,IAAI,GAAG,IAAI;YA+BzD,2BAA2B;IAWzC,OAAO,CAAC,aAAa;IAqBrB,OAAO,CAAC,aAAa;IA+BrB,OAAO,CAAC,qBAAqB;CAG9B"}
@@ -1 +1 @@
1
- {"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../../../src/migrations.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,KAAK,EAAsB,MAAM,mBAAmB,CAAC;AAEnE,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvD,MAAM,MAAM,gBAAgB,GAAG;IAC7B,KAAK,EAAE,KAAK,CAAC;IACb,OAAO,EAAE,gBAAgB,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,CAAC,OAAO,EAAE,gBAAgB,KAAK,YAAY,CAAC,IAAI,CAAC,CAAC;CACzD,CAAC;AAEF,qBAAa,UAAU;IACrB,MAAM,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC1B,MAAM,CAAC,UAAU,EAAE,SAAS,EAAE,CAAM;IACpC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAkD;IAEvE,MAAM,KAAK,eAAe,uBAEzB;IAED,MAAM,KAAK,aAAa,WAEvB;IAED,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK;IAI3B,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE;WAK3C,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM;CA6BnE"}
1
+ {"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../../../src/migrations.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,KAAK,EAAoB,MAAM,mBAAmB,CAAC;AAEjE,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvD,MAAM,MAAM,gBAAgB,GAAG;IAC7B,KAAK,EAAE,KAAK,CAAC;IACb,OAAO,EAAE,gBAAgB,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,CAAC,OAAO,EAAE,gBAAgB,KAAK,YAAY,CAAC,IAAI,CAAC,CAAC;CACzD,CAAC;AAEF,qBAAa,UAAU;IACrB,MAAM,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC1B,MAAM,CAAC,UAAU,EAAE,SAAS,EAAE,CAAM;IACpC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAgD;IAErE,MAAM,KAAK,eAAe,uBAEzB;IAED,MAAM,KAAK,aAAa,WAEvB;IAED,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO;IAIrC,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,GAAG,IAAI;WAKlD,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CA6BtF"}
@@ -1 +1 @@
1
- {"version":"5.7.3"}
1
+ {"version":"5.8.3"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/migrations",
3
- "version": "0.8.1",
3
+ "version": "0.8.2-main.10c050d",
4
4
  "description": "",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -24,15 +24,16 @@
24
24
  "src"
25
25
  ],
26
26
  "dependencies": {
27
- "@dxos/client": "0.8.1",
28
- "@dxos/echo-protocol": "0.8.1",
29
- "@dxos/echo-db": "0.8.1",
30
- "@dxos/echo-schema": "0.8.1",
31
- "@dxos/invariant": "0.8.1",
32
- "@dxos/protocols": "0.8.1",
33
- "@dxos/log": "0.8.1",
34
- "@dxos/util": "0.8.1",
35
- "@dxos/automerge": "0.8.1"
27
+ "@automerge/automerge": "3.0.0-beta.4",
28
+ "@automerge/automerge-repo": "2.0.1",
29
+ "@dxos/client": "0.8.2-main.10c050d",
30
+ "@dxos/echo-db": "0.8.2-main.10c050d",
31
+ "@dxos/echo-protocol": "0.8.2-main.10c050d",
32
+ "@dxos/echo-schema": "0.8.2-main.10c050d",
33
+ "@dxos/invariant": "0.8.2-main.10c050d",
34
+ "@dxos/log": "0.8.2-main.10c050d",
35
+ "@dxos/protocols": "0.8.2-main.10c050d",
36
+ "@dxos/util": "0.8.2-main.10c050d"
36
37
  },
37
38
  "devDependencies": {},
38
39
  "publishConfig": {
@@ -2,33 +2,41 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { type Doc, next as am } from '@dxos/automerge/automerge';
6
- import { type AnyDocumentId, type DocumentId } from '@dxos/automerge/automerge-repo';
5
+ import { type Doc, next as am } from '@automerge/automerge';
6
+ import { type AnyDocumentId, type DocumentId } from '@automerge/automerge-repo';
7
+ import { type Schema } from 'effect';
8
+
7
9
  import { type Space } from '@dxos/client/echo';
8
10
  import { CreateEpochRequest } from '@dxos/client/halo';
9
11
  import { ObjectCore, migrateDocument, type RepoProxy, type DocHandleProxy } from '@dxos/echo-db';
10
- import { SpaceDocVersion, encodeReference, type ObjectStructure, type SpaceDoc, Reference } from '@dxos/echo-protocol';
11
- import { requireTypeReference, type S } from '@dxos/echo-schema';
12
+ import {
13
+ SpaceDocVersion,
14
+ encodeReference,
15
+ type ObjectStructure,
16
+ type DatabaseDirectory,
17
+ Reference,
18
+ } from '@dxos/echo-protocol';
19
+ import { requireTypeReference } from '@dxos/echo-schema';
12
20
  import { invariant } from '@dxos/invariant';
13
21
  import { type MaybePromise } from '@dxos/util';
14
22
 
15
23
  export class MigrationBuilder {
16
24
  private readonly _repo: RepoProxy;
17
- private readonly _rootDoc: Doc<SpaceDoc>;
25
+ private readonly _rootDoc: Doc<DatabaseDirectory>;
18
26
 
19
27
  // echoId -> automergeUrl
20
28
  private readonly _newLinks: Record<string, string> = {};
21
29
  private readonly _flushIds: DocumentId[] = [];
22
30
  private readonly _deleteObjects: string[] = [];
23
31
 
24
- private _newRoot?: DocHandleProxy<SpaceDoc> = undefined;
32
+ private _newRoot?: DocHandleProxy<DatabaseDirectory> = undefined;
25
33
 
26
34
  constructor(private readonly _space: Space) {
27
35
  this._repo = this._space.db.coreDatabase._repo;
28
36
  // TODO(wittjosiah): Accessing private API.
29
37
  this._rootDoc = (this._space.db.coreDatabase as any)._automergeDocLoader
30
38
  .getSpaceRootDocHandle()
31
- .docSync() as Doc<SpaceDoc>;
39
+ .doc() as Doc<DatabaseDirectory>;
32
40
  }
33
41
 
34
42
  async findObject(id: string): Promise<ObjectStructure | undefined> {
@@ -39,14 +47,14 @@ export class MigrationBuilder {
39
47
  }
40
48
 
41
49
  await docHandle.whenReady();
42
- const doc = docHandle.docSync() as Doc<SpaceDoc>;
50
+ const doc = docHandle.doc() as Doc<DatabaseDirectory>;
43
51
  return doc.objects?.[id];
44
52
  }
45
53
 
46
54
  async migrateObject(
47
55
  id: string,
48
- migrate: (objectStructure: ObjectStructure) => MaybePromise<{ schema: S.Schema.AnyNoContext; props: any }>,
49
- ) {
56
+ migrate: (objectStructure: ObjectStructure) => MaybePromise<{ schema: Schema.Schema.AnyNoContext; props: any }>,
57
+ ): Promise<void> {
50
58
  const objectStructure = await this.findObject(id);
51
59
  if (!objectStructure) {
52
60
  return;
@@ -57,7 +65,7 @@ export class MigrationBuilder {
57
65
  const oldHandle = await this._findObjectContainingHandle(id);
58
66
  invariant(oldHandle);
59
67
 
60
- const newState: SpaceDoc = {
68
+ const newState: DatabaseDirectory = {
61
69
  version: SpaceDocVersion.CURRENT,
62
70
  access: {
63
71
  spaceKey: this._space.key.toHex(),
@@ -74,13 +82,13 @@ export class MigrationBuilder {
74
82
  },
75
83
  },
76
84
  };
77
- const migratedDoc = migrateDocument(oldHandle.docSync() as Doc<SpaceDoc>, newState);
78
- const newHandle = this._repo.import<SpaceDoc>(am.save(migratedDoc));
85
+ const migratedDoc = migrateDocument(oldHandle.doc() as Doc<DatabaseDirectory>, newState);
86
+ const newHandle = this._repo.import<DatabaseDirectory>(am.save(migratedDoc));
79
87
  this._newLinks[id] = newHandle.url;
80
88
  this._addHandleToFlushList(newHandle);
81
89
  }
82
90
 
83
- async addObject(schema: S.Schema.AnyNoContext, props: any) {
91
+ async addObject(schema: Schema.Schema.AnyNoContext, props: any): Promise<string> {
84
92
  const core = this._createObject({ schema, props });
85
93
  return core.id;
86
94
  }
@@ -89,17 +97,17 @@ export class MigrationBuilder {
89
97
  return encodeReference(Reference.localObjectReference(id));
90
98
  }
91
99
 
92
- deleteObject(id: string) {
100
+ deleteObject(id: string): void {
93
101
  this._deleteObjects.push(id);
94
102
  }
95
103
 
96
- changeProperties(changeFn: (properties: ObjectStructure) => void) {
104
+ changeProperties(changeFn: (properties: ObjectStructure) => void): void {
97
105
  if (!this._newRoot) {
98
106
  this._buildNewRoot();
99
107
  }
100
108
  invariant(this._newRoot, 'New root not created');
101
109
 
102
- this._newRoot.change((doc: SpaceDoc) => {
110
+ this._newRoot.change((doc: DatabaseDirectory) => {
103
111
  const propertiesStructure = doc.objects?.[this._space.properties.id];
104
112
  propertiesStructure && changeFn(propertiesStructure);
105
113
  });
@@ -109,7 +117,7 @@ export class MigrationBuilder {
109
117
  /**
110
118
  * @internal
111
119
  */
112
- async _commit() {
120
+ async _commit(): Promise<void> {
113
121
  if (!this._newRoot) {
114
122
  this._buildNewRoot();
115
123
  }
@@ -124,7 +132,7 @@ export class MigrationBuilder {
124
132
  });
125
133
  }
126
134
 
127
- private async _findObjectContainingHandle(id: string): Promise<DocHandleProxy<SpaceDoc> | undefined> {
135
+ private async _findObjectContainingHandle(id: string): Promise<DocHandleProxy<DatabaseDirectory> | undefined> {
128
136
  const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString() as AnyDocumentId | undefined;
129
137
  const docHandle = documentId && this._repo.find(documentId);
130
138
  if (!docHandle) {
@@ -135,7 +143,7 @@ export class MigrationBuilder {
135
143
  return docHandle;
136
144
  }
137
145
 
138
- private _buildNewRoot() {
146
+ private _buildNewRoot(): void {
139
147
  const links = { ...(this._rootDoc.links ?? {}) };
140
148
  for (const id of this._deleteObjects) {
141
149
  delete links[id];
@@ -145,7 +153,7 @@ export class MigrationBuilder {
145
153
  links[id] = new am.RawString(url);
146
154
  }
147
155
 
148
- this._newRoot = this._repo.create<SpaceDoc>({
156
+ this._newRoot = this._repo.create<DatabaseDirectory>({
149
157
  version: SpaceDocVersion.CURRENT,
150
158
  access: {
151
159
  spaceKey: this._space.key.toHex(),
@@ -156,7 +164,15 @@ export class MigrationBuilder {
156
164
  this._addHandleToFlushList(this._newRoot);
157
165
  }
158
166
 
159
- private _createObject({ id, schema, props }: { id?: string; schema: S.Schema.AnyNoContext; props: any }) {
167
+ private _createObject({
168
+ id,
169
+ schema,
170
+ props,
171
+ }: {
172
+ id?: string;
173
+ schema: Schema.Schema.AnyNoContext;
174
+ props: any;
175
+ }): ObjectCore {
160
176
  const core = new ObjectCore();
161
177
  if (id) {
162
178
  core.id = id;
@@ -164,7 +180,7 @@ export class MigrationBuilder {
164
180
 
165
181
  core.initNewObject(props);
166
182
  core.setType(requireTypeReference(schema));
167
- const newHandle = this._repo.create<SpaceDoc>({
183
+ const newHandle = this._repo.create<DatabaseDirectory>({
168
184
  version: SpaceDocVersion.CURRENT,
169
185
  access: {
170
186
  spaceKey: this._space.key.toHex(),
@@ -179,7 +195,7 @@ export class MigrationBuilder {
179
195
  return core;
180
196
  }
181
197
 
182
- private _addHandleToFlushList(handle: DocHandleProxy<any>) {
198
+ private _addHandleToFlushList(handle: DocHandleProxy<any>): void {
183
199
  this._flushIds.push(handle.documentId);
184
200
  }
185
201
  }
@@ -5,7 +5,7 @@
5
5
  import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'vitest';
6
6
 
7
7
  import { Client } from '@dxos/client';
8
- import { create, Filter, type Space } from '@dxos/client/echo';
8
+ import { live, Filter, type Space } from '@dxos/client/echo';
9
9
  import { TestBuilder } from '@dxos/client/testing';
10
10
  import { Expando } from '@dxos/echo-schema';
11
11
 
@@ -21,7 +21,8 @@ Migrations.define('test', [
21
21
  {
22
22
  version: '1970-01-02',
23
23
  next: async ({ space, builder }) => {
24
- const { objects } = await space.db.query({ namespace: 'test' }).run();
24
+ // TODO(dmaretskyi): Is this intended to query only expando objects? Change to `Filter.type(Expando, { namespace: 'test' })`
25
+ const { objects } = await space.db.query(Filter._props<any>({ namespace: 'test' })).run();
25
26
  for (const object of objects) {
26
27
  await builder.migrateObject(object.id, ({ data }) => ({
27
28
  schema: Expando,
@@ -33,7 +34,8 @@ Migrations.define('test', [
33
34
  {
34
35
  version: '1970-01-03',
35
36
  next: async ({ space, builder }) => {
36
- const { objects } = await space.db.query({ namespace: 'test' }).run();
37
+ // TODO(dmaretskyi): Is this intended to query only expando objects? Change to `Filter.type(Expando, { namespace: 'test' })`
38
+ const { objects } = await space.db.query(Filter._props<any>({ namespace: 'test' })).run();
37
39
  for (const object of objects) {
38
40
  await builder.migrateObject(object.id, ({ data }) => ({
39
41
  schema: Expando,
@@ -65,7 +67,7 @@ describe('Migrations', () => {
65
67
 
66
68
  test('if no migrations have been run before, runs all migrations', async () => {
67
69
  await Migrations.migrate(space);
68
- const { objects } = await space.db.query(Filter.schema(Expando, { namespace: 'test' })).run();
70
+ const { objects } = await space.db.query(Filter.type(Expando, { namespace: 'test' })).run();
69
71
  expect(objects).to.have.length(1);
70
72
  expect(objects[0].count).to.equal(6);
71
73
  expect(space.properties['test.version']).to.equal('1970-01-03');
@@ -73,9 +75,9 @@ describe('Migrations', () => {
73
75
 
74
76
  test('if some migrations have been run before, runs only the remaining migrations', async () => {
75
77
  space.properties['test.version'] = '1970-01-02';
76
- space.db.add(create(Expando, { namespace: 'test', count: 5 }));
78
+ space.db.add(live(Expando, { namespace: 'test', count: 5 }));
77
79
  await Migrations.migrate(space);
78
- const { objects } = await space.db.query(Filter.schema(Expando, { namespace: 'test' })).run();
80
+ const { objects } = await space.db.query(Filter.type(Expando, { namespace: 'test' })).run();
79
81
  expect(objects).to.have.length(1);
80
82
  expect(objects[0].count).to.equal(15);
81
83
  expect(space.properties['test.version']).to.equal('1970-01-03');
@@ -84,13 +86,13 @@ describe('Migrations', () => {
84
86
  test('if all migrations have been run before, does nothing', async () => {
85
87
  space.properties['test.version'] = '1970-01-03';
86
88
  await Migrations.migrate(space);
87
- const { objects } = await space.db.query(Filter.schema(Expando, { namespace: 'test' })).run();
89
+ const { objects } = await space.db.query(Filter.type(Expando, { namespace: 'test' })).run();
88
90
  expect(objects).to.have.length(0);
89
91
  });
90
92
 
91
93
  test('if target version is specified, runs only the migrations up to that version', async () => {
92
94
  await Migrations.migrate(space, '1970-01-02');
93
- const { objects } = await space.db.query(Filter.schema(Expando, { namespace: 'test' })).run();
95
+ const { objects } = await space.db.query(Filter.type(Expando, { namespace: 'test' })).run();
94
96
  expect(objects).to.have.length(1);
95
97
  expect(objects[0].count).to.equal(2);
96
98
  expect(space.properties['test.version']).to.equal('1970-01-02');
package/src/migrations.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { type Space, create, SpaceState } from '@dxos/client/echo';
5
+ import { type Space, live, SpaceState } from '@dxos/client/echo';
6
6
  import { invariant } from '@dxos/invariant';
7
7
  import { type MaybePromise } from '@dxos/util';
8
8
 
@@ -21,26 +21,26 @@ export type Migration = {
21
21
  export class Migrations {
22
22
  static namespace?: string;
23
23
  static migrations: Migration[] = [];
24
- private static _state = create<{ running: string[] }>({ running: [] });
24
+ private static _state = live<{ running: string[] }>({ running: [] });
25
25
 
26
26
  static get versionProperty() {
27
27
  return this.namespace && `${this.namespace}.version`;
28
28
  }
29
29
 
30
30
  static get targetVersion() {
31
- return this.migrations[this.migrations.length - 1].version;
31
+ return this.migrations[this.migrations.length - 1]?.version;
32
32
  }
33
33
 
34
- static running(space: Space) {
34
+ static running(space: Space): boolean {
35
35
  return this._state.running.includes(space.key.toHex());
36
36
  }
37
37
 
38
- static define(namespace: string, migrations: Migration[]) {
38
+ static define(namespace: string, migrations: Migration[]): void {
39
39
  this.namespace = namespace;
40
40
  this.migrations = migrations;
41
41
  }
42
42
 
43
- static async migrate(space: Space, targetVersion?: string | number) {
43
+ static async migrate(space: Space, targetVersion?: string | number): Promise<boolean> {
44
44
  invariant(!this.running(space), 'Migration already running');
45
45
  invariant(this.versionProperty, 'Migrations namespace not set');
46
46
  invariant(space.state.get() === SpaceState.SPACE_READY, 'Space not ready');