appwrite-utils-cli 1.7.9 → 1.8.2

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.
Files changed (70) hide show
  1. package/CHANGELOG.md +14 -199
  2. package/README.md +87 -30
  3. package/dist/adapters/AdapterFactory.js +5 -25
  4. package/dist/adapters/DatabaseAdapter.d.ts +17 -2
  5. package/dist/adapters/LegacyAdapter.d.ts +2 -1
  6. package/dist/adapters/LegacyAdapter.js +212 -16
  7. package/dist/adapters/TablesDBAdapter.d.ts +2 -12
  8. package/dist/adapters/TablesDBAdapter.js +261 -57
  9. package/dist/cli/commands/databaseCommands.js +4 -3
  10. package/dist/cli/commands/functionCommands.js +17 -8
  11. package/dist/collections/attributes.js +447 -125
  12. package/dist/collections/methods.js +197 -186
  13. package/dist/collections/tableOperations.d.ts +86 -0
  14. package/dist/collections/tableOperations.js +434 -0
  15. package/dist/collections/transferOperations.d.ts +3 -2
  16. package/dist/collections/transferOperations.js +93 -12
  17. package/dist/config/yamlConfig.d.ts +221 -88
  18. package/dist/examples/yamlTerminologyExample.d.ts +1 -1
  19. package/dist/examples/yamlTerminologyExample.js +6 -3
  20. package/dist/functions/fnConfigDiscovery.d.ts +3 -0
  21. package/dist/functions/fnConfigDiscovery.js +108 -0
  22. package/dist/interactiveCLI.js +18 -15
  23. package/dist/main.js +211 -73
  24. package/dist/migrations/appwriteToX.d.ts +88 -23
  25. package/dist/migrations/comprehensiveTransfer.d.ts +2 -0
  26. package/dist/migrations/comprehensiveTransfer.js +83 -6
  27. package/dist/migrations/dataLoader.d.ts +227 -69
  28. package/dist/migrations/dataLoader.js +3 -3
  29. package/dist/migrations/importController.js +3 -3
  30. package/dist/migrations/relationships.d.ts +8 -2
  31. package/dist/migrations/services/ImportOrchestrator.js +3 -3
  32. package/dist/migrations/transfer.js +159 -37
  33. package/dist/shared/attributeMapper.d.ts +20 -0
  34. package/dist/shared/attributeMapper.js +203 -0
  35. package/dist/shared/selectionDialogs.js +8 -4
  36. package/dist/storage/schemas.d.ts +354 -92
  37. package/dist/utils/configDiscovery.js +4 -3
  38. package/dist/utils/versionDetection.d.ts +0 -4
  39. package/dist/utils/versionDetection.js +41 -173
  40. package/dist/utils/yamlConverter.js +89 -16
  41. package/dist/utils/yamlLoader.d.ts +1 -1
  42. package/dist/utils/yamlLoader.js +6 -2
  43. package/dist/utilsController.js +56 -19
  44. package/package.json +4 -4
  45. package/src/adapters/AdapterFactory.ts +119 -143
  46. package/src/adapters/DatabaseAdapter.ts +18 -3
  47. package/src/adapters/LegacyAdapter.ts +236 -105
  48. package/src/adapters/TablesDBAdapter.ts +773 -643
  49. package/src/cli/commands/databaseCommands.ts +13 -12
  50. package/src/cli/commands/functionCommands.ts +23 -14
  51. package/src/collections/attributes.ts +2054 -1611
  52. package/src/collections/methods.ts +208 -293
  53. package/src/collections/tableOperations.ts +506 -0
  54. package/src/collections/transferOperations.ts +218 -144
  55. package/src/examples/yamlTerminologyExample.ts +10 -5
  56. package/src/functions/fnConfigDiscovery.ts +103 -0
  57. package/src/interactiveCLI.ts +25 -20
  58. package/src/main.ts +549 -194
  59. package/src/migrations/comprehensiveTransfer.ts +126 -50
  60. package/src/migrations/dataLoader.ts +3 -3
  61. package/src/migrations/importController.ts +3 -3
  62. package/src/migrations/services/ImportOrchestrator.ts +3 -3
  63. package/src/migrations/transfer.ts +148 -131
  64. package/src/shared/attributeMapper.ts +229 -0
  65. package/src/shared/selectionDialogs.ts +29 -25
  66. package/src/utils/configDiscovery.ts +9 -3
  67. package/src/utils/versionDetection.ts +74 -228
  68. package/src/utils/yamlConverter.ts +94 -17
  69. package/src/utils/yamlLoader.ts +11 -4
  70. package/src/utilsController.ts +80 -30
@@ -1,643 +1,773 @@
1
- /**
2
- * TablesDBAdapter - Native TablesDB API Implementation
3
- *
4
- * This adapter provides direct access to the new Appwrite TablesDB API
5
- * without any translation layer. It uses object notation parameters
6
- * and returns Models.Row instead of Models.Document.
7
- */
8
-
9
- import { Query } from "node-appwrite";
10
- import { chunk } from "es-toolkit";
11
- import {
12
- BaseAdapter,
13
- type DatabaseAdapter,
14
- type CreateRowParams,
15
- type UpdateRowParams,
16
- type ListRowsParams,
17
- type DeleteRowParams,
18
- type CreateTableParams,
19
- type UpdateTableParams,
20
- type ListTablesParams,
21
- type DeleteTableParams,
22
- type GetTableParams,
23
- type BulkCreateRowsParams,
24
- type BulkUpsertRowsParams,
25
- type BulkDeleteRowsParams,
26
- type CreateIndexParams,
27
- type ListIndexesParams,
28
- type DeleteIndexParams,
29
- type CreateAttributeParams,
30
- type UpdateAttributeParams,
31
- type DeleteAttributeParams,
32
- type ApiResponse,
33
- type AdapterMetadata,
34
- AdapterError
35
- } from './DatabaseAdapter.js';
36
-
37
- /**
38
- * TablesDBAdapter implementation for native TablesDB API
39
- */
40
- export class TablesDBAdapter extends BaseAdapter {
41
- private tablesDB: any;
42
-
43
- constructor(client: any) {
44
- super(client, 'tablesdb');
45
- // Assuming TablesDB service is available on the client
46
- this.tablesDB = client.tablesDB || client;
47
- }
48
-
49
- // Row (Document) Operations
50
- async listRows(params: ListRowsParams): Promise<ApiResponse> {
51
- try {
52
- const result = await this.tablesDB.listRows(params);
53
- return {
54
- data: result.rows,
55
- rows: result.rows,
56
- total: result.total
57
- };
58
- } catch (error) {
59
- throw new AdapterError(
60
- `Failed to list rows: ${error instanceof Error ? error.message : 'Unknown error'}`,
61
- 'LIST_ROWS_FAILED',
62
- error instanceof Error ? error : undefined
63
- );
64
- }
65
- }
66
-
67
- async createRow(params: CreateRowParams): Promise<ApiResponse> {
68
- try {
69
- // Remap 'id' to 'rowId' for TablesDB SDK compatibility
70
- const result = await this.tablesDB.createRow({
71
- databaseId: params.databaseId,
72
- tableId: params.tableId,
73
- rowId: params.id,
74
- data: params.data,
75
- permissions: params.permissions
76
- });
77
- return {
78
- data: result,
79
- rows: [result]
80
- };
81
- } catch (error) {
82
- throw new AdapterError(
83
- `Failed to create row: ${error instanceof Error ? error.message : 'Unknown error'}`,
84
- 'CREATE_ROW_FAILED',
85
- error instanceof Error ? error : undefined
86
- );
87
- }
88
- }
89
-
90
- async updateRow(params: UpdateRowParams): Promise<ApiResponse> {
91
- try {
92
- const result = await this.tablesDB.updateRow(
93
- params.databaseId,
94
- params.tableId,
95
- params.id,
96
- params.data,
97
- params.permissions || []
98
- );
99
- return {
100
- data: result,
101
- rows: [result]
102
- };
103
- } catch (error) {
104
- throw new AdapterError(
105
- `Failed to update row: ${error instanceof Error ? error.message : 'Unknown error'}`,
106
- 'UPDATE_ROW_FAILED',
107
- error instanceof Error ? error : undefined
108
- );
109
- }
110
- }
111
-
112
- async deleteRow(params: DeleteRowParams): Promise<ApiResponse> {
113
- try {
114
- const result = await this.tablesDB.deleteRow(
115
- params.databaseId,
116
- params.tableId,
117
- params.id
118
- );
119
- return { data: result };
120
- } catch (error) {
121
- throw new AdapterError(
122
- `Failed to delete row: ${error instanceof Error ? error.message : 'Unknown error'}`,
123
- 'DELETE_ROW_FAILED',
124
- error instanceof Error ? error : undefined
125
- );
126
- }
127
- }
128
-
129
- async getRow(params: { databaseId: string; tableId: string; id: string }): Promise<ApiResponse> {
130
- try {
131
- const result = await this.tablesDB.getRow(
132
- params.databaseId,
133
- params.tableId,
134
- params.id
135
- );
136
- return {
137
- data: result,
138
- rows: [result]
139
- };
140
- } catch (error) {
141
- throw new AdapterError(
142
- `Failed to get row: ${error instanceof Error ? error.message : 'Unknown error'}`,
143
- 'GET_ROW_FAILED',
144
- error instanceof Error ? error : undefined
145
- );
146
- }
147
- }
148
-
149
- // Table (Collection) Operations
150
- async listTables(params: ListTablesParams): Promise<ApiResponse> {
151
- try {
152
- const result = await this.tablesDB.listTables(params);
153
- return {
154
- data: result.tables,
155
- tables: result.tables,
156
- total: result.total
157
- };
158
- } catch (error) {
159
- throw new AdapterError(
160
- `Failed to list tables: ${error instanceof Error ? error.message : 'Unknown error'}`,
161
- 'LIST_TABLES_FAILED',
162
- error instanceof Error ? error : undefined
163
- );
164
- }
165
- }
166
-
167
- async createTable(params: CreateTableParams): Promise<ApiResponse> {
168
- try {
169
- const result = await this.tablesDB.createTable(
170
- params.databaseId,
171
- params.id, // tableId
172
- params.name,
173
- params.permissions || [],
174
- params.documentSecurity ?? false,
175
- params.enabled ?? true
176
- );
177
- return {
178
- data: result,
179
- tables: [result]
180
- };
181
- } catch (error) {
182
- throw new AdapterError(
183
- `Failed to create table: ${error instanceof Error ? error.message : 'Unknown error'}`,
184
- 'CREATE_TABLE_FAILED',
185
- error instanceof Error ? error : undefined
186
- );
187
- }
188
- }
189
-
190
- async updateTable(params: UpdateTableParams): Promise<ApiResponse> {
191
- try {
192
- const result = await this.tablesDB.updateTable(
193
- params.databaseId,
194
- params.id, // tableId
195
- params.name,
196
- params.permissions,
197
- params.documentSecurity,
198
- params.enabled
199
- );
200
- return {
201
- data: result,
202
- tables: [result]
203
- };
204
- } catch (error) {
205
- throw new AdapterError(
206
- `Failed to update table: ${error instanceof Error ? error.message : 'Unknown error'}`,
207
- 'UPDATE_TABLE_FAILED',
208
- error instanceof Error ? error : undefined
209
- );
210
- }
211
- }
212
-
213
- async deleteTable(params: DeleteTableParams): Promise<ApiResponse> {
214
- try {
215
- const result = await this.tablesDB.deleteTable(
216
- params.databaseId,
217
- params.tableId
218
- );
219
- return { data: result };
220
- } catch (error) {
221
- throw new AdapterError(
222
- `Failed to delete table: ${error instanceof Error ? error.message : 'Unknown error'}`,
223
- 'DELETE_TABLE_FAILED',
224
- error instanceof Error ? error : undefined
225
- );
226
- }
227
- }
228
-
229
- async getTable(params: GetTableParams): Promise<ApiResponse> {
230
- try {
231
- const result = await this.tablesDB.getTable(
232
- params.databaseId,
233
- params.tableId
234
- );
235
- return {
236
- data: result,
237
- tables: [result]
238
- };
239
- } catch (error) {
240
- throw new AdapterError(
241
- `Failed to get table: ${error instanceof Error ? error.message : 'Unknown error'}`,
242
- 'GET_TABLE_FAILED',
243
- error instanceof Error ? error : undefined
244
- );
245
- }
246
- }
247
-
248
- // Index Operations
249
- async listIndexes(params: ListIndexesParams): Promise<ApiResponse> {
250
- try {
251
- const result = await this.tablesDB.listIndexes(params);
252
- return {
253
- data: result.indexes,
254
- total: result.total
255
- };
256
- } catch (error) {
257
- throw new AdapterError(
258
- `Failed to list indexes: ${error instanceof Error ? error.message : 'Unknown error'}`,
259
- 'LIST_INDEXES_FAILED',
260
- error instanceof Error ? error : undefined
261
- );
262
- }
263
- }
264
-
265
- async createIndex(params: CreateIndexParams): Promise<ApiResponse> {
266
- try {
267
- const result = await this.tablesDB.createIndex(
268
- params.databaseId,
269
- params.tableId,
270
- params.key,
271
- params.type,
272
- params.attributes,
273
- params.orders || []
274
- );
275
- return { data: result };
276
- } catch (error) {
277
- throw new AdapterError(
278
- `Failed to create index: ${error instanceof Error ? error.message : 'Unknown error'}`,
279
- 'CREATE_INDEX_FAILED',
280
- error instanceof Error ? error : undefined
281
- );
282
- }
283
- }
284
-
285
- async deleteIndex(params: DeleteIndexParams): Promise<ApiResponse> {
286
- try {
287
- const result = await this.tablesDB.deleteIndex(
288
- params.databaseId,
289
- params.tableId,
290
- params.key
291
- );
292
- return { data: result };
293
- } catch (error) {
294
- throw new AdapterError(
295
- `Failed to delete index: ${error instanceof Error ? error.message : 'Unknown error'}`,
296
- 'DELETE_INDEX_FAILED',
297
- error instanceof Error ? error : undefined
298
- );
299
- }
300
- }
301
-
302
- // Attribute Operations
303
- async createAttribute(params: CreateAttributeParams): Promise<ApiResponse> {
304
- try {
305
- // TablesDB uses type-specific attribute methods like the legacy SDK
306
- let result;
307
-
308
- switch (params.type.toLowerCase()) {
309
- case 'string':
310
- result = await this.tablesDB.createStringAttribute(
311
- params.databaseId,
312
- params.tableId,
313
- params.key,
314
- params.size || 255,
315
- params.required ?? false,
316
- params.default,
317
- params.array ?? false,
318
- params.encrypt ?? false
319
- );
320
- break;
321
-
322
- case 'integer':
323
- result = await this.tablesDB.createIntegerAttribute(
324
- params.databaseId,
325
- params.tableId,
326
- params.key,
327
- params.required ?? false,
328
- params.min,
329
- params.max,
330
- params.default,
331
- params.array ?? false
332
- );
333
- break;
334
-
335
- case 'float':
336
- case 'double':
337
- result = await this.tablesDB.createFloatAttribute(
338
- params.databaseId,
339
- params.tableId,
340
- params.key,
341
- params.required ?? false,
342
- params.min,
343
- params.max,
344
- params.default,
345
- params.array ?? false
346
- );
347
- break;
348
-
349
- case 'boolean':
350
- result = await this.tablesDB.createBooleanAttribute(
351
- params.databaseId,
352
- params.tableId,
353
- params.key,
354
- params.required ?? false,
355
- params.default,
356
- params.array ?? false
357
- );
358
- break;
359
-
360
- case 'datetime':
361
- result = await this.tablesDB.createDatetimeAttribute(
362
- params.databaseId,
363
- params.tableId,
364
- params.key,
365
- params.required ?? false,
366
- params.default,
367
- params.array ?? false
368
- );
369
- break;
370
-
371
- case 'email':
372
- result = await this.tablesDB.createEmailAttribute(
373
- params.databaseId,
374
- params.tableId,
375
- params.key,
376
- params.required ?? false,
377
- params.default,
378
- params.array ?? false
379
- );
380
- break;
381
-
382
- case 'enum':
383
- result = await this.tablesDB.createEnumAttribute(
384
- params.databaseId,
385
- params.tableId,
386
- params.key,
387
- params.elements || [],
388
- params.required ?? false,
389
- params.default,
390
- params.array ?? false
391
- );
392
- break;
393
-
394
- case 'ip':
395
- result = await this.tablesDB.createIpAttribute(
396
- params.databaseId,
397
- params.tableId,
398
- params.key,
399
- params.required ?? false,
400
- params.default,
401
- params.array ?? false
402
- );
403
- break;
404
-
405
- case 'url':
406
- result = await this.tablesDB.createUrlAttribute(
407
- params.databaseId,
408
- params.tableId,
409
- params.key,
410
- params.required ?? false,
411
- params.default,
412
- params.array ?? false
413
- );
414
- break;
415
-
416
- case 'relationship':
417
- result = await this.tablesDB.createRelationshipAttribute(
418
- params.databaseId,
419
- params.tableId,
420
- params.key,
421
- params.relatedCollection || '',
422
- params.type || 'oneToOne',
423
- params.twoWay ?? false,
424
- params.onDelete || 'restrict'
425
- );
426
- break;
427
-
428
- default:
429
- throw new AdapterError(
430
- `Unsupported attribute type: ${params.type}`,
431
- 'UNSUPPORTED_ATTRIBUTE_TYPE'
432
- );
433
- }
434
-
435
- return { data: result };
436
- } catch (error) {
437
- throw new AdapterError(
438
- `Failed to create attribute: ${error instanceof Error ? error.message : 'Unknown error'}`,
439
- 'CREATE_ATTRIBUTE_FAILED',
440
- error instanceof Error ? error : undefined
441
- );
442
- }
443
- }
444
-
445
- async updateAttribute(params: UpdateAttributeParams): Promise<ApiResponse> {
446
- try {
447
- // TablesDB uses type-specific update methods or generic updateAttribute with positional params
448
- // Try type-specific first, fallback to generic
449
- const result = await this.tablesDB.updateStringAttribute(
450
- params.databaseId,
451
- params.tableId,
452
- params.key,
453
- params.required ?? false,
454
- params.default
455
- );
456
- return { data: result };
457
- } catch (error) {
458
- throw new AdapterError(
459
- `Failed to update attribute: ${error instanceof Error ? error.message : 'Unknown error'}`,
460
- 'UPDATE_ATTRIBUTE_FAILED',
461
- error instanceof Error ? error : undefined
462
- );
463
- }
464
- }
465
-
466
- async deleteAttribute(params: DeleteAttributeParams): Promise<ApiResponse> {
467
- try {
468
- const result = await this.tablesDB.deleteAttribute(
469
- params.databaseId,
470
- params.tableId,
471
- params.key
472
- );
473
- return { data: result };
474
- } catch (error) {
475
- throw new AdapterError(
476
- `Failed to delete attribute: ${error instanceof Error ? error.message : 'Unknown error'}`,
477
- 'DELETE_ATTRIBUTE_FAILED',
478
- error instanceof Error ? error : undefined
479
- );
480
- }
481
- }
482
-
483
- // Bulk Operations (Native TablesDB Support)
484
- async bulkCreateRows(params: BulkCreateRowsParams): Promise<ApiResponse> {
485
- try {
486
- const result = await this.tablesDB.bulkCreateRows(params);
487
- return {
488
- data: result.rows,
489
- rows: result.rows,
490
- total: result.rows?.length || 0
491
- };
492
- } catch (error) {
493
- throw new AdapterError(
494
- `Failed to bulk create rows: ${error instanceof Error ? error.message : 'Unknown error'}`,
495
- 'BULK_CREATE_ROWS_FAILED',
496
- error instanceof Error ? error : undefined
497
- );
498
- }
499
- }
500
-
501
- async bulkUpsertRows(params: BulkUpsertRowsParams): Promise<ApiResponse> {
502
- try {
503
- const result = await this.tablesDB.bulkUpsertRows(params);
504
- return {
505
- data: result.rows,
506
- rows: result.rows,
507
- total: result.rows?.length || 0
508
- };
509
- } catch (error) {
510
- throw new AdapterError(
511
- `Failed to bulk upsert rows: ${error instanceof Error ? error.message : 'Unknown error'}`,
512
- 'BULK_UPSERT_ROWS_FAILED',
513
- error instanceof Error ? error : undefined
514
- );
515
- }
516
- }
517
-
518
- async bulkDeleteRows(params: BulkDeleteRowsParams): Promise<ApiResponse> {
519
- try {
520
- let queries: string[];
521
-
522
- // Wipe mode: use Query.limit for deleting without fetching
523
- if (params.rowIds.length === 0) {
524
- const batchSize = params.batchSize || 250;
525
- queries = [Query.limit(batchSize)];
526
- }
527
- // Specific IDs mode: chunk into batches of 80-90 to stay within Appwrite limits
528
- // (max 100 IDs per Query.equal, and queries must be < 4096 chars total)
529
- else {
530
- const ID_BATCH_SIZE = 85; // Safe batch size for Query.equal
531
- const idBatches = chunk(params.rowIds, ID_BATCH_SIZE);
532
- queries = idBatches.map(batch => Query.equal('$id', batch));
533
- }
534
-
535
- const result = await this.tablesDB.deleteRows({
536
- databaseId: params.databaseId,
537
- tableId: params.tableId,
538
- queries: queries
539
- });
540
-
541
- return {
542
- data: result,
543
- total: params.rowIds.length || (result as any).total || 0
544
- };
545
- } catch (error) {
546
- throw new AdapterError(
547
- `Failed to bulk delete rows: ${error instanceof Error ? error.message : 'Unknown error'}`,
548
- 'BULK_DELETE_ROWS_FAILED',
549
- error instanceof Error ? error : undefined
550
- );
551
- }
552
- }
553
-
554
- // Metadata and Capabilities
555
- getMetadata(): AdapterMetadata {
556
- return {
557
- apiMode: 'tablesdb',
558
- terminology: {
559
- container: 'table',
560
- item: 'row',
561
- service: 'TablesDB'
562
- },
563
- capabilities: {
564
- bulkOperations: true,
565
- advancedQueries: true,
566
- realtime: true,
567
- transactions: true // TablesDB may support transactions
568
- }
569
- };
570
- }
571
-
572
- supportsBulkOperations(): boolean {
573
- return true; // TablesDB natively supports bulk operations
574
- }
575
-
576
- // Advanced TablesDB Features
577
-
578
- /**
579
- * Execute a transaction (if supported by TablesDB)
580
- */
581
- async executeTransaction(operations: Array<() => Promise<any>>): Promise<ApiResponse> {
582
- if (!this.tablesDB.transaction) {
583
- throw new AdapterError(
584
- 'Transactions are not supported in this TablesDB version',
585
- 'TRANSACTIONS_NOT_SUPPORTED'
586
- );
587
- }
588
-
589
- try {
590
- const result = await this.tablesDB.transaction(operations);
591
- return { data: result };
592
- } catch (error) {
593
- throw new AdapterError(
594
- `Transaction failed: ${error instanceof Error ? error.message : 'Unknown error'}`,
595
- 'TRANSACTION_FAILED',
596
- error instanceof Error ? error : undefined
597
- );
598
- }
599
- }
600
-
601
- /**
602
- * Subscribe to real-time updates (if supported)
603
- */
604
- subscribeToTable(params: { databaseId: string; tableId: string }, callback: (data: any) => void): () => void {
605
- if (!this.tablesDB.subscribe) {
606
- throw new AdapterError(
607
- 'Real-time subscriptions are not supported',
608
- 'REALTIME_NOT_SUPPORTED'
609
- );
610
- }
611
-
612
- try {
613
- return this.tablesDB.subscribe(`databases.${params.databaseId}.tables.${params.tableId}.rows`, callback);
614
- } catch (error) {
615
- throw new AdapterError(
616
- `Failed to subscribe to table: ${error instanceof Error ? error.message : 'Unknown error'}`,
617
- 'SUBSCRIPTION_FAILED',
618
- error instanceof Error ? error : undefined
619
- );
620
- }
621
- }
622
-
623
- /**
624
- * Get table statistics (if available in TablesDB)
625
- */
626
- async getTableStats(params: GetTableParams): Promise<ApiResponse> {
627
- try {
628
- if (!this.tablesDB.getTableStats) {
629
- // Fallback to basic table info
630
- return this.getTable(params);
631
- }
632
-
633
- const result = await this.tablesDB.getTableStats(params);
634
- return { data: result };
635
- } catch (error) {
636
- throw new AdapterError(
637
- `Failed to get table stats: ${error instanceof Error ? error.message : 'Unknown error'}`,
638
- 'GET_TABLE_STATS_FAILED',
639
- error instanceof Error ? error : undefined
640
- );
641
- }
642
- }
643
- }
1
+ /**
2
+ * TablesDBAdapter - Native TablesDB API Implementation
3
+ *
4
+ * This adapter provides direct access to the new Appwrite TablesDB API
5
+ * without any translation layer. It uses object notation parameters
6
+ * and returns Models.Row instead of Models.Document.
7
+ */
8
+
9
+ import { IndexType, Query, RelationMutate, RelationshipType, type Models } from "node-appwrite";
10
+ import { chunk } from "es-toolkit";
11
+ import {
12
+ BaseAdapter,
13
+ type DatabaseAdapter,
14
+ type CreateRowParams,
15
+ type UpdateRowParams,
16
+ type ListRowsParams,
17
+ type DeleteRowParams,
18
+ type CreateTableParams,
19
+ type UpdateTableParams,
20
+ type ListTablesParams,
21
+ type DeleteTableParams,
22
+ type GetTableParams,
23
+ type BulkCreateRowsParams,
24
+ type BulkUpsertRowsParams,
25
+ type BulkDeleteRowsParams,
26
+ type CreateIndexParams,
27
+ type ListIndexesParams,
28
+ type DeleteIndexParams,
29
+ type CreateAttributeParams,
30
+ type UpdateAttributeParams,
31
+ type DeleteAttributeParams,
32
+ type ApiResponse,
33
+ type AdapterMetadata,
34
+ AdapterError
35
+ } from './DatabaseAdapter.js';
36
+ import { TablesDB, Client } from "node-appwrite";
37
+
38
+ /**
39
+ * TablesDBAdapter implementation for native TablesDB API
40
+ */
41
+ export class TablesDBAdapter extends BaseAdapter {
42
+ private tablesDB: TablesDB;
43
+
44
+ constructor(client: Client) {
45
+ super(client, 'tablesdb');
46
+ // Assuming TablesDB service is available on the client
47
+ this.tablesDB = new TablesDB(client);
48
+ }
49
+
50
+ // Row (Document) Operations
51
+ async listRows(params: ListRowsParams): Promise<ApiResponse> {
52
+ try {
53
+ const result = await this.tablesDB.listRows({
54
+ tableId: params.tableId,
55
+ databaseId: params.databaseId,
56
+ queries: params.queries || [],
57
+ });
58
+ return {
59
+ data: result.rows,
60
+ rows: result.rows,
61
+ total: result.total
62
+ };
63
+ } catch (error) {
64
+ throw new AdapterError(
65
+ `Failed to list rows: ${error instanceof Error ? error.message : 'Unknown error'}`,
66
+ 'LIST_ROWS_FAILED',
67
+ error instanceof Error ? error : undefined
68
+ );
69
+ }
70
+ }
71
+
72
+ async createRow(params: CreateRowParams): Promise<ApiResponse> {
73
+ try {
74
+ // Remap 'id' to 'rowId' for TablesDB SDK compatibility
75
+ const result = await this.tablesDB.createRow({
76
+ databaseId: params.databaseId,
77
+ tableId: params.tableId,
78
+ rowId: params.id,
79
+ data: params.data,
80
+ permissions: params.permissions
81
+ });
82
+ return {
83
+ data: result,
84
+ rows: [result]
85
+ };
86
+ } catch (error) {
87
+ throw new AdapterError(
88
+ `Failed to create row: ${error instanceof Error ? error.message : 'Unknown error'}`,
89
+ 'CREATE_ROW_FAILED',
90
+ error instanceof Error ? error : undefined
91
+ );
92
+ }
93
+ }
94
+
95
+ async updateRow(params: UpdateRowParams): Promise<ApiResponse> {
96
+ try {
97
+ const result = await this.tablesDB.updateRow(
98
+ params.databaseId,
99
+ params.tableId,
100
+ params.id,
101
+ params.data,
102
+ params.permissions || []
103
+ );
104
+ return {
105
+ data: result,
106
+ rows: [result]
107
+ };
108
+ } catch (error) {
109
+ throw new AdapterError(
110
+ `Failed to update row: ${error instanceof Error ? error.message : 'Unknown error'}`,
111
+ 'UPDATE_ROW_FAILED',
112
+ error instanceof Error ? error : undefined
113
+ );
114
+ }
115
+ }
116
+
117
+ async deleteRow(params: DeleteRowParams): Promise<ApiResponse> {
118
+ try {
119
+ const result = await this.tablesDB.deleteRow(
120
+ params.databaseId,
121
+ params.tableId,
122
+ params.id
123
+ );
124
+ return { data: result };
125
+ } catch (error) {
126
+ throw new AdapterError(
127
+ `Failed to delete row: ${error instanceof Error ? error.message : 'Unknown error'}`,
128
+ 'DELETE_ROW_FAILED',
129
+ error instanceof Error ? error : undefined
130
+ );
131
+ }
132
+ }
133
+
134
+ async getRow(params: { databaseId: string; tableId: string; id: string }): Promise<ApiResponse> {
135
+ try {
136
+ const result = await this.tablesDB.getRow(
137
+ params.databaseId,
138
+ params.tableId,
139
+ params.id
140
+ );
141
+ return {
142
+ data: result,
143
+ rows: [result]
144
+ };
145
+ } catch (error) {
146
+ throw new AdapterError(
147
+ `Failed to get row: ${error instanceof Error ? error.message : 'Unknown error'}`,
148
+ 'GET_ROW_FAILED',
149
+ error instanceof Error ? error : undefined
150
+ );
151
+ }
152
+ }
153
+
154
+ // Table (Collection) Operations
155
+ async listTables(params: ListTablesParams): Promise<ApiResponse> {
156
+ try {
157
+ const result = await this.tablesDB.listTables(params);
158
+ return {
159
+ data: result.tables,
160
+ tables: result.tables,
161
+ total: result.total
162
+ };
163
+ } catch (error) {
164
+ throw new AdapterError(
165
+ `Failed to list tables: ${error instanceof Error ? error.message : 'Unknown error'}`,
166
+ 'LIST_TABLES_FAILED',
167
+ error instanceof Error ? error : undefined
168
+ );
169
+ }
170
+ }
171
+
172
+ async createTable(params: CreateTableParams): Promise<ApiResponse> {
173
+ try {
174
+ const rowSecurity = params.rowSecurity ?? params.documentSecurity ?? false;
175
+ const result = await this.tablesDB.createTable(
176
+ params.databaseId,
177
+ params.id, // tableId
178
+ params.name,
179
+ params.permissions || [],
180
+ rowSecurity,
181
+ params.enabled ?? true
182
+ );
183
+ return {
184
+ data: result,
185
+ tables: [result]
186
+ };
187
+ } catch (error) {
188
+ throw new AdapterError(
189
+ `Failed to create table: ${error instanceof Error ? error.message : 'Unknown error'}`,
190
+ 'CREATE_TABLE_FAILED',
191
+ error instanceof Error ? error : undefined
192
+ );
193
+ }
194
+ }
195
+
196
+ async updateTable(params: UpdateTableParams): Promise<ApiResponse> {
197
+ try {
198
+ const rowSecurity = params.rowSecurity ?? params.documentSecurity;
199
+ const result = await this.tablesDB.updateTable(
200
+ params.databaseId,
201
+ params.id, // tableId
202
+ params.name,
203
+ params.permissions,
204
+ rowSecurity,
205
+ params.enabled
206
+ );
207
+ return {
208
+ data: result,
209
+ tables: [result]
210
+ };
211
+ } catch (error) {
212
+ throw new AdapterError(
213
+ `Failed to update table: ${error instanceof Error ? error.message : 'Unknown error'}`,
214
+ 'UPDATE_TABLE_FAILED',
215
+ error instanceof Error ? error : undefined
216
+ );
217
+ }
218
+ }
219
+
220
+ async deleteTable(params: DeleteTableParams): Promise<ApiResponse> {
221
+ try {
222
+ const result = await this.tablesDB.deleteTable(
223
+ params.databaseId,
224
+ params.tableId
225
+ );
226
+ return { data: result };
227
+ } catch (error) {
228
+ throw new AdapterError(
229
+ `Failed to delete table: ${error instanceof Error ? error.message : 'Unknown error'}`,
230
+ 'DELETE_TABLE_FAILED',
231
+ error instanceof Error ? error : undefined
232
+ );
233
+ }
234
+ }
235
+
236
+ async getTable(params: GetTableParams): Promise<ApiResponse> {
237
+ try {
238
+ const result = await this.tablesDB.getTable(
239
+ params.databaseId,
240
+ params.tableId
241
+ );
242
+ return {
243
+ data: result,
244
+ tables: [result]
245
+ };
246
+ } catch (error) {
247
+ throw new AdapterError(
248
+ `Failed to get table: ${error instanceof Error ? error.message : 'Unknown error'}`,
249
+ 'GET_TABLE_FAILED',
250
+ error instanceof Error ? error : undefined
251
+ );
252
+ }
253
+ }
254
+
255
+ // Index Operations
256
+ async listIndexes(params: ListIndexesParams): Promise<ApiResponse> {
257
+ try {
258
+ const result = await this.tablesDB.listIndexes(params);
259
+ return {
260
+ data: result.indexes,
261
+ total: result.total
262
+ };
263
+ } catch (error) {
264
+ throw new AdapterError(
265
+ `Failed to list indexes: ${error instanceof Error ? error.message : 'Unknown error'}`,
266
+ 'LIST_INDEXES_FAILED',
267
+ error instanceof Error ? error : undefined
268
+ );
269
+ }
270
+ }
271
+
272
+ async createIndex(params: CreateIndexParams): Promise<ApiResponse> {
273
+ try {
274
+ const result = await this.tablesDB.createIndex(
275
+ params.databaseId,
276
+ params.tableId,
277
+ params.key,
278
+ params.type as IndexType,
279
+ params.attributes,
280
+ params.orders || []
281
+ );
282
+ return { data: result };
283
+ } catch (error) {
284
+ throw new AdapterError(
285
+ `Failed to create index: ${error instanceof Error ? error.message : 'Unknown error'}`,
286
+ 'CREATE_INDEX_FAILED',
287
+ error instanceof Error ? error : undefined
288
+ );
289
+ }
290
+ }
291
+
292
+ async deleteIndex(params: DeleteIndexParams): Promise<ApiResponse> {
293
+ try {
294
+ const result = await this.tablesDB.deleteIndex(
295
+ params.databaseId,
296
+ params.tableId,
297
+ params.key
298
+ );
299
+ return { data: result };
300
+ } catch (error) {
301
+ throw new AdapterError(
302
+ `Failed to delete index: ${error instanceof Error ? error.message : 'Unknown error'}`,
303
+ 'DELETE_INDEX_FAILED',
304
+ error instanceof Error ? error : undefined
305
+ );
306
+ }
307
+ }
308
+
309
+ // Attribute Operations
310
+ async createAttribute(params: CreateAttributeParams): Promise<ApiResponse> {
311
+ try {
312
+ // TablesDB exposes type-specific column methods
313
+ // TablesDB uses type-specific column methods
314
+ let result;
315
+ const type = (params.type || "").toLowerCase();
316
+ const required = params.required ?? false;
317
+ const array = params.array ?? false;
318
+ const encrypt = params.encrypt ?? (params as any).encrypted ?? false;
319
+ const normalizedDefault =
320
+ params.default === null || params.default === undefined
321
+ ? undefined
322
+ : params.default;
323
+ const numberDefault =
324
+ typeof normalizedDefault === "number" ? normalizedDefault : undefined;
325
+ const stringDefault =
326
+ typeof normalizedDefault === "string" ? normalizedDefault : undefined;
327
+ const booleanDefault =
328
+ typeof normalizedDefault === "boolean" ? normalizedDefault : undefined;
329
+
330
+ switch (type) {
331
+ case 'string':
332
+ result = await this.tablesDB.createStringColumn({
333
+ databaseId: params.databaseId,
334
+ tableId: params.tableId,
335
+ key: params.key,
336
+ size: params.size || 255,
337
+ required: params.required ?? false,
338
+ xdefault: params.default,
339
+ array: params.array ?? false,
340
+ encrypt: params.encrypt ?? false
341
+ });
342
+ break;
343
+
344
+ case 'integer':
345
+ result = await this.tablesDB.createIntegerColumn({
346
+ databaseId: params.databaseId,
347
+ tableId: params.tableId,
348
+ key: params.key,
349
+ required: params.required ?? false,
350
+ min: params.min,
351
+ max: params.max,
352
+ xdefault: params.default,
353
+ array: params.array ?? false
354
+ });
355
+ break;
356
+
357
+ case 'float':
358
+ case 'double':
359
+ result = await this.tablesDB.createFloatColumn({
360
+ databaseId: params.databaseId,
361
+ tableId: params.tableId,
362
+ key: params.key,
363
+ required: params.required ?? false,
364
+ min: params.min,
365
+ max: params.max,
366
+ xdefault: params.default,
367
+ array: params.array ?? false
368
+ });
369
+ break;
370
+
371
+ case 'boolean':
372
+ result = await this.tablesDB.createBooleanColumn({
373
+ databaseId: params.databaseId,
374
+ tableId: params.tableId,
375
+ key: params.key,
376
+ required: params.required ?? false,
377
+ xdefault: params.default,
378
+ array: params.array ?? false
379
+ });
380
+ break;
381
+
382
+ case 'datetime':
383
+ result = await this.tablesDB.createDatetimeColumn({
384
+ databaseId: params.databaseId,
385
+ tableId: params.tableId,
386
+ key: params.key,
387
+ required: params.required ?? false,
388
+ xdefault: params.default,
389
+ array: params.array ?? false
390
+ });
391
+ break;
392
+
393
+ case 'email':
394
+ result = await this.tablesDB.createEmailColumn({
395
+ databaseId: params.databaseId,
396
+ tableId: params.tableId,
397
+ key: params.key,
398
+ required: params.required ?? false,
399
+ xdefault: params.default,
400
+ array: params.array ?? false
401
+ });
402
+ break;
403
+
404
+ case 'enum':
405
+ // Defensive: require non-empty elements
406
+ if (!Array.isArray((params as any).elements) || (params as any).elements.length === 0) {
407
+ throw new AdapterError(
408
+ `Enum '${params.key}' requires a non-empty 'elements' array`,
409
+ 'CREATE_ENUM_ELEMENTS_REQUIRED'
410
+ );
411
+ }
412
+ result = await this.tablesDB.createEnumColumn({
413
+ databaseId: params.databaseId,
414
+ tableId: params.tableId,
415
+ key: params.key,
416
+ elements: params.elements!,
417
+ required: params.required ?? false,
418
+ xdefault: params.default,
419
+ array: params.array ?? false
420
+ });
421
+ break;
422
+
423
+ case 'ip':
424
+ result = await this.tablesDB.createIpColumn({
425
+ databaseId: params.databaseId,
426
+ tableId: params.tableId,
427
+ key: params.key,
428
+ required: params.required ?? false,
429
+ xdefault: params.default,
430
+ array: params.array ?? false
431
+ });
432
+ break;
433
+
434
+ case 'url':
435
+ result = await this.tablesDB.createUrlColumn({
436
+ databaseId: params.databaseId,
437
+ tableId: params.tableId,
438
+ key: params.key,
439
+ required: params.required ?? false,
440
+ xdefault: params.default,
441
+ array: params.array ?? false
442
+ });
443
+ break;
444
+
445
+ case 'relationship':
446
+ result = await this.tablesDB.createRelationshipColumn({
447
+ databaseId: params.databaseId,
448
+ tableId: params.tableId,
449
+ key: params.key,
450
+ relatedTableId: params.relatedCollection || '',
451
+ type: (params.type || 'oneToOne') as RelationshipType,
452
+ twoWay: params.twoWay ?? false,
453
+ twoWayKey: params.twoWayKey,
454
+ onDelete: params.onDelete || 'restrict'
455
+ });
456
+ break;
457
+
458
+ default:
459
+ throw new AdapterError(
460
+ `Unsupported attribute type: ${params.type}`,
461
+ 'UNSUPPORTED_ATTRIBUTE_TYPE'
462
+ );
463
+ }
464
+
465
+ return { data: result };
466
+ } catch (error) {
467
+ throw new AdapterError(
468
+ `Failed to create attribute: ${error instanceof Error ? error.message : 'Unknown error'}`,
469
+ 'CREATE_ATTRIBUTE_FAILED',
470
+ error instanceof Error ? error : undefined
471
+ );
472
+ }
473
+ }
474
+
475
+ async updateAttribute(params: UpdateAttributeParams): Promise<ApiResponse> {
476
+ try {
477
+ // TablesDB uses type-specific update methods with object notation
478
+ // We need to detect the existing column type to use the correct update method
479
+ // For now, we'll need to get the column info first, then use the appropriate update method
480
+
481
+ // Get the current table schema to determine the column type
482
+ const tableInfo = await this.tablesDB.getTable(params.databaseId, params.tableId);
483
+
484
+ // Find the column to determine its type
485
+ const column = tableInfo.columns?.find(col => col.key === params.key);
486
+ if (!column) {
487
+ throw new AdapterError(
488
+ `Column '${params.key}' not found in table`,
489
+ 'COLUMN_NOT_FOUND'
490
+ );
491
+ }
492
+
493
+ let result;
494
+
495
+ // Use the appropriate updateXColumn method based on the column type
496
+ // Cast column to proper Models type to access its specific properties
497
+ const columnType = (column as any).type;
498
+
499
+ switch (columnType) {
500
+ case 'string':
501
+ const stringColumn = column as Models.ColumnString;
502
+ result = await this.tablesDB.updateStringColumn({
503
+ databaseId: params.databaseId,
504
+ tableId: params.tableId,
505
+ key: params.key,
506
+ required: params.required !== undefined ? params.required : stringColumn.required,
507
+ xdefault: params.default !== undefined ? params.default : stringColumn.default,
508
+ size: params.size !== undefined ? params.size : stringColumn.size,
509
+ });
510
+ break;
511
+
512
+ case 'integer':
513
+ const integerColumn = column as Models.ColumnInteger;
514
+ result = await this.tablesDB.updateIntegerColumn({
515
+ databaseId: params.databaseId,
516
+ tableId: params.tableId,
517
+ key: params.key,
518
+ required: params.required !== undefined ? params.required : integerColumn.required,
519
+ xdefault: params.default !== undefined ? params.default : integerColumn.default,
520
+ // Only send min/max when explicitly provided to avoid resubmitting extreme values
521
+ ...(params.min !== undefined ? { min: params.min } : {}),
522
+ ...(params.max !== undefined ? { max: params.max } : {}),
523
+ });
524
+ break;
525
+
526
+ case 'float':
527
+ case 'double':
528
+ const floatColumn = column as Models.ColumnFloat;
529
+ result = await this.tablesDB.updateFloatColumn({
530
+ databaseId: params.databaseId,
531
+ tableId: params.tableId,
532
+ key: params.key,
533
+ required: params.required !== undefined ? params.required : floatColumn.required,
534
+ xdefault: params.default !== undefined ? params.default : floatColumn.default,
535
+ ...(params.min !== undefined ? { min: params.min } : {}),
536
+ ...(params.max !== undefined ? { max: params.max } : {}),
537
+ });
538
+ break;
539
+
540
+ case 'boolean':
541
+ const booleanColumn = column as Models.ColumnBoolean;
542
+ result = await this.tablesDB.updateBooleanColumn({
543
+ databaseId: params.databaseId,
544
+ tableId: params.tableId,
545
+ key: params.key,
546
+ required: params.required !== undefined ? params.required : booleanColumn.required,
547
+ xdefault: params.default !== undefined ? params.default : booleanColumn.default
548
+ });
549
+ break;
550
+
551
+ case 'datetime':
552
+ const datetimeColumn = column as Models.ColumnDatetime;
553
+ result = await this.tablesDB.updateDatetimeColumn({
554
+ databaseId: params.databaseId,
555
+ tableId: params.tableId,
556
+ key: params.key,
557
+ required: params.required !== undefined ? params.required : datetimeColumn.required,
558
+ xdefault: params.default !== undefined ? params.default : datetimeColumn.default
559
+ });
560
+ break;
561
+
562
+ case 'email':
563
+ const emailColumn = column as Models.ColumnEmail;
564
+ result = await this.tablesDB.updateEmailColumn({
565
+ databaseId: params.databaseId,
566
+ tableId: params.tableId,
567
+ key: params.key,
568
+ required: params.required !== undefined ? params.required : emailColumn.required,
569
+ xdefault: params.default !== undefined ? params.default : emailColumn.default
570
+ });
571
+ break;
572
+
573
+ case 'enum':
574
+ const enumColumn = column as Models.ColumnEnum;
575
+ // Choose elements to send only when provided, otherwise preserve existing
576
+ const provided = (params as any).elements;
577
+ const existing = (enumColumn as any)?.elements;
578
+ const nextElements = (Array.isArray(provided) && provided.length > 0) ? provided : existing;
579
+ result = await this.tablesDB.updateEnumColumn({
580
+ databaseId: params.databaseId,
581
+ tableId: params.tableId,
582
+ key: params.key,
583
+ required: params.required !== undefined ? params.required : enumColumn.required,
584
+ xdefault: params.default !== undefined ? params.default : enumColumn.default,
585
+ elements: nextElements
586
+ });
587
+ break;
588
+
589
+ case 'ip':
590
+ const ipColumn = column as Models.ColumnIp;
591
+ result = await this.tablesDB.updateIpColumn({
592
+ databaseId: params.databaseId,
593
+ tableId: params.tableId,
594
+ key: params.key,
595
+ required: params.required !== undefined ? params.required : ipColumn.required,
596
+ xdefault: params.default !== undefined ? params.default : ipColumn.default
597
+ });
598
+ break;
599
+
600
+ case 'url':
601
+ const urlColumn = column as Models.ColumnUrl;
602
+ result = await this.tablesDB.updateUrlColumn({
603
+ databaseId: params.databaseId,
604
+ tableId: params.tableId,
605
+ key: params.key,
606
+ required: params.required !== undefined ? params.required : urlColumn.required,
607
+ xdefault: params.default !== undefined ? params.default : urlColumn.default
608
+ });
609
+ break;
610
+
611
+ case 'relationship':
612
+ const relationshipColumn = column as Models.ColumnRelationship;
613
+ result = await this.tablesDB.updateRelationshipColumn({
614
+ databaseId: params.databaseId,
615
+ tableId: params.tableId,
616
+ key: params.key,
617
+ onDelete: (params.onDelete !== undefined ? params.onDelete : relationshipColumn.onDelete) as RelationMutate,
618
+ });
619
+ break;
620
+
621
+ default:
622
+ throw new AdapterError(
623
+ `Unsupported column type for update: ${columnType}`,
624
+ 'UNSUPPORTED_COLUMN_TYPE'
625
+ );
626
+ }
627
+
628
+ return { data: result };
629
+ } catch (error) {
630
+ throw new AdapterError(
631
+ `Failed to update attribute: ${error instanceof Error ? error.message : 'Unknown error'}`,
632
+ 'UPDATE_ATTRIBUTE_FAILED',
633
+ error instanceof Error ? error : undefined
634
+ );
635
+ }
636
+ }
637
+
638
+ async deleteAttribute(params: DeleteAttributeParams): Promise<ApiResponse> {
639
+ try {
640
+ const result = await this.tablesDB.deleteColumn({
641
+ databaseId: params.databaseId,
642
+ tableId: params.tableId,
643
+ key: params.key
644
+ });
645
+ return { data: result };
646
+ } catch (error) {
647
+ throw new AdapterError(
648
+ `Failed to delete attribute: ${error instanceof Error ? error.message : 'Unknown error'}`,
649
+ 'DELETE_ATTRIBUTE_FAILED',
650
+ error instanceof Error ? error : undefined
651
+ );
652
+ }
653
+ }
654
+
655
+ // Bulk Operations (Native TablesDB Support)
656
+ async bulkCreateRows(params: BulkCreateRowsParams): Promise<ApiResponse> {
657
+ try {
658
+ const result = await this.tablesDB.createRows(params);
659
+ return {
660
+ data: result.rows,
661
+ rows: result.rows,
662
+ total: result.rows?.length || 0
663
+ };
664
+ } catch (error) {
665
+ throw new AdapterError(
666
+ `Failed to bulk create rows: ${error instanceof Error ? error.message : 'Unknown error'}`,
667
+ 'BULK_CREATE_ROWS_FAILED',
668
+ error instanceof Error ? error : undefined
669
+ );
670
+ }
671
+ }
672
+
673
+ async bulkUpsertRows(params: BulkUpsertRowsParams): Promise<ApiResponse> {
674
+ try {
675
+ const result = await this.tablesDB.upsertRows(params);
676
+ return {
677
+ data: result.rows,
678
+ rows: result.rows,
679
+ total: result.rows?.length || 0
680
+ };
681
+ } catch (error) {
682
+ throw new AdapterError(
683
+ `Failed to bulk upsert rows: ${error instanceof Error ? error.message : 'Unknown error'}`,
684
+ 'BULK_UPSERT_ROWS_FAILED',
685
+ error instanceof Error ? error : undefined
686
+ );
687
+ }
688
+ }
689
+
690
+ async bulkDeleteRows(params: BulkDeleteRowsParams): Promise<ApiResponse> {
691
+ try {
692
+ let queries: string[];
693
+
694
+ // Wipe mode: use Query.limit for deleting without fetching
695
+ if (params.rowIds.length === 0) {
696
+ const batchSize = params.batchSize || 250;
697
+ queries = [Query.limit(batchSize)];
698
+ }
699
+ // Specific IDs mode: chunk into batches of 80-90 to stay within Appwrite limits
700
+ // (max 100 IDs per Query.equal, and queries must be < 4096 chars total)
701
+ else {
702
+ const ID_BATCH_SIZE = 85; // Safe batch size for Query.equal
703
+ const idBatches = chunk(params.rowIds, ID_BATCH_SIZE);
704
+ queries = idBatches.map(batch => Query.equal('$id', batch));
705
+ }
706
+
707
+ const result = await this.tablesDB.deleteRows({
708
+ databaseId: params.databaseId,
709
+ tableId: params.tableId,
710
+ queries: queries
711
+ });
712
+
713
+ return {
714
+ data: result,
715
+ total: params.rowIds.length || (result as any).total || 0
716
+ };
717
+ } catch (error) {
718
+ throw new AdapterError(
719
+ `Failed to bulk delete rows: ${error instanceof Error ? error.message : 'Unknown error'}`,
720
+ 'BULK_DELETE_ROWS_FAILED',
721
+ error instanceof Error ? error : undefined
722
+ );
723
+ }
724
+ }
725
+
726
+ // Metadata and Capabilities
727
+ getMetadata(): AdapterMetadata {
728
+ return {
729
+ apiMode: 'tablesdb',
730
+ terminology: {
731
+ container: 'table',
732
+ item: 'row',
733
+ service: 'TablesDB'
734
+ },
735
+ capabilities: {
736
+ bulkOperations: true,
737
+ advancedQueries: true,
738
+ realtime: true,
739
+ transactions: true // TablesDB may support transactions
740
+ }
741
+ };
742
+ }
743
+
744
+ supportsBulkOperations(): boolean {
745
+ return true; // TablesDB natively supports bulk operations
746
+ }
747
+
748
+ // Advanced TablesDB Features
749
+
750
+ /**
751
+ * Execute a transaction (if supported by TablesDB)
752
+ */
753
+ async executeTransaction(operations: Array<() => Promise<any>>): Promise<ApiResponse> {
754
+ try {
755
+ // Create a new transaction first
756
+ const transaction = await this.tablesDB.createTransaction();
757
+
758
+ // Add operations to the transaction
759
+ const result = await this.tablesDB.createOperations({
760
+ transactionId: transaction.$id,
761
+ operations: operations.map(op => ({ operation: 'execute', fn: op }))
762
+ });
763
+
764
+ return { data: result };
765
+ } catch (error) {
766
+ throw new AdapterError(
767
+ `Transaction failed: ${error instanceof Error ? error.message : 'Unknown error'}`,
768
+ 'TRANSACTION_FAILED',
769
+ error instanceof Error ? error : undefined
770
+ );
771
+ }
772
+ }
773
+ }