Able to set all collection properties when creating (#21)

* Able to set all collection properties when creating

* Update CHANGELOG.md
This commit is contained in:
Alex Weininger 2021-06-03 07:12:21 -07:00 committed by GitHub
parent 60a510b646
commit 704bd72420
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 132 additions and 41 deletions

View file

@ -6,6 +6,9 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
## [Unreleased] ## [Unreleased]
## Added
Ability to set the `list`, `default` and `array` properties when creating a new collection. | [Issue #20](https://github.com/streamlux/vscode-appwrite/issues/20) | [PR #21](https://github.com/streamlux/vscode-appwrite/pull/21) | Thanks [@Maatteogekko](https://github.com/Maatteogekko)!
## [0.1.1] - 2021-5-31 ## [0.1.1] - 2021-5-31
## Added ## Added
- You can now easily create function tags from multiple places in the extension. [PR #19](https://github.com/streamlux/vscode-appwrite/pull/19) - You can now easily create function tags from multiple places in the extension. [PR #19](https://github.com/streamlux/vscode-appwrite/pull/19)

View file

@ -2,7 +2,7 @@
"name": "vscode-appwrite", "name": "vscode-appwrite",
"displayName": "Appwrite", "displayName": "Appwrite",
"description": "Manage your Appwrite resources right from VS Code!", "description": "Manage your Appwrite resources right from VS Code!",
"version": "0.1.1", "version": "0.1.2",
"engines": { "engines": {
"vscode": "^1.55.0" "vscode": "^1.55.0"
}, },

8
src/appwrite.d.ts vendored
View file

@ -276,7 +276,7 @@ export type CollectionsList = {
collections: Collection[]; collections: Collection[];
}; };
export type CreatedRule = Omit<Rule, "$id" | "$collection" | "default" | "list">; export type CreatedRule = Omit<Rule, "$id" | "$collection">;
export type Rule = { export type Rule = {
$id: string; $id: string;
@ -284,10 +284,10 @@ export type Rule = {
type: string; type: string;
key: string; key: string;
label: string; label: string;
default: string; default?: any;
array: boolean;
required: boolean; required: boolean;
list: string[]; array: boolean;
list?: string[];
}; };
export type Permissions = { export type Permissions = {

View file

@ -10,15 +10,13 @@ export async function createRule(rulesTreeItem: RulesTreeItem): Promise<void> {
return; return;
} }
const ruleContext = await createRuleWizard();
const collection = rulesTreeItem.parent.collection; const collection = rulesTreeItem.parent.collection;
const ruleContext = await createRuleWizard(collection);
if (ruleContext) { if (ruleContext) {
const newRule: CreatedRule = { const newRule: CreatedRule = {
...ruleContext, ...ruleContext,
type: ruleContext.type, type: ruleContext.type,
required: true,
array: false,
}; };
databaseClient.createRule(collection, newRule); databaseClient.createRule(collection, newRule);

View file

@ -1,42 +1,19 @@
import { QuickPickItem, window } from "vscode"; import { QuickPickItem, window } from "vscode";
import { Collection, CollectionsList } from "../appwrite";
import { client } from "../client";
import { AppwriteSDK } from "../constants";
import AppwriteCall from "../utils/AppwriteCall";
export type CreateRuleWizardContext = { export type CreateRuleWizardContext = {
label: string; label: string;
key: string; key: string;
type: keyof typeof ruleTypes; type: keyof typeof ruleTypes;
default: any;
required: boolean;
array: boolean;
list?: string[];
}; };
export async function createRuleWizard(): Promise<CreateRuleWizardContext | undefined> {
const label = await window.showInputBox({
placeHolder: "Label",
prompt: "Attribute internal display name",
});
if (label === undefined) {
return;
}
const key = await window.showInputBox({
placeHolder: "Key",
prompt: "Attribute key name. Used as the document JSON key in the Database API.",
});
if (key === undefined) {
return;
}
const ruleTypeItems: QuickPickItem[] = Object.entries(ruleTypes).map(([label, description]) => ({
label,
description,
}));
const type = await window.showQuickPick(ruleTypeItems);
if (type === undefined) {
return;
}
if (label && key && type) {
return { label, key, type: (type.label as unknown) as keyof typeof ruleTypes };
}
return undefined;
}
const ruleTypes = { const ruleTypes = {
text: "Any string value.", text: "Any string value.",
numeric: "Any integer or float value.", numeric: "Any integer or float value.",
@ -45,6 +22,119 @@ const ruleTypes = {
url: "Any valid URL.", url: "Any valid URL.",
email: "Any valid email address.", email: "Any valid email address.",
ip: "Any valid IP v4 or v6 address.", ip: "Any valid IP v4 or v6 address.",
document: document: "Accept a valid child document from specified collection(s).",
"Accept a valid child document. When using this type you are also required to pass the 'list' parameter with an array of the collections UID values of the document types you want to accept.",
}; };
type RuleType = keyof typeof ruleTypes;
export async function createRuleWizard(collection: Collection): Promise<CreateRuleWizardContext | undefined> {
const label = await window.showInputBox({
placeHolder: "Attribute label",
prompt: "Attribute internal display name",
validateInput: (value) => {
if (value === "") {
return "Label cannot be empty.";
}
},
});
if (label === undefined) {
return;
}
const key = await window.showInputBox({
placeHolder: "Attribute key name",
prompt: "Attribute key name. Used as the document JSON key in the Database API.",
ignoreFocusOut: true,
validateInput: (value) => {
if (value === "") {
return "Key name cannot be empty.";
}
},
});
if (key === undefined) {
return;
}
const ruleTypeItems: QuickPickItem[] = Object.entries(ruleTypes).map(([label, description]) => ({
label,
detail: description,
}));
const typeItem = await window.showQuickPick(ruleTypeItems, { placeHolder: "Rule value type." });
const type: RuleType | undefined = (typeItem?.label as RuleType) ?? undefined;
if (typeItem === undefined || type === undefined) {
return;
}
let list: string[] | undefined = undefined;
if (type === "document") {
const databaseSdk = new AppwriteSDK.Database(client);
const collectionsList = await AppwriteCall<CollectionsList, CollectionsList>(databaseSdk.listCollections());
if (collectionsList === undefined) {
window.showErrorMessage("Could not get collections list.");
return;
}
if (collectionsList) {
const collections = collectionsList.collections.filter((c) => c.$id !== collection.$id);
const qpItems: QuickPickItem[] = collections.map((collection) => ({
label: collection.name,
description: collection.$id,
}));
const listInput = await window.showQuickPick(qpItems, {
canPickMany: true,
placeHolder: "Collections which contain valid child documents for this document attribute.",
ignoreFocusOut: true,
});
list = listInput?.map((item) => item.description as string) ?? [];
}
}
if (label === "document" && list === undefined) {
return;
}
const array = await window.showQuickPick(["Primitive", "Array"], {
placeHolder: "Decide if this rule is a primitive or an array of values.",
ignoreFocusOut: true,
});
if (array === undefined) {
return;
}
const required = await window.showQuickPick(["Required", "Optional"], {
placeHolder: "Decide if this rule value is required in order to pass document validation.",
ignoreFocusOut: true,
});
if (required === undefined) {
return;
}
const defaultValue = await window.showInputBox({
placeHolder: "Default value (press Enter to skip)",
prompt: "Default value for this rule type. Make sure that the default value is able to pass validation in order to avoid errors when skipping optional values.",
ignoreFocusOut: true,
});
if (defaultValue === undefined) {
return;
}
if (label && key && type) {
return {
label,
key,
type,
default: defaultValue,
array: array === "Array",
required: required === "Required",
list,
};
}
return undefined;
}