|
| 1 | +import type { MongoDBJSONSchema } from 'mongodb-schema'; |
| 2 | +import type { FieldPath } from '../services/data-model-storage'; |
| 3 | + |
| 4 | +// TODO: add support for update |
| 5 | +// renaming fields, deleting fields, adding fields... |
| 6 | +// or maybe we can have two traverses? one for parsing, one for updating? |
| 7 | +/** |
| 8 | + * Traverses a MongoDB JSON schema and calls the visitor for each field. |
| 9 | + * |
| 10 | + * @param {Object} params - The parameters object. |
| 11 | + * @param {MongoDBJSONSchema} params.jsonSchema - The JSON schema to traverse. |
| 12 | + * @param {function} params.visitor - Function called for each field. |
| 13 | + * @returns {void} |
| 14 | + */ |
| 15 | +export const traverseSchema = ({ |
| 16 | + jsonSchema, |
| 17 | + parentFieldPath = [], |
| 18 | + visitor, |
| 19 | +}: { |
| 20 | + jsonSchema: MongoDBJSONSchema; |
| 21 | + visitor: ({ |
| 22 | + fieldPath, |
| 23 | + fieldTypes, |
| 24 | + }: { |
| 25 | + fieldPath: FieldPath; |
| 26 | + fieldTypes: string[]; |
| 27 | + }) => void; |
| 28 | + parentFieldPath?: FieldPath; |
| 29 | +}): void => { |
| 30 | + if (!jsonSchema || !jsonSchema.properties) { |
| 31 | + return; |
| 32 | + } |
| 33 | + for (const [name, field] of Object.entries(jsonSchema.properties)) { |
| 34 | + // field has types, properties and (optional) children |
| 35 | + // types are either direct, or from anyof |
| 36 | + // children are either direct (properties), from anyOf, items or items.anyOf |
| 37 | + const types: (string | string[])[] = []; |
| 38 | + const children: (MongoDBJSONSchema | MongoDBJSONSchema[])[] = []; |
| 39 | + if (field.bsonType) { |
| 40 | + types.push(field.bsonType); |
| 41 | + } |
| 42 | + if (field.properties) { |
| 43 | + children.push(field); |
| 44 | + } |
| 45 | + if (field.items) { |
| 46 | + children.push((field.items as MongoDBJSONSchema).anyOf || field.items); |
| 47 | + } |
| 48 | + if (field.anyOf) { |
| 49 | + for (const variant of field.anyOf) { |
| 50 | + if (variant.bsonType) { |
| 51 | + types.push(variant.bsonType); |
| 52 | + } |
| 53 | + if (variant.properties) { |
| 54 | + children.push(variant); |
| 55 | + } |
| 56 | + if (variant.items) { |
| 57 | + children.push(variant.items); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + const newFieldPath = [...parentFieldPath, name]; |
| 63 | + |
| 64 | + visitor({ |
| 65 | + fieldPath: newFieldPath, |
| 66 | + fieldTypes: types.flat(), |
| 67 | + }); |
| 68 | + |
| 69 | + children.flat().forEach((child) => |
| 70 | + traverseSchema({ |
| 71 | + jsonSchema: child, |
| 72 | + visitor, |
| 73 | + parentFieldPath: newFieldPath, |
| 74 | + }) |
| 75 | + ); |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +function searchItemsForChild( |
| 80 | + items: MongoDBJSONSchema['items'], |
| 81 | + child: string |
| 82 | +): MongoDBJSONSchema | undefined { |
| 83 | + // When items is an array, this indicates multiple non-complex types |
| 84 | + if (!items || Array.isArray(items)) return undefined; |
| 85 | + // Nested array - we go deeper |
| 86 | + if (items.items) { |
| 87 | + const result = searchItemsForChild(items.items, child); |
| 88 | + if (result) return result; |
| 89 | + } |
| 90 | + // Array of single type and that type is an object |
| 91 | + if (items.properties && items.properties[child]) { |
| 92 | + return items.properties[child]; |
| 93 | + } |
| 94 | + // Array of multiple types, possibly including objects |
| 95 | + if (items.anyOf) { |
| 96 | + for (const item of items.anyOf) { |
| 97 | + if (item.properties && item.properties[child]) { |
| 98 | + return item.properties[child]; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + return undefined; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Finds a single field in a MongoDB JSON schema. |
| 107 | + * |
| 108 | + * @param {Object} params - The parameters object. |
| 109 | + * @param {MongoDBJSONSchema} params.jsonSchema - The JSON schema to traverse. |
| 110 | + * @param {FieldPath} params.fieldPath - The field path to find. |
| 111 | + * @returns {Object} result - The field information. |
| 112 | + * @returns {FieldPath[]} result.fieldPath - The full path to the found field. |
| 113 | + * @returns {string[]} result.fieldTypes - Flat list of BSON types for the found field. |
| 114 | + * @returns {MongoDBJSONSchema} result.jsonSchema - The JSON schema node for the found field. |
| 115 | + |
| 116 | + */ |
| 117 | +export const getFieldFromSchema = ({ |
| 118 | + jsonSchema, |
| 119 | + fieldPath, |
| 120 | + parentFieldPath = [], |
| 121 | +}: { |
| 122 | + jsonSchema: MongoDBJSONSchema; |
| 123 | + fieldPath: FieldPath; |
| 124 | + parentFieldPath?: FieldPath; |
| 125 | +}): |
| 126 | + | { |
| 127 | + fieldTypes: string[]; |
| 128 | + jsonSchema: MongoDBJSONSchema; |
| 129 | + } |
| 130 | + | undefined => { |
| 131 | + const nextInPath = fieldPath[0]; |
| 132 | + const remainingFieldPath = fieldPath.slice(1); |
| 133 | + let nextStep: MongoDBJSONSchema | undefined; |
| 134 | + if (jsonSchema.properties && jsonSchema.properties[nextInPath]) { |
| 135 | + nextStep = jsonSchema.properties[nextInPath]; |
| 136 | + } |
| 137 | + if (!nextStep && jsonSchema.items) { |
| 138 | + nextStep = searchItemsForChild(jsonSchema.items, nextInPath); |
| 139 | + } |
| 140 | + if (!nextStep && jsonSchema.anyOf) { |
| 141 | + for (const variant of jsonSchema.anyOf) { |
| 142 | + if (variant.properties && variant.properties[nextInPath]) { |
| 143 | + nextStep = variant.properties[nextInPath]; |
| 144 | + break; |
| 145 | + } |
| 146 | + if (variant.items) { |
| 147 | + nextStep = searchItemsForChild(variant.items, nextInPath); |
| 148 | + if (nextStep) break; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + if (!nextStep) { |
| 153 | + throw new Error('Field not found in schema'); |
| 154 | + } |
| 155 | + |
| 156 | + // Reached the end of path, return the field information |
| 157 | + if (fieldPath.length === 1) { |
| 158 | + const types: string[] = []; |
| 159 | + if (nextStep.bsonType) { |
| 160 | + if (Array.isArray(nextStep.bsonType)) { |
| 161 | + types.push(...nextStep.bsonType); |
| 162 | + } else { |
| 163 | + types.push(nextStep.bsonType); |
| 164 | + } |
| 165 | + } |
| 166 | + if (nextStep.anyOf) { |
| 167 | + types.push( |
| 168 | + ...nextStep.anyOf.flatMap((variant) => variant.bsonType || []) |
| 169 | + ); |
| 170 | + } |
| 171 | + return { |
| 172 | + fieldTypes: types, |
| 173 | + jsonSchema: nextStep, |
| 174 | + }; |
| 175 | + } |
| 176 | + console.log('Search lower', { |
| 177 | + jsonSchema: nextStep, |
| 178 | + fieldPath: remainingFieldPath, |
| 179 | + parentFieldPath: [...parentFieldPath, nextInPath], |
| 180 | + }); |
| 181 | + // Continue searching in the next step |
| 182 | + return getFieldFromSchema({ |
| 183 | + jsonSchema: nextStep, |
| 184 | + fieldPath: remainingFieldPath, |
| 185 | + parentFieldPath: [...parentFieldPath, nextInPath], |
| 186 | + }); |
| 187 | +}; |
0 commit comments