@stacksjs/query-builder 0.75.423 → 0.75.432
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/dist/index.d.ts +2 -0
- package/dist/index.js +1 -1
- package/dist/keyset-pagination.d.ts +39 -0
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { createQueryBuilder as createBunQueryBuilder } from 'bun-query-builder';
|
|
|
2
2
|
import { setConfig as setBunQueryBuilderConfig } from 'bun-query-builder';
|
|
3
3
|
import * as bunQueryBuilder from 'bun-query-builder';
|
|
4
4
|
import type { DatabaseSchema } from 'bun-query-builder';
|
|
5
|
+
export type { KeysetOrder, KeysetPage, KeysetValue } from './keyset-pagination';
|
|
5
6
|
export declare function registerPersistentQueryHooks(hooks: QueryHooks): () => void;
|
|
6
7
|
export declare function setConfig(config: QueryBuilderConfig): void;
|
|
7
8
|
/**
|
|
@@ -151,3 +152,4 @@ export * from 'bun-query-builder';
|
|
|
151
152
|
export { saveMigrationSnapshot } from 'bun-query-builder';
|
|
152
153
|
// For backwards compatibility, export QueryBuilder as an alias
|
|
153
154
|
export { createQueryBuilder as QueryBuilder };
|
|
155
|
+
export { InvalidCursorError, keysetPage } from './keyset-pagination';
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
import{setConfig as
|
|
2
|
+
import{setConfig as d}from"bun-query-builder";import*as s from"bun-query-builder";import{config as E,createQueryBuilder as M}from"bun-query-builder";export*from"bun-query-builder";import{saveMigrationSnapshot as le}from"bun-query-builder";import{raw as K}from"bun-query-builder";class i extends Error{status=422;constructor(){super("Invalid pagination cursor");this.name="InvalidCursorError"}}function x(e){if(e instanceof Date)return e.toISOString();if(e===null||typeof e==="string"||typeof e==="boolean"||typeof e==="number"&&Number.isFinite(e))return e;throw new i}async function N(e,r){let{order:n,perPage:o,scope:l}=r;if(!Number.isSafeInteger(o)||o<1||o>500)throw RangeError("perPage must be between 1 and 500");if(!n.length||n.length>8||!l)throw TypeError("A scope and unique ordered tuple are required");for(let t of n)if(!/^[a-z_][a-z0-9_]*(?:\.[a-z_][a-z0-9_]*)?$/i.test(t.column)||t.direction&&!["asc","desc"].includes(t.direction))throw TypeError("Invalid keyset ordering");let g=JSON.stringify([l,n.map((t)=>[t.column,t.key??t.column.split(".").at(-1),t.direction??"desc"])]),u;if(r.cursor!==void 0&&r.cursor!==null&&r.cursor!=="")try{if(typeof r.cursor!=="string"||r.cursor.length>8192||!/^[\w-]+$/.test(r.cursor))throw new i;let t=JSON.parse(Buffer.from(r.cursor,"base64url").toString());if(!Array.isArray(t)||t.length!==3||t[0]!==1||t[1]!==g||!Array.isArray(t[2])||t[2].length!==n.length)throw new i;u=t[2].map(x)}catch{throw new i}if(u){let t=[],_=n.map((c,b)=>{let f=n.slice(0,b).map((S,R)=>{if(u[R]===null)return`${S.column} IS NULL`;return t.push(u[R]),`${S.column} = ?`}),h=u[b];if(h===null)f.push("1 = 0");else t.push(h),f.push(`(${c.column} ${(c.direction??"desc")==="desc"?"<":">"} ? OR ${c.column} IS NULL)`);return`(${f.join(" AND ")})`});e=e.where({sql:`(${_.join(" OR ")})`,parameters:t})}for(let t of n)e=e.orderByRaw(K(`CASE WHEN ${t.column} IS NULL THEN 1 ELSE 0 END ASC`)),e=e.orderBy(t.column,t.direction??"desc");let p=await e.limit(o+1).execute(),m=p.length>o,w=p.slice(0,o),k=w.at(-1),Q=m&&k?n.map((t)=>x(k[t.key??t.column.split(".").at(-1)])):null;return{data:w,has_more:m,next_cursor:Q?Buffer.from(JSON.stringify([1,g,Q])).toString("base64url"):null}}var a=new Set,y;function q(e){return Boolean(e&&Object.values(e).some((r)=>r!==void 0))}function I(e,r,n){for(let o of a)try{let l=o[e];l?.(r)}catch{}try{let o=n?.[e];o?.(r)}catch{}}var T=["onQueryStart","onQueryEnd","onQueryError","onSlowQuery"];function L(e,r){if(r?.[e]!==void 0)return!0;for(let n of a)if(n[e]!==void 0)return!0;return!1}function C(e){let r={...e};for(let n of T)if(L(n,e))r[n]=(o)=>I(n,o,e);return r}function O(){return a.size>0||q(y)?C(y):void 0}function H(e){if(d({hooks:void 0}),e)d({hooks:e})}function B(){H(O())}function ee(e){return a.add(e),B(),()=>{if(a.delete(e))B()}}function j(e){if(Object.prototype.hasOwnProperty.call(e,"hooks"))y=e.hooks;let{hooks:r,...n}=e;d(n),H(O())}var U=["PRAGMA journal_mode = WAL","PRAGMA foreign_keys = ON","PRAGMA busy_timeout = 5000","PRAGMA wal_autocheckpoint = 100"];function v(){return[...U,...E.sqlite?.pragmas??[]]}function V(e){for(let r of v())try{e.unsafe(r).execute().catch(()=>{})}catch{}}var P=new WeakMap;function A(){try{let e=s.getDatabase?.();if(!e||typeof e.run!=="function")return;let r=v(),n=JSON.stringify(r);if(P.get(e)===n)return;for(let o of r)try{e.run(o)}catch{}P.set(e,n)}catch{}}function re(e){s.configureOrm(e),A()}function ne(e){let r=M(e);if(!e?.sql&&E.dialect==="sqlite"&&!s.resolveLibSQLTarget())V(r),A();return r}var F=new Map;function te(e,r){let n=s.registerModel;if(typeof n==="function")return n(e,r);F.set(e,r);let o=typeof r?.getDefinition==="function"?r.getDefinition():r?.definition;if(o&&typeof o==="object")s.defineModel(o);return r}var J=/^[A-Z_][A-Z0-9_]*$/i;function z(e,r={}){if(typeof e!=="string"||e.length===0)return{valid:!1,reason:"empty"};if(e.length>64)return{valid:!1,reason:"too-long"};if(!J.test(e))return{valid:!1,reason:"invalid-shape"};if(r.allowlist&&!r.allowlist.includes(e))return{valid:!1,reason:"not-in-allowlist"};return{valid:!0}}function oe(e,r={}){let n=z(e,r);if(n.valid)return;let o=r.context?` in ${r.context}`:"";throw Error(`[query-builder] Refusing to use ${JSON.stringify(e)} as a SQL identifier${o} - ${n.reason}`)}var D=new Set(["=","!=","<>","<","<=",">",">=","like","not like","ilike","not ilike","in","not in","is","is not","between","not between"]);function G(e){return typeof e==="string"&&D.has(e.toLowerCase())}function se(e,r){if(G(e))return;let n=r?` in ${r}`:"";throw Error(`[query-builder] Refusing to use ${JSON.stringify(e)} as a SQL operator${n} - not in the allowed set (${[...D].join(", ")})`)}j({snapshotDir:"storage/framework/database"});export{i as InvalidCursorError,ne as QueryBuilder,U as SQLITE_BOOTSTRAP_PRAGMAS,V as applySqlitePragmas,oe as assertSafeIdentifier,se as assertSafeOperator,A as bootstrapModelExecutorPragmas,re as configureOrm,ne as createQueryBuilder,G as isSafeOperator,N as keysetPage,te as registerModel,ee as registerPersistentQueryHooks,le as saveMigrationSnapshot,j as setConfig,z as validateIdentifier};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { raw } from 'bun-query-builder';
|
|
2
|
+
import type { BoundSqlFragment } from 'bun-query-builder';
|
|
3
|
+
/**
|
|
4
|
+
* Forward keyset pagination over a unique ordered tuple. The final column must
|
|
5
|
+
* break ties (usually the primary key); pass an unordered, unbounded query.
|
|
6
|
+
* Nulls sort last on every dialect. Select exact timestamp text under an alias
|
|
7
|
+
* when the database keeps more precision than JavaScript Dates.
|
|
8
|
+
*
|
|
9
|
+
* Cursors are untrusted positions, never authorization. Apply ownership and
|
|
10
|
+
* filters to the query, and bind `scope` to those filters to reject reuse.
|
|
11
|
+
*/
|
|
12
|
+
export declare function keysetPage<Row extends Record<string, unknown>>(query: KeysetQuery<Row>, options: {
|
|
13
|
+
order: readonly KeysetOrder[]
|
|
14
|
+
perPage: number
|
|
15
|
+
cursor?: unknown
|
|
16
|
+
scope: string
|
|
17
|
+
}): Promise<KeysetPage<Row>>;
|
|
18
|
+
export declare interface KeysetOrder {
|
|
19
|
+
column: string
|
|
20
|
+
key?: string
|
|
21
|
+
direction?: 'asc' | 'desc'
|
|
22
|
+
}
|
|
23
|
+
declare interface KeysetQuery<Row> {
|
|
24
|
+
where(fragment: BoundSqlFragment): KeysetQuery<Row>
|
|
25
|
+
orderBy(column: string, direction: 'asc' | 'desc'): KeysetQuery<Row>
|
|
26
|
+
orderByRaw(fragment: ReturnType<typeof raw>): KeysetQuery<Row>
|
|
27
|
+
limit(count: number): KeysetQuery<Row>
|
|
28
|
+
execute(): Promise<Row[]>
|
|
29
|
+
}
|
|
30
|
+
export declare interface KeysetPage<Row> {
|
|
31
|
+
data: Row[]
|
|
32
|
+
has_more: boolean
|
|
33
|
+
next_cursor: string | null
|
|
34
|
+
}
|
|
35
|
+
export type KeysetValue = string | number | boolean | null;
|
|
36
|
+
export declare class InvalidCursorError extends Error {
|
|
37
|
+
readonly status: number;
|
|
38
|
+
constructor();
|
|
39
|
+
}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/query-builder",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.75.
|
|
5
|
+
"version": "0.75.432",
|
|
6
6
|
"description": "The Stacks Query Builder integration",
|
|
7
7
|
"author": "Chris Breuer",
|
|
8
8
|
"contributors": [
|
|
@@ -56,11 +56,11 @@
|
|
|
56
56
|
"prepublishOnly": "bun run build"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"bun-query-builder": "^0.3.
|
|
59
|
+
"bun-query-builder": "^0.3.34"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@stacksjs/database": "0.75.
|
|
62
|
+
"@stacksjs/database": "0.75.432",
|
|
63
63
|
"better-dx": "^0.2.24",
|
|
64
|
-
"@stacksjs/types": "0.75.
|
|
64
|
+
"@stacksjs/types": "0.75.432"
|
|
65
65
|
}
|
|
66
66
|
}
|