@stacksjs/query-builder 0.75.569 → 0.75.572

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 CHANGED
@@ -143,7 +143,8 @@ declare type QueryBuilderConfig = Parameters<typeof setBunQueryBuilderConfig>[0]
143
143
  /** Vitess topology controls forwarded to bun-query-builder's DDL driver. */
144
144
  vitess?: { sharded: boolean }
145
145
  }
146
- declare type UnsafeReturn<TResult = unknown> = Promise<TResult> & { execute: () => Promise<TResult> }
146
+ /** A raw statement: awaitable, and `execute()` is its rows where the connection answers in place. */
147
+ declare type UnsafeReturn<TResult = unknown> = Promise<TResult> & { execute: () => TResult | Promise<TResult> }
147
148
  declare type SqlitePragmaExecutor = {
148
149
  unsafe: (query: string, params?: readonly unknown[]) => UnsafeReturn
149
150
  }
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
1
  // @bun
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};
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 T(e){return Boolean(e&&Object.values(e).some((r)=>r!==void 0))}function q(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 I=["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 I)if(L(n,e))r[n]=(o)=>q(n,o,e);return r}function O(){return a.size>0||T(y)?C(y):void 0}function H(e){if(d({hooks:void 0}),e)d({hooks:e})}function P(){H(O())}function ee(e){return a.add(e),P(),()=>{if(a.delete(e))P()}}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{let n=e.unsafe(r).execute();if(n&&typeof n.then==="function")n.catch(()=>{})}catch{}}var B=new WeakMap;function A(){try{let e=s.getDatabase?.();if(!e||typeof e.run!=="function")return;let r=v(),n=JSON.stringify(r);if(B.get(e)===n)return;for(let o of r)try{e.run(o)}catch{}B.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};
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.569",
5
+ "version": "0.75.572",
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.4.2"
59
+ "bun-query-builder": "^0.6.0"
60
60
  },
61
61
  "devDependencies": {
62
- "@stacksjs/database": "0.75.569",
62
+ "@stacksjs/database": "0.75.572",
63
63
  "better-dx": "^0.2.24",
64
- "@stacksjs/types": "0.75.569"
64
+ "@stacksjs/types": "0.75.572"
65
65
  }
66
66
  }