@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
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 wentools
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,141 @@
1
+ # @wentools/schema-migrations
2
+
3
+ Type-safe schema migrations with compile-time chain validation and entry-only runtime validation.
4
+
5
+ ## Why This Exists
6
+
7
+ Local-first apps store data in IndexedDB, localStorage, or similar. When your data shape changes, you need migrations — but most migration tools target SQL databases, not JS objects.
8
+
9
+ This library provides:
10
+
11
+ - **Compile-time chain validation** — TypeScript catches misordered or mismatched migrations before they run
12
+ - **Entry-only validation** — Zod validates once at the entry point, then trusts types through the chain (fast)
13
+ - **Result-based errors** — no thrown exceptions, every failure is typed and trackable
14
+ - **Zero coupling to storage** — works with any storage backend, you control when and where migrations run
15
+
16
+ ## Install
17
+
18
+ ```bash
19
+ # JSR (recommended)
20
+ npx jsr add @wentools/schema-migrations
21
+
22
+ # Deno
23
+ deno add jsr:@wentools/schema-migrations
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ ### Define Schemas and Migrations
29
+
30
+ ```typescript
31
+ import { z } from 'zod'
32
+ import { createMigration, createMigrations } from '@wentools/schema-migrations'
33
+
34
+ const v0Schema = z.object({ name: z.string() })
35
+ const v1Schema = z.object({ name: z.string(), description: z.string() })
36
+ const v2Schema = z.object({ name: z.string(), description: z.string(), capacity: z.number() })
37
+
38
+ const eventMigrations = createMigrations(v0Schema, [
39
+ createMigration(v0Schema, v1Schema, (v0) => ({ ...v0, description: '' })),
40
+ createMigration(v1Schema, v2Schema, (v1) => ({ ...v1, capacity: 100 })),
41
+ ])
42
+ ```
43
+
44
+ Reorder the migrations and TypeScript will error — each migration must accept the previous one's output.
45
+
46
+ ### Migrate Data
47
+
48
+ ```typescript
49
+ import { migrate } from '@wentools/schema-migrations'
50
+
51
+ // From version 0 to current
52
+ const result = migrate(eventMigrations, { version: 0, data: { name: 'Concert' } })
53
+
54
+ if (result.isOk()) {
55
+ console.log(result.value)
56
+ // { version: 2, data: { name: 'Concert', description: '', capacity: 100 } }
57
+ }
58
+
59
+ // From intermediate version
60
+ migrate(eventMigrations, { version: 1, data: { name: 'Concert', description: 'Live' } })
61
+
62
+ // To specific target version (not necessarily current)
63
+ migrate(eventMigrations, { version: 0, data: { name: 'Concert' } }, 1)
64
+
65
+ // Version defaults to 0 when omitted
66
+ migrate(eventMigrations, { data: { name: 'Concert' } })
67
+ ```
68
+
69
+ ### Stamp New Data
70
+
71
+ ```typescript
72
+ import { withVersion } from '@wentools/schema-migrations'
73
+
74
+ const newEvent = { name: 'Concert', description: '', capacity: 200 }
75
+ const versioned = withVersion(eventMigrations, newEvent)
76
+ // { version: 2, data: newEvent }
77
+ ```
78
+
79
+ ### Error Handling
80
+
81
+ ```typescript
82
+ const result = migrate(eventMigrations, { version: 0, data: { name: 123 } })
83
+
84
+ if (result.isErr()) {
85
+ switch (result.error.type) {
86
+ case 'version_out_of_range':
87
+ // version or targetVersion outside [0, currentVersion]
88
+ break
89
+ case 'invalid_input':
90
+ // data doesn't match claimed version's schema (Zod error in .cause)
91
+ break
92
+ case 'migration_threw':
93
+ // migration function threw (.step, .cause, .data for debugging)
94
+ break
95
+ }
96
+ }
97
+ ```
98
+
99
+ ## API
100
+
101
+ ### Functions
102
+
103
+ | Function | Description |
104
+ |----------|-------------|
105
+ | `migrate(config, versionedData, targetVersion?)` | Migrate data to target version (defaults to current) |
106
+ | `withVersion(config, data)` | Wrap data with current version number |
107
+ | `createMigrations(initialSchema, steps)` | Create migration config with compile-time chain validation |
108
+ | `createMigration(fromSchema, toSchema, fn)` | Create a typed migration step |
109
+
110
+ ### Primitives
111
+
112
+ | Function | Description |
113
+ |----------|-------------|
114
+ | `migrateRange(config, data, from, to)` | Migrate through version range with entry validation |
115
+ | `migrateOneStep(fn, data, step)` | Execute single migration with error tracking |
116
+
117
+ ### Types
118
+
119
+ | Type | Description |
120
+ |------|-------------|
121
+ | `MigrationConfig<TSchema>` | Config object with schemas, migrations, currentVersion, currentSchema |
122
+ | `VersionedData<TData>` | `{ version?: number; data: TData }` |
123
+ | `MigrateError` | Union of all migration errors |
124
+ | `VersionOutOfRangeError` | Version outside valid range |
125
+ | `InvalidInputError` | Data fails schema validation |
126
+ | `MigrateOneStepError` | Migration function threw |
127
+ | `MigrateRangeError` | `InvalidInputError | MigrateOneStepError` |
128
+
129
+ ## Validation Strategy
130
+
131
+ This library uses **entry-only validation**: data is validated with Zod once at the starting version, then migrations run without intermediate validation. This is fast and sufficient — if your migration functions are correct (which TypeScript enforces at compile time), intermediate validation is redundant.
132
+
133
+ ## Requirements
134
+
135
+ - TypeScript 5.0+
136
+ - Zod 4.x (peer dependency)
137
+ - @wentools/result 0.1.x (peer dependency)
138
+
139
+ ## License
140
+
141
+ MIT
@@ -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"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Creates a typed migration step with schema inference
3
+ *
4
+ * Helper function that pairs a schema with its migration function,
5
+ * providing type inference for the migration function input.
6
+ *
7
+ * **Type Safety:** The returned tuple is typed as readonly to ensure
8
+ * compile-time validation of migration chain ordering in createMigrations.
9
+ *
10
+ * @param fromSchema - Schema for input version (used for type inference)
11
+ * @param toSchema - Schema for output version
12
+ * @param migrate - Migration function from TFrom to TTo
13
+ * @returns Readonly tuple of [schema, migrateFn] ready for createMigrations
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const v0Schema = z.object({ name: z.string() })
18
+ * const v1Schema = z.object({ name: z.string(), description: z.string() })
19
+ *
20
+ * const addDescription = createMigration(v0Schema, v1Schema, (v0) => ({
21
+ * ...v0,
22
+ * description: '', // v0 is typed as { name: string }
23
+ * }))
24
+ *
25
+ * const migrations = createMigrations(v0Schema, [addDescription])
26
+ * ```
27
+ */
28
+ const createMigration = (fromSchema, toSchema, migrate) => {
29
+ return [toSchema, migrate];
30
+ };
31
+ export { createMigration };
@@ -0,0 +1,71 @@
1
+ import type { ZodTypeAny } from 'zod';
2
+ import type { MigrationConfig } from './migrate_range.js';
3
+ /**
4
+ * Extract output type from a ZodType using the _output property
5
+ */
6
+ type ExtractOutput<T> = T extends {
7
+ _output: infer TOutput;
8
+ } ? TOutput : never;
9
+ /**
10
+ * Validates migration chain type safety at compile time
11
+ *
12
+ * Recursively checks that each migration's input type matches the previous migration's output.
13
+ * Returns the same tuple type if valid, or `never` if the chain is invalid.
14
+ *
15
+ * Uses structural typing to match against function signatures rather than exact types,
16
+ * making it compatible with TypeScript's type inference for complex Zod schemas.
17
+ *
18
+ * @template TInitial - Expected input type for first migration
19
+ * @template TSteps - Tuple of migration steps to validate
20
+ * @returns The same tuple type if valid, `never` if invalid (causing a compile error)
21
+ */
22
+ type ValidChain<TInitial, TSteps extends readonly unknown[]> = TSteps extends readonly [] ? readonly [] : TSteps extends readonly [infer TFirst, ...infer TRest] ? TFirst extends readonly [infer _TSchema, infer TFn] ? TFn extends (from: TInitial) => infer TNext ? readonly [TFirst, ...ValidChain<TNext, TRest>] : never : never : never;
23
+ /**
24
+ * Extracts the final output type from a migration chain
25
+ *
26
+ * @template TInitial - Type of the initial schema (v0)
27
+ * @template TSteps - Tuple of migration steps
28
+ * @returns The output type of the last migration, or TInitial if no migrations
29
+ */
30
+ type FinalType<TInitial, TSteps extends readonly unknown[]> = TSteps extends readonly [] ? TInitial : TSteps extends readonly [...unknown[], readonly [unknown, (from: any) => infer TFinal]] ? TFinal : never;
31
+ /**
32
+ * Creates a type-safe migration configuration from initial schema and migration steps
33
+ *
34
+ * **Type Safety:** The migration chain is validated at compile time. Each migration
35
+ * must accept the output type of the previous migration. Reordering or mismatched
36
+ * migrations will cause TypeScript errors.
37
+ *
38
+ * @param initialSchema - Schema for version 0
39
+ * @param steps - Array of [schema, migrateFn] tuples, each typed to accept previous output
40
+ * @returns Complete migration configuration ready for use with migrate()
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * const v0Schema = z.object({ name: z.string() })
45
+ * const v1Schema = z.object({ name: z.string(), description: z.string() })
46
+ * const v2Schema = z.object({ name: z.string(), description: z.string(), capacity: z.number() })
47
+ *
48
+ * // ✅ Valid - each migration accepts previous output
49
+ * const eventMigrations = createMigrations(v0Schema, [
50
+ * createMigration(v0Schema, v1Schema, (v0) => ({ ...v0, description: '' })), // v0 → v1
51
+ * createMigration(v1Schema, v2Schema, (v1) => ({ ...v1, capacity: 100 })), // v1 → v2
52
+ * ])
53
+ *
54
+ * // ❌ Type Error - wrong order! Second migration expects v1 but gets v0
55
+ * const badMigrations = createMigrations(v0Schema, [
56
+ * createMigration(v1Schema, v2Schema, (v1) => ({ ...v1, capacity: 100 })), // Expects v1, gets v0
57
+ * createMigration(v0Schema, v1Schema, (v0) => ({ ...v0, description: '' })),
58
+ * ])
59
+ *
60
+ * // Result:
61
+ * // {
62
+ * // schemas: [v0Schema, v1Schema, v2Schema],
63
+ * // migrations: [migrate0→1, migrate1→2],
64
+ * // currentVersion: 2,
65
+ * // currentSchema: v2Schema
66
+ * // }
67
+ * ```
68
+ */
69
+ declare const createMigrations: <TInitialSchema extends ZodTypeAny, const TSteps extends readonly unknown[]>(initialSchema: TInitialSchema, steps: TSteps & ValidChain<ExtractOutput<TInitialSchema>, TSteps>) => MigrationConfig<FinalType<ExtractOutput<TInitialSchema>, TSteps>>;
70
+ export { createMigrations, type ValidChain, type FinalType };
71
+ //# sourceMappingURL=create_migrations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create_migrations.d.ts","sourceRoot":"","sources":["../src/create_migrations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAa,UAAU,EAAE,MAAM,KAAK,CAAA;AAChD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAEzD;;GAEG;AACH,KAAK,aAAa,CAAC,CAAC,IAAI,CAAC,SAAS;IAAE,OAAO,EAAE,MAAM,OAAO,CAAA;CAAE,GAAG,OAAO,GAAG,KAAK,CAAA;AAE9E;;;;;;;;;;;;GAYG;AACH,KAAK,UAAU,CAAC,QAAQ,EAAE,MAAM,SAAS,SAAS,OAAO,EAAE,IAAI,MAAM,SAAS,SAAS,EAAE,GACtF,SAAS,EAAE,GACX,MAAM,SAAS,SAAS,CAAC,MAAM,MAAM,EAAE,GAAG,MAAM,KAAK,CAAC,GACrD,MAAM,SAAS,SAAS,CAAC,MAAM,QAAQ,EAAE,MAAM,GAAG,CAAC,GAClD,GAAG,SAAS,CAAC,IAAI,EAAE,QAAQ,KAAK,MAAM,KAAK,GAC1C,SAAS,CAAC,MAAM,EAAE,GAAG,UAAU,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,GAC9C,KAAK,GACN,KAAK,GACN,KAAK,CAAA;AAET;;;;;;GAMG;AACH,KAAK,SAAS,CAAC,QAAQ,EAAE,MAAM,SAAS,SAAS,OAAO,EAAE,IAAI,MAAM,SAAS,SAAS,EAAE,GACrF,QAAQ,GACR,MAAM,SAAS,SAAS,CAAC,GAAG,OAAO,EAAE,EAAE,SAAS,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,MAAM,MAAM,CAAC,CAAC,GACtF,MAAM,GACN,KAAK,CAAA;AAET;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,QAAA,MAAM,gBAAgB,GACrB,cAAc,SAAS,UAAU,EACjC,KAAK,CAAC,MAAM,SAAS,SAAS,OAAO,EAAE,EAEvC,eAAe,cAAc,EAC7B,OAAO,MAAM,GAAG,UAAU,CAAC,aAAa,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC,KAC/D,eAAe,CAAC,SAAS,CAAC,aAAa,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC,CA0BlE,CAAA;AAED,OAAO,EAAE,gBAAgB,EAAE,KAAK,UAAU,EAAE,KAAK,SAAS,EAAE,CAAA"}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Creates a type-safe migration configuration from initial schema and migration steps
3
+ *
4
+ * **Type Safety:** The migration chain is validated at compile time. Each migration
5
+ * must accept the output type of the previous migration. Reordering or mismatched
6
+ * migrations will cause TypeScript errors.
7
+ *
8
+ * @param initialSchema - Schema for version 0
9
+ * @param steps - Array of [schema, migrateFn] tuples, each typed to accept previous output
10
+ * @returns Complete migration configuration ready for use with migrate()
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const v0Schema = z.object({ name: z.string() })
15
+ * const v1Schema = z.object({ name: z.string(), description: z.string() })
16
+ * const v2Schema = z.object({ name: z.string(), description: z.string(), capacity: z.number() })
17
+ *
18
+ * // ✅ Valid - each migration accepts previous output
19
+ * const eventMigrations = createMigrations(v0Schema, [
20
+ * createMigration(v0Schema, v1Schema, (v0) => ({ ...v0, description: '' })), // v0 → v1
21
+ * createMigration(v1Schema, v2Schema, (v1) => ({ ...v1, capacity: 100 })), // v1 → v2
22
+ * ])
23
+ *
24
+ * // ❌ Type Error - wrong order! Second migration expects v1 but gets v0
25
+ * const badMigrations = createMigrations(v0Schema, [
26
+ * createMigration(v1Schema, v2Schema, (v1) => ({ ...v1, capacity: 100 })), // Expects v1, gets v0
27
+ * createMigration(v0Schema, v1Schema, (v0) => ({ ...v0, description: '' })),
28
+ * ])
29
+ *
30
+ * // Result:
31
+ * // {
32
+ * // schemas: [v0Schema, v1Schema, v2Schema],
33
+ * // migrations: [migrate0→1, migrate1→2],
34
+ * // currentVersion: 2,
35
+ * // currentSchema: v2Schema
36
+ * // }
37
+ * ```
38
+ */
39
+ const createMigrations = (initialSchema, steps) => {
40
+ // Build schemas array: [initial, ...step schemas]
41
+ const schemas = [
42
+ initialSchema,
43
+ ...steps.map(([schema]) => schema),
44
+ ];
45
+ // Extract migration functions from steps
46
+ const migrations = steps.map(([, migrate]) => migrate);
47
+ // Current version is the index of the last schema
48
+ const currentVersion = schemas.length - 1;
49
+ // Current schema is the last schema in the array
50
+ const currentSchema = schemas[currentVersion];
51
+ return {
52
+ schemas,
53
+ migrations,
54
+ currentVersion,
55
+ currentSchema,
56
+ };
57
+ };
58
+ export { createMigrations };
@@ -0,0 +1,38 @@
1
+ import { type Err } from 'neverthrow';
2
+ /**
3
+ * Create an Err Result with automatic literal type preservation
4
+ *
5
+ * Uses TypeScript 5.0+ const type parameters to preserve literal types automatically.
6
+ * No need for 'as const' - the type parameter is inferred as a literal!
7
+ *
8
+ * Returns Err<never, E> (not Result<never, E>) so .error is directly accessible.
9
+ *
10
+ * @example
11
+ * // ✅ Simple error
12
+ * return err('not_found', 'User not found')
13
+ * // Type: Err<never, { type: 'not_found', message: string }>
14
+ *
15
+ * @example
16
+ * // ✅ Error with additional properties
17
+ * return err('not_found', 'User not found', { userId: '123' })
18
+ * // Type: Err<never, { type: 'not_found', message: string, userId: string }>
19
+ *
20
+ * @example
21
+ * // ✅ Direct .error access (no narrowing needed)
22
+ * const error = err('not_found', 'User not found').error
23
+ * // error.type is 'not_found'
24
+ *
25
+ * @example
26
+ * // ✅ Error with complex additional data
27
+ * return err('validation_failed', 'Invalid input', {
28
+ * field: 'email',
29
+ * errors: ['Invalid format']
30
+ * })
31
+ * // Type: Err<never, { type: 'validation_failed', message: string, field: string, errors: string[] }>
32
+ */
33
+ declare const err: <const TType extends string, TAdditional extends Record<string, unknown> = Record<string, never>>(type: TType, message: string, additional?: TAdditional) => Err<never, {
34
+ type: TType;
35
+ message: string;
36
+ } & TAdditional>;
37
+ export { err };
38
+ //# sourceMappingURL=err.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"err.d.ts","sourceRoot":"","sources":["../../../../../../../../src/deps/jsr.io/@wentools/result/0.1.3/src/err/err.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,KAAK,GAAG,EAAE,MAAM,YAAY,CAAA;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,QAAA,MAAM,GAAG,GACR,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,GAAG,CAAC,KAAK,EAAE;IAAE,IAAI,EAAE,KAAK,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,WAAW,CAKP,CAAA;AAErD,OAAO,EAAE,GAAG,EAAE,CAAA"}
@@ -0,0 +1,38 @@
1
+ import { err as _Err } from 'neverthrow';
2
+ /**
3
+ * Create an Err Result with automatic literal type preservation
4
+ *
5
+ * Uses TypeScript 5.0+ const type parameters to preserve literal types automatically.
6
+ * No need for 'as const' - the type parameter is inferred as a literal!
7
+ *
8
+ * Returns Err<never, E> (not Result<never, E>) so .error is directly accessible.
9
+ *
10
+ * @example
11
+ * // ✅ Simple error
12
+ * return err('not_found', 'User not found')
13
+ * // Type: Err<never, { type: 'not_found', message: string }>
14
+ *
15
+ * @example
16
+ * // ✅ Error with additional properties
17
+ * return err('not_found', 'User not found', { userId: '123' })
18
+ * // Type: Err<never, { type: 'not_found', message: string, userId: string }>
19
+ *
20
+ * @example
21
+ * // ✅ Direct .error access (no narrowing needed)
22
+ * const error = err('not_found', 'User not found').error
23
+ * // error.type is 'not_found'
24
+ *
25
+ * @example
26
+ * // ✅ Error with complex additional data
27
+ * return err('validation_failed', 'Invalid input', {
28
+ * field: 'email',
29
+ * errors: ['Invalid format']
30
+ * })
31
+ * // Type: Err<never, { type: 'validation_failed', message: string, field: string, errors: string[] }>
32
+ */
33
+ const err = (type, message, additional) => _Err({
34
+ ...additional,
35
+ type,
36
+ message,
37
+ });
38
+ export { err };
@@ -0,0 +1,2 @@
1
+ export { err } from './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/err/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA"}
@@ -0,0 +1 @@
1
+ export { err } from './err.js';
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Check if a value is a neverthrow Result type
3
+ *
4
+ * Uses duck typing to detect Result objects without importing neverthrow types.
5
+ */
6
+ declare const isResult: (value: unknown) => value is {
7
+ isOk: () => boolean;
8
+ isErr: () => boolean;
9
+ };
10
+ export { isResult };
11
+ //# sourceMappingURL=is_result.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is_result.d.ts","sourceRoot":"","sources":["../../../../../../../../src/deps/jsr.io/@wentools/result/0.1.3/src/is_result/is_result.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,QAAA,MAAM,QAAQ,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI;IAAE,IAAI,EAAE,MAAM,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,OAAO,CAAA;CAStF,CAAA;AAED,OAAO,EAAE,QAAQ,EAAE,CAAA"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Check if a value is a neverthrow Result type
3
+ *
4
+ * Uses duck typing to detect Result objects without importing neverthrow types.
5
+ */
6
+ const isResult = (value) => {
7
+ return (value !== null &&
8
+ typeof value === 'object' &&
9
+ 'isOk' in value &&
10
+ 'isErr' in value &&
11
+ typeof value.isOk === 'function' &&
12
+ typeof value.isErr === 'function');
13
+ };
14
+ export { isResult };
@@ -0,0 +1,2 @@
1
+ export { isResult } from './is_result.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/is_result/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA"}
@@ -0,0 +1 @@
1
+ export { isResult } from './is_result.js';
@@ -0,0 +1,156 @@
1
+ import { err as _Err, ok, type Result, type Err, ResultAsync, 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
+ declare const makeErr: <const TType extends string, TAdditional extends Record<string, unknown> = Record<string, never>>(type: TType, message: string, additional?: TAdditional) => {
14
+ type: TType;
15
+ message: string;
16
+ } & TAdditional;
17
+ /**
18
+ * Create an async Err ResultAsync with automatic literal type preservation
19
+ *
20
+ * Async version of err() - same signature, returns ResultAsync instead of Result.
21
+ * Uses TypeScript 5.0+ const type parameters to preserve literal types automatically.
22
+ *
23
+ * @example
24
+ * // ✅ Simple error
25
+ * return errAsync('not_found', 'User not found')
26
+ * // Type: ResultAsync<never, { type: 'not_found', message: string }>
27
+ *
28
+ * @example
29
+ * // ✅ Error with additional properties
30
+ * return errAsync('not_found', 'User not found', { userId: '123' })
31
+ * // Type: ResultAsync<never, { type: 'not_found', message: string, userId: string }>
32
+ *
33
+ * @example
34
+ * // ✅ Error with complex additional data
35
+ * return errAsync('validation_failed', 'Invalid input', {
36
+ * field: 'email',
37
+ * errors: ['Invalid format']
38
+ * })
39
+ * // Type: ResultAsync<never, { type: 'validation_failed', message: string, field: string, errors: string[] }>
40
+ */
41
+ declare const errAsync: <const TType extends string, TAdditional extends Record<string, unknown> = Record<string, never>>(type: TType, message: string, additional?: TAdditional) => ResultAsync<never, {
42
+ type: TType;
43
+ message: string;
44
+ } & TAdditional>;
45
+ /**
46
+ * Re-export okAsync from neverthrow for consistency
47
+ * Creates a successful ResultAsync
48
+ */
49
+ declare const okAsync: typeof neverthrowOkAsync;
50
+ /**
51
+ * Utility type for defining error types concisely
52
+ *
53
+ * @example
54
+ * // Instead of:
55
+ * type UserNotFoundError = {
56
+ * type: 'user_not_found'
57
+ * message: string
58
+ * userId: UserId
59
+ * }
60
+ *
61
+ * // Write:
62
+ * type UserNotFoundError = ErrorType<'user_not_found', { userId: UserId }>
63
+ *
64
+ * @example
65
+ * // Error without additional properties:
66
+ * type InvalidOperationError = ErrorType<'invalid_operation'>
67
+ */
68
+ type ErrorType<TType extends string, TAdditional extends Record<string, unknown> = Record<string, never>> = {
69
+ type: TType;
70
+ message: string;
71
+ } & TAdditional;
72
+ /**
73
+ * Extract the error type from a Result or Promise<Result>
74
+ *
75
+ * @example
76
+ * // From async function
77
+ * const getUser = async (id: string): Promise<Result<User, UserNotFoundError>> => { ... }
78
+ * type GetUserError = ExtractError<ReturnType<typeof getUser>>
79
+ * // Type: UserNotFoundError
80
+ *
81
+ * @example
82
+ * // From Result type directly
83
+ * type MyResult = Result<User, InvalidError>
84
+ * type MyError = ExtractError<MyResult>
85
+ * // Type: InvalidError
86
+ *
87
+ * @example
88
+ * // Works with composed errors
89
+ * const replaceVersion = async (): Promise<Result<Version, VersionNotFoundError | VersionExistsError>> => { ... }
90
+ * type ReplaceError = ExtractError<ReturnType<typeof replaceVersion>>
91
+ * // Type: VersionNotFoundError | VersionExistsError
92
+ */
93
+ type ExtractError<TResultOrPromise> = TResultOrPromise extends Promise<Result<unknown, infer TError>> ? TError : TResultOrPromise extends Result<unknown, infer TError> ? TError : never;
94
+ /**
95
+ * Extract the error type from a function that returns Result or Promise<Result>
96
+ *
97
+ * @example
98
+ * // Simple extraction
99
+ * const getUser = async (id: string): Promise<Result<User, UserNotFoundError>> => { ... }
100
+ * type GetUserError = ExtractFnError<typeof getUser>
101
+ * // Type: UserNotFoundError
102
+ *
103
+ * @example
104
+ * // Compose errors from multiple functions
105
+ * const getUser = async (id: string): Promise<Result<User, UserNotFoundError>> => { ... }
106
+ * const getImage = async (id: string): Promise<Result<Image, ImageNotFoundError>> => { ... }
107
+ * type CombinedError = ExtractFnError<typeof getUser> | ExtractFnError<typeof getImage>
108
+ * // Type: UserNotFoundError | ImageNotFoundError
109
+ */
110
+ type ExtractFnError<TFunction extends (...args: unknown[]) => unknown> = ExtractError<ReturnType<TFunction>>;
111
+ /**
112
+ * Extract and combine error types from multiple Result or Promise<Result> types
113
+ *
114
+ * @example
115
+ * type UserResult = Result<User, UserNotFoundError>
116
+ * type ImageResult = Promise<Result<Image, ImageNotFoundError>>
117
+ * type ProductResult = Result<Product, ProductNotFoundError>
118
+ *
119
+ * type CombinedError = ExtractErrors<[UserResult, ImageResult, ProductResult]>
120
+ * // Type: UserNotFoundError | ImageNotFoundError | ProductNotFoundError
121
+ */
122
+ type ExtractErrors<TResults extends readonly unknown[]> = TResults extends readonly [
123
+ infer TFirst,
124
+ ...infer TRest
125
+ ] ? ExtractError<TFirst> | ExtractErrors<TRest> : never;
126
+ /**
127
+ * Extract and combine error types from multiple functions that return Result or Promise<Result>
128
+ *
129
+ * @example
130
+ * const getUser = async (id: string): Promise<Result<User, UserNotFoundError>> => { ... }
131
+ * const getImage = async (id: string): Promise<Result<Image, ImageNotFoundError>> => { ... }
132
+ * const deleteProduct = async (id: string): Promise<Result<void, ProductNotFoundError>> => { ... }
133
+ *
134
+ * type CombinedError = ExtractFnsError<[typeof getUser, typeof getImage, typeof deleteProduct]>
135
+ * // Type: UserNotFoundError | ImageNotFoundError | ProductNotFoundError
136
+ */
137
+ type ExtractFnsError<TFunctions extends readonly ((...args: unknown[]) => unknown)[]> = TFunctions extends readonly [
138
+ infer TFirst,
139
+ ...infer TRest extends readonly ((...args: unknown[]) => unknown)[]
140
+ ] ? ExtractFnError<TFirst & ((...args: unknown[]) => unknown)> | ExtractFnsError<TRest> : never;
141
+ /**
142
+ * Raw error function from neverthrow - use for lifting database errors directly
143
+ *
144
+ * @example
145
+ * const seedResult = await songSeeds.get.one.byId(songSeedId)
146
+ * if (seedResult.isErr()) {
147
+ * return rawErr(seedResult.error)
148
+ * }
149
+ */
150
+ declare const rawErr: typeof _Err;
151
+ export { makeErr, errAsync, ok, okAsync, rawErr, _Err, ResultAsync };
152
+ export type { Result, Err, ErrorType, ExtractError, ExtractFnError, ExtractErrors, ExtractFnsError };
153
+ export { err } from './err/mod.js';
154
+ export { isResult } from './is_result/mod.js';
155
+ export { propagateErr } from './propagate_err/mod.js';
156
+ //# sourceMappingURL=mod.d.ts.map