Skip to content

Commit 70de8b6

Browse files
committed
feat: add MemoryProtocol implementation and update package dependencies
1 parent 75dc291 commit 70de8b6

4 files changed

Lines changed: 63 additions & 1 deletion

File tree

examples/crm-app/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
"@object-ui/plugin-grid": "workspace:*",
2121
"@object-ui/react": "workspace:*",
2222
"@object-ui/types": "workspace:*",
23+
"@objectstack/core": "^0.6.1",
24+
"@objectstack/runtime": "^0.6.1",
2325
"clsx": "^2.1.1",
2426
"lucide-react": "^0.563.0",
2527
"react": "^18.3.1",
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

examples/crm-app/src/mocks/handlers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import { http, HttpResponse } from 'msw';
22
import { ObjectStackServer } from '@objectstack/plugin-msw';
33
import { protocol } from './protocol';
44
import { mockData } from '../data';
5+
import { MemoryProtocol } from './MemoryProtocol';
56

67
// Initialize the mock server with our protocol definition
7-
ObjectStackServer.init(protocol as any);
8+
// The JSON protocol def is insufficient for methods, so we wrap it or replace it with an implementation
9+
const memoryProtocol = new MemoryProtocol({}, (protocol as any).objects);
10+
ObjectStackServer.init(memoryProtocol as any);
811

912
export const seedData = async () => {
1013
// Seed User

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)