@telamonjs/data 0.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,30 @@
1
+ # Changelog
2
+
3
+ ## 1.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 5d52681: `createCachedPaginatedService` no longer shares cached pages or filter options between users when the service has a `visibility` rule: the cache key now includes the signed-in user's id, and nothing is cached when nobody is signed in. Services without `visibility` keep their existing keys.
8
+
9
+ `sanitizePaginationParams` now treats a non-numeric or non-finite `page`/`pageSize` (such as `?page=abc`) as page 1 and the default page size, instead of sending `NaN` to the query.
10
+
11
+ `buildIncludeClause` returns a new object instead of adding `_count` to the `include` you passed in.
12
+
13
+ Filter options now count distinct values with a Prisma `groupBy` instead of loading every row of the table; the returned options are unchanged.
14
+
15
+ ## 1.0.0
16
+
17
+ ### Major Changes
18
+
19
+ - First release: the server half of a TelamonJS list, moved out of `@telamonjs/ui/server` so a
20
+ backend uses it without installing React.
21
+ - `@telamonjs/data`: the `ListPage` wire contract (`PaginationParams`, `PaginatedResponse`,
22
+ `SortField`, `createPaginatedResponse`), `sanitizePaginationParams`, and `resolveSort` /
23
+ `resolveSortPath`, which read both sort wire formats (`customer.name` and the TanStack id
24
+ `customer_name`) and validate them against `sortableFields`.
25
+ - `@telamonjs/data/prisma`: `buildPrismaOrderBy` / `buildPrismaOrderByMulti` (relation counts
26
+ with `_count.x`), `buildSearchConditions` / `buildFilterConditions` / `buildWhereClause`
27
+ (search, filters, `baseWhere` and visibility combined with `AND`), and
28
+ `createPaginationContext`.
29
+ - Zero dependencies and no React, Next or Prisma import. The boolean filter labels are injected
30
+ (`filterLabels`, English by default), and configuration warnings go to `onWarning`.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Marcos Tullyo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,92 @@
1
1
  # @telamonjs/data
2
2
 
3
- Placeholder that reserves the package name. Install `@telamonjs/data@^1.0.0`.
3
+ The server half of a TelamonJS list. A `ListPage` sends page, page size, search, sort and filters.
4
+ This package reads them on the endpoint and turns them into a query, with no dependencies and
5
+ without React, Next or Prisma.
6
+
7
+ ```bash
8
+ pnpm add @telamonjs/data
9
+ ```
10
+
11
+ | Entry | What it has | Needs an ORM? |
12
+ | --- | --- | --- |
13
+ | `@telamonjs/data` | The wire contract (`PaginationParams`, `PaginatedResponse`, `SortField`, `createPaginatedResponse`), `sanitizePaginationParams`, `resolveSort`, `resolveSortPath` | No |
14
+ | `@telamonjs/data/prisma` | `buildPrismaOrderBy(Multi)`, `buildSearchConditions`, `buildFilterConditions`, `buildWhereClause`, `createPaginationContext` | Builds Prisma's object shape; never imports `@prisma/client` |
15
+
16
+ ## Sorting what a list sends
17
+
18
+ A `ListPage` sends the sort field in one of two forms: the column's data path (`customer.name`,
19
+ with `list.sortParam: 'path'`) or the TanStack column id (`customer_name`, the default). Both
20
+ resolve to the same path:
21
+
22
+ ```ts
23
+ import { resolveSortPath } from '@telamonjs/data'
24
+ import { buildPrismaOrderBy } from '@telamonjs/data/prisma'
25
+
26
+ resolveSortPath('customer_name') // 'customer.name'
27
+ buildPrismaOrderBy('customer_name', 'asc') // { customer: { name: 'asc' } }
28
+ buildPrismaOrderBy('_count.items', 'desc') // { items: { _count: 'desc' } }
29
+ buildPrismaOrderBy('created_at', 'asc', ['created_at']) // { created_at: 'asc' }
30
+ ```
31
+
32
+ Pass the endpoint's `sortableFields`. A listed snake_case field (`created_at`) then stays a
33
+ scalar, and anything not listed is rejected:
34
+
35
+ ```ts
36
+ import { resolveSort } from '@telamonjs/data'
37
+
38
+ const sort = resolveSort(query.sortBy ?? 'name', query.sortOrder, {
39
+ defaultOrderBy: 'name',
40
+ defaultOrderDirection: 'asc',
41
+ sortableFields: ['name', 'customer.name', 'created_at'],
42
+ })
43
+ ```
44
+
45
+ Without an ORM, `resolveSort` and `resolveSortPath` are all you need to build your own
46
+ `ORDER BY`.
47
+
48
+ ## Scope-safe `where`
49
+
50
+ `buildWhereClause` combines search and filters with the endpoint's `baseWhere` (for example
51
+ `{ deletedAt: null }`) and the user's visibility rule using `AND`. None of them can overwrite
52
+ another, even when they share a key:
53
+
54
+ ```ts
55
+ import { buildSearchConditions, buildWhereClause } from '@telamonjs/data/prisma'
56
+
57
+ const where = buildWhereClause(
58
+ buildSearchConditions(query.search, ['name', 'customer.name']),
59
+ [],
60
+ { deletedAt: null },
61
+ { OR: [{ ownerId: user.id }, { public: true }] }
62
+ )
63
+ ```
64
+
65
+ ## Paginated services
66
+
67
+ `createPaginationContext` builds a `list` (and `getFilterOptions`) per model from a declarative
68
+ config: search fields, typed filters, `sortableFields`, visibility, counts and hooks.
69
+
70
+ ```ts
71
+ import { createPaginationContext } from '@telamonjs/data/prisma'
72
+
73
+ export const { createPaginatedService } = createPaginationContext({
74
+ prisma,
75
+ auth, // () => Promise<{ user } | null>
76
+ filterLabels: () => ({ yes: 'Sim', no: 'Não', active: 'Ativo', inactive: 'Inativo' }),
77
+ })
78
+
79
+ export const customers = createPaginatedService({
80
+ model: 'customer',
81
+ searchFields: ['name', 'email'],
82
+ filters: { active: 'boolean', state: 'string' },
83
+ sortableFields: ['name', 'created_at', '_count.orders'],
84
+ })
85
+ ```
86
+
87
+ `@telamonjs/ui/server` re-exports these helpers for Next.js server code. A NestJS or plain Node
88
+ API installs only this package.
89
+
90
+ ## License
91
+
92
+ MIT
@@ -0,0 +1,51 @@
1
+ /**
2
+ * The wire contract between a TelamonJS `ListPage` and the endpoint behind it.
3
+ *
4
+ * @module data
5
+ */
6
+ /** One column of a (possibly multi-column) sort. */
7
+ export interface SortField {
8
+ field: string;
9
+ order: 'asc' | 'desc';
10
+ }
11
+ /** What a list sends to its `queryFn`, and so to the endpoint. */
12
+ export interface PaginationParams {
13
+ page: number;
14
+ pageSize: number;
15
+ search?: string;
16
+ /** One field (with `sortOrder`) or several `SortField`s */
17
+ sortBy?: string | SortField[];
18
+ /** Used only when `sortBy` is a string */
19
+ sortOrder?: 'asc' | 'desc';
20
+ /**
21
+ * App-defined filters (`clienteId`, `status`...). `any` on purpose: the values
22
+ * are heterogeneous and go straight into the query layer's where clauses.
23
+ */
24
+ filters?: Record<string, any>;
25
+ }
26
+ /** What the endpoint answers: one page plus the totals the list paginates with. */
27
+ export interface PaginatedResponse<T> {
28
+ data: T[];
29
+ total: number;
30
+ page: number;
31
+ pageSize: number;
32
+ totalPages: number;
33
+ }
34
+ /** Builds a `PaginatedResponse`, deriving `totalPages`. */
35
+ export declare function createPaginatedResponse<T>(data: T[], total: number, page: number, pageSize: number): PaginatedResponse<T>;
36
+ /**
37
+ * Clamps client-supplied paging: `page` ≥ 1, `1 ≤ pageSize ≤ maxPageSize`
38
+ * (default 100), both floored; a missing `pageSize` is `defaultPageSize` (10).
39
+ * Non-numeric or non-finite values (`?page=abc`) fall back to page 1 and the
40
+ * default page size instead of reaching the query as `NaN`.
41
+ */
42
+ export declare function sanitizePaginationParams(params: {
43
+ page?: number;
44
+ pageSize?: number;
45
+ maxPageSize?: number;
46
+ defaultPageSize?: number;
47
+ }): {
48
+ page: number;
49
+ pageSize: number;
50
+ };
51
+ //# sourceMappingURL=contract.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../src/contract.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,oDAAoD;AACpD,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,KAAK,GAAG,MAAM,CAAA;CACtB;AAED,kEAAkE;AAClE,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,2DAA2D;IAC3D,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,EAAE,CAAA;IAC7B,0CAA0C;IAC1C,SAAS,CAAC,EAAE,KAAK,GAAG,MAAM,CAAA;IAC1B;;;OAGG;IAEH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC9B;AAED,mFAAmF;AACnF,MAAM,WAAW,iBAAiB,CAAC,CAAC;IAClC,IAAI,EAAE,CAAC,EAAE,CAAA;IACT,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,UAAU,EAAE,MAAM,CAAA;CACnB;AAED,2DAA2D;AAC3D,wBAAgB,uBAAuB,CAAC,CAAC,EACvC,IAAI,EAAE,CAAC,EAAE,EACT,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,GACf,iBAAiB,CAAC,CAAC,CAAC,CAQtB;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE;IAC/C,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,eAAe,CAAC,EAAE,MAAM,CAAA;CACzB,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAarC"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The wire contract between a TelamonJS `ListPage` and the endpoint behind it.
3
+ *
4
+ * @module data
5
+ */
6
+ /** Builds a `PaginatedResponse`, deriving `totalPages`. */
7
+ export function createPaginatedResponse(data, total, page, pageSize) {
8
+ return {
9
+ data,
10
+ total,
11
+ page,
12
+ pageSize,
13
+ totalPages: Math.ceil(total / pageSize),
14
+ };
15
+ }
16
+ /**
17
+ * Clamps client-supplied paging: `page` ≥ 1, `1 ≤ pageSize ≤ maxPageSize`
18
+ * (default 100), both floored; a missing `pageSize` is `defaultPageSize` (10).
19
+ * Non-numeric or non-finite values (`?page=abc`) fall back to page 1 and the
20
+ * default page size instead of reaching the query as `NaN`.
21
+ */
22
+ export function sanitizePaginationParams(params) {
23
+ const fallbackPageSize = params.defaultPageSize || 10;
24
+ const { page = 1, pageSize = fallbackPageSize, maxPageSize = 100 } = params;
25
+ const finitePage = Math.floor(Number(page));
26
+ const finitePageSize = Math.floor(Number(pageSize));
27
+ return {
28
+ page: Number.isFinite(finitePage) ? Math.max(1, finitePage) : 1,
29
+ pageSize: Math.min(maxPageSize, Math.max(1, Number.isFinite(finitePageSize) ? finitePageSize : fallbackPageSize)),
30
+ };
31
+ }
32
+ //# sourceMappingURL=contract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract.js","sourceRoot":"","sources":["../src/contract.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAkCH,2DAA2D;AAC3D,MAAM,UAAU,uBAAuB,CACrC,IAAS,EACT,KAAa,EACb,IAAY,EACZ,QAAgB;IAEhB,OAAO;QACL,IAAI;QACJ,KAAK;QACL,IAAI;QACJ,QAAQ;QACR,UAAU,EAAE,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,QAAQ,CAAC;KACxC,CAAA;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CAAC,MAKxC;IACC,MAAM,gBAAgB,GAAG,MAAM,CAAC,eAAe,IAAI,EAAE,CAAA;IACrD,MAAM,EAAE,IAAI,GAAG,CAAC,EAAE,QAAQ,GAAG,gBAAgB,EAAE,WAAW,GAAG,GAAG,EAAE,GAAG,MAAM,CAAA;IAC3E,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAA;IAC3C,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAA;IAEnD,OAAO;QACL,IAAI,EAAE,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;QAC/D,QAAQ,EAAE,IAAI,CAAC,GAAG,CAChB,WAAW,EACX,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,gBAAgB,CAAC,CACjF;KACF,CAAA;AACH,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * `@telamonjs/data` — the server half of a TelamonJS list, ORM-agnostic:
3
+ * the wire contract a `ListPage` sends and expects, and the sort/paging
4
+ * sanitizers an endpoint runs on it. ORM adapters live in subpaths
5
+ * (`@telamonjs/data/prisma`).
6
+ *
7
+ * @module data
8
+ */
9
+ export type { SortField, PaginationParams, PaginatedResponse } from './contract.js';
10
+ export { createPaginatedResponse, sanitizePaginationParams } from './contract.js';
11
+ export { resolveSort, resolveSortPath } from './sort.js';
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,YAAY,EAAE,SAAS,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACnF,OAAO,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAA;AACjF,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * `@telamonjs/data` — the server half of a TelamonJS list, ORM-agnostic:
3
+ * the wire contract a `ListPage` sends and expects, and the sort/paging
4
+ * sanitizers an endpoint runs on it. ORM adapters live in subpaths
5
+ * (`@telamonjs/data/prisma`).
6
+ *
7
+ * @module data
8
+ */
9
+ export { createPaginatedResponse, sanitizePaginationParams } from './contract.js';
10
+ export { resolveSort, resolveSortPath } from './sort.js';
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAA;AACjF,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * `@telamonjs/data/prisma` — the Prisma adapter of a TelamonJS list:
3
+ * `orderBy` from the sort a `ListPage` sends, `where` composition that keeps
4
+ * every scope rule, and a declarative paginated service. Builds plain
5
+ * objects in Prisma's shape; it never imports `@prisma/client`.
6
+ *
7
+ * @module data/prisma
8
+ */
9
+ export { buildPrismaOrderBy, buildPrismaOrderByMulti } from './order-by.js';
10
+ export type { PrismaOrderBy } from './order-by.js';
11
+ export { buildSearchConditions, buildFilterConditions, buildWhereClause } from './where.js';
12
+ export { createPaginationContext } from './pagination.js';
13
+ export type { AuthUser, FilterFieldConfig, FilterFieldType, FilterLabels, FilterOption, FilterValue, PaginatedService, PaginatedServiceConfig, PaginationContext, PrismaClient, PrismaIncludeClause, PrismaWhereClause, SearchCondition, } from './types.js';
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/prisma/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAA;AAC3E,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAClD,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAC3F,OAAO,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAA;AACzD,YAAY,EACV,QAAQ,EACR,iBAAiB,EACjB,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,gBAAgB,EAChB,sBAAsB,EACtB,iBAAiB,EACjB,YAAY,EACZ,mBAAmB,EACnB,iBAAiB,EACjB,eAAe,GAChB,MAAM,YAAY,CAAA"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * `@telamonjs/data/prisma` — the Prisma adapter of a TelamonJS list:
3
+ * `orderBy` from the sort a `ListPage` sends, `where` composition that keeps
4
+ * every scope rule, and a declarative paginated service. Builds plain
5
+ * objects in Prisma's shape; it never imports `@prisma/client`.
6
+ *
7
+ * @module data/prisma
8
+ */
9
+ export { buildPrismaOrderBy, buildPrismaOrderByMulti } from './order-by.js';
10
+ export { buildSearchConditions, buildFilterConditions, buildWhereClause } from './where.js';
11
+ export { createPaginationContext } from './pagination.js';
12
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/prisma/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAA;AAE3E,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAC3F,OAAO,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAA"}
@@ -0,0 +1,32 @@
1
+ import type { SortField } from '../contract.js';
2
+ /** A Prisma `orderBy` entry: nested objects ending in the direction. */
3
+ export type PrismaOrderBy = Record<string, unknown>;
4
+ /**
5
+ * Converts a client `sortBy` to Prisma `orderBy`.
6
+ *
7
+ * Accepts the column's data path (`customer.name`, sent with
8
+ * `list.sortParam: 'path'`) and the legacy TanStack column id (`customer_name`,
9
+ * the default wire format). `_count.<relation>` orders by a relation count.
10
+ * Pass the endpoint's `sortableFields` so a listed snake_case field
11
+ * (`created_at`) is not mistaken for a legacy id.
12
+ *
13
+ * @example
14
+ * buildPrismaOrderBy('customer.name', 'asc') // { customer: { name: 'asc' } }
15
+ * buildPrismaOrderBy('customer_name', 'asc') // { customer: { name: 'asc' } }
16
+ * buildPrismaOrderBy('_count.items', 'desc') // { items: { _count: 'desc' } }
17
+ * buildPrismaOrderBy('created_at', 'asc', ['created_at']) // { created_at: 'asc' }
18
+ */
19
+ export declare function buildPrismaOrderBy(sortBy: string, sortOrder?: 'asc' | 'desc', sortableFields?: readonly string[]): PrismaOrderBy;
20
+ /**
21
+ * Multi-column version of {@link buildPrismaOrderBy}: one Prisma `orderBy`
22
+ * entry per `SortField`, same field rules.
23
+ *
24
+ * @example
25
+ * buildPrismaOrderByMulti([
26
+ * { field: 'category.name', order: 'asc' },
27
+ * { field: 'startsAt', order: 'desc' },
28
+ * ])
29
+ * // [{ category: { name: 'asc' } }, { startsAt: 'desc' }]
30
+ */
31
+ export declare function buildPrismaOrderByMulti(sorting: SortField[], sortableFields?: readonly string[]): PrismaOrderBy[];
32
+ //# sourceMappingURL=order-by.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"order-by.d.ts","sourceRoot":"","sources":["../../src/prisma/order-by.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAG/C,wEAAwE;AACxE,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AAanD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,SAAS,GAAE,KAAK,GAAG,MAAe,EAClC,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,GACjC,aAAa,CAEf;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,SAAS,EAAE,EACpB,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,GACjC,aAAa,EAAE,CAIjB"}
@@ -0,0 +1,44 @@
1
+ import { resolveSortPath } from '../sort.js';
2
+ function toPrismaOrderBy(path, order) {
3
+ const keys = path.split('.');
4
+ // Prisma orders by a relation count as `{ items: { _count: 'asc' } }`
5
+ const i = keys.indexOf('_count');
6
+ if (i >= 0 && i === keys.length - 2) {
7
+ keys[i] = keys[i + 1];
8
+ keys[i + 1] = '_count';
9
+ }
10
+ return keys.reduceRight((acc, key) => ({ [key]: acc }), order);
11
+ }
12
+ /**
13
+ * Converts a client `sortBy` to Prisma `orderBy`.
14
+ *
15
+ * Accepts the column's data path (`customer.name`, sent with
16
+ * `list.sortParam: 'path'`) and the legacy TanStack column id (`customer_name`,
17
+ * the default wire format). `_count.<relation>` orders by a relation count.
18
+ * Pass the endpoint's `sortableFields` so a listed snake_case field
19
+ * (`created_at`) is not mistaken for a legacy id.
20
+ *
21
+ * @example
22
+ * buildPrismaOrderBy('customer.name', 'asc') // { customer: { name: 'asc' } }
23
+ * buildPrismaOrderBy('customer_name', 'asc') // { customer: { name: 'asc' } }
24
+ * buildPrismaOrderBy('_count.items', 'desc') // { items: { _count: 'desc' } }
25
+ * buildPrismaOrderBy('created_at', 'asc', ['created_at']) // { created_at: 'asc' }
26
+ */
27
+ export function buildPrismaOrderBy(sortBy, sortOrder = 'desc', sortableFields) {
28
+ return toPrismaOrderBy(resolveSortPath(sortBy, sortableFields), sortOrder);
29
+ }
30
+ /**
31
+ * Multi-column version of {@link buildPrismaOrderBy}: one Prisma `orderBy`
32
+ * entry per `SortField`, same field rules.
33
+ *
34
+ * @example
35
+ * buildPrismaOrderByMulti([
36
+ * { field: 'category.name', order: 'asc' },
37
+ * { field: 'startsAt', order: 'desc' },
38
+ * ])
39
+ * // [{ category: { name: 'asc' } }, { startsAt: 'desc' }]
40
+ */
41
+ export function buildPrismaOrderByMulti(sorting, sortableFields) {
42
+ return sorting.map(sort => toPrismaOrderBy(resolveSortPath(sort.field, sortableFields), sort.order));
43
+ }
44
+ //# sourceMappingURL=order-by.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"order-by.js","sourceRoot":"","sources":["../../src/prisma/order-by.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAK5C,SAAS,eAAe,CAAC,IAAY,EAAE,KAAqB;IAC1D,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAC5B,sEAAsE;IACtE,MAAM,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;IAChC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACpC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;QACrB,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAA;IACxB,CAAC;IACD,OAAO,IAAI,CAAC,WAAW,CAAU,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,KAAK,CAAkB,CAAA;AAC1F,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,kBAAkB,CAChC,MAAc,EACd,YAA4B,MAAM,EAClC,cAAkC;IAElC,OAAO,eAAe,CAAC,eAAe,CAAC,MAAM,EAAE,cAAc,CAAC,EAAE,SAAS,CAAC,CAAA;AAC5E,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,uBAAuB,CACrC,OAAoB,EACpB,cAAkC;IAElC,OAAO,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CACxB,eAAe,CAAC,eAAe,CAAC,IAAI,CAAC,KAAK,EAAE,cAAc,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,CACzE,CAAA;AACH,CAAC"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * The Prisma pagination service: one `list` (and `getFilterOptions`) per
3
+ * model, from a declarative config.
4
+ *
5
+ * @module data/prisma
6
+ */
7
+ import type { PaginatedService, PaginatedServiceConfig, PaginationContext } from './types.js';
8
+ /**
9
+ * Create pagination context with dependency injection
10
+ * This is the main entry point for projects using the pagination system
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * // In your project's lib/pagination.ts
15
+ * import { createPaginationContext } from '@telamonjs/data/prisma'
16
+ * import { auth } from '@/auth'
17
+ * import { prisma } from '@/lib/prisma'
18
+ *
19
+ * export const { createPaginatedService } = createPaginationContext({
20
+ * prisma,
21
+ * auth
22
+ * })
23
+ * ```
24
+ */
25
+ export declare function createPaginationContext(context: PaginationContext): {
26
+ createPaginatedService: <T extends string>(config: PaginatedServiceConfig<T>) => PaginatedService<Record<string, unknown>>;
27
+ createCachedPaginatedService: <T extends string>(config: PaginatedServiceConfig<T>) => PaginatedService<Record<string, unknown>>;
28
+ createSimplePaginatedService: <T extends string>(model: T, searchFields?: string[]) => PaginatedService<Record<string, unknown>>;
29
+ };
30
+ //# sourceMappingURL=pagination.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pagination.d.ts","sourceRoot":"","sources":["../../src/prisma/pagination.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,OAAO,KAAK,EAGV,gBAAgB,EAChB,sBAAsB,EACtB,iBAAiB,EAClB,MAAM,YAAY,CAAA;AA2BnB;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,iBAAiB;6BA6ChC,CAAC,SAAS,MAAM,UACtC,sBAAsB,CAAC,CAAC,CAAC,KAChC,gBAAgB,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;mCAiON,CAAC,SAAS,MAAM,UAC5C,sBAAsB,CAAC,CAAC,CAAC,KAChC,gBAAgB,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;mCA8DN,CAAC,SAAS,MAAM,SAC7C,CAAC,iBACM,MAAM,EAAE,KACrB,gBAAgB,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;EAa7C"}