@stacksjs/pagination 0.74.4
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/LICENSE.md +21 -0
- package/README.md +33 -0
- package/dist/index.d.ts +99 -0
- package/dist/index.js +2 -0
- package/package.json +47 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Open Web Foundation
|
|
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
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# @stacksjs/pagination
|
|
2
|
+
|
|
3
|
+
The paginator shapes the ORM produces and the router serializes.
|
|
4
|
+
|
|
5
|
+
It has no dependencies, and that is the point. Both `@stacksjs/orm` and
|
|
6
|
+
`@stacksjs/router` need to agree on what a page of rows looks like: the ORM
|
|
7
|
+
builds one from a query, the router recognises one in a handler's return value
|
|
8
|
+
and serializes it. When the shape lived in the ORM, the router had to depend on
|
|
9
|
+
the ORM to recognise it, while the ORM depends on the router to register the
|
|
10
|
+
routes its models generate - so the two packages depended on each other, and no
|
|
11
|
+
publish order could put one before the other.
|
|
12
|
+
|
|
13
|
+
Nothing here knows about queries or requests. It is the contract, three
|
|
14
|
+
duck-typed predicates for recognising it, and the adapters that convert the
|
|
15
|
+
shapes the underlying drivers return.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { isPaginator, toPaginator } from '@stacksjs/pagination'
|
|
19
|
+
|
|
20
|
+
const page = toPaginator(rows, { page: 2, perPage: 25, total: 413 })
|
|
21
|
+
|
|
22
|
+
if (isPaginator(value))
|
|
23
|
+
return Response.json(value)
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Shapes
|
|
27
|
+
|
|
28
|
+
- `Paginator` — knows `total`, so it can offer "jump to page N". Costs a `COUNT(*)`.
|
|
29
|
+
- `SimplePaginator` — knows only whether another page exists.
|
|
30
|
+
- `CursorPaginator` — opaque cursors, for tables too large to offset into.
|
|
31
|
+
|
|
32
|
+
All three are snake_case, matching Laravel's serialized paginator and the REST
|
|
33
|
+
convention across the Stacks API.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/** Duck-typed shape check — used by the response serializer (P4) to
|
|
2
|
+
* detect a paginator-shaped return value from an action. */
|
|
3
|
+
export declare function isPaginator<T = unknown>(value: unknown): value is Paginator<T>;
|
|
4
|
+
/** Duck-typed shape check for the simple paginator. */
|
|
5
|
+
export declare function isSimplePaginator<T = unknown>(value: unknown): value is SimplePaginator<T>;
|
|
6
|
+
/** Duck-typed shape check for the cursor paginator. */
|
|
7
|
+
export declare function isCursorPaginator<T = unknown>(value: unknown): value is CursorPaginator<T>;
|
|
8
|
+
/**
|
|
9
|
+
* Convert bun-query-builder's `paginate()` output (`{ data, meta:
|
|
10
|
+
* { perPage, page, total, lastPage } }`) into the canonical Stacks
|
|
11
|
+
* shape. P1 of the migration — the URL fields stay undefined until
|
|
12
|
+
* P2 wires the request-context resolver.
|
|
13
|
+
*/
|
|
14
|
+
export declare function toPaginator<T>(result: { data: T[], meta: { perPage: number, page: number, total: number, lastPage: number } }): Paginator<T>;
|
|
15
|
+
/** Convert bqb's `simplePaginate()` output to the canonical
|
|
16
|
+
* {@link SimplePaginator} shape. */
|
|
17
|
+
export declare function toSimplePaginator<T>(result: { data: T[], meta: { perPage: number, page: number, hasMore: boolean } }): SimplePaginator<T>;
|
|
18
|
+
/** Convert bqb's `cursorPaginate()` output to the canonical
|
|
19
|
+
* {@link CursorPaginator} shape. Cursors are serialized to strings
|
|
20
|
+
* via JSON — composite cursors (arrays) survive the round-trip. */
|
|
21
|
+
export declare function toCursorPaginator<T>(result: { data: T[], meta: { perPage: number, nextCursor: unknown, prevCursor: unknown } }): CursorPaginator<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Canonical paginator types + adapters (stacksjs/stacks#1905, P1 from #1910).
|
|
24
|
+
*
|
|
25
|
+
* Four pagination shapes used to ship across the framework — bqb's SQL
|
|
26
|
+
* client `{ data, meta }`, bqb's sync ORM `{ data, total, page, ... }`,
|
|
27
|
+
* the search-engine's `{ hits, total, page, perPage }`, and a fifth
|
|
28
|
+
* declared-but-not-implemented Stacks shape (`{ data, paging, next_cursor }`)
|
|
29
|
+
* that the runtime never actually produced. This module unifies them
|
|
30
|
+
* to the Laravel-flavored, snake_case shapes below.
|
|
31
|
+
*
|
|
32
|
+
* P1 ships only `data` + counts; the URL fields are declared but left
|
|
33
|
+
* `undefined` by default. P2 (stacksjs/stacks#1906) wires them in when
|
|
34
|
+
* a request is in scope.
|
|
35
|
+
*
|
|
36
|
+
* Shape choices:
|
|
37
|
+
* - snake_case for JSON friendliness (matches Laravel's serialized
|
|
38
|
+
* paginator + the existing REST convention across the Stacks API)
|
|
39
|
+
* - `from` / `to` are 1-indexed offsets of the first/last row on the
|
|
40
|
+
* current page; `null` when the page is empty
|
|
41
|
+
* - `has_more_pages` is the cheap boolean apps actually want — saves
|
|
42
|
+
* `current_page < last_page` checks everywhere
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* Full paginator — knows the total row count so it can compute
|
|
46
|
+
* `last_page`, supports "jump to page N" UI patterns. Costs an extra
|
|
47
|
+
* `COUNT(*)` query; use {@link SimplePaginator} or {@link CursorPaginator}
|
|
48
|
+
* for very large tables.
|
|
49
|
+
*/
|
|
50
|
+
export declare interface Paginator<T> {
|
|
51
|
+
data: T[]
|
|
52
|
+
current_page: number
|
|
53
|
+
per_page: number
|
|
54
|
+
total: number
|
|
55
|
+
last_page: number
|
|
56
|
+
from: number | null
|
|
57
|
+
to: number | null
|
|
58
|
+
has_more_pages: boolean
|
|
59
|
+
prev_page_url?: string | null
|
|
60
|
+
next_page_url?: string | null
|
|
61
|
+
first_page_url?: string
|
|
62
|
+
last_page_url?: string
|
|
63
|
+
path?: string
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Simple paginator — drops the `COUNT(*)` query. Faster on large tables
|
|
67
|
+
* but can't render "jump to page 5" UIs since `last_page` / `total` are
|
|
68
|
+
* unknown. Suitable for infinite-scroll feeds.
|
|
69
|
+
*/
|
|
70
|
+
export declare interface SimplePaginator<T> {
|
|
71
|
+
data: T[]
|
|
72
|
+
current_page: number
|
|
73
|
+
per_page: number
|
|
74
|
+
has_more_pages: boolean
|
|
75
|
+
prev_page_url?: string | null
|
|
76
|
+
next_page_url?: string | null
|
|
77
|
+
path?: string
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Cursor (keyset) paginator — uses `WHERE id > :cursor` ordering rather
|
|
81
|
+
* than `LIMIT/OFFSET`, so query cost stays constant regardless of how
|
|
82
|
+
* deep the user has paged. The trade-off is no random-access ("jump to
|
|
83
|
+
* page N"); only prev/next.
|
|
84
|
+
*
|
|
85
|
+
* Cursor values are opaque to the caller — serialize as JSON, send to
|
|
86
|
+
* the client, accept back unchanged. Implementations (cursorPaginate)
|
|
87
|
+
* decide the cursor shape (single column value, base64 tuple for
|
|
88
|
+
* composite keys, etc.).
|
|
89
|
+
*/
|
|
90
|
+
export declare interface CursorPaginator<T> {
|
|
91
|
+
data: T[]
|
|
92
|
+
per_page: number
|
|
93
|
+
next_cursor: string | null
|
|
94
|
+
prev_cursor: string | null
|
|
95
|
+
has_more_pages: boolean
|
|
96
|
+
prev_page_url?: string | null
|
|
97
|
+
next_page_url?: string | null
|
|
98
|
+
path?: string
|
|
99
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
function i(r){if(r===null||typeof r!=="object")return!1;let e=r;return Array.isArray(e.data)&&typeof e.current_page==="number"&&typeof e.per_page==="number"&&typeof e.total==="number"&&typeof e.last_page==="number"}function l(r){if(r===null||typeof r!=="object")return!1;let e=r;return Array.isArray(e.data)&&typeof e.current_page==="number"&&typeof e.per_page==="number"&&typeof e.has_more_pages==="boolean"&&!("total"in e)}function _(r){if(r===null||typeof r!=="object")return!1;let e=r;return Array.isArray(e.data)&&(typeof e.next_cursor==="string"||e.next_cursor===null)}function m(r){let{data:e}=r,{perPage:a,page:n,total:g,lastPage:t}=r.meta,o=e.length===0,u=o?null:(n-1)*a+1,s=o?null:u+e.length-1;return{data:e,current_page:n,per_page:a,total:g,last_page:t,from:u,to:s,has_more_pages:n<t}}function f(r){return{data:r.data,current_page:r.meta.page,per_page:r.meta.perPage,has_more_pages:r.meta.hasMore}}function c(r){return{data:r.data,per_page:r.meta.perPage,next_cursor:p(r.meta.nextCursor),prev_cursor:p(r.meta.prevCursor),has_more_pages:r.meta.nextCursor!==null&&r.meta.nextCursor!==void 0}}function p(r){if(r===null||r===void 0)return null;if(typeof r==="string")return r;return JSON.stringify(r)}export{_ as isCursorPaginator,i as isPaginator,l as isSimplePaginator,c as toCursorPaginator,m as toPaginator,f as toSimplePaginator};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stacksjs/pagination",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.74.4",
|
|
5
|
+
"description": "The paginator shapes the ORM produces and the router serializes.",
|
|
6
|
+
"author": "Chris Breuer",
|
|
7
|
+
"contributors": [
|
|
8
|
+
"Chris Breuer <chris@stacksjs.com>"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"funding": "https://github.com/sponsors/chrisbbreuer",
|
|
12
|
+
"homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/pagination#readme",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/stacksjs/stacks.git",
|
|
16
|
+
"directory": "./storage/framework/core/pagination"
|
|
17
|
+
},
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/stacksjs/stacks/issues"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"pagination",
|
|
23
|
+
"paginator",
|
|
24
|
+
"cursor",
|
|
25
|
+
"stacks"
|
|
26
|
+
],
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"bun": "./dist/index.js",
|
|
31
|
+
"import": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./*": {
|
|
34
|
+
"import": "./dist/*"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"module": "./dist/index.js",
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
39
|
+
"files": [
|
|
40
|
+
"README.md",
|
|
41
|
+
"dist"
|
|
42
|
+
],
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "bun build.ts",
|
|
45
|
+
"typecheck": "bun --bun tsc --noEmit"
|
|
46
|
+
}
|
|
47
|
+
}
|