Appwrite uses collections as containers of documents. Each collection contains many documents identical in structure. The terms collections and documents are used because the Appwrite JSON REST API resembles the API of a traditional NoSQL database.
That said, Appwrite is designed to support both SQL and NoSQL database adapters like MariaDB, MySQL, or MongoDB in future versions.
Create collection
You can create collections using the Appwrite Console, a Server SDK, or using the CLI.
You can create a collection by heading to the Databases page, navigate to a database, and click Create collection.
You can also create collections programmatically using a Server SDK. Appwrite Server SDKs require an API key.
const sdk = require('node-appwrite');
// Init SDK
const client = new sdk.Client();
const databases = new sdk.Databases(client);
client
.setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
.setProject('<PROJECT_ID>') // Your project ID
.setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
;
const promise = databases.createCollection('<DATABASE_ID>', '[COLLECTION_ID]', '[NAME]');
promise.then(function (response) {
console.log(response);
}, function (error) {
console.log(error);
});
You can also configure permissions in the createCollection method, learn more about the createCollection in the API references.
Before proceeding
Ensure you install the CLI, log in to your Appwrite account, and initialize your Appwrite project.
To create your collection using the CLI, first use the appwrite init collections command to initialize your collection.
appwrite init collections
Then push your collection using the appwrite push collections command.
appwrite push collections
This will create your collection in the Console with all of your appwrite.json configurations.
Learn more about the CLI collections commands
Permissions
Appwrite uses permissions to control data access. For security, only users that are granted permissions can access a resource. This helps prevent accidental data leaks by forcing you to make more concious decisions around permissions.
By default, Appwrite doesn't grant permissions to any users when a new collection is created. This means users can't create new documents or read, update, and delete existing documents.
Learn about configuring permissions.
Attributes
All documents in a collection follow the same structure. Attributes are used to define the structure of your documents and help the Appwrite's API validate your users' input. Add your first attribute by clicking the Add attribute button.
You can choose between the following types.
Attribute | Description |
string | String attribute. |
integer | Integer attribute. |
float | Float attribute. |
boolean | Boolean attribute. |
datetime | Datetime attribute formatted as an ISO 8601 string. |
enum | Enum attribute. |
ip | IP address attribute for IPv4 and IPv6. |
Email address attribute. | |
url | URL attribute. |
relationship | Relationship attribute relates one collection to another. Learn more about relationships. |
If an attribute must be populated in all documents, set it as required. If not, you may optionally set a default value. Additionally, decide if the attribute should be a single value or an array of values.
If needed, you can change an attribute's key, default value, size (for strings), and whether it is required or not after creation.
You can increase a string attribute's size without any restrictions. When decreasing size, you must ensure that your existing data is less than or equal to the new size, or the operation will fail.
Indexes
Databases use indexes to quickly locate data without having to search through every document for matches. To ensure the best performance, Appwrite recommends an index for every attribute queried. If you plan to query multiple attributes in a single query, creating an index with all queried attributes will yield optimal performance.
The following indexes are currently supported:
Type | Description |
key | Plain Index to allow queries. |
unique | Unique Index to disallow duplicates. |
fulltext | For searching within string attributes. Required for the search query method. |
You can create an index by navigating to your collection's Indexes tab or by using your favorite Server SDK.