@pipelab/migration 1.0.1-beta.19
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/.turbo/turbo-build.log +16 -0
- package/.turbo/turbo-lint.log +18 -0
- package/CHANGELOG.md +121 -0
- package/LICENSE +110 -0
- package/LICENSE.md +110 -0
- package/README.md +3 -0
- package/dist/index.d.mts +66 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +127 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +45 -0
- package/src/index.ts +11 -0
- package/src/models/createMigration.ts +72 -0
- package/src/models/createMigrator.ts +22 -0
- package/src/models/migration.spec.ts +343 -0
- package/src/models/migration.ts +157 -0
- package/src/utils/object-keys.ts +8 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
> @pipelab/migration@1.0.1-beta.17 build /run/media/armaldio/SSD/Projects/Pipelab/pipelab/packages/migration
|
|
4
|
+
> tsdown
|
|
5
|
+
|
|
6
|
+
[34mℹ[39m tsdown [2mv0.21.2[22m powered by rolldown [2mv1.0.0-rc.9[22m
|
|
7
|
+
[34mℹ[39m entry: [34msrc/index.ts[39m
|
|
8
|
+
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
9
|
+
[34mℹ[39m Build start
|
|
10
|
+
[34mℹ[39m Cleaning 4 files
|
|
11
|
+
[34mℹ[39m [2mdist/[22m[1mindex.mjs[22m [2m 4.05 kB[22m [2m│ gzip: 1.27 kB[22m
|
|
12
|
+
[34mℹ[39m [2mdist/[22mindex.mjs.map [2m10.50 kB[22m [2m│ gzip: 3.17 kB[22m
|
|
13
|
+
[34mℹ[39m [2mdist/[22mindex.d.mts.map [2m 1.99 kB[22m [2m│ gzip: 0.69 kB[22m
|
|
14
|
+
[34mℹ[39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m 3.40 kB[22m [2m│ gzip: 0.98 kB[22m
|
|
15
|
+
[34mℹ[39m 4 files, total: 19.95 kB
|
|
16
|
+
[32m✔[39m Build complete in [32m1015ms[39m
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
> @pipelab/migration@1.0.1-beta.4 lint /run/media/armaldio/SSD/Projects/Pipelab/pipelab/packages/migration
|
|
4
|
+
> oxlint .
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
[38;2;244;191;117;1mâš [0m ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-unused-vars.html\[38;2;244;191;117;1meslint(no-unused-vars)[0m]8;;\: [38;2;244;191;117;1mVariable 'InitialState' is declared but never used. Unused variables should start with a '_'.[0m
|
|
8
|
+
â•─[[38;2;92;157;255;1msrc/models/migration.ts[0m:50:33]
|
|
9
|
+
[2m49[0m │
|
|
10
|
+
[2m50[0m │ export interface MigratorConfig<InitialState, FinalState> {
|
|
11
|
+
· [38;2;246;87;248m ──────┬─────[0m
|
|
12
|
+
· [38;2;246;87;248m╰── [38;2;246;87;248m'InitialState' is declared here[0m[0m
|
|
13
|
+
[2m51[0m │ migrations: MigrationClass<any, any, any>[];
|
|
14
|
+
╰────
|
|
15
|
+
[38;2;106;159;181m help: [0mConsider removing this declaration.
|
|
16
|
+
|
|
17
|
+
Found 1 warning and 0 errors.
|
|
18
|
+
Finished in 17ms on 6 files with 93 rules using 16 threads.
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# @pipelab/migration
|
|
2
|
+
|
|
3
|
+
## 1.0.1-beta.19
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- azs
|
|
8
|
+
|
|
9
|
+
## 1.0.1-beta.18
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- sqqd
|
|
14
|
+
|
|
15
|
+
## 1.0.1-beta.17
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- sd
|
|
20
|
+
|
|
21
|
+
## 1.0.1-beta.16
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- sd
|
|
26
|
+
|
|
27
|
+
## 1.0.1-beta.15
|
|
28
|
+
|
|
29
|
+
### Patch Changes
|
|
30
|
+
|
|
31
|
+
- wdf
|
|
32
|
+
|
|
33
|
+
## 1.0.1-beta.14
|
|
34
|
+
|
|
35
|
+
### Patch Changes
|
|
36
|
+
|
|
37
|
+
- sdf
|
|
38
|
+
|
|
39
|
+
## 1.0.1-beta.13
|
|
40
|
+
|
|
41
|
+
### Patch Changes
|
|
42
|
+
|
|
43
|
+
- sd
|
|
44
|
+
|
|
45
|
+
## 1.0.1-beta.12
|
|
46
|
+
|
|
47
|
+
### Patch Changes
|
|
48
|
+
|
|
49
|
+
- rfg
|
|
50
|
+
|
|
51
|
+
## 1.0.1-beta.11
|
|
52
|
+
|
|
53
|
+
### Patch Changes
|
|
54
|
+
|
|
55
|
+
- szd
|
|
56
|
+
|
|
57
|
+
## 1.0.1-beta.10
|
|
58
|
+
|
|
59
|
+
### Patch Changes
|
|
60
|
+
|
|
61
|
+
- sd
|
|
62
|
+
|
|
63
|
+
## 1.0.1-beta.9
|
|
64
|
+
|
|
65
|
+
### Patch Changes
|
|
66
|
+
|
|
67
|
+
- sd
|
|
68
|
+
|
|
69
|
+
## 1.0.1-beta.8
|
|
70
|
+
|
|
71
|
+
### Patch Changes
|
|
72
|
+
|
|
73
|
+
- sd
|
|
74
|
+
|
|
75
|
+
## 1.0.1-beta.7
|
|
76
|
+
|
|
77
|
+
### Patch Changes
|
|
78
|
+
|
|
79
|
+
- sd
|
|
80
|
+
|
|
81
|
+
## 1.0.1-beta.6
|
|
82
|
+
|
|
83
|
+
### Patch Changes
|
|
84
|
+
|
|
85
|
+
- test
|
|
86
|
+
|
|
87
|
+
## 1.0.1-beta.5
|
|
88
|
+
|
|
89
|
+
### Patch Changes
|
|
90
|
+
|
|
91
|
+
- test
|
|
92
|
+
|
|
93
|
+
## 1.0.1-beta.4
|
|
94
|
+
|
|
95
|
+
### Patch Changes
|
|
96
|
+
|
|
97
|
+
- sd
|
|
98
|
+
|
|
99
|
+
## 1.0.1-beta.3
|
|
100
|
+
|
|
101
|
+
### Patch Changes
|
|
102
|
+
|
|
103
|
+
- tesyt
|
|
104
|
+
|
|
105
|
+
## 1.0.1-beta.2
|
|
106
|
+
|
|
107
|
+
### Patch Changes
|
|
108
|
+
|
|
109
|
+
- last test
|
|
110
|
+
|
|
111
|
+
## 1.0.1-beta.1
|
|
112
|
+
|
|
113
|
+
### Patch Changes
|
|
114
|
+
|
|
115
|
+
- 9268b1e: test 3
|
|
116
|
+
|
|
117
|
+
## 1.0.1-beta.0
|
|
118
|
+
|
|
119
|
+
### Patch Changes
|
|
120
|
+
|
|
121
|
+
- test
|
package/LICENSE
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, MIT Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-MIT
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 CynToolkit
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the MIT license that is effective on the second anniversary of the date we make
|
|
91
|
+
the Software available. On or after that date, you may use the Software under
|
|
92
|
+
the MIT license, in which case the following will apply:
|
|
93
|
+
|
|
94
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
95
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
96
|
+
the Software without restriction, including without limitation the rights to
|
|
97
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
98
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
99
|
+
so, subject to the following conditions:
|
|
100
|
+
|
|
101
|
+
The above copyright notice and this permission notice shall be included in all
|
|
102
|
+
copies or substantial portions of the Software.
|
|
103
|
+
|
|
104
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
105
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
106
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
107
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
108
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
109
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
110
|
+
SOFTWARE.
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, MIT Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-MIT
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2024-present Quentin Goinaud - Armaldio
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the MIT license that is effective on the second anniversary of the date we make
|
|
91
|
+
the Software available. On or after that date, you may use the Software under
|
|
92
|
+
the MIT license, in which case the following will apply:
|
|
93
|
+
|
|
94
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
95
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
96
|
+
the Software without restriction, including without limitation the rights to
|
|
97
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
98
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
99
|
+
so, subject to the following conditions:
|
|
100
|
+
|
|
101
|
+
The above copyright notice and this permission notice shall be included in all
|
|
102
|
+
copies or substantial portions of the Software.
|
|
103
|
+
|
|
104
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
105
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
106
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
107
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
108
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
109
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
110
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import * as valibot from "valibot";
|
|
2
|
+
import { InferOutput, ObjectEntries } from "valibot";
|
|
3
|
+
|
|
4
|
+
//#region src/models/migration.d.ts
|
|
5
|
+
type Awaitable<T> = Promise<T> | T;
|
|
6
|
+
type MigrationFn<From, To> = (state: From, targetVersion: string) => Awaitable<To>;
|
|
7
|
+
type MigrateOptions = {
|
|
8
|
+
debug?: boolean;
|
|
9
|
+
target?: SemVer;
|
|
10
|
+
};
|
|
11
|
+
declare const MigrationSchemaValidator: valibot.ObjectSchema<{
|
|
12
|
+
readonly version: valibot.CustomSchema<`${number}.${number}.${number}`, undefined>;
|
|
13
|
+
}, undefined>;
|
|
14
|
+
declare const createVersionSchema: <OBJECTENTRIES extends ObjectEntries>(schema: OBJECTENTRIES) => valibot.ObjectSchema<OBJECTENTRIES, undefined>;
|
|
15
|
+
type MigrationSchema = InferOutput<typeof MigrationSchemaValidator>;
|
|
16
|
+
type OmitVersion<T> = Omit<T, keyof MigrationSchema>;
|
|
17
|
+
type SemVer = `${number}.${number}.${number}`;
|
|
18
|
+
interface MigrationClass<Down, Current, Up> {
|
|
19
|
+
version: SemVer;
|
|
20
|
+
up: MigrationFn<Current, Up>;
|
|
21
|
+
down: MigrationFn<Current, Down>;
|
|
22
|
+
}
|
|
23
|
+
interface MigrationObjInput<Down, Current, Up> {
|
|
24
|
+
version: SemVer;
|
|
25
|
+
up: MigrationFn<OmitVersion<Current>, OmitVersion<Up>>;
|
|
26
|
+
down: MigrationFn<OmitVersion<Current>, OmitVersion<Down>>;
|
|
27
|
+
}
|
|
28
|
+
interface MigratorConfig<InitialState, FinalState> {
|
|
29
|
+
migrations: MigrationClass<any, any, any>[];
|
|
30
|
+
coerce?: boolean;
|
|
31
|
+
defaultValue?: FinalState;
|
|
32
|
+
}
|
|
33
|
+
declare class Migrator<InitialState extends MigrationSchema, OutputState extends MigrationSchema> {
|
|
34
|
+
current: SemVer;
|
|
35
|
+
migrations: Record<SemVer, MigrationClass<any, any, any>>;
|
|
36
|
+
coerce: boolean;
|
|
37
|
+
defaultValue: OutputState;
|
|
38
|
+
constructor(config: MigratorConfig<InitialState, OutputState>);
|
|
39
|
+
getVersions(): `${number}.${number}.${number}`[];
|
|
40
|
+
migrate(_state: MigrationSchema | undefined, options?: MigrateOptions): Promise<OutputState>;
|
|
41
|
+
tryCoerce(version: SemVer): SemVer;
|
|
42
|
+
needMigration(version: SemVer): boolean;
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/models/createMigration.d.ts
|
|
46
|
+
declare function createMigration<Down extends MigrationSchema, Current extends MigrationSchema, Up extends MigrationSchema>(migration: MigrationObjInput<Down, Current, Up>): MigrationClass<Down, Current, Up>;
|
|
47
|
+
declare const initialVersion: () => never;
|
|
48
|
+
declare const finalVersion: () => never;
|
|
49
|
+
/**
|
|
50
|
+
* @deprecated
|
|
51
|
+
* @param version
|
|
52
|
+
* @param migration
|
|
53
|
+
* @returns
|
|
54
|
+
*/
|
|
55
|
+
declare function initial<Current extends MigrationSchema, Up extends MigrationSchema>(version: SemVer, migration: MigrationFn<OmitVersion<Current>, OmitVersion<Up>>): MigrationClass<Current, Current, Up>;
|
|
56
|
+
declare function final<Current extends MigrationSchema, Down extends MigrationSchema>(version: SemVer, migration: MigrationFn<OmitVersion<Current>, OmitVersion<Down>>): MigrationClass<Down, Current, Current>;
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/models/createMigrator.d.ts
|
|
59
|
+
declare const createMigrator: <InitialState extends MigrationSchema, FinalState extends MigrationSchema>() => {
|
|
60
|
+
createDefault(defaultState: FinalState): FinalState;
|
|
61
|
+
createMigrations(config: MigratorConfig<InitialState, FinalState>): Migrator<InitialState, FinalState>;
|
|
62
|
+
};
|
|
63
|
+
type MigratorFactory<InitialState extends MigrationSchema, FinalState extends MigrationSchema> = ReturnType<typeof createMigrator<InitialState, FinalState>>;
|
|
64
|
+
//#endregion
|
|
65
|
+
export { type MigrationFn, type MigrationSchema, type Migrator, type MigratorConfig, MigratorFactory, type OmitVersion, type SemVer, createMigration, createMigrator, createVersionSchema, final, finalVersion, initial, initialVersion };
|
|
66
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/models/migration.ts","../src/models/createMigration.ts","../src/models/createMigrator.ts"],"mappings":";;;;KAMY,SAAA,MAAe,OAAA,CAAQ,CAAA,IAAK,CAAA;AAAA,KAE5B,WAAA,cAAyB,KAAA,EAAO,IAAA,EAAM,aAAA,aAA0B,SAAA,CAAU,EAAA;AAAA,KAE1E,cAAA;EACV,KAAA;EACA,MAAA,GAAS,MAAA;AAAA;AAAA,cAUE,wBAAA,EAEX,OAAA,CAFmC,YAAA;EAAA,kBAEnC,OAAA,CAAA,YAAA;AAAA;AAAA,cAEW,mBAAA,yBAA6C,aAAA,EAAe,MAAA,EAAQ,aAAA,KAAa,OAAA,CAAA,YAAA,CAAA,aAAA;AAAA,KAKlF,eAAA,GAAkB,WAAA,QAAmB,wBAAA;AAAA,KAErC,WAAA,MAAiB,IAAA,CAAK,CAAA,QAAS,eAAA;AAAA,KAE/B,MAAA;AAAA,UAEK,cAAA;EACf,OAAA,EAAS,MAAA;EACT,EAAA,EAAI,WAAA,CAAY,OAAA,EAAS,EAAA;EACzB,IAAA,EAAM,WAAA,CAAY,OAAA,EAAS,IAAA;AAAA;AAAA,UAGZ,iBAAA;EACf,OAAA,EAAS,MAAA;EACT,EAAA,EAAI,WAAA,CAAY,WAAA,CAAY,OAAA,GAAU,WAAA,CAAY,EAAA;EAClD,IAAA,EAAM,WAAA,CAAY,WAAA,CAAY,OAAA,GAAU,WAAA,CAAY,IAAA;AAAA;AAAA,UAGrC,cAAA;EACf,UAAA,EAAY,cAAA;EACZ,MAAA;EACA,YAAA,GAAe,UAAA;AAAA;AAAA,cAGJ,QAAA,sBAA8B,eAAA,sBAAqC,eAAA;EAC9E,OAAA,EAAS,MAAA;EAET,UAAA,EAAY,MAAA,CAAO,MAAA,EAAQ,cAAA;EAE3B,MAAA;EACA,YAAA,EAAc,WAAA;cAEF,MAAA,EAAQ,cAAA,CAAe,YAAA,EAAc,WAAA;EAYjD,WAAA,CAAA;EAKM,OAAA,CACJ,MAAA,EAAQ,eAAA,cACR,OAAA,GAAU,cAAA,GACT,OAAA,CAAQ,WAAA;EAiEX,SAAA,CAAU,OAAA,EAAS,MAAA,GAAS,MAAA;EAI5B,aAAA,CAAc,OAAA,EAAS,MAAA;AAAA;;;iBC/IT,eAAA,cACD,eAAA,kBACG,eAAA,aACL,eAAA,CAAA,CACX,SAAA,EAAW,iBAAA,CAAkB,IAAA,EAAM,OAAA,EAAS,EAAA,IAAM,cAAA,CAAe,IAAA,EAAM,OAAA,EAAS,EAAA;AAAA,cAgBrE,cAAA;AAAA,cAIA,YAAA;AD3Bb;;;;;;AAAA,iBCyCgB,OAAA,iBAAwB,eAAA,aAA4B,eAAA,CAAA,CAClE,OAAA,EAAS,MAAA,EACT,SAAA,EAAW,WAAA,CAAY,WAAA,CAAY,OAAA,GAAU,WAAA,CAAY,EAAA,KACxD,cAAA,CAAe,OAAA,EAAS,OAAA,EAAS,EAAA;AAAA,iBAUpB,KAAA,iBAAsB,eAAA,eAA8B,eAAA,CAAA,CAClE,OAAA,EAAS,MAAA,EACT,SAAA,EAAW,WAAA,CAAY,WAAA,CAAY,OAAA,GAAU,WAAA,CAAY,IAAA,KACxD,cAAA,CAAe,IAAA,EAAM,OAAA,EAAS,OAAA;;;cC7DpB,cAAA,wBACU,eAAA,qBACF,eAAA;8BAGW,UAAA,GAAa,UAAA;2BAI/B,cAAA,CAAe,YAAA,EAAc,UAAA,IACpC,QAAA,CAAS,YAAA,EAAc,UAAA;AAAA;AAAA,KAMlB,eAAA,sBACW,eAAA,qBACF,eAAA,IACjB,UAAA,QAAkB,cAAA,CAAe,YAAA,EAAc,UAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import * as semver from "semver";
|
|
2
|
+
import { coerce } from "semver";
|
|
3
|
+
import { klona } from "klona";
|
|
4
|
+
import { custom, object } from "valibot";
|
|
5
|
+
//#region src/models/createMigration.ts
|
|
6
|
+
function createMigration(migration) {
|
|
7
|
+
return {
|
|
8
|
+
version: migration.version,
|
|
9
|
+
up: async (state, nextVersion) => {
|
|
10
|
+
const newState = await migration.up(state, nextVersion);
|
|
11
|
+
newState.version = nextVersion;
|
|
12
|
+
return newState;
|
|
13
|
+
},
|
|
14
|
+
down: async (state, nextVersion) => {
|
|
15
|
+
const newState = await migration.down(state, nextVersion);
|
|
16
|
+
newState.version = nextVersion;
|
|
17
|
+
return newState;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const initialVersion = () => {
|
|
22
|
+
throw new Error("Unable to go down on the initial version!");
|
|
23
|
+
};
|
|
24
|
+
const finalVersion = () => {
|
|
25
|
+
throw new Error("Unable to go up on the final version!");
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* @deprecated
|
|
29
|
+
* @param version
|
|
30
|
+
* @param migration
|
|
31
|
+
* @returns
|
|
32
|
+
*/
|
|
33
|
+
function initial(version, migration) {
|
|
34
|
+
return createMigration({
|
|
35
|
+
version,
|
|
36
|
+
up: migration,
|
|
37
|
+
down() {
|
|
38
|
+
throw new Error("Unable to go down on the initial version!");
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function final(version, migration) {
|
|
43
|
+
return createMigration({
|
|
44
|
+
version,
|
|
45
|
+
up: () => {
|
|
46
|
+
throw new Error(`Unable to go up on the final version "${version}"!`);
|
|
47
|
+
},
|
|
48
|
+
down: migration
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/utils/object-keys.ts
|
|
53
|
+
const objectKeys = Object.keys;
|
|
54
|
+
object({ version: custom((input) => typeof input === "string" ? /^\d+\.\d+\.\d+$/.test(input) : false) });
|
|
55
|
+
const createVersionSchema = (schema) => object({ ...schema });
|
|
56
|
+
var Migrator = class {
|
|
57
|
+
current;
|
|
58
|
+
migrations = {};
|
|
59
|
+
coerce;
|
|
60
|
+
defaultValue;
|
|
61
|
+
constructor(config) {
|
|
62
|
+
config.migrations.forEach((migration) => {
|
|
63
|
+
this.migrations[migration.version] = migration;
|
|
64
|
+
});
|
|
65
|
+
this.defaultValue = config?.defaultValue ?? {};
|
|
66
|
+
const versions = this.getVersions();
|
|
67
|
+
this.current = versions[versions.length - 1];
|
|
68
|
+
this.coerce = config?.coerce ?? true;
|
|
69
|
+
}
|
|
70
|
+
getVersions() {
|
|
71
|
+
const keys = objectKeys(this.migrations);
|
|
72
|
+
return semver.sort(keys);
|
|
73
|
+
}
|
|
74
|
+
async migrate(_state, options) {
|
|
75
|
+
const state = _state ?? this.defaultValue;
|
|
76
|
+
const currentVersion = this.tryCoerce(state.version);
|
|
77
|
+
const targetVersion = this.tryCoerce(options?.target ?? this.current);
|
|
78
|
+
if (options?.debug) console.log("Migrating from", currentVersion, "to", targetVersion);
|
|
79
|
+
let finalState = klona(state);
|
|
80
|
+
const versions = this.getVersions();
|
|
81
|
+
const currentIndex = versions.indexOf(currentVersion);
|
|
82
|
+
const targetIndex = versions.indexOf(targetVersion);
|
|
83
|
+
if (currentIndex === -1) throw new Error(`Current version "${currentVersion}" not found in migrations`);
|
|
84
|
+
if (targetIndex === -1) throw new Error(`Target version "${targetVersion}" not found in migrations`);
|
|
85
|
+
if (currentIndex === targetIndex) return finalState;
|
|
86
|
+
const isUpgrade = currentIndex < targetIndex;
|
|
87
|
+
const increment = isUpgrade ? 1 : -1;
|
|
88
|
+
const direction = isUpgrade ? "up" : "down";
|
|
89
|
+
for (let i = currentIndex; isUpgrade ? i < targetIndex : i > targetIndex; i += increment) {
|
|
90
|
+
const currentVersion = versions[i];
|
|
91
|
+
const nextVersion = versions[i + increment];
|
|
92
|
+
if (options?.debug) console.log(" Migrating to version:", nextVersion);
|
|
93
|
+
const migrationVersion = currentVersion;
|
|
94
|
+
const migration = this.migrations[migrationVersion];
|
|
95
|
+
const { version: _, ...stateWithoutVersion } = finalState;
|
|
96
|
+
finalState = {
|
|
97
|
+
...await migration[direction](stateWithoutVersion, currentVersion),
|
|
98
|
+
version: nextVersion
|
|
99
|
+
};
|
|
100
|
+
if (options?.debug) console.log(" Migrated state:", finalState);
|
|
101
|
+
}
|
|
102
|
+
return finalState;
|
|
103
|
+
}
|
|
104
|
+
tryCoerce(version) {
|
|
105
|
+
return this.coerce ? coerce(version)?.version ?? version : version;
|
|
106
|
+
}
|
|
107
|
+
needMigration(version) {
|
|
108
|
+
const newVersion = this.tryCoerce(version);
|
|
109
|
+
return semver.lt(newVersion, this.current);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/models/createMigrator.ts
|
|
114
|
+
const createMigrator = () => {
|
|
115
|
+
return {
|
|
116
|
+
createDefault(defaultState) {
|
|
117
|
+
return defaultState;
|
|
118
|
+
},
|
|
119
|
+
createMigrations(config) {
|
|
120
|
+
return new Migrator(config);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
//#endregion
|
|
125
|
+
export { createMigration, createMigrator, createVersionSchema, final, finalVersion, initial, initialVersion };
|
|
126
|
+
|
|
127
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/models/createMigration.ts","../src/utils/object-keys.ts","../src/models/migration.ts","../src/models/createMigrator.ts"],"sourcesContent":["import {\n MigrationFn,\n MigrationObjInput,\n MigrationClass,\n MigrationSchema,\n OmitVersion,\n SemVer,\n} from \"./migration\";\n\nexport function createMigration<\n Down extends MigrationSchema,\n Current extends MigrationSchema,\n Up extends MigrationSchema,\n>(migration: MigrationObjInput<Down, Current, Up>): MigrationClass<Down, Current, Up> {\n return {\n version: migration.version,\n up: async (state, nextVersion) => {\n const newState = (await migration.up(state, nextVersion)) as unknown as Up;\n newState.version = nextVersion as SemVer;\n return newState;\n },\n down: async (state, nextVersion) => {\n const newState = (await migration.down(state, nextVersion)) as unknown as Down;\n newState.version = nextVersion as SemVer;\n return newState;\n },\n };\n}\n\nexport const initialVersion = () => {\n throw new Error(\"Unable to go down on the initial version!\");\n};\n\nexport const finalVersion = () => {\n throw new Error(\"Unable to go up on the final version!\");\n};\n\n// export const finalVersion = <T>(state: T) => {\n// return state\n// }\n\n/**\n * @deprecated\n * @param version\n * @param migration\n * @returns\n */\nexport function initial<Current extends MigrationSchema, Up extends MigrationSchema>(\n version: SemVer,\n migration: MigrationFn<OmitVersion<Current>, OmitVersion<Up>>,\n): MigrationClass<Current, Current, Up> {\n return createMigration({\n version,\n up: migration,\n down() {\n throw new Error(\"Unable to go down on the initial version!\");\n },\n });\n}\n\nexport function final<Current extends MigrationSchema, Down extends MigrationSchema>(\n version: SemVer,\n migration: MigrationFn<OmitVersion<Current>, OmitVersion<Down>>,\n): MigrationClass<Down, Current, Current> {\n return createMigration({\n version,\n up: () => {\n throw new Error(`Unable to go up on the final version \"${version}\"!`);\n },\n down: migration,\n });\n}\n","export type ObjectKeys<T extends object> = `${Exclude<keyof T, symbol>}`;\n\nexport const objectKeys = Object.keys as <Type extends object>(\n value: Type,\n) => Array<ObjectKeys<Type>>;\nexport const objectEntries = Object.entries as <Type extends Record<PropertyKey, unknown>>(\n value: Type,\n) => Array<[ObjectKeys<Type>, Type[ObjectKeys<Type>]]>;\n","import * as semver from \"semver\";\nimport { coerce } from \"semver\";\nimport { objectKeys } from \"../utils/object-keys\";\nimport { klona } from \"klona\";\nimport { custom, InferOutput, object, ObjectEntries } from \"valibot\";\n\nexport type Awaitable<T> = Promise<T> | T;\n\nexport type MigrationFn<From, To> = (state: From, targetVersion: string) => Awaitable<To>;\n\nexport type MigrateOptions = {\n debug?: boolean;\n target?: SemVer;\n};\n\nconst SemverValidator = custom<SemVer>((input) =>\n typeof input === \"string\" ? /^\\d+\\.\\d+\\.\\d+$/.test(input) : false,\n);\n\nexport const SemverVersionValidator = SemverValidator;\n// export const SemverVersionValidator = optional(SemverValidator, '1.0.0')\n\nexport const MigrationSchemaValidator = object({\n version: SemverVersionValidator,\n});\n\nexport const createVersionSchema = <OBJECTENTRIES extends ObjectEntries>(schema: OBJECTENTRIES) =>\n object({\n ...schema,\n });\n\nexport type MigrationSchema = InferOutput<typeof MigrationSchemaValidator>;\n\nexport type OmitVersion<T> = Omit<T, keyof MigrationSchema>;\n\nexport type SemVer = `${number}.${number}.${number}`;\n\nexport interface MigrationClass<Down, Current, Up> {\n version: SemVer;\n up: MigrationFn<Current, Up>;\n down: MigrationFn<Current, Down>;\n}\n\nexport interface MigrationObjInput<Down, Current, Up> {\n version: SemVer;\n up: MigrationFn<OmitVersion<Current>, OmitVersion<Up>>;\n down: MigrationFn<OmitVersion<Current>, OmitVersion<Down>>;\n}\n\nexport interface MigratorConfig<InitialState, FinalState> {\n migrations: MigrationClass<any, any, any>[];\n coerce?: boolean;\n defaultValue?: FinalState;\n}\n\nexport class Migrator<InitialState extends MigrationSchema, OutputState extends MigrationSchema> {\n current: SemVer;\n\n migrations: Record<SemVer, MigrationClass<any, any, any>> = {};\n\n coerce: boolean;\n defaultValue: OutputState;\n\n constructor(config: MigratorConfig<InitialState, OutputState>) {\n config.migrations.forEach((migration) => {\n this.migrations[migration.version] = migration;\n });\n\n this.defaultValue = config?.defaultValue ?? ({} as OutputState);\n\n const versions = this.getVersions();\n this.current = versions[versions.length - 1];\n this.coerce = config?.coerce ?? true;\n }\n\n getVersions() {\n const keys = objectKeys(this.migrations);\n return semver.sort(keys);\n }\n\n async migrate(\n _state: MigrationSchema | undefined,\n options?: MigrateOptions,\n ): Promise<OutputState> {\n const state = _state ?? this.defaultValue;\n const currentVersion = this.tryCoerce(state.version);\n const targetVersion = this.tryCoerce(options?.target ?? this.current);\n\n if (options?.debug) {\n console.log(\"Migrating from\", currentVersion, \"to\", targetVersion);\n }\n\n let finalState = klona(state) as any; // Using any here because we'll be transforming between versions\n const versions = this.getVersions();\n const currentIndex = versions.indexOf(currentVersion);\n const targetIndex = versions.indexOf(targetVersion);\n\n if (currentIndex === -1) {\n throw new Error(`Current version \"${currentVersion}\" not found in migrations`);\n }\n if (targetIndex === -1) {\n throw new Error(`Target version \"${targetVersion}\" not found in migrations`);\n }\n\n // If we're already at the target version, return the state\n if (currentIndex === targetIndex) {\n return finalState as OutputState;\n }\n\n // Get the migration path\n const isUpgrade = currentIndex < targetIndex;\n const increment = isUpgrade ? 1 : -1;\n const direction = isUpgrade ? \"up\" : \"down\";\n\n // Perform migrations\n for (let i = currentIndex; isUpgrade ? i < targetIndex : i > targetIndex; i += increment) {\n const currentVersion = versions[i];\n const nextVersion = versions[i + increment];\n\n if (options?.debug) {\n console.log(\"\\tMigrating to version:\", nextVersion);\n }\n\n // For upgrades, use current version's migration\n // For downgrades, use previous version's migration\n const migrationVersion = currentVersion;\n const migration = this.migrations[migrationVersion];\n\n // Remove version before migration\n const { version: _, ...stateWithoutVersion } = finalState;\n\n // Perform migration\n const migratedState = await migration[direction](stateWithoutVersion, currentVersion);\n\n // Add new version\n finalState = {\n ...migratedState,\n version: nextVersion,\n };\n\n if (options?.debug) {\n console.log(\"\\tMigrated state:\", finalState);\n }\n }\n\n return finalState as OutputState;\n }\n\n tryCoerce(version: SemVer): SemVer {\n return this.coerce ? ((coerce(version)?.version as SemVer | undefined) ?? version) : version;\n }\n\n needMigration(version: SemVer) {\n const newVersion = this.tryCoerce(version);\n return semver.lt(newVersion, this.current);\n }\n}\n","import { MigratorConfig, Migrator, MigrationSchema } from \"./migration\";\n\nexport const createMigrator = <\n InitialState extends MigrationSchema,\n FinalState extends MigrationSchema,\n>() => {\n return {\n createDefault(defaultState: FinalState): FinalState {\n return defaultState;\n },\n createMigrations(\n config: MigratorConfig<InitialState, FinalState>,\n ): Migrator<InitialState, FinalState> {\n return new Migrator<InitialState, FinalState>(config);\n },\n };\n};\n\nexport type MigratorFactory<\n InitialState extends MigrationSchema,\n FinalState extends MigrationSchema,\n> = ReturnType<typeof createMigrator<InitialState, FinalState>>;\n"],"mappings":";;;;;AASA,SAAgB,gBAId,WAAoF;AACpF,QAAO;EACL,SAAS,UAAU;EACnB,IAAI,OAAO,OAAO,gBAAgB;GAChC,MAAM,WAAY,MAAM,UAAU,GAAG,OAAO,YAAY;AACxD,YAAS,UAAU;AACnB,UAAO;;EAET,MAAM,OAAO,OAAO,gBAAgB;GAClC,MAAM,WAAY,MAAM,UAAU,KAAK,OAAO,YAAY;AAC1D,YAAS,UAAU;AACnB,UAAO;;EAEV;;AAGH,MAAa,uBAAuB;AAClC,OAAM,IAAI,MAAM,4CAA4C;;AAG9D,MAAa,qBAAqB;AAChC,OAAM,IAAI,MAAM,wCAAwC;;;;;;;;AAa1D,SAAgB,QACd,SACA,WACsC;AACtC,QAAO,gBAAgB;EACrB;EACA,IAAI;EACJ,OAAO;AACL,SAAM,IAAI,MAAM,4CAA4C;;EAE/D,CAAC;;AAGJ,SAAgB,MACd,SACA,WACwC;AACxC,QAAO,gBAAgB;EACrB;EACA,UAAU;AACR,SAAM,IAAI,MAAM,yCAAyC,QAAQ,IAAI;;EAEvE,MAAM;EACP,CAAC;;;;ACpEJ,MAAa,aAAa,OAAO;ACoBO,OAAO,EAC7C,SARsB,QAAgB,UACtC,OAAO,UAAU,WAAW,kBAAkB,KAAK,MAAM,GAAG,MAC7D,EAOA,CAAC;AAEF,MAAa,uBAA4D,WACvE,OAAO,EACL,GAAG,QACJ,CAAC;AA0BJ,IAAa,WAAb,MAAiG;CAC/F;CAEA,aAA4D,EAAE;CAE9D;CACA;CAEA,YAAY,QAAmD;AAC7D,SAAO,WAAW,SAAS,cAAc;AACvC,QAAK,WAAW,UAAU,WAAW;IACrC;AAEF,OAAK,eAAe,QAAQ,gBAAiB,EAAE;EAE/C,MAAM,WAAW,KAAK,aAAa;AACnC,OAAK,UAAU,SAAS,SAAS,SAAS;AAC1C,OAAK,SAAS,QAAQ,UAAU;;CAGlC,cAAc;EACZ,MAAM,OAAO,WAAW,KAAK,WAAW;AACxC,SAAO,OAAO,KAAK,KAAK;;CAG1B,MAAM,QACJ,QACA,SACsB;EACtB,MAAM,QAAQ,UAAU,KAAK;EAC7B,MAAM,iBAAiB,KAAK,UAAU,MAAM,QAAQ;EACpD,MAAM,gBAAgB,KAAK,UAAU,SAAS,UAAU,KAAK,QAAQ;AAErE,MAAI,SAAS,MACX,SAAQ,IAAI,kBAAkB,gBAAgB,MAAM,cAAc;EAGpE,IAAI,aAAa,MAAM,MAAM;EAC7B,MAAM,WAAW,KAAK,aAAa;EACnC,MAAM,eAAe,SAAS,QAAQ,eAAe;EACrD,MAAM,cAAc,SAAS,QAAQ,cAAc;AAEnD,MAAI,iBAAiB,GACnB,OAAM,IAAI,MAAM,oBAAoB,eAAe,2BAA2B;AAEhF,MAAI,gBAAgB,GAClB,OAAM,IAAI,MAAM,mBAAmB,cAAc,2BAA2B;AAI9E,MAAI,iBAAiB,YACnB,QAAO;EAIT,MAAM,YAAY,eAAe;EACjC,MAAM,YAAY,YAAY,IAAI;EAClC,MAAM,YAAY,YAAY,OAAO;AAGrC,OAAK,IAAI,IAAI,cAAc,YAAY,IAAI,cAAc,IAAI,aAAa,KAAK,WAAW;GACxF,MAAM,iBAAiB,SAAS;GAChC,MAAM,cAAc,SAAS,IAAI;AAEjC,OAAI,SAAS,MACX,SAAQ,IAAI,0BAA2B,YAAY;GAKrD,MAAM,mBAAmB;GACzB,MAAM,YAAY,KAAK,WAAW;GAGlC,MAAM,EAAE,SAAS,GAAG,GAAG,wBAAwB;AAM/C,gBAAa;IACX,GAJoB,MAAM,UAAU,WAAW,qBAAqB,eAAe;IAKnF,SAAS;IACV;AAED,OAAI,SAAS,MACX,SAAQ,IAAI,oBAAqB,WAAW;;AAIhD,SAAO;;CAGT,UAAU,SAAyB;AACjC,SAAO,KAAK,SAAW,OAAO,QAAQ,EAAE,WAAkC,UAAW;;CAGvF,cAAc,SAAiB;EAC7B,MAAM,aAAa,KAAK,UAAU,QAAQ;AAC1C,SAAO,OAAO,GAAG,YAAY,KAAK,QAAQ;;;;;ACxJ9C,MAAa,uBAGN;AACL,QAAO;EACL,cAAc,cAAsC;AAClD,UAAO;;EAET,iBACE,QACoC;AACpC,UAAO,IAAI,SAAmC,OAAO;;EAExD"}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pipelab/migration",
|
|
3
|
+
"version": "1.0.1-beta.19",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Migration utilities for Pipelab data structures",
|
|
6
|
+
"license": "FSL-1.1-MIT",
|
|
7
|
+
"author": "CynToolkit",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/CynToolkit/pipelab.git",
|
|
11
|
+
"directory": "packages/migration"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "./dist/index.cjs",
|
|
15
|
+
"module": "./dist/index.mjs",
|
|
16
|
+
"types": "./dist/index.d.mts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.mts",
|
|
20
|
+
"import": "./dist/index.mjs",
|
|
21
|
+
"require": "./dist/index.cjs",
|
|
22
|
+
"default": "./dist/index.mjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@types/node": "^24.12.2",
|
|
30
|
+
"klona": "2.0.6",
|
|
31
|
+
"semver": "^7.6.3",
|
|
32
|
+
"valibot": "0.42.1"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/semver": "^7.5.8",
|
|
36
|
+
"tsdown": "0.21.2",
|
|
37
|
+
"typescript": "5.9.3",
|
|
38
|
+
"@pipelab/tsconfig": "1.0.1-beta.19"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsdown",
|
|
42
|
+
"format": "oxfmt .",
|
|
43
|
+
"lint": "oxlint ."
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from "./models/createMigration";
|
|
2
|
+
export * from "./models/createMigrator";
|
|
3
|
+
export type {
|
|
4
|
+
MigrationSchema,
|
|
5
|
+
Migrator,
|
|
6
|
+
MigratorConfig,
|
|
7
|
+
MigrationFn,
|
|
8
|
+
SemVer,
|
|
9
|
+
OmitVersion,
|
|
10
|
+
} from "./models/migration";
|
|
11
|
+
export { createVersionSchema } from "./models/migration";
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MigrationFn,
|
|
3
|
+
MigrationObjInput,
|
|
4
|
+
MigrationClass,
|
|
5
|
+
MigrationSchema,
|
|
6
|
+
OmitVersion,
|
|
7
|
+
SemVer,
|
|
8
|
+
} from "./migration";
|
|
9
|
+
|
|
10
|
+
export function createMigration<
|
|
11
|
+
Down extends MigrationSchema,
|
|
12
|
+
Current extends MigrationSchema,
|
|
13
|
+
Up extends MigrationSchema,
|
|
14
|
+
>(migration: MigrationObjInput<Down, Current, Up>): MigrationClass<Down, Current, Up> {
|
|
15
|
+
return {
|
|
16
|
+
version: migration.version,
|
|
17
|
+
up: async (state, nextVersion) => {
|
|
18
|
+
const newState = (await migration.up(state, nextVersion)) as unknown as Up;
|
|
19
|
+
newState.version = nextVersion as SemVer;
|
|
20
|
+
return newState;
|
|
21
|
+
},
|
|
22
|
+
down: async (state, nextVersion) => {
|
|
23
|
+
const newState = (await migration.down(state, nextVersion)) as unknown as Down;
|
|
24
|
+
newState.version = nextVersion as SemVer;
|
|
25
|
+
return newState;
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const initialVersion = () => {
|
|
31
|
+
throw new Error("Unable to go down on the initial version!");
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const finalVersion = () => {
|
|
35
|
+
throw new Error("Unable to go up on the final version!");
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// export const finalVersion = <T>(state: T) => {
|
|
39
|
+
// return state
|
|
40
|
+
// }
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @deprecated
|
|
44
|
+
* @param version
|
|
45
|
+
* @param migration
|
|
46
|
+
* @returns
|
|
47
|
+
*/
|
|
48
|
+
export function initial<Current extends MigrationSchema, Up extends MigrationSchema>(
|
|
49
|
+
version: SemVer,
|
|
50
|
+
migration: MigrationFn<OmitVersion<Current>, OmitVersion<Up>>,
|
|
51
|
+
): MigrationClass<Current, Current, Up> {
|
|
52
|
+
return createMigration({
|
|
53
|
+
version,
|
|
54
|
+
up: migration,
|
|
55
|
+
down() {
|
|
56
|
+
throw new Error("Unable to go down on the initial version!");
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function final<Current extends MigrationSchema, Down extends MigrationSchema>(
|
|
62
|
+
version: SemVer,
|
|
63
|
+
migration: MigrationFn<OmitVersion<Current>, OmitVersion<Down>>,
|
|
64
|
+
): MigrationClass<Down, Current, Current> {
|
|
65
|
+
return createMigration({
|
|
66
|
+
version,
|
|
67
|
+
up: () => {
|
|
68
|
+
throw new Error(`Unable to go up on the final version "${version}"!`);
|
|
69
|
+
},
|
|
70
|
+
down: migration,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { MigratorConfig, Migrator, MigrationSchema } from "./migration";
|
|
2
|
+
|
|
3
|
+
export const createMigrator = <
|
|
4
|
+
InitialState extends MigrationSchema,
|
|
5
|
+
FinalState extends MigrationSchema,
|
|
6
|
+
>() => {
|
|
7
|
+
return {
|
|
8
|
+
createDefault(defaultState: FinalState): FinalState {
|
|
9
|
+
return defaultState;
|
|
10
|
+
},
|
|
11
|
+
createMigrations(
|
|
12
|
+
config: MigratorConfig<InitialState, FinalState>,
|
|
13
|
+
): Migrator<InitialState, FinalState> {
|
|
14
|
+
return new Migrator<InitialState, FinalState>(config);
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type MigratorFactory<
|
|
20
|
+
InitialState extends MigrationSchema,
|
|
21
|
+
FinalState extends MigrationSchema,
|
|
22
|
+
> = ReturnType<typeof createMigrator<InitialState, FinalState>>;
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import { describe, test, expect, beforeAll, beforeEach, afterEach, vi } from "vitest";
|
|
2
|
+
import { createMigration, final, finalVersion, initial, initialVersion } from "./createMigration";
|
|
3
|
+
import { createMigrator, MigratorFactory } from "./createMigrator";
|
|
4
|
+
import { Migrator, MigrationSchema } from "./migration";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
|
|
7
|
+
export const withMigration = (obj: z.AnyZodObject) =>
|
|
8
|
+
z
|
|
9
|
+
.object({
|
|
10
|
+
version: z.string(),
|
|
11
|
+
})
|
|
12
|
+
.merge(obj);
|
|
13
|
+
|
|
14
|
+
interface V1 extends MigrationSchema {
|
|
15
|
+
dummyV1: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface V2 extends MigrationSchema {
|
|
19
|
+
dummyV2: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface V3 extends MigrationSchema {
|
|
23
|
+
dummyV3: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface V4 extends MigrationSchema {
|
|
27
|
+
dummy: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const outputV1: V1 = {
|
|
31
|
+
dummyV1: "aaa",
|
|
32
|
+
version: "1.0.0",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const outputV2: V2 = {
|
|
36
|
+
dummyV2: "aaa",
|
|
37
|
+
version: "2.0.0",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const outputV3: V3 = {
|
|
41
|
+
dummyV3: "aaa",
|
|
42
|
+
version: "3.0.0",
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const outputV4: V4 = {
|
|
46
|
+
dummy: "aaa",
|
|
47
|
+
version: "4.0.0",
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
51
|
+
|
|
52
|
+
describe("migrator", () => {
|
|
53
|
+
let migratorInstance: MigratorFactory<V1, V4>;
|
|
54
|
+
let migratorMigrations: Migrator<V1, V4>;
|
|
55
|
+
beforeAll(() => {
|
|
56
|
+
// migratorInstance = createMigrator<V4>({
|
|
57
|
+
// migrations: [
|
|
58
|
+
// initial<V1, V2>('1.0.0', (state) => ({
|
|
59
|
+
// dummyV2: state.dummyV1,
|
|
60
|
+
// })),
|
|
61
|
+
// createMigration<V1, V2, V3>({
|
|
62
|
+
// version: '2.0.0',
|
|
63
|
+
// up: (state) => ({
|
|
64
|
+
// dummyV3: state.dummyV2,
|
|
65
|
+
// }),
|
|
66
|
+
// down: (state) => ({
|
|
67
|
+
// dummyV1: state.dummyV2,
|
|
68
|
+
// }),
|
|
69
|
+
// }),
|
|
70
|
+
// createMigration<V2, V3, V4>({
|
|
71
|
+
// version: '3.0.0',
|
|
72
|
+
// up: (state) => ({
|
|
73
|
+
// dummy: state.dummyV3,
|
|
74
|
+
// }),
|
|
75
|
+
// down: (state) => ({
|
|
76
|
+
// dummyV2: state.dummyV3,
|
|
77
|
+
// }),
|
|
78
|
+
// }),
|
|
79
|
+
// final<V4, V3>('4.0.0', (state) => ({
|
|
80
|
+
// dummyV3: state.dummy,
|
|
81
|
+
// })),
|
|
82
|
+
// ],
|
|
83
|
+
// });
|
|
84
|
+
|
|
85
|
+
migratorInstance = createMigrator<V1, V4>();
|
|
86
|
+
migratorMigrations = migratorInstance.createMigrations({
|
|
87
|
+
defaultValue: outputV1,
|
|
88
|
+
migrations: [
|
|
89
|
+
createMigration<never, V1, V2>({
|
|
90
|
+
version: "1.0.0",
|
|
91
|
+
up: (state) => ({
|
|
92
|
+
dummyV2: state.dummyV1,
|
|
93
|
+
}),
|
|
94
|
+
down: initialVersion,
|
|
95
|
+
}),
|
|
96
|
+
createMigration<V1, V2, V3>({
|
|
97
|
+
version: "2.0.0",
|
|
98
|
+
up: (state) => ({
|
|
99
|
+
dummyV3: state.dummyV2,
|
|
100
|
+
}),
|
|
101
|
+
down: (state) => ({
|
|
102
|
+
dummyV1: state.dummyV2,
|
|
103
|
+
}),
|
|
104
|
+
}),
|
|
105
|
+
createMigration<V2, V3, V4>({
|
|
106
|
+
version: "3.0.0",
|
|
107
|
+
up: (state) => ({
|
|
108
|
+
dummy: state.dummyV3,
|
|
109
|
+
}),
|
|
110
|
+
down: (state) => ({
|
|
111
|
+
dummyV2: state.dummyV3,
|
|
112
|
+
}),
|
|
113
|
+
}),
|
|
114
|
+
createMigration<V3, V4, never>({
|
|
115
|
+
version: "4.0.0",
|
|
116
|
+
up: finalVersion,
|
|
117
|
+
down: (state) => ({
|
|
118
|
+
dummyV3: state.dummy,
|
|
119
|
+
}),
|
|
120
|
+
}),
|
|
121
|
+
],
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("should not need migration", () => {
|
|
126
|
+
expect(migratorMigrations.needMigration("4.0.0")).toBe(false);
|
|
127
|
+
expect(migratorMigrations.needMigration("5.0.0")).toBe(false);
|
|
128
|
+
expect(migratorMigrations.needMigration("4.1.0")).toBe(false);
|
|
129
|
+
expect(migratorMigrations.needMigration("4.0.1")).toBe(false);
|
|
130
|
+
expect(migratorMigrations.needMigration("5.0.1")).toBe(false);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("should need migration", () => {
|
|
134
|
+
expect(migratorMigrations.needMigration("0.0.0")).toBe(true);
|
|
135
|
+
expect(migratorMigrations.needMigration("0.1.0")).toBe(true);
|
|
136
|
+
expect(migratorMigrations.needMigration("0.0.1")).toBe(true);
|
|
137
|
+
expect(migratorMigrations.needMigration("1.0.1")).toBe(true);
|
|
138
|
+
expect(migratorMigrations.needMigration("2.3.1")).toBe(true);
|
|
139
|
+
expect(migratorMigrations.needMigration("3.4.1")).toBe(true);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("should migrate", async () => {
|
|
143
|
+
const input: V1 = {
|
|
144
|
+
dummyV1: "aaa",
|
|
145
|
+
version: "1.0.0",
|
|
146
|
+
};
|
|
147
|
+
const result = await migratorMigrations.migrate(input);
|
|
148
|
+
expect(result).toStrictEqual(outputV4);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("should return instance if same target version", async () => {
|
|
152
|
+
const input: V4 = {
|
|
153
|
+
dummy: "aaa",
|
|
154
|
+
version: "4.0.0",
|
|
155
|
+
};
|
|
156
|
+
const result = await migratorMigrations.migrate(input);
|
|
157
|
+
expect(result).toStrictEqual(outputV4);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("should not do anything if migrate up but nothing more", () => {
|
|
161
|
+
const input: V4 = {
|
|
162
|
+
dummy: "aaa",
|
|
163
|
+
version: "4.0.0",
|
|
164
|
+
};
|
|
165
|
+
return expect(migratorMigrations.migrate(input)).resolves.toStrictEqual(outputV4);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test("should downgrade", async () => {
|
|
169
|
+
const input: V4 = {
|
|
170
|
+
dummy: "aaa",
|
|
171
|
+
version: "4.0.0",
|
|
172
|
+
};
|
|
173
|
+
const result = await migratorMigrations.migrate(input);
|
|
174
|
+
expect(result).toEqual(outputV4);
|
|
175
|
+
const resultDown = await migratorMigrations.migrate(result, {
|
|
176
|
+
target: "3.0.0",
|
|
177
|
+
});
|
|
178
|
+
expect(resultDown).toEqual(outputV3);
|
|
179
|
+
const resultDown2 = await migratorMigrations.migrate(resultDown, {
|
|
180
|
+
target: "2.0.0",
|
|
181
|
+
});
|
|
182
|
+
expect(resultDown2).toEqual(outputV2);
|
|
183
|
+
const resultUp = await migratorMigrations.migrate(resultDown2, {
|
|
184
|
+
target: "3.0.0",
|
|
185
|
+
});
|
|
186
|
+
expect(resultUp).toEqual(outputV3);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("should throw if target is not in migrations", async () => {
|
|
190
|
+
const input: V4 = {
|
|
191
|
+
dummy: "aaa",
|
|
192
|
+
version: "1.2.0",
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const badMigratorInstance = createMigrator<V1, V4>().createMigrations({
|
|
196
|
+
migrations: [
|
|
197
|
+
initial<V1, V2>("1.0.0", (state) => ({
|
|
198
|
+
dummyV2: state.dummyV1,
|
|
199
|
+
})),
|
|
200
|
+
final<V4, V3>("4.0.0", (state) => ({
|
|
201
|
+
dummyV3: state.dummy,
|
|
202
|
+
})),
|
|
203
|
+
],
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
return expect(badMigratorInstance.migrate(input)).rejects.toThrow();
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test("should throw on migration not found", async () => {
|
|
210
|
+
const input: V4 = {
|
|
211
|
+
dummy: "aaa",
|
|
212
|
+
version: "1.2.0",
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const badMigratorInstance = createMigrator<V1, V4>().createMigrations({
|
|
216
|
+
migrations: [
|
|
217
|
+
initial<V1, V2>("1.0.0", (state) => ({
|
|
218
|
+
dummyV2: state.dummyV1,
|
|
219
|
+
})),
|
|
220
|
+
final<V4, V3>("4.0.0", (state) => ({
|
|
221
|
+
dummyV3: state.dummy,
|
|
222
|
+
})),
|
|
223
|
+
],
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
await expect(badMigratorInstance.migrate(input)).rejects.toThrow();
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test("should migrate an incomplete version (loose)", async () => {
|
|
230
|
+
const input: V1 = {
|
|
231
|
+
dummyV1: "aaa",
|
|
232
|
+
// @ts-expect-error specific to the test
|
|
233
|
+
version: "1",
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const migrator = createMigrator<V1, V4>().createMigrations({
|
|
237
|
+
migrations: [
|
|
238
|
+
createMigration<never, V1, V2>({
|
|
239
|
+
version: "1.0.0",
|
|
240
|
+
up: (state) => ({
|
|
241
|
+
dummyV2: state.dummyV1,
|
|
242
|
+
}),
|
|
243
|
+
down: initialVersion,
|
|
244
|
+
}),
|
|
245
|
+
createMigration<V1, V2, never>({
|
|
246
|
+
version: "2.0.0",
|
|
247
|
+
up: finalVersion,
|
|
248
|
+
down: (state) => ({
|
|
249
|
+
dummyV1: state.dummyV2,
|
|
250
|
+
}),
|
|
251
|
+
}),
|
|
252
|
+
],
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
const result = await migrator.migrate(input);
|
|
256
|
+
|
|
257
|
+
expect(result).toEqual(expect.objectContaining({ version: "2.0.0" }));
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test("should not migrate an incomplete version when coerce option disabled", async () => {
|
|
261
|
+
const input: V1 = {
|
|
262
|
+
dummyV1: "aaa",
|
|
263
|
+
// @ts-expect-error specific to the test
|
|
264
|
+
version: "1",
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
const migrator = createMigrator<V1, V2>().createMigrations({
|
|
268
|
+
migrations: [
|
|
269
|
+
createMigration<never, V1, V2>({
|
|
270
|
+
version: "1.0.0",
|
|
271
|
+
up: (state) => ({
|
|
272
|
+
dummyV2: state.dummyV1,
|
|
273
|
+
}),
|
|
274
|
+
down: initialVersion,
|
|
275
|
+
}),
|
|
276
|
+
createMigration<V1, V2, never>({
|
|
277
|
+
version: "2.0.0",
|
|
278
|
+
up: finalVersion,
|
|
279
|
+
down: (state) => ({
|
|
280
|
+
dummyV1: state.dummyV2,
|
|
281
|
+
}),
|
|
282
|
+
}),
|
|
283
|
+
],
|
|
284
|
+
coerce: false,
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
await expect(migrator.migrate(input)).rejects.toThrow(
|
|
288
|
+
'Current version "1" not found in migrations',
|
|
289
|
+
);
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
describe("async", () => {
|
|
294
|
+
beforeEach(() => {
|
|
295
|
+
vi.useFakeTimers();
|
|
296
|
+
});
|
|
297
|
+
afterEach(() => {
|
|
298
|
+
vi.restoreAllMocks();
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test("should support async migration", async () => {
|
|
302
|
+
const input: V1 = {
|
|
303
|
+
dummyV1: "aaa",
|
|
304
|
+
version: "1.0.0",
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
const migrator = createMigrator<V1, V2>().createMigrations({
|
|
308
|
+
migrations: [
|
|
309
|
+
createMigration<never, V1, V2>({
|
|
310
|
+
version: "1.0.0",
|
|
311
|
+
up: async (state) => {
|
|
312
|
+
await sleep(1000);
|
|
313
|
+
return {
|
|
314
|
+
dummyV2: state.dummyV1,
|
|
315
|
+
};
|
|
316
|
+
},
|
|
317
|
+
down: initialVersion,
|
|
318
|
+
}),
|
|
319
|
+
createMigration<V1, V2, never>({
|
|
320
|
+
version: "2.0.0",
|
|
321
|
+
up: finalVersion,
|
|
322
|
+
down: async (state) => {
|
|
323
|
+
await sleep(1000);
|
|
324
|
+
return {
|
|
325
|
+
dummyV1: state.dummyV2,
|
|
326
|
+
};
|
|
327
|
+
},
|
|
328
|
+
}),
|
|
329
|
+
],
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
const promise = migrator.migrate(input);
|
|
333
|
+
|
|
334
|
+
vi.advanceTimersByTime(1000);
|
|
335
|
+
|
|
336
|
+
const result = await promise;
|
|
337
|
+
|
|
338
|
+
expect(result).toStrictEqual({
|
|
339
|
+
dummyV2: "aaa",
|
|
340
|
+
version: "2.0.0",
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
});
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import * as semver from "semver";
|
|
2
|
+
import { coerce } from "semver";
|
|
3
|
+
import { objectKeys } from "../utils/object-keys";
|
|
4
|
+
import { klona } from "klona";
|
|
5
|
+
import { custom, InferOutput, object, ObjectEntries } from "valibot";
|
|
6
|
+
|
|
7
|
+
export type Awaitable<T> = Promise<T> | T;
|
|
8
|
+
|
|
9
|
+
export type MigrationFn<From, To> = (state: From, targetVersion: string) => Awaitable<To>;
|
|
10
|
+
|
|
11
|
+
export type MigrateOptions = {
|
|
12
|
+
debug?: boolean;
|
|
13
|
+
target?: SemVer;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const SemverValidator = custom<SemVer>((input) =>
|
|
17
|
+
typeof input === "string" ? /^\d+\.\d+\.\d+$/.test(input) : false,
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
export const SemverVersionValidator = SemverValidator;
|
|
21
|
+
// export const SemverVersionValidator = optional(SemverValidator, '1.0.0')
|
|
22
|
+
|
|
23
|
+
export const MigrationSchemaValidator = object({
|
|
24
|
+
version: SemverVersionValidator,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export const createVersionSchema = <OBJECTENTRIES extends ObjectEntries>(schema: OBJECTENTRIES) =>
|
|
28
|
+
object({
|
|
29
|
+
...schema,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
export type MigrationSchema = InferOutput<typeof MigrationSchemaValidator>;
|
|
33
|
+
|
|
34
|
+
export type OmitVersion<T> = Omit<T, keyof MigrationSchema>;
|
|
35
|
+
|
|
36
|
+
export type SemVer = `${number}.${number}.${number}`;
|
|
37
|
+
|
|
38
|
+
export interface MigrationClass<Down, Current, Up> {
|
|
39
|
+
version: SemVer;
|
|
40
|
+
up: MigrationFn<Current, Up>;
|
|
41
|
+
down: MigrationFn<Current, Down>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface MigrationObjInput<Down, Current, Up> {
|
|
45
|
+
version: SemVer;
|
|
46
|
+
up: MigrationFn<OmitVersion<Current>, OmitVersion<Up>>;
|
|
47
|
+
down: MigrationFn<OmitVersion<Current>, OmitVersion<Down>>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface MigratorConfig<InitialState, FinalState> {
|
|
51
|
+
migrations: MigrationClass<any, any, any>[];
|
|
52
|
+
coerce?: boolean;
|
|
53
|
+
defaultValue?: FinalState;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export class Migrator<InitialState extends MigrationSchema, OutputState extends MigrationSchema> {
|
|
57
|
+
current: SemVer;
|
|
58
|
+
|
|
59
|
+
migrations: Record<SemVer, MigrationClass<any, any, any>> = {};
|
|
60
|
+
|
|
61
|
+
coerce: boolean;
|
|
62
|
+
defaultValue: OutputState;
|
|
63
|
+
|
|
64
|
+
constructor(config: MigratorConfig<InitialState, OutputState>) {
|
|
65
|
+
config.migrations.forEach((migration) => {
|
|
66
|
+
this.migrations[migration.version] = migration;
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
this.defaultValue = config?.defaultValue ?? ({} as OutputState);
|
|
70
|
+
|
|
71
|
+
const versions = this.getVersions();
|
|
72
|
+
this.current = versions[versions.length - 1];
|
|
73
|
+
this.coerce = config?.coerce ?? true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
getVersions() {
|
|
77
|
+
const keys = objectKeys(this.migrations);
|
|
78
|
+
return semver.sort(keys);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async migrate(
|
|
82
|
+
_state: MigrationSchema | undefined,
|
|
83
|
+
options?: MigrateOptions,
|
|
84
|
+
): Promise<OutputState> {
|
|
85
|
+
const state = _state ?? this.defaultValue;
|
|
86
|
+
const currentVersion = this.tryCoerce(state.version);
|
|
87
|
+
const targetVersion = this.tryCoerce(options?.target ?? this.current);
|
|
88
|
+
|
|
89
|
+
if (options?.debug) {
|
|
90
|
+
console.log("Migrating from", currentVersion, "to", targetVersion);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let finalState = klona(state) as any; // Using any here because we'll be transforming between versions
|
|
94
|
+
const versions = this.getVersions();
|
|
95
|
+
const currentIndex = versions.indexOf(currentVersion);
|
|
96
|
+
const targetIndex = versions.indexOf(targetVersion);
|
|
97
|
+
|
|
98
|
+
if (currentIndex === -1) {
|
|
99
|
+
throw new Error(`Current version "${currentVersion}" not found in migrations`);
|
|
100
|
+
}
|
|
101
|
+
if (targetIndex === -1) {
|
|
102
|
+
throw new Error(`Target version "${targetVersion}" not found in migrations`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// If we're already at the target version, return the state
|
|
106
|
+
if (currentIndex === targetIndex) {
|
|
107
|
+
return finalState as OutputState;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Get the migration path
|
|
111
|
+
const isUpgrade = currentIndex < targetIndex;
|
|
112
|
+
const increment = isUpgrade ? 1 : -1;
|
|
113
|
+
const direction = isUpgrade ? "up" : "down";
|
|
114
|
+
|
|
115
|
+
// Perform migrations
|
|
116
|
+
for (let i = currentIndex; isUpgrade ? i < targetIndex : i > targetIndex; i += increment) {
|
|
117
|
+
const currentVersion = versions[i];
|
|
118
|
+
const nextVersion = versions[i + increment];
|
|
119
|
+
|
|
120
|
+
if (options?.debug) {
|
|
121
|
+
console.log("\tMigrating to version:", nextVersion);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// For upgrades, use current version's migration
|
|
125
|
+
// For downgrades, use previous version's migration
|
|
126
|
+
const migrationVersion = currentVersion;
|
|
127
|
+
const migration = this.migrations[migrationVersion];
|
|
128
|
+
|
|
129
|
+
// Remove version before migration
|
|
130
|
+
const { version: _, ...stateWithoutVersion } = finalState;
|
|
131
|
+
|
|
132
|
+
// Perform migration
|
|
133
|
+
const migratedState = await migration[direction](stateWithoutVersion, currentVersion);
|
|
134
|
+
|
|
135
|
+
// Add new version
|
|
136
|
+
finalState = {
|
|
137
|
+
...migratedState,
|
|
138
|
+
version: nextVersion,
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
if (options?.debug) {
|
|
142
|
+
console.log("\tMigrated state:", finalState);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return finalState as OutputState;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
tryCoerce(version: SemVer): SemVer {
|
|
150
|
+
return this.coerce ? ((coerce(version)?.version as SemVer | undefined) ?? version) : version;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
needMigration(version: SemVer) {
|
|
154
|
+
const newVersion = this.tryCoerce(version);
|
|
155
|
+
return semver.lt(newVersion, this.current);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type ObjectKeys<T extends object> = `${Exclude<keyof T, symbol>}`;
|
|
2
|
+
|
|
3
|
+
export const objectKeys = Object.keys as <Type extends object>(
|
|
4
|
+
value: Type,
|
|
5
|
+
) => Array<ObjectKeys<Type>>;
|
|
6
|
+
export const objectEntries = Object.entries as <Type extends Record<PropertyKey, unknown>>(
|
|
7
|
+
value: Type,
|
|
8
|
+
) => Array<[ObjectKeys<Type>, Type[ObjectKeys<Type>]]>;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../tsconfig/commonjs.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"emitDeclarationOnly": true,
|
|
5
|
+
"outDir": "dist",
|
|
6
|
+
"baseUrl": ".",
|
|
7
|
+
"paths": {
|
|
8
|
+
"@pipelab/migration": ["./src"]
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"include": ["src/**/*"],
|
|
12
|
+
"exclude": ["node_modules", "dist"]
|
|
13
|
+
}
|