UNPKG

usql

Version:

Tiny, zero-dependency SQL query generator

1 lines 7.11 kB
!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.usql=n():t.usql=n()}(window,(function(){return function(t){var n={};function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{enumerable:!0,get:r})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,n){if(1&n&&(t=e(t)),8&n)return t;if(4&n&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(e.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&n&&"string"!=typeof t)for(var o in t)e.d(r,o,function(n){return t[n]}.bind(null,o));return r},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},e.p="",e(e.s=6)}([function(t,n){t.exports=function(t,n,e){return n in t?Object.defineProperty(t,n,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[n]=e,t}},function(t,n){t.exports=function(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}},function(t,n){function e(t,n){for(var e=0;e<n.length;e++){var r=n[e];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}t.exports=function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}},function(t,n,e){var r=e(7),o=e(8),i=e(9);t.exports=function(t,n){return r(t)||o(t,n)||i()}},function(t,n){function e(n){return"function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?t.exports=e=function(t){return typeof t}:t.exports=e=function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},e(n)}t.exports=e},function(t,n,e){var r=e(10),o=e(11),i=e(12);t.exports=function(t){return r(t)||o(t)||i()}},function(t,n,e){"use strict";t.exports=e(13).default},function(t,n){t.exports=function(t){if(Array.isArray(t))return t}},function(t,n){t.exports=function(t,n){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t)){var e=[],r=!0,o=!1,i=void 0;try{for(var u,a=t[Symbol.iterator]();!(r=(u=a.next()).done)&&(e.push(u.value),!n||e.length!==n);r=!0);}catch(t){o=!0,i=t}finally{try{r||null==a.return||a.return()}finally{if(o)throw i}}return e}}},function(t,n){t.exports=function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}},function(t,n){t.exports=function(t){if(Array.isArray(t)){for(var n=0,e=new Array(t.length);n<t.length;n++)e[n]=t[n];return e}}},function(t,n){t.exports=function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}},function(t,n){t.exports=function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}},function(t,n,e){"use strict";e.r(n);var r=e(3),o=e.n(r),i=e(4),u=e.n(i),a=e(5),s=e.n(a),c=e(1),l=e.n(c),f=e(2),h=e.n(f),p=e(0),d=e.n(p),y=function(){function t(n){l()(this,t),d()(this,"data",void 0),this.data=n}return h()(t,[{key:"toString",value:function(){return this.data}}]),t}(),v=function(){function t(n){l()(this,t),d()(this,"table",null),d()(this,"alias",null),d()(this,"selects",[]),d()(this,"joins",[]),d()(this,"wheres",[]),d()(this,"orders",[]),d()(this,"dataOffset",null),d()(this,"dataLimit",null),this.table=n}return h()(t,[{key:"as",value:function(t){return this.alias=t,this}},{key:"escape",value:function(n){return null===n?null:n instanceof t||n instanceof y?n.toString():"string"==typeof n?JSON.stringify(n).slice(1,-1):JSON.stringify(n)}},{key:"renderColumn",value:function(n){if(n instanceof t||n instanceof y)return n.toString();if("null"===String(n).toLowerCase())return"null";var e=function(t){var n=s()(t.matchAll(/`?(\w+|\*)`?/g));return(n=n.map((function(t){return"*"===t[0]||"*"===t[1]?"*":"`".concat(t[1]?escape(t[1]):escape(t[0]),"`")}))).join(".")};return n.includes(" as ")?n.split(" as ").map((function(t){return e(t)})).join(" as "):e(n)}},{key:"renderStr",value:function(t){return t instanceof y?t.toString():"null"===String(t).toLowerCase()?"NULL":Number(t)===t?'"'.concat(t,'"'):'"'.concat(escape(t),'"')}},{key:"_where",value:function(){var t=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"AND",e=arguments.length>1?arguments[1]:void 0,r=e.column,o=e.operator,i=void 0===o?"=":o,a=e.value;"object"===u()(r)&&Object.keys(r).forEach((function(e){t.wheres.push({joiner:n,column:e,operator:i,value:r[e]})})),"string"==typeof r&&(void 0!==i&&void 0===a&&(a=i,i="="),this.wheres.push({joiner:n,column:r,operator:i,value:a}))}},{key:"where",value:function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"=",e=arguments.length>2?arguments[2]:void 0;return this._where("AND",{column:t,operator:n,value:e}),this}},{key:"whereNot",value:function(t,n){return this._where("AND",{column:t,operator:"!=",value:n}),this}},{key:"orWhere",value:function(t,n,e){return this._where("OR",{column:t,operator:n,value:e}),this}},{key:"join",value:function(t,n,e,r){return void 0!==e&&void 0===r&&(r=e,e="="),this.joins.push([t,n,e,r]),this}},{key:"select",value:function(){var t;return(t=this.selects).push.apply(t,arguments),this}},{key:"count",value:function(n){return this.selects.push(t.raw("COUNT(".concat(this.renderColumn(n),")"))),this}},{key:"orderBy",value:function(t,n){return this.orders.push([t,n]),this}},{key:"offset",value:function(t){return this.dataOffset=t,this}},{key:"limit",value:function(t){return this.dataLimit=t,this}},{key:"toString",value:function(){var t=this,n=[];if(n.push("SELECT"),n.push(this.selects.length>0?this.selects.map((function(n){return t.renderColumn(n)})).join(", "):"*"),n.push("FROM ".concat(this.renderColumn(this.table))),this.joins.length>0&&n.push(this.joins.map((function(n){var e=o()(n,4),r=e[0],i=e[1],u=e[2],a=e[3];return"JOIN ".concat(t.renderColumn(r)," ON ").concat(t.renderColumn(i)," ").concat(u," ").concat(t.renderColumn(a))})).join(" ")),this.wheres.length>0){var e=this.wheres.map((function(n,e){var r=n.joiner,o=n.column,i=n.operator,u=n.value;if("null"===String(u).toLowerCase()){var a={"!=":"IS NOT","=":"IS"};i=a[i]?a[i]:i}return r=e>0?"".concat(r," "):"","".concat(r).concat(t.renderColumn(o)," ").concat(i," ").concat(t.renderStr(u,"value"))}));n.push("WHERE"),n.push(e.join(" "))}this.orders.length>0&&(n.push("ORDER BY"),n.push(this.orders.map((function(n){var e=o()(n,2),r=e[0],i=e[1];return"".concat(t.renderColumn(r)," ").concat(i)})).join(", "))),this.dataLimit&&this.dataOffset?n.push("LIMIT ".concat(this.dataOffset,", ").concat(this.dataLimit)):this.dataLimit&&!this.dataOffset&&n.push("LIMIT ".concat(this.dataLimit));var r=n.join(" ");return null!==this.alias&&(r="(".concat(r,") as ").concat(this.renderColumn(this.alias))),r}}],[{key:"raw",value:function(t){return new y(t)}}]),t}();n.default=v}])}));