Skip to content

Commit b9df62f

Browse files
committed
feat: Add MixDbClient for database operations with error handling
1 parent b5c4b2d commit b9df62f

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-0
lines changed

packages/database/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ export * from './related-attribute-data-rest-form-service';
99
export * from './related-attribute-data-rest-portal-service';
1010
export * from './related-attribute-set-rest-portal-service';
1111
export * from './module-data-services';
12+
13+
export * from './mix-db-client';
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
import { MixDatabaseRestPortalService } from './mix-database-rest-portal-service';
2+
import { MixDatabaseDataRestPortalService } from './mix-database-data-rest-portal-service';
3+
import type { ApiService, ApiResult } from '@mixcore/api';
4+
5+
// Optionally: Define types for params for better DX
6+
export interface ListDatabasesParams {
7+
pageSize?: number;
8+
status?: string;
9+
sortBy?: string;
10+
direction?: 'Asc' | 'Desc';
11+
searchColumns?: string;
12+
compareOperator?: string;
13+
conjunction?: string;
14+
columns?: string;
15+
keyword?: string;
16+
pageIndex?: number;
17+
}
18+
19+
export interface ListTablesParams {
20+
pageSize?: number;
21+
status?: string;
22+
direction?: 'Asc' | 'Desc';
23+
compareOperator?: string;
24+
conjunction?: string;
25+
keyword?: string;
26+
pageIndex?: number;
27+
sortBy?: string;
28+
}
29+
30+
/**
31+
* High-level MixDbClient for ergonomic MixDB database and table operations.
32+
* Wraps low-level SDK services and provides DRY, robust methods.
33+
*/
34+
export class MixDbClient {
35+
private dbService: MixDatabaseRestPortalService;
36+
private tableService: MixDatabaseDataRestPortalService;
37+
38+
constructor(apiService: ApiService) {
39+
this.dbService = new MixDatabaseRestPortalService(apiService);
40+
this.tableService = new MixDatabaseDataRestPortalService(apiService);
41+
}
42+
43+
/** List databases with sensible defaults */
44+
/**
45+
* List databases with sensible defaults and robust error handling.
46+
* Returns ApiResult or fallback mock data on error.
47+
*/
48+
async listDatabases(params?: ListDatabasesParams): Promise<ApiResult> {
49+
try {
50+
return await this.dbService['api'].get('/api/v2/rest/mix-portal/mix-db-database', {
51+
pageSize: 20,
52+
status: 'Published',
53+
sortBy: 'id',
54+
direction: 'Desc',
55+
searchColumns: 'displayName,systemName',
56+
compareOperator: 'Like',
57+
conjunction: 'Or',
58+
columns: 'id,displayName,systemName,type,createdDatetime',
59+
...params
60+
});
61+
} catch (error) {
62+
// Optionally: Provide fallback/mock data here
63+
return {
64+
isSucceed: false,
65+
data: { items: [] },
66+
errors: [error instanceof Error ? error.message : String(error)]
67+
};
68+
}
69+
}
70+
71+
async getDatabaseById(id: string): Promise<ApiResult> {
72+
try {
73+
return await this.dbService['api'].get(`/api/v2/rest/mix-portal/mix-db-database/get-by/${id}`);
74+
} catch (error) {
75+
return {
76+
isSucceed: false,
77+
data: null,
78+
errors: [error instanceof Error ? error.message : String(error)]
79+
};
80+
}
81+
}
82+
83+
async createDatabase(data: any): Promise<ApiResult> {
84+
try {
85+
return await this.dbService['api'].post('/api/v2/rest/mix-portal/mix-db-database/save', data);
86+
} catch (error) {
87+
return {
88+
isSucceed: false,
89+
data: null,
90+
errors: [error instanceof Error ? error.message : String(error)]
91+
};
92+
}
93+
}
94+
95+
async updateDatabase(data: any): Promise<ApiResult> {
96+
try {
97+
return await this.dbService['api'].post('/api/v2/rest/mix-portal/mix-db-database/save', data);
98+
} catch (error) {
99+
return {
100+
isSucceed: false,
101+
data: null,
102+
errors: [error instanceof Error ? error.message : String(error)]
103+
};
104+
}
105+
}
106+
107+
async deleteDatabase(id: string): Promise<ApiResult> {
108+
try {
109+
return await this.dbService['api'].delete(`/api/v2/rest/mix-portal/mix-db-database/delete/${id}`);
110+
} catch (error) {
111+
return {
112+
isSucceed: false,
113+
data: null,
114+
errors: [error instanceof Error ? error.message : String(error)]
115+
};
116+
}
117+
}
118+
119+
/** List tables for a database */
120+
/**
121+
* List tables for a database with sensible defaults and robust error handling.
122+
* Returns ApiResult or fallback mock data on error.
123+
*/
124+
async listTables(mixDbDatabaseId: string | number, params?: ListTablesParams): Promise<ApiResult> {
125+
try {
126+
return await this.dbService['api'].get('/api/v2/rest/mix-portal/mix-db-table', {
127+
pageSize: 20,
128+
status: 'Published',
129+
direction: 'Desc',
130+
compareOperator: 'Like',
131+
conjunction: 'Or',
132+
mixDbDatabaseId: mixDbDatabaseId.toString(),
133+
...params
134+
});
135+
} catch (error) {
136+
// Optionally: Provide fallback/mock data here
137+
return {
138+
isSucceed: false,
139+
data: { items: [] },
140+
errors: [error instanceof Error ? error.message : String(error)]
141+
};
142+
}
143+
}
144+
145+
async getTableById(id: string): Promise<ApiResult> {
146+
try {
147+
return await this.dbService['api'].get(`/api/v2/rest/mix-portal/mix-db-table/get-by/${id}`);
148+
} catch (error) {
149+
return {
150+
isSucceed: false,
151+
data: null,
152+
errors: [error instanceof Error ? error.message : String(error)]
153+
};
154+
}
155+
}
156+
157+
async createTable(data: any): Promise<ApiResult> {
158+
try {
159+
return await this.dbService['api'].post('/api/v2/rest/mix-portal/mix-db-table/save', data);
160+
} catch (error) {
161+
return {
162+
isSucceed: false,
163+
data: null,
164+
errors: [error instanceof Error ? error.message : String(error)]
165+
};
166+
}
167+
}
168+
169+
async updateTable(data: any): Promise<ApiResult> {
170+
try {
171+
return await this.dbService['api'].post('/api/v2/rest/mix-portal/mix-db-table/save', data);
172+
} catch (error) {
173+
return {
174+
isSucceed: false,
175+
data: null,
176+
errors: [error instanceof Error ? error.message : String(error)]
177+
};
178+
}
179+
}
180+
181+
async deleteTable(id: string): Promise<ApiResult> {
182+
try {
183+
return await this.dbService['api'].delete(`/api/v2/rest/mix-portal/mix-db-table/delete/${id}`);
184+
} catch (error) {
185+
return {
186+
isSucceed: false,
187+
data: null,
188+
errors: [error instanceof Error ? error.message : String(error)]
189+
};
190+
}
191+
}
192+
}
193+
194+
/** Factory function for ergonomic usage */
195+
export function createMixDbClient(apiService: ApiService) {
196+
return new MixDbClient(apiService);
197+
}

0 commit comments

Comments
 (0)