@njdamstra/appwrite-utils-cli 1.8.9 → 1.10.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.
Files changed (284) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/CONFIG_TODO.md +1189 -0
  3. package/SELECTION_DIALOGS.md +146 -0
  4. package/SERVICE_IMPLEMENTATION_REPORT.md +462 -0
  5. package/dist/adapters/index.d.ts +7 -8
  6. package/dist/adapters/index.js +7 -9
  7. package/dist/backups/operations/bucketBackup.js +2 -2
  8. package/dist/backups/operations/collectionBackup.d.ts +1 -1
  9. package/dist/backups/operations/collectionBackup.js +3 -3
  10. package/dist/backups/operations/comprehensiveBackup.d.ts +1 -1
  11. package/dist/backups/operations/comprehensiveBackup.js +2 -2
  12. package/dist/backups/tracking/centralizedTracking.d.ts +1 -1
  13. package/dist/backups/tracking/centralizedTracking.js +2 -2
  14. package/dist/cli/commands/configCommands.js +51 -7
  15. package/dist/cli/commands/databaseCommands.d.ts +1 -0
  16. package/dist/cli/commands/databaseCommands.js +119 -9
  17. package/dist/cli/commands/functionCommands.js +3 -3
  18. package/dist/cli/commands/importFileCommands.d.ts +7 -0
  19. package/dist/cli/commands/importFileCommands.js +674 -0
  20. package/dist/cli/commands/schemaCommands.js +3 -3
  21. package/dist/cli/commands/storageCommands.js +2 -3
  22. package/dist/cli/commands/transferCommands.js +3 -5
  23. package/dist/collections/attributes.d.ts +1 -1
  24. package/dist/collections/attributes.js +2 -35
  25. package/dist/collections/indexes.js +1 -3
  26. package/dist/collections/methods.d.ts +1 -1
  27. package/dist/collections/methods.js +111 -192
  28. package/dist/collections/tableOperations.d.ts +1 -0
  29. package/dist/collections/tableOperations.js +55 -23
  30. package/dist/collections/transferOperations.d.ts +1 -1
  31. package/dist/collections/transferOperations.js +3 -4
  32. package/dist/collections/wipeOperations.d.ts +4 -3
  33. package/dist/collections/wipeOperations.js +112 -39
  34. package/dist/databases/methods.js +2 -2
  35. package/dist/databases/setup.js +2 -2
  36. package/dist/examples/yamlTerminologyExample.js +2 -2
  37. package/dist/functions/deployments.d.ts +1 -1
  38. package/dist/functions/deployments.js +5 -5
  39. package/dist/functions/fnConfigDiscovery.js +2 -2
  40. package/dist/functions/methods.js +16 -4
  41. package/dist/init.js +1 -1
  42. package/dist/interactiveCLI.d.ts +6 -1
  43. package/dist/interactiveCLI.js +63 -9
  44. package/dist/main.js +130 -177
  45. package/dist/migrations/afterImportActions.js +2 -3
  46. package/dist/migrations/appwriteToX.d.ts +1 -1
  47. package/dist/migrations/appwriteToX.js +9 -7
  48. package/dist/migrations/comprehensiveTransfer.js +3 -5
  49. package/dist/migrations/dataLoader.js +2 -5
  50. package/dist/migrations/importController.js +3 -4
  51. package/dist/migrations/importDataActions.js +3 -3
  52. package/dist/migrations/relationships.js +1 -2
  53. package/dist/migrations/services/DataTransformationService.js +2 -2
  54. package/dist/migrations/services/FileHandlerService.js +1 -1
  55. package/dist/migrations/services/ImportOrchestrator.js +4 -4
  56. package/dist/migrations/services/RateLimitManager.js +1 -1
  57. package/dist/migrations/services/RelationshipResolver.js +1 -1
  58. package/dist/migrations/services/UserMappingService.js +1 -1
  59. package/dist/migrations/services/ValidationService.js +1 -1
  60. package/dist/migrations/transfer.d.ts +8 -4
  61. package/dist/migrations/transfer.js +106 -55
  62. package/dist/migrations/yaml/YamlImportConfigLoader.js +1 -1
  63. package/dist/migrations/yaml/YamlImportIntegration.js +2 -2
  64. package/dist/migrations/yaml/generateImportSchemas.js +1 -1
  65. package/dist/setupCommands.d.ts +1 -1
  66. package/dist/setupCommands.js +5 -6
  67. package/dist/setupController.js +1 -1
  68. package/dist/shared/backupTracking.d.ts +1 -1
  69. package/dist/shared/backupTracking.js +2 -2
  70. package/dist/shared/confirmationDialogs.js +1 -1
  71. package/dist/shared/migrationHelpers.d.ts +1 -1
  72. package/dist/shared/migrationHelpers.js +3 -3
  73. package/dist/shared/operationQueue.d.ts +1 -1
  74. package/dist/shared/operationQueue.js +2 -3
  75. package/dist/shared/operationsTable.d.ts +1 -1
  76. package/dist/shared/operationsTable.js +2 -2
  77. package/dist/shared/progressManager.js +1 -1
  78. package/dist/shared/selectionDialogs.js +9 -8
  79. package/dist/storage/methods.js +4 -4
  80. package/dist/storage/schemas.d.ts +2 -2
  81. package/dist/tables/indexManager.d.ts +65 -0
  82. package/dist/tables/indexManager.js +294 -0
  83. package/dist/types.d.ts +2 -2
  84. package/dist/types.js +1 -1
  85. package/dist/users/methods.js +2 -3
  86. package/dist/utils/configMigration.js +1 -1
  87. package/dist/utils/index.d.ts +1 -1
  88. package/dist/utils/index.js +1 -1
  89. package/dist/utils/loadConfigs.d.ts +2 -2
  90. package/dist/utils/loadConfigs.js +6 -7
  91. package/dist/utils/setupFiles.js +5 -7
  92. package/dist/utilsController.d.ts +15 -8
  93. package/dist/utilsController.js +57 -28
  94. package/package.json +7 -3
  95. package/src/adapters/index.ts +8 -34
  96. package/src/backups/operations/bucketBackup.ts +2 -2
  97. package/src/backups/operations/collectionBackup.ts +4 -4
  98. package/src/backups/operations/comprehensiveBackup.ts +3 -3
  99. package/src/backups/tracking/centralizedTracking.ts +3 -3
  100. package/src/cli/commands/configCommands.ts +72 -8
  101. package/src/cli/commands/databaseCommands.ts +161 -9
  102. package/src/cli/commands/functionCommands.ts +4 -3
  103. package/src/cli/commands/importFileCommands.ts +815 -0
  104. package/src/cli/commands/schemaCommands.ts +3 -3
  105. package/src/cli/commands/storageCommands.ts +2 -3
  106. package/src/cli/commands/transferCommands.ts +3 -6
  107. package/src/collections/attributes.ts +3 -39
  108. package/src/collections/indexes.ts +2 -4
  109. package/src/collections/methods.ts +115 -150
  110. package/src/collections/tableOperations.ts +57 -21
  111. package/src/collections/transferOperations.ts +4 -5
  112. package/src/collections/wipeOperations.ts +154 -51
  113. package/src/databases/methods.ts +2 -2
  114. package/src/databases/setup.ts +2 -2
  115. package/src/examples/yamlTerminologyExample.ts +2 -2
  116. package/src/functions/deployments.ts +6 -5
  117. package/src/functions/fnConfigDiscovery.ts +2 -2
  118. package/src/functions/methods.ts +17 -4
  119. package/src/init.ts +1 -1
  120. package/src/interactiveCLI.ts +75 -10
  121. package/src/main.ts +143 -287
  122. package/src/migrations/afterImportActions.ts +2 -3
  123. package/src/migrations/appwriteToX.ts +12 -8
  124. package/src/migrations/comprehensiveTransfer.ts +6 -6
  125. package/src/migrations/dataLoader.ts +2 -5
  126. package/src/migrations/importController.ts +3 -4
  127. package/src/migrations/importDataActions.ts +3 -3
  128. package/src/migrations/relationships.ts +1 -2
  129. package/src/migrations/services/DataTransformationService.ts +2 -2
  130. package/src/migrations/services/FileHandlerService.ts +1 -1
  131. package/src/migrations/services/ImportOrchestrator.ts +4 -4
  132. package/src/migrations/services/RateLimitManager.ts +1 -1
  133. package/src/migrations/services/RelationshipResolver.ts +1 -1
  134. package/src/migrations/services/UserMappingService.ts +1 -1
  135. package/src/migrations/services/ValidationService.ts +1 -1
  136. package/src/migrations/transfer.ts +126 -83
  137. package/src/migrations/yaml/YamlImportConfigLoader.ts +1 -1
  138. package/src/migrations/yaml/YamlImportIntegration.ts +2 -2
  139. package/src/migrations/yaml/generateImportSchemas.ts +1 -1
  140. package/src/setupCommands.ts +5 -6
  141. package/src/setupController.ts +1 -1
  142. package/src/shared/backupTracking.ts +3 -3
  143. package/src/shared/confirmationDialogs.ts +1 -1
  144. package/src/shared/migrationHelpers.ts +4 -4
  145. package/src/shared/operationQueue.ts +3 -4
  146. package/src/shared/operationsTable.ts +3 -3
  147. package/src/shared/progressManager.ts +1 -1
  148. package/src/shared/selectionDialogs.ts +9 -8
  149. package/src/storage/methods.ts +4 -4
  150. package/src/tables/indexManager.ts +409 -0
  151. package/src/types.ts +2 -2
  152. package/src/users/methods.ts +2 -3
  153. package/src/utils/configMigration.ts +1 -1
  154. package/src/utils/index.ts +1 -1
  155. package/src/utils/loadConfigs.ts +15 -7
  156. package/src/utils/setupFiles.ts +5 -7
  157. package/src/utilsController.ts +86 -32
  158. package/dist/adapters/AdapterFactory.d.ts +0 -94
  159. package/dist/adapters/AdapterFactory.js +0 -405
  160. package/dist/adapters/DatabaseAdapter.d.ts +0 -233
  161. package/dist/adapters/DatabaseAdapter.js +0 -50
  162. package/dist/adapters/LegacyAdapter.d.ts +0 -50
  163. package/dist/adapters/LegacyAdapter.js +0 -612
  164. package/dist/adapters/TablesDBAdapter.d.ts +0 -45
  165. package/dist/adapters/TablesDBAdapter.js +0 -571
  166. package/dist/config/ConfigManager.d.ts +0 -445
  167. package/dist/config/ConfigManager.js +0 -625
  168. package/dist/config/configMigration.d.ts +0 -87
  169. package/dist/config/configMigration.js +0 -390
  170. package/dist/config/configValidation.d.ts +0 -66
  171. package/dist/config/configValidation.js +0 -358
  172. package/dist/config/index.d.ts +0 -8
  173. package/dist/config/index.js +0 -7
  174. package/dist/config/services/ConfigDiscoveryService.d.ts +0 -126
  175. package/dist/config/services/ConfigDiscoveryService.js +0 -374
  176. package/dist/config/services/ConfigLoaderService.d.ts +0 -129
  177. package/dist/config/services/ConfigLoaderService.js +0 -540
  178. package/dist/config/services/ConfigMergeService.d.ts +0 -208
  179. package/dist/config/services/ConfigMergeService.js +0 -308
  180. package/dist/config/services/ConfigValidationService.d.ts +0 -214
  181. package/dist/config/services/ConfigValidationService.js +0 -310
  182. package/dist/config/services/SessionAuthService.d.ts +0 -225
  183. package/dist/config/services/SessionAuthService.js +0 -456
  184. package/dist/config/services/__tests__/ConfigMergeService.test.d.ts +0 -1
  185. package/dist/config/services/__tests__/ConfigMergeService.test.js +0 -271
  186. package/dist/config/services/index.d.ts +0 -13
  187. package/dist/config/services/index.js +0 -10
  188. package/dist/config/yamlConfig.d.ts +0 -722
  189. package/dist/config/yamlConfig.js +0 -702
  190. package/dist/functions/pathResolution.d.ts +0 -37
  191. package/dist/functions/pathResolution.js +0 -185
  192. package/dist/shared/attributeMapper.d.ts +0 -20
  193. package/dist/shared/attributeMapper.js +0 -203
  194. package/dist/shared/errorUtils.d.ts +0 -54
  195. package/dist/shared/errorUtils.js +0 -95
  196. package/dist/shared/functionManager.d.ts +0 -48
  197. package/dist/shared/functionManager.js +0 -336
  198. package/dist/shared/indexManager.d.ts +0 -24
  199. package/dist/shared/indexManager.js +0 -151
  200. package/dist/shared/jsonSchemaGenerator.d.ts +0 -50
  201. package/dist/shared/jsonSchemaGenerator.js +0 -290
  202. package/dist/shared/logging.d.ts +0 -61
  203. package/dist/shared/logging.js +0 -116
  204. package/dist/shared/messageFormatter.d.ts +0 -39
  205. package/dist/shared/messageFormatter.js +0 -162
  206. package/dist/shared/pydanticModelGenerator.d.ts +0 -17
  207. package/dist/shared/pydanticModelGenerator.js +0 -615
  208. package/dist/shared/schemaGenerator.d.ts +0 -40
  209. package/dist/shared/schemaGenerator.js +0 -556
  210. package/dist/utils/ClientFactory.d.ts +0 -87
  211. package/dist/utils/ClientFactory.js +0 -212
  212. package/dist/utils/configDiscovery.d.ts +0 -78
  213. package/dist/utils/configDiscovery.js +0 -472
  214. package/dist/utils/constantsGenerator.d.ts +0 -31
  215. package/dist/utils/constantsGenerator.js +0 -321
  216. package/dist/utils/dataConverters.d.ts +0 -46
  217. package/dist/utils/dataConverters.js +0 -139
  218. package/dist/utils/directoryUtils.d.ts +0 -22
  219. package/dist/utils/directoryUtils.js +0 -59
  220. package/dist/utils/getClientFromConfig.d.ts +0 -39
  221. package/dist/utils/getClientFromConfig.js +0 -199
  222. package/dist/utils/helperFunctions.d.ts +0 -63
  223. package/dist/utils/helperFunctions.js +0 -156
  224. package/dist/utils/pathResolvers.d.ts +0 -53
  225. package/dist/utils/pathResolvers.js +0 -72
  226. package/dist/utils/projectConfig.d.ts +0 -119
  227. package/dist/utils/projectConfig.js +0 -171
  228. package/dist/utils/retryFailedPromises.d.ts +0 -2
  229. package/dist/utils/retryFailedPromises.js +0 -23
  230. package/dist/utils/sessionAuth.d.ts +0 -48
  231. package/dist/utils/sessionAuth.js +0 -164
  232. package/dist/utils/typeGuards.d.ts +0 -35
  233. package/dist/utils/typeGuards.js +0 -57
  234. package/dist/utils/validationRules.d.ts +0 -43
  235. package/dist/utils/validationRules.js +0 -42
  236. package/dist/utils/versionDetection.d.ts +0 -58
  237. package/dist/utils/versionDetection.js +0 -251
  238. package/dist/utils/yamlConverter.d.ts +0 -100
  239. package/dist/utils/yamlConverter.js +0 -428
  240. package/dist/utils/yamlLoader.d.ts +0 -70
  241. package/dist/utils/yamlLoader.js +0 -267
  242. package/src/adapters/AdapterFactory.ts +0 -510
  243. package/src/adapters/DatabaseAdapter.ts +0 -306
  244. package/src/adapters/LegacyAdapter.ts +0 -841
  245. package/src/adapters/TablesDBAdapter.ts +0 -773
  246. package/src/config/ConfigManager.ts +0 -808
  247. package/src/config/README.md +0 -274
  248. package/src/config/configMigration.ts +0 -575
  249. package/src/config/configValidation.ts +0 -445
  250. package/src/config/index.ts +0 -10
  251. package/src/config/services/ConfigDiscoveryService.ts +0 -463
  252. package/src/config/services/ConfigLoaderService.ts +0 -740
  253. package/src/config/services/ConfigMergeService.ts +0 -388
  254. package/src/config/services/ConfigValidationService.ts +0 -394
  255. package/src/config/services/SessionAuthService.ts +0 -565
  256. package/src/config/services/__tests__/ConfigMergeService.test.ts +0 -351
  257. package/src/config/services/index.ts +0 -29
  258. package/src/config/yamlConfig.ts +0 -761
  259. package/src/functions/pathResolution.ts +0 -227
  260. package/src/shared/attributeMapper.ts +0 -229
  261. package/src/shared/errorUtils.ts +0 -110
  262. package/src/shared/functionManager.ts +0 -525
  263. package/src/shared/indexManager.ts +0 -254
  264. package/src/shared/jsonSchemaGenerator.ts +0 -383
  265. package/src/shared/logging.ts +0 -149
  266. package/src/shared/messageFormatter.ts +0 -208
  267. package/src/shared/pydanticModelGenerator.ts +0 -618
  268. package/src/shared/schemaGenerator.ts +0 -644
  269. package/src/utils/ClientFactory.ts +0 -240
  270. package/src/utils/configDiscovery.ts +0 -557
  271. package/src/utils/constantsGenerator.ts +0 -369
  272. package/src/utils/dataConverters.ts +0 -159
  273. package/src/utils/directoryUtils.ts +0 -61
  274. package/src/utils/getClientFromConfig.ts +0 -257
  275. package/src/utils/helperFunctions.ts +0 -228
  276. package/src/utils/pathResolvers.ts +0 -81
  277. package/src/utils/projectConfig.ts +0 -299
  278. package/src/utils/retryFailedPromises.ts +0 -29
  279. package/src/utils/sessionAuth.ts +0 -230
  280. package/src/utils/typeGuards.ts +0 -65
  281. package/src/utils/validationRules.ts +0 -88
  282. package/src/utils/versionDetection.ts +0 -292
  283. package/src/utils/yamlConverter.ts +0 -542
  284. package/src/utils/yamlLoader.ts +0 -371
@@ -1,618 +0,0 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import { MessageFormatter } from './messageFormatter.js';
4
- import type { AppwriteConfig, Attribute } from '@njdamstra/appwrite-utils';
5
-
6
- // Embedded template for base Pydantic model (always written as base.py)
7
- const BASE_PYDANTIC_TEMPLATE = `"""
8
- Appwrite-compatible Pydantic base models for SmartScraper.
9
-
10
- Provides clean base classes for all Appwrite document models without SQLAlchemy dependencies.
11
- """
12
-
13
- import json
14
- from datetime import datetime
15
- from typing import Any, ClassVar
16
-
17
- from pydantic import BaseModel, Field, field_validator
18
-
19
-
20
- class BaseAppwriteModel(BaseModel):
21
- """
22
- Base Appwrite-compatible Pydantic model with field aliases for Appwrite's $ prefixed fields.
23
-
24
- Handles the mapping between Python-compatible field names and Appwrite's $ prefixed fields:
25
- - rid -> $id
26
- - created_at -> $createdAt
27
- - updated_at -> $updatedAt
28
- - permissions -> $permissions
29
- - database_id -> $databaseId
30
- - collection_id -> $collectionId
31
- - sequence -> $sequence
32
- """
33
-
34
- # Optional class-level defaults for database/collection identifiers
35
- databaseId: ClassVar[str | None] = None
36
- collectionId: ClassVar[str | None] = None
37
-
38
- rid: str = Field(..., alias="$id", description="Appwrite document ID")
39
- created_at: datetime = Field(..., alias="$createdAt", description="Document creation timestamp")
40
- updated_at: datetime = Field(
41
- ..., alias="$updatedAt", description="Document last update timestamp"
42
- )
43
- permissions: list[str] = Field(
44
- default_factory=list, alias="$permissions", description="Document permissions"
45
- )
46
- database_id: str = Field(..., alias="$databaseId", description="Appwrite database ID")
47
- collection_id: str = Field(..., alias="$collectionId", description="Appwrite collection ID")
48
- sequence: int | None = Field(None, alias="$sequence", description="Document sequence number")
49
-
50
- class Config:
51
- """Pydantic configuration for Appwrite compatibility"""
52
-
53
- from_attributes = True
54
- populate_by_name = True # Allow both field name and alias
55
- extra = "allow" # Allow additional fields from Appwrite
56
- json_encoders = {datetime: lambda v: v.isoformat() if v else None}
57
-
58
- @field_validator("created_at", "updated_at", mode="before")
59
- @classmethod
60
- def parse_datetime(cls, v: str | datetime) -> datetime:
61
- """Parse datetime from string or return datetime object"""
62
- if isinstance(v, str):
63
- # Handle ISO format with or without microseconds
64
- try:
65
- return datetime.fromisoformat(v.replace("Z", "+00:00"))
66
- except ValueError:
67
- # Fallback for other formats
68
- return datetime.fromisoformat(v)
69
- return v
70
-
71
- def to_appwrite_dict(self) -> dict[str, Any]:
72
- """Convert model to dictionary with Appwrite field names ($ prefixed)"""
73
- return self.model_dump(by_alias=True, exclude_unset=True)
74
-
75
- def to_python_dict(self) -> dict[str, Any]:
76
- """Convert model to dictionary with Python field names (no $ prefix)"""
77
- return self.model_dump(by_alias=False, exclude_unset=True)
78
-
79
- @classmethod
80
- def from_appwrite_document(cls, document: dict[str, Any]):
81
- """Create model instance from Appwrite document with $ prefixed fields"""
82
- return cls.model_validate(document)
83
-
84
- def to_update_payload(self, exclude_unset: bool = True) -> dict[str, Any]:
85
- """Convert model to update payload excluding system fields and None values"""
86
- data = self.model_dump(by_alias=False, exclude_unset=exclude_unset)
87
- return strip_appwrite_keys(data)
88
-
89
-
90
- class CreateBase(BaseModel):
91
- """
92
- Base model for creating documents in Appwrite.
93
- Makes all Appwrite system fields optional since they're auto-generated.
94
- """
95
-
96
- rid: str | None = Field(None, alias="$id", description="Optional custom document ID")
97
- created_at: datetime | None = Field(
98
- None, alias="$createdAt", description="Auto-generated creation timestamp"
99
- )
100
- updated_at: datetime | None = Field(
101
- None, alias="$updatedAt", description="Auto-generated update timestamp"
102
- )
103
- permissions: list[str] | None = Field(
104
- None, alias="$permissions", description="Optional document permissions"
105
- )
106
- database_id: str | None = Field(
107
- None, alias="$databaseId", description="Auto-set database ID"
108
- )
109
- collection_id: str | None = Field(
110
- None, alias="$collectionId", description="Auto-set collection ID"
111
- )
112
- sequence: int | None = Field(
113
- None, alias="$sequence", description="Auto-generated sequence number"
114
- )
115
-
116
- class Config:
117
- """Pydantic configuration for creation payloads"""
118
-
119
- from_attributes = True
120
- populate_by_name = True
121
- extra = "allow"
122
- json_encoders = {datetime: lambda v: v.isoformat() if v else None}
123
-
124
- @field_validator("created_at", "updated_at", mode="before")
125
- @classmethod
126
- def parse_datetime(cls, v: str | datetime | None) -> datetime | None:
127
- """Parse datetime from string or return datetime object"""
128
- if v is None:
129
- return None
130
- if isinstance(v, str):
131
- try:
132
- return datetime.fromisoformat(v.replace("Z", "+00:00"))
133
- except ValueError:
134
- return datetime.fromisoformat(v)
135
- return v
136
-
137
- def strip_appwrite_fields(self) -> dict[str, Any]:
138
- """
139
- Remove Appwrite system fields and return clean data for creation.
140
- Useful when preparing data for Appwrite document creation.
141
- """
142
- excluded_fields = {
143
- "rid",
144
- "$id",
145
- "created_at",
146
- "$createdAt",
147
- "updated_at",
148
- "$updatedAt",
149
- "permissions",
150
- "$permissions",
151
- "database_id",
152
- "$databaseId",
153
- "collection_id",
154
- "$collectionId",
155
- "sequence",
156
- "$sequence",
157
- }
158
-
159
- data = self.model_dump(by_alias=False, exclude_unset=True)
160
- return {k: v for k, v in data.items() if k not in excluded_fields}
161
-
162
-
163
- class UpdateBase(BaseModel):
164
- """
165
- Generic base model for partial updates.
166
- Makes all fields optional for PATCH operations.
167
- """
168
-
169
- class Config:
170
- """Pydantic configuration for update payloads"""
171
-
172
- from_attributes = True
173
- extra = "allow"
174
- json_encoders = {datetime: lambda v: v.isoformat() if v else None}
175
-
176
- def get_update_data(self, exclude_unset: bool = True) -> dict[str, Any]:
177
- """
178
- Get update data excluding None values and optionally unset fields.
179
- Perfect for PATCH operations where only changed fields should be sent.
180
- """
181
- data = self.model_dump(exclude_unset=exclude_unset)
182
- return {k: v for k, v in data.items() if v is not None}
183
-
184
- def get_creation_data(self) -> dict[str, Any]:
185
- """Get clean data for Appwrite document creation"""
186
- return convert_to_create_payload(self)
187
-
188
-
189
- # ============================================================================
190
- # UTILITY FUNCTIONS
191
- # ============================================================================
192
-
193
-
194
- def strip_appwrite_keys(data: dict[str, Any]) -> dict[str, Any]:
195
- """
196
- Remove Appwrite system fields ($ prefixed) from a dictionary.
197
-
198
- Args:
199
- data: Dictionary that may contain Appwrite system fields
200
-
201
- Returns:
202
- Dictionary with Appwrite system fields removed
203
-
204
- Example:
205
- >>> data = {"name": "John", "$id": "123", "$createdAt": "2023-01-01"}
206
- >>> strip_appwrite_keys(data)
207
- {"name": "John"}
208
- """
209
- excluded_keys = {
210
- "$id",
211
- "$createdAt",
212
- "$updatedAt",
213
- "$permissions",
214
- "$databaseId",
215
- "$collectionId",
216
- "$sequence",
217
- }
218
- return {k: v for k, v in data.items() if k not in excluded_keys}
219
-
220
-
221
- def convert_to_create_payload(model_instance: BaseModel) -> dict[str, Any]:
222
- """
223
- Convert any Pydantic model instance to a clean creation payload.
224
- Removes Appwrite system fields and None values.
225
-
226
- Args:
227
- model_instance: Pydantic model instance
228
-
229
- Returns:
230
- Dictionary suitable for Appwrite document creation
231
-
232
- Example:
233
- >>> user = UserModel(name="John", rid="123", created_at=datetime.now())
234
- >>> convert_to_create_payload(user)
235
- {"name": "John"}
236
- """
237
- data = model_instance.model_dump(exclude_unset=True)
238
- # Remove Appwrite system fields and None values
239
- clean_data = strip_appwrite_keys(data)
240
- return {k: v for k, v in clean_data.items() if v is not None}
241
-
242
-
243
- def convert_to_update_payload(
244
- model_instance: BaseModel, exclude_unset: bool = True
245
- ) -> dict[str, Any]:
246
- """
247
- Convert any Pydantic model instance to a clean update payload.
248
- Removes None values and optionally unset fields.
249
-
250
- Args:
251
- model_instance: Pydantic model instance
252
- exclude_unset: Whether to exclude fields that weren't explicitly set
253
-
254
- Returns:
255
- Dictionary suitable for Appwrite document updates
256
-
257
- Example:
258
- >>> user_update = UserUpdateModel(name="Jane")
259
- >>> convert_to_update_payload(user_update)
260
- {"name": "Jane"}
261
- """
262
- data = model_instance.model_dump(exclude_unset=exclude_unset)
263
- return {k: v for k, v in data.items() if v is not None}
264
-
265
-
266
- # ============================================================================
267
- # JSON FIELD HELPER MIXINS
268
- # ============================================================================
269
-
270
-
271
- class JSONFieldMixin:
272
- """
273
- Mixin providing standardized JSON field helper methods.
274
- Use this to add consistent JSON encode/decode patterns to models.
275
- """
276
-
277
- def _encode_json_field(self, data: Any) -> str | None:
278
- """Safely encode data to JSON string"""
279
- if data is None:
280
- return None
281
- try:
282
- return json.dumps(data)
283
- except (TypeError, ValueError):
284
- return None
285
-
286
- def _decode_json_field(self, json_str: str | None, default: Any = None) -> Any:
287
- """Safely decode JSON string to data"""
288
- if not json_str:
289
- return default
290
- try:
291
- return json.loads(json_str)
292
- except (json.JSONDecodeError, TypeError):
293
- return default
294
-
295
- def _decode_json_list(self, json_str: str | None) -> list[Any]:
296
- """Safely decode JSON string to list"""
297
- return self._decode_json_field(json_str, [])
298
-
299
- def _decode_json_dict(self, json_str: str | None) -> dict[str, Any]:
300
- """Safely decode JSON string to dictionary"""
301
- return self._decode_json_field(json_str, {})
302
-
303
-
304
- class TimestampMixin:
305
- """
306
- Mixin providing standardized timestamp handling for business timestamps.
307
- Use this for models that need to handle ISO timestamp strings.
308
- """
309
-
310
- def _set_timestamp(self, date: datetime | None) -> str | None:
311
- """Convert datetime to ISO string"""
312
- return date.isoformat() if date else None
313
-
314
- def _get_timestamp(self, timestamp_str: str | None) -> datetime | None:
315
- """Convert ISO string to datetime"""
316
- if not timestamp_str:
317
- return None
318
- try:
319
- return datetime.fromisoformat(timestamp_str.replace("Z", "+00:00"))
320
- except ValueError:
321
- return None
322
-
323
-
324
- class StringArrayMixin:
325
- """
326
- Mixin providing standardized string array handling for many-to-many relationships.
327
- Use this for models that manage arrays of IDs for relationships.
328
- """
329
-
330
- def _add_to_array(self, array: list[str], item: str) -> None:
331
- """Add item to array if not already present"""
332
- if item not in array:
333
- array.append(item)
334
-
335
- def _remove_from_array(self, array: list[str], item: str) -> None:
336
- """Remove item from array if present"""
337
- if item in array:
338
- array.remove(item)
339
-
340
- def _ensure_array_field(self, field_value: list[str] | None) -> list[str]:
341
- """Ensure field is a list, return empty list if None"""
342
- return field_value or []
343
-
344
-
345
- # ============================================================================
346
- # ENHANCED UTILITY FUNCTIONS
347
- # ============================================================================
348
-
349
-
350
- def safe_json_encode(data: Any) -> str | None:
351
- """
352
- Safely encode any data to JSON string.
353
-
354
- Args:
355
- data: Data to encode
356
-
357
- Returns:
358
- JSON string or None if encoding fails
359
-
360
- Example:
361
- >>> safe_json_encode({"key": "value"})
362
- '{"key": "value"}'
363
- >>> safe_json_encode(None)
364
- None
365
- """
366
- if data is None:
367
- return None
368
- try:
369
- return json.dumps(data)
370
- except (TypeError, ValueError):
371
- return None
372
-
373
-
374
- def safe_json_decode(json_str: str | None, default: Any = None) -> Any:
375
- """
376
- Safely decode JSON string to data.
377
-
378
- Args:
379
- json_str: JSON string to decode
380
- default: Default value if decoding fails
381
-
382
- Returns:
383
- Decoded data or default value
384
-
385
- Example:
386
- >>> safe_json_decode('{"key": "value"}')
387
- {'key': 'value'}
388
- >>> safe_json_decode('invalid', {})
389
- {}
390
- """
391
- if not json_str:
392
- return default
393
- try:
394
- return json.loads(json_str)
395
- except (json.JSONDecodeError, TypeError):
396
- return default
397
-
398
-
399
- def create_json_field_helpers(field_name: str, default_type: type[Any] = dict):
400
- """
401
- Create getter/setter methods for JSON fields.
402
- Useful for dynamically adding JSON field helpers to models.
403
-
404
- Args:
405
- field_name: Name of the JSON field
406
- default_type: Default type for the field (dict or list)
407
-
408
- Returns:
409
- Tuple of (getter, setter) functions
410
-
411
- Example:
412
- >>> get_metadata, set_metadata = create_json_field_helpers('metadata')
413
- >>> # Then add to model class
414
- """
415
- def getter(self) -> Any:
416
- json_str = getattr(self, field_name, None)
417
- default = default_type() if callable(default_type) else default_type
418
- return safe_json_decode(json_str, default)
419
-
420
- def setter(self, value: Any) -> None:
421
- setattr(self, field_name, safe_json_encode(value))
422
-
423
- return getter, setter
424
-
425
-
426
- def validate_appwrite_document(document: dict[str, Any]) -> bool:
427
- """
428
- Validate that a dictionary contains required Appwrite document fields.
429
-
430
- Args:
431
- document: Dictionary to validate
432
-
433
- Returns:
434
- True if valid Appwrite document format
435
-
436
- Example:
437
- >>> doc = {"$id": "123", "$createdAt": "2023-01-01T00:00:00Z", "name": "test"}
438
- >>> validate_appwrite_document(doc)
439
- True
440
- """
441
- required_fields = {"$id", "$createdAt", "$updatedAt"}
442
- return all(field in document for field in required_fields)
443
-
444
-
445
- def batch_prepare_documents(
446
- models: list[BaseModel], batch_size: int = 100
447
- ) -> list[list[dict[str, Any]]]:
448
- """
449
- Prepare model instances for batch creation in Appwrite.
450
- Splits into batches and removes system fields.
451
-
452
- Args:
453
- models: List of Pydantic model instances
454
- batch_size: Maximum documents per batch
455
-
456
- Returns:
457
- List of batches, each containing clean document data
458
-
459
- Example:
460
- >>> users = [CreateUser(name="John"), CreateUser(name="Jane")]
461
- >>> batches = batch_prepare_documents(users, batch_size=1)
462
- >>> len(batches)
463
- 2
464
- """
465
- clean_docs = [convert_to_create_payload(model) for model in models]
466
-
467
- batches = []
468
- for i in range(0, len(clean_docs), batch_size):
469
- batch = clean_docs[i:i + batch_size]
470
- batches.append(batch)
471
-
472
- return batches
473
- `;
474
-
475
- export class PydanticModelGenerator {
476
- constructor(private config: AppwriteConfig, private appwriteFolderPath: string) {}
477
-
478
- generatePydanticModels(options: { baseOutputDirectory: string; verbose?: boolean }) {
479
- const { baseOutputDirectory, verbose = false } = options;
480
- const pyDir = baseOutputDirectory;
481
- if (!fs.existsSync(pyDir)) fs.mkdirSync(pyDir, { recursive: true });
482
-
483
- this.writeBase(pyDir, verbose);
484
-
485
- const collections = this.config.collections || [];
486
- for (const coll of collections) {
487
- const fileName = `${this.toSnake(coll.name)}.py`;
488
- const filePath = path.join(pyDir, fileName);
489
- const code = this.generateModel(coll.name, coll.attributes || []);
490
- fs.writeFileSync(filePath, code, { encoding: 'utf-8' });
491
- if (verbose) MessageFormatter.success(`Pydantic model written to ${filePath}`, { prefix: 'Schema' });
492
- }
493
-
494
- // __init__.py to ease imports
495
- const initPath = path.join(pyDir, '__init__.py');
496
- try {
497
- const exports = (this.config.collections || []).map(c => `from .${this.toSnake(c.name)} import ${this.toPascal(c.name)}`).join('\n');
498
- fs.writeFileSync(initPath, `${exports}\n`, { encoding: 'utf-8' });
499
- } catch {}
500
- }
501
-
502
- private writeBase(pyDir: string, verbose: boolean) {
503
- const basePath = path.join(pyDir, 'base.py');
504
- // Always write embedded template content
505
- fs.writeFileSync(basePath, BASE_PYDANTIC_TEMPLATE, { encoding: 'utf-8' });
506
- if (verbose) MessageFormatter.success(`Base Pydantic model written to ${basePath}`, { prefix: 'Schema' });
507
- }
508
-
509
- private generateModel(name: string, attributes: Attribute[]): string {
510
- const pascal = this.toPascal(name);
511
- const imports = new Set<string>();
512
- imports.add("from .base import BaseAppwriteModel");
513
- const typeImports = new Set<string>();
514
- typeImports.add('from pydantic import Field');
515
- const typingImports = new Set<string>();
516
-
517
- const fields: string[] = [];
518
- for (const attr of attributes) {
519
- if (!attr || !attr.key) continue;
520
- const ann = this.mapAttributeToPythonType(attr, typingImports);
521
- const required = !!(attr as any).required;
522
- const isArray = !!(attr as any).array;
523
- const defaultInitializer = this.defaultInitializer(attr, required, isArray);
524
- fields.push(` ${attr.key}: ${ann}${defaultInitializer}`);
525
- }
526
-
527
- const header = this.composeHeader(imports, typeImports, typingImports);
528
- return `${header}\n\nclass ${pascal}(BaseAppwriteModel):\n${fields.join('\n')}\n`;
529
- }
530
-
531
- private composeHeader(imports: Set<string>, typeImports: Set<string>, typingImports: Set<string>): string {
532
- const lines: string[] = ["from __future__ import annotations"];
533
- lines.push(...Array.from(typeImports));
534
- if (typingImports.size > 0) {
535
- lines.push(`from typing import ${Array.from(typingImports).sort().join(', ')}`);
536
- }
537
- // datetime import if referenced; include by default as safe
538
- lines.push('from datetime import datetime');
539
- lines.push(...Array.from(imports));
540
- return lines.join('\n');
541
- }
542
-
543
- private defaultInitializer(attr: Attribute, required: boolean, isArray: boolean): string {
544
- if (required) return '';
545
- // Optional fields default to None; arrays can be None to distinguish missing vs empty
546
- return ' = None';
547
- }
548
-
549
- private mapAttributeToPythonType(attr: Attribute, typingImports: Set<string>): string {
550
- const t = String((attr as any).type || '').toLowerCase();
551
- const isArray = !!(attr as any).array;
552
- let base: string;
553
- switch (t) {
554
- case 'string':
555
- case 'email':
556
- case 'ip':
557
- case 'url':
558
- base = 'str';
559
- break;
560
- case 'integer':
561
- base = 'int';
562
- break;
563
- case 'double':
564
- case 'float':
565
- base = 'float';
566
- break;
567
- case 'boolean':
568
- base = 'bool';
569
- break;
570
- case 'datetime':
571
- base = 'datetime';
572
- break;
573
- case 'enum': {
574
- const els = Array.isArray((attr as any).elements) ? (attr as any).elements : [];
575
- if (els.length > 0) {
576
- typingImports.add('Literal');
577
- base = `Literal[${els.map((e: string) => `'${e.replace(/'/g, "\\'")}'`).join(', ')}]`;
578
- } else {
579
- base = 'str';
580
- }
581
- break;
582
- }
583
- case 'relationship': {
584
- const relType = (attr as any).relationType || '';
585
- base = (relType === 'oneToMany' || relType === 'manyToMany') ? 'list[str]' : 'str';
586
- break;
587
- }
588
- default:
589
- base = 'str';
590
- break;
591
- }
592
- if (isArray && t !== 'relationship') {
593
- base = `list[${base}]`;
594
- }
595
- const required = !!(attr as any).required;
596
- if (!required) {
597
- base = `${base} | None`;
598
- }
599
- return base;
600
- }
601
-
602
- private toSnake(s: string): string {
603
- return s
604
- .replace(/([a-z0-9])([A-Z])/g, '$1_$2')
605
- .replace(/[^a-zA-Z0-9]+/g, '_')
606
- .replace(/_+/g, '_')
607
- .replace(/^_|_$/g, '')
608
- .toLowerCase();
609
- }
610
- private toPascal(s: string): string {
611
- return s
612
- .replace(/[^a-zA-Z0-9]+/g, ' ')
613
- .split(' ')
614
- .filter(Boolean)
615
- .map(w => w.charAt(0).toUpperCase() + w.slice(1))
616
- .join('');
617
- }
618
- }