From 7fad72510b596b4c29ec99a4996b4e5b5fffe65d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hornych?= Date: Sun, 17 Dec 2023 17:49:26 +0100 Subject: [PATCH] feat: support the new primary key sorting in data grid --- .../editor/data-grid/LabEditorDataGrid.vue | 4 +- .../LabEditorDataGridGridColumnHeader.vue | 4 +- src/model/editor/data-grid.ts | 30 +++++- .../editor/data-grid-console.service.ts | 100 +++++++++--------- .../evitaql-query-builder.ts | 4 + .../graphql-query-builder.ts | 4 + .../editor/data-grid-console/query-builder.ts | 7 ++ 7 files changed, 95 insertions(+), 58 deletions(-) diff --git a/src/components/lab/editor/data-grid/LabEditorDataGrid.vue b/src/components/lab/editor/data-grid/LabEditorDataGrid.vue index 956fb508..54240395 100644 --- a/src/components/lab/editor/data-grid/LabEditorDataGrid.vue +++ b/src/components/lab/editor/data-grid/LabEditorDataGrid.vue @@ -10,7 +10,7 @@ import { DataGridConsoleData, DataGridConsoleParams, EntityPropertyDescriptor, EntityPropertyKey, EntityPropertyType, - QueryResult, + QueryResult } from '@/model/editor/data-grid' import { DataGridConsoleService, useDataGridConsoleService } from '@/services/editor/data-grid-console.service' import { QueryLanguage } from '@/model/lab' @@ -115,7 +115,7 @@ async function initializeGridHeaders(entityPropertyDescriptors: EntityPropertyDe { key: propertyDescriptor.key.toString(), title: propertyDescriptor.title, - sortable: propertyDescriptor.schema?.sortable || false, + sortable: propertyDescriptor.isSortable(), descriptor: propertyDescriptor } ) diff --git a/src/components/lab/editor/data-grid/grid/LabEditorDataGridGridColumnHeader.vue b/src/components/lab/editor/data-grid/grid/LabEditorDataGridGridColumnHeader.vue index 209c6a72..2330e8b7 100644 --- a/src/components/lab/editor/data-grid/grid/LabEditorDataGridGridColumnHeader.vue +++ b/src/components/lab/editor/data-grid/grid/LabEditorDataGridGridColumnHeader.vue @@ -19,9 +19,9 @@ const prependIcon = computed(() => { } return undefined }) -const sortable = computed(() => props.column.descriptor?.schema?.sortable) +const sortable = computed(() => props.column.descriptor?.isSortable()) const sorted = computed(() => props.isSorted(props.column)) -const localized = computed(() => props.column.descriptor?.schema?.localized) +const localized = computed(() => props.column.descriptor?.isLocalized()) function handleClick() { if (sortable.value) { diff --git a/src/model/editor/data-grid.ts b/src/model/editor/data-grid.ts index 7465d04e..a1d49e5a 100644 --- a/src/model/editor/data-grid.ts +++ b/src/model/editor/data-grid.ts @@ -59,6 +59,11 @@ export enum StaticEntityProperties { PriceInnerRecordHandling = 'priceInnerRecordHandling' } +/** + * List of {@link StaticEntityProperties} that are sortable. + */ +export const sortableStaticEntityProperties: string[] = [StaticEntityProperties.PrimaryKey] + /** * Represents key of a single typed entity property. */ @@ -110,11 +115,26 @@ export class EntityPropertyKey { /** * Full description of a single entity property */ -export type EntityPropertyDescriptor = { - type: EntityPropertyType, - key: EntityPropertyKey, - title: string, - schema: any | undefined +export class EntityPropertyDescriptor { + readonly type: EntityPropertyType + readonly key: EntityPropertyKey + readonly title: string + readonly schema: any | undefined + + constructor(type: EntityPropertyType, key: EntityPropertyKey, title: string, schema: any | undefined) { + this.type = type + this.key = key + this.title = title + this.schema = schema + } + + isSortable(): boolean { + return sortableStaticEntityProperties.includes(this.key.toString()) || this.schema?.sortable || false + } + + isLocalized(): boolean { + return this.schema?.localized || false + } } /** diff --git a/src/services/editor/data-grid-console.service.ts b/src/services/editor/data-grid-console.service.ts index 565cc57b..a1af3e62 100644 --- a/src/services/editor/data-grid-console.service.ts +++ b/src/services/editor/data-grid-console.service.ts @@ -102,7 +102,9 @@ export class DataGridConsoleService { const orderBy: string[] = [] for (const column of columns) { const propertyKey: EntityPropertyKey = EntityPropertyKey.fromString(column.key) - if (propertyKey.type === EntityPropertyType.Attributes) { + if (propertyKey.type === EntityPropertyType.Entity && propertyKey.name === StaticEntityProperties.PrimaryKey) { + orderBy.push(queryBuilder.buildPrimaryKeyOrderBy(column.order)) + } else if (propertyKey.type === EntityPropertyType.Attributes) { const attributeSchema: AttributeSchemaUnion | undefined = Object.values(entitySchema.attributes) .find(attributeSchema => attributeSchema.nameVariants.camelCase === propertyKey.name) if (attributeSchema == undefined) { @@ -162,67 +164,67 @@ export class DataGridConsoleService { async getEntityPropertyDescriptors(dataPointer: DataGridDataPointer): Promise { const entitySchema: EntitySchema = await this.labService.getEntitySchema(dataPointer.connection, dataPointer.catalogName, dataPointer.entityType) const descriptors: EntityPropertyDescriptor[] = [] - descriptors.push({ - type: EntityPropertyType.Entity, - key: EntityPropertyKey.entity(StaticEntityProperties.PrimaryKey), - title: 'Primary key', - schema: undefined - }) + descriptors.push(new EntityPropertyDescriptor( + EntityPropertyType.Entity, + EntityPropertyKey.entity(StaticEntityProperties.PrimaryKey), + 'Primary key', + undefined + )) if (entitySchema.withHierarchy) { - descriptors.push({ - type: EntityPropertyType.Entity, - key: EntityPropertyKey.entity(StaticEntityProperties.ParentPrimaryKey), - title: 'Parent', - schema: undefined - }) + descriptors.push(new EntityPropertyDescriptor( + EntityPropertyType.Entity, + EntityPropertyKey.entity(StaticEntityProperties.ParentPrimaryKey), + 'Parent', + undefined + )) } if (entitySchema.locales.length > 0) { - descriptors.push({ - type: EntityPropertyType.Entity, - key: EntityPropertyKey.entity(StaticEntityProperties.Locales), - title: 'Locales', - schema: undefined - }) - descriptors.push({ - type: EntityPropertyType.Entity, - key: EntityPropertyKey.entity(StaticEntityProperties.AllLocales), - title: 'All locales', - schema: undefined - }) + descriptors.push(new EntityPropertyDescriptor( + EntityPropertyType.Entity, + EntityPropertyKey.entity(StaticEntityProperties.Locales), + 'Locales', + undefined + )) + descriptors.push(new EntityPropertyDescriptor( + EntityPropertyType.Entity, + EntityPropertyKey.entity(StaticEntityProperties.AllLocales), + 'All locales', + undefined + )) } if (entitySchema.withPrice) { - descriptors.push({ - type: EntityPropertyType.Entity, - key: EntityPropertyKey.entity(StaticEntityProperties.PriceInnerRecordHandling), - title: 'Price inner record handling', - schema: undefined - }) + descriptors.push(new EntityPropertyDescriptor( + EntityPropertyType.Entity, + EntityPropertyKey.entity(StaticEntityProperties.PriceInnerRecordHandling), + 'Price inner record handling', + undefined + )) } for (const attributeSchema of Object.values(entitySchema.attributes)) { - descriptors.push({ - type: EntityPropertyType.Attributes, - key: EntityPropertyKey.attributes(attributeSchema.nameVariants.camelCase), - title: attributeSchema.name, - schema: attributeSchema - }) + descriptors.push(new EntityPropertyDescriptor( + EntityPropertyType.Attributes, + EntityPropertyKey.attributes(attributeSchema.nameVariants.camelCase), + attributeSchema.name, + attributeSchema + )) } for (const associatedDataSchema of Object.values(entitySchema.associatedData)) { - descriptors.push({ - type: EntityPropertyType.AssociatedData, - key: EntityPropertyKey.associatedData(associatedDataSchema.nameVariants.camelCase), - title: `${associatedDataSchema.name}`, - schema: associatedDataSchema - }) + descriptors.push(new EntityPropertyDescriptor( + EntityPropertyType.AssociatedData, + EntityPropertyKey.associatedData(associatedDataSchema.nameVariants.camelCase), + `${associatedDataSchema.name}`, + associatedDataSchema + )) } for (const referenceSchema of Object.values(entitySchema.references)) { - descriptors.push({ - type: EntityPropertyType.References, - key: EntityPropertyKey.references(referenceSchema.nameVariants.camelCase), - title: `${referenceSchema.name}`, - schema: referenceSchema - }) + descriptors.push(new EntityPropertyDescriptor( + EntityPropertyType.References, + EntityPropertyKey.references(referenceSchema.nameVariants.camelCase), + `${referenceSchema.name}`, + referenceSchema + )) } return descriptors diff --git a/src/services/editor/data-grid-console/evitaql-query-builder.ts b/src/services/editor/data-grid-console/evitaql-query-builder.ts index 56c8c1b1..4d9c3d1a 100644 --- a/src/services/editor/data-grid-console/evitaql-query-builder.ts +++ b/src/services/editor/data-grid-console/evitaql-query-builder.ts @@ -161,6 +161,10 @@ export class EvitaQLQueryBuilder implements QueryBuilder { return `query(${constraints.join(',')})` } + buildPrimaryKeyOrderBy(orderDirection: string): string { + return `entityPrimaryKeyNatural(${orderDirection.toUpperCase()})` + } + buildAttributeOrderBy(attributeSchema: AttributeSchemaUnion, orderDirection: string): string { return `attributeNatural('${attributeSchema.name}', ${orderDirection.toUpperCase()})` } diff --git a/src/services/editor/data-grid-console/graphql-query-builder.ts b/src/services/editor/data-grid-console/graphql-query-builder.ts index d0e90296..e57e3a09 100644 --- a/src/services/editor/data-grid-console/graphql-query-builder.ts +++ b/src/services/editor/data-grid-console/graphql-query-builder.ts @@ -177,6 +177,10 @@ export class GraphQLQueryBuilder implements QueryBuilder { ` } + buildPrimaryKeyOrderBy(orderDirection: string): string { + return `entityPrimaryKeyNatural: ${orderDirection.toUpperCase()}` + } + buildAttributeOrderBy(attributeSchema: AttributeSchemaUnion, orderDirection: string): string { return `attribute${attributeSchema.nameVariants.pascalCase}Natural: ${orderDirection.toUpperCase()}` } diff --git a/src/services/editor/data-grid-console/query-builder.ts b/src/services/editor/data-grid-console/query-builder.ts index b743b761..4f5d064e 100644 --- a/src/services/editor/data-grid-console/query-builder.ts +++ b/src/services/editor/data-grid-console/query-builder.ts @@ -25,6 +25,13 @@ export interface QueryBuilder { pageNumber: number, pageSize: number): Promise + /** + * Builds single entityPrimaryKeyNatural order constraint in language of implementation for order by clause. + * + * @param orderDirection direction of order by clause + */ + buildPrimaryKeyOrderBy(orderDirection: string): string + /** * Builds single attributeNatural order constraint in language of implementation for order by clause. *