@sidekickdb/platform 0.0.0-stage → 2.0.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/.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 +445 -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 +74 -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,597 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isRenamePromptItem = exports.compareLinks = exports.compareBlobs = exports.diffSchemas = exports.convertTxSteps = void 0;
|
|
4
|
+
exports.buildAutoRenameSelector = buildAutoRenameSelector;
|
|
5
|
+
const core_1 = require("@sidekickdb/core");
|
|
6
|
+
const migrationUtils_ts_1 = require("./migrationUtils.js");
|
|
7
|
+
const relationships_ts_1 = require("./relationships.js");
|
|
8
|
+
const getExistingAttrThrowing = (ident, existingAttrs) => {
|
|
9
|
+
const found = existingAttrs.find((attr) => {
|
|
10
|
+
return (attr['forward-identity'][1] === ident.namespace &&
|
|
11
|
+
attr['forward-identity'][2] === ident.attrName);
|
|
12
|
+
}) || null;
|
|
13
|
+
if (!found) {
|
|
14
|
+
throw new Error(`Attribute ${ident.namespace}.${ident.attrName} not found`);
|
|
15
|
+
}
|
|
16
|
+
return found;
|
|
17
|
+
};
|
|
18
|
+
const convertSimpleConstraintUpdate = (tx, existing) => {
|
|
19
|
+
const found = getExistingAttrThrowing(tx.identifier, existing);
|
|
20
|
+
return {
|
|
21
|
+
'attr-id': found.id,
|
|
22
|
+
'forward-identity': found['forward-identity'],
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
// converts migration operations from Identifier (namespace/name) based
|
|
26
|
+
// into transaction steps that use database ids
|
|
27
|
+
const CONVERTERS = {
|
|
28
|
+
index: convertSimpleConstraintUpdate,
|
|
29
|
+
unique: convertSimpleConstraintUpdate,
|
|
30
|
+
required: convertSimpleConstraintUpdate,
|
|
31
|
+
'remove-index': convertSimpleConstraintUpdate,
|
|
32
|
+
'remove-unique': convertSimpleConstraintUpdate,
|
|
33
|
+
'remove-required': convertSimpleConstraintUpdate,
|
|
34
|
+
'delete-attr': (from, existing) => {
|
|
35
|
+
const found = getExistingAttrThrowing(from.identifier, existing);
|
|
36
|
+
return found.id;
|
|
37
|
+
},
|
|
38
|
+
'update-attr': (from, existing) => {
|
|
39
|
+
const found = getExistingAttrThrowing(from.identifier, existing);
|
|
40
|
+
return {
|
|
41
|
+
id: found.id,
|
|
42
|
+
'forward-identity': from.partialAttr['forward-identity']
|
|
43
|
+
? [
|
|
44
|
+
found.id,
|
|
45
|
+
from.partialAttr['forward-identity'].namespace,
|
|
46
|
+
from.partialAttr['forward-identity'].attrName,
|
|
47
|
+
]
|
|
48
|
+
: undefined,
|
|
49
|
+
'reverse-identity': from.partialAttr['reverse-identity']
|
|
50
|
+
? [
|
|
51
|
+
found.id,
|
|
52
|
+
from.partialAttr['reverse-identity'].namespace,
|
|
53
|
+
from.partialAttr['reverse-identity'].attrName,
|
|
54
|
+
]
|
|
55
|
+
: undefined,
|
|
56
|
+
cardinality: from.partialAttr['cardinality']
|
|
57
|
+
? from.partialAttr['cardinality']
|
|
58
|
+
: undefined,
|
|
59
|
+
'on-delete': from.partialAttr['on-delete']
|
|
60
|
+
? from.partialAttr['on-delete']
|
|
61
|
+
: undefined,
|
|
62
|
+
'on-delete-reverse': from.partialAttr['on-delete-reverse']
|
|
63
|
+
? from.partialAttr['on-delete-reverse']
|
|
64
|
+
: undefined,
|
|
65
|
+
};
|
|
66
|
+
},
|
|
67
|
+
'add-attr': (from, _existing) => {
|
|
68
|
+
const attrId = (0, core_1.id)();
|
|
69
|
+
const forwardIdentity = [
|
|
70
|
+
attrId,
|
|
71
|
+
from.identifier.namespace,
|
|
72
|
+
from.identifier.attrName,
|
|
73
|
+
];
|
|
74
|
+
const steps = [];
|
|
75
|
+
// First, create the attribute without unique, required, or indexed
|
|
76
|
+
steps.push([
|
|
77
|
+
'add-attr',
|
|
78
|
+
{
|
|
79
|
+
'forward-identity': forwardIdentity,
|
|
80
|
+
'reverse-identity': from['reverse-identity']
|
|
81
|
+
? [
|
|
82
|
+
(0, core_1.id)(),
|
|
83
|
+
from['reverse-identity'].namespace,
|
|
84
|
+
from['reverse-identity'].attrName,
|
|
85
|
+
]
|
|
86
|
+
: null,
|
|
87
|
+
'inferred-types': null,
|
|
88
|
+
'value-type': from['value-type'],
|
|
89
|
+
id: attrId,
|
|
90
|
+
cardinality: from.cardinality,
|
|
91
|
+
'index?': false,
|
|
92
|
+
'required?': false,
|
|
93
|
+
'unique?': false,
|
|
94
|
+
catalog: 'user',
|
|
95
|
+
'on-delete': from['on-delete'],
|
|
96
|
+
'on-delete-reverse': from['on-delete-reverse'],
|
|
97
|
+
'checked-data-type': from['checked-data-type'],
|
|
98
|
+
},
|
|
99
|
+
]);
|
|
100
|
+
// Then add separate steps for unique, required, and indexed
|
|
101
|
+
if (from['unique?']) {
|
|
102
|
+
steps.push([
|
|
103
|
+
'unique',
|
|
104
|
+
{
|
|
105
|
+
'attr-id': attrId,
|
|
106
|
+
'forward-identity': forwardIdentity,
|
|
107
|
+
},
|
|
108
|
+
]);
|
|
109
|
+
}
|
|
110
|
+
if (from['required?']) {
|
|
111
|
+
steps.push([
|
|
112
|
+
'required',
|
|
113
|
+
{
|
|
114
|
+
'attr-id': attrId,
|
|
115
|
+
'forward-identity': forwardIdentity,
|
|
116
|
+
},
|
|
117
|
+
]);
|
|
118
|
+
}
|
|
119
|
+
if (from['index?']) {
|
|
120
|
+
steps.push([
|
|
121
|
+
'index',
|
|
122
|
+
{
|
|
123
|
+
'attr-id': attrId,
|
|
124
|
+
'forward-identity': forwardIdentity,
|
|
125
|
+
},
|
|
126
|
+
]);
|
|
127
|
+
}
|
|
128
|
+
return steps;
|
|
129
|
+
},
|
|
130
|
+
'remove-data-type': convertSimpleConstraintUpdate,
|
|
131
|
+
'check-data-type': (from, existing) => {
|
|
132
|
+
const found = getExistingAttrThrowing(from.identifier, existing);
|
|
133
|
+
return {
|
|
134
|
+
'attr-id': found.id,
|
|
135
|
+
'checked-data-type': from['checked-data-type'],
|
|
136
|
+
'forward-identity': found['forward-identity'],
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
function isSystemCatalogAttr(systemCatalogIdentNames, entityName, attrName) {
|
|
141
|
+
return !!systemCatalogIdentNames[entityName]?.has(attrName);
|
|
142
|
+
}
|
|
143
|
+
const convertTxSteps = (txs, existingAttrs) => {
|
|
144
|
+
if (!existingAttrs) {
|
|
145
|
+
throw new Error('Existing attributes are required');
|
|
146
|
+
}
|
|
147
|
+
const result = [];
|
|
148
|
+
txs.forEach((tx) => {
|
|
149
|
+
const converter = CONVERTERS[tx.type];
|
|
150
|
+
if (!converter) {
|
|
151
|
+
throw new Error(`Unknown transaction type: ${tx.type}`);
|
|
152
|
+
}
|
|
153
|
+
const converted = converter(tx, existingAttrs);
|
|
154
|
+
if (Array.isArray(converted)) {
|
|
155
|
+
result.push(...converted);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
result.push([tx.type, converted]);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
return result;
|
|
162
|
+
};
|
|
163
|
+
exports.convertTxSteps = convertTxSteps;
|
|
164
|
+
const diffSchemas = async (oldSchema, newSchema, resolveFn, systemCatalogIdentNames) => {
|
|
165
|
+
const transactions = [];
|
|
166
|
+
const oldEntities = oldSchema.entities;
|
|
167
|
+
const newEntities = newSchema.entities;
|
|
168
|
+
const oldEntityNames = Object.keys(oldEntities);
|
|
169
|
+
const newEntityNames = Object.keys(newEntities);
|
|
170
|
+
const deletedEntityNames = oldEntityNames.filter((name) => !newEntityNames.includes(name));
|
|
171
|
+
for (const entityName of deletedEntityNames) {
|
|
172
|
+
Object.keys(oldEntities[entityName].attrs).forEach((attrName) => {
|
|
173
|
+
if (isSystemCatalogAttr(systemCatalogIdentNames, entityName, attrName))
|
|
174
|
+
return;
|
|
175
|
+
transactions.push({
|
|
176
|
+
type: 'delete-attr',
|
|
177
|
+
identifier: {
|
|
178
|
+
attrName,
|
|
179
|
+
namespace: entityName,
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
if (isSystemCatalogAttr(systemCatalogIdentNames, entityName, 'id')) {
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
transactions.push({
|
|
187
|
+
type: 'delete-attr',
|
|
188
|
+
identifier: {
|
|
189
|
+
attrName: 'id',
|
|
190
|
+
namespace: entityName,
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
const addedEntityNames = newEntityNames.filter((name) => !oldEntityNames.includes(name));
|
|
195
|
+
for (const entityName of addedEntityNames) {
|
|
196
|
+
if (!isSystemCatalogAttr(systemCatalogIdentNames, entityName, 'id')) {
|
|
197
|
+
transactions.push({
|
|
198
|
+
type: 'add-attr',
|
|
199
|
+
'forward-identity': {
|
|
200
|
+
namespace: entityName,
|
|
201
|
+
attrName: 'id',
|
|
202
|
+
},
|
|
203
|
+
identifier: {
|
|
204
|
+
attrName: 'id',
|
|
205
|
+
namespace: entityName,
|
|
206
|
+
},
|
|
207
|
+
'index?': false,
|
|
208
|
+
'required?': true,
|
|
209
|
+
cardinality: 'one',
|
|
210
|
+
'unique?': true,
|
|
211
|
+
'value-type': 'blob',
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
for (const attrName of Object.keys(newEntities[entityName].attrs)) {
|
|
215
|
+
if (isSystemCatalogAttr(systemCatalogIdentNames, entityName, attrName)) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
const attrInSchema = newEntities[entityName].attrs[attrName];
|
|
219
|
+
transactions.push((0, migrationUtils_ts_1.attrDefToNewAttrTx)(attrInSchema, entityName, attrName));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const innerEntityNames = oldEntityNames.filter((name) => newEntityNames.includes(name));
|
|
223
|
+
for (const entityName of innerEntityNames) {
|
|
224
|
+
// BLOB ATTRIBUTES
|
|
225
|
+
const addedFields = Object.keys(newEntities[entityName].attrs).filter((field) => {
|
|
226
|
+
if (isSystemCatalogAttr(systemCatalogIdentNames, entityName, field))
|
|
227
|
+
return false;
|
|
228
|
+
const oldEntityHasIt = Object.keys(oldEntities[entityName].attrs).includes(field);
|
|
229
|
+
return !oldEntityHasIt;
|
|
230
|
+
});
|
|
231
|
+
const removedFields = Object.keys(oldEntities[entityName].attrs).filter((field) => {
|
|
232
|
+
if (isSystemCatalogAttr(systemCatalogIdentNames, entityName, field))
|
|
233
|
+
return false;
|
|
234
|
+
const newEntityHasIt = Object.keys(newEntities[entityName].attrs).includes(field);
|
|
235
|
+
return !newEntityHasIt;
|
|
236
|
+
});
|
|
237
|
+
const resolved = await resolveRenames(addedFields, removedFields, resolveFn, {
|
|
238
|
+
type: 'attribute',
|
|
239
|
+
entityName: entityName,
|
|
240
|
+
});
|
|
241
|
+
const consistentFields = Object.keys(oldEntities[entityName].attrs).filter((field) => Object.keys(newEntities[entityName].attrs).includes(field));
|
|
242
|
+
consistentFields.forEach((fieldName) => {
|
|
243
|
+
if (isSystemCatalogAttr(systemCatalogIdentNames, entityName, fieldName)) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
transactions.push(...(0, exports.compareBlobs)({
|
|
247
|
+
attrName: fieldName,
|
|
248
|
+
namespace: entityName,
|
|
249
|
+
}, oldEntities[entityName].attrs[fieldName], newEntities[entityName].attrs[fieldName]));
|
|
250
|
+
});
|
|
251
|
+
resolved.deleted.forEach((attrName) => {
|
|
252
|
+
transactions.push({
|
|
253
|
+
type: 'delete-attr',
|
|
254
|
+
identifier: {
|
|
255
|
+
attrName,
|
|
256
|
+
namespace: entityName,
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
resolved.created.forEach((createdName) => {
|
|
261
|
+
const attrInSchema = newSchema.entities[entityName].attrs[createdName];
|
|
262
|
+
transactions.push((0, migrationUtils_ts_1.attrDefToNewAttrTx)(attrInSchema, entityName, createdName));
|
|
263
|
+
});
|
|
264
|
+
resolved.renamed.forEach((renamed) => {
|
|
265
|
+
transactions.push({
|
|
266
|
+
type: 'update-attr',
|
|
267
|
+
identifier: {
|
|
268
|
+
attrName: renamed.from,
|
|
269
|
+
namespace: entityName,
|
|
270
|
+
},
|
|
271
|
+
partialAttr: {
|
|
272
|
+
'value-type': 'blob',
|
|
273
|
+
cardinality: 'one',
|
|
274
|
+
'forward-identity': {
|
|
275
|
+
attrName: renamed.to,
|
|
276
|
+
namespace: entityName,
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
transactions.push(...(0, exports.compareBlobs)({
|
|
281
|
+
attrName: renamed.from,
|
|
282
|
+
namespace: entityName,
|
|
283
|
+
}, oldEntities[entityName].attrs[renamed.from], newEntities[entityName].attrs[renamed.to]));
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
const oldLinks = Object.values(oldSchema.links).filter((link) => !isSystemCatalogAttr(systemCatalogIdentNames, link.forward.on, link.forward.label));
|
|
287
|
+
const newLinks = Object.values(newSchema.links).filter((link) => !isSystemCatalogAttr(systemCatalogIdentNames, link.forward.on, link.forward.label));
|
|
288
|
+
// Group links by their forward namespace-label combination for comparison
|
|
289
|
+
const createLinkKey = (link) => `${link.forward.on}<->${link.reverse.on}`;
|
|
290
|
+
const createLinkIdentity = (link) => `${link.forward.on}.${link.forward.label}<->${link.reverse.on}.${link.reverse.label}`;
|
|
291
|
+
const newLinksByKey = new Map();
|
|
292
|
+
const oldLinksByKey = new Map();
|
|
293
|
+
for (const link of newLinks) {
|
|
294
|
+
const key = createLinkKey(link);
|
|
295
|
+
const links = newLinksByKey.get(key) || [];
|
|
296
|
+
links.push(link);
|
|
297
|
+
newLinksByKey.set(key, links);
|
|
298
|
+
}
|
|
299
|
+
for (const link of oldLinks) {
|
|
300
|
+
const key = createLinkKey(link);
|
|
301
|
+
const links = oldLinksByKey.get(key) || [];
|
|
302
|
+
links.push(link);
|
|
303
|
+
oldLinksByKey.set(key, links);
|
|
304
|
+
}
|
|
305
|
+
const allLinkKeys = new Set([
|
|
306
|
+
...oldLinksByKey.keys(),
|
|
307
|
+
...newLinksByKey.keys(),
|
|
308
|
+
]);
|
|
309
|
+
for (const linkKey of allLinkKeys) {
|
|
310
|
+
const oldLinksInGroup = oldLinksByKey.get(linkKey) || [];
|
|
311
|
+
const newLinksInGroup = newLinksByKey.get(linkKey) || [];
|
|
312
|
+
const oldIdentities = oldLinksInGroup.map((link) => createLinkIdentity(link));
|
|
313
|
+
const newIdentities = newLinksInGroup.map((link) => createLinkIdentity(link));
|
|
314
|
+
const addedIdentities = newIdentities.filter((identity) => !oldIdentities.includes(identity));
|
|
315
|
+
const removedIdentities = oldIdentities.filter((identity) => !newIdentities.includes(identity));
|
|
316
|
+
const consistentIdentities = oldIdentities.filter((identity) => newIdentities.includes(identity));
|
|
317
|
+
consistentIdentities.forEach((identity) => {
|
|
318
|
+
const oldLink = oldLinksInGroup.find((l) => createLinkIdentity(l) === identity);
|
|
319
|
+
const newLink = newLinksInGroup.find((l) => createLinkIdentity(l) === identity);
|
|
320
|
+
if (!oldLink || !newLink)
|
|
321
|
+
return;
|
|
322
|
+
transactions.push(...(0, exports.compareLinks)({
|
|
323
|
+
namespace: oldLink.forward.on,
|
|
324
|
+
attrName: oldLink.forward.label,
|
|
325
|
+
}, oldLink, newLink));
|
|
326
|
+
});
|
|
327
|
+
const resolved = await resolveRenames(addedIdentities, removedIdentities, resolveFn, {
|
|
328
|
+
type: 'link',
|
|
329
|
+
forwardEntityName: oldLinksInGroup[0]?.forward.on,
|
|
330
|
+
reverseEntityName: oldLinksInGroup[0]?.reverse.on,
|
|
331
|
+
});
|
|
332
|
+
resolved.deleted.forEach((identity) => {
|
|
333
|
+
const link = oldLinksInGroup.find((l) => createLinkIdentity(l) === identity);
|
|
334
|
+
if (!link)
|
|
335
|
+
return;
|
|
336
|
+
transactions.push({
|
|
337
|
+
type: 'delete-attr',
|
|
338
|
+
identifier: {
|
|
339
|
+
attrName: link.forward.label,
|
|
340
|
+
namespace: link.forward.on,
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
resolved.created.forEach((identity) => {
|
|
345
|
+
const link = newLinksInGroup.find((l) => createLinkIdentity(l) === identity);
|
|
346
|
+
if (!link)
|
|
347
|
+
return;
|
|
348
|
+
transactions.push((0, migrationUtils_ts_1.linkDefToNewAttrTx)(link));
|
|
349
|
+
});
|
|
350
|
+
resolved.renamed.forEach((renamed) => {
|
|
351
|
+
const oldLink = oldLinksInGroup.find((l) => createLinkIdentity(l) === renamed.from);
|
|
352
|
+
const newLink = newLinksInGroup.find((l) => createLinkIdentity(l) === renamed.to);
|
|
353
|
+
if (!oldLink || !newLink)
|
|
354
|
+
return;
|
|
355
|
+
transactions.push({
|
|
356
|
+
type: 'update-attr',
|
|
357
|
+
identifier: {
|
|
358
|
+
attrName: oldLink.forward.label,
|
|
359
|
+
namespace: oldLink.forward.on,
|
|
360
|
+
},
|
|
361
|
+
partialAttr: {
|
|
362
|
+
'value-type': 'ref',
|
|
363
|
+
cardinality: newLink.forward.has === 'one' ? 'one' : 'many',
|
|
364
|
+
'forward-identity': {
|
|
365
|
+
attrName: newLink.forward.label,
|
|
366
|
+
namespace: newLink.forward.on,
|
|
367
|
+
},
|
|
368
|
+
'reverse-identity': {
|
|
369
|
+
attrName: newLink.reverse.label,
|
|
370
|
+
namespace: newLink.reverse.on,
|
|
371
|
+
},
|
|
372
|
+
},
|
|
373
|
+
});
|
|
374
|
+
transactions.push(...(0, exports.compareLinks)({
|
|
375
|
+
attrName: oldLink.forward.label,
|
|
376
|
+
namespace: oldLink.forward.on,
|
|
377
|
+
}, oldLink, newLink));
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
return transactions;
|
|
381
|
+
};
|
|
382
|
+
exports.diffSchemas = diffSchemas;
|
|
383
|
+
const compareBlobs = (identity, oldBlob, newBlob) => {
|
|
384
|
+
const results = [];
|
|
385
|
+
const sendType = (type) => {
|
|
386
|
+
results.push({
|
|
387
|
+
type,
|
|
388
|
+
identifier: identity,
|
|
389
|
+
});
|
|
390
|
+
};
|
|
391
|
+
// check if index needs to be added
|
|
392
|
+
if (oldBlob.isIndexed === false && newBlob.isIndexed === true)
|
|
393
|
+
sendType('index');
|
|
394
|
+
// check if index needs to be removed
|
|
395
|
+
if (oldBlob.isIndexed === true && newBlob.isIndexed === false)
|
|
396
|
+
sendType('remove-index');
|
|
397
|
+
// check if needs to become unique
|
|
398
|
+
if (oldBlob.config.unique === false && newBlob.config.unique === true)
|
|
399
|
+
sendType('unique');
|
|
400
|
+
// check if needs to become non-unique
|
|
401
|
+
if (oldBlob.config.unique === true && newBlob.config.unique === false)
|
|
402
|
+
sendType('remove-unique');
|
|
403
|
+
// check if needs to become required
|
|
404
|
+
if (oldBlob.required === false && newBlob.required === true)
|
|
405
|
+
sendType('required');
|
|
406
|
+
// check if needs to become non-required
|
|
407
|
+
if (oldBlob.required === true && newBlob.required === false)
|
|
408
|
+
sendType('remove-required');
|
|
409
|
+
// check if data type needs to be changed / added
|
|
410
|
+
if (oldBlob.valueType !== 'json' && newBlob.valueType === 'json') {
|
|
411
|
+
results.push({
|
|
412
|
+
type: 'remove-data-type',
|
|
413
|
+
identifier: identity,
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
else if (oldBlob.valueType !== newBlob.valueType &&
|
|
417
|
+
newBlob.valueType !== 'json') {
|
|
418
|
+
results.push({
|
|
419
|
+
type: 'check-data-type',
|
|
420
|
+
identifier: identity,
|
|
421
|
+
'checked-data-type': newBlob.valueType,
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
return results;
|
|
425
|
+
};
|
|
426
|
+
exports.compareBlobs = compareBlobs;
|
|
427
|
+
const compareLinks = (identity, oldLink, newLink) => {
|
|
428
|
+
const results = [];
|
|
429
|
+
const oldRelationship = `${oldLink.forward.has}-${oldLink.reverse.has}`;
|
|
430
|
+
const { cardinality: oldCardinal, 'unique?': oldUnique } = relationships_ts_1.relationshipConstraints[oldRelationship];
|
|
431
|
+
const oldRequired = !!oldLink.forward.required;
|
|
432
|
+
const newRelationship = `${newLink.forward.has}-${newLink.reverse.has}`;
|
|
433
|
+
const { cardinality: newCardinal, 'unique?': newUnique } = relationships_ts_1.relationshipConstraints[newRelationship];
|
|
434
|
+
const newRequired = !!newLink.forward.required;
|
|
435
|
+
if (!oldUnique && newUnique) {
|
|
436
|
+
results.push({
|
|
437
|
+
type: 'unique',
|
|
438
|
+
identifier: identity,
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
if (!newUnique && newUnique !== oldUnique) {
|
|
442
|
+
results.push({
|
|
443
|
+
type: 'remove-unique',
|
|
444
|
+
identifier: identity,
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
if (newRequired !== oldRequired) {
|
|
448
|
+
results.push({
|
|
449
|
+
type: newRequired ? 'required' : 'remove-required',
|
|
450
|
+
identifier: identity,
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
if (oldLink.reverse.onDelete !== newLink.reverse.onDelete ||
|
|
454
|
+
oldLink.forward.onDelete !== newLink.forward.onDelete ||
|
|
455
|
+
oldCardinal !== newCardinal) {
|
|
456
|
+
results.push({
|
|
457
|
+
type: 'update-attr',
|
|
458
|
+
identifier: identity,
|
|
459
|
+
partialAttr: {
|
|
460
|
+
'value-type': 'ref',
|
|
461
|
+
'on-delete-reverse': newLink.reverse.onDelete,
|
|
462
|
+
'on-delete': newLink.forward.onDelete,
|
|
463
|
+
cardinality: newCardinal,
|
|
464
|
+
},
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
return results;
|
|
468
|
+
};
|
|
469
|
+
exports.compareLinks = compareLinks;
|
|
470
|
+
const isRenamePromptItem = (item) => {
|
|
471
|
+
if (typeof item === 'object')
|
|
472
|
+
return true;
|
|
473
|
+
return false;
|
|
474
|
+
};
|
|
475
|
+
exports.isRenamePromptItem = isRenamePromptItem;
|
|
476
|
+
const resolveRenames = async (newItems, missingItems, resolveFn, extraInfo) => {
|
|
477
|
+
if (missingItems.length === 0 || newItems.length === 0) {
|
|
478
|
+
return {
|
|
479
|
+
created: newItems,
|
|
480
|
+
deleted: missingItems,
|
|
481
|
+
renamed: [],
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
const result = { created: [], renamed: [], deleted: [] };
|
|
485
|
+
let index = 0;
|
|
486
|
+
let leftMissing = [...missingItems];
|
|
487
|
+
do {
|
|
488
|
+
const created = newItems[index];
|
|
489
|
+
const renames = leftMissing.map((it) => {
|
|
490
|
+
return { from: it, to: created };
|
|
491
|
+
});
|
|
492
|
+
const promptData = [created, ...renames];
|
|
493
|
+
const data = await resolveFn(created, promptData, extraInfo);
|
|
494
|
+
if ((0, exports.isRenamePromptItem)(data)) {
|
|
495
|
+
if (data.from !== data.to) {
|
|
496
|
+
result.renamed.push(data);
|
|
497
|
+
}
|
|
498
|
+
delete leftMissing[leftMissing.indexOf(data.from)];
|
|
499
|
+
leftMissing = leftMissing.filter(Boolean);
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
result.created.push(created);
|
|
503
|
+
}
|
|
504
|
+
index += 1;
|
|
505
|
+
} while (index < newItems.length);
|
|
506
|
+
result.deleted.push(...leftMissing);
|
|
507
|
+
return result;
|
|
508
|
+
};
|
|
509
|
+
function validateRenameLookup(lookup) {
|
|
510
|
+
const [etype, label] = lookup.split('.').map((x) => x.trim());
|
|
511
|
+
if (!etype || !label) {
|
|
512
|
+
throw new Error(`Invalid lookup. Got '${lookup}'. We expect a pattern like 'entityname.columname;.` +
|
|
513
|
+
'For example: posts.title');
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
// RenameMap goes from `to` -> `from`
|
|
517
|
+
function parseRenameCommands(renames) {
|
|
518
|
+
// Parse rename options: format is "from:to"
|
|
519
|
+
// note that it saves backwards since we will be testing against the base
|
|
520
|
+
// case of a created attr
|
|
521
|
+
const renameMap = new Map();
|
|
522
|
+
for (const renameStr of renames) {
|
|
523
|
+
let [from, to] = renameStr.split(':').map((x) => x.trim());
|
|
524
|
+
if (!from || !to) {
|
|
525
|
+
throw new Error(`Invalid rename command: ${renameStr}. We could not parse a distinct 'from' and 'to'.` +
|
|
526
|
+
" The structure should look like 'from:to'. For example: 'posts.name:posts.title'");
|
|
527
|
+
}
|
|
528
|
+
validateRenameLookup(from);
|
|
529
|
+
validateRenameLookup(to);
|
|
530
|
+
renameMap.set(to.trim(), from.trim());
|
|
531
|
+
}
|
|
532
|
+
return renameMap;
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Given a list of RenameCommands, builds a cusotm `resolveFn` for
|
|
536
|
+
* `diffSchemas`, which automatically resolves rename conflicts with these commands.
|
|
537
|
+
*/
|
|
538
|
+
function buildAutoRenameSelector(renames) {
|
|
539
|
+
const renameMap = parseRenameCommands(renames);
|
|
540
|
+
const renameFn = async function (created, promptData, extraInfo) {
|
|
541
|
+
let lookupNames = [];
|
|
542
|
+
if (extraInfo?.type === 'attribute' && extraInfo?.entityName) {
|
|
543
|
+
lookupNames = [`${extraInfo.entityName}.${created}`];
|
|
544
|
+
}
|
|
545
|
+
else if (extraInfo?.type === 'link') {
|
|
546
|
+
// Extract both forward and reverse parts
|
|
547
|
+
const parts = created.split('<->');
|
|
548
|
+
lookupNames = [parts[0], parts[1]];
|
|
549
|
+
}
|
|
550
|
+
else {
|
|
551
|
+
return created;
|
|
552
|
+
}
|
|
553
|
+
// Try to find a match in the rename map using the lookup names
|
|
554
|
+
let fromAttr = null;
|
|
555
|
+
for (const lookupName of lookupNames) {
|
|
556
|
+
if (renameMap.has(lookupName)) {
|
|
557
|
+
fromAttr = renameMap.get(lookupName) || null;
|
|
558
|
+
break;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
if (fromAttr) {
|
|
562
|
+
let fromValue;
|
|
563
|
+
if (extraInfo?.type === 'attribute') {
|
|
564
|
+
fromValue = fromAttr.split('.').pop();
|
|
565
|
+
}
|
|
566
|
+
else {
|
|
567
|
+
const matchingItem = promptData.find((item) => {
|
|
568
|
+
const itemStr = typeof item === 'string' ? item : item.from;
|
|
569
|
+
const itemParts = itemStr.split('<->');
|
|
570
|
+
return itemParts[0] === fromAttr || itemParts[1] === fromAttr;
|
|
571
|
+
});
|
|
572
|
+
if (matchingItem) {
|
|
573
|
+
fromValue =
|
|
574
|
+
typeof matchingItem === 'string' ? matchingItem : matchingItem.from;
|
|
575
|
+
}
|
|
576
|
+
else {
|
|
577
|
+
return created;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
const hasMatch = promptData.some((item) => {
|
|
581
|
+
if (typeof item === 'string') {
|
|
582
|
+
return item === fromValue;
|
|
583
|
+
}
|
|
584
|
+
else if (item.from) {
|
|
585
|
+
return item.from === fromValue;
|
|
586
|
+
}
|
|
587
|
+
return false;
|
|
588
|
+
});
|
|
589
|
+
if (fromValue && hasMatch) {
|
|
590
|
+
return { from: fromValue, to: created };
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
return created;
|
|
594
|
+
};
|
|
595
|
+
return renameFn;
|
|
596
|
+
}
|
|
597
|
+
//# sourceMappingURL=migrations.js.map
|