Skip to content

Commit

Permalink
feat(code-gen): add sql order by support (#721)
Browse files Browse the repository at this point in the history
Supports both a raw query part and array defining the sorted columns. If an array is passed, the `orderBySpec` argument is used to determine the order. Note that if no spec is given, the order is `ASC` as is the Postgres default.

When more custom ordering is necessary you should be able to just construct a query part, the `orderBySpec` and `tableName` are then ignored.

Closes #704
  • Loading branch information
dirkdev98 authored Feb 24, 2021
1 parent 3083220 commit 519300e
Show file tree
Hide file tree
Showing 20 changed files with 2,382 additions and 286 deletions.
25 changes: 25 additions & 0 deletions gen/code-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,31 @@ function getTypes(T) {
})
.optional()
.loose(),
orderBy: T.object()
.keys({
type: T.string(),
specType: T.string(),
fields: T.array().values(
T.object()
.keys({
key: T.string(),
optional: T.bool(),
options: [
T.string().oneOf(
"ASC",
"DESC",
"ASC NULLS FIRST",
"ASC NULLS LAST",
"DESC NULLS FIRST",
"DESC NULLS LAST",
),
],
})
.loose(),
),
})
.optional()
.loose(),
partial: T.object()
.keys({
insertType: T.string(),
Expand Down
2 changes: 1 addition & 1 deletion gen/testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export function applyTestingSqlStructure(app) {
T.object("post")
.docs("Store a 'user' post.")
.keys({
title: T.string(),
title: T.string().searchable(),
body: T.string(),
})
.enableQueries({ withSoftDeletes: true })
Expand Down
7 changes: 2 additions & 5 deletions packages/code-gen/src/builders/AnyOfType.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isNil } from "@compas/stdlib";
import { stringifyType } from "../stringify.js";
import { TypeBuilder } from "./TypeBuilder.js";
import { buildOrInfer } from "./utils.js";
Expand Down Expand Up @@ -32,17 +31,15 @@ export class AnyOfType extends TypeBuilder {
...this.data,
...AnyOfType.getBaseData(),
};

this.internalValues = [];
}

/**
* @param {...TypeBuilderLike} [items]
* @returns {AnyOfType}
*/
values(...items) {
if (isNil(this.internalValues)) {
this.internalValues = [];
}

this.internalValues.push(...items);

return this;
Expand Down
188 changes: 187 additions & 1 deletion packages/code-gen/src/generated/anonymous-validators.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/code-gen/src/generated/types.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/code-gen/src/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { generateReactQueryFiles } from "./reactQuery/index.js";
import { generateRouterFiles } from "./router/index.js";
import { addFieldsOfRelations } from "./sql/add-fields.js";
import { generateModelFiles } from "./sql/models.js";
import { createOrderByTypes } from "./sql/order-by-type.js";
import { createPartialTypes } from "./sql/partial-type.js";
import { createQueryBuilderTypes } from "./sql/query-builder.js";
import { generateSqlStructure } from "./sql/structure.js";
Expand Down Expand Up @@ -112,6 +113,7 @@ export async function generate(logger, options, structure) {
generateSqlStructure(context);

createWhereTypes(context);
createOrderByTypes(context);
createPartialTypes(context);
createQueryBuilderTypes(context);

Expand Down
Loading

0 comments on commit 519300e

Please sign in to comment.