@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.
Files changed (89) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +141 -0
  3. package/esm/create_migration.d.ts +31 -0
  4. package/esm/create_migration.d.ts.map +1 -0
  5. package/esm/create_migration.js +31 -0
  6. package/esm/create_migrations.d.ts +71 -0
  7. package/esm/create_migrations.d.ts.map +1 -0
  8. package/esm/create_migrations.js +58 -0
  9. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/err.d.ts +38 -0
  10. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/err.d.ts.map +1 -0
  11. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/err.js +38 -0
  12. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.d.ts +2 -0
  13. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.d.ts.map +1 -0
  14. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.js +1 -0
  15. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.d.ts +11 -0
  16. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.d.ts.map +1 -0
  17. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.js +14 -0
  18. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.d.ts +2 -0
  19. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.d.ts.map +1 -0
  20. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.js +1 -0
  21. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/mod.d.ts +156 -0
  22. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/mod.d.ts.map +1 -0
  23. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/mod.js +65 -0
  24. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.d.ts +2 -0
  25. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.d.ts.map +1 -0
  26. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.js +1 -0
  27. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.d.ts +28 -0
  28. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.d.ts.map +1 -0
  29. package/esm/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.js +30 -0
  30. package/esm/migrate.d.ts +51 -0
  31. package/esm/migrate.d.ts.map +1 -0
  32. package/esm/migrate.js +65 -0
  33. package/esm/migrate_one_step.d.ts +35 -0
  34. package/esm/migrate_one_step.d.ts.map +1 -0
  35. package/esm/migrate_one_step.js +37 -0
  36. package/esm/migrate_range.d.ts +65 -0
  37. package/esm/migrate_range.d.ts.map +1 -0
  38. package/esm/migrate_range.js +51 -0
  39. package/esm/mod.d.ts +23 -0
  40. package/esm/mod.d.ts.map +1 -0
  41. package/esm/mod.js +23 -0
  42. package/esm/package.json +3 -0
  43. package/esm/with_version.d.ts +22 -0
  44. package/esm/with_version.d.ts.map +1 -0
  45. package/esm/with_version.js +23 -0
  46. package/package.json +34 -0
  47. package/script/create_migration.d.ts +31 -0
  48. package/script/create_migration.d.ts.map +1 -0
  49. package/script/create_migration.js +34 -0
  50. package/script/create_migrations.d.ts +71 -0
  51. package/script/create_migrations.d.ts.map +1 -0
  52. package/script/create_migrations.js +61 -0
  53. package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/err.d.ts +38 -0
  54. package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/err.d.ts.map +1 -0
  55. package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/err.js +41 -0
  56. package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.d.ts +2 -0
  57. package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.d.ts.map +1 -0
  58. package/script/deps/jsr.io/@wentools/result/0.1.3/src/err/mod.js +5 -0
  59. package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.d.ts +11 -0
  60. package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.d.ts.map +1 -0
  61. package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.js +17 -0
  62. package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.d.ts +2 -0
  63. package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.d.ts.map +1 -0
  64. package/script/deps/jsr.io/@wentools/result/0.1.3/src/is_result/mod.js +5 -0
  65. package/script/deps/jsr.io/@wentools/result/0.1.3/src/mod.d.ts +156 -0
  66. package/script/deps/jsr.io/@wentools/result/0.1.3/src/mod.d.ts.map +1 -0
  67. package/script/deps/jsr.io/@wentools/result/0.1.3/src/mod.js +77 -0
  68. package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.d.ts +2 -0
  69. package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.d.ts.map +1 -0
  70. package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.js +5 -0
  71. package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.d.ts +28 -0
  72. package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.d.ts.map +1 -0
  73. package/script/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.js +33 -0
  74. package/script/migrate.d.ts +51 -0
  75. package/script/migrate.d.ts.map +1 -0
  76. package/script/migrate.js +68 -0
  77. package/script/migrate_one_step.d.ts +35 -0
  78. package/script/migrate_one_step.d.ts.map +1 -0
  79. package/script/migrate_one_step.js +40 -0
  80. package/script/migrate_range.d.ts +65 -0
  81. package/script/migrate_range.d.ts.map +1 -0
  82. package/script/migrate_range.js +54 -0
  83. package/script/mod.d.ts +23 -0
  84. package/script/mod.d.ts.map +1 -0
  85. package/script/mod.js +32 -0
  86. package/script/package.json +3 -0
  87. package/script/with_version.d.ts +22 -0
  88. package/script/with_version.d.ts.map +1 -0
  89. package/script/with_version.js +26 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../../../../../../src/deps/jsr.io/@wentools/result/0.1.3/src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,GAAG,IAAI,IAAI,EACX,EAAE,EACF,KAAK,MAAM,EACX,KAAK,GAAG,EACR,WAAW,EAEX,OAAO,IAAI,iBAAiB,EAC5B,MAAM,YAAY,CAAA;AAEnB;;;;;;;;;;GAUG;AACH,QAAA,MAAM,OAAO,GACZ,KAAK,CAAC,KAAK,SAAS,MAAM,EAC1B,WAAW,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,EAEnE,MAAM,KAAK,EACX,SAAS,MAAM,EACf,aAAa,WAAW,KACtB;IAAE,IAAI,EAAE,KAAK,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,WAKe,CAAA;AAErD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,QAAA,MAAM,QAAQ,GACb,KAAK,CAAC,KAAK,SAAS,MAAM,EAC1B,WAAW,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,EAEnE,MAAM,KAAK,EACX,SAAS,MAAM,EACf,aAAa,WAAW,KACtB,WAAW,CAAC,KAAK,EAAE;IAAE,IAAI,EAAE,KAAK,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,WAAW,CAKf,CAAA;AAErD;;;GAGG;AACH,QAAA,MAAM,OAAO,EAAE,OAAO,iBAAqC,CAAA;AAE3D;;;;;;;;;;;;;;;;;GAiBG;AACH,KAAK,SAAS,CACb,KAAK,SAAS,MAAM,EACpB,WAAW,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC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AAO,IAAW,CAAA;AAEhC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,CAAA;AACpE,YAAY,EAAE,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,eAAe,EAAE,CAAA;AACpG,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAA;AAClC,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAA"}
@@ -0,0 +1,65 @@
1
+ import { err as _Err, ok, ResultAsync, errAsync as neverthrowErrAsync, okAsync as neverthrowOkAsync, } from 'neverthrow';
2
+ /**
3
+ * Create an error object with literal type preservation
4
+ *
5
+ * @example
6
+ * const error = makeErr('not_found', 'User not found')
7
+ * // ✅ Type: { type: 'not_found', message: string }
8
+ *
9
+ * @example
10
+ * const error = makeErr('not_found', 'User not found', { userId: '123' })
11
+ * // ✅ Type: { type: 'not_found', message: string, userId: string }
12
+ */
13
+ const makeErr = (type, message, additional) => ({
14
+ ...additional,
15
+ type,
16
+ message,
17
+ });
18
+ /**
19
+ * Create an async Err ResultAsync with automatic literal type preservation
20
+ *
21
+ * Async version of err() - same signature, returns ResultAsync instead of Result.
22
+ * Uses TypeScript 5.0+ const type parameters to preserve literal types automatically.
23
+ *
24
+ * @example
25
+ * // ✅ Simple error
26
+ * return errAsync('not_found', 'User not found')
27
+ * // Type: ResultAsync<never, { type: 'not_found', message: string }>
28
+ *
29
+ * @example
30
+ * // ✅ Error with additional properties
31
+ * return errAsync('not_found', 'User not found', { userId: '123' })
32
+ * // Type: ResultAsync<never, { type: 'not_found', message: string, userId: string }>
33
+ *
34
+ * @example
35
+ * // ✅ Error with complex additional data
36
+ * return errAsync('validation_failed', 'Invalid input', {
37
+ * field: 'email',
38
+ * errors: ['Invalid format']
39
+ * })
40
+ * // Type: ResultAsync<never, { type: 'validation_failed', message: string, field: string, errors: string[] }>
41
+ */
42
+ const errAsync = (type, message, additional) => neverthrowErrAsync({
43
+ ...additional,
44
+ type,
45
+ message,
46
+ });
47
+ /**
48
+ * Re-export okAsync from neverthrow for consistency
49
+ * Creates a successful ResultAsync
50
+ */
51
+ const okAsync = neverthrowOkAsync;
52
+ /**
53
+ * Raw error function from neverthrow - use for lifting database errors directly
54
+ *
55
+ * @example
56
+ * const seedResult = await songSeeds.get.one.byId(songSeedId)
57
+ * if (seedResult.isErr()) {
58
+ * return rawErr(seedResult.error)
59
+ * }
60
+ */
61
+ const rawErr = _Err;
62
+ export { makeErr, errAsync, ok, okAsync, rawErr, _Err, ResultAsync };
63
+ export { err } from './err/mod.js';
64
+ export { isResult } from './is_result/mod.js';
65
+ export { propagateErr } from './propagate_err/mod.js';
@@ -0,0 +1,2 @@
1
+ export { propagateErr } from './propagate_err.js';
2
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../../../../../../../src/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA"}
@@ -0,0 +1 @@
1
+ export { propagateErr } from './propagate_err.js';
@@ -0,0 +1,28 @@
1
+ import type { Result } from 'neverthrow';
2
+ /**
3
+ * Propagates an error Result to a different success type context.
4
+ *
5
+ * This is safe because error Results never contain the success value - only the error.
6
+ * TypeScript's structural typing prevents direct assignment of Result<T, E> to Result<U, E>
7
+ * even though the error type is identical and we're in the error branch.
8
+ *
9
+ * Common use case: Early return from a function that creates multiple items. If creating
10
+ * item 2 fails with Result<Item, DatabaseError>, we need to return Result<Item[], DatabaseError>.
11
+ *
12
+ * @example
13
+ * const result = await createUser(db, data) // Result<User, DatabaseError>
14
+ * if (result.isErr()) {
15
+ * // Need to return Result<Profile, DatabaseError> but have Result<User, DatabaseError>
16
+ * return propagateErr(result)
17
+ * }
18
+ *
19
+ * @example
20
+ * // In bulk creation, propagate single-item error to array context
21
+ * const itemResult = await createItem(data) // Result<Item, Error>
22
+ * if (itemResult.isErr()) {
23
+ * return propagateErr<Item[], Error>(itemResult) // Result<Item[], Error>
24
+ * }
25
+ */
26
+ declare const propagateErr: <TNew, E>(errorResult: Result<unknown, E>) => Result<TNew, E>;
27
+ export { propagateErr };
28
+ //# sourceMappingURL=propagate_err.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"propagate_err.d.ts","sourceRoot":"","sources":["../../../../../../../../src/deps/jsr.io/@wentools/result/0.1.3/src/propagate_err/propagate_err.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAExC;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,QAAA,MAAM,YAAY,GAAI,IAAI,EAAE,CAAC,EAAE,aAAa,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,KAAG,MAAM,CAAC,IAAI,EAAE,CAAC,CAI9E,CAAA;AAED,OAAO,EAAE,YAAY,EAAE,CAAA"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Propagates an error Result to a different success type context.
3
+ *
4
+ * This is safe because error Results never contain the success value - only the error.
5
+ * TypeScript's structural typing prevents direct assignment of Result<T, E> to Result<U, E>
6
+ * even though the error type is identical and we're in the error branch.
7
+ *
8
+ * Common use case: Early return from a function that creates multiple items. If creating
9
+ * item 2 fails with Result<Item, DatabaseError>, we need to return Result<Item[], DatabaseError>.
10
+ *
11
+ * @example
12
+ * const result = await createUser(db, data) // Result<User, DatabaseError>
13
+ * if (result.isErr()) {
14
+ * // Need to return Result<Profile, DatabaseError> but have Result<User, DatabaseError>
15
+ * return propagateErr(result)
16
+ * }
17
+ *
18
+ * @example
19
+ * // In bulk creation, propagate single-item error to array context
20
+ * const itemResult = await createItem(data) // Result<Item, Error>
21
+ * if (itemResult.isErr()) {
22
+ * return propagateErr<Item[], Error>(itemResult) // Result<Item[], Error>
23
+ * }
24
+ */
25
+ const propagateErr = (errorResult) => {
26
+ // Type assertion is safe: we're only propagating errors, and error Results
27
+ // don't contain the success value. The error type E is preserved exactly.
28
+ return errorResult;
29
+ };
30
+ export { propagateErr };
@@ -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"}
package/esm/migrate.js ADDED
@@ -0,0 +1,65 @@
1
+ import { err, ok, rawErr } from './deps/jsr.io/@wentools/result/0.1.3/src/mod.js';
2
+ import { migrateRange, } from './migrate_range.js';
3
+ /**
4
+ * Migrates versioned data to target version with automatic version management
5
+ *
6
+ * This is the primary API - most users should start here.
7
+ * Handles version validation, migration execution with entry-only validation strategy.
8
+ *
9
+ * @param config - Migration configuration
10
+ * @param versionedData - Data with optional version (defaults to 0 if not provided)
11
+ * @param targetVersion - Target version (defaults to currentVersion)
12
+ * @returns Result with versioned data at target version
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * // Migrate to current version (version defaults to 0)
17
+ * const result = migrate(eventMigrations, { data: oldEvent })
18
+ * if (result.isOk()) {
19
+ * const { version, data } = result.value // version: 3, data: EventV3
20
+ * }
21
+ *
22
+ * // Migrate from specific version
23
+ * const result = migrate(eventMigrations, { version: 1, data: event })
24
+ *
25
+ * // Migrate to specific target version
26
+ * const result = migrate(eventMigrations, { data: oldEvent }, 2)
27
+ * ```
28
+ */
29
+ const migrate = (config, versionedData, targetVersion) => {
30
+ const version = versionedData.version ?? 0;
31
+ const { data } = versionedData;
32
+ const target = targetVersion ?? config.currentVersion;
33
+ // 1. VALIDATE VERSION RANGE
34
+ if (version < 0 || version > config.currentVersion) {
35
+ return err('version_out_of_range', 'Version number is out of valid range', {
36
+ version,
37
+ maxVersion: config.currentVersion,
38
+ });
39
+ }
40
+ if (target < 0 || target > config.currentVersion) {
41
+ return err('version_out_of_range', 'Target version is out of valid range', {
42
+ version: target,
43
+ maxVersion: config.currentVersion,
44
+ });
45
+ }
46
+ // 2. ALREADY AT TARGET VERSION - validate once and return
47
+ if (version === target) {
48
+ const validated = config.schemas[target].safeParse(data);
49
+ if (!validated.success) {
50
+ return err('invalid_input', 'Data does not match claimed version schema', {
51
+ version: target,
52
+ cause: validated.error,
53
+ data,
54
+ });
55
+ }
56
+ return ok({ version: target, data: validated.data });
57
+ }
58
+ // 3. RUN MIGRATIONS - migrateRange handles entry validation, then we trust types
59
+ const migrated = migrateRange(config, data, version, target);
60
+ if (migrated.isErr()) {
61
+ return rawErr(migrated.error);
62
+ }
63
+ return ok({ version: target, data: migrated.value });
64
+ };
65
+ export { 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,37 @@
1
+ import { err, ok } from './deps/jsr.io/@wentools/result/0.1.3/src/mod.js';
2
+ /**
3
+ * Executes a single migration step with error tracking
4
+ *
5
+ * Wraps migration function in try-catch to capture exceptions with step context.
6
+ * This is the lowest-level primitive - it just runs one migration function.
7
+ *
8
+ * @param migrate - Migration function to execute
9
+ * @param data - Input data (already validated by caller)
10
+ * @param stepNumber - Step number for error tracking
11
+ * @returns Result with migrated data or error with step information
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const result = migrateOneStep(
16
+ * (v0) => ({ ...v0, description: '' }),
17
+ * { name: 'Event' },
18
+ * 0
19
+ * )
20
+ * if (result.isErr()) {
21
+ * console.error(`Migration step ${result.error.step} failed`)
22
+ * }
23
+ * ```
24
+ */
25
+ const migrateOneStep = (migrate, data, stepNumber) => {
26
+ try {
27
+ return ok(migrate(data));
28
+ }
29
+ catch (error) {
30
+ return err('migration_threw', 'Migration function threw exception', {
31
+ step: stepNumber,
32
+ cause: error,
33
+ data,
34
+ });
35
+ }
36
+ };
37
+ export { 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,QA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'./deps/jsr.io/@wentools/result/0.1.3/src/mod.js';
2
+ import { migrateOneStep } from './migrate_one_step.js';
3
+ /**
4
+ * Migrates data through a range of versions with entry-only validation
5
+ *
6
+ * Strategy:
7
+ * 1. Validate ONCE at entry using schemas[fromVersion]
8
+ * 2. Run migrations sequentially, trusting TypeScript types
9
+ * 3. Wrap each step in try-catch for error tracking
10
+ *
11
+ * @param config - Migration configuration
12
+ * @param data - Data to migrate (unvalidated)
13
+ * @param fromVersion - Starting version (data's claimed version)
14
+ * @param toVersion - Target version
15
+ * @returns Result with migrated data or error
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * const result = migrateRange(eventMigrations, data, 0, 3)
20
+ * if (result.isErr()) {
21
+ * if (result.error.type === 'invalid_input') {
22
+ * console.error('Data does not match version schema')
23
+ * } else if (result.error.type === 'migration_threw') {
24
+ * console.error(`Migration step ${result.error.step} failed`)
25
+ * }
26
+ * }
27
+ * ```
28
+ */
29
+ const migrateRange = (config, data, fromVersion, toVersion) => {
30
+ // 1. ENTRY VALIDATION - validate data matches claimed version schema
31
+ const entrySchema = config.schemas[fromVersion];
32
+ const validated = entrySchema.safeParse(data);
33
+ if (!validated.success) {
34
+ return err('invalid_input', 'Entry validation failed', {
35
+ version: fromVersion,
36
+ cause: validated.error,
37
+ data,
38
+ });
39
+ }
40
+ // 2. RUN MIGRATIONS - trust types, wrap in try-catch for error tracking
41
+ let current = validated.data;
42
+ for (let i = fromVersion; i < toVersion; i++) {
43
+ const result = migrateOneStep(config.migrations[i], current, i);
44
+ if (result.isErr()) {
45
+ return result; // Pass through migration_threw error with step number
46
+ }
47
+ current = result.value;
48
+ }
49
+ return ok(current);
50
+ };
51
+ export { migrateRange, };
package/esm/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/esm/mod.js 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
+ // Primary API
17
+ export { migrate } from './migrate.js';
18
+ export { withVersion } from './with_version.js';
19
+ export { createMigrations } from './create_migrations.js';
20
+ export { createMigration } from './create_migration.js';
21
+ // Core primitives (for testing and advanced usage)
22
+ export { migrateOneStep } from './migrate_one_step.js';
23
+ export { migrateRange } from './migrate_range.js';
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -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,23 @@
1
+ /**
2
+ * Wraps data with current version from migration config
3
+ *
4
+ * Use this when creating new data that should be at the current version.
5
+ *
6
+ * @param config - Migration configuration
7
+ * @param data - Data to wrap with version
8
+ * @returns Versioned data at current version
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const newEvent = { name: 'Concert', description: '', capacity: 100 }
13
+ * const versioned = withVersion(eventMigrations, newEvent)
14
+ * // { version: 3, data: newEvent }
15
+ *
16
+ * localStorage.setItem('event', JSON.stringify(versioned))
17
+ * ```
18
+ */
19
+ const withVersion = (config, data) => ({
20
+ version: config.currentVersion,
21
+ data,
22
+ });
23
+ export { withVersion };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@wentools/schema-migrations",
3
+ "version": "0.1.0",
4
+ "description": "Type-safe schema migrations with entry-only validation strategy",
5
+ "keywords": [
6
+ "schema",
7
+ "migration",
8
+ "versioning",
9
+ "zod",
10
+ "type-safe"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://gitlab.com/wentools/schema-migrations"
15
+ },
16
+ "license": "MIT",
17
+ "main": "./script/mod.js",
18
+ "module": "./esm/mod.js",
19
+ "exports": {
20
+ ".": {
21
+ "import": "./esm/mod.js",
22
+ "require": "./script/mod.js"
23
+ }
24
+ },
25
+ "scripts": {},
26
+ "dependencies": {
27
+ "neverthrow": "^8.2.0",
28
+ "zod": "^4.3.0"
29
+ },
30
+ "peerDependencies": {
31
+ "zod": "^4.3.0"
32
+ },
33
+ "_generatedBy": "dnt@dev"
34
+ }
@@ -0,0 +1,31 @@
1
+ import type { ZodTypeAny } from 'zod';
2
+ /**
3
+ * Creates a typed migration step with schema inference
4
+ *
5
+ * Helper function that pairs a schema with its migration function,
6
+ * providing type inference for the migration function input.
7
+ *
8
+ * **Type Safety:** The returned tuple is typed as readonly to ensure
9
+ * compile-time validation of migration chain ordering in createMigrations.
10
+ *
11
+ * @param fromSchema - Schema for input version (used for type inference)
12
+ * @param toSchema - Schema for output version
13
+ * @param migrate - Migration function from TFrom to TTo
14
+ * @returns Readonly tuple of [schema, migrateFn] ready for createMigrations
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const v0Schema = z.object({ name: z.string() })
19
+ * const v1Schema = z.object({ name: z.string(), description: z.string() })
20
+ *
21
+ * const addDescription = createMigration(v0Schema, v1Schema, (v0) => ({
22
+ * ...v0,
23
+ * description: '', // v0 is typed as { name: string }
24
+ * }))
25
+ *
26
+ * const migrations = createMigrations(v0Schema, [addDescription])
27
+ * ```
28
+ */
29
+ declare const createMigration: <TFromSchema extends ZodTypeAny, TToSchema extends ZodTypeAny>(fromSchema: TFromSchema, toSchema: TToSchema, migrate: (from: TFromSchema["_output"]) => TToSchema["_output"]) => readonly [TToSchema, (from: TFromSchema["_output"]) => TToSchema["_output"]];
30
+ export { createMigration };
31
+ //# sourceMappingURL=create_migration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create_migration.d.ts","sourceRoot":"","sources":["../src/create_migration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,KAAK,CAAA;AAErC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,QAAA,MAAM,eAAe,GAAI,WAAW,SAAS,UAAU,EAAE,SAAS,SAAS,UAAU,EACpF,YAAY,WAAW,EACvB,UAAU,SAAS,EACnB,SAAS,CAAC,IAAI,EAAE,WAAW,CAAC,SAAS,CAAC,KAAK,SAAS,CAAC,SAAS,CAAC,KAC7D,SAAS,CAAC,SAAS,EAAE,CAAC,IAAI,EAAE,WAAW,CAAC,SAAS,CAAC,KAAK,SAAS,CAAC,SAAS,CAAC,CAE7E,CAAA;AAED,OAAO,EAAE,eAAe,EAAE,CAAA"}