Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support multiple separate order bys. #1076

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/orm/src/EntityFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ColumnCondition, RawCondition } from "./QueryParser";
export type FilterAndSettings<T extends Entity> = {
where: FilterWithAlias<T>;
conditions?: ExpressionFilter;
orderBy?: OrderOf<T>;
orderBy?: OrderOf<T> | OrderOf<T>[];
limit?: number;
offset?: number;
softDeletes?: "exclude" | "include";
Expand Down
2 changes: 1 addition & 1 deletion packages/orm/src/EntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export interface EntityConstructor<T> {
/** Options for the auto-batchable `em.find` queries, i.e. limit & offset aren't allowed. */
export interface FindFilterOptions<T extends Entity> {
conditions?: ExpressionFilter;
orderBy?: OrderOf<T>;
orderBy?: OrderOf<T> | OrderOf<T>[];
softDeletes?: "include" | "exclude";
}

Expand Down
17 changes: 9 additions & 8 deletions packages/orm/src/QueryParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,10 @@ export function parseFindQuery(
}
}

function addOrderBy(meta: EntityMetadata, alias: string, orderBy: any): void {
// Assume only one key
function addOrderBy(meta: EntityMetadata, alias: string, orderBy: Record<string, any>): void {
const entries = Object.entries(orderBy);
if (entries.length === 0) {
return;
}
Object.entries(orderBy).forEach(([key, value]) => {
if (entries.length === 0) return;
for (const [key, value] of entries) {
const field = meta.allFields[key] ?? fail(`${key} not found on ${meta.tableName}`);
if (field.kind === "primitive" || field.kind === "primaryKey" || field.kind === "enum") {
const column = field.serde.columns[0];
Expand Down Expand Up @@ -415,7 +412,7 @@ export function parseFindQuery(
} else {
throw new Error(`Unsupported field ${key}`);
}
});
}
}

// always add the main table
Expand Down Expand Up @@ -443,7 +440,11 @@ export function parseFindQuery(
}

if (orderBy) {
addOrderBy(meta, alias, orderBy);
if (Array.isArray(orderBy)) {
for (const ob of orderBy) addOrderBy(meta, alias, ob);
} else {
addOrderBy(meta, alias, orderBy);
}
}
maybeAddOrderBy(query, meta, alias);

Expand Down
29 changes: 29 additions & 0 deletions packages/tests/integration/src/EntityManager.queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,35 @@ describe("EntityManager.queries", () => {
});
});

it("can order by multiple m2os as an array", async () => {
await insertPublisher({ id: 1, name: "p1" });
await insertPublisher({ id: 2, name: "p2" });
await insertAuthor({ first_name: "a1", publisher_id: 2 });
await insertAuthor({ first_name: "a2", publisher_id: 1 });

const em = newEntityManager();
const orderBy1 = { currentDraftBook: { title: "ASC" } } satisfies AuthorOrder;
const orderBy2 = { publisher: { name: "ASC" } } satisfies AuthorOrder;
const authors = await em.find(Author, {}, { orderBy: [orderBy2, orderBy1] });
expect(authors.length).toEqual(2);
expect(authors[0].firstName).toEqual("a2");
expect(authors[1].firstName).toEqual("a1");

expect(parseFindQuery(am, {}, { ...opts, orderBy: [orderBy2, orderBy1] })).toEqual({
selects: [`a.*`],
tables: [
{ alias: "a", table: "authors", join: "primary" },
{ alias: "p", table: "publishers", join: "outer", col1: "a.publisher_id", col2: "p.id", distinct: false },
{ alias: "b", table: "books", join: "outer", col1: "a.current_draft_book_id", col2: "b.id", distinct: false },
],
orderBys: [
{ alias: "p", column: "name", order: "ASC" },
{ alias: "b", column: "title", order: "ASC" },
{ alias: "a", column: "id", order: "ASC" },
],
});
});

it("can order by joined string asc", async () => {
await insertPublisher({ name: "pB" });
await insertPublisher({ id: 2, name: "pA" });
Expand Down