@dxos/migrations 0.3.9-main.16d375f

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ MIT License
2
+ Copyright (c) 2022 DXOS
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # @dxos/echo-signals
2
+
3
+ ## Installation
4
+
5
+ ```bash
6
+ pnpm i @dxos/echo-signals
7
+ ```
8
+
9
+ ## DXOS Resources
10
+
11
+ - [Website](https://dxos.org)
12
+ - [Developer Documentation](https://docs.dxos.org)
13
+ - Talk to us on [Discord](https://discord.gg/eXVfryv3sW)
14
+
15
+ ## Contributions
16
+
17
+ Your ideas, issues, and code are most welcome. Please take a look at our [community code of conduct](https://github.com/dxos/dxos/blob/main/CODE_OF_CONDUCT.md), the [issue guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-issues), and the [PR contribution guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-prs).
18
+
19
+ License: [MIT](./LICENSE) Copyright 2022 © DXOS
@@ -0,0 +1,70 @@
1
+ // packages/sdk/migrations/src/migrations.ts
2
+ import { SpaceState } from "@dxos/client/echo";
3
+ import { invariant } from "@dxos/invariant";
4
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migrations.ts";
5
+ var Migrations = class {
6
+ static {
7
+ this.migrations = [];
8
+ }
9
+ static get versionProperty() {
10
+ return this.namespace && `${this.namespace}.version`;
11
+ }
12
+ static define(namespace, migrations) {
13
+ this.namespace = namespace;
14
+ this.migrations = migrations;
15
+ }
16
+ // TODO(wittjosiah): Multi-space migrations.
17
+ static async migrate(space, targetVersion) {
18
+ invariant(this.versionProperty, "Migrations namespace not set", {
19
+ F: __dxlog_file,
20
+ L: 35,
21
+ S: this,
22
+ A: [
23
+ "this.versionProperty",
24
+ "'Migrations namespace not set'"
25
+ ]
26
+ });
27
+ invariant(space.state.get() === SpaceState.READY, "Space not ready", {
28
+ F: __dxlog_file,
29
+ L: 36,
30
+ S: this,
31
+ A: [
32
+ "space.state.get() === SpaceState.READY",
33
+ "'Space not ready'"
34
+ ]
35
+ });
36
+ const currentVersion = space.properties[this.versionProperty];
37
+ const currentIndex = this.migrations.findIndex((m) => m.version === currentVersion) + 1;
38
+ const i = this.migrations.findIndex((m) => m.version === targetVersion);
39
+ const targetIndex = i === -1 ? this.migrations.length : i + 1;
40
+ if (currentIndex === targetIndex) {
41
+ return false;
42
+ }
43
+ if (targetIndex > currentIndex) {
44
+ const migrations = this.migrations.slice(currentIndex, targetIndex);
45
+ for (const migration of migrations) {
46
+ await migration.up({
47
+ version: migration.version,
48
+ space
49
+ });
50
+ space.properties[this.versionProperty] = migration.version;
51
+ }
52
+ } else {
53
+ const migrations = this.migrations.slice(targetIndex, currentIndex);
54
+ migrations.reverse();
55
+ for (const migration of migrations) {
56
+ await migration.down({
57
+ version: migration.version,
58
+ space
59
+ });
60
+ const index = this.migrations.indexOf(migration);
61
+ space.properties[this.versionProperty] = this.migrations[index - 1]?.version;
62
+ }
63
+ }
64
+ return true;
65
+ }
66
+ };
67
+ export {
68
+ Migrations
69
+ };
70
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/migrations.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { type Space, SpaceState } from '@dxos/client/echo';\nimport { invariant } from '@dxos/invariant';\nimport type { MaybePromise } from '@dxos/util';\n\nexport type MigrationContext = {\n version: string | number;\n space: Space;\n};\n\ntype Migration = {\n version: string | number;\n up: (context: MigrationContext) => MaybePromise<void>;\n down: (context: MigrationContext) => MaybePromise<void>;\n};\n\nexport class Migrations {\n static namespace?: string;\n static migrations: Migration[] = [];\n\n static get versionProperty() {\n return this.namespace && `${this.namespace}.version`;\n }\n\n static define(namespace: string, migrations: Migration[]) {\n this.namespace = namespace;\n this.migrations = migrations;\n }\n\n // TODO(wittjosiah): Multi-space migrations.\n static async migrate(space: Space, targetVersion?: string | number) {\n invariant(this.versionProperty, 'Migrations namespace not set');\n invariant(space.state.get() === SpaceState.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 if (targetIndex > currentIndex) {\n const migrations = this.migrations.slice(currentIndex, targetIndex);\n for (const migration of migrations) {\n await migration.up({ version: migration.version, space });\n space.properties[this.versionProperty] = migration.version;\n }\n } else {\n const migrations = this.migrations.slice(targetIndex, currentIndex);\n migrations.reverse();\n for (const migration of migrations) {\n await migration.down({ version: migration.version, space });\n const index = this.migrations.indexOf(migration);\n space.properties[this.versionProperty] = this.migrations[index - 1]?.version;\n }\n }\n\n return true;\n }\n}\n"],
5
+ "mappings": ";AAIA,SAAqBA,kBAAkB;AACvC,SAASC,iBAAiB;;AAcnB,IAAMC,aAAN,MAAMA;EAEX;SAAOC,aAA0B,CAAA;;EAEjC,WAAWC,kBAAkB;AAC3B,WAAO,KAAKC,aAAa,GAAG,KAAKA,SAAS;EAC5C;EAEA,OAAOC,OAAOD,WAAmBF,YAAyB;AACxD,SAAKE,YAAYA;AACjB,SAAKF,aAAaA;EACpB;;EAGA,aAAaI,QAAQC,OAAcC,eAAiC;AAClER,cAAU,KAAKG,iBAAiB,gCAAA;;;;;;;;;AAChCH,cAAUO,MAAME,MAAMC,IAAG,MAAOX,WAAWY,OAAO,mBAAA;;;;;;;;;AAClD,UAAMC,iBAAiBL,MAAMM,WAAW,KAAKV,eAAe;AAC5D,UAAMW,eAAe,KAAKZ,WAAWa,UAAU,CAACC,MAAMA,EAAEC,YAAYL,cAAAA,IAAkB;AACtF,UAAMM,IAAI,KAAKhB,WAAWa,UAAU,CAACC,MAAMA,EAAEC,YAAYT,aAAAA;AACzD,UAAMW,cAAcD,MAAM,KAAK,KAAKhB,WAAWkB,SAASF,IAAI;AAC5D,QAAIJ,iBAAiBK,aAAa;AAChC,aAAO;IACT;AAEA,QAAIA,cAAcL,cAAc;AAC9B,YAAMZ,aAAa,KAAKA,WAAWmB,MAAMP,cAAcK,WAAAA;AACvD,iBAAWG,aAAapB,YAAY;AAClC,cAAMoB,UAAUC,GAAG;UAAEN,SAASK,UAAUL;UAASV;QAAM,CAAA;AACvDA,cAAMM,WAAW,KAAKV,eAAe,IAAImB,UAAUL;MACrD;IACF,OAAO;AACL,YAAMf,aAAa,KAAKA,WAAWmB,MAAMF,aAAaL,YAAAA;AACtDZ,iBAAWsB,QAAO;AAClB,iBAAWF,aAAapB,YAAY;AAClC,cAAMoB,UAAUG,KAAK;UAAER,SAASK,UAAUL;UAASV;QAAM,CAAA;AACzD,cAAMmB,QAAQ,KAAKxB,WAAWyB,QAAQL,SAAAA;AACtCf,cAAMM,WAAW,KAAKV,eAAe,IAAI,KAAKD,WAAWwB,QAAQ,CAAA,GAAIT;MACvE;IACF;AAEA,WAAO;EACT;AACF;",
6
+ "names": ["SpaceState", "invariant", "Migrations", "migrations", "versionProperty", "namespace", "define", "migrate", "space", "targetVersion", "state", "get", "READY", "currentVersion", "properties", "currentIndex", "findIndex", "m", "version", "i", "targetIndex", "length", "slice", "migration", "up", "reverse", "down", "index", "indexOf"]
7
+ }
@@ -0,0 +1 @@
1
+ {"inputs":{"packages/sdk/migrations/src/migrations.ts":{"bytes":7881,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/sdk/migrations/src/index.ts":{"bytes":469,"imports":[{"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":3742},"packages/sdk/migrations/dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"exports":["Migrations"],"entryPoint":"packages/sdk/migrations/src/index.ts","inputs":{"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2140},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0}},"bytes":2245}}}
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var node_exports = {};
20
+ __export(node_exports, {
21
+ Migrations: () => Migrations
22
+ });
23
+ module.exports = __toCommonJS(node_exports);
24
+ var import_echo = require("@dxos/client/echo");
25
+ var import_invariant = require("@dxos/invariant");
26
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migrations.ts";
27
+ var Migrations = class {
28
+ static {
29
+ this.migrations = [];
30
+ }
31
+ static get versionProperty() {
32
+ return this.namespace && `${this.namespace}.version`;
33
+ }
34
+ static define(namespace, migrations) {
35
+ this.namespace = namespace;
36
+ this.migrations = migrations;
37
+ }
38
+ // TODO(wittjosiah): Multi-space migrations.
39
+ static async migrate(space, targetVersion) {
40
+ (0, import_invariant.invariant)(this.versionProperty, "Migrations namespace not set", {
41
+ F: __dxlog_file,
42
+ L: 35,
43
+ S: this,
44
+ A: [
45
+ "this.versionProperty",
46
+ "'Migrations namespace not set'"
47
+ ]
48
+ });
49
+ (0, import_invariant.invariant)(space.state.get() === import_echo.SpaceState.READY, "Space not ready", {
50
+ F: __dxlog_file,
51
+ L: 36,
52
+ S: this,
53
+ A: [
54
+ "space.state.get() === SpaceState.READY",
55
+ "'Space not ready'"
56
+ ]
57
+ });
58
+ const currentVersion = space.properties[this.versionProperty];
59
+ const currentIndex = this.migrations.findIndex((m) => m.version === currentVersion) + 1;
60
+ const i = this.migrations.findIndex((m) => m.version === targetVersion);
61
+ const targetIndex = i === -1 ? this.migrations.length : i + 1;
62
+ if (currentIndex === targetIndex) {
63
+ return false;
64
+ }
65
+ if (targetIndex > currentIndex) {
66
+ const migrations = this.migrations.slice(currentIndex, targetIndex);
67
+ for (const migration of migrations) {
68
+ await migration.up({
69
+ version: migration.version,
70
+ space
71
+ });
72
+ space.properties[this.versionProperty] = migration.version;
73
+ }
74
+ } else {
75
+ const migrations = this.migrations.slice(targetIndex, currentIndex);
76
+ migrations.reverse();
77
+ for (const migration of migrations) {
78
+ await migration.down({
79
+ version: migration.version,
80
+ space
81
+ });
82
+ const index = this.migrations.indexOf(migration);
83
+ space.properties[this.versionProperty] = this.migrations[index - 1]?.version;
84
+ }
85
+ }
86
+ return true;
87
+ }
88
+ };
89
+ // Annotate the CommonJS export names for ESM import in node:
90
+ 0 && (module.exports = {
91
+ Migrations
92
+ });
93
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/migrations.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { type Space, SpaceState } from '@dxos/client/echo';\nimport { invariant } from '@dxos/invariant';\nimport type { MaybePromise } from '@dxos/util';\n\nexport type MigrationContext = {\n version: string | number;\n space: Space;\n};\n\ntype Migration = {\n version: string | number;\n up: (context: MigrationContext) => MaybePromise<void>;\n down: (context: MigrationContext) => MaybePromise<void>;\n};\n\nexport class Migrations {\n static namespace?: string;\n static migrations: Migration[] = [];\n\n static get versionProperty() {\n return this.namespace && `${this.namespace}.version`;\n }\n\n static define(namespace: string, migrations: Migration[]) {\n this.namespace = namespace;\n this.migrations = migrations;\n }\n\n // TODO(wittjosiah): Multi-space migrations.\n static async migrate(space: Space, targetVersion?: string | number) {\n invariant(this.versionProperty, 'Migrations namespace not set');\n invariant(space.state.get() === SpaceState.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 if (targetIndex > currentIndex) {\n const migrations = this.migrations.slice(currentIndex, targetIndex);\n for (const migration of migrations) {\n await migration.up({ version: migration.version, space });\n space.properties[this.versionProperty] = migration.version;\n }\n } else {\n const migrations = this.migrations.slice(targetIndex, currentIndex);\n migrations.reverse();\n for (const migration of migrations) {\n await migration.down({ version: migration.version, space });\n const index = this.migrations.indexOf(migration);\n space.properties[this.versionProperty] = this.migrations[index - 1]?.version;\n }\n }\n\n return true;\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;AAIA,kBAAuC;AACvC,uBAA0B;;AAcnB,IAAMA,aAAN,MAAMA;EAEX,OAAA;SAAOC,aAA0B,CAAA;;EAEjC,WAAWC,kBAAkB;AAC3B,WAAO,KAAKC,aAAa,GAAG,KAAKA,SAAS;EAC5C;EAEA,OAAOC,OAAOD,WAAmBF,YAAyB;AACxD,SAAKE,YAAYA;AACjB,SAAKF,aAAaA;EACpB;;EAGA,aAAaI,QAAQC,OAAcC,eAAiC;AAClEC,oCAAU,KAAKN,iBAAiB,gCAAA;;;;;;;;;AAChCM,oCAAUF,MAAMG,MAAMC,IAAG,MAAOC,uBAAWC,OAAO,mBAAA;;;;;;;;;AAClD,UAAMC,iBAAiBP,MAAMQ,WAAW,KAAKZ,eAAe;AAC5D,UAAMa,eAAe,KAAKd,WAAWe,UAAU,CAACC,MAAMA,EAAEC,YAAYL,cAAAA,IAAkB;AACtF,UAAMM,IAAI,KAAKlB,WAAWe,UAAU,CAACC,MAAMA,EAAEC,YAAYX,aAAAA;AACzD,UAAMa,cAAcD,MAAM,KAAK,KAAKlB,WAAWoB,SAASF,IAAI;AAC5D,QAAIJ,iBAAiBK,aAAa;AAChC,aAAO;IACT;AAEA,QAAIA,cAAcL,cAAc;AAC9B,YAAMd,aAAa,KAAKA,WAAWqB,MAAMP,cAAcK,WAAAA;AACvD,iBAAWG,aAAatB,YAAY;AAClC,cAAMsB,UAAUC,GAAG;UAAEN,SAASK,UAAUL;UAASZ;QAAM,CAAA;AACvDA,cAAMQ,WAAW,KAAKZ,eAAe,IAAIqB,UAAUL;MACrD;IACF,OAAO;AACL,YAAMjB,aAAa,KAAKA,WAAWqB,MAAMF,aAAaL,YAAAA;AACtDd,iBAAWwB,QAAO;AAClB,iBAAWF,aAAatB,YAAY;AAClC,cAAMsB,UAAUG,KAAK;UAAER,SAASK,UAAUL;UAASZ;QAAM,CAAA;AACzD,cAAMqB,QAAQ,KAAK1B,WAAW2B,QAAQL,SAAAA;AACtCjB,cAAMQ,WAAW,KAAKZ,eAAe,IAAI,KAAKD,WAAW0B,QAAQ,CAAA,GAAIT;MACvE;IACF;AAEA,WAAO;EACT;AACF;",
6
+ "names": ["Migrations", "migrations", "versionProperty", "namespace", "define", "migrate", "space", "targetVersion", "invariant", "state", "get", "SpaceState", "READY", "currentVersion", "properties", "currentIndex", "findIndex", "m", "version", "i", "targetIndex", "length", "slice", "migration", "up", "reverse", "down", "index", "indexOf"]
7
+ }
@@ -0,0 +1 @@
1
+ {"inputs":{"packages/sdk/migrations/src/migrations.ts":{"bytes":7881,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/sdk/migrations/src/index.ts":{"bytes":469,"imports":[{"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":3742},"packages/sdk/migrations/dist/lib/node/index.cjs":{"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"exports":["Migrations"],"entryPoint":"packages/sdk/migrations/src/index.ts","inputs":{"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2140},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0}},"bytes":2245}}}
@@ -0,0 +1,2 @@
1
+ export * from './migrations';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,cAAc,CAAC"}
@@ -0,0 +1,20 @@
1
+ import { type Space } from '@dxos/client/echo';
2
+ import type { MaybePromise } from '@dxos/util';
3
+ export type MigrationContext = {
4
+ version: string | number;
5
+ space: Space;
6
+ };
7
+ type Migration = {
8
+ version: string | number;
9
+ up: (context: MigrationContext) => MaybePromise<void>;
10
+ down: (context: MigrationContext) => MaybePromise<void>;
11
+ };
12
+ export declare class Migrations {
13
+ static namespace?: string;
14
+ static migrations: Migration[];
15
+ static get versionProperty(): string | undefined;
16
+ static define(namespace: string, migrations: Migration[]): void;
17
+ static migrate(space: Space, targetVersion?: string | number): Promise<boolean>;
18
+ }
19
+ export {};
20
+ //# sourceMappingURL=migrations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../../../src/migrations.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,KAAK,EAAc,MAAM,mBAAmB,CAAC;AAE3D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,MAAM,MAAM,gBAAgB,GAAG;IAC7B,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,KAAK,EAAE,KAAK,CAAC;CACd,CAAC;AAEF,KAAK,SAAS,GAAG;IACf,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,EAAE,EAAE,CAAC,OAAO,EAAE,gBAAgB,KAAK,YAAY,CAAC,IAAI,CAAC,CAAC;IACtD,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;IAEpC,MAAM,KAAK,eAAe,uBAEzB;IAED,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE;WAM3C,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM;CA6BnE"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=migrations.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrations.test.d.ts","sourceRoot":"","sources":["../../../src/migrations.test.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@dxos/migrations",
3
+ "version": "0.3.9-main.16d375f",
4
+ "description": "",
5
+ "homepage": "https://dxos.org",
6
+ "bugs": "https://github.com/dxos/dxos/issues",
7
+ "license": "MIT",
8
+ "author": "info@dxos.org",
9
+ "exports": {
10
+ ".": {
11
+ "browser": "./dist/lib/browser/index.mjs",
12
+ "import": "./dist/lib/browser/index.mjs",
13
+ "require": "./dist/lib/node/index.cjs",
14
+ "node": "./dist/lib/node/index.cjs"
15
+ }
16
+ },
17
+ "types": "dist/types/src/index.d.ts",
18
+ "files": [
19
+ "dist",
20
+ "src"
21
+ ],
22
+ "dependencies": {
23
+ "@dxos/invariant": "0.3.9-main.16d375f",
24
+ "@dxos/util": "0.3.9-main.16d375f",
25
+ "@dxos/client": "0.3.9-main.16d375f"
26
+ },
27
+ "devDependencies": {},
28
+ "publishConfig": {
29
+ "access": "public"
30
+ }
31
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ export * from './migrations';
@@ -0,0 +1,119 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { expect } from 'chai';
6
+
7
+ import { Client } from '@dxos/client';
8
+ import { Expando, type Space } from '@dxos/client/echo';
9
+ import { TestBuilder } from '@dxos/client/testing';
10
+ import { describe, test, beforeEach, beforeAll, afterAll } from '@dxos/test';
11
+
12
+ import { Migrations } from './migrations';
13
+
14
+ Migrations.define('test', [
15
+ {
16
+ version: 1,
17
+ up: async ({ space }) => {
18
+ space.db.add(new Expando({ namespace: 'test', count: 1 }));
19
+ },
20
+ down: async ({ space }) => {
21
+ const { objects } = space.db.query({ namespace: 'test' });
22
+ for (const object of objects) {
23
+ space.db.remove(object);
24
+ }
25
+ },
26
+ },
27
+ {
28
+ version: 2,
29
+ up: async ({ space }) => {
30
+ const { objects } = space.db.query({ namespace: 'test' });
31
+ for (const object of objects) {
32
+ object.count = 2;
33
+ }
34
+ },
35
+ down: async () => {
36
+ // No-op.
37
+ },
38
+ },
39
+ {
40
+ version: 3,
41
+ up: async ({ space }) => {
42
+ const { objects } = space.db.query({ namespace: 'test' });
43
+ for (const object of objects) {
44
+ object.count *= 3;
45
+ }
46
+ },
47
+ down: async ({ space }) => {
48
+ const { objects } = space.db.query({ namespace: 'test' });
49
+ for (const object of objects) {
50
+ object.count /= 3;
51
+ }
52
+ },
53
+ },
54
+ ]);
55
+
56
+ describe('Migrations', () => {
57
+ let client: Client;
58
+ let space: Space;
59
+
60
+ beforeAll(async () => {
61
+ const testBuilder = new TestBuilder();
62
+ client = new Client({ services: testBuilder.createLocal() });
63
+ await client.initialize();
64
+ await client.halo.createIdentity();
65
+ });
66
+
67
+ afterAll(async () => {
68
+ await client.destroy();
69
+ });
70
+
71
+ beforeEach(async () => {
72
+ space = await client.spaces.create();
73
+ });
74
+
75
+ test('if no migrations have been run before, runs all migrations', async () => {
76
+ await Migrations.migrate(space);
77
+ const { objects } = space.db.query({ namespace: 'test' });
78
+ expect(objects).to.have.length(1);
79
+ expect(objects[0].count).to.equal(6);
80
+ expect(space.properties['test.version']).to.equal(3);
81
+ });
82
+
83
+ test('if some migrations have been run before, runs only the remaining migrations', async () => {
84
+ space.properties['test.version'] = 2;
85
+ space.db.add(new Expando({ namespace: 'test', count: 5 }));
86
+ await Migrations.migrate(space);
87
+ const { objects } = space.db.query({ namespace: 'test' });
88
+ expect(objects).to.have.length(1);
89
+ expect(objects[0].count).to.equal(15);
90
+ expect(space.properties['test.version']).to.equal(3);
91
+ });
92
+
93
+ test('if all migrations have been run before, does nothing', async () => {
94
+ space.properties['test.version'] = 3;
95
+ await Migrations.migrate(space);
96
+ const { objects } = space.db.query({ namespace: 'test' });
97
+ expect(objects).to.have.length(0);
98
+ });
99
+
100
+ test('if target version is specified, runs only the migrations up to that version', async () => {
101
+ await Migrations.migrate(space, 2);
102
+ const { objects } = space.db.query({ namespace: 'test' });
103
+ expect(objects).to.have.length(1);
104
+ expect(objects[0].count).to.equal(2);
105
+ expect(space.properties['test.version']).to.equal(2);
106
+ });
107
+
108
+ test('if target version is specified and is lower than current version, runs the down migrations', async () => {
109
+ const query = space.db.query({ namespace: 'test' });
110
+ await Migrations.migrate(space);
111
+ expect(query.objects).to.have.length(1);
112
+ expect(query.objects[0].count).to.equal(6);
113
+ expect(space.properties['test.version']).to.equal(3);
114
+ await Migrations.migrate(space, 1);
115
+ expect(query.objects).to.have.length(1);
116
+ expect(query.objects[0].count).to.equal(2);
117
+ expect(space.properties['test.version']).to.equal(1);
118
+ });
119
+ });
@@ -0,0 +1,63 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { type Space, SpaceState } from '@dxos/client/echo';
6
+ import { invariant } from '@dxos/invariant';
7
+ import type { MaybePromise } from '@dxos/util';
8
+
9
+ export type MigrationContext = {
10
+ version: string | number;
11
+ space: Space;
12
+ };
13
+
14
+ type Migration = {
15
+ version: string | number;
16
+ up: (context: MigrationContext) => MaybePromise<void>;
17
+ down: (context: MigrationContext) => MaybePromise<void>;
18
+ };
19
+
20
+ export class Migrations {
21
+ static namespace?: string;
22
+ static migrations: Migration[] = [];
23
+
24
+ static get versionProperty() {
25
+ return this.namespace && `${this.namespace}.version`;
26
+ }
27
+
28
+ static define(namespace: string, migrations: Migration[]) {
29
+ this.namespace = namespace;
30
+ this.migrations = migrations;
31
+ }
32
+
33
+ // TODO(wittjosiah): Multi-space migrations.
34
+ static async migrate(space: Space, targetVersion?: string | number) {
35
+ invariant(this.versionProperty, 'Migrations namespace not set');
36
+ invariant(space.state.get() === SpaceState.READY, 'Space not ready');
37
+ const currentVersion = space.properties[this.versionProperty];
38
+ const currentIndex = this.migrations.findIndex((m) => m.version === currentVersion) + 1;
39
+ const i = this.migrations.findIndex((m) => m.version === targetVersion);
40
+ const targetIndex = i === -1 ? this.migrations.length : i + 1;
41
+ if (currentIndex === targetIndex) {
42
+ return false;
43
+ }
44
+
45
+ if (targetIndex > currentIndex) {
46
+ const migrations = this.migrations.slice(currentIndex, targetIndex);
47
+ for (const migration of migrations) {
48
+ await migration.up({ version: migration.version, space });
49
+ space.properties[this.versionProperty] = migration.version;
50
+ }
51
+ } else {
52
+ const migrations = this.migrations.slice(targetIndex, currentIndex);
53
+ migrations.reverse();
54
+ for (const migration of migrations) {
55
+ await migration.down({ version: migration.version, space });
56
+ const index = this.migrations.indexOf(migration);
57
+ space.properties[this.versionProperty] = this.migrations[index - 1]?.version;
58
+ }
59
+ }
60
+
61
+ return true;
62
+ }
63
+ }