MCPcopy Create free account
hub / github.com/instantdb/instant / parseSchemaFromJSON

Function parseSchemaFromJSON

client/packages/core/src/parseSchemaFromJSON.ts:4–176  ·  view source on GitHub ↗
(
  s: any,
)

Source from the content-addressed store, hash-verified

2import { DataAttrDef, InstantSchemaDef } from './schemaTypes.ts';
3
4export 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

Callers 1

Calls 3

optionalMethod · 0.80
indexedMethod · 0.80
uniqueMethod · 0.80

Tested by

no test coverage detected