@wentools/schema-migrations 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +141 -0
- package/esm/create_migration.d.ts +31 -0
- package/esm/create_migration.d.ts.map +1 -0
- package/esm/create_migration.js +31 -0
- package/esm/create_migrations.d.ts +71 -0
- package/esm/create_migrations.d.ts.map +1 -0
- package/esm/create_migrations.js +58 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/err.d.ts +38 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/err.d.ts.map +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/err.js +38 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.d.ts +2 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.js +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.d.ts +11 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.d.ts.map +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.js +14 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.d.ts +2 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.js +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/mod.d.ts +156 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/mod.js +65 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.d.ts +2 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.js +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.d.ts +28 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.d.ts.map +1 -0
- package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.js +30 -0
- package/esm/migrate.d.ts +51 -0
- package/esm/migrate.d.ts.map +1 -0
- package/esm/migrate.js +65 -0
- package/esm/migrate_one_step.d.ts +35 -0
- package/esm/migrate_one_step.d.ts.map +1 -0
- package/esm/migrate_one_step.js +37 -0
- package/esm/migrate_range.d.ts +65 -0
- package/esm/migrate_range.d.ts.map +1 -0
- package/esm/migrate_range.js +51 -0
- package/esm/mod.d.ts +23 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +23 -0
- package/esm/package.json +3 -0
- package/esm/with_version.d.ts +22 -0
- package/esm/with_version.d.ts.map +1 -0
- package/esm/with_version.js +23 -0
- package/package.json +34 -0
- package/script/create_migration.d.ts +31 -0
- package/script/create_migration.d.ts.map +1 -0
- package/script/create_migration.js +34 -0
- package/script/create_migrations.d.ts +71 -0
- package/script/create_migrations.d.ts.map +1 -0
- package/script/create_migrations.js +61 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/err.d.ts +38 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/err.d.ts.map +1 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/err.js +41 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.d.ts +2 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.d.ts.map +1 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.js +5 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.d.ts +11 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.d.ts.map +1 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.js +17 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.d.ts +2 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.d.ts.map +1 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.js +5 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/mod.d.ts +156 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/mod.d.ts.map +1 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/mod.js +77 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.d.ts +2 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.d.ts.map +1 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.js +5 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.d.ts +28 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.d.ts.map +1 -0
- package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.js +33 -0
- package/script/migrate.d.ts +51 -0
- package/script/migrate.d.ts.map +1 -0
- package/script/migrate.js +68 -0
- package/script/migrate_one_step.d.ts +35 -0
- package/script/migrate_one_step.d.ts.map +1 -0
- package/script/migrate_one_step.js +40 -0
- package/script/migrate_range.d.ts +65 -0
- package/script/migrate_range.d.ts.map +1 -0
- package/script/migrate_range.js +54 -0
- package/script/mod.d.ts +23 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +32 -0
- package/script/package.json +3 -0
- package/script/with_version.d.ts +22 -0
- package/script/with_version.d.ts.map +1 -0
- package/script/with_version.js +26 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { ZodError } from 'zod';
|
|
2
|
+
import { type ErrorType, type Result } from './deps/jsr.io/@wentools/result/0.1.3/src/mod.js';
|
|
3
|
+
import { type MigrationConfig, type MigrateRangeError, type VersionedData } from './migrate_range.js';
|
|
4
|
+
/**
|
|
5
|
+
* Error when version number is out of valid range
|
|
6
|
+
*/
|
|
7
|
+
type VersionOutOfRangeError = ErrorType<'version_out_of_range', {
|
|
8
|
+
version: number;
|
|
9
|
+
maxVersion: number;
|
|
10
|
+
}>;
|
|
11
|
+
/**
|
|
12
|
+
* Error when data doesn't match its claimed version schema (no migration needed case)
|
|
13
|
+
*/
|
|
14
|
+
type InvalidInputError = ErrorType<'invalid_input', {
|
|
15
|
+
version: number;
|
|
16
|
+
cause: ZodError;
|
|
17
|
+
data: unknown;
|
|
18
|
+
}>;
|
|
19
|
+
/**
|
|
20
|
+
* Union of all errors that can occur during migration
|
|
21
|
+
*/
|
|
22
|
+
type MigrateError = VersionOutOfRangeError | InvalidInputError | MigrateRangeError;
|
|
23
|
+
/**
|
|
24
|
+
* Migrates versioned data to target version with automatic version management
|
|
25
|
+
*
|
|
26
|
+
* This is the primary API - most users should start here.
|
|
27
|
+
* Handles version validation, migration execution with entry-only validation strategy.
|
|
28
|
+
*
|
|
29
|
+
* @param config - Migration configuration
|
|
30
|
+
* @param versionedData - Data with optional version (defaults to 0 if not provided)
|
|
31
|
+
* @param targetVersion - Target version (defaults to currentVersion)
|
|
32
|
+
* @returns Result with versioned data at target version
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* // Migrate to current version (version defaults to 0)
|
|
37
|
+
* const result = migrate(eventMigrations, { data: oldEvent })
|
|
38
|
+
* if (result.isOk()) {
|
|
39
|
+
* const { version, data } = result.value // version: 3, data: EventV3
|
|
40
|
+
* }
|
|
41
|
+
*
|
|
42
|
+
* // Migrate from specific version
|
|
43
|
+
* const result = migrate(eventMigrations, { version: 1, data: event })
|
|
44
|
+
*
|
|
45
|
+
* // Migrate to specific target version
|
|
46
|
+
* const result = migrate(eventMigrations, { data: oldEvent }, 2)
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
declare const migrate: <TSchema>(config: MigrationConfig<TSchema>, versionedData: VersionedData<unknown>, targetVersion?: number) => Result<VersionedData<TSchema>, MigrateError>;
|
|
50
|
+
export { migrate, type MigrateError, type VersionOutOfRangeError };
|
|
51
|
+
//# sourceMappingURL=migrate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate.d.ts","sourceRoot":"","sources":["../src/migrate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,KAAK,CAAA;AACnC,OAAO,EAAmB,KAAK,SAAS,EAAE,KAAK,MAAM,EAAE,MAAM,iDAAiD,CAAA;AAC9G,OAAO,EAEN,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACtB,KAAK,aAAa,EAClB,MAAM,oBAAoB,CAAA;AAE3B;;GAEG;AACH,KAAK,sBAAsB,GAAG,SAAS,CACtC,sBAAsB,EACtB;IACC,OAAO,EAAE,MAAM,CAAA;IACf,UAAU,EAAE,MAAM,CAAA;CAClB,CACD,CAAA;AAED;;GAEG;AACH,KAAK,iBAAiB,GAAG,SAAS,CACjC,eAAe,EACf;IACC,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,QAAQ,CAAA;IACf,IAAI,EAAE,OAAO,CAAA;CACb,CACD,CAAA;AAED;;GAEG;AACH,KAAK,YAAY,GAAG,sBAAsB,GAAG,iBAAiB,GAAG,iBAAiB,CAAA;AAElF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,QAAA,MAAM,OAAO,GAAI,OAAO,EACvB,QAAQ,eAAe,CAAC,OAAO,CAAC,EAChC,eAAe,aAAa,CAAC,OAAO,CAAC,EACrC,gBAAgB,MAAM,KACpB,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,YAAY,CAwC7C,CAAA;AAED,OAAO,EAAE,OAAO,EAAE,KAAK,YAAY,EAAE,KAAK,sBAAsB,EAAE,CAAA"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.migrate = void 0;
|
|
4
|
+
const mod_js_1 = require("./deps/jsr.io/@wentools/result/0.1.3/src/mod.js");
|
|
5
|
+
const migrate_range_js_1 = require("./migrate_range.js");
|
|
6
|
+
/**
|
|
7
|
+
* Migrates versioned data to target version with automatic version management
|
|
8
|
+
*
|
|
9
|
+
* This is the primary API - most users should start here.
|
|
10
|
+
* Handles version validation, migration execution with entry-only validation strategy.
|
|
11
|
+
*
|
|
12
|
+
* @param config - Migration configuration
|
|
13
|
+
* @param versionedData - Data with optional version (defaults to 0 if not provided)
|
|
14
|
+
* @param targetVersion - Target version (defaults to currentVersion)
|
|
15
|
+
* @returns Result with versioned data at target version
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // Migrate to current version (version defaults to 0)
|
|
20
|
+
* const result = migrate(eventMigrations, { data: oldEvent })
|
|
21
|
+
* if (result.isOk()) {
|
|
22
|
+
* const { version, data } = result.value // version: 3, data: EventV3
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* // Migrate from specific version
|
|
26
|
+
* const result = migrate(eventMigrations, { version: 1, data: event })
|
|
27
|
+
*
|
|
28
|
+
* // Migrate to specific target version
|
|
29
|
+
* const result = migrate(eventMigrations, { data: oldEvent }, 2)
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
const migrate = (config, versionedData, targetVersion) => {
|
|
33
|
+
const version = versionedData.version ?? 0;
|
|
34
|
+
const { data } = versionedData;
|
|
35
|
+
const target = targetVersion ?? config.currentVersion;
|
|
36
|
+
// 1. VALIDATE VERSION RANGE
|
|
37
|
+
if (version < 0 || version > config.currentVersion) {
|
|
38
|
+
return (0, mod_js_1.err)('version_out_of_range', 'Version number is out of valid range', {
|
|
39
|
+
version,
|
|
40
|
+
maxVersion: config.currentVersion,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
if (target < 0 || target > config.currentVersion) {
|
|
44
|
+
return (0, mod_js_1.err)('version_out_of_range', 'Target version is out of valid range', {
|
|
45
|
+
version: target,
|
|
46
|
+
maxVersion: config.currentVersion,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
// 2. ALREADY AT TARGET VERSION - validate once and return
|
|
50
|
+
if (version === target) {
|
|
51
|
+
const validated = config.schemas[target].safeParse(data);
|
|
52
|
+
if (!validated.success) {
|
|
53
|
+
return (0, mod_js_1.err)('invalid_input', 'Data does not match claimed version schema', {
|
|
54
|
+
version: target,
|
|
55
|
+
cause: validated.error,
|
|
56
|
+
data,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return (0, mod_js_1.ok)({ version: target, data: validated.data });
|
|
60
|
+
}
|
|
61
|
+
// 3. RUN MIGRATIONS - migrateRange handles entry validation, then we trust types
|
|
62
|
+
const migrated = (0, migrate_range_js_1.migrateRange)(config, data, version, target);
|
|
63
|
+
if (migrated.isErr()) {
|
|
64
|
+
return (0, mod_js_1.rawErr)(migrated.error);
|
|
65
|
+
}
|
|
66
|
+
return (0, mod_js_1.ok)({ version: target, data: migrated.value });
|
|
67
|
+
};
|
|
68
|
+
exports.migrate = migrate;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type ErrorType, type Result } from './deps/jsr.io/@wentools/result/0.1.3/src/mod.js';
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown when a migration function throws an exception
|
|
4
|
+
*/
|
|
5
|
+
type MigrateOneStepError = ErrorType<'migration_threw', {
|
|
6
|
+
step: number;
|
|
7
|
+
cause: unknown;
|
|
8
|
+
data: unknown;
|
|
9
|
+
}>;
|
|
10
|
+
/**
|
|
11
|
+
* Executes a single migration step with error tracking
|
|
12
|
+
*
|
|
13
|
+
* Wraps migration function in try-catch to capture exceptions with step context.
|
|
14
|
+
* This is the lowest-level primitive - it just runs one migration function.
|
|
15
|
+
*
|
|
16
|
+
* @param migrate - Migration function to execute
|
|
17
|
+
* @param data - Input data (already validated by caller)
|
|
18
|
+
* @param stepNumber - Step number for error tracking
|
|
19
|
+
* @returns Result with migrated data or error with step information
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const result = migrateOneStep(
|
|
24
|
+
* (v0) => ({ ...v0, description: '' }),
|
|
25
|
+
* { name: 'Event' },
|
|
26
|
+
* 0
|
|
27
|
+
* )
|
|
28
|
+
* if (result.isErr()) {
|
|
29
|
+
* console.error(`Migration step ${result.error.step} failed`)
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
declare const migrateOneStep: <TInput, TOutput>(migrate: (input: TInput) => TOutput, data: TInput, stepNumber: number) => Result<TOutput, MigrateOneStepError>;
|
|
34
|
+
export { migrateOneStep, type MigrateOneStepError };
|
|
35
|
+
//# sourceMappingURL=migrate_one_step.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate_one_step.d.ts","sourceRoot":"","sources":["../src/migrate_one_step.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,KAAK,SAAS,EAAE,KAAK,MAAM,EAAE,MAAM,iDAAiD,CAAA;AAEtG;;GAEG;AACH,KAAK,mBAAmB,GAAG,SAAS,CACnC,iBAAiB,EACjB;IACC,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,OAAO,CAAA;IACd,IAAI,EAAE,OAAO,CAAA;CACb,CACD,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,QAAA,MAAM,cAAc,GAAI,MAAM,EAAE,OAAO,EACtC,SAAS,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,EACnC,MAAM,MAAM,EACZ,YAAY,MAAM,KAChB,MAAM,CAAC,OAAO,EAAE,mBAAmB,CAUrC,CAAA;AAED,OAAO,EAAE,cAAc,EAAE,KAAK,mBAAmB,EAAE,CAAA"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.migrateOneStep = void 0;
|
|
4
|
+
const mod_js_1 = require("./deps/jsr.io/@wentools/result/0.1.3/src/mod.js");
|
|
5
|
+
/**
|
|
6
|
+
* Executes a single migration step with error tracking
|
|
7
|
+
*
|
|
8
|
+
* Wraps migration function in try-catch to capture exceptions with step context.
|
|
9
|
+
* This is the lowest-level primitive - it just runs one migration function.
|
|
10
|
+
*
|
|
11
|
+
* @param migrate - Migration function to execute
|
|
12
|
+
* @param data - Input data (already validated by caller)
|
|
13
|
+
* @param stepNumber - Step number for error tracking
|
|
14
|
+
* @returns Result with migrated data or error with step information
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const result = migrateOneStep(
|
|
19
|
+
* (v0) => ({ ...v0, description: '' }),
|
|
20
|
+
* { name: 'Event' },
|
|
21
|
+
* 0
|
|
22
|
+
* )
|
|
23
|
+
* if (result.isErr()) {
|
|
24
|
+
* console.error(`Migration step ${result.error.step} failed`)
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
const migrateOneStep = (migrate, data, stepNumber) => {
|
|
29
|
+
try {
|
|
30
|
+
return (0, mod_js_1.ok)(migrate(data));
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
return (0, mod_js_1.err)('migration_threw', 'Migration function threw exception', {
|
|
34
|
+
step: stepNumber,
|
|
35
|
+
cause: error,
|
|
36
|
+
data,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
exports.migrateOneStep = migrateOneStep;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { ZodError, ZodSchema } from 'zod';
|
|
2
|
+
import { type ErrorType, type Result } from './deps/jsr.io/@wentools/result/0.1.3/src/mod.js';
|
|
3
|
+
import { type MigrateOneStepError } from './migrate_one_step.js';
|
|
4
|
+
/**
|
|
5
|
+
* Data with version tracking
|
|
6
|
+
* Version is optional - if not provided, defaults to 0 (first version)
|
|
7
|
+
*/
|
|
8
|
+
type VersionedData<TData> = {
|
|
9
|
+
version?: number;
|
|
10
|
+
data: TData;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Configuration object for schema migrations
|
|
14
|
+
*/
|
|
15
|
+
type MigrationConfig<TSchema = unknown> = {
|
|
16
|
+
/** Array of schemas, one for each version [v0Schema, v1Schema, v2Schema, ...] */
|
|
17
|
+
schemas: ZodSchema<unknown>[];
|
|
18
|
+
/** Array of migration functions [migrate0→1, migrate1→2, ...] */
|
|
19
|
+
migrations: ((data: unknown) => unknown)[];
|
|
20
|
+
/** Current version number (equals migrations.length) */
|
|
21
|
+
currentVersion: number;
|
|
22
|
+
/** Schema for current version (equals schemas[currentVersion]) */
|
|
23
|
+
currentSchema: ZodSchema<TSchema>;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Error when entry validation fails
|
|
27
|
+
*/
|
|
28
|
+
type InvalidInputError = ErrorType<'invalid_input', {
|
|
29
|
+
version: number;
|
|
30
|
+
cause: ZodError;
|
|
31
|
+
data: unknown;
|
|
32
|
+
}>;
|
|
33
|
+
/**
|
|
34
|
+
* Union of all errors that can occur during range migration
|
|
35
|
+
*/
|
|
36
|
+
type MigrateRangeError = InvalidInputError | MigrateOneStepError;
|
|
37
|
+
/**
|
|
38
|
+
* Migrates data through a range of versions with entry-only validation
|
|
39
|
+
*
|
|
40
|
+
* Strategy:
|
|
41
|
+
* 1. Validate ONCE at entry using schemas[fromVersion]
|
|
42
|
+
* 2. Run migrations sequentially, trusting TypeScript types
|
|
43
|
+
* 3. Wrap each step in try-catch for error tracking
|
|
44
|
+
*
|
|
45
|
+
* @param config - Migration configuration
|
|
46
|
+
* @param data - Data to migrate (unvalidated)
|
|
47
|
+
* @param fromVersion - Starting version (data's claimed version)
|
|
48
|
+
* @param toVersion - Target version
|
|
49
|
+
* @returns Result with migrated data or error
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* const result = migrateRange(eventMigrations, data, 0, 3)
|
|
54
|
+
* if (result.isErr()) {
|
|
55
|
+
* if (result.error.type === 'invalid_input') {
|
|
56
|
+
* console.error('Data does not match version schema')
|
|
57
|
+
* } else if (result.error.type === 'migration_threw') {
|
|
58
|
+
* console.error(`Migration step ${result.error.step} failed`)
|
|
59
|
+
* }
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
declare const migrateRange: (config: MigrationConfig<unknown>, data: unknown, fromVersion: number, toVersion: number) => Result<unknown, MigrateRangeError>;
|
|
64
|
+
export { migrateRange, type MigrationConfig, type MigrateRangeError, type InvalidInputError, type VersionedData, };
|
|
65
|
+
//# sourceMappingURL=migrate_range.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate_range.d.ts","sourceRoot":"","sources":["../src/migrate_range.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,KAAK,CAAA;AAC9C,OAAO,EAAW,KAAK,SAAS,EAAE,KAAK,MAAM,EAAE,MAAM,iDAAiD,CAAA;AACtG,OAAO,EAAkB,KAAK,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAEhF;;;GAGG;AACH,KAAK,aAAa,CAAC,KAAK,IAAI;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,IAAI,EAAE,KAAK,CAAA;CACX,CAAA;AAED;;GAEG;AACH,KAAK,eAAe,CAAC,OAAO,GAAG,OAAO,IAAI;IACzC,iFAAiF;IACjF,OAAO,EAAE,SAAS,CAAC,OAAO,CAAC,EAAE,CAAA;IAC7B,iEAAiE;IACjE,UAAU,EAAE,CAAC,CAAC,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC,EAAE,CAAA;IAC1C,wDAAwD;IACxD,cAAc,EAAE,MAAM,CAAA;IACtB,kEAAkE;IAClE,aAAa,EAAE,SAAS,CAAC,OAAO,CAAC,CAAA;CACjC,CAAA;AAED;;GAEG;AACH,KAAK,iBAAiB,GAAG,SAAS,CACjC,eAAe,EACf;IACC,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,QAAQ,CAAA;IACf,IAAI,EAAE,OAAO,CAAA;CACb,CACD,CAAA;AAED;;GAEG;AACH,KAAK,iBAAiB,GAAG,iBAAiB,GAAG,mBAAmB,CAAA;AAEhE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,QAAA,MAAM,YAAY,GACjB,QAAQ,eAAe,CAAC,OAAO,CAAC,EAChC,MAAM,OAAO,EACb,aAAa,MAAM,EACnB,WAAW,MAAM,KACf,MAAM,CAAC,OAAO,EAAE,iBAAiB,CAuBnC,CAAA;AAED,OAAO,EACN,YAAY,EACZ,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACtB,KAAK,iBAAiB,EACtB,KAAK,aAAa,GAClB,CAAA"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.migrateRange = void 0;
|
|
4
|
+
const mod_js_1 = require("./deps/jsr.io/@wentools/result/0.1.3/src/mod.js");
|
|
5
|
+
const migrate_one_step_js_1 = require("./migrate_one_step.js");
|
|
6
|
+
/**
|
|
7
|
+
* Migrates data through a range of versions with entry-only validation
|
|
8
|
+
*
|
|
9
|
+
* Strategy:
|
|
10
|
+
* 1. Validate ONCE at entry using schemas[fromVersion]
|
|
11
|
+
* 2. Run migrations sequentially, trusting TypeScript types
|
|
12
|
+
* 3. Wrap each step in try-catch for error tracking
|
|
13
|
+
*
|
|
14
|
+
* @param config - Migration configuration
|
|
15
|
+
* @param data - Data to migrate (unvalidated)
|
|
16
|
+
* @param fromVersion - Starting version (data's claimed version)
|
|
17
|
+
* @param toVersion - Target version
|
|
18
|
+
* @returns Result with migrated data or error
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const result = migrateRange(eventMigrations, data, 0, 3)
|
|
23
|
+
* if (result.isErr()) {
|
|
24
|
+
* if (result.error.type === 'invalid_input') {
|
|
25
|
+
* console.error('Data does not match version schema')
|
|
26
|
+
* } else if (result.error.type === 'migration_threw') {
|
|
27
|
+
* console.error(`Migration step ${result.error.step} failed`)
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
const migrateRange = (config, data, fromVersion, toVersion) => {
|
|
33
|
+
// 1. ENTRY VALIDATION - validate data matches claimed version schema
|
|
34
|
+
const entrySchema = config.schemas[fromVersion];
|
|
35
|
+
const validated = entrySchema.safeParse(data);
|
|
36
|
+
if (!validated.success) {
|
|
37
|
+
return (0, mod_js_1.err)('invalid_input', 'Entry validation failed', {
|
|
38
|
+
version: fromVersion,
|
|
39
|
+
cause: validated.error,
|
|
40
|
+
data,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
// 2. RUN MIGRATIONS - trust types, wrap in try-catch for error tracking
|
|
44
|
+
let current = validated.data;
|
|
45
|
+
for (let i = fromVersion; i < toVersion; i++) {
|
|
46
|
+
const result = (0, migrate_one_step_js_1.migrateOneStep)(config.migrations[i], current, i);
|
|
47
|
+
if (result.isErr()) {
|
|
48
|
+
return result; // Pass through migration_threw error with step number
|
|
49
|
+
}
|
|
50
|
+
current = result.value;
|
|
51
|
+
}
|
|
52
|
+
return (0, mod_js_1.ok)(current);
|
|
53
|
+
};
|
|
54
|
+
exports.migrateRange = migrateRange;
|
package/script/mod.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema Migration Library
|
|
3
|
+
*
|
|
4
|
+
* Provides type-safe schema migrations with entry-only validation strategy.
|
|
5
|
+
*
|
|
6
|
+
* Primary API (most users start here):
|
|
7
|
+
* - migrate() - Migrate versioned data to target version
|
|
8
|
+
* - withVersion() - Wrap data with current version
|
|
9
|
+
* - createMigrations() - Factory for creating migration configuration
|
|
10
|
+
* - createMigration() - Helper for creating typed migration steps
|
|
11
|
+
*
|
|
12
|
+
* Core primitives (testing/advanced usage):
|
|
13
|
+
* - migrateOneStep() - Execute single migration with error tracking
|
|
14
|
+
* - migrateRange() - Migrate through version range with entry validation
|
|
15
|
+
*/
|
|
16
|
+
export { migrate, type MigrateError, type VersionOutOfRangeError } from './migrate.js';
|
|
17
|
+
export { withVersion } from './with_version.js';
|
|
18
|
+
export { createMigrations } from './create_migrations.js';
|
|
19
|
+
export { createMigration } from './create_migration.js';
|
|
20
|
+
export type { MigrationConfig, VersionedData } from './migrate_range.js';
|
|
21
|
+
export { migrateOneStep, type MigrateOneStepError } from './migrate_one_step.js';
|
|
22
|
+
export { migrateRange, type MigrateRangeError, type InvalidInputError } from './migrate_range.js';
|
|
23
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAE,OAAO,EAAE,KAAK,YAAY,EAAE,KAAK,sBAAsB,EAAE,MAAM,cAAc,CAAA;AACtF,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAGvD,YAAY,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAGxE,OAAO,EAAE,cAAc,EAAE,KAAK,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAChF,OAAO,EAAE,YAAY,EAAE,KAAK,iBAAiB,EAAE,KAAK,iBAAiB,EAAE,MAAM,oBAAoB,CAAA"}
|
package/script/mod.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Schema Migration Library
|
|
4
|
+
*
|
|
5
|
+
* Provides type-safe schema migrations with entry-only validation strategy.
|
|
6
|
+
*
|
|
7
|
+
* Primary API (most users start here):
|
|
8
|
+
* - migrate() - Migrate versioned data to target version
|
|
9
|
+
* - withVersion() - Wrap data with current version
|
|
10
|
+
* - createMigrations() - Factory for creating migration configuration
|
|
11
|
+
* - createMigration() - Helper for creating typed migration steps
|
|
12
|
+
*
|
|
13
|
+
* Core primitives (testing/advanced usage):
|
|
14
|
+
* - migrateOneStep() - Execute single migration with error tracking
|
|
15
|
+
* - migrateRange() - Migrate through version range with entry validation
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.migrateRange = exports.migrateOneStep = exports.createMigration = exports.createMigrations = exports.withVersion = exports.migrate = void 0;
|
|
19
|
+
// Primary API
|
|
20
|
+
var migrate_js_1 = require("./migrate.js");
|
|
21
|
+
Object.defineProperty(exports, "migrate", { enumerable: true, get: function () { return migrate_js_1.migrate; } });
|
|
22
|
+
var with_version_js_1 = require("./with_version.js");
|
|
23
|
+
Object.defineProperty(exports, "withVersion", { enumerable: true, get: function () { return with_version_js_1.withVersion; } });
|
|
24
|
+
var create_migrations_js_1 = require("./create_migrations.js");
|
|
25
|
+
Object.defineProperty(exports, "createMigrations", { enumerable: true, get: function () { return create_migrations_js_1.createMigrations; } });
|
|
26
|
+
var create_migration_js_1 = require("./create_migration.js");
|
|
27
|
+
Object.defineProperty(exports, "createMigration", { enumerable: true, get: function () { return create_migration_js_1.createMigration; } });
|
|
28
|
+
// Core primitives (for testing and advanced usage)
|
|
29
|
+
var migrate_one_step_js_1 = require("./migrate_one_step.js");
|
|
30
|
+
Object.defineProperty(exports, "migrateOneStep", { enumerable: true, get: function () { return migrate_one_step_js_1.migrateOneStep; } });
|
|
31
|
+
var migrate_range_js_1 = require("./migrate_range.js");
|
|
32
|
+
Object.defineProperty(exports, "migrateRange", { enumerable: true, get: function () { return migrate_range_js_1.migrateRange; } });
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { MigrationConfig, VersionedData } from './migrate_range.js';
|
|
2
|
+
/**
|
|
3
|
+
* Wraps data with current version from migration config
|
|
4
|
+
*
|
|
5
|
+
* Use this when creating new data that should be at the current version.
|
|
6
|
+
*
|
|
7
|
+
* @param config - Migration configuration
|
|
8
|
+
* @param data - Data to wrap with version
|
|
9
|
+
* @returns Versioned data at current version
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const newEvent = { name: 'Concert', description: '', capacity: 100 }
|
|
14
|
+
* const versioned = withVersion(eventMigrations, newEvent)
|
|
15
|
+
* // { version: 3, data: newEvent }
|
|
16
|
+
*
|
|
17
|
+
* localStorage.setItem('event', JSON.stringify(versioned))
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
declare const withVersion: <TSchema>(config: MigrationConfig<TSchema>, data: TSchema) => VersionedData<TSchema>;
|
|
21
|
+
export { withVersion };
|
|
22
|
+
//# sourceMappingURL=with_version.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with_version.d.ts","sourceRoot":"","sources":["../src/with_version.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAExE;;;;;;;;;;;;;;;;;GAiBG;AACH,QAAA,MAAM,WAAW,GAAI,OAAO,EAC3B,QAAQ,eAAe,CAAC,OAAO,CAAC,EAChC,MAAM,OAAO,KACX,aAAa,CAAC,OAAO,CAGtB,CAAA;AAEF,OAAO,EAAE,WAAW,EAAE,CAAA"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withVersion = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Wraps data with current version from migration config
|
|
6
|
+
*
|
|
7
|
+
* Use this when creating new data that should be at the current version.
|
|
8
|
+
*
|
|
9
|
+
* @param config - Migration configuration
|
|
10
|
+
* @param data - Data to wrap with version
|
|
11
|
+
* @returns Versioned data at current version
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const newEvent = { name: 'Concert', description: '', capacity: 100 }
|
|
16
|
+
* const versioned = withVersion(eventMigrations, newEvent)
|
|
17
|
+
* // { version: 3, data: newEvent }
|
|
18
|
+
*
|
|
19
|
+
* localStorage.setItem('event', JSON.stringify(versioned))
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
const withVersion = (config, data) => ({
|
|
23
|
+
version: config.currentVersion,
|
|
24
|
+
data,
|
|
25
|
+
});
|
|
26
|
+
exports.withVersion = withVersion;
|