workers-qb
Advanced tools
Comparing version 0.1.0 to 0.1.1
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.D1QB = exports.QueryBuilder = void 0; | ||
exports.FetchTypes = exports.OrderTypes = exports.D1QB = exports.QueryBuilder = void 0; | ||
var Builder_1 = require("./Builder"); | ||
@@ -8,1 +8,4 @@ Object.defineProperty(exports, "QueryBuilder", { enumerable: true, get: function () { return Builder_1.QueryBuilder; } }); | ||
Object.defineProperty(exports, "D1QB", { enumerable: true, get: function () { return Databases_1.D1QB; } }); | ||
var enums_1 = require("./enums"); | ||
Object.defineProperty(exports, "OrderTypes", { enumerable: true, get: function () { return enums_1.OrderTypes; } }); | ||
Object.defineProperty(exports, "FetchTypes", { enumerable: true, get: function () { return enums_1.FetchTypes; } }); |
import { QueryBuilder } from './Builder'; | ||
import { D1QB } from './Databases'; | ||
export { QueryBuilder, D1QB }; | ||
import { OrderTypes, FetchTypes } from './enums'; | ||
export { QueryBuilder, D1QB, OrderTypes, FetchTypes }; |
import { QueryBuilder } from './Builder'; | ||
import { D1QB } from './Databases'; | ||
export { QueryBuilder, D1QB }; | ||
import { OrderTypes, FetchTypes } from './enums'; | ||
import { Where, Insert, Update, Delete, SelectAll, SelectOne } from './interfaces'; | ||
export { QueryBuilder, D1QB, OrderTypes, FetchTypes, Where, Insert, Update, Delete, SelectAll, SelectOne }; |
@@ -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,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{s(n.next(e))}catch(e){i(e)}}function c(e){try{s(n.throw(e))}catch(e){i(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(u,c)}s((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: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(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,c])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.QueryBuilder=void 0;var u=r(40),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+" ("+e.schema+")"})]}))}))},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(){return i(this,(function(t){return[2,this.execute({query:this._select(n(n({},e),{limit:1})),arguments:e.where?e.where.params:void 0,fetchType:u.FetchTypes.ALL})]}))}))},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(){return i(this,(function(t){return[2,this.execute({query:this._update(e),arguments:e.where&&e.where.params?Object.values(e.data).concat(e.where.params):Object.values(e.data),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._insert=function(e){var t=Object.keys(e.data).join(", "),r=[];return Object.keys(e.data).forEach((function(e,t){r.push("?"+(t+1))})),"INSERT INTO "+e.tableName+" ("+t+") VALUES("+r.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];e[1],n.push(o+" = ?"+(r+t+1))})),"UPDATE "+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._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._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"===e.constructor.name.toLowerCase())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{s(n.next(e))}catch(e){i(e)}}function c(e){try{s(n.throw(e))}catch(e){i(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(u,c)}s((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: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(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,c])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.D1QB=void 0;var c=r(501),s=r(40),a=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;return u(this,(function(r){return t=this.db.prepare(e.query),e.arguments&&(t=t.bind.apply(t,e.arguments)),console.log(e),e.fetchType===s.FetchTypes.ONE?[2,t.first()]:e.fetchType===s.FetchTypes.ALL?[2,t.all()]:[2,t.run()]}))}))},t}(c.QueryBuilder);t.D1QB=a},40:(e,t)=>{var r,n;Object.defineProperty(t,"__esModule",{value:!0}),t.FetchTypes=t.OrderTypes=void 0,(n=t.OrderTypes||(t.OrderTypes={})).ASC="ASC",n.DESC="DESC",(r=t.FetchTypes||(t.FetchTypes={})).ONE="ONE",r.ALL="ALL"}},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.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}})})(),n})()})); | ||
!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{s(n.next(e))}catch(e){i(e)}}function c(e){try{s(n.throw(e))}catch(e){i(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(u,c)}s((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: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(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,c])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.QueryBuilder=void 0;var u=r(40),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+" ("+e.schema+")"})]}))}))},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(){return i(this,(function(t){return[2,this.execute({query:this._select(n(n({},e),{limit:1})),arguments:e.where?e.where.params:void 0,fetchType:u.FetchTypes.ALL})]}))}))},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(){return i(this,(function(t){return[2,this.execute({query:this._update(e),arguments:e.where&&e.where.params?Object.values(e.data).concat(e.where.params):Object.values(e.data),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._insert=function(e){var t=Object.keys(e.data).join(", "),r=[];return Object.keys(e.data).forEach((function(e,t){r.push("?"+(t+1))})),"INSERT INTO "+e.tableName+" ("+t+") VALUES("+r.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];e[1],n.push(o+" = ?"+(r+t+1))})),"UPDATE "+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._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._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"===e.constructor.name.toLowerCase())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{s(n.next(e))}catch(e){i(e)}}function c(e){try{s(n.throw(e))}catch(e){i(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(u,c)}s((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: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(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,c])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.D1QB=void 0;var c=r(501),s=r(40),a=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;return u(this,(function(r){return t=this.db.prepare(e.query),e.arguments&&(t=t.bind.apply(t,e.arguments)),console.log(e),e.fetchType===s.FetchTypes.ONE?[2,t.first()]:e.fetchType===s.FetchTypes.ALL?[2,t.all()]:[2,t.run()]}))}))},t}(c.QueryBuilder);t.D1QB=a},40:(e,t)=>{var r,n;Object.defineProperty(t,"__esModule",{value:!0}),t.FetchTypes=t.OrderTypes=void 0,(n=t.OrderTypes||(t.OrderTypes={})).ASC="ASC",n.DESC="DESC",(r=t.FetchTypes||(t.FetchTypes={})).ONE="ONE",r.ALL="ALL"}},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.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}})})(),n})()})); |
{ | ||
"name": "workers-qb", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Easily run SQL queries in Cloudflare Workers D1", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
123
README.md
# workers-qb | ||
Zero dependencies SQL Builder for [Cloudflare D1](https://blog.cloudflare.com/introducing-d1/) inside | ||
[Workers](https://developers.cloudflare.com/workers/) | ||
## Features | ||
- [x] zero dependencies. | ||
- [x] [Fully typed/TypeScript support](#typescript) | ||
- [x] SQL Type checking with compatible IDE's | ||
- [x] Insert/update/select/delete queries | ||
- [x] Create/drop tables | ||
- [x] Easily snap together a bunch of SQL conditions without adding too much complexity to your project | ||
- [ ] Bulk insert/updates | ||
- [ ] Named parameters (waiting of full support from D1) | ||
## Installation | ||
``` | ||
npm install workers-qb | ||
``` | ||
## Example | ||
#### Basic insert/select/update/delete queries | ||
```ts | ||
import { D1QB } from 'workers-qb' | ||
const qb = new D1QB(env.DB) | ||
const inserted = await qb.insert({ | ||
tableName: "employees", | ||
data: { | ||
name: "Joe", | ||
role: "manager", | ||
department: "store", | ||
}, | ||
returning: "*", | ||
}) | ||
console.log(inserted) // This will contain the data after SQL triggers and primary keys that are automated | ||
const joeAgain = await qb.fetchAll({ | ||
tableName: "employees", | ||
fields: "*", | ||
where: { | ||
conditions: "id = ?1", | ||
params: [inserted.results[0].id] // Filter using the id returned above | ||
}, | ||
}) | ||
const joeUpdated = await qb.update({ | ||
tableName: "employees", | ||
data: { | ||
role: "CEO", | ||
department: "HQ", | ||
}, | ||
where: { | ||
conditions: "id = ?1", | ||
params: [inserted.results[0].id] | ||
}, | ||
}) | ||
await qb.delete({ | ||
tableName: "employees", | ||
where: { | ||
conditions: "id = ?1", | ||
params: [inserted.results[0].id] | ||
}, | ||
}) | ||
``` | ||
#### Fetching a single record | ||
```ts | ||
import { D1QB, OrderTypes } from 'workers-qb' | ||
const qb = new D1QB(env.DB) | ||
const result = await qb.fetchOne({ | ||
tableName: "employees", | ||
fields: "*", | ||
where: { | ||
conditions: [ | ||
"department = ?1", | ||
"name LIKE 'J%'", | ||
], | ||
params: ["HQ"] | ||
}, | ||
orderBy: { | ||
"timestamp": OrderTypes.DESC, | ||
}, | ||
}) | ||
``` | ||
#### Fetching multiple record with dynamic where | ||
```ts | ||
import { D1QB, OrderTypes } from 'workers-qb' | ||
const qb = new D1QB(env.DB) | ||
async function countRoles(department?: string) { | ||
const conditions = [] | ||
if (department) conditions.push("department = ?1") | ||
const result = await qb.fetchAll({ | ||
tableName: "employees", | ||
fields: "role, count(*) as count", | ||
where: { | ||
conditions: conditions, | ||
params: [department] | ||
}, | ||
groupBy: "role", | ||
orderBy: { | ||
"count": OrderTypes.DESC, | ||
}, | ||
}) | ||
return result.results | ||
} | ||
``` | ||
## Development | ||
@@ -68,3 +188,3 @@ | ||
```ts | ||
import { D1QB } from 'workers-qb' TODO | ||
import { D1QB } from 'workers-qb' | ||
const qb = new D1QB(env.DB) | ||
@@ -108,2 +228,1 @@ | ||
``` | ||
52466
756
227