|
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); |
|
} |