Skip to content

Commit

Permalink
Fix order by error with multiple id columns
Browse files Browse the repository at this point in the history
  • Loading branch information
noam-honig committed May 2, 2024
1 parent ab6e4cb commit 3721e85
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## [0.26.9] 2024-05-02

- Fix order by error with multiple id columns

## [0.26.8] 2024-05-01

- Added support for `sqlite3` that runs on stackblitz
Expand Down
17 changes: 4 additions & 13 deletions projects/core/src/sort.ts
Expand Up @@ -182,22 +182,13 @@ export class Sort {
): EntityOrderBy<T> {
if (!orderBy || Object.keys(orderBy).length === 0)
orderBy = entityMetadata.options.defaultOrderBy
if (!orderBy)
orderBy = {
[entityMetadata.idMetadata.field.key]: 'asc',
} as EntityOrderBy<T>
if (!orderBy) orderBy = {} as EntityOrderBy<T>
else orderBy = { ...orderBy }

if (entityMetadata.idMetadata.field instanceof CompoundIdField) {
for (const field of entityMetadata.idMetadata.field.fields) {
if (!orderBy[field.key]) {
orderBy[field.key] = 'asc'
}
for (const field of entityMetadata.idMetadata.fields) {
if (!orderBy[field.key]) {
orderBy[field.key] = 'asc'
}
} else if (!orderBy[entityMetadata.idMetadata.field.key]) {
orderBy[entityMetadata.idMetadata.field.key] = 'asc'
}

return orderBy
}
}
Expand Down
30 changes: 29 additions & 1 deletion projects/tests/tests/test-paged-foreach.spec.ts
@@ -1,5 +1,11 @@
import type { EntityFilter, EntityOrderBy } from '../../core'
import { CompoundIdField, Entity, EntityBase, Fields } from '../../core'
import {
CompoundIdField,
Entity,
EntityBase,
Fields,
InMemoryDataProvider,
} from '../../core'
import type { FieldMetadata } from '../../core/src/column-interfaces'
import { Remult, queryConfig } from '../../core/src/context'
import { entityFilterToJson } from '../../core/src/filter/filter-interfaces'
Expand All @@ -10,6 +16,7 @@ import { Categories } from './remult-3-entities'
import { describe, expect, it } from 'vitest'
import { testRestDb } from './testHelper'
import { getRepositoryInternals } from '../../core/src/remult3/repository-internals'
import { entity } from './dynamic-classes.js'

describe('test paged foreach ', () => {
queryConfig.defaultPageSize = 2
Expand Down Expand Up @@ -237,6 +244,27 @@ describe('test paged foreach ', () => {
}
expect(i).toBe(5)
})
it('test createUniqueSort with multiple Id Columns', async () => {
const task = new Remult(new InMemoryDataProvider()).repo(
entity(
'task',
{ a: Fields.number(), b: Fields.number(), c: Fields.number() },
{
id: {
a: true,
b: true,
},
},
),
)
expect(Sort.createUniqueEntityOrderBy(task.metadata))
.toMatchInlineSnapshot(`
{
"a": "asc",
"b": "asc",
}
`)
})
it('test make sort unique', async () => {
const remult = new Remult()
const e = remult.repo(Categories)
Expand Down

0 comments on commit 3721e85

Please sign in to comment.