( s: any, )
| 2 | import { DataAttrDef, InstantSchemaDef } from './schemaTypes.ts'; |
| 3 | |
| 4 | export const parseSchemaFromJSON = ( |
| 5 | s: any, |
| 6 | ): InstantSchemaDef<any, any, any> => { |
| 7 | // Parse entities |
| 8 | const entities: Record<string, any> = {}; |
| 9 | |
| 10 | for (const [entityName, entityInfo] of Object.entries(s.entities)) { |
| 11 | const entityDef = entityInfo as any; |
| 12 | const attrs: Record<string, any> = {}; |
| 13 | |
| 14 | // Parse attributes |
| 15 | for (const [attrName, attrInfo] of Object.entries(entityDef.attrs)) { |
| 16 | const attrDef = attrInfo as any; |
| 17 | let attr: DataAttrDef<any, any, any>; |
| 18 | |
| 19 | // Create the appropriate attribute type |
| 20 | switch (attrDef.valueType) { |
| 21 | case 'string': |
| 22 | attr = i.string(); |
| 23 | break; |
| 24 | case 'number': |
| 25 | attr = i.number(); |
| 26 | break; |
| 27 | case 'boolean': |
| 28 | attr = i.boolean(); |
| 29 | break; |
| 30 | case 'date': |
| 31 | attr = i.date(); |
| 32 | break; |
| 33 | case 'json': |
| 34 | attr = i.json(); |
| 35 | break; |
| 36 | default: |
| 37 | attr = i.json(); |
| 38 | } |
| 39 | |
| 40 | // Apply modifiers |
| 41 | if (!attrDef.required) { |
| 42 | attr = attr.optional(); |
| 43 | } |
| 44 | |
| 45 | if (attrDef.config?.indexed) { |
| 46 | attr = attr.indexed(); |
| 47 | } |
| 48 | |
| 49 | if (attrDef.config?.unique) { |
| 50 | attr = attr.unique(); |
| 51 | } |
| 52 | |
| 53 | attrs[attrName] = attr; |
| 54 | } |
| 55 | |
| 56 | entities[entityName] = i.entity(attrs); |
| 57 | } |
| 58 | |
| 59 | // Parse links |
| 60 | const links: Record<string, any> = s.links || {}; |
| 61 |
no test coverage detected