appwrite-utils-cli 1.5.1 → 1.6.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/CHANGELOG.md +199 -0
- package/README.md +251 -29
- package/dist/adapters/AdapterFactory.d.ts +10 -3
- package/dist/adapters/AdapterFactory.js +213 -17
- package/dist/adapters/TablesDBAdapter.js +60 -17
- 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 +160 -0
- package/dist/cli/commands/databaseCommands.d.ts +13 -0
- package/dist/cli/commands/databaseCommands.js +478 -0
- package/dist/cli/commands/functionCommands.d.ts +7 -0
- package/dist/cli/commands/functionCommands.js +289 -0
- package/dist/cli/commands/schemaCommands.d.ts +7 -0
- package/dist/cli/commands/schemaCommands.js +134 -0
- package/dist/cli/commands/transferCommands.d.ts +5 -0
- package/dist/cli/commands/transferCommands.js +384 -0
- package/dist/collections/attributes.d.ts +5 -4
- package/dist/collections/attributes.js +539 -246
- package/dist/collections/indexes.js +39 -37
- package/dist/collections/methods.d.ts +2 -16
- package/dist/collections/methods.js +90 -538
- package/dist/collections/transferOperations.d.ts +7 -0
- package/dist/collections/transferOperations.js +331 -0
- package/dist/collections/wipeOperations.d.ts +16 -0
- package/dist/collections/wipeOperations.js +328 -0
- package/dist/config/configMigration.d.ts +87 -0
- package/dist/config/configMigration.js +390 -0
- package/dist/config/configValidation.d.ts +66 -0
- package/dist/config/configValidation.js +358 -0
- package/dist/config/yamlConfig.d.ts +455 -1
- package/dist/config/yamlConfig.js +145 -52
- package/dist/databases/methods.js +3 -2
- package/dist/databases/setup.d.ts +1 -2
- package/dist/databases/setup.js +9 -87
- package/dist/examples/yamlTerminologyExample.d.ts +42 -0
- package/dist/examples/yamlTerminologyExample.js +269 -0
- package/dist/functions/deployments.js +11 -10
- package/dist/functions/methods.d.ts +1 -1
- package/dist/functions/methods.js +5 -4
- package/dist/init.js +9 -9
- package/dist/interactiveCLI.d.ts +8 -17
- package/dist/interactiveCLI.js +186 -1171
- package/dist/main.js +364 -21
- package/dist/migrations/afterImportActions.js +22 -30
- package/dist/migrations/appwriteToX.js +71 -25
- package/dist/migrations/dataLoader.js +35 -26
- package/dist/migrations/importController.js +29 -30
- package/dist/migrations/relationships.js +13 -12
- package/dist/migrations/services/ImportOrchestrator.js +16 -19
- package/dist/migrations/transfer.js +46 -46
- package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +3 -1
- package/dist/migrations/yaml/YamlImportConfigLoader.js +6 -3
- package/dist/migrations/yaml/YamlImportIntegration.d.ts +9 -3
- package/dist/migrations/yaml/YamlImportIntegration.js +22 -11
- package/dist/migrations/yaml/generateImportSchemas.d.ts +14 -1
- package/dist/migrations/yaml/generateImportSchemas.js +736 -7
- package/dist/schemas/authUser.d.ts +1 -1
- package/dist/setupController.js +3 -2
- 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.js +15 -15
- package/dist/shared/errorUtils.d.ts +54 -0
- package/dist/shared/errorUtils.js +95 -0
- package/dist/shared/functionManager.js +20 -19
- package/dist/shared/indexManager.js +12 -11
- package/dist/shared/jsonSchemaGenerator.js +10 -26
- package/dist/shared/logging.d.ts +51 -0
- package/dist/shared/logging.js +70 -0
- package/dist/shared/messageFormatter.d.ts +2 -0
- package/dist/shared/messageFormatter.js +10 -0
- package/dist/shared/migrationHelpers.d.ts +6 -16
- package/dist/shared/migrationHelpers.js +24 -21
- package/dist/shared/operationLogger.d.ts +8 -1
- package/dist/shared/operationLogger.js +11 -24
- package/dist/shared/operationQueue.d.ts +28 -1
- package/dist/shared/operationQueue.js +268 -66
- package/dist/shared/operationsTable.d.ts +26 -0
- package/dist/shared/operationsTable.js +286 -0
- package/dist/shared/operationsTableSchema.d.ts +48 -0
- package/dist/shared/operationsTableSchema.js +35 -0
- package/dist/shared/relationshipExtractor.d.ts +56 -0
- package/dist/shared/relationshipExtractor.js +138 -0
- package/dist/shared/schemaGenerator.d.ts +19 -1
- package/dist/shared/schemaGenerator.js +56 -75
- package/dist/storage/backupCompression.d.ts +20 -0
- package/dist/storage/backupCompression.js +67 -0
- package/dist/storage/methods.d.ts +16 -2
- package/dist/storage/methods.js +98 -14
- package/dist/users/methods.js +9 -8
- package/dist/utils/configDiscovery.d.ts +78 -0
- package/dist/utils/configDiscovery.js +430 -0
- package/dist/utils/directoryUtils.d.ts +22 -0
- package/dist/utils/directoryUtils.js +59 -0
- package/dist/utils/getClientFromConfig.d.ts +17 -8
- package/dist/utils/getClientFromConfig.js +162 -17
- package/dist/utils/helperFunctions.d.ts +16 -2
- package/dist/utils/helperFunctions.js +19 -5
- package/dist/utils/loadConfigs.d.ts +34 -9
- package/dist/utils/loadConfigs.js +236 -316
- package/dist/utils/pathResolvers.d.ts +53 -0
- package/dist/utils/pathResolvers.js +72 -0
- package/dist/utils/projectConfig.d.ts +119 -0
- package/dist/utils/projectConfig.js +171 -0
- package/dist/utils/retryFailedPromises.js +4 -2
- package/dist/utils/sessionAuth.d.ts +48 -0
- package/dist/utils/sessionAuth.js +164 -0
- package/dist/utils/sessionPreservationExample.d.ts +1666 -0
- package/dist/utils/sessionPreservationExample.js +101 -0
- package/dist/utils/setupFiles.js +301 -41
- package/dist/utils/typeGuards.d.ts +35 -0
- package/dist/utils/typeGuards.js +57 -0
- package/dist/utils/versionDetection.js +145 -9
- package/dist/utils/yamlConverter.d.ts +53 -3
- package/dist/utils/yamlConverter.js +232 -13
- package/dist/utils/yamlLoader.d.ts +70 -0
- package/dist/utils/yamlLoader.js +263 -0
- package/dist/utilsController.d.ts +36 -3
- package/dist/utilsController.js +186 -56
- package/package.json +12 -2
- package/src/adapters/AdapterFactory.ts +263 -35
- package/src/adapters/TablesDBAdapter.ts +225 -36
- package/src/backups/operations/bucketBackup.ts +277 -0
- package/src/backups/operations/collectionBackup.ts +310 -0
- package/src/backups/operations/comprehensiveBackup.ts +342 -0
- package/src/backups/schemas/bucketManifest.ts +78 -0
- package/src/backups/schemas/comprehensiveManifest.ts +76 -0
- package/src/backups/tracking/centralizedTracking.ts +352 -0
- package/src/cli/commands/configCommands.ts +194 -0
- package/src/cli/commands/databaseCommands.ts +635 -0
- package/src/cli/commands/functionCommands.ts +379 -0
- package/src/cli/commands/schemaCommands.ts +163 -0
- package/src/cli/commands/transferCommands.ts +457 -0
- package/src/collections/attributes.ts +900 -621
- package/src/collections/attributes.ts.backup +1555 -0
- package/src/collections/indexes.ts +116 -114
- package/src/collections/methods.ts +295 -968
- package/src/collections/transferOperations.ts +516 -0
- package/src/collections/wipeOperations.ts +501 -0
- package/src/config/README.md +274 -0
- package/src/config/configMigration.ts +575 -0
- package/src/config/configValidation.ts +445 -0
- package/src/config/yamlConfig.ts +168 -55
- package/src/databases/methods.ts +3 -2
- package/src/databases/setup.ts +11 -138
- package/src/examples/yamlTerminologyExample.ts +341 -0
- package/src/functions/deployments.ts +14 -12
- package/src/functions/methods.ts +11 -11
- package/src/functions/templates/hono-typescript/README.md +286 -0
- package/src/functions/templates/hono-typescript/package.json +26 -0
- package/src/functions/templates/hono-typescript/src/adapters/request.ts +74 -0
- package/src/functions/templates/hono-typescript/src/adapters/response.ts +106 -0
- package/src/functions/templates/hono-typescript/src/app.ts +180 -0
- package/src/functions/templates/hono-typescript/src/context.ts +103 -0
- package/src/functions/templates/hono-typescript/src/index.ts +54 -0
- package/src/functions/templates/hono-typescript/src/middleware/appwrite.ts +119 -0
- package/src/functions/templates/hono-typescript/tsconfig.json +20 -0
- package/src/functions/templates/typescript-node/package.json +2 -1
- package/src/functions/templates/typescript-node/src/context.ts +103 -0
- package/src/functions/templates/typescript-node/src/index.ts +18 -12
- package/src/functions/templates/uv/pyproject.toml +1 -0
- package/src/functions/templates/uv/src/context.py +125 -0
- package/src/functions/templates/uv/src/index.py +35 -5
- package/src/init.ts +9 -11
- package/src/interactiveCLI.ts +276 -1591
- package/src/main.ts +418 -24
- package/src/migrations/afterImportActions.ts +71 -44
- package/src/migrations/appwriteToX.ts +100 -34
- package/src/migrations/dataLoader.ts +48 -34
- package/src/migrations/importController.ts +44 -39
- package/src/migrations/relationships.ts +28 -18
- package/src/migrations/services/ImportOrchestrator.ts +24 -27
- package/src/migrations/transfer.ts +159 -121
- package/src/migrations/yaml/YamlImportConfigLoader.ts +11 -4
- package/src/migrations/yaml/YamlImportIntegration.ts +47 -20
- package/src/migrations/yaml/generateImportSchemas.ts +751 -12
- package/src/setupController.ts +3 -2
- package/src/shared/backupMetadataSchema.ts +93 -0
- package/src/shared/backupTracking.ts +211 -0
- package/src/shared/confirmationDialogs.ts +19 -19
- package/src/shared/errorUtils.ts +110 -0
- package/src/shared/functionManager.ts +21 -20
- package/src/shared/indexManager.ts +12 -11
- package/src/shared/jsonSchemaGenerator.ts +38 -52
- package/src/shared/logging.ts +75 -0
- package/src/shared/messageFormatter.ts +14 -1
- package/src/shared/migrationHelpers.ts +45 -38
- package/src/shared/operationLogger.ts +11 -36
- package/src/shared/operationQueue.ts +322 -93
- package/src/shared/operationsTable.ts +338 -0
- package/src/shared/operationsTableSchema.ts +60 -0
- package/src/shared/relationshipExtractor.ts +214 -0
- package/src/shared/schemaGenerator.ts +179 -219
- package/src/storage/backupCompression.ts +88 -0
- package/src/storage/methods.ts +131 -34
- package/src/users/methods.ts +11 -9
- package/src/utils/configDiscovery.ts +502 -0
- package/src/utils/directoryUtils.ts +61 -0
- package/src/utils/getClientFromConfig.ts +205 -22
- package/src/utils/helperFunctions.ts +23 -5
- package/src/utils/loadConfigs.ts +313 -345
- package/src/utils/pathResolvers.ts +81 -0
- package/src/utils/projectConfig.ts +299 -0
- package/src/utils/retryFailedPromises.ts +4 -2
- package/src/utils/sessionAuth.ts +230 -0
- package/src/utils/setupFiles.ts +322 -54
- package/src/utils/typeGuards.ts +65 -0
- package/src/utils/versionDetection.ts +218 -64
- package/src/utils/yamlConverter.ts +296 -13
- package/src/utils/yamlLoader.ts +364 -0
- package/src/utilsController.ts +314 -110
- package/tests/README.md +497 -0
- package/tests/adapters/AdapterFactory.test.ts +277 -0
- package/tests/integration/syncOperations.test.ts +463 -0
- package/tests/jest.config.js +25 -0
- package/tests/migration/configMigration.test.ts +546 -0
- package/tests/setup.ts +62 -0
- package/tests/testUtils.ts +340 -0
- package/tests/utils/loadConfigs.test.ts +350 -0
- package/tests/validation/configValidation.test.ts +412 -0
- package/src/utils/schemaStrings.ts +0 -517
@@ -0,0 +1,286 @@
|
|
1
|
+
import { logger } from "./logging.js";
|
2
|
+
import { tryAwaitWithRetry } from "../utils/helperFunctions.js";
|
3
|
+
import { Query, ID } from "node-appwrite";
|
4
|
+
import { OPERATIONS_TABLE_ID, OPERATIONS_TABLE_NAME, OperationRecordSchema, } from "./operationsTableSchema.js";
|
5
|
+
/**
|
6
|
+
* Operations Table Manager
|
7
|
+
*
|
8
|
+
* Manages dynamic operations tracking tables in each database.
|
9
|
+
* Creates _appwrite_operations table on-demand for tracking imports, exports, transfers, etc.
|
10
|
+
*/
|
11
|
+
/**
|
12
|
+
* Checks if operations table exists in database
|
13
|
+
*/
|
14
|
+
async function tableExists(db, databaseId) {
|
15
|
+
try {
|
16
|
+
await db.getTable({ databaseId, tableId: OPERATIONS_TABLE_ID });
|
17
|
+
return true;
|
18
|
+
}
|
19
|
+
catch (error) {
|
20
|
+
return false;
|
21
|
+
}
|
22
|
+
}
|
23
|
+
/**
|
24
|
+
* Creates the operations tracking table in the specified database
|
25
|
+
* Table is created with underscore prefix to indicate it's a system table
|
26
|
+
*/
|
27
|
+
export async function createOperationsTable(db, databaseId) {
|
28
|
+
// Check if table already exists
|
29
|
+
const exists = await tableExists(db, databaseId);
|
30
|
+
if (exists) {
|
31
|
+
logger.debug("Operations table already exists", {
|
32
|
+
databaseId,
|
33
|
+
tableId: OPERATIONS_TABLE_ID,
|
34
|
+
});
|
35
|
+
return;
|
36
|
+
}
|
37
|
+
logger.info("Creating operations tracking table", {
|
38
|
+
databaseId,
|
39
|
+
tableId: OPERATIONS_TABLE_ID,
|
40
|
+
});
|
41
|
+
// Create table
|
42
|
+
await tryAwaitWithRetry(async () => {
|
43
|
+
await db.createTable({
|
44
|
+
databaseId,
|
45
|
+
id: OPERATIONS_TABLE_ID,
|
46
|
+
name: OPERATIONS_TABLE_NAME,
|
47
|
+
});
|
48
|
+
});
|
49
|
+
// Create attributes with retry logic
|
50
|
+
const attributes = [
|
51
|
+
{
|
52
|
+
key: "operationType",
|
53
|
+
type: "string",
|
54
|
+
size: 50,
|
55
|
+
required: true,
|
56
|
+
},
|
57
|
+
{
|
58
|
+
key: "targetCollection",
|
59
|
+
type: "string",
|
60
|
+
size: 50,
|
61
|
+
required: false,
|
62
|
+
},
|
63
|
+
{
|
64
|
+
key: "status",
|
65
|
+
type: "enum",
|
66
|
+
elements: ["pending", "in_progress", "completed", "failed", "cancelled"],
|
67
|
+
required: true,
|
68
|
+
default: "pending",
|
69
|
+
},
|
70
|
+
{
|
71
|
+
key: "progress",
|
72
|
+
type: "integer",
|
73
|
+
required: true,
|
74
|
+
default: 0,
|
75
|
+
},
|
76
|
+
{
|
77
|
+
key: "total",
|
78
|
+
type: "integer",
|
79
|
+
required: true,
|
80
|
+
default: 0,
|
81
|
+
},
|
82
|
+
{
|
83
|
+
key: "data",
|
84
|
+
type: "string",
|
85
|
+
size: 1048576, // 1MB for serialized data
|
86
|
+
required: false,
|
87
|
+
},
|
88
|
+
{
|
89
|
+
key: "error",
|
90
|
+
type: "string",
|
91
|
+
size: 10000,
|
92
|
+
required: false,
|
93
|
+
},
|
94
|
+
];
|
95
|
+
for (const attr of attributes) {
|
96
|
+
await tryAwaitWithRetry(async () => {
|
97
|
+
await db.createAttribute({
|
98
|
+
databaseId,
|
99
|
+
tableId: OPERATIONS_TABLE_ID,
|
100
|
+
...attr,
|
101
|
+
});
|
102
|
+
});
|
103
|
+
}
|
104
|
+
logger.info("Operations tracking table created successfully", {
|
105
|
+
databaseId,
|
106
|
+
tableId: OPERATIONS_TABLE_ID,
|
107
|
+
attributes: attributes.length,
|
108
|
+
});
|
109
|
+
}
|
110
|
+
/**
|
111
|
+
* Finds an existing operation or creates a new one
|
112
|
+
* Useful for resuming interrupted operations
|
113
|
+
*/
|
114
|
+
export async function findOrCreateOperation(db, databaseId, operationType, params) {
|
115
|
+
// Ensure operations table exists
|
116
|
+
await createOperationsTable(db, databaseId);
|
117
|
+
// Try to find existing pending operation
|
118
|
+
try {
|
119
|
+
const queries = [
|
120
|
+
Query.equal("operationType", operationType),
|
121
|
+
Query.equal("status", "pending"),
|
122
|
+
];
|
123
|
+
if (params?.targetCollection) {
|
124
|
+
queries.push(Query.equal("targetCollection", params.targetCollection));
|
125
|
+
}
|
126
|
+
const response = await db.listRows({
|
127
|
+
databaseId,
|
128
|
+
tableId: OPERATIONS_TABLE_ID,
|
129
|
+
queries,
|
130
|
+
});
|
131
|
+
if (response.rows && response.rows.length > 0) {
|
132
|
+
logger.debug("Found existing operation", {
|
133
|
+
operationId: response.rows[0].$id,
|
134
|
+
operationType,
|
135
|
+
});
|
136
|
+
return response.rows[0];
|
137
|
+
}
|
138
|
+
}
|
139
|
+
catch (error) {
|
140
|
+
logger.debug("No existing operation found, creating new one", {
|
141
|
+
operationType,
|
142
|
+
error: error instanceof Error ? error.message : String(error),
|
143
|
+
});
|
144
|
+
}
|
145
|
+
// Create new operation
|
146
|
+
const newOperation = {
|
147
|
+
operationType,
|
148
|
+
targetCollection: params?.targetCollection,
|
149
|
+
status: "pending",
|
150
|
+
progress: params?.progress ?? 0,
|
151
|
+
total: params?.total ?? 0,
|
152
|
+
data: params?.data ? JSON.stringify(params.data) : undefined,
|
153
|
+
error: params?.error,
|
154
|
+
};
|
155
|
+
const result = await db.createRow({
|
156
|
+
databaseId,
|
157
|
+
tableId: OPERATIONS_TABLE_ID,
|
158
|
+
id: ID.unique(),
|
159
|
+
data: newOperation,
|
160
|
+
});
|
161
|
+
logger.info("Created new operation", {
|
162
|
+
operationId: result.data?.$id,
|
163
|
+
operationType,
|
164
|
+
});
|
165
|
+
return result.data;
|
166
|
+
}
|
167
|
+
/**
|
168
|
+
* Updates an existing operation record
|
169
|
+
*/
|
170
|
+
export async function updateOperation(db, databaseId, operationId, updates) {
|
171
|
+
// Prepare update data (exclude system fields like $id, $createdAt, $updatedAt)
|
172
|
+
const updateData = {};
|
173
|
+
if (updates.operationType !== undefined)
|
174
|
+
updateData.operationType = updates.operationType;
|
175
|
+
if (updates.targetCollection !== undefined)
|
176
|
+
updateData.targetCollection = updates.targetCollection;
|
177
|
+
if (updates.status !== undefined)
|
178
|
+
updateData.status = updates.status;
|
179
|
+
if (updates.progress !== undefined)
|
180
|
+
updateData.progress = updates.progress;
|
181
|
+
if (updates.total !== undefined)
|
182
|
+
updateData.total = updates.total;
|
183
|
+
if (updates.error !== undefined)
|
184
|
+
updateData.error = updates.error;
|
185
|
+
if (updates.data !== undefined) {
|
186
|
+
updateData.data =
|
187
|
+
typeof updates.data === "string"
|
188
|
+
? updates.data
|
189
|
+
: JSON.stringify(updates.data);
|
190
|
+
}
|
191
|
+
try {
|
192
|
+
const result = await db.updateRow({
|
193
|
+
databaseId,
|
194
|
+
tableId: OPERATIONS_TABLE_ID,
|
195
|
+
id: operationId,
|
196
|
+
data: updateData,
|
197
|
+
});
|
198
|
+
logger.debug("Updated operation", {
|
199
|
+
operationId,
|
200
|
+
updates: Object.keys(updateData),
|
201
|
+
});
|
202
|
+
return result.data;
|
203
|
+
}
|
204
|
+
catch (error) {
|
205
|
+
logger.error("Failed to update operation", {
|
206
|
+
operationId,
|
207
|
+
error: error instanceof Error ? error.message : String(error),
|
208
|
+
});
|
209
|
+
throw error;
|
210
|
+
}
|
211
|
+
}
|
212
|
+
/**
|
213
|
+
* Gets a single operation by ID
|
214
|
+
*/
|
215
|
+
export async function getOperation(db, databaseId, operationId) {
|
216
|
+
try {
|
217
|
+
const result = await db.getRow({
|
218
|
+
databaseId,
|
219
|
+
tableId: OPERATIONS_TABLE_ID,
|
220
|
+
id: operationId,
|
221
|
+
});
|
222
|
+
return result.data;
|
223
|
+
}
|
224
|
+
catch (error) {
|
225
|
+
logger.debug("Operation not found", {
|
226
|
+
operationId,
|
227
|
+
error: error instanceof Error ? error.message : String(error),
|
228
|
+
});
|
229
|
+
return null;
|
230
|
+
}
|
231
|
+
}
|
232
|
+
/**
|
233
|
+
* Cleans up old completed operations
|
234
|
+
* @param olderThan - Optional date, defaults to operations older than 7 days
|
235
|
+
* @returns Number of operations deleted
|
236
|
+
*/
|
237
|
+
export async function cleanupOperations(db, databaseId, olderThan) {
|
238
|
+
const cutoffDate = olderThan || new Date(Date.now() - 7 * 24 * 60 * 60 * 1000); // 7 days ago
|
239
|
+
const cutoffIso = cutoffDate.toISOString();
|
240
|
+
try {
|
241
|
+
// Query for completed operations older than cutoff
|
242
|
+
const response = await db.listRows({
|
243
|
+
databaseId,
|
244
|
+
tableId: OPERATIONS_TABLE_ID,
|
245
|
+
queries: [
|
246
|
+
Query.equal("status", ["completed", "failed", "cancelled"]),
|
247
|
+
Query.lessThan("$createdAt", cutoffIso),
|
248
|
+
],
|
249
|
+
});
|
250
|
+
if (!response.rows || response.rows.length === 0) {
|
251
|
+
logger.debug("No old operations to clean up", { databaseId });
|
252
|
+
return 0;
|
253
|
+
}
|
254
|
+
// Delete in batches
|
255
|
+
let deletedCount = 0;
|
256
|
+
for (const operation of response.rows) {
|
257
|
+
try {
|
258
|
+
await db.deleteRow({
|
259
|
+
databaseId,
|
260
|
+
tableId: OPERATIONS_TABLE_ID,
|
261
|
+
id: operation.$id,
|
262
|
+
});
|
263
|
+
deletedCount++;
|
264
|
+
}
|
265
|
+
catch (error) {
|
266
|
+
logger.warn("Failed to delete operation", {
|
267
|
+
operationId: operation.$id,
|
268
|
+
error: error instanceof Error ? error.message : String(error),
|
269
|
+
});
|
270
|
+
}
|
271
|
+
}
|
272
|
+
logger.info("Cleaned up old operations", {
|
273
|
+
databaseId,
|
274
|
+
deletedCount,
|
275
|
+
cutoffDate: cutoffIso,
|
276
|
+
});
|
277
|
+
return deletedCount;
|
278
|
+
}
|
279
|
+
catch (error) {
|
280
|
+
logger.error("Failed to cleanup operations", {
|
281
|
+
databaseId,
|
282
|
+
error: error instanceof Error ? error.message : String(error),
|
283
|
+
});
|
284
|
+
return 0;
|
285
|
+
}
|
286
|
+
}
|
@@ -0,0 +1,48 @@
|
|
1
|
+
/**
|
2
|
+
* Schema for operation tracking table (_appwrite_operations)
|
3
|
+
*
|
4
|
+
* This table is created dynamically in each database to track long-running operations
|
5
|
+
* like imports, exports, transfers, and backups.
|
6
|
+
*/
|
7
|
+
import { z } from "zod";
|
8
|
+
export interface OperationRecord {
|
9
|
+
$id: string;
|
10
|
+
$createdAt: string;
|
11
|
+
$updatedAt: string;
|
12
|
+
operationType: string;
|
13
|
+
targetCollection?: string;
|
14
|
+
status: OperationStatus;
|
15
|
+
progress: number;
|
16
|
+
total: number;
|
17
|
+
data?: any;
|
18
|
+
error?: string;
|
19
|
+
}
|
20
|
+
export type OperationStatus = 'pending' | 'in_progress' | 'completed' | 'failed' | 'cancelled';
|
21
|
+
export declare const OPERATION_STATUSES: readonly ["pending", "in_progress", "completed", "failed", "cancelled"];
|
22
|
+
export declare const OperationStatusSchema: z.ZodEnum<{
|
23
|
+
pending: "pending";
|
24
|
+
in_progress: "in_progress";
|
25
|
+
completed: "completed";
|
26
|
+
cancelled: "cancelled";
|
27
|
+
failed: "failed";
|
28
|
+
}>;
|
29
|
+
export declare const OperationRecordSchema: z.ZodObject<{
|
30
|
+
$id: z.ZodString;
|
31
|
+
$createdAt: z.ZodString;
|
32
|
+
$updatedAt: z.ZodString;
|
33
|
+
operationType: z.ZodString;
|
34
|
+
targetCollection: z.ZodOptional<z.ZodString>;
|
35
|
+
status: z.ZodEnum<{
|
36
|
+
pending: "pending";
|
37
|
+
in_progress: "in_progress";
|
38
|
+
completed: "completed";
|
39
|
+
cancelled: "cancelled";
|
40
|
+
failed: "failed";
|
41
|
+
}>;
|
42
|
+
progress: z.ZodNumber;
|
43
|
+
total: z.ZodNumber;
|
44
|
+
data: z.ZodOptional<z.ZodAny>;
|
45
|
+
error: z.ZodOptional<z.ZodString>;
|
46
|
+
}, z.core.$strip>;
|
47
|
+
export declare const OPERATIONS_TABLE_ID = "_appwrite_operations";
|
48
|
+
export declare const OPERATIONS_TABLE_NAME = "Operations Tracking";
|
@@ -0,0 +1,35 @@
|
|
1
|
+
/**
|
2
|
+
* Schema for operation tracking table (_appwrite_operations)
|
3
|
+
*
|
4
|
+
* This table is created dynamically in each database to track long-running operations
|
5
|
+
* like imports, exports, transfers, and backups.
|
6
|
+
*/
|
7
|
+
import { z } from "zod";
|
8
|
+
export const OPERATION_STATUSES = [
|
9
|
+
'pending',
|
10
|
+
'in_progress',
|
11
|
+
'completed',
|
12
|
+
'failed',
|
13
|
+
'cancelled'
|
14
|
+
];
|
15
|
+
export const OperationStatusSchema = z.enum([
|
16
|
+
'pending',
|
17
|
+
'in_progress',
|
18
|
+
'completed',
|
19
|
+
'failed',
|
20
|
+
'cancelled'
|
21
|
+
]);
|
22
|
+
export const OperationRecordSchema = z.object({
|
23
|
+
$id: z.string(),
|
24
|
+
$createdAt: z.string(),
|
25
|
+
$updatedAt: z.string(),
|
26
|
+
operationType: z.string(),
|
27
|
+
targetCollection: z.string().optional(),
|
28
|
+
status: OperationStatusSchema,
|
29
|
+
progress: z.number(),
|
30
|
+
total: z.number(),
|
31
|
+
data: z.any().optional(),
|
32
|
+
error: z.string().optional()
|
33
|
+
});
|
34
|
+
export const OPERATIONS_TABLE_ID = "_appwrite_operations";
|
35
|
+
export const OPERATIONS_TABLE_NAME = "Operations Tracking";
|
@@ -0,0 +1,56 @@
|
|
1
|
+
import type { AppwriteConfig, Attribute, RelationshipAttribute } from "appwrite-utils";
|
2
|
+
/**
|
3
|
+
* Represents detailed information about a two-way relationship between collections
|
4
|
+
*/
|
5
|
+
export interface RelationshipDetail {
|
6
|
+
parentCollection: string;
|
7
|
+
childCollection: string;
|
8
|
+
parentKey: string;
|
9
|
+
childKey: string;
|
10
|
+
isArray: boolean;
|
11
|
+
isChild: boolean;
|
12
|
+
}
|
13
|
+
/**
|
14
|
+
* Represents basic relationship information for JSON schema generation
|
15
|
+
*/
|
16
|
+
export interface SimpleRelationship {
|
17
|
+
attributeKey: string;
|
18
|
+
relatedCollection: string;
|
19
|
+
relationType: string;
|
20
|
+
isArray: boolean;
|
21
|
+
}
|
22
|
+
/**
|
23
|
+
* Helper function to resolve collection name from ID or name
|
24
|
+
* @param config - Appwrite configuration containing collections
|
25
|
+
* @param idOrName - Collection ID or name to resolve
|
26
|
+
* @returns Resolved collection name, or the original input if not found
|
27
|
+
*/
|
28
|
+
export declare function resolveCollectionName(config: AppwriteConfig, idOrName: string): string;
|
29
|
+
/**
|
30
|
+
* Determines if a relationship type results in an array
|
31
|
+
* @param relationType - The type of relationship (oneToOne, oneToMany, manyToOne, manyToMany)
|
32
|
+
* @returns true if the relationship results in an array
|
33
|
+
*/
|
34
|
+
export declare function isArrayRelationship(relationType: string): boolean;
|
35
|
+
/**
|
36
|
+
* Extracts two-way relationship details from collections for Zod schema generation
|
37
|
+
* This handles complex bidirectional relationships with parent-child tracking
|
38
|
+
*
|
39
|
+
* @param config - Appwrite configuration containing collections
|
40
|
+
* @returns Map of collection names to their relationship details
|
41
|
+
*/
|
42
|
+
export declare function extractTwoWayRelationships(config: AppwriteConfig): Map<string, RelationshipDetail[]>;
|
43
|
+
/**
|
44
|
+
* Extracts simple relationship information from collections for JSON schema generation
|
45
|
+
* This handles one-way and basic relationship tracking
|
46
|
+
*
|
47
|
+
* @param config - Appwrite configuration containing collections
|
48
|
+
* @returns Map of collection names to their simple relationships
|
49
|
+
*/
|
50
|
+
export declare function extractSimpleRelationships(config: AppwriteConfig): Map<string, SimpleRelationship[]>;
|
51
|
+
/**
|
52
|
+
* Extracts all relationship attributes from a collection's attributes
|
53
|
+
* @param attributes - Array of collection attributes
|
54
|
+
* @returns Array of relationship attributes only
|
55
|
+
*/
|
56
|
+
export declare function filterRelationshipAttributes(attributes: Attribute[]): RelationshipAttribute[];
|
@@ -0,0 +1,138 @@
|
|
1
|
+
/**
|
2
|
+
* Helper function to resolve collection name from ID or name
|
3
|
+
* @param config - Appwrite configuration containing collections
|
4
|
+
* @param idOrName - Collection ID or name to resolve
|
5
|
+
* @returns Resolved collection name, or the original input if not found
|
6
|
+
*/
|
7
|
+
export function resolveCollectionName(config, idOrName) {
|
8
|
+
const col = config.collections?.find((c) => c.$id === idOrName || c.name === idOrName);
|
9
|
+
return col?.name ?? idOrName;
|
10
|
+
}
|
11
|
+
/**
|
12
|
+
* Determines if a relationship type results in an array
|
13
|
+
* @param relationType - The type of relationship (oneToOne, oneToMany, manyToOne, manyToMany)
|
14
|
+
* @returns true if the relationship results in an array
|
15
|
+
*/
|
16
|
+
export function isArrayRelationship(relationType) {
|
17
|
+
return relationType === "oneToMany" || relationType === "manyToMany";
|
18
|
+
}
|
19
|
+
/**
|
20
|
+
* Extracts two-way relationship details from collections for Zod schema generation
|
21
|
+
* This handles complex bidirectional relationships with parent-child tracking
|
22
|
+
*
|
23
|
+
* @param config - Appwrite configuration containing collections
|
24
|
+
* @returns Map of collection names to their relationship details
|
25
|
+
*/
|
26
|
+
export function extractTwoWayRelationships(config) {
|
27
|
+
const relationshipMap = new Map();
|
28
|
+
if (!config.collections) {
|
29
|
+
return relationshipMap;
|
30
|
+
}
|
31
|
+
config.collections.forEach((collection) => {
|
32
|
+
if (!collection.attributes) {
|
33
|
+
return;
|
34
|
+
}
|
35
|
+
collection.attributes.forEach((attr) => {
|
36
|
+
if (attr.type === "relationship" && attr.twoWay && attr.twoWayKey) {
|
37
|
+
const relationshipAttr = attr;
|
38
|
+
let isArrayParent = false;
|
39
|
+
let isArrayChild = false;
|
40
|
+
switch (relationshipAttr.relationType) {
|
41
|
+
case "oneToMany":
|
42
|
+
isArrayParent = true;
|
43
|
+
isArrayChild = false;
|
44
|
+
break;
|
45
|
+
case "manyToMany":
|
46
|
+
isArrayParent = true;
|
47
|
+
isArrayChild = true;
|
48
|
+
break;
|
49
|
+
case "oneToOne":
|
50
|
+
isArrayParent = false;
|
51
|
+
isArrayChild = false;
|
52
|
+
break;
|
53
|
+
case "manyToOne":
|
54
|
+
isArrayParent = false;
|
55
|
+
isArrayChild = true;
|
56
|
+
break;
|
57
|
+
default:
|
58
|
+
break;
|
59
|
+
}
|
60
|
+
const relatedCollectionName = resolveCollectionName(config, relationshipAttr.relatedCollection);
|
61
|
+
addTwoWayRelationship(relationshipMap, collection.name, relatedCollectionName, attr.key, relationshipAttr.twoWayKey, isArrayParent, isArrayChild);
|
62
|
+
console.log(`Extracted relationship: ${attr.key}\n\t${collection.name} -> ${relationshipAttr.relatedCollection}, databaseId: ${collection.databaseId}`);
|
63
|
+
}
|
64
|
+
});
|
65
|
+
});
|
66
|
+
return relationshipMap;
|
67
|
+
}
|
68
|
+
/**
|
69
|
+
* Helper to add two-way relationship details to both parent and child collections
|
70
|
+
* @param relationshipMap - The map to add relationships to
|
71
|
+
* @param parentCollection - Parent collection name
|
72
|
+
* @param childCollection - Child collection name
|
73
|
+
* @param parentKey - Attribute key in parent collection
|
74
|
+
* @param childKey - Attribute key in child collection
|
75
|
+
* @param isArrayParent - Whether parent side is an array
|
76
|
+
* @param isArrayChild - Whether child side is an array
|
77
|
+
*/
|
78
|
+
function addTwoWayRelationship(relationshipMap, parentCollection, childCollection, parentKey, childKey, isArrayParent, isArrayChild) {
|
79
|
+
const relationshipsChild = relationshipMap.get(childCollection) || [];
|
80
|
+
const relationshipsParent = relationshipMap.get(parentCollection) || [];
|
81
|
+
relationshipsParent.push({
|
82
|
+
parentCollection,
|
83
|
+
childCollection,
|
84
|
+
parentKey,
|
85
|
+
childKey,
|
86
|
+
isArray: isArrayParent,
|
87
|
+
isChild: false,
|
88
|
+
});
|
89
|
+
relationshipsChild.push({
|
90
|
+
parentCollection,
|
91
|
+
childCollection,
|
92
|
+
parentKey,
|
93
|
+
childKey,
|
94
|
+
isArray: isArrayChild,
|
95
|
+
isChild: true,
|
96
|
+
});
|
97
|
+
relationshipMap.set(childCollection, relationshipsChild);
|
98
|
+
relationshipMap.set(parentCollection, relationshipsParent);
|
99
|
+
}
|
100
|
+
/**
|
101
|
+
* Extracts simple relationship information from collections for JSON schema generation
|
102
|
+
* This handles one-way and basic relationship tracking
|
103
|
+
*
|
104
|
+
* @param config - Appwrite configuration containing collections
|
105
|
+
* @returns Map of collection names to their simple relationships
|
106
|
+
*/
|
107
|
+
export function extractSimpleRelationships(config) {
|
108
|
+
const relationshipMap = new Map();
|
109
|
+
if (!config.collections) {
|
110
|
+
return relationshipMap;
|
111
|
+
}
|
112
|
+
config.collections.forEach((collection) => {
|
113
|
+
if (!collection.attributes) {
|
114
|
+
return;
|
115
|
+
}
|
116
|
+
collection.attributes.forEach((attr) => {
|
117
|
+
if (attr.type === "relationship" && attr.relatedCollection) {
|
118
|
+
const relationships = relationshipMap.get(collection.name) || [];
|
119
|
+
relationships.push({
|
120
|
+
attributeKey: attr.key,
|
121
|
+
relatedCollection: resolveCollectionName(config, attr.relatedCollection),
|
122
|
+
relationType: attr.relationType || "oneToOne",
|
123
|
+
isArray: isArrayRelationship(attr.relationType || "oneToOne")
|
124
|
+
});
|
125
|
+
relationshipMap.set(collection.name, relationships);
|
126
|
+
}
|
127
|
+
});
|
128
|
+
});
|
129
|
+
return relationshipMap;
|
130
|
+
}
|
131
|
+
/**
|
132
|
+
* Extracts all relationship attributes from a collection's attributes
|
133
|
+
* @param attributes - Array of collection attributes
|
134
|
+
* @returns Array of relationship attributes only
|
135
|
+
*/
|
136
|
+
export function filterRelationshipAttributes(attributes) {
|
137
|
+
return attributes.filter((attr) => attr.type === "relationship");
|
138
|
+
}
|
@@ -7,11 +7,29 @@ export declare class SchemaGenerator {
|
|
7
7
|
private resolveCollectionName;
|
8
8
|
updateYamlCollections(): void;
|
9
9
|
updateTsSchemas(): void;
|
10
|
+
/**
|
11
|
+
* Determines the appropriate directory for collections/tables based on API mode
|
12
|
+
* Uses version detection or config hints to choose between 'collections' and 'tables'
|
13
|
+
*/
|
14
|
+
private getVersionAwareCollectionsDirectory;
|
15
|
+
/**
|
16
|
+
* Get directory for a specific API mode (legacy or tablesdb)
|
17
|
+
* @param apiMode - The API mode to get directory for
|
18
|
+
* @returns The directory name for the specified API mode
|
19
|
+
*/
|
20
|
+
getDirectoryForApiMode(apiMode: 'legacy' | 'tablesdb'): string;
|
21
|
+
/**
|
22
|
+
* Get both directory paths for dual API support
|
23
|
+
* @returns Object with both collectionsDirectory and tablesDirectory paths
|
24
|
+
*/
|
25
|
+
getDualDirectoryConfiguration(): {
|
26
|
+
collectionsDirectory: string;
|
27
|
+
tablesDirectory: string;
|
28
|
+
};
|
10
29
|
updateConfig(config: AppwriteConfig, isYamlConfig?: boolean): Promise<void>;
|
11
30
|
private updateYamlConfig;
|
12
31
|
private updateTypeScriptConfig;
|
13
32
|
private extractRelationships;
|
14
|
-
private addRelationship;
|
15
33
|
generateSchemas(options?: {
|
16
34
|
format?: "zod" | "json" | "both";
|
17
35
|
verbose?: boolean;
|