Skip to content

Commit

Permalink
fix: indexing key.
Browse files Browse the repository at this point in the history
  • Loading branch information
ItzNotABug committed Dec 15, 2024
1 parent b0ed014 commit f516387
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -11,17 +11,16 @@
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;
export let externalAttribute: Attributes = null;
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' },
Expand All @@ -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' }]
Expand All @@ -49,6 +59,7 @@
$: if (showCreateIndex) {
error = null;
initialize();
key = generateIndexKey();
}
$: addAttributeDisabled = !attributeList.at(-1)?.value || !attributeList.at(-1)?.order;
Expand Down

0 comments on commit f516387

Please sign in to comment.