|
| 1 | +--- |
| 2 | +layout: article |
| 3 | +title: Generate SDK |
| 4 | +description: Generate a type-safe SDK for your Appwrite project using the Command-Line Tool (CLI). Automatically create typed helpers based on your database schema. |
| 5 | +--- |
| 6 | + |
| 7 | +{% partial file="cli-disclaimer.md" /%} |
| 8 | + |
| 9 | +The `generate` command creates a type-safe SDK tailored to your Appwrite project. It reads your database schema and generates typed helpers, so you can interact with your tables using auto-completed methods, resulting in a better developer experience. |
| 10 | + |
| 11 | +# Generate SDK {% #generate-sdk %} |
| 12 | + |
| 13 | +Run the following command in your project directory: |
| 14 | + |
| 15 | +```sh |
| 16 | +appwrite generate |
| 17 | +``` |
| 18 | + |
| 19 | +The CLI automatically detects your project's language and generates the SDK to a `generated/appwrite/` directory. |
| 20 | + |
| 21 | +# Options {% #options %} |
| 22 | + |
| 23 | +{% table %} |
| 24 | +* Option |
| 25 | +* Description |
| 26 | +--- |
| 27 | +* `-o, --output <directory>` |
| 28 | +* Output directory for generated files (default: `"generated"`) |
| 29 | +--- |
| 30 | +* `-l, --language <language>` |
| 31 | +* Target language for SDK generation (supported: `typescript`) |
| 32 | +--- |
| 33 | +* `--server <mode>` |
| 34 | +* Override server-side generation (`auto`|`true`|`false`) (default: `"auto"`) |
| 35 | +--- |
| 36 | +* `-h, --help` |
| 37 | +* Display help for command |
| 38 | +--- |
| 39 | +{% /table %} |
| 40 | + |
| 41 | +# Generated files {% #generated-files %} |
| 42 | + |
| 43 | +The generated SDK includes the following files: |
| 44 | + |
| 45 | +{% table %} |
| 46 | +* File |
| 47 | +* Description |
| 48 | +--- |
| 49 | +* `types.ts` |
| 50 | +* Type definitions based on your database schema. |
| 51 | +--- |
| 52 | +* `databases.ts` |
| 53 | +* Typed database helpers for querying and mutating rows. |
| 54 | +--- |
| 55 | +* `index.ts` |
| 56 | +* Entry point that exports all generated helpers. |
| 57 | +--- |
| 58 | +* `constants.ts` |
| 59 | +* Configuration constants such as your project endpoint and project ID. Update these values before using the SDK. |
| 60 | +--- |
| 61 | +{% /table %} |
| 62 | + |
| 63 | +# Usage {% #usage %} |
| 64 | + |
| 65 | +After generating the SDK, import it into your project: |
| 66 | + |
| 67 | +```ts |
| 68 | +import { databases } from "./generated/appwrite"; |
| 69 | +``` |
| 70 | + |
| 71 | +Configure your SDK constants by setting the values in `./generated/appwrite/constants.ts`. |
| 72 | + |
| 73 | +Use the generated helpers to interact with your tables: |
| 74 | + |
| 75 | +```ts |
| 76 | +const customers = databases.use("main").use("customers"); |
| 77 | + |
| 78 | +const customer = await customers.create({ |
| 79 | + name: "Walter O' Brian", |
| 80 | + email: "walter@example.com" |
| 81 | +}); |
| 82 | +``` |
| 83 | + |
| 84 | +The generated helpers provide auto-completion and type checking based on your database schema, reducing errors and improving developer experience. |
| 85 | + |
| 86 | +# Examples {% #examples %} |
| 87 | + |
| 88 | +The generated SDK supports all common database operations. Below are examples across different use cases. |
| 89 | + |
| 90 | +## Get a row {% #get-row %} |
| 91 | + |
| 92 | +```ts |
| 93 | +const customer = await customers.get("customer-id-123"); |
| 94 | +``` |
| 95 | + |
| 96 | +## List rows with queries {% #list-rows %} |
| 97 | + |
| 98 | +The `list` method accepts a typed query builder that provides auto-completion for your table's columns. |
| 99 | + |
| 100 | +```ts |
| 101 | +const results = await customers.list({ |
| 102 | + queries: (q) => [ |
| 103 | + q.equal("name", "Walter O' Brian"), |
| 104 | + q.orderDesc("$createdAt"), |
| 105 | + q.limit(10) |
| 106 | + ] |
| 107 | +}); |
| 108 | +``` |
| 109 | + |
| 110 | +## Update a row {% #update-row %} |
| 111 | + |
| 112 | +```ts |
| 113 | +await customers.update("customer-id-123", { |
| 114 | + email: "walter@scorpion.com" |
| 115 | +}); |
| 116 | +``` |
| 117 | + |
| 118 | +## Delete a row {% #delete-row %} |
| 119 | + |
| 120 | +```ts |
| 121 | +await customers.delete("customer-id-123"); |
| 122 | +``` |
| 123 | + |
| 124 | +## Bulk operations {% #bulk-operations %} |
| 125 | + |
| 126 | +Create, update, or delete multiple rows at once. |
| 127 | + |
| 128 | +```ts |
| 129 | +await customers.createMany([ |
| 130 | + { name: "Walter O' Brian", email: "walter@example.com" }, |
| 131 | + { name: "Paige Dineen", email: "paige@example.com" } |
| 132 | +]); |
| 133 | +``` |
| 134 | + |
| 135 | +```ts |
| 136 | +await customers.updateMany( |
| 137 | + { email: "updated@example.com" }, |
| 138 | + { |
| 139 | + queries: (q) => [q.equal("name", "Walter O' Brian")] |
| 140 | + } |
| 141 | +); |
| 142 | +``` |
| 143 | + |
| 144 | +```ts |
| 145 | +await customers.deleteMany({ |
| 146 | + queries: (q) => [q.equal("name", "Paige Dineen")] |
| 147 | +}); |
| 148 | +``` |
| 149 | + |
| 150 | +## Permissions {% #permissions %} |
| 151 | + |
| 152 | +Set row-level permissions when creating or updating rows. |
| 153 | + |
| 154 | +```ts |
| 155 | +await customers.create( |
| 156 | + { name: "Walter O' Brian", email: "walter@example.com" }, |
| 157 | + { |
| 158 | + permissions: (permission, role) => [ |
| 159 | + permission.read(role.any()), |
| 160 | + permission.write(role.user("user-id-123")) |
| 161 | + ] |
| 162 | + } |
| 163 | +); |
| 164 | +``` |
0 commit comments