-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValueDataSource.ts
More file actions
459 lines (407 loc) · 14.2 KB
/
ValueDataSource.ts
File metadata and controls
459 lines (407 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/**
* ObjectUI — ValueDataSource
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* A DataSource adapter for the `provider: 'value'` ViewData mode.
* Operates entirely on an in-memory array — no network requests.
*/
import type {
DataSource,
MutationEvent,
QueryParams,
QueryResult,
AggregateParams,
AggregateResult,
} from '@object-ui/types';
// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------
export interface ValueDataSourceConfig<T = any> {
/** The static data array */
items: T[];
/** Optional ID field name for findOne/update/delete (defaults to 'id' then '_id') */
idField?: string;
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Resolve the ID of a record given possible field names */
function getRecordId(record: any, idField?: string): string | number | undefined {
if (idField) return record[idField];
return record.id ?? record._id;
}
/**
* Evaluate an AST-format filter node against a record.
* Supports conditions like ['field', 'op', value] and logical
* combinations like ['and', ...conditions] or ['or', ...conditions].
*/
function matchesASTFilter(record: any, filterNode: any[]): boolean {
if (!filterNode || filterNode.length === 0) return true;
const head = filterNode[0];
// Logical operators: ['and', ...conditions] or ['or', ...conditions]
if (head === 'and') {
return filterNode.slice(1).every((sub: any) => matchesASTFilter(record, sub));
}
if (head === 'or') {
return filterNode.slice(1).some((sub: any) => matchesASTFilter(record, sub));
}
// Condition node: [field, operator, value]
if (filterNode.length === 3 && typeof head === 'string') {
const [field, operator, target] = filterNode;
const value = record[field];
switch (operator) {
case '=':
return value === target;
case '!=':
return value !== target;
case '>':
return value > target;
case '>=':
return value >= target;
case '<':
return value < target;
case '<=':
return value <= target;
case 'in':
return Array.isArray(target) && target.includes(value);
case 'not in':
case 'notin': // alias used by convertFiltersToAST
return Array.isArray(target) && !target.includes(value);
case 'contains': {
const lv = typeof value === 'string' ? value.toLowerCase() : '';
return typeof value === 'string' && lv.includes(String(target).toLowerCase());
}
case 'notcontains': {
const lv = typeof value === 'string' ? value.toLowerCase() : '';
return typeof value === 'string' && !lv.includes(String(target).toLowerCase());
}
case 'startswith': {
const lv = typeof value === 'string' ? value.toLowerCase() : '';
return typeof value === 'string' && lv.startsWith(String(target).toLowerCase());
}
case 'between':
return Array.isArray(target) && target.length === 2 && value >= target[0] && value <= target[1];
default:
return true;
}
}
return true;
}
/**
* Simple in-memory filter evaluation.
* Supports flat key-value equality and basic operators ($gt, $gte, $lt, $lte, $ne, $in).
*/
function matchesFilter(record: any, filter: Record<string, any>): boolean {
for (const [key, condition] of Object.entries(filter)) {
const value = record[key];
if (condition && typeof condition === 'object' && !Array.isArray(condition)) {
// Operator-based filter
for (const [op, target] of Object.entries(condition)) {
switch (op) {
case '$gt':
if (!(value > (target as any))) return false;
break;
case '$gte':
if (!(value >= (target as any))) return false;
break;
case '$lt':
if (!(value < (target as any))) return false;
break;
case '$lte':
if (!(value <= (target as any))) return false;
break;
case '$ne':
if (value === target) return false;
break;
case '$in':
if (!Array.isArray(target) || !target.includes(value)) return false;
break;
case '$contains':
if (typeof value !== 'string' || !value.toLowerCase().includes(String(target).toLowerCase())) return false;
break;
default:
break;
}
}
} else {
// Simple equality
if (value !== condition) return false;
}
}
return true;
}
/** Apply sort ordering to an array (returns a new sorted array) */
function applySort<T>(
data: T[],
orderby?: QueryParams['$orderby'],
): T[] {
if (!orderby) return data;
// Normalize to array of { field, order }
let sorts: Array<{ field: string; order: 'asc' | 'desc' }> = [];
if (Array.isArray(orderby)) {
sorts = orderby.map((item) => {
if (typeof item === 'string') {
if (item.startsWith('-')) {
return { field: item.slice(1), order: 'desc' as const };
}
return { field: item, order: 'asc' as const };
}
return { field: item.field, order: (item.order ?? 'asc') as 'asc' | 'desc' };
});
} else if (typeof orderby === 'object') {
sorts = Object.entries(orderby).map(([field, order]) => ({
field,
order: order as 'asc' | 'desc',
}));
}
if (sorts.length === 0) return data;
return [...data].sort((a: any, b: any) => {
for (const { field, order } of sorts) {
const av = a[field];
const bv = b[field];
if (av === bv) continue;
if (av == null) return order === 'asc' ? -1 : 1;
if (bv == null) return order === 'asc' ? 1 : -1;
const cmp = av < bv ? -1 : 1;
return order === 'asc' ? cmp : -cmp;
}
return 0;
});
}
/** Pick specific fields from a record */
function selectFields<T>(record: T, fields?: string[]): T {
if (!fields || fields.length === 0) return record;
const out: any = {};
for (const f of fields) {
if (f in (record as any)) {
out[f] = (record as any)[f];
}
}
return out as T;
}
// ---------------------------------------------------------------------------
// ValueDataSource
// ---------------------------------------------------------------------------
/**
* ValueDataSource — an in-memory DataSource backed by a static array.
*
* Used when `ViewData.provider === 'value'`. All operations are synchronous
* (but wrapped in Promises to satisfy the DataSource interface). Supports
* basic filter, sort, pagination, and CRUD operations.
*
* @example
* ```ts
* const ds = new ValueDataSource({
* items: [
* { id: '1', name: 'Alice', age: 30 },
* { id: '2', name: 'Bob', age: 25 },
* ],
* });
*
* const result = await ds.find('users', { $filter: { age: { $gt: 26 } } });
* // result.data === [{ id: '1', name: 'Alice', age: 30 }]
* ```
*/
export class ValueDataSource<T = any> implements DataSource<T> {
private items: T[];
private idField: string | undefined;
private mutationListeners = new Set<(event: MutationEvent<T>) => void>();
constructor(config: ValueDataSourceConfig<T>) {
// Deep clone to prevent external mutation
this.items = JSON.parse(JSON.stringify(config.items));
this.idField = config.idField;
}
/** Notify all mutation subscribers */
private emitMutation(event: MutationEvent<T>): void {
for (const listener of this.mutationListeners) {
try { listener(event); } catch (err) { console.warn('ValueDataSource: mutation listener error', err); }
}
}
// -----------------------------------------------------------------------
// DataSource interface
// -----------------------------------------------------------------------
async find(_resource: string, params?: QueryParams): Promise<QueryResult<T>> {
let result = [...this.items];
// Filter — support both MongoDB-style objects and AST-format arrays
if (params?.$filter) {
if (Array.isArray(params.$filter) && params.$filter.length > 0) {
result = result.filter((r) => matchesASTFilter(r, params.$filter as any[]));
} else if (!Array.isArray(params.$filter) && Object.keys(params.$filter).length > 0) {
result = result.filter((r) => matchesFilter(r, params.$filter!));
}
}
// Search (simple text search across all string fields)
if (params?.$search) {
const q = params.$search.toLowerCase();
result = result.filter((r) =>
Object.values(r as any).some(
(v) => typeof v === 'string' && v.toLowerCase().includes(q),
),
);
}
const totalCount = result.length;
// Sort
result = applySort(result, params?.$orderby);
// Pagination
const skip = params?.$skip ?? 0;
const top = params?.$top;
if (skip > 0) result = result.slice(skip);
if (top !== undefined) result = result.slice(0, top);
// Select
if (params?.$select?.length) {
result = result.map((r) => selectFields(r, params.$select));
}
return {
data: result,
total: totalCount,
hasMore: skip + (top ?? result.length) < totalCount,
};
}
async findOne(
_resource: string,
id: string | number,
params?: QueryParams,
): Promise<T | null> {
const record = this.items.find(
(r) => String(getRecordId(r, this.idField)) === String(id),
);
if (!record) return null;
if (params?.$select?.length) {
return selectFields(record, params.$select);
}
return { ...record };
}
async create(_resource: string, data: Partial<T>): Promise<T> {
const record = { ...data } as T;
// Auto-generate an ID if missing
if (!getRecordId(record, this.idField)) {
const field = this.idField ?? 'id';
(record as any)[field] = `auto_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
}
this.items.push(record);
this.emitMutation({ type: 'create', resource: _resource, record: { ...record } });
return { ...record };
}
async update(
_resource: string,
id: string | number,
data: Partial<T>,
): Promise<T> {
const index = this.items.findIndex(
(r) => String(getRecordId(r, this.idField)) === String(id),
);
if (index === -1) {
throw new Error(`ValueDataSource: Record with id "${id}" not found`);
}
this.items[index] = { ...this.items[index], ...data };
this.emitMutation({ type: 'update', resource: _resource, id, record: { ...this.items[index] } });
return { ...this.items[index] };
}
async delete(_resource: string, id: string | number): Promise<boolean> {
const index = this.items.findIndex(
(r) => String(getRecordId(r, this.idField)) === String(id),
);
if (index === -1) return false;
this.items.splice(index, 1);
this.emitMutation({ type: 'delete', resource: _resource, id });
return true;
}
async bulk(
_resource: string,
operation: 'create' | 'update' | 'delete',
data: Partial<T>[],
): Promise<T[]> {
const results: T[] = [];
for (const item of data) {
switch (operation) {
case 'create':
results.push(await this.create(_resource, item));
break;
case 'update': {
const id = getRecordId(item, this.idField);
if (id !== undefined) {
results.push(await this.update(_resource, id, item));
}
break;
}
case 'delete': {
const id = getRecordId(item, this.idField);
if (id !== undefined) {
await this.delete(_resource, id);
}
break;
}
}
}
return results;
}
async getObjectSchema(_objectName: string): Promise<any> {
// Infer a minimal schema from the first item
if (this.items.length === 0) return { name: _objectName, fields: {} };
const sample = this.items[0];
const fields: Record<string, any> = {};
for (const [key, value] of Object.entries(sample as any)) {
fields[key] = { type: typeof value };
}
return { name: _objectName, fields };
}
async getView(_objectName: string, _viewId: string): Promise<any | null> {
return null;
}
async getApp(_appId: string): Promise<any | null> {
return null;
}
async aggregate(_resource: string, params: AggregateParams): Promise<AggregateResult[]> {
const { field, function: aggFn, groupBy } = params;
const groups: Record<string, any[]> = {};
for (const record of this.items as any[]) {
const key = String(record[groupBy] ?? 'Unknown');
if (!groups[key]) groups[key] = [];
groups[key].push(record);
}
return Object.entries(groups).map(([key, group]) => {
const values = group.map(r => Number(r[field]) || 0);
let result: number;
switch (aggFn) {
case 'count':
result = group.length;
break;
case 'avg':
result = values.length > 0 ? values.reduce((a, b) => a + b, 0) / values.length : 0;
break;
case 'min':
result = values.length > 0 ? Math.min(...values) : 0;
break;
case 'max':
result = values.length > 0 ? Math.max(...values) : 0;
break;
case 'sum':
default:
result = values.reduce((a, b) => a + b, 0);
break;
}
return { [groupBy]: key, [field]: result };
});
}
// -----------------------------------------------------------------------
// Mutation subscription (P2 — Event Bus)
// -----------------------------------------------------------------------
onMutation(callback: (event: MutationEvent<T>) => void): () => void {
this.mutationListeners.add(callback);
return () => { this.mutationListeners.delete(callback); };
}
// -----------------------------------------------------------------------
// Extra utilities
// -----------------------------------------------------------------------
/** Get the current number of items */
get count(): number {
return this.items.length;
}
/** Get a snapshot of all items (cloned) */
getAll(): T[] {
return JSON.parse(JSON.stringify(this.items));
}
}