rads-db 3.0.19 → 3.0.21
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/dist/index.cjs +49 -55
- package/dist/index.d.ts +7 -1
- package/dist/index.mjs +49 -55
- package/drivers/azureCosmos.cjs +15 -17
- package/drivers/azureCosmos.mjs +7 -10
- package/drivers/azureStorageBlob.cjs +19 -20
- package/drivers/azureStorageBlob.mjs +17 -20
- package/drivers/memory.cjs +30 -19
- package/drivers/memory.mjs +20 -15
- package/integrations/lib.cjs +229 -171
- package/integrations/lib.d.ts +1 -1
- package/integrations/lib.mjs +198 -130
- package/package.json +1 -1
package/drivers/memory.mjs
CHANGED
|
@@ -31,20 +31,37 @@ const operatorFns = {
|
|
|
31
31
|
}
|
|
32
32
|
};
|
|
33
33
|
export default (options) => (schema, entity) => {
|
|
34
|
-
|
|
34
|
+
const itemsById = {};
|
|
35
35
|
function getItemById(id) {
|
|
36
36
|
return itemsById[id] ?? null;
|
|
37
37
|
}
|
|
38
38
|
function getItemByIds(ids) {
|
|
39
39
|
return ids.map((id) => itemsById[id]);
|
|
40
40
|
}
|
|
41
|
+
function getMany(args) {
|
|
42
|
+
args = args || {};
|
|
43
|
+
const where = args.where || {};
|
|
44
|
+
const whereKeys = _.keys(where);
|
|
45
|
+
if (whereKeys.length === 1) {
|
|
46
|
+
if (whereKeys[0] === "id")
|
|
47
|
+
return { nodes: _.cloneDeep([getItemById(where.id)].filter((x) => x)), cursor: null };
|
|
48
|
+
if (whereKeys[0] === "id_in")
|
|
49
|
+
return { nodes: _.cloneDeep(getItemByIds(where.id_in).filter((x) => x)), cursor: null };
|
|
50
|
+
}
|
|
51
|
+
return queryArray(Object.values(itemsById), args);
|
|
52
|
+
}
|
|
41
53
|
const instance = {
|
|
42
54
|
driverName: "memory",
|
|
43
55
|
get itemsById() {
|
|
44
56
|
return itemsById;
|
|
45
57
|
},
|
|
46
|
-
|
|
47
|
-
|
|
58
|
+
getMany,
|
|
59
|
+
deleteMany(args) {
|
|
60
|
+
const { nodes, cursor } = getMany(args);
|
|
61
|
+
for (const node of nodes) {
|
|
62
|
+
delete itemsById[node.id];
|
|
63
|
+
}
|
|
64
|
+
return { nodes, cursor };
|
|
48
65
|
},
|
|
49
66
|
putMany(items) {
|
|
50
67
|
for (const item of items) {
|
|
@@ -52,18 +69,6 @@ export default (options) => (schema, entity) => {
|
|
|
52
69
|
throw new Error(`You must provide an id`);
|
|
53
70
|
itemsById[item.id] = item;
|
|
54
71
|
}
|
|
55
|
-
},
|
|
56
|
-
getMany(args) {
|
|
57
|
-
args = args || {};
|
|
58
|
-
const where = args.where || {};
|
|
59
|
-
const whereKeys = _.keys(where);
|
|
60
|
-
if (whereKeys.length === 1) {
|
|
61
|
-
if (whereKeys[0] === "id")
|
|
62
|
-
return { nodes: _.cloneDeep([getItemById(where.id)].filter((x) => x)), cursor: null };
|
|
63
|
-
if (whereKeys[0] === "id_in")
|
|
64
|
-
return { nodes: _.cloneDeep(getItemByIds(where.id_in).filter((x) => x)), cursor: null };
|
|
65
|
-
}
|
|
66
|
-
return queryArray(Object.values(itemsById), args);
|
|
67
72
|
}
|
|
68
73
|
};
|
|
69
74
|
return instance;
|
package/integrations/lib.cjs
CHANGED
|
@@ -10,8 +10,77 @@ var _pluralize = _interopRequireDefault(require("pluralize"));
|
|
|
10
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
11
|
const supportedPrimitiveTypes = ["string", "number", "boolean", "Record<string, string>", "Record<string, any>"];
|
|
12
12
|
function parseSchema(typescriptFiles) {
|
|
13
|
+
const typeNodesMap = getTypeNodesMap(typescriptFiles);
|
|
14
|
+
const schema = getSchema(typeNodesMap);
|
|
15
|
+
resolveIsExtending(schema);
|
|
16
|
+
fillComputedDefinitionsForType(schema);
|
|
17
|
+
verifyDefaultValueTypes(schema, typeNodesMap);
|
|
18
|
+
verifyRelationFields(schema);
|
|
19
|
+
return schema;
|
|
20
|
+
}
|
|
21
|
+
function resolveIsExtending(schema) {
|
|
22
|
+
for (const key in schema) {
|
|
23
|
+
const isExtendingType = schema[key].isExtending;
|
|
24
|
+
if (!isExtendingType) continue;
|
|
25
|
+
if (!schema[isExtendingType]) throw new Error(`Unknown type: "${isExtendingType}"`);
|
|
26
|
+
schema[key].fields = {
|
|
27
|
+
...schema[isExtendingType].fields,
|
|
28
|
+
...schema[key].fields
|
|
29
|
+
};
|
|
30
|
+
schema[key].decorators = {
|
|
31
|
+
...schema[isExtendingType].decorators,
|
|
32
|
+
...schema[key].decorators
|
|
33
|
+
};
|
|
34
|
+
if (schema[key].decorators.entity && !schema[key].fields?.id) {
|
|
35
|
+
throw new Error(`Entity "${key}" must have an id`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function getSchema(typeNodesMap) {
|
|
13
40
|
const result = {};
|
|
14
|
-
const
|
|
41
|
+
for (const key in typeNodesMap) {
|
|
42
|
+
const {
|
|
43
|
+
node,
|
|
44
|
+
name
|
|
45
|
+
} = typeNodesMap[key];
|
|
46
|
+
if (![_typescript.SyntaxKind.ClassDeclaration, _typescript.SyntaxKind.TypeAliasDeclaration].includes(node.kind)) {
|
|
47
|
+
throw new Error(`Unexpected type kind - "${name}"`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
for (const key in typeNodesMap) {
|
|
51
|
+
if (result[key]) continue;
|
|
52
|
+
const {
|
|
53
|
+
node,
|
|
54
|
+
sourceFile,
|
|
55
|
+
name
|
|
56
|
+
} = typeNodesMap[key];
|
|
57
|
+
if (node.kind !== _typescript.SyntaxKind.ClassDeclaration) continue;
|
|
58
|
+
const parsedClass = parseClassDeclaration(node, name, {
|
|
59
|
+
typeNodesMap,
|
|
60
|
+
sourceFile,
|
|
61
|
+
result
|
|
62
|
+
});
|
|
63
|
+
result[key] = parsedClass;
|
|
64
|
+
}
|
|
65
|
+
for (const key in typeNodesMap) {
|
|
66
|
+
if (result[key]) continue;
|
|
67
|
+
const {
|
|
68
|
+
node,
|
|
69
|
+
sourceFile,
|
|
70
|
+
name
|
|
71
|
+
} = typeNodesMap[key];
|
|
72
|
+
if (node.kind !== _typescript.SyntaxKind.TypeAliasDeclaration) continue;
|
|
73
|
+
const parsedClass = parseTypeAliasDeclaration(node, name, {
|
|
74
|
+
typeNodesMap,
|
|
75
|
+
sourceFile,
|
|
76
|
+
result
|
|
77
|
+
});
|
|
78
|
+
result[key] = parsedClass;
|
|
79
|
+
}
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
function getTypeNodesMap(typescriptFiles) {
|
|
83
|
+
const typeNodesMap = {};
|
|
15
84
|
for (const key in typescriptFiles) {
|
|
16
85
|
const text = typescriptFiles[key];
|
|
17
86
|
const sourceFile = (0, _typescript.createSourceFile)(`${key}.ts`, text, _typescript.ScriptTarget.Latest);
|
|
@@ -20,75 +89,44 @@ function parseSchema(typescriptFiles) {
|
|
|
20
89
|
const nameNode = cd.name;
|
|
21
90
|
if (!nameNode || nameNode.kind !== _typescript.SyntaxKind.Identifier) throw new Error("Cannot detect class name");
|
|
22
91
|
const name = nameNode.text;
|
|
23
|
-
|
|
92
|
+
typeNodesMap[name] = {
|
|
24
93
|
name,
|
|
25
94
|
node: cd,
|
|
26
95
|
sourceFile
|
|
27
96
|
};
|
|
28
97
|
}
|
|
29
|
-
if (!
|
|
98
|
+
if (!typeNodesMap[key]) {
|
|
30
99
|
throw new Error(`File ${key}.ts must contain class declaration with name "${key}"`);
|
|
31
100
|
}
|
|
32
101
|
}
|
|
33
|
-
|
|
34
|
-
if (result[key]) continue;
|
|
35
|
-
const {
|
|
36
|
-
node,
|
|
37
|
-
sourceFile,
|
|
38
|
-
name
|
|
39
|
-
} = typesMap[key];
|
|
40
|
-
const parsedClass = parseType(node, name, {
|
|
41
|
-
typesMap,
|
|
42
|
-
sourceFile,
|
|
43
|
-
result
|
|
44
|
-
});
|
|
45
|
-
if (parsedClass.name !== key) throw new Error(`File name must correspond to exported class name ("${key}", "${parsedClass.name}")`);
|
|
46
|
-
result[key] = parsedClass;
|
|
47
|
-
}
|
|
48
|
-
for (const key in result) {
|
|
49
|
-
const isExtendingType = result[key].isExtending;
|
|
50
|
-
if (!isExtendingType) continue;
|
|
51
|
-
if (!result[isExtendingType]) throw new Error(`Unknown type: "${isExtendingType}"`);
|
|
52
|
-
result[key].fields = {
|
|
53
|
-
...result[isExtendingType].fields,
|
|
54
|
-
...result[key].fields
|
|
55
|
-
};
|
|
56
|
-
result[key].decorators = {
|
|
57
|
-
...result[isExtendingType].decorators,
|
|
58
|
-
...result[key].decorators
|
|
59
|
-
};
|
|
60
|
-
if (result[key].decorators.entity && !result[key].fields?.id) {
|
|
61
|
-
throw new Error(`Entity "${key}" must have an id`);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
fillComputedDefinitionsForType(result);
|
|
65
|
-
processThisReferenceDefaultValues(result, typesMap);
|
|
66
|
-
verifyRelationFields(result);
|
|
67
|
-
return result;
|
|
102
|
+
return typeNodesMap;
|
|
68
103
|
}
|
|
69
|
-
function
|
|
70
|
-
for (const key in
|
|
71
|
-
const type =
|
|
104
|
+
function verifyDefaultValueTypes(schema, typeNodesMap) {
|
|
105
|
+
for (const key in schema) {
|
|
106
|
+
const type = schema[key];
|
|
72
107
|
const fields = type.fields;
|
|
73
108
|
if (!fields) continue;
|
|
74
109
|
for (const fName in fields) {
|
|
75
110
|
const field = fields[fName];
|
|
76
|
-
if (field.
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
field.isArray = sourceField.isArray;
|
|
82
|
-
}
|
|
83
|
-
verifyDefaultValueType(field.isArray || false, {
|
|
84
|
-
type: sourceField.type,
|
|
85
|
-
value: sourceField.defaultValue
|
|
86
|
-
}, field.type, supportedPrimitiveTypes, {
|
|
87
|
-
typesMap,
|
|
88
|
-
result,
|
|
89
|
-
sourceFile: typesMap[key]?.sourceFile
|
|
111
|
+
if (field.defaultValue) {
|
|
112
|
+
verifyDefaultValueType(field, {
|
|
113
|
+
typeNodesMap,
|
|
114
|
+
result: schema,
|
|
115
|
+
sourceFile: typeNodesMap[key]?.sourceFile
|
|
90
116
|
});
|
|
91
117
|
}
|
|
118
|
+
for (const fName2 in fields) {
|
|
119
|
+
const field2 = fields[fName2];
|
|
120
|
+
if (field2.defaultValueCopyFrom) {
|
|
121
|
+
const sourceField = fields[field2.defaultValueCopyFrom];
|
|
122
|
+
if (!sourceField) throw new Error(`Cannot find field ${key}.${field2.defaultValueCopyFrom}"`);
|
|
123
|
+
verifyDefaultValueTypeCopyFrom(field2, sourceField, {
|
|
124
|
+
typeNodesMap,
|
|
125
|
+
result: schema,
|
|
126
|
+
sourceFile: typeNodesMap[key]?.sourceFile
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
92
130
|
}
|
|
93
131
|
}
|
|
94
132
|
}
|
|
@@ -113,7 +151,12 @@ function getOrder(f) {
|
|
|
113
151
|
};
|
|
114
152
|
return decorator.order ?? defaultOrders[decorator.preset] ?? 0;
|
|
115
153
|
}
|
|
116
|
-
function
|
|
154
|
+
function parseClassOrTypeDeclaration(typeDeclaration, typeName, ctx) {
|
|
155
|
+
if (typeDeclaration.kind === _typescript.SyntaxKind.ClassDeclaration) return parseClassDeclaration(typeDeclaration, typeName, ctx);
|
|
156
|
+
if (typeDeclaration.kind === _typescript.SyntaxKind.TypeAliasDeclaration) return parseTypeAliasDeclaration(typeDeclaration, typeName, ctx);
|
|
157
|
+
throw new Error(`Unexpected type definition - ${typeName}`);
|
|
158
|
+
}
|
|
159
|
+
function parseClassDeclaration(typeDeclaration, typeName, ctx) {
|
|
117
160
|
if (ctx.result[typeName]) return ctx.result[typeName];
|
|
118
161
|
const {
|
|
119
162
|
modifiers
|
|
@@ -123,62 +166,64 @@ function parseType(typeDeclaration, typeName, ctx) {
|
|
|
123
166
|
const name = nameNode.text;
|
|
124
167
|
const comment = typeDeclaration.jsDoc?.[0]?.comment;
|
|
125
168
|
const decorators = parseDecorators(modifiers, ctx);
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
throw new Error(`Unexpected class member - only properties are allowed("${m.getText(ctx.sourceFile)}")`);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
const fieldsArray = members.map(m => parseClassMember(m, name, ctx));
|
|
140
|
-
const fields = {};
|
|
141
|
-
for (const f of fieldsArray) {
|
|
142
|
-
fields[f.name] = f;
|
|
169
|
+
const classDeclaration = typeDeclaration;
|
|
170
|
+
const {
|
|
171
|
+
members,
|
|
172
|
+
heritageClauses
|
|
173
|
+
} = classDeclaration;
|
|
174
|
+
const isExtendingExpr = heritageClauses?.[0]?.types?.[0]?.expression;
|
|
175
|
+
const isExtending = isExtendingExpr?.text;
|
|
176
|
+
for (const m of members) {
|
|
177
|
+
if (m.kind !== _typescript.SyntaxKind.PropertyDeclaration) {
|
|
178
|
+
throw new Error(`Unexpected class member - only properties are allowed("${m.getText(ctx.sourceFile)}")`);
|
|
143
179
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
180
|
+
}
|
|
181
|
+
const fields = {};
|
|
182
|
+
for (const m of members) {
|
|
183
|
+
const field = parseClassMember(m, fields, name, ctx);
|
|
184
|
+
fields[field.name] = field;
|
|
185
|
+
}
|
|
186
|
+
const handle = _lodash.default.lowerFirst(name);
|
|
187
|
+
const handlePlural = (0, _pluralize.default)(handle);
|
|
188
|
+
const result = {
|
|
189
|
+
name,
|
|
190
|
+
handle,
|
|
191
|
+
handlePlural,
|
|
192
|
+
decorators,
|
|
193
|
+
fields,
|
|
194
|
+
isExtending,
|
|
195
|
+
comment
|
|
196
|
+
};
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
function parseTypeAliasDeclaration(typeDeclaration, typeName, ctx) {
|
|
200
|
+
if (ctx.result[typeName]) return ctx.result[typeName];
|
|
201
|
+
const nameNode = typeDeclaration.name;
|
|
202
|
+
if (!nameNode || nameNode.kind !== _typescript.SyntaxKind.Identifier) throw new Error("Cannot detect class name");
|
|
203
|
+
const name = nameNode.text;
|
|
204
|
+
const comment = typeDeclaration.jsDoc?.[0]?.comment;
|
|
205
|
+
const typeAliasDeclaration = typeDeclaration;
|
|
206
|
+
const typeAliasType = typeAliasDeclaration.type;
|
|
207
|
+
if (typeAliasType.kind === _typescript.SyntaxKind.UnionType) {
|
|
208
|
+
const typeAliasValue = typeAliasDeclaration.type;
|
|
209
|
+
const enumValues = getEnumValues(typeAliasValue, typeDeclaration, ctx);
|
|
210
|
+
return {
|
|
147
211
|
name,
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
decorators
|
|
151
|
-
fields,
|
|
152
|
-
isExtending,
|
|
153
|
-
comment
|
|
212
|
+
enumValues,
|
|
213
|
+
comment,
|
|
214
|
+
decorators: {}
|
|
154
215
|
};
|
|
155
|
-
return result;
|
|
156
216
|
}
|
|
157
|
-
if (
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
decorators,
|
|
166
|
-
enumValues,
|
|
167
|
-
comment
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
if (typeAliasType.kind === _typescript.SyntaxKind.TypeOperator && typeAliasType.operator === _typescript.SyntaxKind.KeyOfKeyword) {
|
|
171
|
-
const enumValues = getEnumValuesFromKeyOf(typeAliasType, ctx);
|
|
172
|
-
return {
|
|
173
|
-
name,
|
|
174
|
-
decorators,
|
|
175
|
-
enumValues,
|
|
176
|
-
comment
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
throw new Error(`Unexpected type definition - ${typeDeclaration.getText(ctx.sourceFile)}. Did you mean 'class'?`);
|
|
217
|
+
if (typeAliasType.kind === _typescript.SyntaxKind.TypeOperator && typeAliasType.operator === _typescript.SyntaxKind.KeyOfKeyword) {
|
|
218
|
+
const enumValues = getEnumValuesFromKeyOf(typeAliasType, ctx);
|
|
219
|
+
return {
|
|
220
|
+
name,
|
|
221
|
+
enumValues,
|
|
222
|
+
comment,
|
|
223
|
+
decorators: {}
|
|
224
|
+
};
|
|
180
225
|
}
|
|
181
|
-
throw new Error(`Unexpected type
|
|
226
|
+
throw new Error(`Unexpected type definition - ${typeDeclaration.getText(ctx.sourceFile)}. Did you mean 'class'?`);
|
|
182
227
|
}
|
|
183
228
|
function parseDecorators(modifiers, ctx) {
|
|
184
229
|
if (!modifiers) return {};
|
|
@@ -205,28 +250,29 @@ function getEnumValues(node, parentNode, ctx) {
|
|
|
205
250
|
});
|
|
206
251
|
return _lodash.default.keyBy(enumValuesArray, "name");
|
|
207
252
|
}
|
|
208
|
-
function parseClassMember(node, parentName, ctx) {
|
|
253
|
+
function parseClassMember(node, parentFields, parentName, ctx) {
|
|
209
254
|
const name = node.name.getText(ctx.sourceFile);
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
defaultValueCopyFrom = defaultValueDescription.value;
|
|
216
|
-
} else {
|
|
217
|
-
defaultValue = defaultValueDescription.value;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
255
|
+
const {
|
|
256
|
+
defaultValue,
|
|
257
|
+
defaultValueCopyFrom,
|
|
258
|
+
defaultValueClass
|
|
259
|
+
} = parseDefaultValueExpression(node.initializer, ctx);
|
|
220
260
|
const isRequired = !node.questionToken;
|
|
221
261
|
const comment = node.jsDoc?.[0]?.comment;
|
|
222
262
|
const decorators = parseDecorators(node.modifiers, ctx);
|
|
263
|
+
let defaultValueType = defaultValueClass || getPrimitiveTypeFromDefaultValue(defaultValue);
|
|
264
|
+
if (defaultValueCopyFrom) {
|
|
265
|
+
const parentField = parentFields[defaultValueCopyFrom];
|
|
266
|
+
if (!parentField) throw new Error(`Cannot find field ${parentName}.${defaultValueCopyFrom}"`);
|
|
267
|
+
defaultValueType = parentField.type;
|
|
268
|
+
}
|
|
223
269
|
const {
|
|
224
270
|
isArray,
|
|
225
271
|
isRelation,
|
|
226
272
|
isChange,
|
|
227
273
|
relationDenormFields,
|
|
228
274
|
type
|
|
229
|
-
} = parseFieldType(ctx, parentName, name, node,
|
|
275
|
+
} = parseFieldType(ctx, parentName, name, node, defaultValueType);
|
|
230
276
|
const result = {
|
|
231
277
|
type,
|
|
232
278
|
defaultValue,
|
|
@@ -240,9 +286,18 @@ function parseClassMember(node, parentName, ctx) {
|
|
|
240
286
|
comment
|
|
241
287
|
};
|
|
242
288
|
if (!_lodash.default.isEmpty(decorators)) result.decorators = decorators;
|
|
289
|
+
if (defaultValueClass) result.defaultValueClass = defaultValueClass;
|
|
243
290
|
return result;
|
|
244
291
|
}
|
|
245
|
-
function
|
|
292
|
+
function getPrimitiveTypeFromDefaultValue(value) {
|
|
293
|
+
if (_lodash.default.isString(value)) return "string";
|
|
294
|
+
if (_lodash.default.isNumber(value)) return "number";
|
|
295
|
+
if (_lodash.default.isBoolean(value)) return "boolean";
|
|
296
|
+
if (_lodash.default.isArray(value)) return "array";
|
|
297
|
+
if (_lodash.default.isObject(value)) return "object";
|
|
298
|
+
return void 0;
|
|
299
|
+
}
|
|
300
|
+
function parseFieldType(ctx, parentName, fieldName, node, defaultValueType) {
|
|
246
301
|
const parsedType = {
|
|
247
302
|
isArray: false,
|
|
248
303
|
isRelation: false,
|
|
@@ -257,20 +312,17 @@ function parseFieldType(ctx, parentName, fieldName, node, defaultValueDescriptio
|
|
|
257
312
|
parseFieldTypeInlineEnum(parsedType, parentName, fieldName, ctx);
|
|
258
313
|
parseFieldTypeKeyofEnum(parsedType, parentName, fieldName, ctx);
|
|
259
314
|
parseFieldTypeRecordEnum(parsedType, parentName, fieldName, ctx);
|
|
260
|
-
parsedType.type = parsedType.type ?? parsedType.nodeType?.getText(ctx.sourceFile) ??
|
|
315
|
+
parsedType.type = parsedType.type ?? parsedType.nodeType?.getText(ctx.sourceFile) ?? defaultValueType;
|
|
261
316
|
if (!parsedType.type) throw new Error(`Cannot detect property type: '${node.getText(ctx.sourceFile)}'`);
|
|
262
317
|
if (parsedType.type.startsWith("Change<")) {
|
|
263
318
|
parsedType.type = parsedType.type.slice(7, -1);
|
|
264
|
-
if (!ctx.
|
|
319
|
+
if (!ctx.typeNodesMap[parsedType.type]) throw new Error(`Unexpected property type: '${parsedType.type}'`);
|
|
265
320
|
parsedType.isChange = true;
|
|
266
321
|
} else {
|
|
267
|
-
if (!supportedPrimitiveTypes.includes(parsedType.type) && !ctx.
|
|
322
|
+
if (!supportedPrimitiveTypes.includes(parsedType.type) && !ctx.typeNodesMap[parsedType.type] && !ctx.result[parsedType.type]) {
|
|
268
323
|
throw new Error(`Unexpected property type: '${parsedType.type}'`);
|
|
269
324
|
}
|
|
270
325
|
}
|
|
271
|
-
if (defaultValueDescription && defaultValueDescription.type !== "thisReference") {
|
|
272
|
-
verifyDefaultValueType(parsedType.isArray, defaultValueDescription, parsedType.type, supportedPrimitiveTypes, ctx);
|
|
273
|
-
}
|
|
274
326
|
return {
|
|
275
327
|
isArray: parsedType.isArray || void 0,
|
|
276
328
|
isRelation: parsedType.isRelation || void 0,
|
|
@@ -286,9 +338,9 @@ function parseFieldTypeRecordEnum(parsedType, parentName, fieldName, ctx) {
|
|
|
286
338
|
if (nt.typeArguments?.length !== 2) return;
|
|
287
339
|
const keyTypeName = nt.typeArguments[0].getText(ctx.sourceFile);
|
|
288
340
|
const valueTypeName = nt.typeArguments[1].getText(ctx.sourceFile);
|
|
289
|
-
const keyType = ctx.
|
|
341
|
+
const keyType = ctx.typeNodesMap[keyTypeName];
|
|
290
342
|
if (!keyType) return;
|
|
291
|
-
if (!ctx.result[keyTypeName]) ctx.result[keyTypeName] =
|
|
343
|
+
if (!ctx.result[keyTypeName]) ctx.result[keyTypeName] = parseClassOrTypeDeclaration(keyType.node, keyTypeName, ctx);
|
|
292
344
|
const enumValues = ctx.result[keyTypeName].enumValues;
|
|
293
345
|
if (!enumValues) throw new Error(`Unexpected type - ${keyTypeName}`);
|
|
294
346
|
const newTypeName = `${parentName}_${_lodash.default.upperFirst(fieldName)}`;
|
|
@@ -362,11 +414,11 @@ function getEnumValuesFromKeyOf(nodeType, ctx) {
|
|
|
362
414
|
throw new Error(`Unexpected type - ${typeReferenceNode.getText(ctx.sourceFile)}`);
|
|
363
415
|
}
|
|
364
416
|
const typeName = typeReferenceNode.typeName.text;
|
|
365
|
-
const type = ctx.
|
|
417
|
+
const type = ctx.typeNodesMap[typeName];
|
|
366
418
|
if (!type) {
|
|
367
419
|
throw new Error(`Unexpected type - ${typeName}`);
|
|
368
420
|
}
|
|
369
|
-
if (!ctx.result[typeName]) ctx.result[typeName] =
|
|
421
|
+
if (!ctx.result[typeName]) ctx.result[typeName] = parseClassOrTypeDeclaration(type.node, typeName, ctx);
|
|
370
422
|
if (!ctx.result[typeName].fields) throw new Error(`Unexpected type - ${typeName}`);
|
|
371
423
|
return _lodash.default.mapValues(ctx.result[typeName].fields || {}, v => ({
|
|
372
424
|
name: v.name,
|
|
@@ -388,23 +440,43 @@ function getRelationDenormFields(ctx, node) {
|
|
|
388
440
|
}
|
|
389
441
|
throw new Error(`Unexpected type - ${node.getText(ctx.sourceFile)}`);
|
|
390
442
|
}
|
|
391
|
-
function verifyDefaultValueType(
|
|
443
|
+
function verifyDefaultValueType(field, ctx) {
|
|
444
|
+
const {
|
|
445
|
+
isArray,
|
|
446
|
+
defaultValue,
|
|
447
|
+
type
|
|
448
|
+
} = field;
|
|
392
449
|
if (isArray) {
|
|
393
|
-
if (
|
|
394
|
-
throw new
|
|
450
|
+
if (!_lodash.default.isArray(defaultValue)) {
|
|
451
|
+
throw new TypeError(`Default value type is different from field type: '${type}'`);
|
|
395
452
|
}
|
|
396
453
|
} else {
|
|
397
|
-
if (
|
|
454
|
+
if (supportedPrimitiveTypes.includes(type) && getPrimitiveTypeFromDefaultValue(defaultValue) !== type) {
|
|
398
455
|
throw new Error(`Default value type is different from field type: '${type}'`);
|
|
399
456
|
}
|
|
400
|
-
if (!ctx.result[type] && ctx.
|
|
457
|
+
if (!ctx.result[type] && ctx.typeNodesMap[type]) ctx.result[type] = parseClassOrTypeDeclaration(ctx.typeNodesMap[type].node, type, ctx);
|
|
401
458
|
const enumValues = ctx.result[type]?.enumValues;
|
|
402
|
-
if (enumValues && !enumValues[
|
|
459
|
+
if (enumValues && !enumValues[defaultValue]) {
|
|
403
460
|
const enumValuesStr = _lodash.default.keys(enumValues).map(x => `'x'`).join(", ");
|
|
404
461
|
throw new Error(`Default value must be one of: ${enumValuesStr}`);
|
|
405
462
|
}
|
|
406
463
|
}
|
|
407
464
|
}
|
|
465
|
+
function verifyDefaultValueTypeCopyFrom(field, sourceField, ctx) {
|
|
466
|
+
const {
|
|
467
|
+
isArray,
|
|
468
|
+
type
|
|
469
|
+
} = field;
|
|
470
|
+
if (isArray) {
|
|
471
|
+
if (!sourceField.isArray) {
|
|
472
|
+
throw new TypeError(`Default value type is not an array: '${field.name}'`);
|
|
473
|
+
}
|
|
474
|
+
} else {
|
|
475
|
+
if (sourceField.type !== type) {
|
|
476
|
+
throw new Error(`Default value type is different from field type: '${field.name}'`);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
408
480
|
function parseDecorator(decoratorNode, ctx) {
|
|
409
481
|
const expr = decoratorNode.expression;
|
|
410
482
|
if (expr.kind !== _typescript.SyntaxKind.CallExpression) throw new Error(`Unexpected decorator format: "${expr.getText(ctx.sourceFile)}"`);
|
|
@@ -420,40 +492,35 @@ function parseDecoratorArguments(expr, ctx) {
|
|
|
420
492
|
if (args.length === 0) return {};
|
|
421
493
|
if (args.length > 1) throw new Error(`Too many arguments - one expected: "${expr.getText(ctx.sourceFile)}"`);
|
|
422
494
|
const arg = args[0];
|
|
423
|
-
return parseLiteralNode(arg, ctx)
|
|
495
|
+
return parseLiteralNode(arg, ctx) ?? {};
|
|
424
496
|
}
|
|
425
|
-
function
|
|
426
|
-
if (!expr) return
|
|
497
|
+
function parseDefaultValueExpression(expr, ctx) {
|
|
498
|
+
if (!expr) return {};
|
|
427
499
|
if (expr.kind === _typescript.SyntaxKind.NewExpression) {
|
|
428
500
|
const identifier = expr.expression;
|
|
429
501
|
const type = identifier?.text;
|
|
430
502
|
if (type) {
|
|
431
503
|
return {
|
|
432
|
-
type,
|
|
433
|
-
|
|
504
|
+
defaultValueClass: type,
|
|
505
|
+
defaultValue: {}
|
|
434
506
|
};
|
|
435
507
|
}
|
|
508
|
+
} else if (expr.kind === _typescript.SyntaxKind.PropertyAccessExpression && expr.expression.kind === _typescript.SyntaxKind.ThisKeyword) {
|
|
509
|
+
return {
|
|
510
|
+
defaultValueCopyFrom: expr.name?.text
|
|
511
|
+
};
|
|
436
512
|
}
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
value: expr.text
|
|
440
|
-
};
|
|
441
|
-
if (expr.kind === _typescript.SyntaxKind.FalseKeyword) return {
|
|
442
|
-
type: "boolean",
|
|
443
|
-
value: false
|
|
444
|
-
};
|
|
445
|
-
if (expr.kind === _typescript.SyntaxKind.TrueKeyword) return {
|
|
446
|
-
type: "boolean",
|
|
447
|
-
value: true
|
|
448
|
-
};
|
|
449
|
-
if (expr.kind === _typescript.SyntaxKind.NumericLiteral) return {
|
|
450
|
-
type: "number",
|
|
451
|
-
value: Number.parseFloat(expr.text)
|
|
452
|
-
};
|
|
453
|
-
if (expr.kind === _typescript.SyntaxKind.ObjectLiteralExpression) return {
|
|
454
|
-
type: "object",
|
|
455
|
-
value: parseObjectLiteral(expr, ctx)
|
|
513
|
+
return {
|
|
514
|
+
defaultValue: parseLiteralNode(expr, ctx)
|
|
456
515
|
};
|
|
516
|
+
}
|
|
517
|
+
function parseLiteralNode(expr, ctx) {
|
|
518
|
+
if (!expr) return void 0;
|
|
519
|
+
if (expr.kind === _typescript.SyntaxKind.StringLiteral) return expr.text;
|
|
520
|
+
if (expr.kind === _typescript.SyntaxKind.FalseKeyword) return false;
|
|
521
|
+
if (expr.kind === _typescript.SyntaxKind.TrueKeyword) return true;
|
|
522
|
+
if (expr.kind === _typescript.SyntaxKind.NumericLiteral) return Number.parseFloat(expr.text);
|
|
523
|
+
if (expr.kind === _typescript.SyntaxKind.ObjectLiteralExpression) return parseObjectLiteral(expr, ctx);
|
|
457
524
|
if (expr.kind === _typescript.SyntaxKind.ArrayLiteralExpression) {
|
|
458
525
|
const defaultValueStr = expr.getText(ctx.sourceFile);
|
|
459
526
|
let defaultValue;
|
|
@@ -463,16 +530,7 @@ function parseLiteralNode(expr, ctx) {
|
|
|
463
530
|
throw new Error("Value must be valid array");
|
|
464
531
|
}
|
|
465
532
|
if (!_lodash.default.isArray(defaultValue)) throw new Error("Value must be valid array");
|
|
466
|
-
return
|
|
467
|
-
type: "array",
|
|
468
|
-
value: defaultValue
|
|
469
|
-
};
|
|
470
|
-
}
|
|
471
|
-
if (expr.kind === _typescript.SyntaxKind.PropertyAccessExpression && expr.expression.kind === _typescript.SyntaxKind.ThisKeyword) {
|
|
472
|
-
return {
|
|
473
|
-
type: "thisReference",
|
|
474
|
-
value: expr.name?.text
|
|
475
|
-
};
|
|
533
|
+
return defaultValue;
|
|
476
534
|
}
|
|
477
535
|
throw new Error(`Unexpected property expression: "${expr.getText(ctx.sourceFile)}"`);
|
|
478
536
|
}
|
|
@@ -485,7 +543,7 @@ function parseObjectLiteral(arg, ctx) {
|
|
|
485
543
|
if (p.kind !== _typescript.SyntaxKind.PropertyAssignment) throw new Error(`Unexpected property value: "${p.getText(ctx.sourceFile)}"`);
|
|
486
544
|
const p2 = p;
|
|
487
545
|
const valueExpression = p2.initializer;
|
|
488
|
-
const value = parseLiteralNode(valueExpression, ctx)
|
|
546
|
+
const value = parseLiteralNode(valueExpression, ctx);
|
|
489
547
|
result[name] = value;
|
|
490
548
|
}
|
|
491
549
|
return result;
|
package/integrations/lib.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function parseSchema(typescriptFiles: Record<string, string>): Record<string,
|
|
1
|
+
export declare function parseSchema(typescriptFiles: Record<string, string>): Record<string, TypeDefinition>;
|