@object-ui/data-objectstack 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +317 -1
- package/dist/index.cjs +649 -34
- package/dist/index.d.cts +267 -7
- package/dist/index.d.ts +267 -7
- package/dist/index.js +639 -33
- package/package.json +4 -4
- package/src/cache/MetadataCache.test.ts +426 -0
- package/src/cache/MetadataCache.ts +229 -0
- package/src/connection.test.ts +100 -0
- package/src/errors.test.ts +426 -0
- package/src/errors.ts +275 -0
- package/src/index.ts +463 -42
package/dist/index.js
CHANGED
|
@@ -5,11 +5,351 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
5
5
|
// src/index.ts
|
|
6
6
|
import { ObjectStackClient } from "@objectstack/client";
|
|
7
7
|
import { convertFiltersToAST } from "@object-ui/core";
|
|
8
|
+
|
|
9
|
+
// src/cache/MetadataCache.ts
|
|
10
|
+
var MetadataCache = class {
|
|
11
|
+
/**
|
|
12
|
+
* Create a new MetadataCache instance
|
|
13
|
+
*
|
|
14
|
+
* @param options - Configuration options
|
|
15
|
+
* @param options.maxSize - Maximum number of entries (default: 100)
|
|
16
|
+
* @param options.ttl - Time to live in milliseconds (default: 5 minutes)
|
|
17
|
+
*/
|
|
18
|
+
constructor(options = {}) {
|
|
19
|
+
__publicField(this, "cache");
|
|
20
|
+
__publicField(this, "maxSize");
|
|
21
|
+
__publicField(this, "ttl");
|
|
22
|
+
__publicField(this, "stats");
|
|
23
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
24
|
+
this.maxSize = options.maxSize || 100;
|
|
25
|
+
this.ttl = options.ttl || 5 * 60 * 1e3;
|
|
26
|
+
this.stats = {
|
|
27
|
+
hits: 0,
|
|
28
|
+
misses: 0,
|
|
29
|
+
evictions: 0
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Get a value from cache or fetch it using the provided fetcher function
|
|
34
|
+
*
|
|
35
|
+
* @param key - Cache key
|
|
36
|
+
* @param fetcher - Async function to fetch data if not in cache
|
|
37
|
+
* @returns Promise resolving to the cached or fetched data
|
|
38
|
+
*/
|
|
39
|
+
async get(key, fetcher) {
|
|
40
|
+
const now = Date.now();
|
|
41
|
+
const cached = this.cache.get(key);
|
|
42
|
+
if (cached) {
|
|
43
|
+
const age = now - cached.timestamp;
|
|
44
|
+
if (age < this.ttl) {
|
|
45
|
+
cached.accessCount++;
|
|
46
|
+
cached.lastAccessed = now;
|
|
47
|
+
this.stats.hits++;
|
|
48
|
+
this.cache.delete(key);
|
|
49
|
+
this.cache.set(key, cached);
|
|
50
|
+
return cached.data;
|
|
51
|
+
} else {
|
|
52
|
+
this.cache.delete(key);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
this.stats.misses++;
|
|
56
|
+
const data = await fetcher();
|
|
57
|
+
this.set(key, data);
|
|
58
|
+
return data;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Set a value in the cache
|
|
62
|
+
*
|
|
63
|
+
* @param key - Cache key
|
|
64
|
+
* @param data - Data to cache
|
|
65
|
+
*/
|
|
66
|
+
set(key, data) {
|
|
67
|
+
const now = Date.now();
|
|
68
|
+
if (this.cache.size >= this.maxSize && !this.cache.has(key)) {
|
|
69
|
+
this.evictLRU();
|
|
70
|
+
}
|
|
71
|
+
this.cache.set(key, {
|
|
72
|
+
data,
|
|
73
|
+
timestamp: now,
|
|
74
|
+
accessCount: 1,
|
|
75
|
+
lastAccessed: now
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Evict the least recently used entry
|
|
80
|
+
*/
|
|
81
|
+
evictLRU() {
|
|
82
|
+
const firstKey = this.cache.keys().next().value;
|
|
83
|
+
if (firstKey !== void 0) {
|
|
84
|
+
this.cache.delete(firstKey);
|
|
85
|
+
this.stats.evictions++;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Invalidate a specific cache entry or all entries
|
|
90
|
+
*
|
|
91
|
+
* @param key - Optional key to invalidate. If omitted, invalidates all entries
|
|
92
|
+
*/
|
|
93
|
+
invalidate(key) {
|
|
94
|
+
if (key) {
|
|
95
|
+
this.cache.delete(key);
|
|
96
|
+
} else {
|
|
97
|
+
this.cache.clear();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Clear all cache entries and reset statistics
|
|
102
|
+
*/
|
|
103
|
+
clear() {
|
|
104
|
+
this.cache.clear();
|
|
105
|
+
this.stats = {
|
|
106
|
+
hits: 0,
|
|
107
|
+
misses: 0,
|
|
108
|
+
evictions: 0
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Get cache statistics
|
|
113
|
+
*
|
|
114
|
+
* @returns Cache statistics including hit rate
|
|
115
|
+
*/
|
|
116
|
+
getStats() {
|
|
117
|
+
const total = this.stats.hits + this.stats.misses;
|
|
118
|
+
const hitRate = total > 0 ? this.stats.hits / total : 0;
|
|
119
|
+
return {
|
|
120
|
+
size: this.cache.size,
|
|
121
|
+
maxSize: this.maxSize,
|
|
122
|
+
hits: this.stats.hits,
|
|
123
|
+
misses: this.stats.misses,
|
|
124
|
+
evictions: this.stats.evictions,
|
|
125
|
+
hitRate
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Check if a key exists in the cache (and is not expired)
|
|
130
|
+
*
|
|
131
|
+
* @param key - Cache key to check
|
|
132
|
+
* @returns true if the key exists and is not expired
|
|
133
|
+
*/
|
|
134
|
+
has(key) {
|
|
135
|
+
const cached = this.cache.get(key);
|
|
136
|
+
if (!cached) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
const age = Date.now() - cached.timestamp;
|
|
140
|
+
if (age >= this.ttl) {
|
|
141
|
+
this.cache.delete(key);
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// src/errors.ts
|
|
149
|
+
var ObjectStackError = class extends Error {
|
|
150
|
+
/**
|
|
151
|
+
* Create a new ObjectStackError
|
|
152
|
+
*
|
|
153
|
+
* @param message - Human-readable error message
|
|
154
|
+
* @param code - Unique error code for programmatic handling
|
|
155
|
+
* @param statusCode - Optional HTTP status code
|
|
156
|
+
* @param details - Optional additional error details for debugging
|
|
157
|
+
*/
|
|
158
|
+
constructor(message, code, statusCode, details) {
|
|
159
|
+
super(message);
|
|
160
|
+
this.code = code;
|
|
161
|
+
this.statusCode = statusCode;
|
|
162
|
+
this.details = details;
|
|
163
|
+
this.name = "ObjectStackError";
|
|
164
|
+
if (Error.captureStackTrace) {
|
|
165
|
+
Error.captureStackTrace(this, this.constructor);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Convert error to JSON for logging/debugging
|
|
170
|
+
*/
|
|
171
|
+
toJSON() {
|
|
172
|
+
return {
|
|
173
|
+
name: this.name,
|
|
174
|
+
message: this.message,
|
|
175
|
+
code: this.code,
|
|
176
|
+
statusCode: this.statusCode,
|
|
177
|
+
details: this.details,
|
|
178
|
+
stack: this.stack
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
var MetadataNotFoundError = class extends ObjectStackError {
|
|
183
|
+
constructor(objectName, details) {
|
|
184
|
+
super(
|
|
185
|
+
`Metadata not found for object: ${objectName}`,
|
|
186
|
+
"METADATA_NOT_FOUND",
|
|
187
|
+
404,
|
|
188
|
+
{ objectName, ...details }
|
|
189
|
+
);
|
|
190
|
+
this.name = "MetadataNotFoundError";
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
var BulkOperationError = class extends ObjectStackError {
|
|
194
|
+
/**
|
|
195
|
+
* Create a new BulkOperationError
|
|
196
|
+
*
|
|
197
|
+
* @param operation - The bulk operation that failed (create, update, delete)
|
|
198
|
+
* @param successCount - Number of successful operations
|
|
199
|
+
* @param failureCount - Number of failed operations
|
|
200
|
+
* @param errors - Array of individual errors
|
|
201
|
+
* @param details - Additional error details
|
|
202
|
+
*/
|
|
203
|
+
constructor(operation, successCount, failureCount, errors, details) {
|
|
204
|
+
super(
|
|
205
|
+
`Bulk ${operation} operation failed: ${successCount} succeeded, ${failureCount} failed`,
|
|
206
|
+
"BULK_OPERATION_ERROR",
|
|
207
|
+
500,
|
|
208
|
+
{
|
|
209
|
+
operation,
|
|
210
|
+
successCount,
|
|
211
|
+
failureCount,
|
|
212
|
+
errors,
|
|
213
|
+
...details
|
|
214
|
+
}
|
|
215
|
+
);
|
|
216
|
+
this.successCount = successCount;
|
|
217
|
+
this.failureCount = failureCount;
|
|
218
|
+
this.errors = errors;
|
|
219
|
+
this.name = "BulkOperationError";
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Get a summary of the bulk operation failure
|
|
223
|
+
*/
|
|
224
|
+
getSummary() {
|
|
225
|
+
const total = this.successCount + this.failureCount;
|
|
226
|
+
const failureRate = total > 0 ? this.failureCount / total : 0;
|
|
227
|
+
return {
|
|
228
|
+
operation: this.details?.operation,
|
|
229
|
+
total,
|
|
230
|
+
successful: this.successCount,
|
|
231
|
+
failed: this.failureCount,
|
|
232
|
+
failureRate,
|
|
233
|
+
errors: this.errors
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
var ConnectionError = class extends ObjectStackError {
|
|
238
|
+
constructor(message, url, details, statusCode) {
|
|
239
|
+
super(
|
|
240
|
+
`Connection error: ${message}`,
|
|
241
|
+
"CONNECTION_ERROR",
|
|
242
|
+
statusCode || 503,
|
|
243
|
+
{ url, ...details }
|
|
244
|
+
);
|
|
245
|
+
this.url = url;
|
|
246
|
+
this.name = "ConnectionError";
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
var AuthenticationError = class extends ObjectStackError {
|
|
250
|
+
constructor(message = "Authentication failed", details, statusCode) {
|
|
251
|
+
super(
|
|
252
|
+
message,
|
|
253
|
+
"AUTHENTICATION_ERROR",
|
|
254
|
+
statusCode || 401,
|
|
255
|
+
details
|
|
256
|
+
);
|
|
257
|
+
this.name = "AuthenticationError";
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
var ValidationError = class extends ObjectStackError {
|
|
261
|
+
/**
|
|
262
|
+
* Create a new ValidationError
|
|
263
|
+
*
|
|
264
|
+
* @param message - Human-readable error message
|
|
265
|
+
* @param field - The field that failed validation (optional)
|
|
266
|
+
* @param validationErrors - Array of validation error details
|
|
267
|
+
* @param details - Additional error details
|
|
268
|
+
*/
|
|
269
|
+
constructor(message, field, validationErrors, details) {
|
|
270
|
+
super(
|
|
271
|
+
message,
|
|
272
|
+
"VALIDATION_ERROR",
|
|
273
|
+
400,
|
|
274
|
+
{
|
|
275
|
+
field,
|
|
276
|
+
validationErrors,
|
|
277
|
+
...details
|
|
278
|
+
}
|
|
279
|
+
);
|
|
280
|
+
this.field = field;
|
|
281
|
+
this.validationErrors = validationErrors;
|
|
282
|
+
this.name = "ValidationError";
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Get all validation errors as a formatted list
|
|
286
|
+
*/
|
|
287
|
+
getValidationErrors() {
|
|
288
|
+
return this.validationErrors || [];
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
function createErrorFromResponse(response, context) {
|
|
292
|
+
const status = response?.status || response?.statusCode || 500;
|
|
293
|
+
const message = response?.message || response?.statusText || "Unknown error";
|
|
294
|
+
const details = {
|
|
295
|
+
context,
|
|
296
|
+
response: {
|
|
297
|
+
status,
|
|
298
|
+
data: response?.data,
|
|
299
|
+
headers: response?.headers
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
switch (status) {
|
|
303
|
+
case 401:
|
|
304
|
+
return new AuthenticationError(message, details, 401);
|
|
305
|
+
case 403:
|
|
306
|
+
return new AuthenticationError(message, details, 403);
|
|
307
|
+
case 404:
|
|
308
|
+
if (context?.includes("metadata") || context?.includes("schema") || context?.includes("getObjectSchema")) {
|
|
309
|
+
const objectName = extractObjectName(context);
|
|
310
|
+
return new MetadataNotFoundError(objectName, details);
|
|
311
|
+
}
|
|
312
|
+
return new ObjectStackError(message, "NOT_FOUND", 404, details);
|
|
313
|
+
case 400:
|
|
314
|
+
return new ValidationError(message, void 0, response?.data?.errors, details);
|
|
315
|
+
case 503:
|
|
316
|
+
return new ConnectionError(message, response?.config?.url, details, 503);
|
|
317
|
+
case 504:
|
|
318
|
+
return new ConnectionError(message, response?.config?.url, details, 504);
|
|
319
|
+
default:
|
|
320
|
+
return new ObjectStackError(message, "UNKNOWN_ERROR", status, details);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
function extractObjectName(context) {
|
|
324
|
+
if (!context) return "unknown";
|
|
325
|
+
const match = context.match(/\(([^)]+)\)/);
|
|
326
|
+
return match ? match[1] : "unknown";
|
|
327
|
+
}
|
|
328
|
+
function isObjectStackError(error) {
|
|
329
|
+
return error instanceof ObjectStackError;
|
|
330
|
+
}
|
|
331
|
+
function isErrorType(error, errorClass) {
|
|
332
|
+
return error instanceof errorClass;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// src/index.ts
|
|
8
336
|
var ObjectStackAdapter = class {
|
|
9
337
|
constructor(config) {
|
|
10
338
|
__publicField(this, "client");
|
|
11
339
|
__publicField(this, "connected", false);
|
|
340
|
+
__publicField(this, "metadataCache");
|
|
341
|
+
__publicField(this, "connectionState", "disconnected");
|
|
342
|
+
__publicField(this, "connectionStateListeners", []);
|
|
343
|
+
__publicField(this, "batchProgressListeners", []);
|
|
344
|
+
__publicField(this, "autoReconnect");
|
|
345
|
+
__publicField(this, "maxReconnectAttempts");
|
|
346
|
+
__publicField(this, "reconnectDelay");
|
|
347
|
+
__publicField(this, "reconnectAttempts", 0);
|
|
12
348
|
this.client = new ObjectStackClient(config);
|
|
349
|
+
this.metadataCache = new MetadataCache(config.cache);
|
|
350
|
+
this.autoReconnect = config.autoReconnect ?? true;
|
|
351
|
+
this.maxReconnectAttempts = config.maxReconnectAttempts ?? 3;
|
|
352
|
+
this.reconnectDelay = config.reconnectDelay ?? 1e3;
|
|
13
353
|
}
|
|
14
354
|
/**
|
|
15
355
|
* Ensure the client is connected to the server.
|
|
@@ -17,10 +357,105 @@ var ObjectStackAdapter = class {
|
|
|
17
357
|
*/
|
|
18
358
|
async connect() {
|
|
19
359
|
if (!this.connected) {
|
|
20
|
-
|
|
21
|
-
|
|
360
|
+
this.setConnectionState("connecting");
|
|
361
|
+
try {
|
|
362
|
+
await this.client.connect();
|
|
363
|
+
this.connected = true;
|
|
364
|
+
this.reconnectAttempts = 0;
|
|
365
|
+
this.setConnectionState("connected");
|
|
366
|
+
} catch (error) {
|
|
367
|
+
const errorMessage = error instanceof Error ? error.message : "Failed to connect to ObjectStack server";
|
|
368
|
+
const connectionError = new ConnectionError(
|
|
369
|
+
errorMessage,
|
|
370
|
+
void 0,
|
|
371
|
+
{ originalError: error }
|
|
372
|
+
);
|
|
373
|
+
this.setConnectionState("error", connectionError);
|
|
374
|
+
if (this.autoReconnect && this.reconnectAttempts < this.maxReconnectAttempts) {
|
|
375
|
+
await this.attemptReconnect();
|
|
376
|
+
} else {
|
|
377
|
+
throw connectionError;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
22
380
|
}
|
|
23
381
|
}
|
|
382
|
+
/**
|
|
383
|
+
* Attempt to reconnect to the server with exponential backoff
|
|
384
|
+
*/
|
|
385
|
+
async attemptReconnect() {
|
|
386
|
+
this.reconnectAttempts++;
|
|
387
|
+
this.setConnectionState("reconnecting");
|
|
388
|
+
const delay = this.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1);
|
|
389
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
390
|
+
this.connected = false;
|
|
391
|
+
await this.connect();
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Get the current connection state
|
|
395
|
+
*/
|
|
396
|
+
getConnectionState() {
|
|
397
|
+
return this.connectionState;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Check if the adapter is currently connected
|
|
401
|
+
*/
|
|
402
|
+
isConnected() {
|
|
403
|
+
return this.connected && this.connectionState === "connected";
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Register a listener for connection state changes
|
|
407
|
+
*/
|
|
408
|
+
onConnectionStateChange(listener) {
|
|
409
|
+
this.connectionStateListeners.push(listener);
|
|
410
|
+
return () => {
|
|
411
|
+
const index = this.connectionStateListeners.indexOf(listener);
|
|
412
|
+
if (index > -1) {
|
|
413
|
+
this.connectionStateListeners.splice(index, 1);
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Register a listener for batch operation progress
|
|
419
|
+
*/
|
|
420
|
+
onBatchProgress(listener) {
|
|
421
|
+
this.batchProgressListeners.push(listener);
|
|
422
|
+
return () => {
|
|
423
|
+
const index = this.batchProgressListeners.indexOf(listener);
|
|
424
|
+
if (index > -1) {
|
|
425
|
+
this.batchProgressListeners.splice(index, 1);
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Set connection state and notify listeners
|
|
431
|
+
*/
|
|
432
|
+
setConnectionState(state, error) {
|
|
433
|
+
this.connectionState = state;
|
|
434
|
+
const event = {
|
|
435
|
+
state,
|
|
436
|
+
timestamp: Date.now(),
|
|
437
|
+
error
|
|
438
|
+
};
|
|
439
|
+
this.connectionStateListeners.forEach((listener) => {
|
|
440
|
+
try {
|
|
441
|
+
listener(event);
|
|
442
|
+
} catch (err) {
|
|
443
|
+
console.error("Error in connection state listener:", err);
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Emit batch progress event to listeners
|
|
449
|
+
*/
|
|
450
|
+
emitBatchProgress(event) {
|
|
451
|
+
this.batchProgressListeners.forEach((listener) => {
|
|
452
|
+
try {
|
|
453
|
+
listener(event);
|
|
454
|
+
} catch (err) {
|
|
455
|
+
console.error("Error in batch progress listener:", err);
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
}
|
|
24
459
|
/**
|
|
25
460
|
* Find multiple records with query parameters.
|
|
26
461
|
* Converts OData-style params to ObjectStack query options.
|
|
@@ -29,12 +464,23 @@ var ObjectStackAdapter = class {
|
|
|
29
464
|
await this.connect();
|
|
30
465
|
const queryOptions = this.convertQueryParams(params);
|
|
31
466
|
const result = await this.client.data.find(resource, queryOptions);
|
|
467
|
+
if (Array.isArray(result)) {
|
|
468
|
+
return {
|
|
469
|
+
data: result,
|
|
470
|
+
total: result.length,
|
|
471
|
+
page: 1,
|
|
472
|
+
pageSize: result.length,
|
|
473
|
+
hasMore: false
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
const resultObj = result;
|
|
32
477
|
return {
|
|
33
|
-
data:
|
|
34
|
-
total:
|
|
35
|
-
|
|
478
|
+
data: resultObj.value || [],
|
|
479
|
+
total: resultObj.count || (resultObj.value ? resultObj.value.length : 0),
|
|
480
|
+
// Calculate page number safely
|
|
481
|
+
page: params?.$skip && params.$top ? Math.floor(params.$skip / params.$top) + 1 : 1,
|
|
36
482
|
pageSize: params?.$top,
|
|
37
|
-
hasMore:
|
|
483
|
+
hasMore: params?.$top ? (resultObj.value?.length || 0) === params.$top : false
|
|
38
484
|
};
|
|
39
485
|
}
|
|
40
486
|
/**
|
|
@@ -75,28 +521,133 @@ var ObjectStackAdapter = class {
|
|
|
75
521
|
return result.success;
|
|
76
522
|
}
|
|
77
523
|
/**
|
|
78
|
-
* Bulk operations
|
|
524
|
+
* Bulk operations with optimized batch processing and error handling.
|
|
525
|
+
* Emits progress events for tracking operation status.
|
|
526
|
+
*
|
|
527
|
+
* @param resource - Resource name
|
|
528
|
+
* @param operation - Operation type (create, update, delete)
|
|
529
|
+
* @param data - Array of records to process
|
|
530
|
+
* @returns Promise resolving to array of results
|
|
79
531
|
*/
|
|
80
532
|
async bulk(resource, operation, data) {
|
|
81
533
|
await this.connect();
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
534
|
+
if (!data || data.length === 0) {
|
|
535
|
+
return [];
|
|
536
|
+
}
|
|
537
|
+
const total = data.length;
|
|
538
|
+
let completed = 0;
|
|
539
|
+
let failed = 0;
|
|
540
|
+
const emitProgress = () => {
|
|
541
|
+
this.emitBatchProgress({
|
|
542
|
+
operation,
|
|
543
|
+
total,
|
|
544
|
+
completed,
|
|
545
|
+
failed,
|
|
546
|
+
percentage: total > 0 ? (completed + failed) / total * 100 : 0
|
|
547
|
+
});
|
|
548
|
+
};
|
|
549
|
+
try {
|
|
550
|
+
switch (operation) {
|
|
551
|
+
case "create": {
|
|
552
|
+
emitProgress();
|
|
553
|
+
const created = await this.client.data.createMany(resource, data);
|
|
554
|
+
completed = created.length;
|
|
555
|
+
failed = total - completed;
|
|
556
|
+
emitProgress();
|
|
557
|
+
return created;
|
|
558
|
+
}
|
|
559
|
+
case "delete": {
|
|
560
|
+
const ids = data.map((item) => item.id).filter(Boolean);
|
|
561
|
+
if (ids.length === 0) {
|
|
562
|
+
const errors = data.map((_, index) => ({
|
|
563
|
+
index,
|
|
564
|
+
error: `Missing ID for item at index ${index}`
|
|
565
|
+
}));
|
|
566
|
+
failed = data.length;
|
|
567
|
+
emitProgress();
|
|
568
|
+
throw new BulkOperationError("delete", 0, data.length, errors);
|
|
569
|
+
}
|
|
570
|
+
emitProgress();
|
|
571
|
+
await this.client.data.deleteMany(resource, ids);
|
|
572
|
+
completed = ids.length;
|
|
573
|
+
failed = total - completed;
|
|
574
|
+
emitProgress();
|
|
575
|
+
return [];
|
|
576
|
+
}
|
|
577
|
+
case "update": {
|
|
578
|
+
if (typeof this.client.data.updateMany === "function") {
|
|
579
|
+
try {
|
|
580
|
+
emitProgress();
|
|
581
|
+
const updateMany = this.client.data.updateMany;
|
|
582
|
+
const updated = await updateMany(resource, data);
|
|
583
|
+
completed = updated.length;
|
|
584
|
+
failed = total - completed;
|
|
585
|
+
emitProgress();
|
|
586
|
+
return updated;
|
|
587
|
+
} catch {
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
const results = [];
|
|
591
|
+
const errors = [];
|
|
592
|
+
for (let i = 0; i < data.length; i++) {
|
|
593
|
+
const item = data[i];
|
|
594
|
+
const id = item.id;
|
|
595
|
+
if (!id) {
|
|
596
|
+
errors.push({ index: i, error: "Missing ID" });
|
|
597
|
+
failed++;
|
|
598
|
+
emitProgress();
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
try {
|
|
602
|
+
const result = await this.client.data.update(resource, String(id), item);
|
|
603
|
+
results.push(result);
|
|
604
|
+
completed++;
|
|
605
|
+
emitProgress();
|
|
606
|
+
} catch (error) {
|
|
607
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
608
|
+
errors.push({ index: i, error: errorMessage });
|
|
609
|
+
failed++;
|
|
610
|
+
emitProgress();
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
if (errors.length > 0) {
|
|
614
|
+
throw new BulkOperationError(
|
|
615
|
+
"update",
|
|
616
|
+
results.length,
|
|
617
|
+
errors.length,
|
|
618
|
+
errors,
|
|
619
|
+
{ resource, totalRecords: data.length }
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
return results;
|
|
623
|
+
}
|
|
624
|
+
default:
|
|
625
|
+
throw new ObjectStackError(
|
|
626
|
+
`Unsupported bulk operation: ${operation}`,
|
|
627
|
+
"UNSUPPORTED_OPERATION",
|
|
628
|
+
400
|
|
629
|
+
);
|
|
630
|
+
}
|
|
631
|
+
} catch (error) {
|
|
632
|
+
emitProgress();
|
|
633
|
+
if (error instanceof BulkOperationError) {
|
|
634
|
+
throw error;
|
|
97
635
|
}
|
|
98
|
-
|
|
99
|
-
throw
|
|
636
|
+
if (error instanceof ObjectStackError) {
|
|
637
|
+
throw error;
|
|
638
|
+
}
|
|
639
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
640
|
+
const errors = data.map((_, index) => ({
|
|
641
|
+
index,
|
|
642
|
+
error: errorMessage
|
|
643
|
+
}));
|
|
644
|
+
throw new BulkOperationError(
|
|
645
|
+
operation,
|
|
646
|
+
0,
|
|
647
|
+
data.length,
|
|
648
|
+
errors,
|
|
649
|
+
{ resource, originalError: error }
|
|
650
|
+
);
|
|
100
651
|
}
|
|
101
652
|
}
|
|
102
653
|
/**
|
|
@@ -110,13 +661,26 @@ var ObjectStackAdapter = class {
|
|
|
110
661
|
options.select = params.$select;
|
|
111
662
|
}
|
|
112
663
|
if (params.$filter) {
|
|
113
|
-
|
|
664
|
+
if (Array.isArray(params.$filter)) {
|
|
665
|
+
options.filters = params.$filter;
|
|
666
|
+
} else {
|
|
667
|
+
options.filters = convertFiltersToAST(params.$filter);
|
|
668
|
+
}
|
|
114
669
|
}
|
|
115
670
|
if (params.$orderby) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
671
|
+
if (Array.isArray(params.$orderby)) {
|
|
672
|
+
options.sort = params.$orderby.map((item) => {
|
|
673
|
+
if (typeof item === "string") return item;
|
|
674
|
+
const field = item.field;
|
|
675
|
+
const order = item.order || "asc";
|
|
676
|
+
return order === "desc" ? `-${field}` : field;
|
|
677
|
+
});
|
|
678
|
+
} else {
|
|
679
|
+
const sortArray = Object.entries(params.$orderby).map(([field, order]) => {
|
|
680
|
+
return order === "desc" ? `-${field}` : field;
|
|
681
|
+
});
|
|
682
|
+
options.sort = sortArray;
|
|
683
|
+
}
|
|
120
684
|
}
|
|
121
685
|
if (params.$skip !== void 0) {
|
|
122
686
|
options.skip = params.$skip;
|
|
@@ -128,6 +692,7 @@ var ObjectStackAdapter = class {
|
|
|
128
692
|
}
|
|
129
693
|
/**
|
|
130
694
|
* Get object schema/metadata from ObjectStack.
|
|
695
|
+
* Uses caching to improve performance for repeated requests.
|
|
131
696
|
*
|
|
132
697
|
* @param objectName - Object name
|
|
133
698
|
* @returns Promise resolving to the object schema
|
|
@@ -135,11 +700,23 @@ var ObjectStackAdapter = class {
|
|
|
135
700
|
async getObjectSchema(objectName) {
|
|
136
701
|
await this.connect();
|
|
137
702
|
try {
|
|
138
|
-
const schema = await this.
|
|
703
|
+
const schema = await this.metadataCache.get(objectName, async () => {
|
|
704
|
+
const result = await this.client.meta.getObject(objectName);
|
|
705
|
+
if (result && result.item) {
|
|
706
|
+
return result.item;
|
|
707
|
+
}
|
|
708
|
+
return result;
|
|
709
|
+
});
|
|
139
710
|
return schema;
|
|
140
711
|
} catch (error) {
|
|
141
|
-
|
|
142
|
-
|
|
712
|
+
const errorObj = error;
|
|
713
|
+
if (errorObj?.status === 404 || errorObj?.statusCode === 404) {
|
|
714
|
+
throw new MetadataNotFoundError(objectName, { originalError: error });
|
|
715
|
+
}
|
|
716
|
+
if (error instanceof ObjectStackError) {
|
|
717
|
+
throw error;
|
|
718
|
+
}
|
|
719
|
+
throw createErrorFromResponse(errorObj, `getObjectSchema(${objectName})`);
|
|
143
720
|
}
|
|
144
721
|
}
|
|
145
722
|
/**
|
|
@@ -148,11 +725,40 @@ var ObjectStackAdapter = class {
|
|
|
148
725
|
getClient() {
|
|
149
726
|
return this.client;
|
|
150
727
|
}
|
|
728
|
+
/**
|
|
729
|
+
* Get cache statistics for monitoring performance.
|
|
730
|
+
*/
|
|
731
|
+
getCacheStats() {
|
|
732
|
+
return this.metadataCache.getStats();
|
|
733
|
+
}
|
|
734
|
+
/**
|
|
735
|
+
* Invalidate metadata cache entries.
|
|
736
|
+
*
|
|
737
|
+
* @param key - Optional key to invalidate. If omitted, invalidates all entries.
|
|
738
|
+
*/
|
|
739
|
+
invalidateCache(key) {
|
|
740
|
+
this.metadataCache.invalidate(key);
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* Clear all cache entries and statistics.
|
|
744
|
+
*/
|
|
745
|
+
clearCache() {
|
|
746
|
+
this.metadataCache.clear();
|
|
747
|
+
}
|
|
151
748
|
};
|
|
152
749
|
function createObjectStackAdapter(config) {
|
|
153
750
|
return new ObjectStackAdapter(config);
|
|
154
751
|
}
|
|
155
752
|
export {
|
|
753
|
+
AuthenticationError,
|
|
754
|
+
BulkOperationError,
|
|
755
|
+
ConnectionError,
|
|
756
|
+
MetadataNotFoundError,
|
|
156
757
|
ObjectStackAdapter,
|
|
157
|
-
|
|
758
|
+
ObjectStackError,
|
|
759
|
+
ValidationError,
|
|
760
|
+
createErrorFromResponse,
|
|
761
|
+
createObjectStackAdapter,
|
|
762
|
+
isErrorType,
|
|
763
|
+
isObjectStackError
|
|
158
764
|
};
|