Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@kikko-land/kikko

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kikko-land/kikko - npm Package Compare versions

Comparing version 0.8.0 to 0.9.0

6

CHANGELOG.md
# @kikko-land/core
## 0.9.0
### Minor Changes
- 8ffc4e0: Rename atomicTtransaction -> runInAtomicTransaction
## 0.8.0

@@ -4,0 +10,0 @@

291

dist/index.es.js
import { deepEqual as U } from "fast-equals";
import { sql as S } from "@kikko-land/boono-sql";
import { sql as v } from "@kikko-land/boono-sql";
export * from "@kikko-land/boono-sql";

@@ -7,7 +7,7 @@ const R = (e, i) => {

throw new Error("Not in transaction.");
const t = e.__state.localState.transactionState.current, r = [], a = (l) => (d, o) => {
if (t.id === o.id) {
i(l, d, t);
for (const c of r)
c();
const t = e.__state.localState.transactionState.current, r = [], a = (u) => (s, l) => {
if (t.id === l.id) {
i(u, s, t);
for (const o of r)
o();
}

@@ -47,3 +47,3 @@ };

}
const J = (e, i) => {
const L = (e, i) => {
const t = i.deduplicate === void 0 ? !0 : i.deduplicate;

@@ -77,16 +77,16 @@ return {

throw new Error(`reactiveVar ${i.label} is stopped!`);
let l;
const d = (o) => {
l && l(), l = r(o);
let u;
const s = (l) => {
u && u(), u = r(l);
};
return this.__state.subscriptions.push(d), a && d(this.__state.value), () => {
this.__state.subscriptions = this.__state.subscriptions.filter((o) => o !== d);
return this.__state.subscriptions.push(s), a && s(this.__state.value), () => {
this.__state.subscriptions = this.__state.subscriptions.filter((l) => l !== s);
};
},
waitTill(r, a) {
const l = new A(
const u = new A(
`waitUntil for reactiveVar ${i.label} is stopped due to stop signal`
), d = new g(
), s = new g(
`waitUntil for reactiveVar ${i.label} is timed out`
), o = new A(
), l = new A(
`waitUntil for reactiveVar ${i.label} is stopped due to reactive var stop`

@@ -96,16 +96,16 @@ );

throw new Error(`reactiveVar ${i.label} is stopped!`);
return new Promise((n, u) => {
var v;
const s = [], T = () => {
for (const f of s)
return new Promise((c, d) => {
var w;
const n = [], T = () => {
for (const f of n)
f();
};
if (s.push(
((v = a == null ? void 0 : a.stopIf) == null ? void 0 : v.subscribe((f) => {
!f || (T(), u(l));
if (n.push(
((w = a == null ? void 0 : a.stopIf) == null ? void 0 : w.subscribe((f) => {
!f || (T(), d(u));
}, !0)) || (() => {
})
), s.push(
), n.push(
this.subscribe((f) => {
!r(f) || (T(), n());
!r(f) || (T(), c());
}, !0)

@@ -115,7 +115,7 @@ ), (a == null ? void 0 : a.timeout) === void 0 || typeof (a == null ? void 0 : a.timeout) == "number") {

() => {
T(), u(d);
T(), d(s);
},
(a == null ? void 0 : a.timeout) === void 0 ? 6e4 : a.timeout
);
s.push(() => {
n.push(() => {
clearTimeout(f);

@@ -125,3 +125,3 @@ });

this.__state.onStop.push(() => {
T(), u(o);
T(), d(l);
});

@@ -139,3 +139,3 @@ });

};
}, L = (e, i) => {
}, J = (e, i) => {
const {

@@ -161,9 +161,9 @@ __state: {

}) => {
var T, v, f, k, $, x, F, I, E;
var T, w, f, k, $, x, I, F, E;
const {
localState: { transactionState: r },
sharedState: { dbBackend: a },
sharedState: l
sharedState: u
} = e.__state;
if (r.current || L(e, () => JSON.stringify(i)), t && ((T = r.current) == null ? void 0 : T.id) !== t.transactionId)
if (r.current || J(e, () => JSON.stringify(i)), t && ((T = r.current) == null ? void 0 : T.id) !== t.transactionId)
throw new Error(

@@ -174,4 +174,4 @@ `Cannot run queries in a transaction that is not the current one. Transaction opts: ${JSON.stringify(

);
const d = G(i.map((m) => m.toSql())), o = performance.now(), { result: c, performance: n } = await a.execQueries(
d,
const s = G(i.map((m) => m.toSql())), l = performance.now(), { result: o, performance: c } = await a.execQueries(
s,
t || (r.current ? {

@@ -184,5 +184,5 @@ transactionId: r.current.id,

} : void 0)
), u = performance.now();
), d = performance.now();
if (!e.__state.localState.suppressLog) {
const m = c.map(({ performance: _ }, B) => {
const m = o.map(({ performance: _ }, B) => {
const D = [

@@ -193,35 +193,35 @@ _.prepareTime !== void 0 ? `prepareTime=${(_.prepareTime / 1e3).toFixed(4)}` : "",

].filter((y) => y.length !== 0).join(" ");
return [d[B].text.slice(0, 1e3), D].filter((y) => y.length !== 0).join(" ");
}), w = (() => m.length === 1 ? m[0] : `
return [s[B].text.slice(0, 1e3), D].filter((y) => y.length !== 0).join(" ");
}), S = (() => m.length === 1 ? m[0] : `
` + m.map((_) => `{${_}}`).join(`
`))(), h = `%c[${e.__state.sharedState.dbName}] ` + [
(v = r.current) != null && v.id ? `[tr_id=${(f = r.current) == null ? void 0 : f.id.substring(0, 6)}]` : "",
w,
(n == null ? void 0 : n.sendTime) !== void 0 ? `sendTime=${(n.sendTime / 1e3).toFixed(4)}` : "",
(n == null ? void 0 : n.receiveTime) !== void 0 ? `receiveTime=${(n.receiveTime / 1e3).toFixed(4)}` : "",
(n == null ? void 0 : n.blockTime) !== void 0 ? `blockTime=${(n.blockTime / 1e3).toFixed(4)}` : "",
`totalTime=${((u - o) / 1e3).toFixed(4)}`
].filter((_) => _.length !== 0).join(" "), C = (x = l.transactionsStates.byId[($ = (k = r.current) == null ? void 0 : k.id) != null ? $ : ""]) == null ? void 0 : x.i, V = typeof C == "number" ? q[C % q.length] : "white";
(w = r.current) != null && w.id ? `[tr_id=${(f = r.current) == null ? void 0 : f.id.substring(0, 6)}]` : "",
S,
(c == null ? void 0 : c.sendTime) !== void 0 ? `sendTime=${(c.sendTime / 1e3).toFixed(4)}` : "",
(c == null ? void 0 : c.receiveTime) !== void 0 ? `receiveTime=${(c.receiveTime / 1e3).toFixed(4)}` : "",
(c == null ? void 0 : c.blockTime) !== void 0 ? `blockTime=${(c.blockTime / 1e3).toFixed(4)}` : "",
`totalTime=${((d - l) / 1e3).toFixed(4)}`
].filter((_) => _.length !== 0).join(" "), C = (x = u.transactionsStates.byId[($ = (k = r.current) == null ? void 0 : k.id) != null ? $ : ""]) == null ? void 0 : x.i, V = typeof C == "number" ? q[C % q.length] : "white";
console.log(h, `color: ${V}`);
}
const s = (E = l.transactionsStates.byId[(I = (F = r.current) == null ? void 0 : F.id) != null ? I : ""]) == null ? void 0 : E.performance;
return s && (c.some((m) => m.performance.execTime !== void 0) && (s.execTime === void 0 && (s.execTime = 0), s.execTime += c.reduce(
(m, w) => {
const n = (E = u.transactionsStates.byId[(F = (I = r.current) == null ? void 0 : I.id) != null ? F : ""]) == null ? void 0 : E.performance;
return n && (o.some((m) => m.performance.execTime !== void 0) && (n.execTime === void 0 && (n.execTime = 0), n.execTime += o.reduce(
(m, S) => {
var h;
return m + ((h = w.performance.execTime) != null ? h : 0);
return m + ((h = S.performance.execTime) != null ? h : 0);
},
0
)), c.some((m) => m.performance.freeTime !== void 0) && (s.freeTime === void 0 && (s.freeTime = 0), s.freeTime += c.reduce(
(m, w) => {
)), o.some((m) => m.performance.freeTime !== void 0) && (n.freeTime === void 0 && (n.freeTime = 0), n.freeTime += o.reduce(
(m, S) => {
var h;
return m + ((h = w.performance.freeTime) != null ? h : 0);
return m + ((h = S.performance.freeTime) != null ? h : 0);
},
0
)), c.some((m) => m.performance.prepareTime !== void 0) && (s.prepareTime === void 0 && (s.prepareTime = 0), s.prepareTime += c.reduce(
(m, w) => {
)), o.some((m) => m.performance.prepareTime !== void 0) && (n.prepareTime === void 0 && (n.prepareTime = 0), n.prepareTime += o.reduce(
(m, S) => {
var h;
return m + ((h = w.performance.prepareTime) != null ? h : 0);
return m + ((h = S.performance.prepareTime) != null ? h : 0);
},
0
)), n.sendTime && (s.sendTime || (s.sendTime = 0), s.sendTime += n.sendTime), n.receiveTime && (s.receiveTime || (s.receiveTime = 0), s.receiveTime += n.receiveTime), n.blockTime && (s.blockTime || (s.blockTime = 0), s.blockTime += n.blockTime)), { db: e, result: c, performance: n, queries: i };
)), c.sendTime && (n.sendTime || (n.sendTime = 0), n.sendTime += c.sendTime), c.receiveTime && (n.receiveTime || (n.receiveTime = 0), n.receiveTime += c.receiveTime), c.blockTime && (n.blockTime || (n.blockTime = 0), n.blockTime += c.blockTime)), { db: e, result: o, performance: c, queries: i };
}, p = async (e, i, t) => {

@@ -232,6 +232,6 @@ const r = [

].reverse();
let a = (l) => Promise.resolve(l);
for (const l of r) {
const d = a;
a = (o) => l({ ...o, next: d });
let a = (u) => Promise.resolve(u);
for (const u of r) {
const s = a;
a = (l) => u({ ...l, next: s });
}

@@ -246,3 +246,3 @@ return await a({

},
queries: i.map((l) => l.toSql()),
queries: i.map((u) => u.toSql()),
transactionOpts: t

@@ -274,9 +274,8 @@ });

localState: { transactionState: r },
sharedState: { eventsEmitter: a, transactionsStates: l },
sharedState: d
sharedState: { eventsEmitter: a, transactionsStates: u }
} = e.__state;
if (r.current)
return await t(e);
L(e, () => "transaction");
const o = {
J(e, () => "transaction");
const s = {
id: b(),

@@ -291,9 +290,9 @@ type: "async"

...e.__state.localState,
transactionState: { current: o }
transactionState: { current: s }
}
}
};
const c = performance.now(), n = {
const l = performance.now(), o = {
i: M++,
current: o,
current: s,
performance: {

@@ -309,9 +308,9 @@ prepareTime: 0,

};
l.byId[o.id] = n;
u.byId[s.id] = o;
try {
await a.emit("transactionWillStart", e, o), await p(
await a.emit("transactionWillStart", e, s), await p(
e,
[S`BEGIN ${S.raw(i.toLocaleUpperCase())} TRANSACTION`],
[v`BEGIN ${v.raw(i.toLocaleUpperCase())} TRANSACTION`],
{
transactionId: o.id,
transactionId: s.id,
containsTransactionStart: !0,

@@ -322,7 +321,7 @@ containsTransactionFinish: !1,

}
), await a.emit("transactionStarted", e, o);
), await a.emit("transactionStarted", e, s);
try {
const u = await t(e);
return await a.emit("transactionWillCommit", e, o), await p(e, [S`COMMIT`], {
transactionId: o.id,
const c = await t(e);
return await a.emit("transactionWillCommit", e, s), await p(e, [v`COMMIT`], {
transactionId: s.id,
containsTransactionStart: !1,

@@ -332,8 +331,8 @@ containsTransactionFinish: !0,

rollbackOnFail: !1
}), await a.emit("transactionCommitted", e, o), u;
} catch (u) {
console.error("Rollback transaction", u), await a.emit("transactionWillRollback", e, o);
}), await a.emit("transactionCommitted", e, s), c;
} catch (c) {
console.error("Rollback transaction", c), await a.emit("transactionWillRollback", e, s);
try {
await p(e, [S`ROLLBACK`], {
transactionId: o.id,
await p(e, [v`ROLLBACK`], {
transactionId: s.id,
containsTransactionStart: !1,

@@ -344,9 +343,9 @@ containsTransactionFinish: !1,

});
} catch (s) {
console.warn("Rollback transaction failed", s);
} catch (d) {
console.warn("Rollback transaction failed", d);
}
throw await a.emit("transactionRollbacked", e, o), u;
throw await a.emit("transactionRollbacked", e, s), c;
}
} finally {
n.performance.totalTime = performance.now() - c, W(e, o.id, n.performance), delete l.byId[o.id];
o.performance.totalTime = performance.now() - l, W(e, s.id, o.performance), delete u.byId[s.id];
}

@@ -363,4 +362,4 @@ }, j = () => ({

localState: { transactionState: r },
sharedState: { eventsEmitter: a, transactionsStates: l },
sharedState: d
sharedState: { eventsEmitter: a, transactionsStates: u },
sharedState: s
} = e.__state;

@@ -371,15 +370,15 @@ if (r.current)

);
const o = await (async () => {
const l = await (async () => {
if (Array.isArray(t))
return t;
{
const s = j();
return await t(s), s.__state.queries;
const n = j();
return await t(n), n.__state.queries;
}
})(), c = {
})(), o = {
id: b(),
type: "atomic"
}, n = {
}, c = {
i: M++,
current: c,
current: o,
performance: {

@@ -401,17 +400,17 @@ prepareTime: 0,

...e.__state.localState,
transactionState: { current: c }
transactionState: { current: o }
}
}
}, l.byId[c.id] = n;
const u = performance.now();
}, u.byId[o.id] = c;
const d = performance.now();
try {
await a.emit("transactionWillStart", e, c), await a.emit("transactionStarted", e, c), await p(
await a.emit("transactionWillStart", e, o), await a.emit("transactionStarted", e, o), await p(
e,
[
S`BEGIN ${S.raw(i.toUpperCase())} TRANSACTION`,
...o,
S`COMMIT`
v`BEGIN ${v.raw(i.toUpperCase())} TRANSACTION`,
...l,
v`COMMIT`
],
{
transactionId: c.id,
transactionId: o.id,
containsTransactionStart: !0,

@@ -422,7 +421,7 @@ containsTransactionFinish: !0,

}
), await a.emit("transactionWillCommit", e, c), await a.emit("transactionCommitted", e, c);
} catch (s) {
throw console.error("Rollback transaction", s), await a.emit("transactionWillRollback", e, c), await a.emit("transactionRollbacked", e, c), s;
), await a.emit("transactionWillCommit", e, o), await a.emit("transactionCommitted", e, o);
} catch (n) {
throw console.error("Rollback transaction", n), await a.emit("transactionWillRollback", e, o), await a.emit("transactionRollbacked", e, o), n;
} finally {
n.performance.totalTime = performance.now() - u, W(e, c.id, n.performance), delete l.byId[c.id];
c.performance.totalTime = performance.now() - d, W(e, o.id, c.performance), delete u.byId[o.id];
}

@@ -433,16 +432,17 @@ }, O = async ({

queriesMiddlewares: t,
dbBackend: r
dbBackend: r,
suppressLog: a
}) => {
const a = J(
const u = L(
"running",
{ label: "runningState" }
), l = (await r)({
), s = (await r)({
dbName: e
}), d = {
}), l = {
__state: {
sharedState: {
clientId: b(),
dbBackend: l,
dbBackend: s,
dbName: e,
runningState: a,
runningState: u,
eventsEmitter: z(),

@@ -453,35 +453,36 @@ transactionsStates: { byId: {} }

queriesMiddlewares: t || [],
transactionState: {}
transactionState: {},
suppressLog: Boolean(a)
}
},
runInTransaction(n, u) {
return Y(this, (u == null ? void 0 : u.type) || "deferred", n);
runInTransaction(d, n) {
return Y(this, (n == null ? void 0 : n.type) || "deferred", d);
},
async runAtomicTransaction(n, u) {
return await H(this, (u == null ? void 0 : u.type) || "deferred", n);
async runInAtomicTransaction(d, n) {
return await H(this, (n == null ? void 0 : n.type) || "deferred", d);
},
async runQueries(n) {
return (await p(this, n)).result.map(({ rows: s }) => s);
async runQueries(d) {
return (await p(this, d)).result.map(({ rows: T }) => T);
},
async runQuery(n) {
return (await this.runQueries([n]))[0];
async runQuery(d) {
return (await this.runQueries([d]))[0];
},
runAfterTransactionCommitted(n) {
return R(this, (u, s, T) => {
u === "committed" && n(s, T);
runAfterTransactionCommitted(d) {
return R(this, (n, T, w) => {
n === "committed" && d(T, w);
});
},
runAfterTransactionRollbacked(n) {
R(d, (u, s, T) => {
u === "rollbacked" && n(s, T);
runAfterTransactionRollbacked(d) {
R(l, (n, T, w) => {
n === "rollbacked" && d(T, w);
});
}
};
let o = d;
const c = () => d.__state.sharedState.runningState.value;
if (c() !== "running" || (await l.initialize(), c() !== "running"))
return d;
for (const n of i || [])
o = n(o);
return await d.__state.sharedState.eventsEmitter.emit("initialized", d), o;
let o = l;
const c = () => l.__state.sharedState.runningState.value;
if (c() !== "running" || (await s.initialize(), c() !== "running"))
return l;
for (const d of i || [])
o = d(o);
return await l.__state.sharedState.eventsEmitter.emit("initialized", l), o;
}, ee = async (e) => {

@@ -498,3 +499,3 @@ e.__state.sharedState.runningState.value = "stopping", await e.__state.sharedState.dbBackend.stop(), e.__state.sharedState.runningState.value = "stopped", queueMicrotask(() => {

)}, queue: ${JSON.stringify(t, null, 2)}`;
}, te = () => J(
}, te = () => L(
{

@@ -506,20 +507,20 @@ queue: [],

), X = async (e, i) => {
const t = i != null ? i : b(), r = { id: t }, { current: a, queue: l } = e.value;
if (a || l.length > 0) {
const d = e.waitTill((o) => {
var c;
return ((c = o.current) == null ? void 0 : c.id) === t;
const t = i != null ? i : b(), r = { id: t }, { current: a, queue: u } = e.value;
if (a || u.length > 0) {
const s = e.waitTill((l) => {
var o;
return ((o = l.current) == null ? void 0 : o.id) === t;
});
e.value = {
queue: [...l, r],
queue: [...u, r],
current: a
};
try {
await d;
} catch (o) {
throw o instanceof g ? new g(
`Timeout error while job acquire: '${o.message}'. Is it a dead lock? ${Q(
await s;
} catch (l) {
throw l instanceof g ? new g(
`Timeout error while job acquire: '${l.message}'. Is it a dead lock? ${Q(
e.value
)}, jobToAcquire: ${JSON.stringify(r, null, 2)}`
) : o;
) : l;
}

@@ -586,3 +587,3 @@ } else

b as makeId,
J as reactiveVar,
L as reactiveVar,
N as releaseJob,

@@ -589,0 +590,0 @@ oe as releaseTrJobIfPossible,

@@ -1,4 +0,4 @@

(function(m,b){typeof exports=="object"&&typeof module<"u"?b(exports,require("fast-equals"),require("@kikko-land/boono-sql")):typeof define=="function"&&define.amd?define(["exports","fast-equals","@kikko-land/boono-sql"],b):(m=typeof globalThis<"u"?globalThis:m||self,b(m.core={},m.fastEquals,m.boonoSql))})(this,function(m,b,S){"use strict";const q=(e,i)=>{if(!e.__state.localState.transactionState.current)throw new Error("Not in transaction.");const t=e.__state.localState.transactionState.current,r=[],a=l=>(d,s)=>{if(t.id===s.id){i(l,d,t);for(const c of r)c()}};r.push(e.__state.sharedState.eventsEmitter.on("transactionCommitted",a("committed"))),r.push(e.__state.sharedState.eventsEmitter.on("transactionRollbacked",a("rollbacked")))};function U(){const e={};return{async emit(i,...t){const r=e[i]||[];for(const a of r)await a(...t)},on(i,t){return(e[i]=e[i]||[]).push(t),()=>{const r=e[i]||[];e[i]=r.filter(a=>a!==t)}}}}class p extends Error{}class $ extends Error{}const E=(e,i)=>{const t=i.deduplicate===void 0?!0:i.deduplicate;return{__state:{subscriptions:[],value:e,isStopped:!1,onStop:[]},get isStopped(){return this.__state.isStopped},set value(r){if(this.isStopped)throw new Error(`reactiveVar ${i.label} is stopped!`);if(!(t&&b.deepEqual(this.__state.value,r))){this.__state.value=r;for(const a of this.__state.subscriptions)a(r)}},get value(){if(this.isStopped)throw new Error(`reactiveVar ${i.label} is stopped!`);return this.__state.value},subscribe(r,a=!0){if(this.isStopped)throw new Error(`reactiveVar ${i.label} is stopped!`);let l;const d=s=>{l&&l(),l=r(s)};return this.__state.subscriptions.push(d),a&&d(this.__state.value),()=>{this.__state.subscriptions=this.__state.subscriptions.filter(s=>s!==d)}},waitTill(r,a){const l=new $(`waitUntil for reactiveVar ${i.label} is stopped due to stop signal`),d=new p(`waitUntil for reactiveVar ${i.label} is timed out`),s=new $(`waitUntil for reactiveVar ${i.label} is stopped due to reactive var stop`);if(this.isStopped)throw new Error(`reactiveVar ${i.label} is stopped!`);return new Promise((n,u)=>{var k;const o=[],f=()=>{for(const h of o)h()};if(o.push(((k=a==null?void 0:a.stopIf)==null?void 0:k.subscribe(h=>{!h||(f(),u(l))},!0))||(()=>{})),o.push(this.subscribe(h=>{!r(h)||(f(),n())},!0)),(a==null?void 0:a.timeout)===void 0||typeof(a==null?void 0:a.timeout)=="number"){const h=setTimeout(()=>{f(),u(d)},(a==null?void 0:a.timeout)===void 0?6e4:a.timeout);o.push(()=>{clearTimeout(h)})}this.__state.onStop.push(()=>{f(),u(s)})})},stop(){if(this.isStopped)throw new Error(`reactiveVar ${i.label} is already stopped!`);this.__state.subscriptions=[];for(const r of this.__state.onStop)r();this.__state.isStopped=!0}}},x=(e,i)=>{const{__state:{sharedState:{runningState:t,dbName:r}}}=e;if(t.value!=="running")throw new Error(`Failed to start ${i()}, db ${r} is stopping`)},j=e=>e.map(i=>i.preparedQuery);function g(){let e="";const i="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",t=i.length;for(let r=0;r<32;r++)e+=i.charAt(Math.floor(Math.random()*t));return e}const C=["yellow","cyan","magenta"],z=async({db:e,queries:i,transactionOpts:t})=>{var f,k,h,L,M,W,D,V,Q;const{localState:{transactionState:r},sharedState:{dbBackend:a},sharedState:l}=e.__state;if(r.current||x(e,()=>JSON.stringify(i)),t&&((f=r.current)==null?void 0:f.id)!==t.transactionId)throw new Error(`Cannot run queries in a transaction that is not the current one. Transaction opts: ${JSON.stringify(t)}, local transaction: ${JSON.stringify(r)}`);const d=j(i.map(T=>T.toSql())),s=performance.now(),{result:c,performance:n}=await a.execQueries(d,t||(r.current?{transactionId:r.current.id,containsTransactionStart:!1,containsTransactionFinish:!1,containsTransactionRollback:!1,rollbackOnFail:!1}:void 0)),u=performance.now();if(!e.__state.localState.suppressLog){const T=c.map(({performance:w},re)=>{const ne=[w.prepareTime!==void 0?`prepareTime=${(w.prepareTime/1e3).toFixed(4)}`:"",w.execTime!==void 0?`execTime=${(w.execTime/1e3).toFixed(4)}`:"",w.freeTime!==void 0?`freeTime=${(w.freeTime/1e3).toFixed(4)}`:""].filter(F=>F.length!==0).join(" ");return[d[re].text.slice(0,1e3),ne].filter(F=>F.length!==0).join(" ")}),v=(()=>T.length===1?T[0]:`
(function(d,b){typeof exports=="object"&&typeof module<"u"?b(exports,require("fast-equals"),require("@kikko-land/boono-sql")):typeof define=="function"&&define.amd?define(["exports","fast-equals","@kikko-land/boono-sql"],b):(d=typeof globalThis<"u"?globalThis:d||self,b(d.core={},d.fastEquals,d.boonoSql))})(this,function(d,b,S){"use strict";const q=(e,i)=>{if(!e.__state.localState.transactionState.current)throw new Error("Not in transaction.");const t=e.__state.localState.transactionState.current,r=[],a=u=>(o,l)=>{if(t.id===l.id){i(u,o,t);for(const s of r)s()}};r.push(e.__state.sharedState.eventsEmitter.on("transactionCommitted",a("committed"))),r.push(e.__state.sharedState.eventsEmitter.on("transactionRollbacked",a("rollbacked")))};function U(){const e={};return{async emit(i,...t){const r=e[i]||[];for(const a of r)await a(...t)},on(i,t){return(e[i]=e[i]||[]).push(t),()=>{const r=e[i]||[];e[i]=r.filter(a=>a!==t)}}}}class g extends Error{}class $ extends Error{}const I=(e,i)=>{const t=i.deduplicate===void 0?!0:i.deduplicate;return{__state:{subscriptions:[],value:e,isStopped:!1,onStop:[]},get isStopped(){return this.__state.isStopped},set value(r){if(this.isStopped)throw new Error(`reactiveVar ${i.label} is stopped!`);if(!(t&&b.deepEqual(this.__state.value,r))){this.__state.value=r;for(const a of this.__state.subscriptions)a(r)}},get value(){if(this.isStopped)throw new Error(`reactiveVar ${i.label} is stopped!`);return this.__state.value},subscribe(r,a=!0){if(this.isStopped)throw new Error(`reactiveVar ${i.label} is stopped!`);let u;const o=l=>{u&&u(),u=r(l)};return this.__state.subscriptions.push(o),a&&o(this.__state.value),()=>{this.__state.subscriptions=this.__state.subscriptions.filter(l=>l!==o)}},waitTill(r,a){const u=new $(`waitUntil for reactiveVar ${i.label} is stopped due to stop signal`),o=new g(`waitUntil for reactiveVar ${i.label} is timed out`),l=new $(`waitUntil for reactiveVar ${i.label} is stopped due to reactive var stop`);if(this.isStopped)throw new Error(`reactiveVar ${i.label} is stopped!`);return new Promise((c,m)=>{var v;const n=[],f=()=>{for(const h of n)h()};if(n.push(((v=a==null?void 0:a.stopIf)==null?void 0:v.subscribe(h=>{!h||(f(),m(u))},!0))||(()=>{})),n.push(this.subscribe(h=>{!r(h)||(f(),c())},!0)),(a==null?void 0:a.timeout)===void 0||typeof(a==null?void 0:a.timeout)=="number"){const h=setTimeout(()=>{f(),m(o)},(a==null?void 0:a.timeout)===void 0?6e4:a.timeout);n.push(()=>{clearTimeout(h)})}this.__state.onStop.push(()=>{f(),m(l)})})},stop(){if(this.isStopped)throw new Error(`reactiveVar ${i.label} is already stopped!`);this.__state.subscriptions=[];for(const r of this.__state.onStop)r();this.__state.isStopped=!0}}},x=(e,i)=>{const{__state:{sharedState:{runningState:t,dbName:r}}}=e;if(t.value!=="running")throw new Error(`Failed to start ${i()}, db ${r} is stopping`)},j=e=>e.map(i=>i.preparedQuery);function y(){let e="";const i="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",t=i.length;for(let r=0;r<32;r++)e+=i.charAt(Math.floor(Math.random()*t));return e}const C=["yellow","cyan","magenta"],z=async({db:e,queries:i,transactionOpts:t})=>{var f,v,h,L,M,W,D,V,Q;const{localState:{transactionState:r},sharedState:{dbBackend:a},sharedState:u}=e.__state;if(r.current||x(e,()=>JSON.stringify(i)),t&&((f=r.current)==null?void 0:f.id)!==t.transactionId)throw new Error(`Cannot run queries in a transaction that is not the current one. Transaction opts: ${JSON.stringify(t)}, local transaction: ${JSON.stringify(r)}`);const o=j(i.map(T=>T.toSql())),l=performance.now(),{result:s,performance:c}=await a.execQueries(o,t||(r.current?{transactionId:r.current.id,containsTransactionStart:!1,containsTransactionFinish:!1,containsTransactionRollback:!1,rollbackOnFail:!1}:void 0)),m=performance.now();if(!e.__state.localState.suppressLog){const T=s.map(({performance:w},re)=>{const ne=[w.prepareTime!==void 0?`prepareTime=${(w.prepareTime/1e3).toFixed(4)}`:"",w.execTime!==void 0?`execTime=${(w.execTime/1e3).toFixed(4)}`:"",w.freeTime!==void 0?`freeTime=${(w.freeTime/1e3).toFixed(4)}`:""].filter(F=>F.length!==0).join(" ");return[o[re].text.slice(0,1e3),ne].filter(F=>F.length!==0).join(" ")}),p=(()=>T.length===1?T[0]:`
`+T.map(w=>`{${w}}`).join(`
`))(),_=`%c[${e.__state.sharedState.dbName}] `+[(k=r.current)!=null&&k.id?`[tr_id=${(h=r.current)==null?void 0:h.id.substring(0,6)}]`:"",v,(n==null?void 0:n.sendTime)!==void 0?`sendTime=${(n.sendTime/1e3).toFixed(4)}`:"",(n==null?void 0:n.receiveTime)!==void 0?`receiveTime=${(n.receiveTime/1e3).toFixed(4)}`:"",(n==null?void 0:n.blockTime)!==void 0?`blockTime=${(n.blockTime/1e3).toFixed(4)}`:"",`totalTime=${((u-s)/1e3).toFixed(4)}`].filter(w=>w.length!==0).join(" "),B=(W=l.transactionsStates.byId[(M=(L=r.current)==null?void 0:L.id)!=null?M:""])==null?void 0:W.i,ae=typeof B=="number"?C[B%C.length]:"white";console.log(_,`color: ${ae}`)}const o=(Q=l.transactionsStates.byId[(V=(D=r.current)==null?void 0:D.id)!=null?V:""])==null?void 0:Q.performance;return o&&(c.some(T=>T.performance.execTime!==void 0)&&(o.execTime===void 0&&(o.execTime=0),o.execTime+=c.reduce((T,v)=>{var _;return T+((_=v.performance.execTime)!=null?_:0)},0)),c.some(T=>T.performance.freeTime!==void 0)&&(o.freeTime===void 0&&(o.freeTime=0),o.freeTime+=c.reduce((T,v)=>{var _;return T+((_=v.performance.freeTime)!=null?_:0)},0)),c.some(T=>T.performance.prepareTime!==void 0)&&(o.prepareTime===void 0&&(o.prepareTime=0),o.prepareTime+=c.reduce((T,v)=>{var _;return T+((_=v.performance.prepareTime)!=null?_:0)},0)),n.sendTime&&(o.sendTime||(o.sendTime=0),o.sendTime+=n.sendTime),n.receiveTime&&(o.receiveTime||(o.receiveTime=0),o.receiveTime+=n.receiveTime),n.blockTime&&(o.blockTime||(o.blockTime=0),o.blockTime+=n.blockTime)),{db:e,result:c,performance:n,queries:i}},y=async(e,i,t)=>{const r=[...e.__state.localState.queriesMiddlewares,z].reverse();let a=l=>Promise.resolve(l);for(const l of r){const d=a;a=s=>l({...s,next:d})}return await a({db:e,result:[],performance:{sendTime:void 0,receiveTime:void 0,totalTime:0},queries:i.map(l=>l.toSql()),transactionOpts:t})};let A=0;const R=(e,i,t)=>{if(e.__state.localState.suppressLog)return;const r=[t.prepareTime===void 0?"":`prepareTime=${(t.prepareTime/1e3).toFixed(4)}`,t.execTime===void 0?"":`execTime=${(t.execTime/1e3).toFixed(4)}`,t.freeTime===void 0?"":`freeTime=${(t.freeTime/1e3).toFixed(4)}`,t.sendTime===void 0?"":`sendTime=${(t.sendTime/1e3).toFixed(4)}`,t.receiveTime===void 0?"":`receiveTime=${(t.receiveTime/1e3).toFixed(4)}`,t.blockTime===void 0?"":`blockTime=${(t.blockTime/1e3).toFixed(4)}`,`totalTime=${(t.totalTime/1e3).toFixed(4)}`].filter(a=>a.length!==0).join(" ");console.log(`%c[${e.__state.sharedState.dbName}][tr_id=${i.slice(0,6)}] Transaction finished with ${r}`,"color: #fff; background-color: #1da1f2; padding: 2px 4px; border-radius: 2px")},G=async(e,i,t)=>{const{localState:{transactionState:r},sharedState:{eventsEmitter:a,transactionsStates:l},sharedState:d}=e.__state;if(r.current)return await t(e);x(e,()=>"transaction");const s={id:g(),type:"async"};e={...e,__state:{...e.__state,localState:{...e.__state.localState,transactionState:{current:s}}}};const c=performance.now(),n={i:A++,current:s,performance:{prepareTime:0,execTime:0,freeTime:0,sendTime:0,receiveTime:0,totalTime:0,blockTime:0}};l.byId[s.id]=n;try{await a.emit("transactionWillStart",e,s),await y(e,[S.sql`BEGIN ${S.sql.raw(i.toLocaleUpperCase())} TRANSACTION`],{transactionId:s.id,containsTransactionStart:!0,containsTransactionFinish:!1,containsTransactionRollback:!1,rollbackOnFail:!1}),await a.emit("transactionStarted",e,s);try{const u=await t(e);return await a.emit("transactionWillCommit",e,s),await y(e,[S.sql`COMMIT`],{transactionId:s.id,containsTransactionStart:!1,containsTransactionFinish:!0,containsTransactionRollback:!1,rollbackOnFail:!1}),await a.emit("transactionCommitted",e,s),u}catch(u){console.error("Rollback transaction",u),await a.emit("transactionWillRollback",e,s);try{await y(e,[S.sql`ROLLBACK`],{transactionId:s.id,containsTransactionStart:!1,containsTransactionFinish:!1,containsTransactionRollback:!0,rollbackOnFail:!1})}catch(o){console.warn("Rollback transaction failed",o)}throw await a.emit("transactionRollbacked",e,s),u}}finally{n.performance.totalTime=performance.now()-c,R(e,s.id,n.performance),delete l.byId[s.id]}},P=()=>({__state:{queries:[]},addQuery(e){this.__state.queries.push(e)}}),K=async(e,i,t)=>{const{localState:{transactionState:r},sharedState:{eventsEmitter:a,transactionsStates:l},sharedState:d}=e.__state;if(r.current)throw new Error("You are running atomic transaction inside of a transaction. Consider moving atomic transaction call to runAfterTransaction callback.");const s=await(async()=>{if(Array.isArray(t))return t;{const o=P();return await t(o),o.__state.queries}})(),c={id:g(),type:"atomic"},n={i:A++,current:c,performance:{prepareTime:0,execTime:0,freeTime:0,sendTime:0,receiveTime:0,totalTime:0,blockTime:0}};e={...e,__state:{...e.__state,localState:{...e.__state.localState,transactionState:{current:c}}}},l.byId[c.id]=n;const u=performance.now();try{await a.emit("transactionWillStart",e,c),await a.emit("transactionStarted",e,c),await y(e,[S.sql`BEGIN ${S.sql.raw(i.toUpperCase())} TRANSACTION`,...s,S.sql`COMMIT`],{transactionId:c.id,containsTransactionStart:!0,containsTransactionFinish:!0,containsTransactionRollback:!1,rollbackOnFail:!0}),await a.emit("transactionWillCommit",e,c),await a.emit("transactionCommitted",e,c)}catch(o){throw console.error("Rollback transaction",o),await a.emit("transactionWillRollback",e,c),await a.emit("transactionRollbacked",e,c),o}finally{n.performance.totalTime=performance.now()-u,R(e,c.id,n.performance),delete l.byId[c.id]}},Y=async({dbName:e,plugins:i,queriesMiddlewares:t,dbBackend:r})=>{const a=E("running",{label:"runningState"}),l=(await r)({dbName:e}),d={__state:{sharedState:{clientId:g(),dbBackend:l,dbName:e,runningState:a,eventsEmitter:U(),transactionsStates:{byId:{}}},localState:{queriesMiddlewares:t||[],transactionState:{}}},runInTransaction(n,u){return G(this,(u==null?void 0:u.type)||"deferred",n)},async runAtomicTransaction(n,u){return await K(this,(u==null?void 0:u.type)||"deferred",n)},async runQueries(n){return(await y(this,n)).result.map(({rows:o})=>o)},async runQuery(n){return(await this.runQueries([n]))[0]},runAfterTransactionCommitted(n){return q(this,(u,o,f)=>{u==="committed"&&n(o,f)})},runAfterTransactionRollbacked(n){q(d,(u,o,f)=>{u==="rollbacked"&&n(o,f)})}};let s=d;const c=()=>d.__state.sharedState.runningState.value;if(c()!=="running"||(await l.initialize(),c()!=="running"))return d;for(const n of i||[])s=n(s);return await d.__state.sharedState.eventsEmitter.emit("initialized",d),s},H=async e=>{e.__state.sharedState.runningState.value="stopping",await e.__state.sharedState.dbBackend.stop(),e.__state.sharedState.runningState.value="stopped",queueMicrotask(()=>{e.__state.sharedState.runningState.stop()})},J=e=>{const{current:i,queue:t}=e;return`current running job: ${JSON.stringify(i,null,2)}, queue: ${JSON.stringify(t,null,2)}`},X=()=>E({queue:[],current:void 0},{label:"jobsState"}),N=async(e,i)=>{const t=i!=null?i:g(),r={id:t},{current:a,queue:l}=e.value;if(a||l.length>0){const d=e.waitTill(s=>{var c;return((c=s.current)==null?void 0:c.id)===t});e.value={queue:[...l,r],current:a};try{await d}catch(s){throw s instanceof p?new p(`Timeout error while job acquire: '${s.message}'. Is it a dead lock? ${J(e.value)}, jobToAcquire: ${JSON.stringify(r,null,2)}`):s}}else e.value={queue:[],current:r};return r},I=(e,i)=>{const{current:t,queue:r}=e.value;if((t==null?void 0:t.id)!==i.id)throw new Error(`Can't release job that is not currently running, ${J(e.value)}, toRelease: ${JSON.stringify(i,null,2)}`);e.value={queue:r.slice(1),current:r[0]}},Z=async e=>{try{return e.waitTill(({queue:i,current:t})=>i.length===0&&t===void 0,{timeout:3e4})}catch(i){throw i instanceof p?new p(`Timeout error while awaiting all jobs done: '${i.message}'. Is it a dead lock?`):i}},O=(e,i)=>i({...e,__state:{...e.__state,localState:{...e.__state.localState,suppressLog:!0}}}),ee=e=>({...e,__state:{...e.__state,localState:{...e.__state.localState,suppressLog:!0}}}),te=async(e,i)=>{let t;return(!i||(i==null?void 0:i.containsTransactionStart))&&(t=await N(e,i==null?void 0:i.transactionId)),i&&!i.containsTransactionStart&&await e.waitTill(r=>{var a;return((a=r.current)==null?void 0:a.id)===i.transactionId}),t},ie=(e,i,t)=>{i&&(!t||(t==null?void 0:t.containsTransactionFinish)||(t==null?void 0:t.containsTransactionRollback))&&I(e,i),!i&&t&&((t==null?void 0:t.containsTransactionRollback)||(t==null?void 0:t.containsTransactionFinish))&&I(e,{id:t.transactionId})};m.StoppedError=$,m.TimeoutError=p,m.acquireJob=N,m.acquireWithTrJobOrWait=te,m.initDbClient=Y,m.initJobsState=X,m.makeId=g,m.reactiveVar=E,m.releaseJob=I,m.releaseTrJobIfPossible=ie,m.stopDb=H,m.suppressLog=O,m.whenAllJobsDone=Z,m.withSuppressedLog=ee;for(const e in S)e!=="default"&&!m.hasOwnProperty(e)&&Object.defineProperty(m,e,{enumerable:!0,get:()=>S[e]});Object.defineProperties(m,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
`))(),_=`%c[${e.__state.sharedState.dbName}] `+[(v=r.current)!=null&&v.id?`[tr_id=${(h=r.current)==null?void 0:h.id.substring(0,6)}]`:"",p,(c==null?void 0:c.sendTime)!==void 0?`sendTime=${(c.sendTime/1e3).toFixed(4)}`:"",(c==null?void 0:c.receiveTime)!==void 0?`receiveTime=${(c.receiveTime/1e3).toFixed(4)}`:"",(c==null?void 0:c.blockTime)!==void 0?`blockTime=${(c.blockTime/1e3).toFixed(4)}`:"",`totalTime=${((m-l)/1e3).toFixed(4)}`].filter(w=>w.length!==0).join(" "),B=(W=u.transactionsStates.byId[(M=(L=r.current)==null?void 0:L.id)!=null?M:""])==null?void 0:W.i,ae=typeof B=="number"?C[B%C.length]:"white";console.log(_,`color: ${ae}`)}const n=(Q=u.transactionsStates.byId[(V=(D=r.current)==null?void 0:D.id)!=null?V:""])==null?void 0:Q.performance;return n&&(s.some(T=>T.performance.execTime!==void 0)&&(n.execTime===void 0&&(n.execTime=0),n.execTime+=s.reduce((T,p)=>{var _;return T+((_=p.performance.execTime)!=null?_:0)},0)),s.some(T=>T.performance.freeTime!==void 0)&&(n.freeTime===void 0&&(n.freeTime=0),n.freeTime+=s.reduce((T,p)=>{var _;return T+((_=p.performance.freeTime)!=null?_:0)},0)),s.some(T=>T.performance.prepareTime!==void 0)&&(n.prepareTime===void 0&&(n.prepareTime=0),n.prepareTime+=s.reduce((T,p)=>{var _;return T+((_=p.performance.prepareTime)!=null?_:0)},0)),c.sendTime&&(n.sendTime||(n.sendTime=0),n.sendTime+=c.sendTime),c.receiveTime&&(n.receiveTime||(n.receiveTime=0),n.receiveTime+=c.receiveTime),c.blockTime&&(n.blockTime||(n.blockTime=0),n.blockTime+=c.blockTime)),{db:e,result:s,performance:c,queries:i}},k=async(e,i,t)=>{const r=[...e.__state.localState.queriesMiddlewares,z].reverse();let a=u=>Promise.resolve(u);for(const u of r){const o=a;a=l=>u({...l,next:o})}return await a({db:e,result:[],performance:{sendTime:void 0,receiveTime:void 0,totalTime:0},queries:i.map(u=>u.toSql()),transactionOpts:t})};let A=0;const R=(e,i,t)=>{if(e.__state.localState.suppressLog)return;const r=[t.prepareTime===void 0?"":`prepareTime=${(t.prepareTime/1e3).toFixed(4)}`,t.execTime===void 0?"":`execTime=${(t.execTime/1e3).toFixed(4)}`,t.freeTime===void 0?"":`freeTime=${(t.freeTime/1e3).toFixed(4)}`,t.sendTime===void 0?"":`sendTime=${(t.sendTime/1e3).toFixed(4)}`,t.receiveTime===void 0?"":`receiveTime=${(t.receiveTime/1e3).toFixed(4)}`,t.blockTime===void 0?"":`blockTime=${(t.blockTime/1e3).toFixed(4)}`,`totalTime=${(t.totalTime/1e3).toFixed(4)}`].filter(a=>a.length!==0).join(" ");console.log(`%c[${e.__state.sharedState.dbName}][tr_id=${i.slice(0,6)}] Transaction finished with ${r}`,"color: #fff; background-color: #1da1f2; padding: 2px 4px; border-radius: 2px")},G=async(e,i,t)=>{const{localState:{transactionState:r},sharedState:{eventsEmitter:a,transactionsStates:u}}=e.__state;if(r.current)return await t(e);x(e,()=>"transaction");const o={id:y(),type:"async"};e={...e,__state:{...e.__state,localState:{...e.__state.localState,transactionState:{current:o}}}};const l=performance.now(),s={i:A++,current:o,performance:{prepareTime:0,execTime:0,freeTime:0,sendTime:0,receiveTime:0,totalTime:0,blockTime:0}};u.byId[o.id]=s;try{await a.emit("transactionWillStart",e,o),await k(e,[S.sql`BEGIN ${S.sql.raw(i.toLocaleUpperCase())} TRANSACTION`],{transactionId:o.id,containsTransactionStart:!0,containsTransactionFinish:!1,containsTransactionRollback:!1,rollbackOnFail:!1}),await a.emit("transactionStarted",e,o);try{const c=await t(e);return await a.emit("transactionWillCommit",e,o),await k(e,[S.sql`COMMIT`],{transactionId:o.id,containsTransactionStart:!1,containsTransactionFinish:!0,containsTransactionRollback:!1,rollbackOnFail:!1}),await a.emit("transactionCommitted",e,o),c}catch(c){console.error("Rollback transaction",c),await a.emit("transactionWillRollback",e,o);try{await k(e,[S.sql`ROLLBACK`],{transactionId:o.id,containsTransactionStart:!1,containsTransactionFinish:!1,containsTransactionRollback:!0,rollbackOnFail:!1})}catch(m){console.warn("Rollback transaction failed",m)}throw await a.emit("transactionRollbacked",e,o),c}}finally{s.performance.totalTime=performance.now()-l,R(e,o.id,s.performance),delete u.byId[o.id]}},P=()=>({__state:{queries:[]},addQuery(e){this.__state.queries.push(e)}}),K=async(e,i,t)=>{const{localState:{transactionState:r},sharedState:{eventsEmitter:a,transactionsStates:u},sharedState:o}=e.__state;if(r.current)throw new Error("You are running atomic transaction inside of a transaction. Consider moving atomic transaction call to runAfterTransaction callback.");const l=await(async()=>{if(Array.isArray(t))return t;{const n=P();return await t(n),n.__state.queries}})(),s={id:y(),type:"atomic"},c={i:A++,current:s,performance:{prepareTime:0,execTime:0,freeTime:0,sendTime:0,receiveTime:0,totalTime:0,blockTime:0}};e={...e,__state:{...e.__state,localState:{...e.__state.localState,transactionState:{current:s}}}},u.byId[s.id]=c;const m=performance.now();try{await a.emit("transactionWillStart",e,s),await a.emit("transactionStarted",e,s),await k(e,[S.sql`BEGIN ${S.sql.raw(i.toUpperCase())} TRANSACTION`,...l,S.sql`COMMIT`],{transactionId:s.id,containsTransactionStart:!0,containsTransactionFinish:!0,containsTransactionRollback:!1,rollbackOnFail:!0}),await a.emit("transactionWillCommit",e,s),await a.emit("transactionCommitted",e,s)}catch(n){throw console.error("Rollback transaction",n),await a.emit("transactionWillRollback",e,s),await a.emit("transactionRollbacked",e,s),n}finally{c.performance.totalTime=performance.now()-m,R(e,s.id,c.performance),delete u.byId[s.id]}},Y=async({dbName:e,plugins:i,queriesMiddlewares:t,dbBackend:r,suppressLog:a})=>{const u=I("running",{label:"runningState"}),o=(await r)({dbName:e}),l={__state:{sharedState:{clientId:y(),dbBackend:o,dbName:e,runningState:u,eventsEmitter:U(),transactionsStates:{byId:{}}},localState:{queriesMiddlewares:t||[],transactionState:{},suppressLog:Boolean(a)}},runInTransaction(m,n){return G(this,(n==null?void 0:n.type)||"deferred",m)},async runInAtomicTransaction(m,n){return await K(this,(n==null?void 0:n.type)||"deferred",m)},async runQueries(m){return(await k(this,m)).result.map(({rows:f})=>f)},async runQuery(m){return(await this.runQueries([m]))[0]},runAfterTransactionCommitted(m){return q(this,(n,f,v)=>{n==="committed"&&m(f,v)})},runAfterTransactionRollbacked(m){q(l,(n,f,v)=>{n==="rollbacked"&&m(f,v)})}};let s=l;const c=()=>l.__state.sharedState.runningState.value;if(c()!=="running"||(await o.initialize(),c()!=="running"))return l;for(const m of i||[])s=m(s);return await l.__state.sharedState.eventsEmitter.emit("initialized",l),s},H=async e=>{e.__state.sharedState.runningState.value="stopping",await e.__state.sharedState.dbBackend.stop(),e.__state.sharedState.runningState.value="stopped",queueMicrotask(()=>{e.__state.sharedState.runningState.stop()})},J=e=>{const{current:i,queue:t}=e;return`current running job: ${JSON.stringify(i,null,2)}, queue: ${JSON.stringify(t,null,2)}`},X=()=>I({queue:[],current:void 0},{label:"jobsState"}),N=async(e,i)=>{const t=i!=null?i:y(),r={id:t},{current:a,queue:u}=e.value;if(a||u.length>0){const o=e.waitTill(l=>{var s;return((s=l.current)==null?void 0:s.id)===t});e.value={queue:[...u,r],current:a};try{await o}catch(l){throw l instanceof g?new g(`Timeout error while job acquire: '${l.message}'. Is it a dead lock? ${J(e.value)}, jobToAcquire: ${JSON.stringify(r,null,2)}`):l}}else e.value={queue:[],current:r};return r},E=(e,i)=>{const{current:t,queue:r}=e.value;if((t==null?void 0:t.id)!==i.id)throw new Error(`Can't release job that is not currently running, ${J(e.value)}, toRelease: ${JSON.stringify(i,null,2)}`);e.value={queue:r.slice(1),current:r[0]}},Z=async e=>{try{return e.waitTill(({queue:i,current:t})=>i.length===0&&t===void 0,{timeout:3e4})}catch(i){throw i instanceof g?new g(`Timeout error while awaiting all jobs done: '${i.message}'. Is it a dead lock?`):i}},O=(e,i)=>i({...e,__state:{...e.__state,localState:{...e.__state.localState,suppressLog:!0}}}),ee=e=>({...e,__state:{...e.__state,localState:{...e.__state.localState,suppressLog:!0}}}),te=async(e,i)=>{let t;return(!i||(i==null?void 0:i.containsTransactionStart))&&(t=await N(e,i==null?void 0:i.transactionId)),i&&!i.containsTransactionStart&&await e.waitTill(r=>{var a;return((a=r.current)==null?void 0:a.id)===i.transactionId}),t},ie=(e,i,t)=>{i&&(!t||(t==null?void 0:t.containsTransactionFinish)||(t==null?void 0:t.containsTransactionRollback))&&E(e,i),!i&&t&&((t==null?void 0:t.containsTransactionRollback)||(t==null?void 0:t.containsTransactionFinish))&&E(e,{id:t.transactionId})};d.StoppedError=$,d.TimeoutError=g,d.acquireJob=N,d.acquireWithTrJobOrWait=te,d.initDbClient=Y,d.initJobsState=X,d.makeId=y,d.reactiveVar=I,d.releaseJob=E,d.releaseTrJobIfPossible=ie,d.stopDb=H,d.suppressLog=O,d.whenAllJobsDone=Z,d.withSuppressedLog=ee;for(const e in S)e!=="default"&&!d.hasOwnProperty(e)&&Object.defineProperty(d,e,{enumerable:!0,get:()=>S[e]});Object.defineProperties(d,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
//# sourceMappingURL=index.umd.js.map

@@ -8,4 +8,5 @@ import { IDb, IDbBackend, IQueriesMiddleware } from "./types";

queriesMiddlewares?: IQueriesMiddleware[];
suppressLog?: boolean;
};
export declare const initDbClient: ({ dbName, plugins, queriesMiddlewares, dbBackend, }: IInitDbClientConfig) => Promise<IDb>;
export declare const initDbClient: ({ dbName, plugins, queriesMiddlewares, dbBackend, suppressLog, }: IInitDbClientConfig) => Promise<IDb>;
export declare const stopDb: (state: IDb) => Promise<void>;

@@ -59,3 +59,3 @@ import { ISqlAdapter } from "@kikko-land/boono-sql";

}): Promise<T>;
runAtomicTransaction(func: ((scope: IAtomicTransactionScope) => Promise<void> | void) | ISqlAdapter[], opts?: {
runInAtomicTransaction(func: ((scope: IAtomicTransactionScope) => Promise<void> | void) | ISqlAdapter[], opts?: {
type?: "deferred" | "immediate" | "exclusive";

@@ -62,0 +62,0 @@ }): Promise<void>;

{
"name": "@kikko-land/kikko",
"version": "0.8.0",
"version": "0.9.0",
"author": "Sergey Popov",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -7,2 +7,3 @@ import { ISqlAdapter } from "@kikko-land/boono-sql";

import { runQueries } from "./runQueries";
import { withSuppressedLog } from "./suppressLog";
import { execAtomicTransaction, runInTransactionFunc } from "./transaction";

@@ -26,2 +27,3 @@ import {

queriesMiddlewares?: IQueriesMiddleware[];
suppressLog?: boolean;
};

@@ -34,2 +36,3 @@

dbBackend,
suppressLog,
}: IInitDbClientConfig): Promise<IDb> => {

@@ -58,2 +61,3 @@ const runningState = reactiveVar<"running" | "stopping" | "stopped">(

transactionState: {},
suppressLog: Boolean(suppressLog),
},

@@ -67,3 +71,3 @@ },

},
async runAtomicTransaction(
async runInAtomicTransaction(
func:

@@ -70,0 +74,0 @@ | ((scope: IAtomicTransactionScope) => Promise<void> | void)

import { ISqlAdapter, sql } from "@kikko-land/boono-sql";
import { acquireJob, IJob, IJobsState, releaseJob } from "./job";
import { ReactiveVar } from "./reactiveVar";
import { runQueries } from "./runQueries";

@@ -10,3 +8,2 @@ import {

ITransaction,
ITransactionOpts,
ITransactionPerformance,

@@ -67,3 +64,2 @@ } from "./types";

sharedState: { eventsEmitter, transactionsStates },
sharedState,
} = db.__state;

@@ -70,0 +66,0 @@

@@ -93,3 +93,3 @@ import { ISqlAdapter } from "@kikko-land/boono-sql";

): Promise<T>;
runAtomicTransaction(
runInAtomicTransaction(
func:

@@ -96,0 +96,0 @@ | ((scope: IAtomicTransactionScope) => Promise<void> | void)

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc