@travetto/model-sql 8.0.0-alpha.8 → 8.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/src/schema.ts ADDED
@@ -0,0 +1,61 @@
1
+ import { ModelRegistryIndex } from '@travetto/model';
2
+ import type { Class } from '@travetto/runtime';
3
+ import { castTo, RuntimeError, TypedObject } from '@travetto/runtime';
4
+ import { SchemaRegistryIndex } from '@travetto/schema';
5
+
6
+ import type { SchemaContext } from './types';
7
+
8
+ export class SQLModelSchemaUtil {
9
+ static SCHEMA_CACHE = new Map<Class, SchemaContext<unknown>>();
10
+
11
+ // Schema and Context management
12
+ static getSchemaContext<T>(modelClass: Class<T>): SchemaContext<T> {
13
+ if (this.SCHEMA_CACHE.has(modelClass)) {
14
+ return castTo(this.SCHEMA_CACHE.get(modelClass)!);
15
+ }
16
+
17
+ const registryConfig = SchemaRegistryIndex.getOptional(modelClass)?.get();
18
+ if (!registryConfig) {
19
+ throw new RuntimeError('Cannot store unregistered models', { category: 'data' });
20
+ }
21
+
22
+ const fields = Object.values(registryConfig.fields);
23
+
24
+ const hasModel = ModelRegistryIndex.has(modelClass);
25
+ if (hasModel && registryConfig.discriminatedBase) {
26
+ const fieldMap = new Set(fields.map(field => field.name));
27
+ for (const subclass of SchemaRegistryIndex.getDiscriminatedClasses(modelClass)) {
28
+ const subclassConfig = SchemaRegistryIndex.getConfig(subclass);
29
+ for (const field of TypedObject.values(subclassConfig.fields)) {
30
+ if (!fieldMap.has(field.name)) {
31
+ fieldMap.add(field.name);
32
+ fields.push(field);
33
+ }
34
+ }
35
+ }
36
+ }
37
+
38
+ const simpleFieldsList = fields.filter(field => !SchemaRegistryIndex.has(field.type) && !field.array);
39
+ const complexFieldsList = fields.filter(field => SchemaRegistryIndex.has(field.type) || field.array);
40
+ const simpleFields = new Map(simpleFieldsList.map(field => [field.name, field]));
41
+ const complexFields = new Map(complexFieldsList.map(field => [field.name, field]));
42
+ const context: SchemaContext<T> = { cls: modelClass, simpleFields, complexFields, allFields: fields };
43
+ this.SCHEMA_CACHE.set(modelClass, context);
44
+ return context;
45
+ }
46
+
47
+ static isColumnNotNull<T>(context: SchemaContext<T>, fieldName: string): boolean {
48
+ const schemaConfig = SchemaRegistryIndex.getOptional(context.cls)?.get();
49
+ const fieldConfig = schemaConfig?.fields[fieldName];
50
+ if (!fieldConfig || fieldConfig.required?.active === false || fieldConfig.accessor) {
51
+ return false;
52
+ }
53
+ if (ModelRegistryIndex.has(context.cls)) {
54
+ const modelConfig = ModelRegistryIndex.getConfig(context.cls);
55
+ if (modelConfig.transientFields?.includes(fieldName)) {
56
+ return false;
57
+ }
58
+ }
59
+ return true;
60
+ }
61
+ }