import*as o from"https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@1.28.0/+esm";const w=await o.selectBundle({mvp:{mainModule:"https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@1.28.0/dist/duckdb-mvp.wasm",mainWorker:"https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@1.28.0/dist/duckdb-browser-mvp.worker.js"},eh:{mainModule:"https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@1.28.0/dist/duckdb-eh.wasm",mainWorker:"https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@1.28.0/dist/duckdb-browser-eh.worker.js"}}),f=new o.ConsoleLogger;class l{constructor(t){Object.defineProperties(this,{_db:{value:t}})}async queryStream(t,e){const n=await this._db.connect();let s,r;try{if(e?.length>0?s=await(await n.prepare(t)).send(...e):s=await n.send(t),r=await s.next(),r.done)throw new Error("missing first batch")}catch(i){throw await n.close(),i}return{schema:I(r.value),async*readRows(){try{for(;!r.done;)yield r.value.toArray(),r=await s.next()}finally{await n.close()}}}}async query(t,e){const n=await this.queryStream(t,e),s=[];for await(const r of n.readRows())for(const i of r)s.push(i);return s.schema=n.schema,s}async queryRow(t,e){const n=(await this.queryStream(t,e)).readRows();try{const{done:s,value:r}=await n.next();return s||!r.length?null:r[0]}finally{await n.return()}}async sql(t,...e){return await this.query(t.join("?"),e)}queryTag(t,...e){return[t.join("?"),e]}escape(t){return`"${t}"`}async describeTables(){return(await this.query("SHOW TABLES")).map(({name:t})=>({name:t}))}async describeColumns({table:t}={}){return(await this.query(`DESCRIBE ${this.escape(t)}`)).map(({column_name:e,column_type:n,null:s})=>({name:e,type:T(n),nullable:s!=="NO",databaseType:n}))}static async of(t={},e={}){const n=await b();return e.query?.castTimestampToDate===void 0&&(e={...e,query:{...e.query,castTimestampToDate:!0}}),e.query?.castBigIntToDouble===void 0&&(e={...e,query:{...e.query,castBigIntToDouble:!0}}),await n.open(e),await Promise.all(Object.entries(t).map(async([s,r])=>{if(r=await r,A(r))await m(n,s,r);else if(y(r))await u(n,s,r);else if(Array.isArray(r))await d(n,s,r);else if(E(r))await h(n,s,r);else if("data"in r){const{data:i,...c}=r;y(i)?await u(n,s,i,c):await d(n,s,i,c)}else if("file"in r){const{file:i,...c}=r;await m(n,s,i,c)}else throw new Error(`invalid source: ${r}`)})),new l(n)}}Object.defineProperty(l.prototype,"dialect",{value:"duckdb"});async function m(a,t,e,n){const s=await e.url();if(s.startsWith("blob:")){const i=await e.arrayBuffer();await a.registerFileBuffer(e.name,new Uint8Array(i))}else await a.registerFileURL(e.name,new URL(s,location).href,4);const r=await a.connect();try{switch(e.mimeType){case"text/csv":case"text/tab-separated-values":return await r.insertCSVFromPath(e.name,{name:t,schema:"main",...n}).catch(async i=>{if(i.toString().includes("Could not convert"))return await p(r,e,t);throw i});case"application/json":return await r.insertJSONFromPath(e.name,{name:t,schema:"main",...n});default:if(/\.arrow$/i.test(e.name)){const i=new Uint8Array(await e.arrayBuffer());return await r.insertArrowFromIPCStream(i,{name:t,schema:"main",...n})}if(/\.parquet$/i.test(e.name))return await r.query(`CREATE VIEW '${t}' AS SELECT * FROM parquet_scan('${e.name}')`);throw new Error(`unknown file type: ${e.mimeType}`)}}finally{await r.close()}}async function p(a,t,e){return await(await a.prepare(`CREATE TABLE '${e}' AS SELECT * FROM read_csv_auto(?, ALL_VARCHAR=TRUE)`)).send(t.name)}async function u(a,t,e,n){const s=await a.connect();try{await s.insertArrowTable(e,{name:t,schema:"main",...n})}finally{await s.close()}}async function h(a,t,e){const n=(await import("https://cdn.jsdelivr.net/npm/apache-arrow@13.0.0/+esm")).tableFromIPC(e.toArrowBuffer());return await u(a,t,n)}async function d(a,t,e,n){const s=(await import("https://cdn.jsdelivr.net/npm/apache-arrow@13.0.0/+esm")).tableFromJSON(e);return await u(a,t,s,n)}async function b(){const a=await o.createWorker(w.mainWorker),t=new o.AsyncDuckDB(f,a);return await t.instantiate(w.mainModule),t}function T(a){switch(a){case"BIGINT":case"HUGEINT":case"UBIGINT":return"bigint";case"DOUBLE":case"REAL":case"FLOAT":return"number";case"INTEGER":case"SMALLINT":case"TINYINT":case"USMALLINT":case"UINTEGER":case"UTINYINT":return"integer";case"BOOLEAN":return"boolean";case"DATE":case"TIMESTAMP":case"TIMESTAMP WITH TIME ZONE":return"date";case"VARCHAR":case"UUID":return"string";default:return/^DECIMAL\(/.test(a)?"integer":"other"}}function A(a){return a&&typeof a.name=="string"&&typeof a.url=="function"&&typeof a.arrayBuffer=="function"}function E(a){return a&&typeof a.toArrowBuffer=="function"}function y(a){return a&&typeof a.getChild=="function"&&typeof a.toArray=="function"&&a.schema&&Array.isArray(a.schema.fields)}function I(a){return a.schema.fields.map(k)}function k(a){return{name:a.name,type:g(a.type),nullable:a.nullable,databaseType:`${a.type}`}}function g(a){switch(a.typeId){case 2:return"integer";case 3:case 7:return"number";case 4:case 15:return"buffer";case 5:return"string";case 6:return"boolean";case 8:case 9:case 10:return"date";case 12:case 16:return"array";case 13:case 14:return"object";case 11:case 17:default:return"other"}}export{l as DuckDBClient}; | |