UNPKG

sqlite-prepare

Version:

A type-safe SQL query builder for Cloudflare D1

3 lines (2 loc) 3.19 kB
"use strict";function r(r){return null==r?null:r instanceof Date||r instanceof Uint8Array||r instanceof ArrayBuffer?r:r&&"object"==typeof r?"__raw"in r&&!0===r.__raw?{__raw:!0,query:r.query,params:r.params}:"__fragment"in r&&!0===r.__fragment?r:"query"in r&&"params"in r?{__subquery:!0,query:r.query,params:r.params}:JSON.stringify(r):r}function e(e,...n){let t,a=[];if("string"==typeof e)t=e,a=n.map(r).flatMap((r=>"object"==typeof r&&null!==r&&("__subquery"in r||"__fragment"in r||"__raw"in r)?r.params||[]:[r]));else{t="";for(let o=0;o<e.length;o++)if(t+=e[o],o<n.length){const e=n[o];if(Array.isArray(e))0===e.length?t+="(NULL)":(t+="("+e.map((()=>"?")).join(", ")+")",a.push(...e.map(r).flatMap((r=>"object"==typeof r&&null!==r&&("__subquery"in r||"__fragment"in r||"__raw"in r)?r.params||[]:[r]))));else if(e&&"object"==typeof e)if("__raw"in e||"__fragment"in e)t+=e.query,e.params&&a.push(...e.params);else if("query"in e&&"params"in e)t+="("+e.query+")",a.push(...e.params);else{const n=r(e);"object"==typeof n&&null!==n&&("__raw"in n||"__fragment"in n||"__subquery"in n)?(t+=n.query,n.params&&a.push(...n.params)):(t+="?",a.push(n))}else t+="?",a.push(r(e))}}return{query:t,params:a}}const n=e;function t(r,...e){if("string"!=typeof r){let n="";return r.forEach(((r,t)=>{n+=r+(void 0!==e[t]?e[t]:"")})),{__raw:!0,query:n}}const n=e[0]||[];return{__raw:!0,query:r,...n.length>0?{params:n}:{}}}const a=t;function o(r,n,...t){const a=e(n,...t);return r.prepare(a.query).bind(...a.params)}const s=o;function i(r){return r}function u(e,n){const t=Array.isArray(n)?n:[n];if(0===t.length)throw new Error("Values array cannot be empty");const a=[],o=new Set;for(const r of t)Object.keys(r).forEach((r=>o.add(r)));const s=Array.from(o),i=t.map((e=>{const n={};for(const t of s){const a=t in e?e[t]:null;n[t]=r(a)}return n})).map((r=>`(${s.map((e=>{const n=r[e];if("object"==typeof n&&null!==n){if("__raw"in n||"__fragment"in n)return a.push(...n.params||[]),n.query;if("__subquery"in n)return a.push(...n.params),`(${n.query})`}return a.push(n),"?"})).join(", ")})`)).join(", ");return{__fragment:!0,query:`INSERT INTO ${e} (${s.join(", ")}) VALUES ${i}`,params:a}}const p=u,f=i;function y(r,...n){const t=e(r,...n);return{__fragment:!0,query:t.query,params:t.params}}const c=y;function l(r,e){const n="string"==typeof r?t(r):r;if(Array.isArray(e)){if(!e.includes(n.query))throw new Error(`Query "${n.query}" is not in the allowed list`);return n}if(function(r){return"function"==typeof r&&1===r.length}(e)){if("string"==typeof r){if(!e(r))throw new Error(`Query "${r}" is not valid`)}else if(!e(n.query))throw new Error(`Query "${n.query}" is not valid`)}else{if(!function(r){return"function"==typeof r&&2===r.length}(e))throw new Error("Invalid validator type");if(!e(n,!0))throw new Error(`Query "${n.query}" is not valid`)}return n}const _=l;exports.b=n,exports.build=e,exports.f=c,exports.fragment=y,exports.i=p,exports.insert=u,exports.into=i,exports.it=f,exports.pp=s,exports.prepare=o,exports.r=a,exports.raw=t,exports.v=_,exports.validate=l,exports.wrapD1=r=>{if(!r)throw new Error("Database connection is not established.");return o.bind(null,r)}; //# sourceMappingURL=index.js.map