workers-qb
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -111,6 +111,17 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
var args, _i, _a, row; | ||
return __generator(this, function (_b) { | ||
args = []; | ||
if (Array.isArray(params.data)) { | ||
for (_i = 0, _a = params.data; _i < _a.length; _i++) { | ||
row = _a[_i]; | ||
args = args.concat(this._parse_arguments(row)); | ||
} | ||
} | ||
else { | ||
args = args.concat(this._parse_arguments(params.data)); | ||
} | ||
return [2 /*return*/, this.execute({ | ||
query: this._insert(params), | ||
arguments: Object.values(params.data), | ||
arguments: args, | ||
fetchType: enums_1.FetchTypes.ALL, | ||
@@ -125,10 +136,5 @@ })]; | ||
return __generator(this, function (_a) { | ||
args = Object.values(params.data); | ||
args = this._parse_arguments(params.data); | ||
if (params.where && params.where.params) { | ||
args = params.where.params.concat(Object.values(params.data).map(function (value) { | ||
if (value instanceof tools_1.Raw) { | ||
return value.content; | ||
} | ||
return value; | ||
})); | ||
args = params.where.params.concat(args); | ||
} | ||
@@ -154,2 +160,10 @@ return [2 /*return*/, this.execute({ | ||
}; | ||
QueryBuilder.prototype._parse_arguments = function (row) { | ||
return Object.values(row).map(function (value) { | ||
if (value instanceof tools_1.Raw) { | ||
return value.content; | ||
} | ||
return value; | ||
}); | ||
}; | ||
QueryBuilder.prototype._onConflict = function (resolution) { | ||
@@ -162,15 +176,28 @@ if (resolution) { | ||
QueryBuilder.prototype._insert = function (params) { | ||
var columns = Object.keys(params.data).join(', '); | ||
var values = []; | ||
Object.entries(params.data).forEach(function (_a, index) { | ||
var key = _a[0], value = _a[1]; | ||
if (value instanceof tools_1.Raw) { | ||
values.push(value.content); | ||
} | ||
else { | ||
values.push("?" + (index + 1)); | ||
} | ||
}); | ||
var rows = []; | ||
if (!Array.isArray(params.data)) { | ||
params.data = [params.data]; | ||
} | ||
var columns = Object.keys(params.data[0]).join(', '); | ||
var index = 1; | ||
var _loop_1 = function (row) { | ||
var values = []; | ||
Object.entries(row).forEach(function (_a) { | ||
var key = _a[0], value = _a[1]; | ||
if (value instanceof tools_1.Raw) { | ||
values.push(value.content); | ||
} | ||
else { | ||
values.push("?" + index); | ||
} | ||
index += 1; | ||
}); | ||
rows.push("(" + values.join(', ') + ")"); | ||
}; | ||
for (var _i = 0, _a = params.data; _i < _a.length; _i++) { | ||
var row = _a[_i]; | ||
_loop_1(row); | ||
} | ||
return ("INSERT " + this._onConflict(params.onConflict) + "INTO " + params.tableName + " (" + columns + ")" + | ||
(" VALUES(" + values.join(', ') + ")") + | ||
(" VALUES " + rows.join(', ')) + | ||
this._returning(params.returning)); | ||
@@ -177,0 +204,0 @@ }; |
@@ -108,6 +108,17 @@ var __assign = (this && this.__assign) || function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
var args, _i, _a, row; | ||
return __generator(this, function (_b) { | ||
args = []; | ||
if (Array.isArray(params.data)) { | ||
for (_i = 0, _a = params.data; _i < _a.length; _i++) { | ||
row = _a[_i]; | ||
args = args.concat(this._parse_arguments(row)); | ||
} | ||
} | ||
else { | ||
args = args.concat(this._parse_arguments(params.data)); | ||
} | ||
return [2 /*return*/, this.execute({ | ||
query: this._insert(params), | ||
arguments: Object.values(params.data), | ||
arguments: args, | ||
fetchType: FetchTypes.ALL, | ||
@@ -122,10 +133,5 @@ })]; | ||
return __generator(this, function (_a) { | ||
args = Object.values(params.data); | ||
args = this._parse_arguments(params.data); | ||
if (params.where && params.where.params) { | ||
args = params.where.params.concat(Object.values(params.data).map(function (value) { | ||
if (value instanceof Raw) { | ||
return value.content; | ||
} | ||
return value; | ||
})); | ||
args = params.where.params.concat(args); | ||
} | ||
@@ -151,2 +157,10 @@ return [2 /*return*/, this.execute({ | ||
}; | ||
QueryBuilder.prototype._parse_arguments = function (row) { | ||
return Object.values(row).map(function (value) { | ||
if (value instanceof Raw) { | ||
return value.content; | ||
} | ||
return value; | ||
}); | ||
}; | ||
QueryBuilder.prototype._onConflict = function (resolution) { | ||
@@ -159,15 +173,28 @@ if (resolution) { | ||
QueryBuilder.prototype._insert = function (params) { | ||
var columns = Object.keys(params.data).join(', '); | ||
var values = []; | ||
Object.entries(params.data).forEach(function (_a, index) { | ||
var key = _a[0], value = _a[1]; | ||
if (value instanceof Raw) { | ||
values.push(value.content); | ||
} | ||
else { | ||
values.push("?" + (index + 1)); | ||
} | ||
}); | ||
var rows = []; | ||
if (!Array.isArray(params.data)) { | ||
params.data = [params.data]; | ||
} | ||
var columns = Object.keys(params.data[0]).join(', '); | ||
var index = 1; | ||
var _loop_1 = function (row) { | ||
var values = []; | ||
Object.entries(row).forEach(function (_a) { | ||
var key = _a[0], value = _a[1]; | ||
if (value instanceof Raw) { | ||
values.push(value.content); | ||
} | ||
else { | ||
values.push("?" + index); | ||
} | ||
index += 1; | ||
}); | ||
rows.push("(" + values.join(', ') + ")"); | ||
}; | ||
for (var _i = 0, _a = params.data; _i < _a.length; _i++) { | ||
var row = _a[_i]; | ||
_loop_1(row); | ||
} | ||
return ("INSERT " + this._onConflict(params.onConflict) + "INTO " + params.tableName + " (" + columns + ")" + | ||
(" VALUES(" + values.join(', ') + ")") + | ||
(" VALUES " + rows.join(', ')) + | ||
this._returning(params.returning)); | ||
@@ -174,0 +201,0 @@ }; |
@@ -24,2 +24,3 @@ import { Delete, Insert, Join, Result, ResultOne, SelectAll, SelectOne, Update } from './interfaces'; | ||
delete(params: Delete): Promise<Result>; | ||
_parse_arguments(row: Record<string, string | boolean | number | null | Raw>): Array<any>; | ||
_onConflict(resolution?: string | ConflictTypes): string; | ||
@@ -26,0 +27,0 @@ _insert(params: Insert): string; |
@@ -27,3 +27,3 @@ import { ConflictTypes, JoinTypes, OrderTypes } from './enums'; | ||
tableName: string; | ||
data: Record<string, string | boolean | number | null | Raw>; | ||
data: Record<string, string | boolean | number | null | Raw> | Array<Record<string, string | boolean | number | null | Raw>>; | ||
returning?: string | Array<string>; | ||
@@ -30,0 +30,0 @@ onConflict?: string | ConflictTypes; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["workers-qb"]=t():e["workers-qb"]=t()}(this,(function(){return(()=>{"use strict";var e={501:function(e,t,n){var r=this&&this.__assign||function(){return r=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},r.apply(this,arguments)},o=this&&this.__awaiter||function(e,t,n,r){return new(n||(n=Promise))((function(o,i){function u(e){try{s(r.next(e))}catch(e){i(e)}}function c(e){try{s(r.throw(e))}catch(e){i(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(u,c)}s((r=r.apply(e,t||[])).next())}))},i=this&&this.__generator||function(e,t){var n,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;u;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,r=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!((o=(o=u.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=t.call(e,u)}catch(e){i=[6,e],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.QueryBuilder=void 0;var u=n(40),c=n(235),s=function(){function e(){}return e.prototype.execute=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(e){throw new Error("Execute method not implemented")}))}))},e.prototype.createTable=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:"CREATE TABLE "+(e.ifNotExists?"IF NOT EXISTS":"")+" "+e.tableName+"\n (\n "+e.schema+"\n )"})]}))}))},e.prototype.dropTable=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:"DROP TABLE "+(e.ifExists?"IF EXISTS":"")+" "+e.tableName})]}))}))},e.prototype.fetchOne=function(e){return o(this,void 0,void 0,(function(){var t;return i(this,(function(n){switch(n.label){case 0:return[4,this.execute({query:this._select(r(r({},e),{limit:1})),arguments:e.where?e.where.params:void 0,fetchType:u.FetchTypes.ALL})];case 1:return t=n.sent(),[2,r(r({},t),{results:t.results[0]})]}}))}))},e.prototype.fetchAll=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:this._select(e),arguments:e.where?e.where.params:void 0,fetchType:u.FetchTypes.ALL})]}))}))},e.prototype.insert=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:this._insert(e),arguments:Object.values(e.data),fetchType:u.FetchTypes.ALL})]}))}))},e.prototype.update=function(e){return o(this,void 0,void 0,(function(){var t;return i(this,(function(n){return t=Object.values(e.data),e.where&&e.where.params&&(t=e.where.params.concat(Object.values(e.data).map((function(e){return e instanceof c.Raw?e.content:e})))),[2,this.execute({query:this._update(e),arguments:t,fetchType:u.FetchTypes.ALL})]}))}))},e.prototype.delete=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:this._delete(e),arguments:e.where?e.where.params:void 0,fetchType:u.FetchTypes.ALL})]}))}))},e.prototype._onConflict=function(e){return e?"OR "+e+" ":""},e.prototype._insert=function(e){var t=Object.keys(e.data).join(", "),n=[];return Object.entries(e.data).forEach((function(e,t){e[0];var r=e[1];r instanceof c.Raw?n.push(r.content):n.push("?"+(t+1))})),"INSERT "+this._onConflict(e.onConflict)+"INTO "+e.tableName+" ("+t+") VALUES("+n.join(", ")+")"+this._returning(e.returning)},e.prototype._update=function(e){var t,n=e.where&&e.where.params?Object.keys(e.where.params).length:0,r=[];return Object.entries(e.data).forEach((function(e,t){var o=e[0],i=e[1];i instanceof c.Raw?r.push(o+" = "+i.content):r.push(o+" = ?"+(n+t+1))})),"UPDATE "+this._onConflict(e.onConflict)+e.tableName+" SET "+r.join(", ")+this._where(null===(t=e.where)||void 0===t?void 0:t.conditions)+this._returning(e.returning)},e.prototype._delete=function(e){var t;return"DELETE FROM "+e.tableName+this._where(null===(t=e.where)||void 0===t?void 0:t.conditions)+this._returning(e.returning)},e.prototype._select=function(e){var t;return"SELECT "+this._fields(e.fields)+" FROM "+e.tableName+this._join(e.join)+this._where(null===(t=e.where)||void 0===t?void 0:t.conditions)+this._groupBy(e.groupBy)+this._having(e.having)+this._orderBy(e.orderBy)+this._limit(e.limit)+this._offset(e.offset)},e.prototype._fields=function(e){return"string"==typeof e?e:e.join(", ")},e.prototype._where=function(e){return e?"string"==typeof e?" WHERE "+e:" WHERE "+e.join(" AND "):""},e.prototype._join=function(e){if(!e)return"";Array.isArray(e)||(e=[e]);var t=[];return e.forEach((function(e){var n=e.type?e.type+" ":"";t.push(n+"JOIN "+e.table+" ON "+e.on)}))," "+t.join(" ")},e.prototype._groupBy=function(e){return e?"string"==typeof e?" GROUP BY "+e:" GROUP BY "+e.join(", "):""},e.prototype._having=function(e){return e?" HAVING "+e:""},e.prototype._orderBy=function(e){if(!e)return"";if("string"==typeof e)return" ORDER BY "+e;if(Array.isArray(e))return" ORDER BY "+e.join(", ");var t=[];return Object.entries(e).forEach((function(e){var n=e[0],r=e[1];t.push(n+" "+r)}))," ORDER BY "+t.join(", ")},e.prototype._limit=function(e){return e?" LIMIT "+e:""},e.prototype._offset=function(e){return e?" OFFSET "+e:""},e.prototype._returning=function(e){return e?"string"==typeof e?" RETURNING "+e:" RETURNING "+e.join(", "):""},e}();t.QueryBuilder=s},513:function(e,t,n){var r,o=this&&this.__extends||(r=function(e,t){return r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])},r(e,t)},function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}),i=this&&this.__awaiter||function(e,t,n,r){return new(n||(n=Promise))((function(o,i){function u(e){try{s(r.next(e))}catch(e){i(e)}}function c(e){try{s(r.throw(e))}catch(e){i(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(u,c)}s((r=r.apply(e,t||[])).next())}))},u=this&&this.__generator||function(e,t){var n,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;u;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,r=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!((o=(o=u.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=t.call(e,u)}catch(e){i=[6,e],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.D1QB=void 0;var c=n(501),s=n(40),a=n(235),f=function(e){function t(t){var n=e.call(this)||this;return n.db=t,n}return o(t,e),t.prototype.execute=function(e){return i(this,void 0,void 0,(function(){var t,n;return u(this,(function(r){return t=this.db.prepare(e.query),e.arguments&&(n=e.arguments.map((function(e){return e instanceof a.Raw?e.content:e})),t=t.bind.apply(t,n)),e.fetchType===s.FetchTypes.ONE?[2,t.first()]:e.fetchType===s.FetchTypes.ALL?[2,t.all()]:[2,t.run()]}))}))},t}(c.QueryBuilder);t.D1QB=f},40:(e,t)=>{var n,r,o,i;Object.defineProperty(t,"__esModule",{value:!0}),t.JoinTypes=t.ConflictTypes=t.FetchTypes=t.OrderTypes=void 0,(i=t.OrderTypes||(t.OrderTypes={})).ASC="ASC",i.DESC="DESC",(o=t.FetchTypes||(t.FetchTypes={})).ONE="ONE",o.ALL="ALL",(r=t.ConflictTypes||(t.ConflictTypes={})).ROLLBACK="ROLLBACK",r.ABORT="ABORT",r.FAIL="FAIL",r.IGNORE="IGNORE",r.REPLACE="REPLACE",(n=t.JoinTypes||(t.JoinTypes={})).INNER="INNER",n.LEFT="LEFT",n.CROSS="CROSS"},235:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Raw=void 0;t.Raw=function(e){this.isRaw=!0,this.content=e}}},t={};function n(r){var o=t[r];if(void 0!==o)return o.exports;var i=t[r]={exports:{}};return e[r].call(i.exports,i,i.exports,n),i.exports}var r={};return(()=>{var e=r;Object.defineProperty(e,"__esModule",{value:!0}),e.Raw=e.FetchTypes=e.OrderTypes=e.D1QB=e.QueryBuilder=void 0;var t=n(501);Object.defineProperty(e,"QueryBuilder",{enumerable:!0,get:function(){return t.QueryBuilder}});var o=n(513);Object.defineProperty(e,"D1QB",{enumerable:!0,get:function(){return o.D1QB}});var i=n(40);Object.defineProperty(e,"OrderTypes",{enumerable:!0,get:function(){return i.OrderTypes}}),Object.defineProperty(e,"FetchTypes",{enumerable:!0,get:function(){return i.FetchTypes}});var u=n(235);Object.defineProperty(e,"Raw",{enumerable:!0,get:function(){return u.Raw}})})(),r})()})); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["workers-qb"]=t():e["workers-qb"]=t()}(this,(function(){return(()=>{"use strict";var e={501:function(e,t,r){var n=this&&this.__assign||function(){return n=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},n.apply(this,arguments)},o=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function u(e){try{c(n.next(e))}catch(e){i(e)}}function a(e){try{c(n.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(u,a)}c((n=n.apply(e,t||[])).next())}))},i=this&&this.__generator||function(e,t){var r,n,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(i){return function(a){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;u;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,n=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!((o=(o=u.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=t.call(e,u)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,a])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.QueryBuilder=void 0;var u=r(40),a=r(235),c=function(){function e(){}return e.prototype.execute=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(e){throw new Error("Execute method not implemented")}))}))},e.prototype.createTable=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:"CREATE TABLE "+(e.ifNotExists?"IF NOT EXISTS":"")+" "+e.tableName+"\n (\n "+e.schema+"\n )"})]}))}))},e.prototype.dropTable=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:"DROP TABLE "+(e.ifExists?"IF EXISTS":"")+" "+e.tableName})]}))}))},e.prototype.fetchOne=function(e){return o(this,void 0,void 0,(function(){var t;return i(this,(function(r){switch(r.label){case 0:return[4,this.execute({query:this._select(n(n({},e),{limit:1})),arguments:e.where?e.where.params:void 0,fetchType:u.FetchTypes.ALL})];case 1:return t=r.sent(),[2,n(n({},t),{results:t.results[0]})]}}))}))},e.prototype.fetchAll=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:this._select(e),arguments:e.where?e.where.params:void 0,fetchType:u.FetchTypes.ALL})]}))}))},e.prototype.insert=function(e){return o(this,void 0,void 0,(function(){var t,r,n,o;return i(this,(function(i){if(t=[],Array.isArray(e.data))for(r=0,n=e.data;r<n.length;r++)o=n[r],t=t.concat(this._parse_arguments(o));else t=t.concat(this._parse_arguments(e.data));return[2,this.execute({query:this._insert(e),arguments:t,fetchType:u.FetchTypes.ALL})]}))}))},e.prototype.update=function(e){return o(this,void 0,void 0,(function(){var t;return i(this,(function(r){return t=this._parse_arguments(e.data),e.where&&e.where.params&&(t=e.where.params.concat(t)),[2,this.execute({query:this._update(e),arguments:t,fetchType:u.FetchTypes.ALL})]}))}))},e.prototype.delete=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:this._delete(e),arguments:e.where?e.where.params:void 0,fetchType:u.FetchTypes.ALL})]}))}))},e.prototype._parse_arguments=function(e){return Object.values(e).map((function(e){return e instanceof a.Raw?e.content:e}))},e.prototype._onConflict=function(e){return e?"OR "+e+" ":""},e.prototype._insert=function(e){var t=[];Array.isArray(e.data)||(e.data=[e.data]);for(var r=Object.keys(e.data[0]).join(", "),n=1,o=function(e){var r=[];Object.entries(e).forEach((function(e){e[0];var t=e[1];t instanceof a.Raw?r.push(t.content):r.push("?"+n),n+=1})),t.push("("+r.join(", ")+")")},i=0,u=e.data;i<u.length;i++)o(u[i]);return"INSERT "+this._onConflict(e.onConflict)+"INTO "+e.tableName+" ("+r+") VALUES "+t.join(", ")+this._returning(e.returning)},e.prototype._update=function(e){var t,r=e.where&&e.where.params?Object.keys(e.where.params).length:0,n=[];return Object.entries(e.data).forEach((function(e,t){var o=e[0],i=e[1];i instanceof a.Raw?n.push(o+" = "+i.content):n.push(o+" = ?"+(r+t+1))})),"UPDATE "+this._onConflict(e.onConflict)+e.tableName+" SET "+n.join(", ")+this._where(null===(t=e.where)||void 0===t?void 0:t.conditions)+this._returning(e.returning)},e.prototype._delete=function(e){var t;return"DELETE FROM "+e.tableName+this._where(null===(t=e.where)||void 0===t?void 0:t.conditions)+this._returning(e.returning)},e.prototype._select=function(e){var t;return"SELECT "+this._fields(e.fields)+" FROM "+e.tableName+this._join(e.join)+this._where(null===(t=e.where)||void 0===t?void 0:t.conditions)+this._groupBy(e.groupBy)+this._having(e.having)+this._orderBy(e.orderBy)+this._limit(e.limit)+this._offset(e.offset)},e.prototype._fields=function(e){return"string"==typeof e?e:e.join(", ")},e.prototype._where=function(e){return e?"string"==typeof e?" WHERE "+e:" WHERE "+e.join(" AND "):""},e.prototype._join=function(e){if(!e)return"";Array.isArray(e)||(e=[e]);var t=[];return e.forEach((function(e){var r=e.type?e.type+" ":"";t.push(r+"JOIN "+e.table+" ON "+e.on)}))," "+t.join(" ")},e.prototype._groupBy=function(e){return e?"string"==typeof e?" GROUP BY "+e:" GROUP BY "+e.join(", "):""},e.prototype._having=function(e){return e?" HAVING "+e:""},e.prototype._orderBy=function(e){if(!e)return"";if("string"==typeof e)return" ORDER BY "+e;if(Array.isArray(e))return" ORDER BY "+e.join(", ");var t=[];return Object.entries(e).forEach((function(e){var r=e[0],n=e[1];t.push(r+" "+n)}))," ORDER BY "+t.join(", ")},e.prototype._limit=function(e){return e?" LIMIT "+e:""},e.prototype._offset=function(e){return e?" OFFSET "+e:""},e.prototype._returning=function(e){return e?"string"==typeof e?" RETURNING "+e:" RETURNING "+e.join(", "):""},e}();t.QueryBuilder=c},513:function(e,t,r){var n,o=this&&this.__extends||(n=function(e,t){return n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])},n(e,t)},function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function r(){this.constructor=e}n(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}),i=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function u(e){try{c(n.next(e))}catch(e){i(e)}}function a(e){try{c(n.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(u,a)}c((n=n.apply(e,t||[])).next())}))},u=this&&this.__generator||function(e,t){var r,n,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(i){return function(a){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;u;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,n=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!((o=(o=u.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=t.call(e,u)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,a])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.D1QB=void 0;var a=r(501),c=r(40),s=r(235),f=function(e){function t(t){var r=e.call(this)||this;return r.db=t,r}return o(t,e),t.prototype.execute=function(e){return i(this,void 0,void 0,(function(){var t,r;return u(this,(function(n){return t=this.db.prepare(e.query),e.arguments&&(r=e.arguments.map((function(e){return e instanceof s.Raw?e.content:e})),t=t.bind.apply(t,r)),e.fetchType===c.FetchTypes.ONE?[2,t.first()]:e.fetchType===c.FetchTypes.ALL?[2,t.all()]:[2,t.run()]}))}))},t}(a.QueryBuilder);t.D1QB=f},40:(e,t)=>{var r,n,o,i;Object.defineProperty(t,"__esModule",{value:!0}),t.JoinTypes=t.ConflictTypes=t.FetchTypes=t.OrderTypes=void 0,(i=t.OrderTypes||(t.OrderTypes={})).ASC="ASC",i.DESC="DESC",(o=t.FetchTypes||(t.FetchTypes={})).ONE="ONE",o.ALL="ALL",(n=t.ConflictTypes||(t.ConflictTypes={})).ROLLBACK="ROLLBACK",n.ABORT="ABORT",n.FAIL="FAIL",n.IGNORE="IGNORE",n.REPLACE="REPLACE",(r=t.JoinTypes||(t.JoinTypes={})).INNER="INNER",r.LEFT="LEFT",r.CROSS="CROSS"},235:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Raw=void 0;t.Raw=function(e){this.isRaw=!0,this.content=e}}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var i=t[n]={exports:{}};return e[n].call(i.exports,i,i.exports,r),i.exports}var n={};return(()=>{var e=n;Object.defineProperty(e,"__esModule",{value:!0}),e.Raw=e.FetchTypes=e.OrderTypes=e.D1QB=e.QueryBuilder=void 0;var t=r(501);Object.defineProperty(e,"QueryBuilder",{enumerable:!0,get:function(){return t.QueryBuilder}});var o=r(513);Object.defineProperty(e,"D1QB",{enumerable:!0,get:function(){return o.D1QB}});var i=r(40);Object.defineProperty(e,"OrderTypes",{enumerable:!0,get:function(){return i.OrderTypes}}),Object.defineProperty(e,"FetchTypes",{enumerable:!0,get:function(){return i.FetchTypes}});var u=r(235);Object.defineProperty(e,"Raw",{enumerable:!0,get:function(){return u.Raw}})})(),n})()})); |
{ | ||
"name": "workers-qb", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Zero dependencies Query Builder for Cloudflare D1 Workers", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
@@ -23,3 +23,3 @@ # workers-qb | ||
- [x] Keep where conditions simple in code | ||
- [ ] Bulk insert/update | ||
- [x] Bulk insert | ||
- [ ] Named parameters (waiting for full support in D1) | ||
@@ -114,2 +114,33 @@ | ||
## Bulk Inserting rows | ||
```ts | ||
import { Raw } from 'workers-qb' | ||
const qb = new D1QB(env.DB) | ||
const inserted = await qb.insert({ | ||
tableName: 'employees', | ||
data: [ | ||
{ | ||
name: 'Joe', | ||
role: 'manager', | ||
department: 'store', | ||
created_at: new Raw('CURRENT_TIMESTAMP'), | ||
}, | ||
{ | ||
name: 'John', | ||
role: 'employee', | ||
department: 'store', | ||
created_at: new Raw('CURRENT_TIMESTAMP'), | ||
}, | ||
{ | ||
name: 'Mickael', | ||
role: 'employee', | ||
department: 'store', | ||
created_at: new Raw('CURRENT_TIMESTAMP'), | ||
}, | ||
], | ||
}) | ||
``` | ||
#### Updating rows | ||
@@ -116,0 +147,0 @@ |
64124
1014
297