@palantir/pack.schema 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-lint.log +3 -3
- package/.turbo/turbo-transpileBrowser.log +1 -1
- package/.turbo/turbo-transpileCjs.log +1 -1
- package/.turbo/turbo-transpileEsm.log +1 -1
- package/.turbo/turbo-transpileTypes.log +1 -1
- package/.turbo/turbo-typecheck.log +1 -1
- package/CHANGELOG.md +9 -0
- package/build/browser/index.js +55 -5
- package/build/browser/index.js.map +1 -1
- package/build/cjs/index.cjs +57 -4
- package/build/cjs/index.cjs.map +1 -1
- package/build/cjs/index.d.cts +39 -5
- package/build/esm/index.js +55 -5
- package/build/esm/index.js.map +1 -1
- package/build/types/__tests__/defineSchemaUpdate.test.d.ts +1 -0
- package/build/types/__tests__/defineSchemaUpdate.test.d.ts.map +1 -0
- package/build/types/defineMigration.d.ts +9 -7
- package/build/types/defineMigration.d.ts.map +1 -1
- package/build/types/defineSchemaUpdate.d.ts +38 -0
- package/build/types/defineSchemaUpdate.d.ts.map +1 -0
- package/build/types/defs.d.ts +1 -0
- package/build/types/defs.d.ts.map +1 -1
- package/build/types/index.d.ts +1 -0
- package/build/types/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/defineSchemaUpdate.test.ts +365 -0
- package/src/defineMigration.ts +13 -29
- package/src/defineSchemaUpdate.ts +114 -0
- package/src/defs.ts +1 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 Palantir Technologies, Inc. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { SchemaBuilder } from "./defineMigration.js";
|
|
18
|
+
import { defineMigration } from "./defineMigration.js";
|
|
19
|
+
import type { ModelDefs } from "./defs.js";
|
|
20
|
+
|
|
21
|
+
export interface InitialSchema<T extends ModelDefs = ModelDefs> {
|
|
22
|
+
readonly type: "initial";
|
|
23
|
+
readonly version: 1;
|
|
24
|
+
readonly models: T;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface FieldMigration {
|
|
28
|
+
readonly derivedFrom: readonly string[];
|
|
29
|
+
readonly forward: (oldFields: Record<string, unknown>) => unknown;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type VersionMigrations = Record<string, Record<string, FieldMigration>>;
|
|
33
|
+
|
|
34
|
+
export interface VersionedSchema<T extends ModelDefs = ModelDefs> {
|
|
35
|
+
readonly type: "versioned";
|
|
36
|
+
readonly models: T;
|
|
37
|
+
readonly version: number;
|
|
38
|
+
readonly previous: SchemaDefinition;
|
|
39
|
+
readonly migrations?: VersionMigrations;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export type SchemaDefinition<T extends ModelDefs = ModelDefs> =
|
|
43
|
+
| InitialSchema<T>
|
|
44
|
+
| VersionedSchema<T>;
|
|
45
|
+
|
|
46
|
+
export function defineSchema<T extends ModelDefs>(models: T): InitialSchema<T> {
|
|
47
|
+
return { type: "initial", version: 1, models };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface SchemaUpdate<T extends ModelDefs, S extends ModelDefs> {
|
|
51
|
+
readonly name: string;
|
|
52
|
+
readonly migration: (schema: SchemaBuilder<T>) => S;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function defineSchemaUpdate<T extends ModelDefs, S extends ModelDefs>(
|
|
56
|
+
name: string,
|
|
57
|
+
migration: (schema: SchemaBuilder<T>) => S,
|
|
58
|
+
): SchemaUpdate<T, S> {
|
|
59
|
+
return { name, migration };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface SchemaVersionBuilder<T extends ModelDefs> {
|
|
63
|
+
addSchemaUpdate<S extends ModelDefs>(
|
|
64
|
+
update: SchemaUpdate<T, S>,
|
|
65
|
+
): SchemaVersionBuilder<T & S>;
|
|
66
|
+
withMigrations(migrations: VersionMigrations): SchemaVersionBuilder<T>;
|
|
67
|
+
build(): VersionedSchema<T>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
class SchemaVersionBuilderImpl<T extends ModelDefs> implements SchemaVersionBuilder<T> {
|
|
71
|
+
private readonly models: T;
|
|
72
|
+
private readonly version: number;
|
|
73
|
+
private readonly previous: SchemaDefinition;
|
|
74
|
+
private readonly _migrations: VersionMigrations | undefined;
|
|
75
|
+
|
|
76
|
+
constructor(
|
|
77
|
+
models: T,
|
|
78
|
+
version: number,
|
|
79
|
+
previous: SchemaDefinition,
|
|
80
|
+
migrations?: VersionMigrations,
|
|
81
|
+
) {
|
|
82
|
+
this.models = models;
|
|
83
|
+
this.version = version;
|
|
84
|
+
this.previous = previous;
|
|
85
|
+
this._migrations = migrations;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
addSchemaUpdate<S extends ModelDefs>(
|
|
89
|
+
update: SchemaUpdate<T, S>,
|
|
90
|
+
): SchemaVersionBuilder<T & S> {
|
|
91
|
+
const merged = defineMigration(this.models, update.migration);
|
|
92
|
+
return new SchemaVersionBuilderImpl(merged, this.version, this.previous, this._migrations);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
withMigrations(migrations: VersionMigrations): SchemaVersionBuilder<T> {
|
|
96
|
+
return new SchemaVersionBuilderImpl(this.models, this.version, this.previous, migrations);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
build(): VersionedSchema<T> {
|
|
100
|
+
return {
|
|
101
|
+
type: "versioned",
|
|
102
|
+
models: { ...this.models },
|
|
103
|
+
version: this.version,
|
|
104
|
+
previous: this.previous,
|
|
105
|
+
...(this._migrations != null ? { migrations: this._migrations } : {}),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function nextSchema<T extends ModelDefs>(
|
|
111
|
+
previous: SchemaDefinition<T>,
|
|
112
|
+
): SchemaVersionBuilder<T> {
|
|
113
|
+
return new SchemaVersionBuilderImpl(previous.models, previous.version + 1, previous);
|
|
114
|
+
}
|
package/src/defs.ts
CHANGED