appwrite-utils-cli 1.11.0 → 1.12.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/{src/adapters/index.ts → dist/adapters/index.d.ts} +0 -1
- package/dist/adapters/index.js +10 -0
- package/dist/backups/operations/bucketBackup.d.ts +19 -0
- package/dist/backups/operations/bucketBackup.js +197 -0
- package/dist/backups/operations/collectionBackup.d.ts +30 -0
- package/dist/backups/operations/collectionBackup.js +201 -0
- package/dist/backups/operations/comprehensiveBackup.d.ts +25 -0
- package/dist/backups/operations/comprehensiveBackup.js +238 -0
- package/dist/backups/schemas/bucketManifest.d.ts +93 -0
- package/dist/backups/schemas/bucketManifest.js +33 -0
- package/dist/backups/schemas/comprehensiveManifest.d.ts +108 -0
- package/dist/backups/schemas/comprehensiveManifest.js +32 -0
- package/dist/backups/tracking/centralizedTracking.d.ts +34 -0
- package/dist/backups/tracking/centralizedTracking.js +274 -0
- package/dist/cli/commands/configCommands.d.ts +8 -0
- package/dist/cli/commands/configCommands.js +210 -0
- package/dist/cli/commands/databaseCommands.d.ts +14 -0
- package/dist/cli/commands/databaseCommands.js +696 -0
- package/dist/cli/commands/functionCommands.d.ts +7 -0
- package/dist/cli/commands/functionCommands.js +330 -0
- package/dist/cli/commands/importFileCommands.d.ts +7 -0
- package/dist/cli/commands/importFileCommands.js +674 -0
- package/dist/cli/commands/schemaCommands.d.ts +7 -0
- package/dist/cli/commands/schemaCommands.js +169 -0
- package/dist/cli/commands/storageCommands.d.ts +5 -0
- package/dist/cli/commands/storageCommands.js +142 -0
- package/dist/cli/commands/transferCommands.d.ts +5 -0
- package/dist/cli/commands/transferCommands.js +382 -0
- package/dist/collections/columns.d.ts +13 -0
- package/dist/collections/columns.js +1339 -0
- package/dist/collections/indexes.d.ts +12 -0
- package/dist/collections/indexes.js +215 -0
- package/dist/collections/methods.d.ts +19 -0
- package/dist/collections/methods.js +605 -0
- package/dist/collections/tableOperations.d.ts +87 -0
- package/dist/collections/tableOperations.js +466 -0
- package/dist/collections/transferOperations.d.ts +8 -0
- package/dist/collections/transferOperations.js +411 -0
- package/dist/collections/wipeOperations.d.ts +17 -0
- package/dist/collections/wipeOperations.js +306 -0
- package/dist/databases/methods.d.ts +6 -0
- package/dist/databases/methods.js +35 -0
- package/dist/databases/setup.d.ts +5 -0
- package/dist/databases/setup.js +45 -0
- package/dist/examples/yamlTerminologyExample.d.ts +42 -0
- package/dist/examples/yamlTerminologyExample.js +272 -0
- package/dist/functions/deployments.d.ts +4 -0
- package/dist/functions/deployments.js +146 -0
- package/dist/functions/fnConfigDiscovery.d.ts +3 -0
- package/dist/functions/fnConfigDiscovery.js +108 -0
- package/dist/functions/methods.d.ts +16 -0
- package/dist/functions/methods.js +174 -0
- package/dist/init.d.ts +2 -0
- package/dist/init.js +57 -0
- package/dist/interactiveCLI.d.ts +36 -0
- package/dist/interactiveCLI.js +952 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +1125 -0
- package/dist/migrations/afterImportActions.d.ts +17 -0
- package/dist/migrations/afterImportActions.js +305 -0
- package/dist/migrations/appwriteToX.d.ts +211 -0
- package/dist/migrations/appwriteToX.js +493 -0
- package/dist/migrations/comprehensiveTransfer.d.ts +147 -0
- package/dist/migrations/comprehensiveTransfer.js +1315 -0
- package/dist/migrations/dataLoader.d.ts +755 -0
- package/dist/migrations/dataLoader.js +1272 -0
- package/dist/migrations/importController.d.ts +25 -0
- package/dist/migrations/importController.js +283 -0
- package/dist/migrations/importDataActions.d.ts +50 -0
- package/dist/migrations/importDataActions.js +230 -0
- package/dist/migrations/relationships.d.ts +29 -0
- package/dist/migrations/relationships.js +203 -0
- package/dist/migrations/services/DataTransformationService.d.ts +55 -0
- package/dist/migrations/services/DataTransformationService.js +158 -0
- package/dist/migrations/services/FileHandlerService.d.ts +75 -0
- package/dist/migrations/services/FileHandlerService.js +236 -0
- package/dist/migrations/services/ImportOrchestrator.d.ts +99 -0
- package/dist/migrations/services/ImportOrchestrator.js +493 -0
- package/dist/migrations/services/RateLimitManager.d.ts +138 -0
- package/dist/migrations/services/RateLimitManager.js +279 -0
- package/dist/migrations/services/RelationshipResolver.d.ts +120 -0
- package/dist/migrations/services/RelationshipResolver.js +332 -0
- package/dist/migrations/services/UserMappingService.d.ts +109 -0
- package/dist/migrations/services/UserMappingService.js +277 -0
- package/dist/migrations/services/ValidationService.d.ts +74 -0
- package/dist/migrations/services/ValidationService.js +260 -0
- package/dist/migrations/transfer.d.ts +30 -0
- package/dist/migrations/transfer.js +661 -0
- package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +131 -0
- package/dist/migrations/yaml/YamlImportConfigLoader.js +383 -0
- package/dist/migrations/yaml/YamlImportIntegration.d.ts +93 -0
- package/dist/migrations/yaml/YamlImportIntegration.js +341 -0
- package/dist/migrations/yaml/generateImportSchemas.d.ts +30 -0
- package/dist/migrations/yaml/generateImportSchemas.js +1327 -0
- package/dist/schemas/authUser.d.ts +24 -0
- package/dist/schemas/authUser.js +17 -0
- package/dist/setup.d.ts +2 -0
- package/{src/setup.ts → dist/setup.js} +0 -3
- package/dist/setupCommands.d.ts +58 -0
- package/dist/setupCommands.js +489 -0
- package/dist/setupController.d.ts +9 -0
- package/dist/setupController.js +34 -0
- package/dist/shared/backupMetadataSchema.d.ts +94 -0
- package/dist/shared/backupMetadataSchema.js +38 -0
- package/dist/shared/backupTracking.d.ts +18 -0
- package/dist/shared/backupTracking.js +176 -0
- package/dist/shared/confirmationDialogs.d.ts +75 -0
- package/dist/shared/confirmationDialogs.js +236 -0
- package/dist/shared/migrationHelpers.d.ts +61 -0
- package/dist/shared/migrationHelpers.js +145 -0
- package/{src/shared/operationLogger.ts → dist/shared/operationLogger.d.ts} +1 -11
- package/dist/shared/operationLogger.js +12 -0
- package/dist/shared/operationQueue.d.ts +40 -0
- package/dist/shared/operationQueue.js +310 -0
- package/dist/shared/operationsTable.d.ts +26 -0
- package/dist/shared/operationsTable.js +287 -0
- package/dist/shared/operationsTableSchema.d.ts +48 -0
- package/dist/shared/operationsTableSchema.js +35 -0
- package/dist/shared/progressManager.d.ts +62 -0
- package/dist/shared/progressManager.js +215 -0
- package/dist/shared/relationshipExtractor.d.ts +56 -0
- package/dist/shared/relationshipExtractor.js +138 -0
- package/dist/shared/selectionDialogs.d.ts +220 -0
- package/dist/shared/selectionDialogs.js +588 -0
- package/dist/storage/backupCompression.d.ts +20 -0
- package/dist/storage/backupCompression.js +67 -0
- package/dist/storage/methods.d.ts +44 -0
- package/dist/storage/methods.js +475 -0
- package/dist/storage/schemas.d.ts +842 -0
- package/dist/storage/schemas.js +175 -0
- package/dist/tables/indexManager.d.ts +65 -0
- package/dist/tables/indexManager.js +294 -0
- package/{src/types.ts → dist/types.d.ts} +1 -6
- package/dist/types.js +3 -0
- package/dist/users/methods.d.ts +16 -0
- package/dist/users/methods.js +276 -0
- package/dist/utils/configMigration.d.ts +1 -0
- package/dist/utils/configMigration.js +261 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/loadConfigs.d.ts +50 -0
- package/dist/utils/loadConfigs.js +357 -0
- package/dist/utils/setupFiles.d.ts +4 -0
- package/dist/utils/setupFiles.js +1190 -0
- package/dist/utilsController.d.ts +114 -0
- package/dist/utilsController.js +898 -0
- package/package.json +6 -3
- package/CHANGELOG.md +0 -35
- package/CONFIG_TODO.md +0 -1189
- package/SELECTION_DIALOGS.md +0 -146
- package/SERVICE_IMPLEMENTATION_REPORT.md +0 -462
- package/scripts/copy-templates.ts +0 -23
- package/src/backups/operations/bucketBackup.ts +0 -277
- package/src/backups/operations/collectionBackup.ts +0 -310
- package/src/backups/operations/comprehensiveBackup.ts +0 -342
- package/src/backups/schemas/bucketManifest.ts +0 -78
- package/src/backups/schemas/comprehensiveManifest.ts +0 -76
- package/src/backups/tracking/centralizedTracking.ts +0 -352
- package/src/cli/commands/configCommands.ts +0 -265
- package/src/cli/commands/databaseCommands.ts +0 -931
- package/src/cli/commands/functionCommands.ts +0 -419
- package/src/cli/commands/importFileCommands.ts +0 -815
- package/src/cli/commands/schemaCommands.ts +0 -200
- package/src/cli/commands/storageCommands.ts +0 -151
- package/src/cli/commands/transferCommands.ts +0 -454
- package/src/collections/attributes.ts.backup +0 -1555
- package/src/collections/columns.ts +0 -2025
- package/src/collections/indexes.ts +0 -350
- package/src/collections/methods.ts +0 -714
- package/src/collections/tableOperations.ts +0 -542
- package/src/collections/transferOperations.ts +0 -589
- package/src/collections/wipeOperations.ts +0 -449
- package/src/databases/methods.ts +0 -49
- package/src/databases/setup.ts +0 -77
- package/src/examples/yamlTerminologyExample.ts +0 -346
- package/src/functions/deployments.ts +0 -221
- package/src/functions/fnConfigDiscovery.ts +0 -103
- package/src/functions/methods.ts +0 -284
- package/src/init.ts +0 -62
- package/src/interactiveCLI.ts +0 -1201
- package/src/main.ts +0 -1517
- package/src/migrations/afterImportActions.ts +0 -579
- package/src/migrations/appwriteToX.ts +0 -668
- package/src/migrations/comprehensiveTransfer.ts +0 -2285
- package/src/migrations/dataLoader.ts +0 -1729
- package/src/migrations/importController.ts +0 -440
- package/src/migrations/importDataActions.ts +0 -315
- package/src/migrations/relationships.ts +0 -333
- package/src/migrations/services/DataTransformationService.ts +0 -196
- package/src/migrations/services/FileHandlerService.ts +0 -311
- package/src/migrations/services/ImportOrchestrator.ts +0 -675
- package/src/migrations/services/RateLimitManager.ts +0 -363
- package/src/migrations/services/RelationshipResolver.ts +0 -461
- package/src/migrations/services/UserMappingService.ts +0 -345
- package/src/migrations/services/ValidationService.ts +0 -349
- package/src/migrations/transfer.ts +0 -1113
- package/src/migrations/yaml/YamlImportConfigLoader.ts +0 -439
- package/src/migrations/yaml/YamlImportIntegration.ts +0 -446
- package/src/migrations/yaml/generateImportSchemas.ts +0 -1354
- package/src/schemas/authUser.ts +0 -23
- package/src/setupCommands.ts +0 -602
- package/src/setupController.ts +0 -43
- package/src/shared/backupMetadataSchema.ts +0 -93
- package/src/shared/backupTracking.ts +0 -211
- package/src/shared/confirmationDialogs.ts +0 -327
- package/src/shared/migrationHelpers.ts +0 -232
- package/src/shared/operationQueue.ts +0 -376
- package/src/shared/operationsTable.ts +0 -338
- package/src/shared/operationsTableSchema.ts +0 -60
- package/src/shared/progressManager.ts +0 -278
- package/src/shared/relationshipExtractor.ts +0 -214
- package/src/shared/selectionDialogs.ts +0 -802
- package/src/storage/backupCompression.ts +0 -88
- package/src/storage/methods.ts +0 -711
- package/src/storage/schemas.ts +0 -205
- package/src/tables/indexManager.ts +0 -409
- package/src/types/node-appwrite-tablesdb.d.ts +0 -44
- package/src/users/methods.ts +0 -358
- package/src/utils/configMigration.ts +0 -348
- package/src/utils/loadConfigs.ts +0 -457
- package/src/utils/setupFiles.ts +0 -1236
- package/src/utilsController.ts +0 -1263
- package/tests/README.md +0 -497
- package/tests/adapters/AdapterFactory.test.ts +0 -277
- package/tests/integration/syncOperations.test.ts +0 -463
- package/tests/jest.config.js +0 -25
- package/tests/migration/configMigration.test.ts +0 -546
- package/tests/setup.ts +0 -62
- package/tests/testUtils.ts +0 -340
- package/tests/utils/loadConfigs.test.ts +0 -350
- package/tests/validation/configValidation.test.ts +0 -412
- package/tsconfig.json +0 -44
- /package/{src → dist}/functions/templates/count-docs-in-collection/README.md +0 -0
- /package/{src → dist}/functions/templates/count-docs-in-collection/src/main.ts +0 -0
- /package/{src → dist}/functions/templates/count-docs-in-collection/src/request.ts +0 -0
- /package/{src → dist}/functions/templates/hono-typescript/README.md +0 -0
- /package/{src → dist}/functions/templates/hono-typescript/src/adapters/request.ts +0 -0
- /package/{src → dist}/functions/templates/hono-typescript/src/adapters/response.ts +0 -0
- /package/{src → dist}/functions/templates/hono-typescript/src/app.ts +0 -0
- /package/{src → dist}/functions/templates/hono-typescript/src/context.ts +0 -0
- /package/{src → dist}/functions/templates/hono-typescript/src/main.ts +0 -0
- /package/{src → dist}/functions/templates/hono-typescript/src/middleware/appwrite.ts +0 -0
- /package/{src → dist}/functions/templates/typescript-node/README.md +0 -0
- /package/{src → dist}/functions/templates/typescript-node/src/context.ts +0 -0
- /package/{src → dist}/functions/templates/typescript-node/src/main.ts +0 -0
- /package/{src → dist}/functions/templates/uv/README.md +0 -0
- /package/{src → dist}/functions/templates/uv/pyproject.toml +0 -0
- /package/{src → dist}/functions/templates/uv/src/__init__.py +0 -0
- /package/{src → dist}/functions/templates/uv/src/context.py +0 -0
- /package/{src → dist}/functions/templates/uv/src/main.py +0 -0
- /package/{src/utils/index.ts → dist/utils/index.d.ts} +0 -0
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
import { ID, Query, type Databases } from "node-appwrite";
|
|
2
|
-
import { BatchSchema, OperationSchema, type Operation } from "../storage/schemas.js";
|
|
3
|
-
import { AttributeMappingsSchema } from "appwrite-utils";
|
|
4
|
-
import { z } from "zod";
|
|
5
|
-
import { logger } from 'appwrite-utils-helpers';
|
|
6
|
-
import { tryAwaitWithRetry } from "appwrite-utils-helpers";
|
|
7
|
-
import {
|
|
8
|
-
findOrCreateOperation as findOrCreateOp,
|
|
9
|
-
updateOperation as updateOp,
|
|
10
|
-
getOperation as getOp
|
|
11
|
-
} from "./operationsTable.js";
|
|
12
|
-
import type { DatabaseAdapter } from "appwrite-utils-helpers";
|
|
13
|
-
import { MessageFormatter } from 'appwrite-utils-helpers';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Object that contains the context for an action that needs to be executed after import
|
|
17
|
-
* Used in the afterImportActionsDefinitions
|
|
18
|
-
* @type {ContextObject}
|
|
19
|
-
* @typedef {Object} ContextObject
|
|
20
|
-
* @property {string} collectionId - The ID of the collection
|
|
21
|
-
* @property {any} finalItem - The final item that was imported
|
|
22
|
-
* @property {string} action - The name of the action
|
|
23
|
-
* @property {string[]} params - The parameters for the action
|
|
24
|
-
* @property {Object} context - The context object for the action (all the data of this specific item)
|
|
25
|
-
*/
|
|
26
|
-
export const ContextObject = z.object({
|
|
27
|
-
dbId: z.string(),
|
|
28
|
-
collectionId: z.string(),
|
|
29
|
-
finalItem: z.any(),
|
|
30
|
-
attributeMappings: AttributeMappingsSchema,
|
|
31
|
-
context: z.any(),
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
export type ContextObject = z.infer<typeof ContextObject>;
|
|
35
|
-
|
|
36
|
-
export const createOrFindAfterImportOperation = async (
|
|
37
|
-
database: Databases,
|
|
38
|
-
collectionId: string,
|
|
39
|
-
context: ContextObject
|
|
40
|
-
) => {
|
|
41
|
-
let operation = await findOrCreateOperationLegacy(
|
|
42
|
-
database,
|
|
43
|
-
collectionId,
|
|
44
|
-
"afterImportAction"
|
|
45
|
-
);
|
|
46
|
-
if (!operation.batches) {
|
|
47
|
-
operation.batches = [];
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Directly create a new batch for the context without checking for an existing batch
|
|
51
|
-
const contextData = JSON.stringify(context);
|
|
52
|
-
// Create a new batch with the contextData
|
|
53
|
-
const newBatchId = await addBatch(database, contextData);
|
|
54
|
-
// Update the operation with the new batch's $id
|
|
55
|
-
operation.batches = [...operation.batches, newBatchId];
|
|
56
|
-
await database.updateDocument(
|
|
57
|
-
"migrations",
|
|
58
|
-
"currentOperations",
|
|
59
|
-
operation.$id,
|
|
60
|
-
{ batches: operation.batches }
|
|
61
|
-
);
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
export const addBatch = async (database: Databases, data: string) => {
|
|
65
|
-
const batch = await database.createDocument(
|
|
66
|
-
"migrations",
|
|
67
|
-
"batches",
|
|
68
|
-
ID.unique(),
|
|
69
|
-
{
|
|
70
|
-
data,
|
|
71
|
-
processed: false,
|
|
72
|
-
}
|
|
73
|
-
);
|
|
74
|
-
return batch.$id;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
export const getAfterImportOperations = async (
|
|
78
|
-
database: Databases,
|
|
79
|
-
collectionId: string
|
|
80
|
-
) => {
|
|
81
|
-
let lastDocumentId: string | undefined;
|
|
82
|
-
const allOperations = [];
|
|
83
|
-
let total = 0;
|
|
84
|
-
|
|
85
|
-
do {
|
|
86
|
-
const query = [
|
|
87
|
-
Query.equal("collectionId", collectionId),
|
|
88
|
-
Query.equal("operationType", "afterImportAction"),
|
|
89
|
-
Query.limit(100),
|
|
90
|
-
];
|
|
91
|
-
|
|
92
|
-
if (lastDocumentId) {
|
|
93
|
-
query.push(Query.cursorAfter(lastDocumentId));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const operations = await database.listDocuments(
|
|
97
|
-
"migrations",
|
|
98
|
-
"currentOperations",
|
|
99
|
-
query
|
|
100
|
-
);
|
|
101
|
-
total = operations.total; // Update total with the latest fetch
|
|
102
|
-
allOperations.push(...operations.documents);
|
|
103
|
-
|
|
104
|
-
if (operations.documents.length > 0 && operations.documents.length >= 100) {
|
|
105
|
-
lastDocumentId =
|
|
106
|
-
operations.documents[operations.documents.length - 1].$id;
|
|
107
|
-
}
|
|
108
|
-
} while (allOperations.length < total);
|
|
109
|
-
|
|
110
|
-
const allOps = allOperations.map((op) => OperationSchema.parse(op));
|
|
111
|
-
return allOps;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
// Legacy function for backward compatibility with old migrations database
|
|
115
|
-
const findOrCreateOperationLegacy = async (
|
|
116
|
-
database: Databases,
|
|
117
|
-
collectionId: string,
|
|
118
|
-
operationType: string,
|
|
119
|
-
additionalQueries?: string[]
|
|
120
|
-
) => {
|
|
121
|
-
const operations = await tryAwaitWithRetry(
|
|
122
|
-
async () =>
|
|
123
|
-
await database.listDocuments("migrations", "currentOperations", [
|
|
124
|
-
Query.equal("collectionId", collectionId),
|
|
125
|
-
Query.equal("operationType", operationType),
|
|
126
|
-
Query.equal("status", "pending"),
|
|
127
|
-
...(additionalQueries || []),
|
|
128
|
-
])
|
|
129
|
-
);
|
|
130
|
-
|
|
131
|
-
if (operations.documents.length > 0) {
|
|
132
|
-
return OperationSchema.parse(operations.documents[0]);
|
|
133
|
-
} else {
|
|
134
|
-
const op = await tryAwaitWithRetry(
|
|
135
|
-
async () =>
|
|
136
|
-
await database.createDocument(
|
|
137
|
-
"migrations",
|
|
138
|
-
"currentOperations",
|
|
139
|
-
ID.unique(),
|
|
140
|
-
{
|
|
141
|
-
operationType,
|
|
142
|
-
collectionId,
|
|
143
|
-
status: "pending",
|
|
144
|
-
batches: [],
|
|
145
|
-
progress: 0,
|
|
146
|
-
total: 0,
|
|
147
|
-
error: "",
|
|
148
|
-
}
|
|
149
|
-
)
|
|
150
|
-
);
|
|
151
|
-
|
|
152
|
-
return OperationSchema.parse(op);
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
export const findOrCreateOperation = async (
|
|
157
|
-
db: DatabaseAdapter,
|
|
158
|
-
databaseId: string,
|
|
159
|
-
operationType: string,
|
|
160
|
-
collectionId?: string,
|
|
161
|
-
data?: any
|
|
162
|
-
): Promise<any> => {
|
|
163
|
-
// Use new operations table system
|
|
164
|
-
return await findOrCreateOp(db, databaseId, operationType, {
|
|
165
|
-
targetTable: collectionId,
|
|
166
|
-
data: data
|
|
167
|
-
});
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
export const updateOperation = async (
|
|
171
|
-
db: DatabaseAdapter,
|
|
172
|
-
databaseId: string,
|
|
173
|
-
operationId: string,
|
|
174
|
-
updates: any
|
|
175
|
-
): Promise<any> => {
|
|
176
|
-
// Use new operations table system
|
|
177
|
-
return await updateOp(db, databaseId, operationId, updates);
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
export const getOperation = async (
|
|
181
|
-
db: DatabaseAdapter,
|
|
182
|
-
databaseId: string,
|
|
183
|
-
operationId: string
|
|
184
|
-
): Promise<any> => {
|
|
185
|
-
// Use new operations table system
|
|
186
|
-
return await getOp(db, databaseId, operationId);
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
// Actual max 1073741824
|
|
190
|
-
export const maxDataLength = 1073741820;
|
|
191
|
-
export const maxBatchItems = 25;
|
|
192
|
-
|
|
193
|
-
export const splitIntoBatches = (data: any[]): any[][] => {
|
|
194
|
-
let batches = [];
|
|
195
|
-
let currentBatch: any[] = [];
|
|
196
|
-
let currentBatchLength = 0;
|
|
197
|
-
let currentBatchItemCount = 0;
|
|
198
|
-
|
|
199
|
-
data.forEach((item, index) => {
|
|
200
|
-
const itemLength = JSON.stringify(item).length;
|
|
201
|
-
if (itemLength > maxDataLength) {
|
|
202
|
-
MessageFormatter.warning(
|
|
203
|
-
`Large item found at index ${index} with length ${itemLength}`,
|
|
204
|
-
{ prefix: "Batch Splitter" }
|
|
205
|
-
);
|
|
206
|
-
logger.debug("Large item data:", item);
|
|
207
|
-
}
|
|
208
|
-
// Check if adding the current item would exceed the max length or max items per batch
|
|
209
|
-
if (
|
|
210
|
-
currentBatchLength + itemLength >= maxDataLength ||
|
|
211
|
-
currentBatchItemCount >= maxBatchItems
|
|
212
|
-
) {
|
|
213
|
-
// If so, start a new batch
|
|
214
|
-
batches.push(currentBatch);
|
|
215
|
-
currentBatch = [item];
|
|
216
|
-
currentBatchLength = itemLength;
|
|
217
|
-
currentBatchItemCount = 1; // Reset item count for the new batch
|
|
218
|
-
} else {
|
|
219
|
-
// Otherwise, add the item to the current batch
|
|
220
|
-
currentBatch.push(item);
|
|
221
|
-
currentBatchLength += itemLength;
|
|
222
|
-
currentBatchItemCount++;
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
// Don't forget to add the last batch if it's not empty
|
|
227
|
-
if (currentBatch.length > 0) {
|
|
228
|
-
batches.push(currentBatch);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
return batches;
|
|
232
|
-
};
|
|
@@ -1,376 +0,0 @@
|
|
|
1
|
-
import { Query, type Databases, type Models } from "node-appwrite";
|
|
2
|
-
import type { Attribute } from "appwrite-utils";
|
|
3
|
-
import { createOrUpdateAttributeWithStatusCheck } from "../collections/columns.js";
|
|
4
|
-
import { fetchAndCacheCollectionByName } from "../collections/methods.js";
|
|
5
|
-
import { tryAwaitWithRetry } from "appwrite-utils-helpers";
|
|
6
|
-
import type { DatabaseAdapter } from "appwrite-utils-helpers";
|
|
7
|
-
import { logger, MessageFormatter } from "appwrite-utils-helpers";
|
|
8
|
-
|
|
9
|
-
export interface QueuedOperation {
|
|
10
|
-
type: "attribute";
|
|
11
|
-
collectionId?: string;
|
|
12
|
-
attribute?: Attribute;
|
|
13
|
-
collection?: Models.Collection;
|
|
14
|
-
dependencies?: string[];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Global state management
|
|
18
|
-
export const queuedOperations: QueuedOperation[] = [];
|
|
19
|
-
export const nameToIdMapping: Map<string, string> = new Map();
|
|
20
|
-
// Keys are scoped per database to avoid cross-database collisions
|
|
21
|
-
// Collections key format: `${databaseId}::${collectionId}`
|
|
22
|
-
// Attributes key format: `${databaseId}::${collectionId}::${attributeKey}`
|
|
23
|
-
export const processedCollections: Set<string> = new Set();
|
|
24
|
-
export const processedAttributes: Set<string> = new Set();
|
|
25
|
-
|
|
26
|
-
// Helpers to build scoped keys
|
|
27
|
-
const collectionKey = (databaseId: string, collectionId: string) => `${databaseId}::${collectionId}`;
|
|
28
|
-
const attributeKeyScoped = (databaseId: string, collectionId: string, key: string) => `${databaseId}::${collectionId}::${key}`;
|
|
29
|
-
|
|
30
|
-
export const enqueueOperation = (operation: QueuedOperation) => {
|
|
31
|
-
// Avoid duplicate queue entries for same attribute
|
|
32
|
-
const attributeKey = operation.attribute?.key;
|
|
33
|
-
const collectionId = operation.collectionId;
|
|
34
|
-
|
|
35
|
-
logger.info('Enqueueing operation', {
|
|
36
|
-
type: operation.type,
|
|
37
|
-
attributeKey,
|
|
38
|
-
collectionId,
|
|
39
|
-
dependencies: operation.dependencies,
|
|
40
|
-
queueSizeBefore: queuedOperations.length,
|
|
41
|
-
operation: 'enqueueOperation'
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
if (attributeKey && collectionId) {
|
|
45
|
-
const duplicateIndex = queuedOperations.findIndex(
|
|
46
|
-
(op) => op.collectionId === collectionId && op.attribute?.key === attributeKey
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
if (duplicateIndex !== -1) {
|
|
50
|
-
MessageFormatter.info(`Replacing existing queue entry for attribute: ${attributeKey}`);
|
|
51
|
-
logger.info('Replacing duplicate queue entry', {
|
|
52
|
-
attributeKey,
|
|
53
|
-
collectionId,
|
|
54
|
-
duplicateIndex,
|
|
55
|
-
operation: 'enqueueOperation'
|
|
56
|
-
});
|
|
57
|
-
queuedOperations[duplicateIndex] = operation;
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
queuedOperations.push(operation);
|
|
63
|
-
logger.debug('Operation enqueued successfully', {
|
|
64
|
-
attributeKey,
|
|
65
|
-
collectionId,
|
|
66
|
-
queueSizeAfter: queuedOperations.length,
|
|
67
|
-
operation: 'enqueueOperation'
|
|
68
|
-
});
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Clear all caches and processing state - use between operations
|
|
73
|
-
*/
|
|
74
|
-
export const clearProcessingState = () => {
|
|
75
|
-
const sizeBefore = {
|
|
76
|
-
collections: processedCollections.size,
|
|
77
|
-
attributes: processedAttributes.size,
|
|
78
|
-
nameMapping: nameToIdMapping.size
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
processedCollections.clear();
|
|
82
|
-
processedAttributes.clear();
|
|
83
|
-
nameToIdMapping.clear();
|
|
84
|
-
|
|
85
|
-
logger.debug("Cleared processing state caches", { operation: "clearProcessingState", sizeBefore });
|
|
86
|
-
logger.info('Processing state cleared', {
|
|
87
|
-
sizeBefore,
|
|
88
|
-
operation: 'clearProcessingState'
|
|
89
|
-
});
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Check if a collection has already been fully processed
|
|
94
|
-
*/
|
|
95
|
-
export const isCollectionProcessed = (collectionId: string, databaseId: string): boolean => {
|
|
96
|
-
return processedCollections.has(collectionKey(databaseId, collectionId));
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Mark a collection as fully processed
|
|
101
|
-
*/
|
|
102
|
-
export const markCollectionProcessed = (collectionId: string, collectionName: string | undefined, databaseId: string) => {
|
|
103
|
-
processedCollections.add(collectionKey(databaseId, collectionId));
|
|
104
|
-
|
|
105
|
-
const logData = {
|
|
106
|
-
databaseId,
|
|
107
|
-
collectionId,
|
|
108
|
-
collectionName,
|
|
109
|
-
totalProcessedCollections: processedCollections.size,
|
|
110
|
-
operation: 'markCollectionProcessed'
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
if (collectionName) {
|
|
114
|
-
MessageFormatter.success(`Marked collection '${collectionName}' (${collectionId}) as processed`, { prefix: 'Tables' });
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
logger.info('Collection marked as processed', logData);
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Check if a specific attribute has been processed
|
|
122
|
-
*/
|
|
123
|
-
export const isAttributeProcessed = (databaseId: string, collectionId: string, attributeKey: string): boolean => {
|
|
124
|
-
return processedAttributes.has(attributeKeyScoped(databaseId, collectionId, attributeKey));
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Mark a specific attribute as processed
|
|
129
|
-
*/
|
|
130
|
-
export const markAttributeProcessed = (databaseId: string, collectionId: string, attributeKey: string) => {
|
|
131
|
-
const identifier = attributeKeyScoped(databaseId, collectionId, attributeKey);
|
|
132
|
-
processedAttributes.add(identifier);
|
|
133
|
-
|
|
134
|
-
logger.debug('Attribute marked as processed', {
|
|
135
|
-
databaseId,
|
|
136
|
-
collectionId,
|
|
137
|
-
attributeKey,
|
|
138
|
-
identifier,
|
|
139
|
-
totalProcessedAttributes: processedAttributes.size,
|
|
140
|
-
operation: 'markAttributeProcessed'
|
|
141
|
-
});
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Process only specific attributes in the queue, not entire collections
|
|
146
|
-
* This prevents triggering full collection re-processing cycles
|
|
147
|
-
*/
|
|
148
|
-
export const processQueue = async (db: Databases | DatabaseAdapter, dbId: string) => {
|
|
149
|
-
const startTime = Date.now();
|
|
150
|
-
|
|
151
|
-
if (queuedOperations.length === 0) {
|
|
152
|
-
MessageFormatter.info("No queued operations to process");
|
|
153
|
-
logger.info('Queue processing skipped - no operations', {
|
|
154
|
-
dbId,
|
|
155
|
-
operation: 'processQueue'
|
|
156
|
-
});
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
MessageFormatter.section(`Starting surgical queue processing of ${queuedOperations.length} operations for ${dbId}`);
|
|
161
|
-
|
|
162
|
-
logger.info('Starting queue processing', {
|
|
163
|
-
dbId,
|
|
164
|
-
queueSize: queuedOperations.length,
|
|
165
|
-
operations: queuedOperations.map(op => ({
|
|
166
|
-
type: op.type,
|
|
167
|
-
attributeKey: op.attribute?.key,
|
|
168
|
-
collectionId: op.collectionId,
|
|
169
|
-
dependencies: op.dependencies
|
|
170
|
-
})),
|
|
171
|
-
operation: 'processQueue'
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
let progress = true;
|
|
175
|
-
let attempts = 0;
|
|
176
|
-
const maxAttempts = 3; // Prevent infinite loops
|
|
177
|
-
|
|
178
|
-
while (progress && attempts < maxAttempts) {
|
|
179
|
-
progress = false;
|
|
180
|
-
attempts++;
|
|
181
|
-
|
|
182
|
-
MessageFormatter.info(`Queue processing attempt ${attempts}/${maxAttempts}`);
|
|
183
|
-
|
|
184
|
-
logger.info('Queue processing attempt started', {
|
|
185
|
-
attempt: attempts,
|
|
186
|
-
maxAttempts,
|
|
187
|
-
remainingOperations: queuedOperations.length,
|
|
188
|
-
dbId,
|
|
189
|
-
operation: 'processQueue'
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
for (let i = queuedOperations.length - 1; i >= 0; i--) {
|
|
193
|
-
const operation = queuedOperations[i];
|
|
194
|
-
|
|
195
|
-
if (!operation.attribute || !operation.collectionId) {
|
|
196
|
-
MessageFormatter.warning("Invalid operation, removing from queue");
|
|
197
|
-
queuedOperations.splice(i, 1);
|
|
198
|
-
continue;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
const attributeKey = operation.attribute.key;
|
|
202
|
-
const collectionId = operation.collectionId;
|
|
203
|
-
|
|
204
|
-
// Skip if this specific attribute was already processed (per database)
|
|
205
|
-
if (isAttributeProcessed(dbId, collectionId, attributeKey)) {
|
|
206
|
-
MessageFormatter.debug(`Attribute '${attributeKey}' already processed, removing from queue`);
|
|
207
|
-
logger.debug('Removing already processed attribute from queue', {
|
|
208
|
-
attributeKey,
|
|
209
|
-
collectionId,
|
|
210
|
-
queueIndex: i,
|
|
211
|
-
operation: 'processQueue'
|
|
212
|
-
});
|
|
213
|
-
queuedOperations.splice(i, 1);
|
|
214
|
-
continue;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
let targetCollection: Models.Collection | undefined;
|
|
218
|
-
|
|
219
|
-
// Resolve the target collection (where the attribute will be created)
|
|
220
|
-
try {
|
|
221
|
-
targetCollection = await tryAwaitWithRetry(
|
|
222
|
-
async () => {
|
|
223
|
-
if ('getMetadata' in db && typeof db.getMetadata === 'function') {
|
|
224
|
-
// DatabaseAdapter
|
|
225
|
-
return (await (db as DatabaseAdapter).getTable({ databaseId: dbId, tableId: collectionId })).data;
|
|
226
|
-
} else {
|
|
227
|
-
// Legacy Databases
|
|
228
|
-
return await (db as Databases).getCollection(dbId, collectionId);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
);
|
|
232
|
-
} catch (e) {
|
|
233
|
-
const errorMessage = e instanceof Error ? e.message : String(e);
|
|
234
|
-
MessageFormatter.error(`Target collection ${collectionId} not found, removing from queue`);
|
|
235
|
-
logger.error('Target collection not found during queue processing', {
|
|
236
|
-
collectionId,
|
|
237
|
-
attributeKey,
|
|
238
|
-
error: errorMessage,
|
|
239
|
-
operation: 'processQueue'
|
|
240
|
-
});
|
|
241
|
-
queuedOperations.splice(i, 1);
|
|
242
|
-
continue;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// For relationship attributes, ensure the related collection exists
|
|
246
|
-
let canProcess = true;
|
|
247
|
-
if (operation.attribute.type === "relationship") {
|
|
248
|
-
const relatedCollection = operation.attribute.relatedCollection;
|
|
249
|
-
if (relatedCollection) {
|
|
250
|
-
// Try to resolve related collection by ID first, then by name
|
|
251
|
-
let relatedFound = false;
|
|
252
|
-
|
|
253
|
-
try {
|
|
254
|
-
await tryAwaitWithRetry(
|
|
255
|
-
async () => {
|
|
256
|
-
if ('getMetadata' in db && typeof db.getMetadata === 'function') {
|
|
257
|
-
// DatabaseAdapter
|
|
258
|
-
return (await (db as DatabaseAdapter).getTable({ databaseId: dbId, tableId: relatedCollection })).data;
|
|
259
|
-
} else {
|
|
260
|
-
// Legacy Databases
|
|
261
|
-
return await (db as Databases).getCollection(dbId, relatedCollection);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
);
|
|
265
|
-
relatedFound = true;
|
|
266
|
-
nameToIdMapping.set(relatedCollection, relatedCollection); // Cache the ID mapping
|
|
267
|
-
} catch (_) {
|
|
268
|
-
// Try by name lookup
|
|
269
|
-
const cachedId = nameToIdMapping.get(relatedCollection);
|
|
270
|
-
if (cachedId) {
|
|
271
|
-
try {
|
|
272
|
-
await tryAwaitWithRetry(
|
|
273
|
-
async () => {
|
|
274
|
-
if ('getMetadata' in db && typeof db.getMetadata === 'function') {
|
|
275
|
-
// DatabaseAdapter
|
|
276
|
-
return (await (db as DatabaseAdapter).getTable({ databaseId: dbId, tableId: cachedId })).data;
|
|
277
|
-
} else {
|
|
278
|
-
// Legacy Databases
|
|
279
|
-
return await (db as Databases).getCollection(dbId, cachedId);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
);
|
|
283
|
-
relatedFound = true;
|
|
284
|
-
} catch (_) {
|
|
285
|
-
nameToIdMapping.delete(relatedCollection); // Remove stale cache
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
if (!relatedFound) {
|
|
290
|
-
// Final attempt: search by name
|
|
291
|
-
try {
|
|
292
|
-
const collections = 'getMetadata' in db && typeof db.getMetadata === 'function'
|
|
293
|
-
? await (db as DatabaseAdapter).listTables({ databaseId: dbId, queries: [Query.equal("name", relatedCollection)] })
|
|
294
|
-
: await (db as Databases).listCollections(dbId, [Query.equal("name", relatedCollection)]);
|
|
295
|
-
|
|
296
|
-
if (collections.total && collections.total > 0) {
|
|
297
|
-
const firstCollection = 'getMetadata' in db && typeof db.getMetadata === 'function'
|
|
298
|
-
? (collections as any).tables?.[0]
|
|
299
|
-
: (collections as any).collections?.[0];
|
|
300
|
-
nameToIdMapping.set(relatedCollection, firstCollection.$id);
|
|
301
|
-
relatedFound = true;
|
|
302
|
-
}
|
|
303
|
-
} catch (_) {
|
|
304
|
-
// Related collection truly doesn't exist yet
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
if (!relatedFound) {
|
|
310
|
-
MessageFormatter.warning(
|
|
311
|
-
`Related collection '${relatedCollection}' not ready for attribute '${attributeKey}', keeping in queue`
|
|
312
|
-
);
|
|
313
|
-
canProcess = false;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
if (canProcess && targetCollection) {
|
|
319
|
-
MessageFormatter.progress(
|
|
320
|
-
`Processing queued ${operation.attribute.type} attribute: '${attributeKey}' for collection: '${targetCollection.name}'`
|
|
321
|
-
);
|
|
322
|
-
|
|
323
|
-
const success = await createOrUpdateAttributeWithStatusCheck(
|
|
324
|
-
db,
|
|
325
|
-
dbId,
|
|
326
|
-
targetCollection,
|
|
327
|
-
operation.attribute
|
|
328
|
-
);
|
|
329
|
-
|
|
330
|
-
if (success) {
|
|
331
|
-
MessageFormatter.success(`Successfully processed queued attribute: '${attributeKey}'`);
|
|
332
|
-
logger.info('Queued attribute processed successfully', {
|
|
333
|
-
attributeKey,
|
|
334
|
-
collectionId,
|
|
335
|
-
targetCollectionName: targetCollection.name,
|
|
336
|
-
operation: 'processQueue'
|
|
337
|
-
});
|
|
338
|
-
markAttributeProcessed(dbId, collectionId, attributeKey);
|
|
339
|
-
queuedOperations.splice(i, 1);
|
|
340
|
-
progress = true;
|
|
341
|
-
} else {
|
|
342
|
-
MessageFormatter.error(`Failed to process queued attribute: '${attributeKey}', removing from queue`);
|
|
343
|
-
logger.error('Failed to process queued attribute', {
|
|
344
|
-
attributeKey,
|
|
345
|
-
collectionId,
|
|
346
|
-
targetCollectionName: targetCollection.name,
|
|
347
|
-
operation: 'processQueue'
|
|
348
|
-
});
|
|
349
|
-
queuedOperations.splice(i, 1);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
if (queuedOperations.length === 0) {
|
|
355
|
-
break;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
MessageFormatter.info(`Remaining operations after attempt ${attempts}: ${queuedOperations.length}`);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
if (queuedOperations.length > 0) {
|
|
362
|
-
MessageFormatter.warning(
|
|
363
|
-
`${queuedOperations.length} operations remain unresolved after ${maxAttempts} attempts:`
|
|
364
|
-
);
|
|
365
|
-
queuedOperations.forEach((op, index) => {
|
|
366
|
-
MessageFormatter.warning(
|
|
367
|
-
` ${index + 1}. ${op.attribute?.type} attribute '${op.attribute?.key}' for collection ${op.collectionId}`
|
|
368
|
-
);
|
|
369
|
-
});
|
|
370
|
-
MessageFormatter.warning("These may have unmet dependencies or require manual intervention");
|
|
371
|
-
} else {
|
|
372
|
-
MessageFormatter.success("All queued operations processed successfully");
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
MessageFormatter.section(`Surgical queue processing complete for ${dbId}`);
|
|
376
|
-
};
|