|
| 1 | +import { IObjectStackProtocol } from '@objectstack/spec/api'; |
| 2 | +import { IDataEngine } from '@objectstack/spec/system'; |
| 3 | + |
| 4 | +// We import SchemaRegistry directly since this class lives in the same package |
| 5 | +import { SchemaRegistry } from './registry'; |
| 6 | + |
| 7 | +export class ObjectStackProtocolImplementation implements IObjectStackProtocol { |
| 8 | + private engine: IDataEngine; |
| 9 | + |
| 10 | + constructor(engine: IDataEngine) { |
| 11 | + this.engine = engine; |
| 12 | + } |
| 13 | + |
| 14 | + getDiscovery() { |
| 15 | + return { |
| 16 | + name: 'ObjectStack API', |
| 17 | + version: '1.0', |
| 18 | + capabilities: { |
| 19 | + metadata: true, |
| 20 | + data: true, |
| 21 | + ui: true |
| 22 | + } |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + getMetaTypes() { |
| 27 | + return SchemaRegistry.getRegisteredTypes(); |
| 28 | + } |
| 29 | + |
| 30 | + getMetaItems(type: string) { |
| 31 | + return SchemaRegistry.listItems(type); |
| 32 | + } |
| 33 | + |
| 34 | + getMetaItem(type: string, name: string) { |
| 35 | + return SchemaRegistry.getItem(type, name); |
| 36 | + } |
| 37 | + |
| 38 | + getUiView(object: string, type: 'list' | 'form') { |
| 39 | + const schema = SchemaRegistry.getObject(object); |
| 40 | + if (!schema) throw new Error(`Object ${object} not found`); |
| 41 | + |
| 42 | + if (type === 'list') { |
| 43 | + return { |
| 44 | + type: 'list', |
| 45 | + object: object, |
| 46 | + columns: Object.keys(schema.fields || {}).slice(0, 5).map(f => ({ |
| 47 | + field: f, |
| 48 | + label: schema.fields[f].label || f |
| 49 | + })) |
| 50 | + }; |
| 51 | + } else { |
| 52 | + return { |
| 53 | + type: 'form', |
| 54 | + object: object, |
| 55 | + sections: [ |
| 56 | + { |
| 57 | + label: 'General', |
| 58 | + fields: Object.keys(schema.fields || {}).map(f => ({ |
| 59 | + field: f |
| 60 | + })) |
| 61 | + } |
| 62 | + ] |
| 63 | + }; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + findData(object: string, query: any) { |
| 68 | + return this.engine.find(object, query); |
| 69 | + } |
| 70 | + |
| 71 | + async getData(object: string, id: string) { |
| 72 | + // IDataEngine doesn't have findOne, so we simulate it with find and limit 1 |
| 73 | + // Assuming the ID field is named '_id' or 'id'. |
| 74 | + // For broad compatibility, we might need to know the ID field name. |
| 75 | + // But traditionally it is _id in ObjectStack/mongo or id in others. |
| 76 | + // Let's rely on finding by ID if the engine supports it via find? |
| 77 | + // Actually, ObjectQL (the implementation) DOES have findOne. |
| 78 | + // But we are programming against IDataEngine interface here. |
| 79 | + |
| 80 | + // If the engine IS ObjectQL (which it practically is), we could cast it. |
| 81 | + // But let's try to stick to interface. |
| 82 | + |
| 83 | + const results = await this.engine.find(object, { |
| 84 | + filter: { _id: id }, // Default Assumption: _id |
| 85 | + limit: 1 |
| 86 | + }); |
| 87 | + if (results && results.length > 0) { |
| 88 | + return results[0]; |
| 89 | + } |
| 90 | + throw new Error(`Record ${id} not found in ${object}`); |
| 91 | + } |
| 92 | + |
| 93 | + createData(object: string, data: any) { |
| 94 | + return this.engine.insert(object, data); |
| 95 | + } |
| 96 | + |
| 97 | + updateData(object: string, id: string, data: any) { |
| 98 | + return this.engine.update(object, id, data); |
| 99 | + } |
| 100 | + |
| 101 | + deleteData(object: string, id: string) { |
| 102 | + return this.engine.delete(object, id); |
| 103 | + } |
| 104 | +} |
0 commit comments