@sidekickdb/platform 0.0.0 → 2.0.1
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/.tshy/build.json +8 -0
- package/.tshy/commonjs.json +16 -0
- package/.tshy/esm.json +15 -0
- package/.turbo/turbo-build.log +50 -0
- package/LICENSE.md +202 -0
- package/NOTICE +27 -0
- package/README.md +444 -2
- package/__tests__/src/apiURI.test.ts +58 -0
- package/__tests__/src/backupDownload.test.ts +427 -0
- package/__tests__/src/backups.test.ts +168 -0
- package/__tests__/src/clerk.test.ts +13 -0
- package/__tests__/src/consts.test.ts +11 -0
- package/__tests__/src/migrations.test.ts +885 -0
- package/__tests__/src/perms.test.ts +102 -0
- package/__tests__/src/rename.test.ts +126 -0
- package/__tests__/src/schema.push.e2e.test.ts +186 -0
- package/__tests__/src/schema.test.ts +644 -0
- package/__tests__/src/typescript-schema.test.ts +117 -0
- package/dist/commonjs/ProgressPromise.d.ts +18 -0
- package/dist/commonjs/ProgressPromise.d.ts.map +1 -0
- package/dist/commonjs/ProgressPromise.js +77 -0
- package/dist/commonjs/ProgressPromise.js.map +1 -0
- package/dist/commonjs/api.d.ts +480 -0
- package/dist/commonjs/api.d.ts.map +1 -0
- package/dist/commonjs/api.js +1132 -0
- package/dist/commonjs/api.js.map +1 -0
- package/dist/commonjs/backupDownload.d.ts +89 -0
- package/dist/commonjs/backupDownload.d.ts.map +1 -0
- package/dist/commonjs/backupDownload.js +357 -0
- package/dist/commonjs/backupDownload.js.map +1 -0
- package/dist/commonjs/backups.d.ts +146 -0
- package/dist/commonjs/backups.d.ts.map +1 -0
- package/dist/commonjs/backups.js +248 -0
- package/dist/commonjs/backups.js.map +1 -0
- package/dist/commonjs/clerk.d.ts +2 -0
- package/dist/commonjs/clerk.d.ts.map +1 -0
- package/dist/commonjs/clerk.js +29 -0
- package/dist/commonjs/clerk.js.map +1 -0
- package/dist/commonjs/consts.d.ts +11 -0
- package/dist/commonjs/consts.d.ts.map +1 -0
- package/dist/commonjs/consts.js +17 -0
- package/dist/commonjs/consts.js.map +1 -0
- package/dist/commonjs/crypto.d.ts +4 -0
- package/dist/commonjs/crypto.d.ts.map +1 -0
- package/dist/commonjs/crypto.js +26 -0
- package/dist/commonjs/crypto.js.map +1 -0
- package/dist/commonjs/index.d.ts +18 -0
- package/dist/commonjs/index.d.ts.map +1 -0
- package/dist/commonjs/index.js +61 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/migrationUtils.d.ts +4 -0
- package/dist/commonjs/migrationUtils.d.ts.map +1 -0
- package/dist/commonjs/migrationUtils.js +52 -0
- package/dist/commonjs/migrationUtils.js.map +1 -0
- package/dist/commonjs/migrations.d.ts +99 -0
- package/dist/commonjs/migrations.d.ts.map +1 -0
- package/dist/commonjs/migrations.js +597 -0
- package/dist/commonjs/migrations.js.map +1 -0
- package/dist/commonjs/oauth.d.ts +98 -0
- package/dist/commonjs/oauth.d.ts.map +1 -0
- package/dist/commonjs/oauth.js +277 -0
- package/dist/commonjs/oauth.js.map +1 -0
- package/dist/commonjs/oauthCommon.d.ts +12 -0
- package/dist/commonjs/oauthCommon.d.ts.map +1 -0
- package/dist/commonjs/oauthCommon.js +26 -0
- package/dist/commonjs/oauthCommon.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/perms.d.ts +3 -0
- package/dist/commonjs/perms.d.ts.map +1 -0
- package/dist/commonjs/perms.js +66 -0
- package/dist/commonjs/perms.js.map +1 -0
- package/dist/commonjs/relationships.d.ts +7 -0
- package/dist/commonjs/relationships.d.ts.map +1 -0
- package/dist/commonjs/relationships.js +46 -0
- package/dist/commonjs/relationships.js.map +1 -0
- package/dist/commonjs/schema.d.ts +161 -0
- package/dist/commonjs/schema.d.ts.map +1 -0
- package/dist/commonjs/schema.js +174 -0
- package/dist/commonjs/schema.js.map +1 -0
- package/dist/commonjs/serverOAuth.d.ts +25 -0
- package/dist/commonjs/serverOAuth.d.ts.map +1 -0
- package/dist/commonjs/serverOAuth.js +72 -0
- package/dist/commonjs/serverOAuth.js.map +1 -0
- package/dist/commonjs/typescript-schema.d.ts +17 -0
- package/dist/commonjs/typescript-schema.d.ts.map +1 -0
- package/dist/commonjs/typescript-schema.js +314 -0
- package/dist/commonjs/typescript-schema.js.map +1 -0
- package/dist/commonjs/util.d.ts +13 -0
- package/dist/commonjs/util.d.ts.map +1 -0
- package/dist/commonjs/util.js +55 -0
- package/dist/commonjs/util.js.map +1 -0
- package/dist/commonjs/version.d.ts +3 -0
- package/dist/commonjs/version.d.ts.map +1 -0
- package/dist/commonjs/version.js +5 -0
- package/dist/commonjs/version.js.map +1 -0
- package/dist/esm/ProgressPromise.d.ts +18 -0
- package/dist/esm/ProgressPromise.d.ts.map +1 -0
- package/dist/esm/ProgressPromise.js +73 -0
- package/dist/esm/ProgressPromise.js.map +1 -0
- package/dist/esm/api.d.ts +480 -0
- package/dist/esm/api.d.ts.map +1 -0
- package/dist/esm/api.js +1121 -0
- package/dist/esm/api.js.map +1 -0
- package/dist/esm/backupDownload.d.ts +89 -0
- package/dist/esm/backupDownload.d.ts.map +1 -0
- package/dist/esm/backupDownload.js +354 -0
- package/dist/esm/backupDownload.js.map +1 -0
- package/dist/esm/backups.d.ts +146 -0
- package/dist/esm/backups.d.ts.map +1 -0
- package/dist/esm/backups.js +237 -0
- package/dist/esm/backups.js.map +1 -0
- package/dist/esm/clerk.d.ts +2 -0
- package/dist/esm/clerk.d.ts.map +1 -0
- package/dist/esm/clerk.js +26 -0
- package/dist/esm/clerk.js.map +1 -0
- package/dist/esm/consts.d.ts +11 -0
- package/dist/esm/consts.d.ts.map +1 -0
- package/dist/esm/consts.js +13 -0
- package/dist/esm/consts.js.map +1 -0
- package/dist/esm/crypto.d.ts +4 -0
- package/dist/esm/crypto.d.ts.map +1 -0
- package/dist/esm/crypto.js +21 -0
- package/dist/esm/crypto.js.map +1 -0
- package/dist/esm/index.d.ts +18 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/migrationUtils.d.ts +4 -0
- package/dist/esm/migrationUtils.d.ts.map +1 -0
- package/dist/esm/migrationUtils.js +47 -0
- package/dist/esm/migrationUtils.js.map +1 -0
- package/dist/esm/migrations.d.ts +99 -0
- package/dist/esm/migrations.d.ts.map +1 -0
- package/dist/esm/migrations.js +588 -0
- package/dist/esm/migrations.js.map +1 -0
- package/dist/esm/oauth.d.ts +98 -0
- package/dist/esm/oauth.d.ts.map +1 -0
- package/dist/esm/oauth.js +268 -0
- package/dist/esm/oauth.js.map +1 -0
- package/dist/esm/oauthCommon.d.ts +12 -0
- package/dist/esm/oauthCommon.d.ts.map +1 -0
- package/dist/esm/oauthCommon.js +22 -0
- package/dist/esm/oauthCommon.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/perms.d.ts +3 -0
- package/dist/esm/perms.d.ts.map +1 -0
- package/dist/esm/perms.js +62 -0
- package/dist/esm/perms.js.map +1 -0
- package/dist/esm/relationships.d.ts +7 -0
- package/dist/esm/relationships.d.ts.map +1 -0
- package/dist/esm/relationships.js +43 -0
- package/dist/esm/relationships.js.map +1 -0
- package/dist/esm/schema.d.ts +161 -0
- package/dist/esm/schema.d.ts.map +1 -0
- package/dist/esm/schema.js +158 -0
- package/dist/esm/schema.js.map +1 -0
- package/dist/esm/serverOAuth.d.ts +25 -0
- package/dist/esm/serverOAuth.d.ts.map +1 -0
- package/dist/esm/serverOAuth.js +68 -0
- package/dist/esm/serverOAuth.js.map +1 -0
- package/dist/esm/typescript-schema.d.ts +17 -0
- package/dist/esm/typescript-schema.d.ts.map +1 -0
- package/dist/esm/typescript-schema.js +310 -0
- package/dist/esm/typescript-schema.js.map +1 -0
- package/dist/esm/util.d.ts +13 -0
- package/dist/esm/util.d.ts.map +1 -0
- package/dist/esm/util.js +46 -0
- package/dist/esm/util.js.map +1 -0
- package/dist/esm/version.d.ts +3 -0
- package/dist/esm/version.d.ts.map +1 -0
- package/dist/esm/version.js +3 -0
- package/dist/esm/version.js.map +1 -0
- package/dist/standalone/index.js +13747 -0
- package/dist/standalone/index.umd.cjs +72 -0
- package/package.json +69 -3
- package/src/ProgressPromise.ts +97 -0
- package/src/api.ts +1903 -0
- package/src/backupDownload.ts +519 -0
- package/src/backups.ts +342 -0
- package/src/clerk.ts +24 -0
- package/src/consts.ts +22 -0
- package/src/crypto.ts +28 -0
- package/src/index.ts +147 -0
- package/src/migrationUtils.ts +56 -0
- package/src/migrations.ts +920 -0
- package/src/oauth.ts +390 -0
- package/src/oauthCommon.ts +38 -0
- package/src/perms.ts +81 -0
- package/src/relationships.ts +57 -0
- package/src/schema.ts +497 -0
- package/src/serverOAuth.ts +119 -0
- package/src/typescript-schema.ts +439 -0
- package/src/util.ts +77 -0
- package/src/version.ts +3 -0
- package/tsconfig.dev.json +14 -0
- package/tsconfig.json +14 -0
- package/vite.config.ts +19 -0
|
@@ -0,0 +1,920 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DataAttrDef,
|
|
3
|
+
id,
|
|
4
|
+
InstantDBAttr,
|
|
5
|
+
InstantDBAttrOnDelete,
|
|
6
|
+
InstantDBCheckedDataType,
|
|
7
|
+
InstantSchemaDef,
|
|
8
|
+
LinkDef,
|
|
9
|
+
} from '@sidekickdb/core';
|
|
10
|
+
import { PlanStep } from './api.ts';
|
|
11
|
+
import { attrDefToNewAttrTx, linkDefToNewAttrTx } from './migrationUtils.ts';
|
|
12
|
+
import { relationshipConstraints, RelationshipKinds } from './relationships.ts';
|
|
13
|
+
|
|
14
|
+
export type Identifier = {
|
|
15
|
+
namespace: string;
|
|
16
|
+
attrName: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type AttrWithIdentifier = {
|
|
20
|
+
'value-type': 'blob' | 'ref';
|
|
21
|
+
cardinality?: 'many' | 'one';
|
|
22
|
+
'forward-identity'?: Identifier;
|
|
23
|
+
'reverse-identity'?: Identifier | null;
|
|
24
|
+
'on-delete'?: InstantDBAttrOnDelete | null | undefined;
|
|
25
|
+
'on-delete-reverse'?: InstantDBAttrOnDelete | null | undefined;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type MigrationTxTypes = {
|
|
29
|
+
'delete-attr': { identifier: Identifier };
|
|
30
|
+
'update-attr': { identifier: Identifier; partialAttr: AttrWithIdentifier };
|
|
31
|
+
'add-attr': {
|
|
32
|
+
identifier: Identifier;
|
|
33
|
+
'unique?': boolean;
|
|
34
|
+
'index?': boolean;
|
|
35
|
+
'required?': boolean;
|
|
36
|
+
'reverse-identity'?: Identifier | null;
|
|
37
|
+
'forward-identity': Identifier;
|
|
38
|
+
cardinality: 'many' | 'one';
|
|
39
|
+
'value-type': 'blob' | 'ref';
|
|
40
|
+
'on-delete'?: InstantDBAttrOnDelete | null;
|
|
41
|
+
'on-delete-reverse'?: InstantDBAttrOnDelete | null;
|
|
42
|
+
'checked-data-type'?: InstantDBCheckedDataType | null;
|
|
43
|
+
};
|
|
44
|
+
index: { identifier: Identifier };
|
|
45
|
+
'remove-index': { identifier: Identifier };
|
|
46
|
+
unique: { identifier: Identifier };
|
|
47
|
+
'remove-unique': { identifier: Identifier };
|
|
48
|
+
required: { identifier: Identifier };
|
|
49
|
+
'remove-required': { identifier: Identifier };
|
|
50
|
+
'check-data-type': {
|
|
51
|
+
identifier: Identifier;
|
|
52
|
+
'checked-data-type': InstantDBCheckedDataType;
|
|
53
|
+
};
|
|
54
|
+
'remove-data-type': { identifier: Identifier };
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
type JobMigrationTypes =
|
|
58
|
+
| 'index'
|
|
59
|
+
| 'remove-index'
|
|
60
|
+
| 'remove-unique'
|
|
61
|
+
| 'remove-required'
|
|
62
|
+
| 'unique'
|
|
63
|
+
| 'remove-data-type'
|
|
64
|
+
| 'required';
|
|
65
|
+
|
|
66
|
+
type PlanStepMap = {
|
|
67
|
+
[K in PlanStep as K[0]]: K[1];
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const getExistingAttrThrowing = (
|
|
71
|
+
ident: Identifier,
|
|
72
|
+
existingAttrs: InstantDBAttr[],
|
|
73
|
+
): InstantDBAttr => {
|
|
74
|
+
const found =
|
|
75
|
+
existingAttrs.find((attr) => {
|
|
76
|
+
return (
|
|
77
|
+
attr['forward-identity'][1] === ident.namespace &&
|
|
78
|
+
attr['forward-identity'][2] === ident.attrName
|
|
79
|
+
);
|
|
80
|
+
}) || null;
|
|
81
|
+
if (!found) {
|
|
82
|
+
throw new Error(`Attribute ${ident.namespace}.${ident.attrName} not found`);
|
|
83
|
+
}
|
|
84
|
+
return found;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const convertSimpleConstraintUpdate: ConvertPlanStepFn<any> = (
|
|
88
|
+
tx,
|
|
89
|
+
existing,
|
|
90
|
+
) => {
|
|
91
|
+
const found = getExistingAttrThrowing(tx.identifier, existing);
|
|
92
|
+
return {
|
|
93
|
+
'attr-id': found.id,
|
|
94
|
+
'forward-identity': found['forward-identity'],
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// converts migration operations from Identifier (namespace/name) based
|
|
99
|
+
// into transaction steps that use database ids
|
|
100
|
+
const CONVERTERS: AllConvertPlanStepFns = {
|
|
101
|
+
index: convertSimpleConstraintUpdate,
|
|
102
|
+
unique: convertSimpleConstraintUpdate,
|
|
103
|
+
required: convertSimpleConstraintUpdate,
|
|
104
|
+
'remove-index': convertSimpleConstraintUpdate,
|
|
105
|
+
'remove-unique': convertSimpleConstraintUpdate,
|
|
106
|
+
'remove-required': convertSimpleConstraintUpdate,
|
|
107
|
+
|
|
108
|
+
'delete-attr': (from, existing) => {
|
|
109
|
+
const found = getExistingAttrThrowing(from.identifier, existing);
|
|
110
|
+
return found.id;
|
|
111
|
+
},
|
|
112
|
+
'update-attr': (from, existing) => {
|
|
113
|
+
const found = getExistingAttrThrowing(from.identifier, existing);
|
|
114
|
+
return {
|
|
115
|
+
id: found.id,
|
|
116
|
+
'forward-identity': from.partialAttr['forward-identity']
|
|
117
|
+
? [
|
|
118
|
+
found.id,
|
|
119
|
+
from.partialAttr['forward-identity'].namespace,
|
|
120
|
+
from.partialAttr['forward-identity'].attrName,
|
|
121
|
+
]
|
|
122
|
+
: undefined,
|
|
123
|
+
'reverse-identity': from.partialAttr['reverse-identity']
|
|
124
|
+
? [
|
|
125
|
+
found.id,
|
|
126
|
+
from.partialAttr['reverse-identity'].namespace,
|
|
127
|
+
from.partialAttr['reverse-identity'].attrName,
|
|
128
|
+
]
|
|
129
|
+
: undefined,
|
|
130
|
+
cardinality: from.partialAttr['cardinality']
|
|
131
|
+
? from.partialAttr['cardinality']
|
|
132
|
+
: undefined,
|
|
133
|
+
|
|
134
|
+
'on-delete': from.partialAttr['on-delete']
|
|
135
|
+
? from.partialAttr['on-delete']
|
|
136
|
+
: undefined,
|
|
137
|
+
'on-delete-reverse': from.partialAttr['on-delete-reverse']
|
|
138
|
+
? from.partialAttr['on-delete-reverse']
|
|
139
|
+
: undefined,
|
|
140
|
+
};
|
|
141
|
+
},
|
|
142
|
+
'add-attr': (from, _existing) => {
|
|
143
|
+
const attrId = id();
|
|
144
|
+
const forwardIdentity: [string, string, string] = [
|
|
145
|
+
attrId,
|
|
146
|
+
from.identifier.namespace,
|
|
147
|
+
from.identifier.attrName,
|
|
148
|
+
];
|
|
149
|
+
|
|
150
|
+
const steps: PlanStep[] = [];
|
|
151
|
+
|
|
152
|
+
// First, create the attribute without unique, required, or indexed
|
|
153
|
+
steps.push([
|
|
154
|
+
'add-attr',
|
|
155
|
+
{
|
|
156
|
+
'forward-identity': forwardIdentity,
|
|
157
|
+
'reverse-identity': from['reverse-identity']
|
|
158
|
+
? [
|
|
159
|
+
id(),
|
|
160
|
+
from['reverse-identity'].namespace,
|
|
161
|
+
from['reverse-identity'].attrName,
|
|
162
|
+
]
|
|
163
|
+
: null,
|
|
164
|
+
'inferred-types': null,
|
|
165
|
+
'value-type': from['value-type'],
|
|
166
|
+
id: attrId,
|
|
167
|
+
cardinality: from.cardinality,
|
|
168
|
+
'index?': false,
|
|
169
|
+
'required?': false,
|
|
170
|
+
'unique?': false,
|
|
171
|
+
catalog: 'user',
|
|
172
|
+
'on-delete': from['on-delete'],
|
|
173
|
+
'on-delete-reverse': from['on-delete-reverse'],
|
|
174
|
+
'checked-data-type': from['checked-data-type'],
|
|
175
|
+
},
|
|
176
|
+
]);
|
|
177
|
+
|
|
178
|
+
// Then add separate steps for unique, required, and indexed
|
|
179
|
+
if (from['unique?']) {
|
|
180
|
+
steps.push([
|
|
181
|
+
'unique',
|
|
182
|
+
{
|
|
183
|
+
'attr-id': attrId,
|
|
184
|
+
'forward-identity': forwardIdentity,
|
|
185
|
+
},
|
|
186
|
+
]);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (from['required?']) {
|
|
190
|
+
steps.push([
|
|
191
|
+
'required',
|
|
192
|
+
{
|
|
193
|
+
'attr-id': attrId,
|
|
194
|
+
'forward-identity': forwardIdentity,
|
|
195
|
+
},
|
|
196
|
+
]);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (from['index?']) {
|
|
200
|
+
steps.push([
|
|
201
|
+
'index',
|
|
202
|
+
{
|
|
203
|
+
'attr-id': attrId,
|
|
204
|
+
'forward-identity': forwardIdentity,
|
|
205
|
+
},
|
|
206
|
+
]);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return steps;
|
|
210
|
+
},
|
|
211
|
+
'remove-data-type': convertSimpleConstraintUpdate,
|
|
212
|
+
'check-data-type': (from, existing) => {
|
|
213
|
+
const found = getExistingAttrThrowing(from.identifier, existing);
|
|
214
|
+
return {
|
|
215
|
+
'attr-id': found.id,
|
|
216
|
+
'checked-data-type': from['checked-data-type'],
|
|
217
|
+
'forward-identity': found['forward-identity'],
|
|
218
|
+
};
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
function isSystemCatalogAttr(
|
|
223
|
+
systemCatalogIdentNames: Record<string, Set<string>>,
|
|
224
|
+
entityName: string,
|
|
225
|
+
attrName: string,
|
|
226
|
+
): boolean {
|
|
227
|
+
return !!systemCatalogIdentNames[entityName]?.has(attrName);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export const convertTxSteps = (
|
|
231
|
+
txs: MigrationTx[],
|
|
232
|
+
existingAttrs: InstantDBAttr[],
|
|
233
|
+
): PlanStep[] => {
|
|
234
|
+
if (!existingAttrs) {
|
|
235
|
+
throw new Error('Existing attributes are required');
|
|
236
|
+
}
|
|
237
|
+
const result: PlanStep[] = [];
|
|
238
|
+
txs.forEach((tx) => {
|
|
239
|
+
const converter = CONVERTERS[tx.type];
|
|
240
|
+
if (!converter) {
|
|
241
|
+
throw new Error(`Unknown transaction type: ${tx.type}`);
|
|
242
|
+
}
|
|
243
|
+
const converted = converter(tx as any, existingAttrs);
|
|
244
|
+
if (Array.isArray(converted)) {
|
|
245
|
+
result.push(...converted);
|
|
246
|
+
} else {
|
|
247
|
+
result.push([tx.type, converted] as PlanStep);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
return result;
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
type ConvertPlanStepFn<T extends keyof MigrationTxTypes> = (
|
|
254
|
+
from: MigrationTxTypes[T],
|
|
255
|
+
existingAttrs: InstantDBAttr[],
|
|
256
|
+
) => PlanStepMap[T] | PlanStep[];
|
|
257
|
+
|
|
258
|
+
type AllConvertPlanStepFns = {
|
|
259
|
+
[K in keyof MigrationTxTypes]: ConvertPlanStepFn<K>;
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
export type MigrationTx = {
|
|
263
|
+
[K in keyof MigrationTxTypes]: {
|
|
264
|
+
type: K;
|
|
265
|
+
} & MigrationTxTypes[K];
|
|
266
|
+
}[keyof MigrationTxTypes];
|
|
267
|
+
|
|
268
|
+
export type MigrationTxSpecific<T extends keyof MigrationTxTypes> = {
|
|
269
|
+
type: T;
|
|
270
|
+
} & MigrationTxTypes[T];
|
|
271
|
+
|
|
272
|
+
export type AnyLink = LinkDef<any, any, any, any, any, any, any>;
|
|
273
|
+
export type AnyBlob = DataAttrDef<any, any, any>;
|
|
274
|
+
|
|
275
|
+
export const diffSchemas = async (
|
|
276
|
+
oldSchema: InstantSchemaDef<any, any, any>,
|
|
277
|
+
newSchema: InstantSchemaDef<any, any, any>,
|
|
278
|
+
resolveFn: RenameResolveFn<string>,
|
|
279
|
+
systemCatalogIdentNames: Record<string, Set<string>>,
|
|
280
|
+
): Promise<MigrationTx[]> => {
|
|
281
|
+
const transactions: MigrationTx[] = [];
|
|
282
|
+
|
|
283
|
+
const oldEntities = oldSchema.entities;
|
|
284
|
+
const newEntities = newSchema.entities;
|
|
285
|
+
|
|
286
|
+
const oldEntityNames = Object.keys(oldEntities);
|
|
287
|
+
const newEntityNames = Object.keys(newEntities);
|
|
288
|
+
|
|
289
|
+
const deletedEntityNames = oldEntityNames.filter(
|
|
290
|
+
(name) => !newEntityNames.includes(name),
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
for (const entityName of deletedEntityNames) {
|
|
294
|
+
Object.keys(oldEntities[entityName].attrs).forEach((attrName) => {
|
|
295
|
+
if (isSystemCatalogAttr(systemCatalogIdentNames, entityName, attrName))
|
|
296
|
+
return;
|
|
297
|
+
transactions.push({
|
|
298
|
+
type: 'delete-attr',
|
|
299
|
+
identifier: {
|
|
300
|
+
attrName,
|
|
301
|
+
namespace: entityName,
|
|
302
|
+
},
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
if (isSystemCatalogAttr(systemCatalogIdentNames, entityName, 'id')) {
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
transactions.push({
|
|
309
|
+
type: 'delete-attr',
|
|
310
|
+
identifier: {
|
|
311
|
+
attrName: 'id',
|
|
312
|
+
namespace: entityName,
|
|
313
|
+
},
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const addedEntityNames = newEntityNames.filter(
|
|
318
|
+
(name) => !oldEntityNames.includes(name),
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
for (const entityName of addedEntityNames) {
|
|
322
|
+
if (!isSystemCatalogAttr(systemCatalogIdentNames, entityName, 'id')) {
|
|
323
|
+
transactions.push({
|
|
324
|
+
type: 'add-attr',
|
|
325
|
+
'forward-identity': {
|
|
326
|
+
namespace: entityName,
|
|
327
|
+
attrName: 'id',
|
|
328
|
+
},
|
|
329
|
+
identifier: {
|
|
330
|
+
attrName: 'id',
|
|
331
|
+
namespace: entityName,
|
|
332
|
+
},
|
|
333
|
+
'index?': false,
|
|
334
|
+
'required?': true,
|
|
335
|
+
cardinality: 'one',
|
|
336
|
+
'unique?': true,
|
|
337
|
+
'value-type': 'blob',
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
for (const attrName of Object.keys(newEntities[entityName].attrs)) {
|
|
342
|
+
if (isSystemCatalogAttr(systemCatalogIdentNames, entityName, attrName)) {
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
const attrInSchema = newEntities[entityName].attrs[attrName];
|
|
346
|
+
transactions.push(attrDefToNewAttrTx(attrInSchema, entityName, attrName));
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const innerEntityNames = oldEntityNames.filter((name) =>
|
|
351
|
+
newEntityNames.includes(name),
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
for (const entityName of innerEntityNames) {
|
|
355
|
+
// BLOB ATTRIBUTES
|
|
356
|
+
const addedFields = Object.keys(newEntities[entityName].attrs).filter(
|
|
357
|
+
(field) => {
|
|
358
|
+
if (isSystemCatalogAttr(systemCatalogIdentNames, entityName, field))
|
|
359
|
+
return false;
|
|
360
|
+
|
|
361
|
+
const oldEntityHasIt = Object.keys(
|
|
362
|
+
oldEntities[entityName].attrs,
|
|
363
|
+
).includes(field);
|
|
364
|
+
return !oldEntityHasIt;
|
|
365
|
+
},
|
|
366
|
+
);
|
|
367
|
+
const removedFields = Object.keys(oldEntities[entityName].attrs).filter(
|
|
368
|
+
(field) => {
|
|
369
|
+
if (isSystemCatalogAttr(systemCatalogIdentNames, entityName, field))
|
|
370
|
+
return false;
|
|
371
|
+
|
|
372
|
+
const newEntityHasIt = Object.keys(
|
|
373
|
+
newEntities[entityName].attrs,
|
|
374
|
+
).includes(field);
|
|
375
|
+
return !newEntityHasIt;
|
|
376
|
+
},
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
const resolved = await resolveRenames(
|
|
380
|
+
addedFields,
|
|
381
|
+
removedFields,
|
|
382
|
+
resolveFn,
|
|
383
|
+
{
|
|
384
|
+
type: 'attribute',
|
|
385
|
+
entityName: entityName,
|
|
386
|
+
},
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
const consistentFields = Object.keys(oldEntities[entityName].attrs).filter(
|
|
390
|
+
(field) => Object.keys(newEntities[entityName].attrs).includes(field),
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
consistentFields.forEach((fieldName) => {
|
|
394
|
+
if (isSystemCatalogAttr(systemCatalogIdentNames, entityName, fieldName)) {
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
transactions.push(
|
|
398
|
+
...compareBlobs(
|
|
399
|
+
{
|
|
400
|
+
attrName: fieldName,
|
|
401
|
+
namespace: entityName,
|
|
402
|
+
},
|
|
403
|
+
oldEntities[entityName].attrs[fieldName],
|
|
404
|
+
newEntities[entityName].attrs[fieldName],
|
|
405
|
+
),
|
|
406
|
+
);
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
resolved.deleted.forEach((attrName) => {
|
|
410
|
+
transactions.push({
|
|
411
|
+
type: 'delete-attr',
|
|
412
|
+
identifier: {
|
|
413
|
+
attrName,
|
|
414
|
+
namespace: entityName,
|
|
415
|
+
},
|
|
416
|
+
});
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
resolved.created.forEach((createdName) => {
|
|
420
|
+
const attrInSchema = newSchema.entities[entityName].attrs[
|
|
421
|
+
createdName
|
|
422
|
+
] as AnyBlob;
|
|
423
|
+
transactions.push(
|
|
424
|
+
attrDefToNewAttrTx(attrInSchema, entityName, createdName),
|
|
425
|
+
);
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
resolved.renamed.forEach((renamed) => {
|
|
429
|
+
transactions.push({
|
|
430
|
+
type: 'update-attr',
|
|
431
|
+
identifier: {
|
|
432
|
+
attrName: renamed.from,
|
|
433
|
+
namespace: entityName,
|
|
434
|
+
},
|
|
435
|
+
partialAttr: {
|
|
436
|
+
'value-type': 'blob',
|
|
437
|
+
cardinality: 'one',
|
|
438
|
+
'forward-identity': {
|
|
439
|
+
attrName: renamed.to,
|
|
440
|
+
namespace: entityName,
|
|
441
|
+
},
|
|
442
|
+
},
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
transactions.push(
|
|
446
|
+
...compareBlobs(
|
|
447
|
+
{
|
|
448
|
+
attrName: renamed.from,
|
|
449
|
+
namespace: entityName,
|
|
450
|
+
},
|
|
451
|
+
oldEntities[entityName].attrs[renamed.from],
|
|
452
|
+
newEntities[entityName].attrs[renamed.to],
|
|
453
|
+
),
|
|
454
|
+
);
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const oldLinks = (Object.values(oldSchema.links) as AnyLink[]).filter(
|
|
459
|
+
(link) =>
|
|
460
|
+
!isSystemCatalogAttr(
|
|
461
|
+
systemCatalogIdentNames,
|
|
462
|
+
link.forward.on,
|
|
463
|
+
link.forward.label,
|
|
464
|
+
),
|
|
465
|
+
);
|
|
466
|
+
const newLinks = (Object.values(newSchema.links) as AnyLink[]).filter(
|
|
467
|
+
(link) =>
|
|
468
|
+
!isSystemCatalogAttr(
|
|
469
|
+
systemCatalogIdentNames,
|
|
470
|
+
link.forward.on,
|
|
471
|
+
link.forward.label,
|
|
472
|
+
),
|
|
473
|
+
);
|
|
474
|
+
|
|
475
|
+
// Group links by their forward namespace-label combination for comparison
|
|
476
|
+
const createLinkKey = (link: AnyLink) =>
|
|
477
|
+
`${link.forward.on}<->${link.reverse.on}`;
|
|
478
|
+
const createLinkIdentity = (link: AnyLink) =>
|
|
479
|
+
`${link.forward.on}.${link.forward.label}<->${link.reverse.on}.${link.reverse.label}`;
|
|
480
|
+
|
|
481
|
+
const newLinksByKey = new Map<string, AnyLink[]>();
|
|
482
|
+
const oldLinksByKey = new Map<string, AnyLink[]>();
|
|
483
|
+
|
|
484
|
+
for (const link of newLinks) {
|
|
485
|
+
const key = createLinkKey(link);
|
|
486
|
+
const links = newLinksByKey.get(key) || [];
|
|
487
|
+
links.push(link);
|
|
488
|
+
newLinksByKey.set(key, links);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
for (const link of oldLinks) {
|
|
492
|
+
const key = createLinkKey(link);
|
|
493
|
+
const links = oldLinksByKey.get(key) || [];
|
|
494
|
+
links.push(link);
|
|
495
|
+
oldLinksByKey.set(key, links);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const allLinkKeys = new Set([
|
|
499
|
+
...oldLinksByKey.keys(),
|
|
500
|
+
...newLinksByKey.keys(),
|
|
501
|
+
]);
|
|
502
|
+
|
|
503
|
+
for (const linkKey of allLinkKeys) {
|
|
504
|
+
const oldLinksInGroup = oldLinksByKey.get(linkKey) || [];
|
|
505
|
+
const newLinksInGroup = newLinksByKey.get(linkKey) || [];
|
|
506
|
+
|
|
507
|
+
const oldIdentities = oldLinksInGroup.map((link) =>
|
|
508
|
+
createLinkIdentity(link),
|
|
509
|
+
);
|
|
510
|
+
const newIdentities = newLinksInGroup.map((link) =>
|
|
511
|
+
createLinkIdentity(link),
|
|
512
|
+
);
|
|
513
|
+
|
|
514
|
+
const addedIdentities = newIdentities.filter(
|
|
515
|
+
(identity) => !oldIdentities.includes(identity),
|
|
516
|
+
);
|
|
517
|
+
|
|
518
|
+
const removedIdentities = oldIdentities.filter(
|
|
519
|
+
(identity) => !newIdentities.includes(identity),
|
|
520
|
+
);
|
|
521
|
+
|
|
522
|
+
const consistentIdentities = oldIdentities.filter((identity) =>
|
|
523
|
+
newIdentities.includes(identity),
|
|
524
|
+
);
|
|
525
|
+
|
|
526
|
+
consistentIdentities.forEach((identity) => {
|
|
527
|
+
const oldLink = oldLinksInGroup.find(
|
|
528
|
+
(l) => createLinkIdentity(l) === identity,
|
|
529
|
+
);
|
|
530
|
+
const newLink = newLinksInGroup.find(
|
|
531
|
+
(l) => createLinkIdentity(l) === identity,
|
|
532
|
+
);
|
|
533
|
+
if (!oldLink || !newLink) return;
|
|
534
|
+
transactions.push(
|
|
535
|
+
...compareLinks(
|
|
536
|
+
{
|
|
537
|
+
namespace: oldLink.forward.on,
|
|
538
|
+
attrName: oldLink.forward.label,
|
|
539
|
+
},
|
|
540
|
+
oldLink,
|
|
541
|
+
newLink,
|
|
542
|
+
),
|
|
543
|
+
);
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
const resolved = await resolveRenames(
|
|
547
|
+
addedIdentities,
|
|
548
|
+
removedIdentities,
|
|
549
|
+
resolveFn,
|
|
550
|
+
{
|
|
551
|
+
type: 'link',
|
|
552
|
+
forwardEntityName: oldLinksInGroup[0]?.forward.on,
|
|
553
|
+
reverseEntityName: oldLinksInGroup[0]?.reverse.on,
|
|
554
|
+
},
|
|
555
|
+
);
|
|
556
|
+
|
|
557
|
+
resolved.deleted.forEach((identity) => {
|
|
558
|
+
const link = oldLinksInGroup.find(
|
|
559
|
+
(l) => createLinkIdentity(l) === identity,
|
|
560
|
+
);
|
|
561
|
+
if (!link) return;
|
|
562
|
+
transactions.push({
|
|
563
|
+
type: 'delete-attr',
|
|
564
|
+
identifier: {
|
|
565
|
+
attrName: link.forward.label,
|
|
566
|
+
namespace: link.forward.on,
|
|
567
|
+
},
|
|
568
|
+
});
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
resolved.created.forEach((identity) => {
|
|
572
|
+
const link = newLinksInGroup.find(
|
|
573
|
+
(l) => createLinkIdentity(l) === identity,
|
|
574
|
+
);
|
|
575
|
+
if (!link) return;
|
|
576
|
+
transactions.push(linkDefToNewAttrTx(link));
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
resolved.renamed.forEach((renamed) => {
|
|
580
|
+
const oldLink = oldLinksInGroup.find(
|
|
581
|
+
(l) => createLinkIdentity(l) === renamed.from,
|
|
582
|
+
);
|
|
583
|
+
const newLink = newLinksInGroup.find(
|
|
584
|
+
(l) => createLinkIdentity(l) === renamed.to,
|
|
585
|
+
);
|
|
586
|
+
if (!oldLink || !newLink) return;
|
|
587
|
+
|
|
588
|
+
transactions.push({
|
|
589
|
+
type: 'update-attr',
|
|
590
|
+
identifier: {
|
|
591
|
+
attrName: oldLink.forward.label,
|
|
592
|
+
namespace: oldLink.forward.on,
|
|
593
|
+
},
|
|
594
|
+
partialAttr: {
|
|
595
|
+
'value-type': 'ref',
|
|
596
|
+
cardinality: newLink.forward.has === 'one' ? 'one' : 'many',
|
|
597
|
+
'forward-identity': {
|
|
598
|
+
attrName: newLink.forward.label,
|
|
599
|
+
namespace: newLink.forward.on,
|
|
600
|
+
},
|
|
601
|
+
'reverse-identity': {
|
|
602
|
+
attrName: newLink.reverse.label,
|
|
603
|
+
namespace: newLink.reverse.on,
|
|
604
|
+
},
|
|
605
|
+
},
|
|
606
|
+
});
|
|
607
|
+
transactions.push(
|
|
608
|
+
...compareLinks(
|
|
609
|
+
{
|
|
610
|
+
attrName: oldLink.forward.label,
|
|
611
|
+
namespace: oldLink.forward.on,
|
|
612
|
+
},
|
|
613
|
+
oldLink,
|
|
614
|
+
newLink,
|
|
615
|
+
),
|
|
616
|
+
);
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
return transactions;
|
|
621
|
+
};
|
|
622
|
+
|
|
623
|
+
export interface RenamePromptItem<T> {
|
|
624
|
+
from: T;
|
|
625
|
+
to: T;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
export const compareBlobs = (
|
|
629
|
+
identity: Identifier,
|
|
630
|
+
oldBlob: AnyBlob,
|
|
631
|
+
newBlob: AnyBlob,
|
|
632
|
+
): MigrationTx[] => {
|
|
633
|
+
const results: MigrationTx[] = [];
|
|
634
|
+
const sendType = <T extends JobMigrationTypes>(type: T) => {
|
|
635
|
+
results.push({
|
|
636
|
+
type,
|
|
637
|
+
identifier: identity,
|
|
638
|
+
});
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
// check if index needs to be added
|
|
642
|
+
if (oldBlob.isIndexed === false && newBlob.isIndexed === true)
|
|
643
|
+
sendType('index');
|
|
644
|
+
|
|
645
|
+
// check if index needs to be removed
|
|
646
|
+
if (oldBlob.isIndexed === true && newBlob.isIndexed === false)
|
|
647
|
+
sendType('remove-index');
|
|
648
|
+
|
|
649
|
+
// check if needs to become unique
|
|
650
|
+
if (oldBlob.config.unique === false && newBlob.config.unique === true)
|
|
651
|
+
sendType('unique');
|
|
652
|
+
|
|
653
|
+
// check if needs to become non-unique
|
|
654
|
+
if (oldBlob.config.unique === true && newBlob.config.unique === false)
|
|
655
|
+
sendType('remove-unique');
|
|
656
|
+
|
|
657
|
+
// check if needs to become required
|
|
658
|
+
if (oldBlob.required === false && newBlob.required === true)
|
|
659
|
+
sendType('required');
|
|
660
|
+
|
|
661
|
+
// check if needs to become non-required
|
|
662
|
+
if (oldBlob.required === true && newBlob.required === false)
|
|
663
|
+
sendType('remove-required');
|
|
664
|
+
|
|
665
|
+
// check if data type needs to be changed / added
|
|
666
|
+
if (oldBlob.valueType !== 'json' && newBlob.valueType === 'json') {
|
|
667
|
+
results.push({
|
|
668
|
+
type: 'remove-data-type',
|
|
669
|
+
identifier: identity,
|
|
670
|
+
});
|
|
671
|
+
} else if (
|
|
672
|
+
oldBlob.valueType !== newBlob.valueType &&
|
|
673
|
+
newBlob.valueType !== 'json'
|
|
674
|
+
) {
|
|
675
|
+
results.push({
|
|
676
|
+
type: 'check-data-type',
|
|
677
|
+
identifier: identity,
|
|
678
|
+
'checked-data-type': newBlob.valueType,
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
return results;
|
|
683
|
+
};
|
|
684
|
+
|
|
685
|
+
export const compareLinks = (
|
|
686
|
+
identity: Identifier,
|
|
687
|
+
oldLink: AnyLink,
|
|
688
|
+
newLink: AnyLink,
|
|
689
|
+
): MigrationTx[] => {
|
|
690
|
+
const results: MigrationTx[] = [];
|
|
691
|
+
const oldRelationship =
|
|
692
|
+
`${oldLink.forward.has}-${oldLink.reverse.has}` as RelationshipKinds;
|
|
693
|
+
const { cardinality: oldCardinal, 'unique?': oldUnique } =
|
|
694
|
+
relationshipConstraints[oldRelationship];
|
|
695
|
+
const oldRequired = !!oldLink.forward.required;
|
|
696
|
+
|
|
697
|
+
const newRelationship =
|
|
698
|
+
`${newLink.forward.has}-${newLink.reverse.has}` as RelationshipKinds;
|
|
699
|
+
const { cardinality: newCardinal, 'unique?': newUnique } =
|
|
700
|
+
relationshipConstraints[newRelationship];
|
|
701
|
+
const newRequired = !!newLink.forward.required;
|
|
702
|
+
|
|
703
|
+
if (!oldUnique && newUnique) {
|
|
704
|
+
results.push({
|
|
705
|
+
type: 'unique',
|
|
706
|
+
identifier: identity,
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
if (!newUnique && newUnique !== oldUnique) {
|
|
710
|
+
results.push({
|
|
711
|
+
type: 'remove-unique',
|
|
712
|
+
identifier: identity,
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
if (newRequired !== oldRequired) {
|
|
717
|
+
results.push({
|
|
718
|
+
type: newRequired ? 'required' : 'remove-required',
|
|
719
|
+
identifier: identity,
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
if (
|
|
724
|
+
oldLink.reverse.onDelete !== newLink.reverse.onDelete ||
|
|
725
|
+
oldLink.forward.onDelete !== newLink.forward.onDelete ||
|
|
726
|
+
oldCardinal !== newCardinal
|
|
727
|
+
) {
|
|
728
|
+
results.push({
|
|
729
|
+
type: 'update-attr',
|
|
730
|
+
identifier: identity,
|
|
731
|
+
partialAttr: {
|
|
732
|
+
'value-type': 'ref',
|
|
733
|
+
'on-delete-reverse': newLink.reverse.onDelete,
|
|
734
|
+
'on-delete': newLink.forward.onDelete,
|
|
735
|
+
cardinality: newCardinal,
|
|
736
|
+
},
|
|
737
|
+
});
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
return results;
|
|
741
|
+
};
|
|
742
|
+
|
|
743
|
+
export const isRenamePromptItem = <T>(
|
|
744
|
+
item: RenamePromptItem<T> | T,
|
|
745
|
+
): item is RenamePromptItem<T> => {
|
|
746
|
+
if (typeof item === 'object') return true;
|
|
747
|
+
return false;
|
|
748
|
+
};
|
|
749
|
+
|
|
750
|
+
export type RenameResolveFn<T> = (
|
|
751
|
+
created: T,
|
|
752
|
+
promptData: (RenamePromptItem<T> | T)[],
|
|
753
|
+
extraInfo?: any,
|
|
754
|
+
) => Promise<T | RenamePromptItem<T>>;
|
|
755
|
+
|
|
756
|
+
const resolveRenames = async <T>(
|
|
757
|
+
newItems: T[],
|
|
758
|
+
missingItems: T[],
|
|
759
|
+
resolveFn: RenameResolveFn<T>,
|
|
760
|
+
extraInfo?: any,
|
|
761
|
+
): Promise<{
|
|
762
|
+
created: T[];
|
|
763
|
+
deleted: T[];
|
|
764
|
+
renamed: {
|
|
765
|
+
from: T;
|
|
766
|
+
to: T;
|
|
767
|
+
}[];
|
|
768
|
+
}> => {
|
|
769
|
+
if (missingItems.length === 0 || newItems.length === 0) {
|
|
770
|
+
return {
|
|
771
|
+
created: newItems,
|
|
772
|
+
deleted: missingItems,
|
|
773
|
+
renamed: [],
|
|
774
|
+
};
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
const result: {
|
|
778
|
+
created: T[];
|
|
779
|
+
renamed: { from: T; to: T }[];
|
|
780
|
+
deleted: T[];
|
|
781
|
+
} = { created: [], renamed: [], deleted: [] };
|
|
782
|
+
let index = 0;
|
|
783
|
+
let leftMissing = [...missingItems];
|
|
784
|
+
|
|
785
|
+
do {
|
|
786
|
+
const created = newItems[index];
|
|
787
|
+
const renames: RenamePromptItem<T>[] = leftMissing.map((it) => {
|
|
788
|
+
return { from: it, to: created };
|
|
789
|
+
});
|
|
790
|
+
const promptData: (RenamePromptItem<T> | T)[] = [created, ...renames];
|
|
791
|
+
|
|
792
|
+
const data = await resolveFn(created, promptData, extraInfo);
|
|
793
|
+
if (isRenamePromptItem(data)) {
|
|
794
|
+
if (data.from !== data.to) {
|
|
795
|
+
result.renamed.push(data);
|
|
796
|
+
}
|
|
797
|
+
delete leftMissing[leftMissing.indexOf(data.from)];
|
|
798
|
+
leftMissing = leftMissing.filter(Boolean);
|
|
799
|
+
} else {
|
|
800
|
+
result.created.push(created);
|
|
801
|
+
}
|
|
802
|
+
index += 1;
|
|
803
|
+
} while (index < newItems.length);
|
|
804
|
+
|
|
805
|
+
result.deleted.push(...leftMissing);
|
|
806
|
+
return result;
|
|
807
|
+
};
|
|
808
|
+
|
|
809
|
+
/**
|
|
810
|
+
* a `rename command` lets us know your intent to rename a particular entity
|
|
811
|
+
*
|
|
812
|
+
* The format is `from:to`, where `from` and `to` are lookups.
|
|
813
|
+
*
|
|
814
|
+
* For example, to rename `posts.name` to `posts.title`, a command could
|
|
815
|
+
* look like:
|
|
816
|
+
*
|
|
817
|
+
* `posts.name:posts.title`
|
|
818
|
+
*/
|
|
819
|
+
export type RenameCommand = `${string}.${string}:${string}.${string}`;
|
|
820
|
+
function validateRenameLookup(lookup: string) {
|
|
821
|
+
const [etype, label] = lookup.split('.').map((x) => x.trim());
|
|
822
|
+
if (!etype || !label) {
|
|
823
|
+
throw new Error(
|
|
824
|
+
`Invalid lookup. Got '${lookup}'. We expect a pattern like 'entityname.columname;.` +
|
|
825
|
+
'For example: posts.title',
|
|
826
|
+
);
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
// RenameMap goes from `to` -> `from`
|
|
831
|
+
function parseRenameCommands(renames: RenameCommand[]): Map<string, string> {
|
|
832
|
+
// Parse rename options: format is "from:to"
|
|
833
|
+
// note that it saves backwards since we will be testing against the base
|
|
834
|
+
// case of a created attr
|
|
835
|
+
const renameMap: Map<string, string> = new Map();
|
|
836
|
+
for (const renameStr of renames) {
|
|
837
|
+
let [from, to] = renameStr.split(':').map((x) => x.trim());
|
|
838
|
+
if (!from || !to) {
|
|
839
|
+
throw new Error(
|
|
840
|
+
`Invalid rename command: ${renameStr}. We could not parse a distinct 'from' and 'to'.` +
|
|
841
|
+
" The structure should look like 'from:to'. For example: 'posts.name:posts.title'",
|
|
842
|
+
);
|
|
843
|
+
}
|
|
844
|
+
validateRenameLookup(from);
|
|
845
|
+
validateRenameLookup(to);
|
|
846
|
+
renameMap.set(to.trim(), from.trim());
|
|
847
|
+
}
|
|
848
|
+
return renameMap;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
/**
|
|
852
|
+
* Given a list of RenameCommands, builds a cusotm `resolveFn` for
|
|
853
|
+
* `diffSchemas`, which automatically resolves rename conflicts with these commands.
|
|
854
|
+
*/
|
|
855
|
+
export function buildAutoRenameSelector(renames: RenameCommand[]) {
|
|
856
|
+
const renameMap = parseRenameCommands(renames);
|
|
857
|
+
|
|
858
|
+
const renameFn: RenameResolveFn<string> = async function (
|
|
859
|
+
created: string,
|
|
860
|
+
promptData: (RenamePromptItem<string> | string)[],
|
|
861
|
+
extraInfo: any,
|
|
862
|
+
): Promise<string | RenamePromptItem<string>> {
|
|
863
|
+
let lookupNames: string[] = [];
|
|
864
|
+
if (extraInfo?.type === 'attribute' && extraInfo?.entityName) {
|
|
865
|
+
lookupNames = [`${extraInfo.entityName}.${created}`];
|
|
866
|
+
} else if (extraInfo?.type === 'link') {
|
|
867
|
+
// Extract both forward and reverse parts
|
|
868
|
+
const parts = created.split('<->');
|
|
869
|
+
lookupNames = [parts[0], parts[1]];
|
|
870
|
+
} else {
|
|
871
|
+
return created;
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
// Try to find a match in the rename map using the lookup names
|
|
875
|
+
let fromAttr: string | null = null;
|
|
876
|
+
for (const lookupName of lookupNames) {
|
|
877
|
+
if (renameMap.has(lookupName)) {
|
|
878
|
+
fromAttr = renameMap.get(lookupName) || null;
|
|
879
|
+
break;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
if (fromAttr) {
|
|
884
|
+
let fromValue;
|
|
885
|
+
if (extraInfo?.type === 'attribute') {
|
|
886
|
+
fromValue = fromAttr.split('.').pop();
|
|
887
|
+
} else {
|
|
888
|
+
const matchingItem = promptData.find((item) => {
|
|
889
|
+
const itemStr = typeof item === 'string' ? item : item.from;
|
|
890
|
+
const itemParts = itemStr.split('<->');
|
|
891
|
+
return itemParts[0] === fromAttr || itemParts[1] === fromAttr;
|
|
892
|
+
});
|
|
893
|
+
|
|
894
|
+
if (matchingItem) {
|
|
895
|
+
fromValue =
|
|
896
|
+
typeof matchingItem === 'string' ? matchingItem : matchingItem.from;
|
|
897
|
+
} else {
|
|
898
|
+
return created;
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
const hasMatch = promptData.some((item) => {
|
|
903
|
+
if (typeof item === 'string') {
|
|
904
|
+
return item === fromValue;
|
|
905
|
+
} else if (item.from) {
|
|
906
|
+
return item.from === fromValue;
|
|
907
|
+
}
|
|
908
|
+
return false;
|
|
909
|
+
});
|
|
910
|
+
|
|
911
|
+
if (fromValue && hasMatch) {
|
|
912
|
+
return { from: fromValue, to: created };
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
return created;
|
|
917
|
+
};
|
|
918
|
+
|
|
919
|
+
return renameFn;
|
|
920
|
+
}
|