Back

[databases] collection/attribute creation delay

  • 0
  • Databases
Francisco "Klogan" Barros
10 Jan, 2024, 23:43

I was wondering what is the strategy employed by Appwrite regarding collection and collection attribute creations via SDK. Consider the code below. I executed this migration 5x and always got an error saying createDocument had an invalid document structure ('designation' attribute does not exist on specified collection). Then, I added the 4s sleep and the migration started to pass.

Does this mean that Appwrite accepts the request and responds with 201 Created before actually writing the collection as a table on the cloud MariaDB instance? If so, could you tell me what is the 99th percentile or even the 95th percentile of elapsed time until an collection attribute is created? E.g.: 99 out of 100 requests, the attribute is created under 500ms.

TypeScript
await db.createCollection(db.id, this.#collectionID, this.#collectionID);

log(`Created collection with ID: ${db.id}`);

// Required Fields
await db.createStringAttribute(db.id, this.#collectionID, 'designation', 32, true);
await db.createStringAttribute(db.id, this.#collectionID, 'friendly', 64, true);

await new Promise((resolve) => setTimeout(resolve, 4000));

// Create fixed documents
await db.createDocument(db.id, this.#collectionID, ID.unique(), {
  designation: 'sms',
  friendly: 'SMS',
});
TL;DR
When creating collections and collection attributes in Appwrite, there may be a delay before they are available. The actual creation of entities in the database is done by a worker. If you encounter an error when trying to create a document immediately after creating a collection or attribute, you can add a delay using `setTimeout` function to wait for the creation to complete. Appwrite does accept the request and respond with a 201 Created status before the collection is written as a table in the database. However, the exact elapsed time for attribute creation can vary. It is recommended to poll for the status of the collection and attribute creation until they are available before
Drake
11 Jan, 2024, 00:25

Does this mean that Appwrite accepts the request and responds with 201 Created before actually writing the collection as a table on the cloud MariaDB instance?

essentially yes. The DB operations go to a worker to create the actual entities in the database: https://github.com/appwrite/appwrite/blob/main/src/Appwrite/Platform/Workers/Databases.php

Drake
11 Jan, 2024, 00:25

You should poll for their status until they're available. That's what the CLI does: https://github.com/appwrite/sdk-for-cli/blob/ae7f9fecbbe5cc0cc702e878fd48cc8559fa18b2/lib/commands/deploy.js#L639-L647

Drake
11 Jan, 2024, 00:26

when it is available depends on the queue

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more