From f516387c65fb042df25836dac4522b0c7dc3cf73 Mon Sep 17 00:00:00 2001 From: Darshan Date: Sun, 15 Dec 2024 19:15:29 +0530 Subject: [PATCH] fix: indexing key. --- .../indexes/createIndex.svelte | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/routes/(console)/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte b/src/routes/(console)/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte index 5c0bba01f..634de7ffa 100644 --- a/src/routes/(console)/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte +++ b/src/routes/(console)/project-[project]/databases/database-[database]/collection-[collection]/indexes/createIndex.svelte @@ -2,7 +2,7 @@ import { goto, invalidate } from '$app/navigation'; import { base } from '$app/paths'; import { page } from '$app/stores'; - import { Submit, trackEvent, trackError } from '$lib/actions/analytics'; + import { Submit, trackError, trackEvent } from '$lib/actions/analytics'; import { Modal } from '$lib/components'; import { Dependencies } from '$lib/constants'; import { Button, FormList, InputSelect, InputText } from '$lib/elements/forms'; @@ -11,8 +11,7 @@ import { sdk } from '$lib/stores/sdk'; import { IndexType } from '@appwrite.io/console'; import { isRelationship } from '../document-[document]/attributes/store'; - import { indexes, type Attributes } from '../store'; - import { collection } from '../store'; + import { type Attributes, collection, indexes } from '../store'; import Select from './select.svelte'; export let showCreateIndex = false; @@ -20,8 +19,8 @@ const databaseId = $page.params.database; + let key = ''; let error: string; - let key = `index_${$indexes.length + 1}`; let types = [ { value: IndexType.Key, label: 'Key' }, { value: IndexType.Unique, label: 'Unique' }, @@ -38,6 +37,17 @@ let attributeList = [{ value: '', order: '' }]; + function generateIndexKey() { + let indexKeys = $indexes.map((index) => index.key); + + let highestIndex = indexKeys.reduce((max, key) => { + const match = key.match(/^index_(\d+)$/); + return match ? Math.max(max, parseInt(match[1], 10)) : max; + }, indexKeys.length); + + return `index_${highestIndex + 1}`; + } + function initialize() { attributeList = externalAttribute ? [{ value: externalAttribute.key, order: 'ASC' }] @@ -49,6 +59,7 @@ $: if (showCreateIndex) { error = null; initialize(); + key = generateIndexKey(); } $: addAttributeDisabled = !attributeList.at(-1)?.value || !attributeList.at(-1)?.order;