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
@@ -0,0 +1,434 @@
1
+ import { mapToCreateAttributeParams, mapToUpdateAttributeParams } from "../shared/attributeMapper.js";
2
+ import { Decimal } from "decimal.js";
3
+ const EXTREME_BOUND = new Decimal('1e12');
4
+ // Property configuration for different column types
5
+ const MUTABLE_PROPERTIES = {
6
+ string: ["required", "default", "size", "array"],
7
+ integer: ["required", "default", "min", "max", "array"],
8
+ float: ["required", "default", "min", "max", "array"],
9
+ double: ["required", "default", "min", "max", "array"],
10
+ boolean: ["required", "default", "array"],
11
+ datetime: ["required", "default", "array"],
12
+ email: ["required", "default", "array"],
13
+ ip: ["required", "default", "array"],
14
+ url: ["required", "default", "array"],
15
+ enum: ["required", "default", "elements", "array"],
16
+ relationship: ["required", "default"],
17
+ };
18
+ const IMMUTABLE_PROPERTIES = {
19
+ string: ["encrypt", "key"],
20
+ integer: ["encrypt", "key"],
21
+ float: ["encrypt", "key"],
22
+ double: ["encrypt", "key"],
23
+ boolean: ["key"],
24
+ datetime: ["key"],
25
+ email: ["key"],
26
+ ip: ["key"],
27
+ url: ["key"],
28
+ enum: ["key"],
29
+ relationship: ["key", "relatedCollection", "relationType", "twoWay", "twoWayKey", "onDelete"],
30
+ };
31
+ const TYPE_CHANGE_REQUIRES_RECREATE = [
32
+ "string",
33
+ "integer",
34
+ "float",
35
+ "double",
36
+ "boolean",
37
+ "datetime",
38
+ "email",
39
+ "ip",
40
+ "url",
41
+ "enum",
42
+ "relationship",
43
+ ];
44
+ function normDefault(val) {
45
+ // Treat undefined and null as equal unset default
46
+ return val === undefined ? null : val;
47
+ }
48
+ function toNumber(n) {
49
+ if (n === null || n === undefined)
50
+ return undefined;
51
+ const num = Number(n);
52
+ return Number.isFinite(num) ? num : undefined;
53
+ }
54
+ export function normalizeAttributeToComparable(attr) {
55
+ const t = String(attr.type || '').toLowerCase();
56
+ const base = {
57
+ key: attr.key,
58
+ type: t,
59
+ required: !!attr.required,
60
+ array: !!attr.array,
61
+ default: normDefault(attr.xdefault),
62
+ };
63
+ if (t === 'string') {
64
+ base.size = attr.size ?? 255;
65
+ base.encrypt = !!(attr.encrypted ?? attr.encrypt);
66
+ }
67
+ if (t === 'integer' || t === 'float' || t === 'double') {
68
+ const min = toNumber(attr.min);
69
+ const max = toNumber(attr.max);
70
+ if (min !== undefined && max !== undefined) {
71
+ base.min = Math.min(min, max);
72
+ base.max = Math.max(min, max);
73
+ }
74
+ else {
75
+ base.min = min;
76
+ base.max = max;
77
+ }
78
+ }
79
+ if (t === 'enum') {
80
+ base.elements = Array.isArray(attr.elements) ? attr.elements.slice().sort() : [];
81
+ }
82
+ if (t === 'relationship') {
83
+ base.relatedCollection = attr.relatedCollection;
84
+ base.relationType = attr.relationType;
85
+ base.twoWay = !!attr.twoWay;
86
+ base.twoWayKey = attr.twoWayKey;
87
+ base.onDelete = attr.onDelete;
88
+ base.side = attr.side;
89
+ }
90
+ return base;
91
+ }
92
+ export function normalizeColumnToComparable(col) {
93
+ // Detect enum surfaced as string+elements from server and normalize to enum for comparison
94
+ let t = String((col?.type ?? col?.columnType ?? '')).toLowerCase();
95
+ const hasElements = Array.isArray(col?.elements) && col.elements.length > 0;
96
+ if (t === 'string' && hasElements)
97
+ t = 'enum';
98
+ const base = {
99
+ key: col?.key,
100
+ type: t,
101
+ required: !!col?.required,
102
+ array: !!col?.array,
103
+ default: normDefault(col?.default ?? col?.xdefault),
104
+ };
105
+ if (t === 'string') {
106
+ base.size = typeof col?.size === 'number' ? col.size : undefined;
107
+ base.encrypt = !!col?.encrypt;
108
+ }
109
+ if (t === 'integer' || t === 'float' || t === 'double') {
110
+ // Preserve raw min/max without forcing extremes; compare with Decimal in shallowEqual
111
+ const rawMin = col?.min;
112
+ const rawMax = col?.max;
113
+ base.min = rawMin;
114
+ base.max = rawMax;
115
+ }
116
+ if (t === 'enum') {
117
+ base.elements = Array.isArray(col?.elements) ? col.elements.slice().sort() : [];
118
+ }
119
+ if (t === 'relationship') {
120
+ base.relatedCollection = col?.relatedTableId || col?.relatedCollection;
121
+ base.relationType = col?.relationType || col?.typeName;
122
+ base.twoWay = !!col?.twoWay;
123
+ base.twoWayKey = col?.twoWayKey;
124
+ base.onDelete = col?.onDelete;
125
+ base.side = col?.side;
126
+ }
127
+ return base;
128
+ }
129
+ function shallowEqual(a, b) {
130
+ const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
131
+ for (const k of keys) {
132
+ const va = a[k];
133
+ const vb = b[k];
134
+ if (Array.isArray(va) && Array.isArray(vb)) {
135
+ if (va.length !== vb.length)
136
+ return false;
137
+ for (let i = 0; i < va.length; i++)
138
+ if (va[i] !== vb[i])
139
+ return false;
140
+ }
141
+ else if (k === 'min' || k === 'max') {
142
+ // Compare numeric bounds with Decimal to avoid precision issues
143
+ if (va == null && vb == null)
144
+ continue;
145
+ if (va == null || vb == null) {
146
+ // Treat extreme bounds on one side as equivalent to undefined (unbounded)
147
+ const present = va == null ? vb : va;
148
+ try {
149
+ const dp = new Decimal(String(present));
150
+ if (dp.abs().greaterThanOrEqualTo(EXTREME_BOUND))
151
+ continue; // equal
152
+ }
153
+ catch { }
154
+ return false;
155
+ }
156
+ try {
157
+ const da = new Decimal(String(va));
158
+ const db = new Decimal(String(vb));
159
+ if (!da.equals(db))
160
+ return false;
161
+ }
162
+ catch {
163
+ if (va !== vb)
164
+ return false;
165
+ }
166
+ }
167
+ else if (va !== vb) {
168
+ // Treat null and undefined as equal for defaults
169
+ if (!(va == null && vb == null))
170
+ return false;
171
+ }
172
+ }
173
+ return true;
174
+ }
175
+ export function isColumnEqualToColumn(a, b) {
176
+ const na = normalizeColumnToComparable(a);
177
+ const nb = normalizeColumnToComparable(b);
178
+ return shallowEqual(na, nb);
179
+ }
180
+ export function isIndexEqualToIndex(a, b) {
181
+ if (!a || !b)
182
+ return false;
183
+ if (a.key !== b.key)
184
+ return false;
185
+ if (String(a.type).toLowerCase() !== String(b.type).toLowerCase())
186
+ return false;
187
+ // Compare attributes as sets (order-insensitive)
188
+ const attrsA = Array.isArray(a.attributes) ? [...a.attributes].sort() : [];
189
+ const attrsB = Array.isArray(b.attributes) ? [...b.attributes].sort() : [];
190
+ if (attrsA.length !== attrsB.length)
191
+ return false;
192
+ for (let i = 0; i < attrsA.length; i++)
193
+ if (attrsA[i] !== attrsB[i])
194
+ return false;
195
+ // Orders are only considered if BOTH have orders defined
196
+ const hasOrdersA = Array.isArray(a.orders) && a.orders.length > 0;
197
+ const hasOrdersB = Array.isArray(b.orders) && b.orders.length > 0;
198
+ if (hasOrdersA && hasOrdersB) {
199
+ const ordersA = [...a.orders].sort();
200
+ const ordersB = [...b.orders].sort();
201
+ if (ordersA.length !== ordersB.length)
202
+ return false;
203
+ for (let i = 0; i < ordersA.length; i++)
204
+ if (ordersA[i] !== ordersB[i])
205
+ return false;
206
+ }
207
+ // If only one side has orders, treat as equal (orders unspecified by user)
208
+ return true;
209
+ }
210
+ /**
211
+ * Compare individual properties between old and new columns
212
+ */
213
+ function compareColumnProperties(oldColumn, newAttribute, columnType) {
214
+ const changes = [];
215
+ const t = String(columnType || newAttribute.type || '').toLowerCase();
216
+ const mutableProps = MUTABLE_PROPERTIES[t] || [];
217
+ const immutableProps = IMMUTABLE_PROPERTIES[t] || [];
218
+ const getNewVal = (prop) => {
219
+ const na = newAttribute;
220
+ if (prop === 'default')
221
+ return na.xdefault;
222
+ if (prop === 'encrypt')
223
+ return na.encrypted ?? na.encrypt;
224
+ return na[prop];
225
+ };
226
+ const getOldVal = (prop) => {
227
+ if (prop === 'default')
228
+ return oldColumn?.default ?? oldColumn?.xdefault;
229
+ return oldColumn?.[prop];
230
+ };
231
+ for (const prop of mutableProps) {
232
+ const oldValue = getOldVal(prop);
233
+ let newValue = getNewVal(prop);
234
+ // Special-case: enum elements empty/missing should not trigger updates
235
+ if (t === 'enum' && prop === 'elements') {
236
+ if (!Array.isArray(newValue) || newValue.length === 0)
237
+ newValue = oldValue;
238
+ }
239
+ if (Array.isArray(oldValue) && Array.isArray(newValue)) {
240
+ if (oldValue.length !== newValue.length || oldValue.some((v, i) => v !== newValue[i])) {
241
+ changes.push({ property: prop, oldValue, newValue, requiresRecreate: false });
242
+ }
243
+ }
244
+ else if (oldValue !== newValue) {
245
+ changes.push({ property: prop, oldValue, newValue, requiresRecreate: false });
246
+ }
247
+ }
248
+ for (const prop of immutableProps) {
249
+ const oldValue = getOldVal(prop);
250
+ const newValue = getNewVal(prop);
251
+ if (Array.isArray(oldValue) && Array.isArray(newValue)) {
252
+ if (oldValue.length !== newValue.length || oldValue.some((v, i) => v !== newValue[i])) {
253
+ changes.push({ property: prop, oldValue, newValue, requiresRecreate: true });
254
+ }
255
+ }
256
+ else if (oldValue !== newValue) {
257
+ changes.push({ property: prop, oldValue, newValue, requiresRecreate: true });
258
+ }
259
+ }
260
+ // Type change requires recreate (normalize string+elements to enum on old side)
261
+ const oldTypeRaw = String(oldColumn?.type || oldColumn?.columnType || '').toLowerCase();
262
+ const oldHasElements = Array.isArray(oldColumn?.elements) && oldColumn.elements.length > 0;
263
+ const oldType = oldTypeRaw === 'string' && oldHasElements ? 'enum' : oldTypeRaw;
264
+ if (oldType && t && oldType !== t && TYPE_CHANGE_REQUIRES_RECREATE.includes(oldType)) {
265
+ changes.push({ property: 'type', oldValue: oldType, newValue: t, requiresRecreate: true });
266
+ }
267
+ return changes;
268
+ }
269
+ /**
270
+ * Analyze what changes are needed for a specific column
271
+ */
272
+ function analyzeColumnChanges(oldColumn, newAttribute) {
273
+ const columnType = String(newAttribute.type || 'string').toLowerCase();
274
+ const columnKey = newAttribute.key;
275
+ // Use normalized comparison to reduce false positives then property-wise details
276
+ const normalizedOld = normalizeColumnToComparable(oldColumn);
277
+ const normalizedNew = normalizeAttributeToComparable(newAttribute);
278
+ const hasAnyDiff = !shallowEqual(normalizedOld, normalizedNew);
279
+ const changes = hasAnyDiff ? compareColumnProperties(oldColumn, newAttribute, columnType) : [];
280
+ const requiresRecreate = changes.some((c) => c.requiresRecreate);
281
+ const mutableChanges = {};
282
+ const immutableChanges = {};
283
+ for (const c of changes) {
284
+ if (c.requiresRecreate)
285
+ immutableChanges[c.property] = { old: c.oldValue, new: c.newValue };
286
+ else
287
+ mutableChanges[c.property] = { old: c.oldValue, new: c.newValue };
288
+ }
289
+ return {
290
+ columnKey,
291
+ columnType,
292
+ hasChanges: changes.length > 0,
293
+ requiresRecreate,
294
+ changes,
295
+ mutableChanges,
296
+ immutableChanges,
297
+ };
298
+ }
299
+ /**
300
+ * Enhanced version of columns diff with detailed change analysis
301
+ * Order: desired first, then existing (matches internal usage here)
302
+ */
303
+ export function diffColumnsDetailed(desiredAttributes, existingColumns) {
304
+ const byKey = new Map((existingColumns || []).map((col) => [col?.key, col]));
305
+ const toCreate = [];
306
+ const toUpdate = [];
307
+ const toRecreate = [];
308
+ const unchanged = [];
309
+ for (const attr of desiredAttributes || []) {
310
+ const key = attr?.key;
311
+ const existing = key ? byKey.get(key) : undefined;
312
+ if (!existing) {
313
+ toCreate.push(attr);
314
+ continue;
315
+ }
316
+ const analysis = analyzeColumnChanges(existing, attr);
317
+ if (!analysis.hasChanges)
318
+ unchanged.push(analysis.columnKey);
319
+ else if (analysis.requiresRecreate)
320
+ toRecreate.push({ oldAttribute: existing, newAttribute: attr });
321
+ else
322
+ toUpdate.push({ attribute: attr, changes: analysis.changes });
323
+ }
324
+ // Note: we keep toDelete empty for now (conservative behavior)
325
+ return { toCreate, toUpdate, toRecreate, toDelete: [], unchanged };
326
+ }
327
+ /**
328
+ * Returns true if there is any difference between existing columns and desired attributes
329
+ */
330
+ export function areTableColumnsDiff(existingColumns, desired) {
331
+ const byKey = new Map();
332
+ for (const c of existingColumns || []) {
333
+ if (c?.key)
334
+ byKey.set(c.key, c);
335
+ }
336
+ for (const attr of desired || []) {
337
+ const desiredNorm = normalizeAttributeToComparable(attr);
338
+ const existing = byKey.get(desiredNorm.key);
339
+ if (!existing)
340
+ return true;
341
+ const existingNorm = normalizeColumnToComparable(existing);
342
+ if (!shallowEqual(desiredNorm, existingNorm))
343
+ return true;
344
+ }
345
+ // Extra columns on remote also constitute a diff
346
+ const desiredKeys = new Set((desired || []).map((a) => a.key));
347
+ for (const k of byKey.keys())
348
+ if (!desiredKeys.has(k))
349
+ return true;
350
+ return false;
351
+ }
352
+ export function diffTableColumns(existingColumns, desired) {
353
+ // Use detailed plan but return legacy structure for compatibility
354
+ const plan = diffColumnsDetailed(desired, existingColumns);
355
+ const toUpdate = [
356
+ ...plan.toUpdate.map((u) => u.attribute),
357
+ ...plan.toRecreate.map((r) => r.newAttribute),
358
+ ];
359
+ return { toCreate: plan.toCreate, toUpdate, unchanged: plan.unchanged };
360
+ }
361
+ /**
362
+ * Execute the column operation plan using the adapter
363
+ */
364
+ export async function executeColumnOperations(adapter, databaseId, tableId, plan) {
365
+ if (!databaseId || !tableId)
366
+ throw new Error('Database ID and Table ID are required for column operations');
367
+ if (!adapter || typeof adapter.createAttribute !== 'function')
368
+ throw new Error('Valid adapter is required for column operations');
369
+ const results = { success: [], errors: [] };
370
+ const exec = async (fn, key, op) => {
371
+ try {
372
+ await fn();
373
+ results.success.push(`${op}: ${key}`);
374
+ }
375
+ catch (e) {
376
+ results.errors.push({ column: key, error: `${op} failed: ${e?.message || String(e)}` });
377
+ }
378
+ };
379
+ for (const attr of plan.toCreate) {
380
+ const params = mapToCreateAttributeParams(attr, { databaseId, tableId });
381
+ await exec(() => adapter.createAttribute(params), attr.key, 'CREATE');
382
+ }
383
+ for (const { attribute } of plan.toUpdate) {
384
+ const params = mapToUpdateAttributeParams(attribute, { databaseId, tableId });
385
+ await exec(() => adapter.updateAttribute(params), attribute.key, 'UPDATE');
386
+ }
387
+ for (const { oldAttribute, newAttribute } of plan.toRecreate) {
388
+ await exec(() => adapter.deleteAttribute({ databaseId, tableId, key: oldAttribute.key }), oldAttribute.key, 'DELETE (for recreate)');
389
+ // Wait until the attribute is actually removed (or no longer 'deleting') before recreating
390
+ try {
391
+ const start = Date.now();
392
+ const maxWaitMs = 60000; // 60s
393
+ while (Date.now() - start < maxWaitMs) {
394
+ try {
395
+ const tableRes = await adapter.getTable({ databaseId, tableId });
396
+ const cols = (tableRes?.data?.columns || tableRes?.data?.attributes || []);
397
+ const found = cols.find((c) => c.key === oldAttribute.key);
398
+ if (!found)
399
+ break; // fully removed
400
+ if (found.status && found.status !== 'deleting')
401
+ break; // no longer deleting (failed/stuck) -> stop waiting
402
+ }
403
+ catch { }
404
+ await new Promise((r) => setTimeout(r, 1500));
405
+ }
406
+ }
407
+ catch { }
408
+ const params = mapToCreateAttributeParams(newAttribute, { databaseId, tableId });
409
+ await exec(() => adapter.createAttribute(params), newAttribute.key, 'CREATE (after recreate)');
410
+ }
411
+ return results;
412
+ }
413
+ /**
414
+ * Integration function for methods.ts - processes columns using enhanced logic
415
+ */
416
+ export async function processTableColumns(adapter, databaseId, tableId, desiredAttributes, existingColumns = []) {
417
+ if (!existingColumns || existingColumns.length === 0) {
418
+ const tableInfo = await adapter.getTable({ databaseId, tableId });
419
+ existingColumns = (tableInfo?.data?.columns || tableInfo?.data?.attributes || []);
420
+ }
421
+ const plan = diffColumnsDetailed(desiredAttributes, existingColumns);
422
+ const results = await executeColumnOperations(adapter, databaseId, tableId, plan);
423
+ return {
424
+ totalProcessed: plan.toCreate.length + plan.toUpdate.length + plan.toRecreate.length,
425
+ success: results.success,
426
+ errors: results.errors,
427
+ summary: {
428
+ created: plan.toCreate.length,
429
+ updated: plan.toUpdate.length,
430
+ recreated: plan.toRecreate.length,
431
+ unchanged: plan.unchanged.length,
432
+ },
433
+ };
434
+ }
@@ -1,7 +1,8 @@
1
1
  import { Databases } from "node-appwrite";
2
+ import type { DatabaseAdapter } from "../adapters/DatabaseAdapter.js";
2
3
  /**
3
4
  * Transfers all documents from one collection to another in a different database
4
5
  * within the same Appwrite Project
5
6
  */
6
- export declare const transferDocumentsBetweenDbsLocalToLocal: (db: Databases, fromDbId: string, toDbId: string, fromCollId: string, toCollId: string) => Promise<void>;
7
- export declare const transferDocumentsBetweenDbsLocalToRemote: (localDb: Databases, endpoint: string, projectId: string, apiKey: string, fromDbId: string, toDbId: string, fromCollId: string, toCollId: string) => Promise<void>;
7
+ export declare const transferDocumentsBetweenDbsLocalToLocal: (db: Databases | DatabaseAdapter, fromDbId: string, toDbId: string, fromCollId: string, toCollId: string) => Promise<void>;
8
+ export declare const transferDocumentsBetweenDbsLocalToRemote: (localDb: Databases | DatabaseAdapter, endpoint: string, projectId: string, apiKey: string, fromDbId: string, toDbId: string, fromCollId: string, toCollId: string) => Promise<void>;
@@ -2,12 +2,62 @@ import { Client, Databases, ID, Query, } from "node-appwrite";
2
2
  import { tryAwaitWithRetry, delay, calculateExponentialBackoff } from "../utils/helperFunctions.js";
3
3
  import { MessageFormatter } from "../shared/messageFormatter.js";
4
4
  import { chunk } from "es-toolkit";
5
+ import { isLegacyDatabases } from "../utils/typeGuards.js";
6
+ import { getAdapter } from "../utils/getClientFromConfig.js";
5
7
  /**
6
8
  * Transfers all documents from one collection to another in a different database
7
9
  * within the same Appwrite Project
8
10
  */
9
11
  export const transferDocumentsBetweenDbsLocalToLocal = async (db, fromDbId, toDbId, fromCollId, toCollId) => {
10
- let fromCollDocs = await tryAwaitWithRetry(async () => db.listDocuments(fromDbId, fromCollId, [Query.limit(50)]));
12
+ // Use adapter path when available for bulk operations
13
+ if (!isLegacyDatabases(db)) {
14
+ const adapter = db;
15
+ const pageSize = 1000;
16
+ let lastId;
17
+ let totalTransferred = 0;
18
+ while (true) {
19
+ const queries = [Query.limit(pageSize)];
20
+ if (lastId)
21
+ queries.push(Query.cursorAfter(lastId));
22
+ const result = await adapter.listRows({ databaseId: fromDbId, tableId: fromCollId, queries });
23
+ const rows = result.rows || result.documents || [];
24
+ if (!rows.length)
25
+ break;
26
+ // Prepare rows: strip system fields, keep $id and $permissions
27
+ const prepared = rows.map((doc) => {
28
+ const data = { ...doc };
29
+ delete data.$databaseId;
30
+ delete data.$collectionId;
31
+ delete data.$createdAt;
32
+ delete data.$updatedAt;
33
+ return data; // keep $id and $permissions for upsert
34
+ });
35
+ // Prefer bulk upsert, then bulk create, then individual
36
+ if (typeof adapter.bulkUpsertRows === 'function' && adapter.supportsBulkOperations()) {
37
+ await adapter.bulkUpsertRows({ databaseId: toDbId, tableId: toCollId, rows: prepared });
38
+ }
39
+ else if (typeof adapter.bulkCreateRows === 'function' && adapter.supportsBulkOperations()) {
40
+ await adapter.bulkCreateRows({ databaseId: toDbId, tableId: toCollId, rows: prepared });
41
+ }
42
+ else {
43
+ for (const row of prepared) {
44
+ const id = row.$id || ID.unique();
45
+ const permissions = row.$permissions || [];
46
+ const { $id, $permissions, ...data } = row;
47
+ await adapter.createRow({ databaseId: toDbId, tableId: toCollId, id, data, permissions });
48
+ }
49
+ }
50
+ totalTransferred += rows.length;
51
+ if (rows.length < pageSize)
52
+ break;
53
+ lastId = rows[rows.length - 1].$id;
54
+ }
55
+ MessageFormatter.success(`Transferred ${totalTransferred} rows from ${fromDbId}/${fromCollId} to ${toDbId}/${toCollId}`, { prefix: "Transfer" });
56
+ return;
57
+ }
58
+ // Legacy path (Databases) – keep existing behavior
59
+ const legacyDb = db;
60
+ let fromCollDocs = await tryAwaitWithRetry(async () => legacyDb.listDocuments(fromDbId, fromCollId, [Query.limit(50)]));
11
61
  let totalDocumentsTransferred = 0;
12
62
  if (fromCollDocs.documents.length === 0) {
13
63
  MessageFormatter.info(`No documents found in collection ${fromCollId}`, { prefix: "Transfer" });
@@ -24,7 +74,7 @@ export const transferDocumentsBetweenDbsLocalToLocal = async (db, fromDbId, toDb
24
74
  delete toCreateObject.$updatedAt;
25
75
  delete toCreateObject.$id;
26
76
  delete toCreateObject.$permissions;
27
- return tryAwaitWithRetry(async () => await db.createDocument(toDbId, toCollId, doc.$id, toCreateObject, doc.$permissions));
77
+ return tryAwaitWithRetry(async () => await legacyDb.createDocument(toDbId, toCollId, doc.$id, toCreateObject, doc.$permissions));
28
78
  });
29
79
  await Promise.all(batchedPromises);
30
80
  totalDocumentsTransferred += fromCollDocs.documents.length;
@@ -40,12 +90,12 @@ export const transferDocumentsBetweenDbsLocalToLocal = async (db, fromDbId, toDb
40
90
  delete toCreateObject.$updatedAt;
41
91
  delete toCreateObject.$id;
42
92
  delete toCreateObject.$permissions;
43
- return tryAwaitWithRetry(async () => db.createDocument(toDbId, toCollId, doc.$id, toCreateObject, doc.$permissions));
93
+ return tryAwaitWithRetry(async () => legacyDb.createDocument(toDbId, toCollId, doc.$id, toCreateObject, doc.$permissions));
44
94
  });
45
95
  await Promise.all(batchedPromises);
46
96
  totalDocumentsTransferred += fromCollDocs.documents.length;
47
97
  while (fromCollDocs.documents.length === 50) {
48
- fromCollDocs = await tryAwaitWithRetry(async () => await db.listDocuments(fromDbId, fromCollId, [
98
+ fromCollDocs = await tryAwaitWithRetry(async () => await legacyDb.listDocuments(fromDbId, fromCollId, [
49
99
  Query.limit(50),
50
100
  Query.cursorAfter(fromCollDocs.documents[fromCollDocs.documents.length - 1].$id),
51
101
  ]));
@@ -59,7 +109,7 @@ export const transferDocumentsBetweenDbsLocalToLocal = async (db, fromDbId, toDb
59
109
  delete toCreateObject.$updatedAt;
60
110
  delete toCreateObject.$id;
61
111
  delete toCreateObject.$permissions;
62
- return tryAwaitWithRetry(async () => await db.createDocument(toDbId, toCollId, doc.$id, toCreateObject, doc.$permissions));
112
+ return tryAwaitWithRetry(async () => await legacyDb.createDocument(toDbId, toCollId, doc.$id, toCreateObject, doc.$permissions));
63
113
  });
64
114
  await Promise.all(batchedPromises);
65
115
  totalDocumentsTransferred += fromCollDocs.documents.length;
@@ -282,11 +332,9 @@ const transferDocumentBatchWithRetry = async (db, client, dbId, collectionId, do
282
332
  };
283
333
  export const transferDocumentsBetweenDbsLocalToRemote = async (localDb, endpoint, projectId, apiKey, fromDbId, toDbId, fromCollId, toCollId) => {
284
334
  MessageFormatter.info(`Starting enhanced document transfer from ${fromCollId} to ${toCollId}...`, { prefix: "Transfer" });
285
- const client = new Client()
286
- .setEndpoint(endpoint)
287
- .setProject(projectId)
288
- .setKey(apiKey);
289
- const remoteDb = new Databases(client);
335
+ // Prefer adapter for remote to enable bulk operations
336
+ const { adapter: remoteAdapter, client } = await getAdapter(endpoint, projectId, apiKey, 'auto');
337
+ const remoteDb = new Databases(client); // Legacy fallback for HTTP/individual
290
338
  let totalDocumentsProcessed = 0;
291
339
  let totalSuccessful = 0;
292
340
  let totalFailed = 0;
@@ -298,13 +346,46 @@ export const transferDocumentsBetweenDbsLocalToRemote = async (localDb, endpoint
298
346
  if (lastDocumentId) {
299
347
  queries.push(Query.cursorAfter(lastDocumentId));
300
348
  }
301
- const fromCollDocs = await tryAwaitWithRetry(async () => localDb.listDocuments(fromDbId, fromCollId, queries));
349
+ const fromCollDocs = await tryAwaitWithRetry(async () => {
350
+ if (isLegacyDatabases(localDb)) {
351
+ return localDb.listDocuments(fromDbId, fromCollId, queries);
352
+ }
353
+ else {
354
+ const res = await localDb.listRows({ databaseId: fromDbId, tableId: fromCollId, queries });
355
+ const rows = res.rows || res.documents || [];
356
+ return { documents: rows };
357
+ }
358
+ });
302
359
  if (fromCollDocs.documents.length === 0) {
303
360
  hasMoreDocuments = false;
304
361
  break;
305
362
  }
306
363
  MessageFormatter.progress(`Fetched ${fromCollDocs.documents.length} documents, processing for transfer...`, { prefix: "Transfer" });
307
- const { successful, failed } = await transferDocumentBatchWithRetry(remoteDb, client, toDbId, toCollId, fromCollDocs.documents);
364
+ // Prefer remote adapter bulk upsert if available
365
+ const prepared = fromCollDocs.documents.map((doc) => {
366
+ const data = { ...doc };
367
+ delete data.$databaseId;
368
+ delete data.$collectionId;
369
+ delete data.$createdAt;
370
+ delete data.$updatedAt;
371
+ return data; // Keep $id and $permissions for upsert
372
+ });
373
+ let successful = 0;
374
+ let failed = 0;
375
+ if (typeof remoteAdapter.bulkUpsertRows === 'function' && remoteAdapter.supportsBulkOperations()) {
376
+ try {
377
+ await remoteAdapter.bulkUpsertRows({ databaseId: toDbId, tableId: toCollId, rows: prepared });
378
+ successful = prepared.length;
379
+ }
380
+ catch (e) {
381
+ MessageFormatter.warning('Remote adapter bulk upsert failed, falling back to HTTP/individual', { prefix: 'Transfer' });
382
+ }
383
+ }
384
+ if (successful === 0) {
385
+ const res = await transferDocumentBatchWithRetry(remoteDb, client, toDbId, toCollId, fromCollDocs.documents);
386
+ successful = res.successful;
387
+ failed = res.failed;
388
+ }
308
389
  totalDocumentsProcessed += fromCollDocs.documents.length;
309
390
  totalSuccessful += successful;
310
391
  totalFailed += failed;