|
| 1 | +// Temporary Memory Protocol Implementation |
| 2 | +// Since we cannot find the official one in the packages, we implement a minimal one for the mock |
| 3 | + |
| 4 | +import type { IObjectStackProtocol } from '@objectstack/spec/api'; |
| 5 | + |
| 6 | +export class MemoryProtocol implements IObjectStackProtocol { |
| 7 | + private data: Record<string, any[]> = {}; |
| 8 | + private objects: any[] = []; |
| 9 | + |
| 10 | + constructor(data: Record<string, any[]> = {}, objects: any[] = []) { |
| 11 | + this.data = data; |
| 12 | + this.objects = objects; |
| 13 | + } |
| 14 | + |
| 15 | + async findData(object: string, params?: any): Promise<any[]> { |
| 16 | + const items = this.data[object] || []; |
| 17 | + // Minimal mock filtering could go here |
| 18 | + return items; |
| 19 | + } |
| 20 | + |
| 21 | + async getData(object: string, id: string): Promise<any> { |
| 22 | + const items = this.data[object] || []; |
| 23 | + const item = items.find((i: any) => i.id === id); |
| 24 | + if (!item) throw new Error(`Record not found: ${object} ${id}`); |
| 25 | + return item; |
| 26 | + } |
| 27 | + |
| 28 | + async createData(object: string, data: any): Promise<any> { |
| 29 | + if (!this.data[object]) this.data[object] = []; |
| 30 | + const newItem = { id: Math.random().toString(36).substr(2, 9), ...data }; |
| 31 | + this.data[object].push(newItem); |
| 32 | + return newItem; |
| 33 | + } |
| 34 | + |
| 35 | + async updateData(object: string, id: string, data: any): Promise<any> { |
| 36 | + const items = this.data[object] || []; |
| 37 | + const index = items.findIndex((i: any) => i.id === id); |
| 38 | + if (index === -1) throw new Error(`Record not found: ${object} ${id}`); |
| 39 | + |
| 40 | + const updated = { ...items[index], ...data }; |
| 41 | + this.data[object][index] = updated; |
| 42 | + return updated; |
| 43 | + } |
| 44 | + |
| 45 | + async deleteData(object: string, id: string): Promise<any> { |
| 46 | + if (!this.data[object]) return { success: false }; |
| 47 | + const initialLength = this.data[object].length; |
| 48 | + this.data[object] = this.data[object].filter((i: any) => i.id !== id); |
| 49 | + return { success: this.data[object].length < initialLength }; |
| 50 | + } |
| 51 | +} |
0 commit comments