Skip to content

Commit f631f39

Browse files
committed
Use default primary sort of createdAt_DESC, id_DESC
1 parent 6495bfe commit f631f39

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/model/config/indices.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DirectiveNode, ObjectValueNode, StringValueNode } from 'graphql';
2+
import { OrderDirection } from '../implementation/order';
23

34
export interface IndexDefinitionConfig {
45
readonly name?: string;
@@ -23,7 +24,7 @@ export interface IndexDefinitionConfig {
2324

2425
export interface FlexSearchPrimarySortClauseConfig {
2526
readonly field: string;
26-
readonly asc: boolean;
27+
readonly direction: OrderDirection;
2728
}
2829

2930
export interface FlexSearchIndexConfig {

src/model/create-model.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ import {
110110
} from './config';
111111
import { BillingConfig } from './config/billing';
112112
import { Model } from './implementation';
113+
import { OrderDirection } from './implementation/order';
113114
import { parseBillingConfigs } from './parse-billing';
114115
import { parseI18nConfigs } from './parse-i18n';
115116
import { parseTTLConfigs } from './parse-ttl';
@@ -342,7 +343,7 @@ function getFlexSearchOrder(rootEntityDirective?: DirectiveNode): ReadonlyArray<
342343
.map((value: any) => {
343344
return {
344345
field: value.field,
345-
asc: value.direction === 'ASC' ? true : false
346+
direction: value.direction === 'DESC' ? OrderDirection.DESCENDING : OrderDirection.ASCENDING
346347
};
347348
});
348349
}

src/model/implementation/root-entity-type.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { GraphQLID, GraphQLString } from 'graphql';
22
import memorize from 'memorize-decorator';
33
import {
44
ACCESS_GROUP_FIELD,
5+
ENTITY_CREATED_AT,
56
FLEX_SEARCH_FULLTEXT_INDEXED_DIRECTIVE,
67
FLEX_SEARCH_INDEXED_DIRECTIVE,
78
FLEX_SEARCH_ORDER_ARGUMENT,
@@ -349,21 +350,29 @@ export class RootEntityType extends ObjectTypeBase {
349350
// primary sort is only used for sorting, so make sure it's unique
350351
// - this makes querying more consistent
351352
// - this enables us to use primary sort for cursor-based pagination (which requires an absolute sort order)
353+
// the default primary sort should be createdAt_DESC, because this is useful most of the time. to avoid
354+
// surprises when you do specify a primary sort, always add this default at the end (as long as it's not already
355+
// included in the index)
356+
if (!clauses.some(clause => clause.field === ENTITY_CREATED_AT)) {
357+
clauses = [
358+
...clauses,
359+
{
360+
field: ENTITY_CREATED_AT,
361+
direction: OrderDirection.DESCENDING
362+
}
363+
];
364+
}
352365
if (!clauses.some(clause => clause.field === this.discriminatorField.name)) {
353366
clauses = [
354367
...clauses,
355368
{
356369
field: this.discriminatorField.name,
357-
asc: true
370+
direction: OrderDirection.DESCENDING
358371
}
359372
];
360373
}
361374
return clauses.map(
362-
c =>
363-
new FlexSearchPrimarySortClause(
364-
new FieldPath({ path: c.field, baseType: this }),
365-
c.asc ? OrderDirection.ASCENDING : OrderDirection.DESCENDING
366-
)
375+
c => new FlexSearchPrimarySortClause(new FieldPath({ path: c.field, baseType: this }), c.direction)
367376
);
368377
}
369378

0 commit comments

Comments
 (0)