Skip to content

Instantly share code, notes, and snippets.

@byawitz
Last active April 14, 2023 16:33
Show Gist options
  • Save byawitz/157a2e814129f09d547acc54e9341592 to your computer and use it in GitHub Desktop.
Save byawitz/157a2e814129f09d547acc54e9341592 to your computer and use it in GitHub Desktop.
Copy Appwrite collection scheme to a new collection in the same project

Simple script that connected to any Appwrite API SDK through NodeSDK

At the beginning of the file enter the appropriate values into the variables:

  • ENDPOINT
  • PROJECT
  • API_KEY

To copy scheme run the function

copy(ORIGIN_DB_ID, ORIGIN_COLLECTION_ID, DEST_DB_ID, DEST_COLLECTION_ID);
const sdk = require("node-appwrite");
const client = new sdk.Client();
const database = new sdk.Databases(client);
const ENDPOINT = '';
const PROJECT = '';
const API_KEY = '';
async function copy(origDatabaseID, origCollectionID, destDatabaseID, destCollectionID) {
client
.setEndpoint(ENDPOINT)
.setProject(PROJECT)
.setKey(API_KEY)
.setSelfSigned(true);
const attributes = await database.listAttributes(origDatabaseID, origCollectionID);
if (attributes.total > 0) {
for (const attribute of attributes.attributes) {
await createAttribute(attribute, destDatabaseID, destCollectionID);
}
}
const indexes = await database.listIndexes(origDatabaseID, origCollectionID);
if (indexes.total > 0) {
for (const index of indexes.indexes) {
await database.createIndex(destDatabaseID, destCollectionID, index.key, index.type, index.attributes, index.orders);
}
}
}
async function createAttribute(attribute, destDatabaseID, destCollectionID) {
if (attribute.type === 'boolean')
return await database.createBooleanAttribute(destDatabaseID, destCollectionID, attribute.key, attribute.required, attribute.default, attribute.array);
if (attribute.type === 'integer')
return await database.createIntegerAttribute(destDatabaseID, destCollectionID, attribute.key, attribute.required, attribute.default, attribute.array);
if (attribute.type === 'double')
return await database.createFloatAttribute(destDatabaseID, destCollectionID, attribute.key, attribute.required, attribute.min, attribute.max, attribute.default, attribute.array);
if (attribute.format === 'email')
return await database.createEmailAttribute(destDatabaseID, destCollectionID, attribute.key, attribute.required, attribute.default, attribute.array);
if (attribute.format === 'enum')
return await database.createEnumAttribute(destDatabaseID, destCollectionID, attribute.key, attribute.elements, attribute.required, attribute.default, attribute.array);
if (attribute.format === 'ip')
return await database.createIpAttribute(destDatabaseID, destCollectionID, attribute.key, attribute.required, attribute.default, attribute.array);
if (attribute.format === 'url')
return await database.createUrlAttribute(destDatabaseID, destCollectionID, attribute.key, attribute.required, attribute.default, attribute.array);
if (attribute.type === 'string')
return await database.createStringAttribute(destDatabaseID, destCollectionID, attribute.key, attribute.size, attribute.required, attribute.default, attribute.array);
if (attribute.type === 'datetime')
return await database.createDatetimeAttribute(destDatabaseID, destCollectionID, attribute.key, attribute.required, attribute.default, attribute.array);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment