diff --git a/package.json b/package.json index 13b816a..f99dbfa 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "onCommand:vscode-appwrite.Connect", "onView:Appwrite", "onView:Users", - "onView:Database", + "onView:Databases", "onView:Health", "onView:Functions", "onCommand:vscode-appwrite.AddProject", @@ -348,8 +348,8 @@ "name": "Users" }, { - "id": "Database", - "name": "Database" + "id": "Databases", + "name": "Databases" }, { "id": "Storage", @@ -388,7 +388,7 @@ }, { "command": "vscode-appwrite.OpenDatabaseDocumentation", - "when": "view == Database", + "when": "view == Databases", "group": "navigation" }, { @@ -403,12 +403,12 @@ }, { "command": "vscode-appwrite.refreshCollectionsList", - "when": "view == Database", + "when": "view == Databases", "group": "navigation" }, { "command": "vscode-appwrite.createCollection", - "when": "view == Database", + "when": "view == Databases", "group": "navigation" }, { @@ -738,19 +738,21 @@ "scripts": { "vscode:prepublish": "npm run package", "compile": "webpack", - "watch": "webpack --watch", + "watch": "webpack --mode development --watch", "package": "webpack --mode production --devtool hidden-source-map", "test-compile": "tsc -p ./", "test-watch": "tsc -watch -p ./", "pretest": "npm run test-compile && npm run lint", "lint": "eslint src --ext ts", - "test": "node ./out/test/runTest.js" + "test": "node ./out/test/runTest.js", + "webpack": "webpack --mode development", + "webpack-dev": "webpack --mode development --watch" }, "devDependencies": { "@types/fs-extra": "^9.0.11", "@types/glob": "^7.1.3", "@types/mocha": "^8.0.4", - "@types/node": "^12.11.7", + "@types/node": "^12.20.55", "@types/tar": "^4.0.4", "@types/vscode": "^1.55.0", "@typescript-eslint/eslint-plugin": "^4.14.1", @@ -758,19 +760,20 @@ "eslint": "^7.19.0", "glob": "^7.1.6", "mocha": "^8.2.1", - "ts-loader": "^8.0.14", + "ts-loader": "^8.4.0", "typescript": "^4.1.3", "vsce": "^1.88.0", "vscode-test": "^1.5.0", - "webpack": "^5.19.0", - "webpack-cli": "^4.4.0" + "webpack": "^5.74.0", + "webpack-cli": "^4.10.0" }, "dependencies": { "cron-validate": "^1.4.3", "cronstrue": "^1.113.0", "dayjs": "^1.10.4", "fs-extra": "^9.1.0", - "node-appwrite": "^2.2.3", + "node-appwrite": "^8.1.0", + "node-polyfill-webpack-plugin": "^2.0.1", "tar": "^6.1.0" } } diff --git a/src/appwrite.d.ts b/src/appwrite.d.ts index dda0ed0..c18b37f 100644 --- a/src/appwrite.d.ts +++ b/src/appwrite.d.ts @@ -260,8 +260,13 @@ export type File = { sizeOriginal: number; }; +export type Database = { + $id: string; +} + export type Collection = { $id: string; + $database: string; $permissions: Permissions; name: string; dateCreated: number; @@ -312,12 +317,13 @@ export type UsersClient = { }; export type DatabaseClient = { - listCollections: () => Promise; - listDocuments: (collectionId: string) => Promise; - updateCollection: (collectionId: string, name: string, read: string[], write: string[], rules?: (Rule | CreatedRule)[]) => Promise; - deleteCollection: (collectionId: string) => Promise; - getCollection: (collectionId: string) => Promise; - deleteDocument: (collectionId: string, documentId: string) => Promise; + list: () => Promise; + listCollections: (collectionDatabase: string) => Promise; + listDocuments: (collectionDatabase: string, collectionId: string) => Promise; + updateCollection: (collectionDatabase: string, collectionId: string, name: string, read: string[], write: string[], rules?: (Rule | CreatedRule)[]) => Promise; + deleteCollection: (collectionDatabase: string, collectionId: string) => Promise; + getCollection: (collectionDatabase: string, collectionId: string) => Promise; + deleteDocument: (collectionDatabase: string, collectionId: string, documentId: string) => Promise; createCollection: (name: string, read: string[], write: string[], rules: Rule[]) => Promise; }; @@ -448,7 +454,7 @@ export type SDK = { Users: new (client: Client) => UsersClient; Health: new (client: Client) => HealthClient; - Database: new (client: Client) => DatabaseClient; + Databases: new (client: Client) => DatabaseClient; Storage: new (client: Client) => StorageClient; Functions: new (client: Client) => FunctionsClient; }; diff --git a/src/appwrite/Database.ts b/src/appwrite/Database.ts index 7d5be16..e784968 100644 --- a/src/appwrite/Database.ts +++ b/src/appwrite/Database.ts @@ -6,19 +6,23 @@ export class Database { private readonly database: DatabaseClient; constructor(client: Client) { - this.database = new AppwriteSDK.Database(client); + this.database = new AppwriteSDK.Databases(client); } - public async getCollection(collectionId: string): Promise { - return await AppwriteCall(this.database.getCollection(collectionId)); + public async list(): Promise { + return await AppwriteCall(this.database.list()); } - public async deleteDocument(collectionId: string, documentId: string): Promise { - await AppwriteCall(this.database.deleteDocument(collectionId, documentId)); + public async getCollection(collectionDB: string, collectionId: string): Promise { + return await AppwriteCall(this.database.getCollection(collectionDB, collectionId)); } - public async deleteCollection(collectionId: string): Promise { - await AppwriteCall(this.database.deleteCollection(collectionId)); + public async deleteDocument(collectionDB:string, collectionId: string, documentId: string): Promise { + await AppwriteCall(this.database.deleteDocument(collectionDB, collectionId, documentId)); + } + + public async deleteCollection(collectionDB:string, collectionId: string): Promise { + await AppwriteCall(this.database.deleteCollection(collectionDB, collectionId)); } public async createCollection(collection: CreatedCollection): Promise { @@ -33,12 +37,12 @@ export class Database { } public async updatePermissions(collection: Collection, read: string[], write: string[]): Promise { - await AppwriteCall(this.database.updateCollection(collection.$id, collection.name, read, write, collection.rules)); + await AppwriteCall(this.database.updateCollection(collection.$database, collection.$id, collection.name, read, write, collection.rules)); } public async createRule(collection: Collection, newRule: CreatedRule): Promise { await AppwriteCall( - this.database.updateCollection(collection.$id, collection.name, collection.$permissions.read, collection.$permissions.write, [ + this.database.updateCollection(collection.$database, collection.$id, collection.name, collection.$permissions.read, collection.$permissions.write, [ ...collection.rules, newRule, ]) @@ -50,6 +54,7 @@ export class Database { await AppwriteCall( this.database.updateCollection( + collection.$database, collection.$id, collection.name, collection.$permissions.read, diff --git a/src/commands/database/deleteCollection.ts b/src/commands/database/deleteCollection.ts index fe1458f..6cd4174 100644 --- a/src/commands/database/deleteCollection.ts +++ b/src/commands/database/deleteCollection.ts @@ -11,10 +11,10 @@ export async function deleteCollection(collectionTreeItem: CollectionTreeItem): try { const shouldDelete = await confirmDialog(`Delete collection "${collection.name}"?`); if (shouldDelete) { - await databaseClient.deleteCollection(collection.$id); + await databaseClient.deleteCollection(collection.$database, collection.$id); window.showInformationMessage(`Deleted collection "${collection.name}".`); } - } catch (e) { + } catch (e: any) { window.showErrorMessage(e); } } diff --git a/src/commands/database/deleteDocument.ts b/src/commands/database/deleteDocument.ts index 996f72f..d758b45 100644 --- a/src/commands/database/deleteDocument.ts +++ b/src/commands/database/deleteDocument.ts @@ -12,10 +12,10 @@ export async function deleteDocument(documentTreeItem: DocumentTreeItem): Promis try { const shouldDelete = await confirmDialog(`Delete document "${document["$id"]}" from ${collection.name}?`); if (shouldDelete) { - await databaseClient.deleteDocument(collection.$id, document["$id"]); + await databaseClient.deleteDocument(collection.$database, collection.$id, document["$id"]); window.showInformationMessage(`Deleted document "${document["$id"]}" from ${collection.name}.`); } - } catch (e) { + } catch (e: any) { window.showErrorMessage(e); } } diff --git a/src/extension.ts b/src/extension.ts index 39969a5..a103f90 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,5 +1,6 @@ import * as vscode from "vscode"; import { createAppwriteClient } from "./client"; +import { activateTag } from "./commands/functions/activateTag"; import { registerCommands } from "./commands/registerCommands"; import { ext } from "./extensionVariables"; import { getActiveProjectConfiguration } from "./settings"; diff --git a/src/tree/database/CollectionTreeItem.ts b/src/tree/database/CollectionTreeItem.ts index 22c3a02..6465ec3 100644 --- a/src/tree/database/CollectionTreeItem.ts +++ b/src/tree/database/CollectionTreeItem.ts @@ -20,7 +20,7 @@ export class CollectionTreeItem extends AppwriteTreeItemBase { if (!databaseClient) { return; } - this.collection = (await databaseClient.getCollection(this.collection.$id)) ?? this.collection; + this.collection = (await databaseClient.getCollection(this.collection.$database, this.collection.$id)) ?? this.collection; this.provider.refreshChild(this); } diff --git a/src/tree/database/DatabaseTreeItem.ts b/src/tree/database/DatabaseTreeItem.ts new file mode 100644 index 0000000..035b6bd --- /dev/null +++ b/src/tree/database/DatabaseTreeItem.ts @@ -0,0 +1,36 @@ +import { ThemeIcon, TreeItem, TreeItemCollapsibleState } from "vscode"; +import { Database } from "../../appwrite"; +import { databaseClient } from "../../client"; +import { AppwriteTreeItemBase } from "../../ui/AppwriteTreeItemBase"; +import { CollectionTreeItem } from "./CollectionTreeItem"; +import { DatabaseTreeItemProvider } from "./DatabaseTreeItemProvider"; +import { DocumentsTreeItem } from "./DocumentsTreeItem"; +import { PermissionsTreeItem } from "./settings/PermissionsTreeItem"; +import { RulesTreeItem } from "./settings/RulesTreeItem"; + +export class DatabaseTreeItem extends AppwriteTreeItemBase { + constructor(public database: any, public readonly provider: DatabaseTreeItemProvider) { + super(undefined, database.name); + } + + public async getChildren(): Promise { + // return [new RulesTreeItem(this), new PermissionsTreeItem(this), new DocumentsTreeItem(this)]; + // return [new CollectionTreeItem(this)]; + console.warn(this); + return []; + } + + public async refresh(): Promise { + if (!databaseClient) { + return; + } + this.database = (await databaseClient.list()) ?? this.database; + this.provider.refreshChild(this); + } + + collapsibleState = TreeItemCollapsibleState.Collapsed; + + contextValue = "database"; + + iconPath = new ThemeIcon("folder"); +} diff --git a/src/tree/database/DatabaseTreeItemProvider.ts b/src/tree/database/DatabaseTreeItemProvider.ts index 9d92c16..6d74116 100644 --- a/src/tree/database/DatabaseTreeItemProvider.ts +++ b/src/tree/database/DatabaseTreeItemProvider.ts @@ -3,6 +3,7 @@ import { client } from "../../client"; import AppwriteCall from "../../utils/AppwriteCall"; import { Collection, CollectionsList } from "../../appwrite"; import { CollectionTreeItem } from "./CollectionTreeItem"; +import { DatabaseTreeItem } from "./DatabaseTreeItem"; import { AppwriteSDK } from "../../constants"; import { ext } from '../../extensionVariables'; import { AppwriteTreeItemBase } from '../../ui/AppwriteTreeItemBase'; @@ -37,17 +38,27 @@ export class DatabaseTreeItemProvider implements vscode.TreeDataProvider(databaseSdk.listCollections()); - if (collectionsList) { - const collectionTreeItems = collectionsList.collections.map((collection: Collection) => new CollectionTreeItem(collection, this)) ?? []; + const databaseList = await AppwriteCall(databaseSdk.list()); + + if(databaseList) { + const databaseTreeItems = databaseList.databases.map((database: any) => new DatabaseTreeItem(database, this)) ?? []; const headerItem: vscode.TreeItem = { - label: `Total collections: ${collectionsList.sum}`, + label: `Total databases: ${databaseList.sum}`, }; - return [headerItem, ...collectionTreeItems]; + return [headerItem, ...databaseTreeItems]; } - return [{ label: "No collections found" }]; + // const collectionsList = await AppwriteCall(databaseSdk.listCollections()); + // if (collectionsList) { + // const collectionTreeItems = collectionsList.collections.map((collection: Collection) => new CollectionTreeItem(collection, this)) ?? []; + // const headerItem: vscode.TreeItem = { + // label: `Total collections: ${collectionsList.sum}`, + // }; + // return [headerItem, ...collectionTreeItems]; + // } + + return [{ label: "No databases found" }]; } } diff --git a/src/tree/database/DocumentsTreeItem.ts b/src/tree/database/DocumentsTreeItem.ts index 314c58c..a6a2180 100644 --- a/src/tree/database/DocumentsTreeItem.ts +++ b/src/tree/database/DocumentsTreeItem.ts @@ -14,8 +14,8 @@ export class DocumentsTreeItem extends AppwriteTreeItemBase } public async getChildren(): Promise { - const databaseSdk = new AppwriteSDK.Database(client); - const documentList = await AppwriteCall(databaseSdk.listDocuments(this.parent.collection.$id)); + const databaseSdk = new AppwriteSDK.Databases(client); + const documentList = await AppwriteCall(databaseSdk.listDocuments(this.parent.collection.$database, this.parent.collection.$id)); if (documentList === undefined) { return []; } diff --git a/src/tree/health/HealthTreeItemProvider.ts b/src/tree/health/HealthTreeItemProvider.ts index 2d508b1..b8fa7a8 100644 --- a/src/tree/health/HealthTreeItemProvider.ts +++ b/src/tree/health/HealthTreeItemProvider.ts @@ -40,7 +40,7 @@ export class HealthTreeItemProvider implements vscode.TreeDataProvider { try { return await healthClient?.checkup(); - } catch (e) { + } catch (e: any) { ext.outputChannel?.append('Error: ' + e.message); vscode.window.showErrorMessage('Could not connect to Appwrite project'); } diff --git a/src/ui/createRuleWizard.ts b/src/ui/createRuleWizard.ts index c53bd44..b13ee61 100644 --- a/src/ui/createRuleWizard.ts +++ b/src/ui/createRuleWizard.ts @@ -68,7 +68,7 @@ export async function createRuleWizard(collection: Collection): Promise(databaseSdk.listCollections()); if (collectionsList === undefined) {