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) 1.85 kB
"use strict";var l=Object.defineProperty;var c=(i,d)=>l(i,"name",{value:d,configurable:!0});var a,n=require("./baseAdapterError.cjs");const E=class E{constructor(d){this.db=d,this.queryAll=t=>{let e;try{e=this.db.prepare(t)}catch(r){throw s.createFromBetterSqlite3(r)}return r=>{try{return r?e.all(r):e.all()}catch(o){if(o instanceof TypeError&&o.message==="This statement does not return data. Use run() instead")return this.handleTypeErrorRun(e,r),[];throw s.createFromBetterSqlite3(o)}}},this.queryFirst=t=>{let e;try{e=this.db.prepare(t)}catch(r){throw s.createFromBetterSqlite3(r)}return r=>{try{return r?e.get(r):e.get()}catch(o){if(o instanceof TypeError&&o.message==="This statement does not return data. Use run() instead"){this.handleTypeErrorRun(e,r);return}throw s.createFromBetterSqlite3(o)}}},this.queryFirstOrThrow=t=>{const e=this.queryFirst(t);return r=>{const o=e(r);if(o===void 0)throw new s("No result",n.AdapterErrorCode.NoResult);return o}},this.queryIterate=t=>{const e=this.db.prepare(t);return r=>r?e.iterate(r):e.iterate()},this.handleTypeErrorRun=(t,e)=>{try{e?t.run(e):t.run()}catch(r){throw s.createFromBetterSqlite3(r)}}}};c(E,"BetterSqlite3Adapter");let h=E;const u=(a=class extends n.BaseAdapterError{},c(a,"_SchemQlAdapterError"),a);u.createFromBetterSqlite3=i=>{const d=new Map([["SQLITE_CONSTRAINT_UNIQUE",n.AdapterErrorCode.UniqueConstraint],["SQLITE_CONSTRAINT_FOREIGNKEY",n.AdapterErrorCode.ForeignkeyConstraint],["SQLITE_CONSTRAINT_NOTNULL",n.AdapterErrorCode.NotnullConstraint],["SQLITE_CONSTRAINT_CHECK",n.AdapterErrorCode.CheckConstraint],["SQLITE_CONSTRAINT_PRIMARYKEY",n.AdapterErrorCode.PrimarykeyConstraint]]);return new u(i.message,d.get(i.code)??n.AdapterErrorCode.Generic,i)};let s=u;exports.SchemQlAdapterErrorCode=n.AdapterErrorCode,exports.BetterSqlite3Adapter=h,exports.SchemQlAdapterError=s;