|
1 | | -import type Owner from '@ember/owner'; |
2 | 1 | import DataSourceStore from '../../data-sources/store.ts'; |
3 | 2 | import type MemorySourceFactory from '../factories/memory-source-factory.ts'; |
4 | 3 | import type ModelFactory from '../model-factory.ts'; |
| 4 | +import DataKeyMap from '../services/data-key-map.ts'; |
| 5 | +import DataNormalizer from '../services/data-normalizer.ts'; |
| 6 | +import DataSchema from '../services/data-schema.ts'; |
| 7 | +import DataValidator from '../services/data-validator.ts'; |
5 | 8 | import { getName } from '../utils/get-name.ts'; |
6 | 9 | import { orbitRegistry } from '../utils/orbit-registry.ts'; |
7 | 10 | import type { Strategy } from '@orbit/coordinator'; |
8 | 11 | import type { Bucket } from '@orbit/core'; |
9 | 12 | import { type MemorySourceSettings } from '@orbit/memory'; |
10 | 13 |
|
11 | | -type Folder = |
12 | | - | '/data-buckets/' |
13 | | - | '/data-models/' |
14 | | - | '/data-sources/' |
15 | | - | '/data-strategies/'; |
16 | | - |
17 | 14 | interface FactoryForFolderType { |
18 | 15 | '/data-buckets/': { default: { create(): Bucket } }; |
19 | 16 | '/data-models/': { default: ModelFactory }; |
20 | 17 | '/data-sources/': { default: typeof MemorySourceFactory }; |
21 | 18 | '/data-strategies/': { default: { create(): Strategy } }; |
22 | 19 | } |
23 | 20 |
|
24 | | -function injectModules(modules: Record<string, unknown>) { |
25 | | - const folderConfig = { |
26 | | - '/data-buckets/': { registry: orbitRegistry.registrations.buckets }, |
27 | | - '/data-models/': { registry: orbitRegistry.registrations.models }, |
28 | | - '/data-sources/': { registry: orbitRegistry.registrations.sources }, |
29 | | - '/data-strategies/': { registry: orbitRegistry.registrations.strategies }, |
30 | | - }; |
31 | | - |
32 | | - for (const [folder, { registry }] of Object.entries(folderConfig) as [ |
33 | | - Folder, |
34 | | - { registry: any }, |
35 | | - ][]) { |
36 | | - const matches = Object.entries(modules).filter(([key]) => |
37 | | - key.includes(folder), |
| 21 | +function injectDataBuckets(modules: Record<string, unknown>) { |
| 22 | + const registry = orbitRegistry.registrations.buckets; |
| 23 | + const matches = Object.entries(modules); |
| 24 | + |
| 25 | + if (matches.length > 1) { |
| 26 | + throw new Error( |
| 27 | + `Expected only one file under /data-buckets/, found ${matches.length}: ` + |
| 28 | + matches.map(([key]) => key).join(', '), |
38 | 29 | ); |
| 30 | + } |
39 | 31 |
|
40 | | - if (folder === '/data-buckets/' && matches.length > 1) { |
41 | | - throw new Error( |
42 | | - `Expected only one file under /data-buckets/, found ${matches.length}: ` + |
43 | | - matches.map(([key]) => key).join(', '), |
44 | | - ); |
45 | | - } |
| 32 | + for (const [, module] of matches) { |
| 33 | + registry['main'] = ( |
| 34 | + module as FactoryForFolderType['/data-buckets/'] |
| 35 | + ).default?.create?.(); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function injectDataModels(modules: Record<string, unknown>) { |
| 40 | + const folder = '/data-models/'; |
| 41 | + const registry = orbitRegistry.registrations.models; |
| 42 | + |
| 43 | + for (const [key, module] of Object.entries(modules)) { |
| 44 | + let [, name] = key.split(folder); |
| 45 | + name = getName(name as string); |
| 46 | + |
| 47 | + registry[name] = |
| 48 | + (module as FactoryForFolderType['/data-models/']).default ?? |
| 49 | + (module as ModelFactory); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function injectDataSources(modules: Record<string, unknown>) { |
| 54 | + const folder = '/data-sources/'; |
| 55 | + const registry = orbitRegistry.registrations.sources; |
| 56 | + |
| 57 | + for (const [key, module] of Object.entries(modules)) { |
| 58 | + let [, name] = key.split(folder); |
| 59 | + name = getName(name as string); |
| 60 | + |
| 61 | + registry[name] = (module as FactoryForFolderType['/data-sources/']).default // eslint-disable-next-line @typescript-eslint/no-unsafe-argument |
| 62 | + ?.create?.({} as any); |
| 63 | + } |
| 64 | +} |
46 | 65 |
|
47 | | - for (const [key, module] of matches) { |
48 | | - if (folder === '/data-buckets/') { |
49 | | - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
50 | | - registry['main'] = ( |
51 | | - module as FactoryForFolderType['/data-buckets/'] |
52 | | - ).default?.create?.(); |
53 | | - continue; |
54 | | - } |
55 | | - |
56 | | - let [, name] = key.split(folder); |
57 | | - name = getName(name as string); |
58 | | - |
59 | | - if (folder === '/data-models/') { |
60 | | - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
61 | | - registry[name] = |
62 | | - (module as FactoryForFolderType[typeof folder]).default ?? |
63 | | - (module as ModelFactory); |
64 | | - } else { |
65 | | - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
66 | | - registry[name] = (module as FactoryForFolderType[typeof folder]).default // eslint-disable-next-line @typescript-eslint/no-unsafe-argument |
67 | | - ?.create?.({} as any); |
68 | | - } |
| 66 | +function injectDataStrategies(modules: Record<string, unknown>) { |
| 67 | + const folder = '/data-strategies/'; |
| 68 | + const registry = orbitRegistry.registrations.strategies; |
| 69 | + |
| 70 | + for (const [key, module] of Object.entries(modules)) { |
| 71 | + let [, name] = key.split(folder); |
| 72 | + name = getName(name as string); |
| 73 | + |
| 74 | + registry[name] = ( |
| 75 | + module as FactoryForFolderType['/data-strategies/'] |
| 76 | + ).default?.create?.(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function injectModules(modules: Record<string, unknown>) { |
| 81 | + const bucketModules: Record<string, unknown> = {}; |
| 82 | + const modelModules: Record<string, unknown> = {}; |
| 83 | + const sourceModules: Record<string, unknown> = {}; |
| 84 | + const strategyModules: Record<string, unknown> = {}; |
| 85 | + |
| 86 | + for (const [key, module] of Object.entries(modules)) { |
| 87 | + if (key.includes('/data-buckets/')) { |
| 88 | + bucketModules[key] = module; |
| 89 | + } else if (key.includes('/data-models/')) { |
| 90 | + modelModules[key] = module; |
| 91 | + } else if (key.includes('/data-sources/')) { |
| 92 | + sourceModules[key] = module; |
| 93 | + } else if (key.includes('/data-strategies/')) { |
| 94 | + strategyModules[key] = module; |
69 | 95 | } |
70 | 96 | } |
71 | 97 |
|
72 | | - // Register the store source after processing all modules |
73 | | - orbitRegistry.registrations.sources['store'] = DataSourceStore.create( |
74 | | - {} as MemorySourceSettings, |
75 | | - ); |
| 98 | + injectDataBuckets(bucketModules); |
| 99 | + injectDataModels(modelModules); |
| 100 | + injectServices(); |
| 101 | + injectDataSources(sourceModules); |
| 102 | + injectDataStrategies(strategyModules); |
| 103 | +} |
| 104 | + |
| 105 | +function injectServices() { |
| 106 | + const keyMap = DataKeyMap.create(); |
| 107 | + const schema = DataSchema.create(); |
| 108 | + orbitRegistry.services.keyMap = keyMap; |
| 109 | + orbitRegistry.services.schema = schema; |
| 110 | + orbitRegistry.services.normalizer = DataNormalizer.create({ |
| 111 | + keyMap, |
| 112 | + schema, |
| 113 | + }); |
| 114 | + orbitRegistry.services.validatorFor = DataValidator.create({ |
| 115 | + validators: {}, |
| 116 | + }); |
76 | 117 | } |
77 | 118 |
|
78 | 119 | export function setupOrbit( |
79 | | - application: Owner, |
80 | 120 | modules: Record<string, unknown>, |
81 | 121 | config?: { schemaVersion?: number }, |
82 | 122 | ) { |
83 | | - orbitRegistry.application = application; |
84 | 123 | orbitRegistry.schemaVersion = config?.schemaVersion; |
85 | 124 | injectModules(modules); |
| 125 | + // Register the store source after processing all modules |
| 126 | + orbitRegistry.registrations.sources['store'] = DataSourceStore.create( |
| 127 | + {} as MemorySourceSettings, |
| 128 | + ); |
86 | 129 | } |
0 commit comments