UNPKG

@a2lix/schemql

Version:

A lightweight TypeScript library that enhances your SQL workflow by combining raw SQL with targeted type safety and schema validation

2 lines (1 loc) 3.44 kB
"use strict";var S=Object.defineProperty;var n=(i,a)=>S(i,"name",{value:a,configurable:!0});const p=class p{constructor(a){this.options=a,this.createQueryExecutor=t=>r=>async s=>{const e=typeof s=="function"?s(this.createSqlHelper()):s;if(typeof r.params=="function"){const l=t(e),y=n(async u=>{const h=await this.validateAndStringifyParams({...r,params:u}),f=await l(h);return r.resultSchema?await d(r.resultSchema,f):f},"executeAndValidateResult");return async function*(){for await(const u of r.params())yield await y(u)}()}if(Array.isArray(r.params)){const l=t(e),y=await this.validateAndStringifyParams({...r,params:r.params}),u=n(async h=>{const f=await l(h);return r.resultSchema?await d(r.resultSchema,f):f},"executeAndValidateResult");return async function*(){for(const h of y)yield await u(h)}()}const o=await this.validateAndStringifyParams({...r,params:r.params}),c=await t(e)(o);return r.resultSchema?await d(r.resultSchema,c):c},this.createIterativeExecutor=t=>r=>async s=>{const e=typeof s=="function"?s(this.createSqlHelper()):s,o=await this.validateAndStringifyParams({...r,params:r.params});return async function*(){for await(const c of t(e)(o)())yield r.resultSchema?await d(r.resultSchema,c):c}()},this.validateAndStringifyParams=async t=>{if(typeof t.params>"u")return;const r=t.paramsSchema?await d(t.paramsSchema,t.params):t.params;return this.options.shouldStringifyObjectParams||this.options.stringifyObjectParams?Array.isArray(r)?r.map(s=>w(s)):w(r):r},this.createSqlHelper=()=>({sql:n((t,...r)=>this.processLiteralExpressions(t,r),"sql"),sqlCond:n((t,r,s="")=>`\xA7${t?r:s}`,"sqlCond"),sqlRaw:n(t=>`\xA7${t}`,"sqlRaw")}),this.processLiteralExpressions=(t,r)=>t.reduce((s,e,o)=>{const c=r[o];return`${s}${e}${c!==void 0?this.processLiteralExpression(c):""}`},""),this.processLiteralExpression=t=>{if(typeof t=="object"){const r=Object.entries(t);if(r.length===1){const[s,e]=r[0];return this.options.quoteSqlIdentifiers?`"${s}" ("${Array.isArray(e)?e.join('", "'):String(e)}")`:`${s} (${Array.isArray(e)?e.join(", "):String(e)})`}}if(typeof t=="string")switch(!0){case t.startsWith("@"):{const r=t.indexOf(" $.");if(r!==-1)return`'${t.slice(r+1)}'`;let s=t;const e=s.indexOf(" ->");if(e!==-1){const o=A(s.slice(e+1));s=`${s.slice(0,e)}${o}`}return s.endsWith("-")?s.split(".")[1]?.slice(0,-1)??"":s.slice(1)}case t.startsWith("$"):case t.startsWith("\xA7"):return t.slice(1);default:return t}return String(t)},this.first=this.createQueryExecutor(this.options.adapter.queryFirst),this.firstOrThrow=this.createQueryExecutor(this.options.adapter.queryFirstOrThrow),this.all=this.createQueryExecutor(this.options.adapter.queryAll),this.iterate=this.createIterativeExecutor(this.options.adapter.queryIterate)}};n(p,"SchemQl");let m=p;const w=n(i=>Object.entries(i).reduce((a,[t,r])=>(a[t]=r!==null&&typeof r=="object"?JSON.stringify(r):r,a),{}),"stringifyObjectParams"),A=n(i=>i.split(/(?=->)/).reduce((a,t)=>{const r=t.startsWith("->>")?"->>":"->",s=t.replace(r,"");return`${a}${r}'${s}'`},""),"quotifyJsonPath"),d=n(async(i,a)=>{let t=i["~standard"].validate(a);if(t instanceof Promise&&(t=await t),t.issues)throw new Error(`Validation failed: ${JSON.stringify(t.issues,null,2)}`);return t.value},"standardValidate"),P=n((i,a)=>{if(typeof i=="string")try{return JSON.parse(i)}catch(t){a.addIssue({code:"custom",message:t.message})}return i},"parseJsonPreprocessor");exports.SchemQl=m,exports.parseJsonPreprocessor=P;