add a "GetCollections" function

This commit is contained in:
2022-08-11 23:06:19 -06:00
commit 5f8526adaf
9 changed files with 543 additions and 0 deletions

View File

@ -0,0 +1,44 @@
const sdk = require("node-appwrite");
/*
'req' variable has:
'headers' - object with request headers
'payload' - object with request body data
'env' - object with environment variables
'res' variable has:
'send(text, status)' - function to return text response. Status code defaults to 200
'json(obj, status)' - function to return JSON response. Status code defaults to 200
If an error is thrown, a response with code 500 will be returned.
*/
module.exports = async function (req, res) {
const client = new sdk.Client();
// You can remove services you don't use
// let account = new sdk.Account(client);
// let avatars = new sdk.Avatars(client);
// let functions = new sdk.Functions(client);
// let health = new sdk.Health(client);
// let storage = new sdk.Storage(client);
// let teams = new sdk.Teams(client);
// let users = new sdk.Users(client);
if (
!req.env['APPWRITE_FUNCTION_ENDPOINT'] ||
!req.env['APPWRITE_FUNCTION_API_KEY']
) {
console.warn("Environment variables are not set. Function cannot use Appwrite SDK.");
throw new Error('Environment not set up');
} else {
client
.setEndpoint(req.env['APPWRITE_FUNCTION_ENDPOINT'])
.setProject(req.env['APPWRITE_FUNCTION_PROJECT_ID'])
.setKey(req.env['APPWRITE_FUNCTION_API_KEY']);
}
let database = new sdk.Databases(client, req.env['DATABASE_ID']);
// let locale = new sdk.Locale(client);
let collections = await database.listCollections();
res.json(collections);
};