Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
48 changes: 48 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Validate

on:
push:
branches:
- master
pull_request:

permissions:
contents: read

jobs:
validate:
name: Validate packages
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: 22

- name: Enable Corepack
run: corepack enable

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "dir=$(yarn config get cacheFolder)" >> "$GITHUB_OUTPUT"

- name: Cache yarn dependencies
uses: actions/cache@v4
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-

- name: Install dependencies
run: yarn install --immutable

- name: Compile packages
run: yarn workspaces foreach -A --topological run compile

- name: Validate
run: yarn validate
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "ai-conventions/general"]
path = ai-conventions/general
url = git@github.com:wookieb/conventions.git
1 change: 1 addition & 0 deletions ai-conventions/general
Submodule general added at dbe2b4
8 changes: 8 additions & 0 deletions config/entrysmith.config.base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
entrypoints: ["index.ts"],
entrypointOutputMode: "esm",
typescript: {
tsConfigReferenceTargetPath: "tsconfig.build.json",
referenceTsConfigPaths: ["tsconfig.json", "tsconfig.build.json"],
},
};
13 changes: 13 additions & 0 deletions config/eslint.config.base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = [
...require("@pallad/eslint-config"),
{
ignores: ["package/main/src/__tests__/example-monorepo/**"],
},
{
rules: {
"@typescript-eslint/no-useless-constructor": ["off"],
"no-console": ["error", { allow: ["warn", "error"] }],
"no-restricted-syntax": ["off"],
},
},
];
14 changes: 14 additions & 0 deletions config/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"declaration": true,
"importHelpers": true,
"module": "node20",
"moduleResolution": "nodenext",
"esModuleInterop": true,
"strict": true,
"target": "es2024",
"declarationMap": true,
"skipLibCheck": true,
"strictNullChecks": true
}
}
18 changes: 18 additions & 0 deletions config/vitest.config.base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
globals: true,
environment: "node",
testTimeout: 30000,
projects: [
{
extends: true,
test: {
name: "unit",
include: ["src/**/__tests__/*.spec.ts"],
},
},
],
},
});
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./config/eslint.config.base.js");
3 changes: 0 additions & 3 deletions jest.config.js

This file was deleted.

14 changes: 14 additions & 0 deletions knip.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"ignore": ["config/entrysmith.config.base.js", "package/*/entrysmith.config.js"],
"ignoreDependencies": [
"@pallad/entrysmith",
"@pallad/errors-dev",
"@pallad/plop-templates",
"barrelsby",
"conditional-type-checks",
"eslint",
"lerna",
"prettier"
],
"ignoreUnresolved": ["tslib"]
}
4 changes: 4 additions & 0 deletions opencode.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://opencode.ai/config.json",
"instructions": ["ai-conventions/general/*.md"]
}
28 changes: 21 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
{
"name": "@pallad/query-monorepo",
"private": true,
"packageManager": "yarn@4.11.0",
"scripts": {
"validate": "yarn check && yarn workspaces foreach -A --topological-dev run lint && yarn workspaces foreach -A --topological-dev run test",
"check": "yarn check:manypkg && yarn check:knip",
"check:knip": "knip",
"check:manypkg": "manypkg check",
"fix:manypkg": "manypkg fix"
},
"devDependencies": {
"@babel/core": "^7.23.7",
"@babel/preset-typescript": "^7.23.3",
"@manypkg/cli": "^0.25.1",
"@pallad/entrysmith": "^0.2.1",
"@pallad/errors-dev": "^1.1.0",
"@pallad/scripts": "^4",
"@pallad/eslint-config": "^2",
"@pallad/plop-templates": "^1.7.0",
"barrelsby": "^2.8.1",
"conditional-type-checks": "^1.0.5",
"lerna": "^8.0.0",
"eslint": "^9",
"knip": "^6.14.2",
"lerna": "^9",
"prettier": "^3.2.2",
"typescript": "^5"
"typescript": "^5",
"vitest": "^4.1.4"
},
"workspaces": [
"packages/*"
]
"package/*"
],
"manypkg": {}
}
97 changes: 97 additions & 0 deletions package/descriptor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# @pallad/query-descriptor

`@pallad/query-descriptor` builds typed, runtime-validated query contracts for `@pallad/query`.

The main entry point is `QueryDescriptor`. It combines optional filter validation, pagination configuration, and sorting configuration into one Zod schema. The same descriptor can then parse incoming query input and create response metadata for result lists.

## QueryDescriptor

`QueryDescriptor` is a builder for query objects. It supports:

- `filterSchema(schema)` - adds a Zod object schema under `filter`.
- `paginationByOffset(options?)` - adds `offset` and `limit` pagination.
- `paginationByCursor(options?)` - adds `before`, `after`, and `limit` cursor pagination.
- `sortingBySingleField(config)` - allows one sortable field.
- `sortingByMultipleFields(config)` - allows a non-empty list of sortable fields.
- `createQuery(input)` - parses input through the generated Zod schema.
- `createResult(query, entityList, paginationContext?)` - creates response data with list/page/sort metadata.

Each builder method updates TypeScript input, output, pagination, and sorting types. `querySchema` is generated lazily and cached until descriptor configuration changes.

## Example

```ts
import { QueryDescriptor } from "@pallad/query-descriptor";
import { z } from "zod";

const descriptor = new QueryDescriptor()
.filterSchema(
z.object({
status: z.enum(["active", "archived"]).optional(),
})
)
.paginationByOffset({ defaultLimit: 20 })
.sortingByMultipleFields({
sortableFields: ["name", "createdAt"],
defaultSorting: [{ field: "createdAt", direction: "DESC" }],
});

const query = descriptor.createQuery({
filter: { status: "active" },
offset: 0,
sortBy: [{ field: "name", direction: "ASC" }],
});

const result = descriptor.createResult(query, [{ id: "user-1", name: "Ada" }], {
hasNextPage: false,
hasPreviousPage: false,
});
```

`query` contains parsed filter, pagination defaults, and validated sorting. `result` contains the entity list, pagination `pageInfo`, and `sortBy` metadata.

## Pagination

Offset pagination produces input with `offset` and `limit`, and result metadata under `pageInfo`:

```ts
{
list: entities,
pageInfo: {
offset,
limit,
hasNextPage,
hasPreviousPage,
},
}
```

Cursor pagination produces input with optional `before`/`after` cursors and `limit`. Result data follows the connection shape:

```ts
{
edges: [{ node, cursor }],
nodes: entities,
pageInfo: {
limit,
startCursor,
endCursor,
hasNextPage,
hasPreviousPage,
},
}
```

Cursor pagination requires single-field sorting and rejects multi-field sorting. Cursors use entity `id` by default; pass `idExtractor` in `createResult` context when entities do not expose a string `id` field.

## Sorting

Single-field sorting validates one `sortBy` object:

```ts
{ sortBy: { field: "name", direction: "ASC" } }
```

Multi-field sorting validates a non-empty `sortBy` array and applies configured default sorting when `sortBy` is missing.

Both sorting modes add `sortBy` metadata to results through `createResult`.
3 changes: 3 additions & 0 deletions package/descriptor/entrysmith.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require("../../config/entrysmith.config.base.js"),
};
1 change: 1 addition & 0 deletions package/descriptor/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("../../config/eslint.config.base.js");
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
"version": "5.1.2",
"description": "",
"scripts": {
"test": "pallad-scripts test",
"lint": "pallad-scripts lint",
"compile": "npx preconstruct build",
"prepublishOnly": "npm run compile"
"test": "yarn run -TB vitest run --passWithNoTests",
"lint": "yarn run -TB eslint src/**/*.ts",
"build:clean": "yarn run -TB tsc --build --clean tsconfig.build.json",
"entrysmith": "yarn run generate-barrels && yarn run -TB entrysmith fix",
"prepare": "yarn run entrysmith",
"prepublish": "yarn run compile",
"compile": "yarn run entrysmith && yarn run build:clean && yarn run -TB tsc --build tsconfig.build.json",
"generate-barrels": "yarn run -TB barrelsby -l replace -L --delete -d ./src"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/pallad-ts/query.git",
"directory": "packages/builder"
"directory": "packages/descriptor"
},
"publishConfig": {
"access": "public"
Expand All @@ -31,8 +37,12 @@
"@pallad/cursor-encoder": "^1.1.0",
"@pallad/errors": "^1.0.1",
"@pallad/query": "^4.0.5",
"@sweet-monads/either": "^3.3.1",
"@sweet-monads/maybe": "^3",
"zod": "^4"
},
"exports": {
".": {
"import": "./dist/index.js"
},
"./package.json": "./package.json"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export const DEFAULT_MAX_LIMIT = 1000;

export interface PaginationDescriptor<TInput, TOutput> {
readonly schema: z.ZodType<TOutput, TInput>;
createInitialResult<T>(query: TOutput, list: T[], context: unknown): any;
}
Loading
Loading