├── .github └── ISSUE_TEMPLATE │ ├── bug.yml │ ├── documentation.yaml │ └── feature.yaml ├── .gitignore ├── Dockerfile ├── README.md ├── docker-compose.yml ├── package-lock.json ├── package.json ├── resources ├── code.tar.gz ├── index.js └── nature.jpg └── src └── app.js /.github/ISSUE_TEMPLATE/bug.yml: -------------------------------------------------------------------------------- 1 | name: "🐛 Bug Report" 2 | description: "Submit a bug report to help us improve" 3 | title: "🐛 Bug Report: " 4 | labels: [bug] 5 | body: 6 | - type: markdown 7 | attributes: 8 | value: | 9 | Thanks for taking the time to fill out our bug report form 🙏 10 | - type: textarea 11 | id: steps-to-reproduce 12 | validations: 13 | required: true 14 | attributes: 15 | label: "👟 Reproduction steps" 16 | description: "How do you trigger this bug? Please walk us through it step by step." 17 | placeholder: "When I ..." 18 | - type: textarea 19 | id: expected-behavior 20 | validations: 21 | required: true 22 | attributes: 23 | label: "👍 Expected behavior" 24 | description: "What did you think would happen?" 25 | placeholder: "It should ..." 26 | - type: textarea 27 | id: actual-behavior 28 | validations: 29 | required: true 30 | attributes: 31 | label: "👎 Actual Behavior" 32 | description: "What did actually happen? Add screenshots, if applicable." 33 | placeholder: "It actually ..." 34 | - type: dropdown 35 | id: appwrite-version 36 | attributes: 37 | label: "🎲 Appwrite version" 38 | description: "What version of Appwrite are you running?" 39 | options: 40 | - Version 0.10.x 41 | - Version 0.9.x 42 | - Version 0.8.x 43 | - Version 0.7.x 44 | - Version 0.6.x 45 | - Different version (specify in environment) 46 | validations: 47 | required: true 48 | - type: dropdown 49 | id: operating-system 50 | attributes: 51 | label: "💻 Operating system" 52 | description: "What OS is your server / device running on?" 53 | options: 54 | - Linux 55 | - MacOS 56 | - Windows 57 | - Something else 58 | validations: 59 | required: true 60 | - type: textarea 61 | id: environment 62 | validations: 63 | required: false 64 | attributes: 65 | label: "🧱 Your Environment" 66 | description: "Is your environment customized in any way?" 67 | placeholder: "I use Cloudflare for ..." 68 | - type: checkboxes 69 | id: no-duplicate-issues 70 | attributes: 71 | label: "👀 Have you spent some time to check if this issue has been raised before?" 72 | description: "Have you Googled for a similar issue or checked our older issues for a similar bug?" 73 | options: 74 | - label: "I checked and didn't find similar issue" 75 | required: true 76 | - type: checkboxes 77 | id: read-code-of-conduct 78 | attributes: 79 | label: "🏢 Have you read the Code of Conduct?" 80 | options: 81 | - label: "I have read the [Code of Conduct](https://github.com/appwrite/appwrite/blob/HEAD/CODE_OF_CONDUCT.md)" 82 | required: true -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/documentation.yaml: -------------------------------------------------------------------------------- 1 | name: "📚 Documentation" 2 | description: "Report an issue related to documentation" 3 | title: "📚 Documentation: " 4 | labels: [documentation] 5 | body: 6 | - type: markdown 7 | attributes: 8 | value: | 9 | Thanks for taking the time to make our documentation better 🙏 10 | - type: textarea 11 | id: issue-description 12 | validations: 13 | required: true 14 | attributes: 15 | label: "💭 Description" 16 | description: "A clear and concise description of what the issue is." 17 | placeholder: "Documentation should not ..." 18 | - type: checkboxes 19 | id: no-duplicate-issues 20 | attributes: 21 | label: "👀 Have you spent some time to check if this issue has been raised before?" 22 | description: "Have you Googled for a similar issue or checked our older issues for a similar bug?" 23 | options: 24 | - label: "I checked and didn't find similar issue" 25 | required: true 26 | - type: checkboxes 27 | id: read-code-of-conduct 28 | attributes: 29 | label: "🏢 Have you read the Code of Conduct?" 30 | options: 31 | - label: "I have read the [Code of Conduct](https://github.com/appwrite/appwrite/blob/HEAD/CODE_OF_CONDUCT.md)" 32 | required: true -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature.yaml: -------------------------------------------------------------------------------- 1 | name: 🚀 Feature 2 | description: "Submit a proposal for a new feature" 3 | title: "🚀 Feature: " 4 | labels: [feature] 5 | body: 6 | - type: markdown 7 | attributes: 8 | value: | 9 | Thanks for taking the time to fill out our feature request form 🙏 10 | - type: textarea 11 | id: feature-description 12 | validations: 13 | required: true 14 | attributes: 15 | label: "🔖 Feature description" 16 | description: "A clear and concise description of what the feature is." 17 | placeholder: "You should add ..." 18 | - type: textarea 19 | id: pitch 20 | validations: 21 | required: true 22 | attributes: 23 | label: "🎤 Pitch" 24 | description: "Please explain why this feature should be implemented and how it would be used. Add examples, if applicable." 25 | placeholder: "In my use-case, ..." 26 | - type: checkboxes 27 | id: no-duplicate-issues 28 | attributes: 29 | label: "👀 Have you spent some time to check if this issue has been raised before?" 30 | description: "Have you Googled for a similar issue or checked our older issues for a similar bug?" 31 | options: 32 | - label: "I checked and didn't find similar issue" 33 | required: true 34 | - type: checkboxes 35 | id: read-code-of-conduct 36 | attributes: 37 | label: "🏢 Have you read the Code of Conduct?" 38 | options: 39 | - label: "I have read the [Code of Conduct](https://github.com/appwrite/appwrite/blob/HEAD/CODE_OF_CONDUCT.md)" 40 | required: true -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .idea 3 | **.DS_Store 4 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:16.0-alpine 2 | WORKDIR /app 3 | COPY . . 4 | CMD npm install 5 | CMD node src/app.js -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Appwrite's Node.js Playground 🎮 2 | 3 | Appwrite playground is a simple way to explore the Appwrite API & Appwrite Node.js SDK. Use the source code of this repository to learn how to use the different Appwrite Node.js SDK features. 4 | 5 | [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) 6 | 7 | **Work in progress** 8 | 9 | ## Get Started 10 | 11 | This playground doesn't include any NodeJS best practices but rather intended to show some of the most simple examples and use cases of using the Appwrite API in your NodeJS application. 12 | 13 | ## Requirements 14 | * A system with NodeJS or Docker installed. 15 | * An Appwrite instance. 16 | * An Appwrite project created in the console. 17 | * An Appwrite API key created in the console. 18 | 19 | ### Installation 20 | 21 | 1. Clone this repository. 22 | 2. `cd` into the repository. 23 | 3. Open the `app.js` file found in the root of the cloned repository. 24 | 4. Copy Project ID, endpoint and API key from Appwrite console into `app.js` 25 | 5. Run the playground: 26 | NodeJS: 27 | - Install dependencies `npm install` 28 | - Execute the command `node app.js` 29 | Docker: 30 | - Execute the command `docker compose up` 31 | 6. You will see the JSON response in the console. 32 | 33 | ### API's Covered 34 | 35 | - Databse 36 | * Create Collection 37 | * List Collections 38 | * Delete Collection 39 | * Create Document 40 | * List Documents 41 | * Delete Document 42 | 43 | - Storage 44 | * Create Bucket 45 | * List Buckets 46 | * Delete Bucket 47 | * Upload File 48 | * List Files 49 | * Delete File 50 | 51 | - Users 52 | * Create User 53 | * List Users 54 | * Delete User 55 | 56 | - Functions 57 | * Create Function 58 | * List Functions 59 | * Delete Function 60 | * Upload Deployment 61 | * Execute function (sync) 62 | * Execute function (async) 63 | 64 | ## Contributing 65 | 66 | All code contributions - including those of people having commit access - must go through a pull request and approved by a core developer before being merged. This is to ensure proper review of all the code. 67 | 68 | We truly ❤️ pull requests! If you wish to help, you can learn more about how you can contribute to this project in the [contribution guide](https://github.com/appwrite/appwrite/blob/master/CONTRIBUTING.md). 69 | 70 | ## Security 71 | 72 | For security issues, kindly email us [security@appwrite.io](mailto:security@appwrite.io) instead of posting a public issue in GitHub. 73 | 74 | ## Follow Us 75 | 76 | Join our growing community around the world! Follow us on [Twitter](https://twitter.com/appwrite), [Facebook Page](https://www.facebook.com/appwrite.io), [Facebook Group](https://www.facebook.com/groups/appwrite.developers/) or join our [Discord Server](https://appwrite.io/discord) for more help, ideas and discussions. 77 | 78 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.9' 2 | 3 | services: 4 | playground-for-deno: 5 | build: . 6 | volumes: 7 | - "./:/app" 8 | working_dir: /app 9 | command: "node src/app.js" -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "playground-for-node", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "playground-for-node", 9 | "version": "1.0.0", 10 | "license": "BSD-3-Clause", 11 | "dependencies": { 12 | "chalk": "^4.1.0", 13 | "node-appwrite": "14.0.0" 14 | } 15 | }, 16 | "node_modules/ansi-styles": { 17 | "version": "4.3.0", 18 | "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", 19 | "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", 20 | "dependencies": { 21 | "color-convert": "^2.0.1" 22 | }, 23 | "engines": { 24 | "node": ">=8" 25 | }, 26 | "funding": { 27 | "url": "https://github.com/chalk/ansi-styles?sponsor=1" 28 | } 29 | }, 30 | "node_modules/chalk": { 31 | "version": "4.1.2", 32 | "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", 33 | "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", 34 | "dependencies": { 35 | "ansi-styles": "^4.1.0", 36 | "supports-color": "^7.1.0" 37 | }, 38 | "engines": { 39 | "node": ">=10" 40 | }, 41 | "funding": { 42 | "url": "https://github.com/chalk/chalk?sponsor=1" 43 | } 44 | }, 45 | "node_modules/color-convert": { 46 | "version": "2.0.1", 47 | "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", 48 | "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", 49 | "dependencies": { 50 | "color-name": "~1.1.4" 51 | }, 52 | "engines": { 53 | "node": ">=7.0.0" 54 | } 55 | }, 56 | "node_modules/color-name": { 57 | "version": "1.1.4", 58 | "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", 59 | "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" 60 | }, 61 | "node_modules/has-flag": { 62 | "version": "4.0.0", 63 | "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", 64 | "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", 65 | "engines": { 66 | "node": ">=8" 67 | } 68 | }, 69 | "node_modules/node-appwrite": { 70 | "version": "14.0.0", 71 | "resolved": "https://registry.npmjs.org/node-appwrite/-/node-appwrite-14.0.0.tgz", 72 | "integrity": "sha512-pPynEAmhep1rNHpv2UqsZfM7FbtDX7y+nRRZS/IP5CMDznMu8PBqzFD2WbbQIvVCv3MEDRY5xpXbPfyxbysCPQ==", 73 | "dependencies": { 74 | "node-fetch-native-with-agent": "1.7.2" 75 | } 76 | }, 77 | "node_modules/node-fetch-native-with-agent": { 78 | "version": "1.7.2", 79 | "resolved": "https://registry.npmjs.org/node-fetch-native-with-agent/-/node-fetch-native-with-agent-1.7.2.tgz", 80 | "integrity": "sha512-5MaOOCuJEvcckoz7/tjdx1M6OusOY6Xc5f459IaruGStWnKzlI1qpNgaAwmn4LmFYcsSlj+jBMk84wmmRxfk5g==" 81 | }, 82 | "node_modules/supports-color": { 83 | "version": "7.2.0", 84 | "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", 85 | "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", 86 | "dependencies": { 87 | "has-flag": "^4.0.0" 88 | }, 89 | "engines": { 90 | "node": ">=8" 91 | } 92 | } 93 | }, 94 | "dependencies": { 95 | "ansi-styles": { 96 | "version": "4.3.0", 97 | "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", 98 | "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", 99 | "requires": { 100 | "color-convert": "^2.0.1" 101 | } 102 | }, 103 | "chalk": { 104 | "version": "4.1.2", 105 | "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", 106 | "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", 107 | "requires": { 108 | "ansi-styles": "^4.1.0", 109 | "supports-color": "^7.1.0" 110 | } 111 | }, 112 | "color-convert": { 113 | "version": "2.0.1", 114 | "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", 115 | "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", 116 | "requires": { 117 | "color-name": "~1.1.4" 118 | } 119 | }, 120 | "color-name": { 121 | "version": "1.1.4", 122 | "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", 123 | "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" 124 | }, 125 | "has-flag": { 126 | "version": "4.0.0", 127 | "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", 128 | "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" 129 | }, 130 | "node-appwrite": { 131 | "version": "14.0.0", 132 | "resolved": "https://registry.npmjs.org/node-appwrite/-/node-appwrite-14.0.0.tgz", 133 | "integrity": "sha512-pPynEAmhep1rNHpv2UqsZfM7FbtDX7y+nRRZS/IP5CMDznMu8PBqzFD2WbbQIvVCv3MEDRY5xpXbPfyxbysCPQ==", 134 | "requires": { 135 | "node-fetch-native-with-agent": "1.7.2" 136 | } 137 | }, 138 | "node-fetch-native-with-agent": { 139 | "version": "1.7.2", 140 | "resolved": "https://registry.npmjs.org/node-fetch-native-with-agent/-/node-fetch-native-with-agent-1.7.2.tgz", 141 | "integrity": "sha512-5MaOOCuJEvcckoz7/tjdx1M6OusOY6Xc5f459IaruGStWnKzlI1qpNgaAwmn4LmFYcsSlj+jBMk84wmmRxfk5g==" 142 | }, 143 | "supports-color": { 144 | "version": "7.2.0", 145 | "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", 146 | "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", 147 | "requires": { 148 | "has-flag": "^4.0.0" 149 | } 150 | } 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "playground-for-node", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "repository": "https://github.com/appwrite/playground-for-node.git", 6 | "license": "BSD-3-Clause", 7 | "scripts": { 8 | "start": "node src/app.js", 9 | "test": "echo \"Error: no test specified\" && exit 1" 10 | }, 11 | "dependencies": { 12 | "chalk": "^4.1.0", 13 | "node-appwrite": "14.0.0" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /resources/code.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/appwrite/playground-for-node/dd3023f042d464be3c4a5811449d18bb2fdd0fff/resources/code.tar.gz -------------------------------------------------------------------------------- /resources/index.js: -------------------------------------------------------------------------------- 1 | module.exports = async ({req, res}) => { 2 | return res.json({ 3 | message: "Hello Open Runtimes 👋" 4 | }); 5 | } 6 | -------------------------------------------------------------------------------- /resources/nature.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/appwrite/playground-for-node/dd3023f042d464be3c4a5811449d18bb2fdd0fff/resources/nature.jpg -------------------------------------------------------------------------------- /src/app.js: -------------------------------------------------------------------------------- 1 | const { Client, Databases, Functions, Account, Users, Storage, Query, Permission, Role, ID } = require('node-appwrite'); 2 | const chalk = require('chalk'); 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | const { InputFile } = require('node-appwrite/file') 6 | 7 | // Config 8 | const client = new Client() 9 | .setEndpoint('YOUR_ENDPOINT') // Replace with your endpoint 10 | .setProject('YOUR_PROJECT_ID') // Replace with your project ID 11 | .setKey('YOUR_API_KEY'); // Replace with your API Key 12 | //.setJWT('jwt'); // Use this to authenticate with JWT generated from Client SDK 13 | 14 | const databases = new Databases(client); 15 | const functions = new Functions(client); 16 | const storage = new Storage(client); 17 | const users = new Users(client); 18 | const account = new Account(client); 19 | 20 | let databaseId; 21 | let collectionId; 22 | let documentId; 23 | let userId; 24 | let bucketId; 25 | let fileId; 26 | let functionId; 27 | let executionId; 28 | 29 | // List of API Definitions 30 | const createDatabase = async () => { 31 | console.log(chalk.greenBright('Running Create Database API')); 32 | 33 | const response = await databases.create(ID.unique(), "Default"); 34 | 35 | databaseId = response.$id; 36 | 37 | console.log(response); 38 | } 39 | 40 | const listDatabases = async () => { 41 | console.log(chalk.greenBright("Running List Databases API")); 42 | 43 | const response = await databases.list(); 44 | 45 | console.log(response); 46 | } 47 | 48 | const getDatabase = async () => { 49 | console.log(chalk.greenBright("Running Get Database API")); 50 | 51 | const response = await databases.get(databaseId); 52 | 53 | console.log(response); 54 | } 55 | 56 | const updateDatabase = async () => { 57 | console.log(chalk.greenBright('Running Update Database API')); 58 | 59 | const response = await databases.update( 60 | databaseId, 61 | "Updated Database" 62 | ); 63 | 64 | console.log(response); 65 | } 66 | 67 | const deleteDatabase = async () => { 68 | console.log(chalk.greenBright("Running Delete Database API")); 69 | 70 | const response = await databases.delete(databaseId); 71 | 72 | console.log(response); 73 | } 74 | 75 | const createCollection = async () => { 76 | console.log(chalk.greenBright('Running Create Collection API')); 77 | 78 | const response = await databases.createCollection( 79 | databaseId, // ID of the collection 80 | ID.unique(), // Collection ID 81 | "Collection", // Collection Name 82 | [ 83 | Permission.read(Role.any()), 84 | Permission.create(Role.users()), 85 | Permission.update(Role.users()), 86 | Permission.delete(Role.users()), 87 | ] 88 | ); 89 | 90 | collectionId = response.$id; 91 | console.log(response); 92 | 93 | const nameAttributeResponse = await databases.createStringAttribute( 94 | databaseId, 95 | collectionId, 96 | 'name', 97 | 255, 98 | false, 99 | "Empty Name", 100 | false 101 | ); 102 | console.log(nameAttributeResponse); 103 | 104 | const yearAttributeResponse = await databases.createIntegerAttribute( 105 | databaseId, 106 | collectionId, 107 | 'release_year', 108 | false, 109 | 0, 5000, 110 | 1970, 111 | false 112 | ); 113 | console.log(yearAttributeResponse); 114 | 115 | console.log("Waiting a little to ensure attributes are created ..."); 116 | await new Promise((resolve) => setTimeout(resolve, 2000)); 117 | 118 | const yearIndexResponse = await databases.createIndex( 119 | databaseId, 120 | collectionId, 121 | 'key_release_year_asc', 122 | 'key', 123 | ['release_year'], 124 | ['ASC'], 125 | ); 126 | console.log(yearIndexResponse); 127 | 128 | console.log("Waiting a little to ensure index is created ..."); 129 | await new Promise((resolve) => setTimeout(resolve, 2000)); 130 | } 131 | 132 | const listCollections = async () => { 133 | console.log(chalk.greenBright('Running List Collections API')); 134 | 135 | const response = await databases.listCollections(databaseId); 136 | 137 | console.log(response); 138 | } 139 | 140 | const getCollection = async () => { 141 | console.log(chalk.greenBright("Running Get Collection API")); 142 | 143 | const response = await databases.getCollection(databaseId, collectionId); 144 | 145 | console.log(response); 146 | } 147 | 148 | const updateCollection = async () => { 149 | console.log(chalk.greenBright("Running Update Collection API")); 150 | 151 | const response = await databases.updateCollection( 152 | databaseId, 153 | collectionId, 154 | "Updated Collection" 155 | ); 156 | 157 | console.log(response); 158 | } 159 | 160 | const deleteCollection = async () => { 161 | console.log(chalk.greenBright("Running Delete Collection API")); 162 | 163 | const response = await databases.deleteCollection(databaseId, collectionId); 164 | 165 | console.log(response); 166 | } 167 | 168 | const listAttributes = async () => { 169 | console.log(chalk.greenBright('Running List Attributes API')); 170 | 171 | const response = await databases.listAttributes(databaseId, collectionId); 172 | 173 | console.log(response); 174 | } 175 | 176 | const createDocument = async () => { 177 | console.log(chalk.greenBright('Running Add Document API')); 178 | 179 | const response = await databases.createDocument( 180 | databaseId, 181 | collectionId, 182 | ID.unique(), 183 | { 184 | name: 'Spider Man', 185 | release_year: 1920 186 | }, 187 | [ 188 | Permission.read(Role.any()), 189 | Permission.update(Role.users()), 190 | Permission.delete(Role.users()), 191 | ] 192 | ); 193 | documentId = response.$id; 194 | 195 | console.log(response); 196 | } 197 | 198 | const listDocuments = async () => { 199 | console.log(chalk.greenBright('Running List Documents API')); 200 | 201 | const response = await databases.listDocuments( 202 | databaseId, 203 | collectionId, 204 | [ 205 | Query.equal('release_year', 1920), 206 | ] 207 | ); 208 | 209 | console.log(response); 210 | } 211 | 212 | const getDocument = async () => { 213 | console.log(chalk.greenBright("Running Get Document API")); 214 | 215 | const response = await databases.getDocument( 216 | databaseId, 217 | collectionId, 218 | documentId 219 | ); 220 | 221 | console.log(response); 222 | } 223 | 224 | const updateDocument = async () => { 225 | console.log(chalk.greenBright("Running Update Document API")); 226 | 227 | const response = await databases.updateDocument( 228 | databaseId, 229 | collectionId, 230 | documentId, 231 | { 232 | release_year: 2005 233 | } 234 | ); 235 | 236 | console.log(response); 237 | } 238 | 239 | const deleteDocument = async () => { 240 | console.log(chalk.greenBright("Running Delete Document API")); 241 | 242 | const response = await databases.deleteDocument( 243 | databaseId, 244 | collectionId, 245 | documentId 246 | ); 247 | 248 | console.log(response); 249 | } 250 | 251 | const createBucket = async () => { 252 | console.log(chalk.greenBright("Running Create Bucket API")); 253 | 254 | const response = await storage.createBucket( 255 | ID.unique(), 256 | "All Files", 257 | [ 258 | Permission.read(Role.any()), 259 | Permission.create(Role.users()), 260 | Permission.update(Role.users()), 261 | Permission.delete(Role.users()), 262 | ] 263 | ); 264 | bucketId = response.$id; 265 | 266 | console.log(response); 267 | } 268 | 269 | const listBuckets = async () => { 270 | console.log(chalk.greenBright("Running List Buckets API")); 271 | 272 | const response = await storage.listBuckets(); 273 | 274 | console.log(response); 275 | } 276 | 277 | const getBucket = async () => { 278 | console.log(chalk.greenBright("Running Get Bucket API")); 279 | 280 | const response = await storage.getBucket(bucketId); 281 | 282 | console.log(response); 283 | } 284 | 285 | const updateBucket = async () => { 286 | console.log(chalk.greenBright("Running Update Bucket API")); 287 | 288 | const response = await storage.updateBucket( 289 | bucketId, 290 | "Updated Bucket" 291 | ); 292 | 293 | console.log(response); 294 | } 295 | 296 | const deleteBucket = async () => { 297 | console.log(chalk.greenBright("Running Delete Bucket API")); 298 | 299 | const response = await storage.deleteBucket(bucketId); 300 | 301 | console.log(response); 302 | } 303 | 304 | const uploadFile = async () => { 305 | console.log(chalk.greenBright('Running Upload File API')); 306 | 307 | const response = await storage.createFile( 308 | bucketId, 309 | ID.unique(), 310 | InputFile.fromPath("./resources/nature.jpg", "nature.jpg"), 311 | [ 312 | Permission.read(Role.any()), 313 | Permission.update(Role.users()), 314 | Permission.delete(Role.users()), 315 | ] 316 | ); 317 | fileId = response.$id; 318 | 319 | console.log(response); 320 | } 321 | 322 | const listFiles = async () => { 323 | console.log(chalk.greenBright("Running List Files API")); 324 | 325 | const response = await storage.listFiles(bucketId); 326 | 327 | console.log(response); 328 | } 329 | 330 | const getFile = async () => { 331 | console.log(chalk.greenBright("Running Get File API")); 332 | 333 | const response = await storage.getFile(bucketId, fileId); 334 | 335 | console.log(response); 336 | } 337 | 338 | const updateFile = async () => { 339 | console.log(chalk.greenBright("Running Update File API")); 340 | 341 | const response = await storage.updateFile( 342 | bucketId, 343 | fileId, 344 | "abc", 345 | [ 346 | Permission.read(Role.any()), 347 | Permission.update(Role.any()), 348 | Permission.delete(Role.any()), 349 | ] 350 | ); 351 | 352 | console.log(response); 353 | } 354 | 355 | const deleteFile = async () => { 356 | console.log(chalk.greenBright("Running Delete File API")); 357 | 358 | const response = await storage.deleteFile(bucketId, fileId); 359 | 360 | console.log(response); 361 | } 362 | 363 | const createUser = async () => { 364 | console.log(chalk.greenBright('Running Create User API')); 365 | 366 | const response = await users.create( 367 | ID.unique(), 368 | new Date().getTime() + '@example.com', 369 | null, 370 | 'user@123', 371 | 'Some User' 372 | ); 373 | userId = response.$id; 374 | 375 | console.log(response); 376 | } 377 | 378 | const listUsers = async () => { 379 | console.log(chalk.greenBright('Running List Users API')); 380 | 381 | const response = await users.list(); 382 | 383 | console.log(response); 384 | } 385 | 386 | const getUser = async () => { 387 | console.log(chalk.greenBright('Running Get User API')); 388 | 389 | const response = await users.get(userId); 390 | 391 | console.log(response); 392 | } 393 | 394 | const getAccount = async () => { 395 | console.log(chalk.greenBright('Running List Users API')); 396 | 397 | const response = await account.get(); 398 | 399 | console.log(response); 400 | } 401 | 402 | const updateUserName = async () => { 403 | console.log(chalk.greenBright('Running Update User Name API')); 404 | 405 | const response = await users.updateName(userId, 'Updated Name'); 406 | 407 | console.log(response); 408 | } 409 | 410 | const deleteUser = async () => { 411 | console.log(chalk.greenBright('Running Delete User API')); 412 | 413 | const response = await users.delete(userId); 414 | 415 | console.log(response); 416 | } 417 | 418 | const createFunction = async () => { 419 | console.log(chalk.greenBright('Running Create Function API')); 420 | 421 | const response = await functions.create( 422 | ID.unique(), 423 | "Node Hello World", 424 | "node-16.0", 425 | [Role.any()], 426 | [], 427 | '', 428 | 15, 429 | true, 430 | true, 431 | "index.js", 432 | '' 433 | ); 434 | 435 | functionId = response.$id; 436 | 437 | console.log(response); 438 | } 439 | 440 | const listFunctions = async () => { 441 | console.log(chalk.greenBright('Running List Functions API')); 442 | 443 | let response = await functions.list(); 444 | 445 | console.log(response); 446 | } 447 | 448 | const getFunction = async () => { 449 | console.log(chalk.greenBright('Running Get Function API')); 450 | 451 | let response = await functions.get(functionId); 452 | 453 | console.log(response); 454 | } 455 | 456 | const uploadDeployment = async () => { 457 | console.log(chalk.greenBright('Running Upload Deployment API')); 458 | 459 | let response = await functions.createDeployment( 460 | functionId, 461 | InputFile.fromPath("./resources/code.tar.gz", "code.tar.gz"), 462 | true, 463 | "index.js" 464 | ); 465 | 466 | console.log(response); 467 | 468 | console.log("Waiting a little to ensure deployment has built ..."); 469 | await new Promise((resolve) => setTimeout(resolve, 3000)); 470 | } 471 | 472 | const listDeployments = async () => { 473 | console.log(chalk.greenBright('Running List Deployments API')); 474 | 475 | let response = await functions.listDeployments(functionId); 476 | 477 | console.log(response); 478 | } 479 | 480 | const executeSync = async () => { 481 | console.log(chalk.greenBright('Running Execute Function API (sync)')); 482 | 483 | let response = await functions.createExecution(functionId, "", false, "/", "GET", {}); 484 | 485 | // sleep for 3 seconds 486 | console.log("Waiting a little to ensure execution is finished ..."); 487 | await new Promise((resolve) => setTimeout(resolve, 3000)); 488 | 489 | console.log(response); 490 | } 491 | 492 | const executeAsync = async () => { 493 | console.log(chalk.greenBright('Running Execute Function API (async)')); 494 | 495 | let response = await functions.createExecution(functionId, '', true); 496 | 497 | executionId = response.$id; 498 | 499 | console.log(response); 500 | 501 | console.log("Waiting a little to ensure execution is finished ..."); 502 | await new Promise((resolve) => setTimeout(resolve, 2000)); 503 | 504 | let asyncResponse = await functions.getExecution(functionId, executionId); 505 | 506 | console.log(asyncResponse); 507 | } 508 | 509 | const deleteFunction = async () => { 510 | console.log(chalk.greenBright('Running Delete function API')); 511 | 512 | const response = await functions.delete(functionId); 513 | 514 | console.log(response); 515 | } 516 | 517 | const runAllTasks = async () => { 518 | await createDatabase(); 519 | await listDatabases(); 520 | await getDatabase(); 521 | await updateDatabase(); 522 | 523 | await createCollection(); 524 | await listCollections(); 525 | await getCollection(); 526 | await updateCollection(); 527 | await listAttributes(); 528 | 529 | await createDocument(); 530 | await listDocuments(); 531 | await getDocument(); 532 | await updateDocument(); 533 | 534 | await deleteDocument(); 535 | await deleteCollection(); 536 | await deleteDatabase(); 537 | 538 | await createBucket(); 539 | await listBuckets(); 540 | await getBucket(); 541 | await updateBucket(); 542 | 543 | await uploadFile(); 544 | await listFiles(); 545 | await getFile(); 546 | await updateFile(); 547 | 548 | await deleteFile(); 549 | await deleteBucket(); 550 | 551 | // await getAccount() // works only with JWT 552 | await createUser(); 553 | await listUsers(); 554 | await getUser(); 555 | await updateUserName(); 556 | await deleteUser(); 557 | 558 | await createFunction(); 559 | await listFunctions(); 560 | await uploadDeployment(); 561 | await listDeployments(); 562 | await executeSync(); 563 | await executeAsync(); 564 | await deleteFunction(); 565 | } 566 | 567 | runAllTasks() 568 | .then(() => { 569 | console.log(chalk.green.bold('Successfully ran playground!')) 570 | }) 571 | .catch(err => { 572 | console.error(err) 573 | }) 574 | --------------------------------------------------------------------------------