Skip to content

Commit

Permalink
Customize partition key validation rule for Gremlin DB (#2184)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonYeMSFT authored Sep 21, 2023
1 parent 10fd538 commit 9bbf104
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/docdb/tree/DocDBDatabaseTreeItemBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,7 @@ export abstract class DocDBDatabaseTreeItemBase extends DocDBTreeItemBase<Contai
id: containerName
};

let partitionKey: string | undefined = await context.ui.showInputBox({
prompt: 'Enter the partition key for the collection, or leave blank for fixed size.',
stepName: 'partitionKeyForCollection',
validateInput: validatePartitionKey,
placeHolder: 'e.g. address/zipCode'
});

if (partitionKey && partitionKey.length && partitionKey[0] !== '/') {
partitionKey = '/' + partitionKey;
}
const partitionKey = await this.getNewPartitionKey(context);
if (partitionKey) {
containerDefinition.partitionKey = {
paths: [partitionKey]
Expand All @@ -113,13 +104,28 @@ export abstract class DocDBDatabaseTreeItemBase extends DocDBTreeItemBase<Contai

return this.initChild(nonNullProp(container, 'resource'));
}
}

function validatePartitionKey(key: string): string | undefined | null {
if (/[#?\\]/.test(key)) {
return "Cannot contain these characters: ?,#,\\, etc.";
protected async getNewPartitionKey(context: IActionContext): Promise<string | undefined> {
let partitionKey: string | undefined = await context.ui.showInputBox({
prompt: 'Enter the partition key for the collection, or leave blank for fixed size.',
stepName: 'partitionKeyForCollection',
validateInput: this.validatePartitionKey,
placeHolder: 'e.g. /address/zipCode'
});

if (partitionKey && partitionKey.length && partitionKey[0] !== '/') {
partitionKey = '/' + partitionKey;
}

return partitionKey;
}

protected validatePartitionKey(key: string): string | undefined {
if (/[#?\\]/.test(key)) {
return "Cannot contain these characters: ?,#,\\, etc.";
}
return undefined;
}
return undefined;
}

function validateThroughput(isFixed: boolean, input: string): string | undefined | null {
Expand Down
26 changes: 26 additions & 0 deletions src/graph/tree/GraphDatabaseTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { ContainerDefinition, CosmosClient, Database, DatabaseDefinition, Resource } from '@azure/cosmos';
import { IActionContext } from '@microsoft/vscode-azext-utils';
import { DocDBDatabaseTreeItemBase } from '../../docdb/tree/DocDBDatabaseTreeItemBase';
import { IGremlinEndpoint } from '../../vscode-cosmosdbgraph.api';
import { getPossibleGremlinEndpoints } from '../gremlinEndpoints';
Expand Down Expand Up @@ -36,4 +37,29 @@ export class GraphDatabaseTreeItem extends DocDBDatabaseTreeItemBase {
return client.database(this.id);

}

protected override async getNewPartitionKey(context: IActionContext): Promise<string | undefined> {
let partitionKey: string | undefined = await context.ui.showInputBox({
prompt: 'Enter the partition key for the collection, or leave blank for fixed size.',
stepName: 'partitionKeyForCollection',
validateInput: this.validatePartitionKey,
placeHolder: 'e.g. /address'
});

if (partitionKey && partitionKey.length && partitionKey[0] !== '/') {
partitionKey = '/' + partitionKey;
}

return partitionKey;
}

protected validatePartitionKey(key: string): string | undefined {
if (/[#?\\]/.test(key)) {
return "Cannot contain these characters: ?,#,\\, etc.";
}
if (/.+\//.test(key)) {
return "Cannot be a nested path";
}
return undefined;
}
}

0 comments on commit 9bbf104

Please sign in to comment.