Skip to content

Commit

Permalink
feat: support the new primary key sorting in data grid
Browse files Browse the repository at this point in the history
  • Loading branch information
lukashornych committed Dec 17, 2023
1 parent f162a04 commit 7fad725
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 58 deletions.
4 changes: 2 additions & 2 deletions src/components/lab/editor/data-grid/LabEditorDataGrid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const prependIcon = computed<string | undefined>(() => {
}
return undefined
})
const sortable = computed<boolean>(() => props.column.descriptor?.schema?.sortable)
const sortable = computed<boolean>(() => props.column.descriptor?.isSortable())
const sorted = computed<boolean>(() => props.isSorted(props.column))
const localized = computed<boolean>(() => props.column.descriptor?.schema?.localized)
const localized = computed<boolean>(() => props.column.descriptor?.isLocalized())
function handleClick() {
if (sortable.value) {
Expand Down
30 changes: 25 additions & 5 deletions src/model/editor/data-grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
}
}

/**
Expand Down
100 changes: 51 additions & 49 deletions src/services/editor/data-grid-console.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -162,67 +164,67 @@ export class DataGridConsoleService {
async getEntityPropertyDescriptors(dataPointer: DataGridDataPointer): Promise<EntityPropertyDescriptor[]> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()})`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`
}
Expand Down
7 changes: 7 additions & 0 deletions src/services/editor/data-grid-console/query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export interface QueryBuilder {
pageNumber: number,
pageSize: number): Promise<string>

/**
* 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.
*
Expand Down

0 comments on commit 7fad725

Please sign in to comment.