|
| 1 | +# Reliable attribute creation for Appwrite Collections (Windows PowerShell) |
| 2 | +# USAGE: Fill in $apiEndpoint, $projectId, $apiKey, $databaseId, and collection IDs below |
| 3 | +# Run in PowerShell from this script's folder. |
| 4 | + |
| 5 | +$apiEndpoint = "https://YOUR_REGION.cloud.appwrite.io/v1" # Set to your Appwrite API endpoint |
| 6 | +$projectId = "YOUR_PROJECT_ID" |
| 7 | +$apiKey = "YOUR_SECRET_API_KEY" |
| 8 | +$databaseId = "YOUR_DATABASE_ID" |
| 9 | + |
| 10 | +# Map of collection IDs and attribute groups (customize as needed) |
| 11 | +$collections = @{ |
| 12 | + "profiles_collection_id" = @( |
| 13 | + @{ key = "username"; type = "string"; size = 64; required = $true; unique = $true; array = $false }, |
| 14 | + @{ key = "full_name"; type = "string"; size = 128; required = $true; array = $false }, |
| 15 | + @{ key = "email"; type = "email"; required = $true; unique = $true; array = $false }, |
| 16 | + @{ key = "balance"; type = "float"; required = $true; array = $false; default = 0.0 } |
| 17 | + ); |
| 18 | + "transactions_collection_id" = @( |
| 19 | + @{ key = "from_user"; type = "string"; size = 64; required = $true; array = $false }, |
| 20 | + @{ key = "to_user"; type = "string"; size = 64; required = $true; array = $false }, |
| 21 | + @{ key = "amount"; type = "float"; required = $true; array = $false }, |
| 22 | + @{ key = "timestamp"; type = "datetime"; required = $true; array = $false }, |
| 23 | + @{ key = "status"; type = "enum"; required = $true; array = $false; elements = @("pending", "completed", "failed") }, |
| 24 | + @{ key = "reference"; type = "string"; size = 32; required = $false; array = $false } |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +# Helper: attribute type -> endpoint path |
| 29 | +$endpoints = @{ |
| 30 | + "string" = "/attributes/string"; |
| 31 | + "email" = "/attributes/email"; |
| 32 | + "float" = "/attributes/float"; |
| 33 | + "datetime" = "/attributes/datetime"; |
| 34 | + "enum" = "/attributes/enum"; |
| 35 | +} |
| 36 | + |
| 37 | +Write-Host "Starting attribute creation..." |
| 38 | + |
| 39 | +foreach ($collectionId in $collections.Keys) { |
| 40 | + $attributes = $collections[$collectionId] |
| 41 | + foreach ($attr in $attributes) { |
| 42 | + $type = $attr.type |
| 43 | + $endpointSuffix = $endpoints[$type] |
| 44 | + $url = "$apiEndpoint/databases/$databaseId/collections/$collectionId$endpointSuffix" |
| 45 | + |
| 46 | + # Payload creation |
| 47 | + $payload = @{ |
| 48 | + key = $attr.key |
| 49 | + required = $attr.required |
| 50 | + array = $attr.array |
| 51 | + } |
| 52 | + if ($type -eq "string") { $payload.size = $attr.size } |
| 53 | + if ($type -eq "enum") { $payload.elements = $attr.elements } |
| 54 | + if ($type -eq "float" -and $attr.ContainsKey('default')) { $payload.default = $attr.default } |
| 55 | + if ($attr.ContainsKey('unique')) { $payload.unique = $attr.unique } |
| 56 | + |
| 57 | + $payloadJson = $payload | ConvertTo-Json -Compress |
| 58 | + $tmpFile = "_appwriteattr_$($collectionId)_$($attr.key).json" |
| 59 | + Set-Content -Encoding UTF8 -Path $tmpFile -Value $payloadJson |
| 60 | + |
| 61 | + Write-Host "Creating [$($attr.key)] on collection [$collectionId]..." |
| 62 | + $curlArgs = @( |
| 63 | + '-X', 'POST', |
| 64 | + $url, |
| 65 | + '-H', "Content-Type: application/json", |
| 66 | + '-H', "X-Appwrite-Project: $projectId", |
| 67 | + '-H', "X-Appwrite-Key: $apiKey", |
| 68 | + '-d', "@$tmpFile" |
| 69 | + ) |
| 70 | + $result = & curl @curlArgs 2>&1 |
| 71 | + Write-Host $result |
| 72 | + Remove-Item $tmpFile |
| 73 | + Start-Sleep -Seconds 1 # Give Appwrite time between calls, recommended by docs |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +Write-Host "All attributes attempted. Check your Appwrite Console or verify via API." |
0 commit comments