@kikko-land/electron-better-sqlite3-backend
Advanced tools
Comparing version 0.7.0 to 0.8.0
# @kikko-land/electron-better-sqlite3-backend | ||
## 0.8.0 | ||
### Minor Changes | ||
- d6204b9: Add preparedQueries support | ||
### Patch Changes | ||
- Updated dependencies [de4fd98] | ||
- Updated dependencies [d6204b9] | ||
- @kikko-land/kikko@0.11.0 | ||
- @kikko-land/d1-backend@0.3.0 | ||
## 0.7.0 | ||
@@ -4,0 +17,0 @@ |
@@ -1,55 +0,68 @@ | ||
import { initJobsState as x, getTime as o, acquireWithTrJobOrWait as y, releaseTrJobIfPossible as B } from "@kikko-land/kikko"; | ||
const q = (d) => ({ dbName: m }) => { | ||
const c = x(); | ||
let a = !0, t; | ||
return { | ||
async initialize() { | ||
if (a) | ||
throw new Error("Failed to start DB cause it is stopped"); | ||
t = await window.sqliteDb(d(m)), a && t.close(); | ||
}, | ||
async execQueries(s, r) { | ||
if (!t) | ||
throw new Error( | ||
`Failed to run queries: ${s.map((e) => e.text).join(" ")}, db not initialized` | ||
); | ||
const u = o(), f = o(), b = await y(c, r), w = o() - f, l = []; | ||
try { | ||
for (const e of s) { | ||
const n = o(), k = (() => { | ||
try { | ||
return t.all(e.text, e.values); | ||
} catch (i) { | ||
throw i instanceof Error && (i.message = `Error while executing query: ${e.text} - ${i.message}`), i; | ||
} | ||
})(), T = o(); | ||
l.push({ | ||
rows: k, | ||
performance: { | ||
execTime: T - n | ||
} | ||
}); | ||
} | ||
} catch (e) { | ||
if (r != null && r.rollbackOnFail) | ||
import { initJobsState as k, getTime as a, acquireWithTrJobOrWait as v, releaseTrJobIfPossible as B } from "@kikko-land/kikko"; | ||
const E = (m) => ({ dbName: f }) => { | ||
const l = k(); | ||
let n = !0, r; | ||
const u = async (t, e) => { | ||
if (!r) | ||
throw new Error("Failed to run queries, db not initialized"); | ||
const c = a(), y = a(), b = await v(l, e), p = a() - y, d = []; | ||
try { | ||
const s = t.type === "usual" ? t.values : t.preparedValues.map( | ||
(o) => ({ | ||
text: t.query.text, | ||
values: o | ||
}) | ||
); | ||
for (const o of s) { | ||
const h = a(), x = (() => { | ||
try { | ||
t.all("ROLLBACK", []); | ||
} catch (n) { | ||
console.error("Failed to rollback", e, n); | ||
return r.all(o.text, o.values); | ||
} catch (i) { | ||
throw i instanceof Error && (i.message = `Error while executing query: ${o.text} - ${i.message}`), i; | ||
} | ||
throw e; | ||
} finally { | ||
B(c, b, r); | ||
})(), T = a(); | ||
d.push({ | ||
rows: x, | ||
performance: { | ||
execTime: T - h | ||
} | ||
}); | ||
} | ||
const h = o(); | ||
return Promise.resolve({ | ||
result: l, | ||
performance: { | ||
totalTime: h - u, | ||
blockTime: w | ||
} catch (s) { | ||
if (e != null && e.rollbackOnFail) | ||
try { | ||
r.all("ROLLBACK", []); | ||
} catch (o) { | ||
console.error("Failed to rollback", s, o); | ||
} | ||
}); | ||
throw s; | ||
} finally { | ||
B(l, b, e); | ||
} | ||
const w = a(); | ||
return Promise.resolve({ | ||
result: d, | ||
performance: { | ||
totalTime: w - c, | ||
blockTime: p | ||
} | ||
}); | ||
}; | ||
return { | ||
async initialize() { | ||
if (n) | ||
throw new Error("Failed to start DB cause it is stopped"); | ||
r = await window.sqliteDb(m(f)), n && r.close(); | ||
}, | ||
async execQueries(t, e) { | ||
return u({ type: "usual", values: t }, e); | ||
}, | ||
async execPreparedQuery(t, e, c) { | ||
return u( | ||
{ type: "prepared", query: t, preparedValues: e }, | ||
c | ||
); | ||
}, | ||
stop() { | ||
return a = !0, t && t.close(), Promise.resolve(); | ||
return n = !0, r && r.close(), Promise.resolve(); | ||
} | ||
@@ -59,4 +72,4 @@ }; | ||
export { | ||
q as electronBetterSqlite3Backend | ||
E as electronBetterSqlite3Backend | ||
}; | ||
//# sourceMappingURL=index.es.js.map |
@@ -1,2 +0,2 @@ | ||
(function(o,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("@kikko-land/kikko")):typeof define=="function"&&define.amd?define(["exports","@kikko-land/kikko"],e):(o=typeof globalThis<"u"?globalThis:o||self,e(o.core={},o.kikko))})(this,function(o,e){"use strict";const u=f=>({dbName:m})=>{const c=e.initJobsState();let s=!0,i;return{async initialize(){if(s)throw new Error("Failed to start DB cause it is stopped");i=await window.sqliteDb(f(m)),s&&i.close()},async execQueries(a,r){if(!i)throw new Error(`Failed to run queries: ${a.map(t=>t.text).join(" ")}, db not initialized`);const T=e.getTime(),b=e.getTime(),h=await e.acquireWithTrJobOrWait(c,r),w=e.getTime()-b,d=[];try{for(const t of a){const l=e.getTime(),p=(()=>{try{return i.all(t.text,t.values)}catch(n){throw n instanceof Error&&(n.message=`Error while executing query: ${t.text} - ${n.message}`),n}})(),y=e.getTime();d.push({rows:p,performance:{execTime:y-l}})}}catch(t){if(r!=null&&r.rollbackOnFail)try{i.all("ROLLBACK",[])}catch(l){console.error("Failed to rollback",t,l)}throw t}finally{e.releaseTrJobIfPossible(c,h,r)}const g=e.getTime();return Promise.resolve({result:d,performance:{totalTime:g-T,blockTime:w}})},stop(){return s=!0,i&&i.close(),Promise.resolve()}}};o.electronBetterSqlite3Backend=u,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); | ||
(function(o,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("@kikko-land/kikko")):typeof define=="function"&&define.amd?define(["exports","@kikko-land/kikko"],e):(o=typeof globalThis<"u"?globalThis:o||self,e(o.core={},o.kikko))})(this,function(o,e){"use strict";const m=p=>({dbName:y})=>{const u=e.initJobsState();let l=!0,n;const d=async(r,t)=>{if(!n)throw new Error("Failed to run queries, db not initialized");const c=e.getTime(),T=e.getTime(),b=await e.acquireWithTrJobOrWait(u,t),h=e.getTime()-T,f=[];try{const s=r.type==="usual"?r.values:r.preparedValues.map(i=>({text:r.query.text,values:i}));for(const i of s){const g=e.getTime(),x=(()=>{try{return n.all(i.text,i.values)}catch(a){throw a instanceof Error&&(a.message=`Error while executing query: ${i.text} - ${a.message}`),a}})(),v=e.getTime();f.push({rows:x,performance:{execTime:v-g}})}}catch(s){if(t!=null&&t.rollbackOnFail)try{n.all("ROLLBACK",[])}catch(i){console.error("Failed to rollback",s,i)}throw s}finally{e.releaseTrJobIfPossible(u,b,t)}const w=e.getTime();return Promise.resolve({result:f,performance:{totalTime:w-c,blockTime:h}})};return{async initialize(){if(l)throw new Error("Failed to start DB cause it is stopped");n=await window.sqliteDb(p(y)),l&&n.close()},async execQueries(r,t){return d({type:"usual",values:r},t)},async execPreparedQuery(r,t,c){return d({type:"prepared",query:r,preparedValues:t},c)},stop(){return l=!0,n&&n.close(),Promise.resolve()}}};o.electronBetterSqlite3Backend=m,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "@kikko-land/electron-better-sqlite3-backend", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"author": "Sergey Popov", | ||
@@ -61,5 +61,5 @@ "license": "MIT", | ||
"dependencies": { | ||
"@kikko-land/kikko": "^0.10.0", | ||
"@kikko-land/kikko": "^0.11.0", | ||
"@kikko-land/boono-sql": "^0.3.0", | ||
"@kikko-land/d1-backend": "^0.2.0", | ||
"@kikko-land/d1-backend": "^0.3.0", | ||
"@kikko-land/sql.js": "^1.6.8", | ||
@@ -66,0 +66,0 @@ "better-sqlite3": ">=7.6.2" |
@@ -7,2 +7,3 @@ import { | ||
initJobsState, | ||
IPrimitiveValue, | ||
IQuery, | ||
@@ -38,2 +39,82 @@ IQueryResult, | ||
const runQueries = async ( | ||
queries: | ||
| { type: "usual"; values: IQuery[] } | ||
| { | ||
type: "prepared"; | ||
query: IQuery; | ||
preparedValues: IPrimitiveValue[][]; | ||
}, | ||
trOpts?: ITransactionOpts | ||
) => { | ||
if (!db) { | ||
throw new Error(`Failed to run queries, db not initialized`); | ||
} | ||
const totalStartedAt = getTime(); | ||
const startBlockAt = getTime(); | ||
const job = await acquireWithTrJobOrWait(jobsState, trOpts); | ||
const endBlockAt = getTime(); | ||
const blockTime = endBlockAt - startBlockAt; | ||
const result: IExecQueriesResult["result"] = []; | ||
try { | ||
const queriesToRun = | ||
queries.type === "usual" | ||
? queries.values | ||
: queries.preparedValues.map( | ||
(v): IQuery => ({ | ||
text: queries.query.text, | ||
values: v, | ||
}) | ||
); | ||
for (const q of queriesToRun) { | ||
const startTime = getTime(); | ||
const rows = (() => { | ||
try { | ||
return db.all(q.text, q.values) as IQueryResult; | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
e.message = `Error while executing query: ${q.text} - ${e.message}`; | ||
} | ||
throw e; | ||
} | ||
})(); | ||
const endTime = getTime(); | ||
result.push({ | ||
rows, | ||
performance: { | ||
execTime: endTime - startTime, | ||
}, | ||
}); | ||
} | ||
} catch (e) { | ||
if (trOpts?.rollbackOnFail) { | ||
try { | ||
db.all("ROLLBACK", []); | ||
} catch (rollbackError) { | ||
console.error(`Failed to rollback`, e, rollbackError); | ||
} | ||
} | ||
throw e; | ||
} finally { | ||
releaseTrJobIfPossible(jobsState, job, trOpts); | ||
} | ||
const totalFinishedAt = getTime(); | ||
return Promise.resolve({ | ||
result, | ||
performance: { | ||
totalTime: totalFinishedAt - totalStartedAt, | ||
blockTime, | ||
}, | ||
}); | ||
}; | ||
return { | ||
@@ -50,69 +131,14 @@ async initialize() { | ||
}, | ||
async execQueries( | ||
queries: IQuery[], | ||
trOpts: ITransactionOpts | ||
async execQueries(queries: IQuery[], transactionOpts?: ITransactionOpts) { | ||
return runQueries({ type: "usual", values: queries }, transactionOpts); | ||
}, | ||
async execPreparedQuery( | ||
query: IQuery, | ||
preparedValues: IPrimitiveValue[][], | ||
transactionOpts?: ITransactionOpts | ||
): Promise<IExecQueriesResult> { | ||
if (!db) { | ||
throw new Error( | ||
`Failed to run queries: ${queries | ||
.map((q) => q.text) | ||
.join(" ")}, db not initialized` | ||
); | ||
} | ||
const totalStartedAt = getTime(); | ||
const startBlockAt = getTime(); | ||
const job = await acquireWithTrJobOrWait(jobsState, trOpts); | ||
const endBlockAt = getTime(); | ||
const blockTime = endBlockAt - startBlockAt; | ||
const result: IExecQueriesResult["result"] = []; | ||
try { | ||
for (const q of queries) { | ||
const startTime = getTime(); | ||
const rows = (() => { | ||
try { | ||
return db.all(q.text, q.values) as IQueryResult; | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
e.message = `Error while executing query: ${q.text} - ${e.message}`; | ||
} | ||
throw e; | ||
} | ||
})(); | ||
const endTime = getTime(); | ||
result.push({ | ||
rows, | ||
performance: { | ||
execTime: endTime - startTime, | ||
}, | ||
}); | ||
} | ||
} catch (e) { | ||
if (trOpts?.rollbackOnFail) { | ||
try { | ||
db.all("ROLLBACK", []); | ||
} catch (rollbackError) { | ||
console.error(`Failed to rollback`, e, rollbackError); | ||
} | ||
} | ||
throw e; | ||
} finally { | ||
releaseTrJobIfPossible(jobsState, job, trOpts); | ||
} | ||
const totalFinishedAt = getTime(); | ||
return Promise.resolve({ | ||
result, | ||
performance: { | ||
totalTime: totalFinishedAt - totalStartedAt, | ||
blockTime, | ||
}, | ||
}); | ||
return runQueries( | ||
{ type: "prepared", query, preparedValues }, | ||
transactionOpts | ||
); | ||
}, | ||
@@ -119,0 +145,0 @@ stop() { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33850
315
+ Added@kikko-land/d1-backend@0.3.4(transitive)
+ Added@kikko-land/kikko@0.11.00.15.0(transitive)
- Removed@kikko-land/d1-backend@0.2.0(transitive)
- Removed@kikko-land/kikko@0.10.0(transitive)
Updated@kikko-land/kikko@^0.11.0