UNPKG

@wonderlandlabs/rxjs-sqlite

Version:

A reactive adapter for SQLite using RxJS, with RxDB adapter support

1,025 lines (1,024 loc) 28.7 kB
var or = Object.defineProperty; var ir = (t, e, r) => e in t ? or(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r; var S = (t, e, r) => ir(t, typeof e != "symbol" ? e + "" : e, r); import sr from "better-sqlite3"; import { Subject as ur, of as cr } from "rxjs"; function d(t) { return typeof t == "function"; } function ar(t) { return d(t == null ? void 0 : t.lift); } function Z(t) { return function(e) { if (ar(e)) return e.lift(function(r) { try { return t(r, this); } catch (n) { this.error(n); } }); throw new TypeError("Unable to lift unknown Observable type"); }; } var $ = function(t, e) { return $ = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(r, n) { r.__proto__ = n; } || function(r, n) { for (var i in n) Object.prototype.hasOwnProperty.call(n, i) && (r[i] = n[i]); }, $(t, e); }; function E(t, e) { if (typeof e != "function" && e !== null) throw new TypeError("Class extends value " + String(e) + " is not a constructor or null"); $(t, e); function r() { this.constructor = t; } t.prototype = e === null ? Object.create(e) : (r.prototype = e.prototype, new r()); } function fr(t, e, r, n) { function i(o) { return o instanceof r ? o : new r(function(s) { s(o); }); } return new (r || (r = Promise))(function(o, s) { function c(f) { try { u(n.next(f)); } catch (p) { s(p); } } function a(f) { try { u(n.throw(f)); } catch (p) { s(p); } } function u(f) { f.done ? o(f.value) : i(f.value).then(c, a); } u((n = n.apply(t, e || [])).next()); }); } function H(t, e) { var r = { label: 0, sent: function() { if (o[0] & 1) throw o[1]; return o[1]; }, trys: [], ops: [] }, n, i, o, s = Object.create((typeof Iterator == "function" ? Iterator : Object).prototype); return s.next = c(0), s.throw = c(1), s.return = c(2), typeof Symbol == "function" && (s[Symbol.iterator] = function() { return this; }), s; function c(u) { return function(f) { return a([u, f]); }; } function a(u) { if (n) throw new TypeError("Generator is already executing."); for (; s && (s = 0, u[0] && (r = 0)), r; ) try { if (n = 1, i && (o = u[0] & 2 ? i.return : u[0] ? i.throw || ((o = i.return) && o.call(i), 0) : i.next) && !(o = o.call(i, u[1])).done) return o; switch (i = 0, o && (u = [u[0] & 2, o.value]), u[0]) { case 0: case 1: o = u; break; case 4: return r.label++, { value: u[1], done: !1 }; case 5: r.label++, i = u[1], u = [0]; continue; case 7: u = r.ops.pop(), r.trys.pop(); continue; default: if (o = r.trys, !(o = o.length > 0 && o[o.length - 1]) && (u[0] === 6 || u[0] === 2)) { r = 0; continue; } if (u[0] === 3 && (!o || u[1] > o[0] && u[1] < o[3])) { r.label = u[1]; break; } if (u[0] === 6 && r.label < o[1]) { r.label = o[1], o = u; break; } if (o && r.label < o[2]) { r.label = o[2], r.ops.push(u); break; } o[2] && r.ops.pop(), r.trys.pop(); continue; } u = e.call(t, r); } catch (f) { u = [6, f], i = 0; } finally { n = o = 0; } if (u[0] & 5) throw u[1]; return { value: u[0] ? u[1] : void 0, done: !0 }; } } function _(t) { var e = typeof Symbol == "function" && Symbol.iterator, r = e && t[e], n = 0; if (r) return r.call(t); if (t && typeof t.length == "number") return { next: function() { return t && n >= t.length && (t = void 0), { value: t && t[n++], done: !t }; } }; throw new TypeError(e ? "Object is not iterable." : "Symbol.iterator is not defined."); } function P(t, e) { var r = typeof Symbol == "function" && t[Symbol.iterator]; if (!r) return t; var n = r.call(t), i, o = [], s; try { for (; (e === void 0 || e-- > 0) && !(i = n.next()).done; ) o.push(i.value); } catch (c) { s = { error: c }; } finally { try { i && !i.done && (r = n.return) && r.call(n); } finally { if (s) throw s.error; } } return o; } function R(t, e, r) { if (r || arguments.length === 2) for (var n = 0, i = e.length, o; n < i; n++) (o || !(n in e)) && (o || (o = Array.prototype.slice.call(e, 0, n)), o[n] = e[n]); return t.concat(o || Array.prototype.slice.call(e)); } function w(t) { return this instanceof w ? (this.v = t, this) : new w(t); } function lr(t, e, r) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var n = r.apply(t, e || []), i, o = []; return i = Object.create((typeof AsyncIterator == "function" ? AsyncIterator : Object).prototype), c("next"), c("throw"), c("return", s), i[Symbol.asyncIterator] = function() { return this; }, i; function s(l) { return function(v) { return Promise.resolve(v).then(l, p); }; } function c(l, v) { n[l] && (i[l] = function(h) { return new Promise(function(m, O) { o.push([l, h, m, O]) > 1 || a(l, h); }); }, v && (i[l] = v(i[l]))); } function a(l, v) { try { u(n[l](v)); } catch (h) { b(o[0][3], h); } } function u(l) { l.value instanceof w ? Promise.resolve(l.value.v).then(f, p) : b(o[0][2], l); } function f(l) { a("next", l); } function p(l) { a("throw", l); } function b(l, v) { l(v), o.shift(), o.length && a(o[0][0], o[0][1]); } } function hr(t) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var e = t[Symbol.asyncIterator], r; return e ? e.call(t) : (t = typeof _ == "function" ? _(t) : t[Symbol.iterator](), r = {}, n("next"), n("throw"), n("return"), r[Symbol.asyncIterator] = function() { return this; }, r); function n(o) { r[o] = t[o] && function(s) { return new Promise(function(c, a) { s = t[o](s), i(c, a, s.done, s.value); }); }; } function i(o, s, c, a) { Promise.resolve(a).then(function(u) { o({ value: u, done: c }); }, s); } } var pr = function(t) { return t && typeof t.length == "number" && typeof t != "function"; }; function dr(t) { return d(t == null ? void 0 : t.then); } function K(t) { var e = function(n) { Error.call(n), n.stack = new Error().stack; }, r = t(e); return r.prototype = Object.create(Error.prototype), r.prototype.constructor = r, r; } var U = K(function(t) { return function(r) { t(this), this.message = r ? r.length + ` errors occurred during unsubscription: ` + r.map(function(n, i) { return i + 1 + ") " + n.toString(); }).join(` `) : "", this.name = "UnsubscriptionError", this.errors = r; }; }); function L(t, e) { if (t) { var r = t.indexOf(e); 0 <= r && t.splice(r, 1); } } var C = function() { function t(e) { this.initialTeardown = e, this.closed = !1, this._parentage = null, this._finalizers = null; } return t.prototype.unsubscribe = function() { var e, r, n, i, o; if (!this.closed) { this.closed = !0; var s = this._parentage; if (s) if (this._parentage = null, Array.isArray(s)) try { for (var c = _(s), a = c.next(); !a.done; a = c.next()) { var u = a.value; u.remove(this); } } catch (h) { e = { error: h }; } finally { try { a && !a.done && (r = c.return) && r.call(c); } finally { if (e) throw e.error; } } else s.remove(this); var f = this.initialTeardown; if (d(f)) try { f(); } catch (h) { o = h instanceof U ? h.errors : [h]; } var p = this._finalizers; if (p) { this._finalizers = null; try { for (var b = _(p), l = b.next(); !l.done; l = b.next()) { var v = l.value; try { D(v); } catch (h) { o = o ?? [], h instanceof U ? o = R(R([], P(o)), P(h.errors)) : o.push(h); } } } catch (h) { n = { error: h }; } finally { try { l && !l.done && (i = b.return) && i.call(b); } finally { if (n) throw n.error; } } } if (o) throw new U(o); } }, t.prototype.add = function(e) { var r; if (e && e !== this) if (this.closed) D(e); else { if (e instanceof t) { if (e.closed || e._hasParent(this)) return; e._addParent(this); } (this._finalizers = (r = this._finalizers) !== null && r !== void 0 ? r : []).push(e); } }, t.prototype._hasParent = function(e) { var r = this._parentage; return r === e || Array.isArray(r) && r.includes(e); }, t.prototype._addParent = function(e) { var r = this._parentage; this._parentage = Array.isArray(r) ? (r.push(e), r) : r ? [r, e] : e; }, t.prototype._removeParent = function(e) { var r = this._parentage; r === e ? this._parentage = null : Array.isArray(r) && L(r, e); }, t.prototype.remove = function(e) { var r = this._finalizers; r && L(r, e), e instanceof t && e._removeParent(this); }, t.EMPTY = function() { var e = new t(); return e.closed = !0, e; }(), t; }(), X = C.EMPTY; function J(t) { return t instanceof C || t && "closed" in t && d(t.remove) && d(t.add) && d(t.unsubscribe); } function D(t) { d(t) ? t() : t.unsubscribe(); } var vr = { Promise: void 0 }, br = { setTimeout: function(t, e) { for (var r = [], n = 2; n < arguments.length; n++) r[n - 2] = arguments[n]; return setTimeout.apply(void 0, R([t, e], P(r))); }, clearTimeout: function(t) { return clearTimeout(t); }, delegate: void 0 }; function N(t) { br.setTimeout(function() { throw t; }); } function z() { } function A(t) { t(); } var F = function(t) { E(e, t); function e(r) { var n = t.call(this) || this; return n.isStopped = !1, r ? (n.destination = r, J(r) && r.add(n)) : n.destination = Sr, n; } return e.create = function(r, n, i) { return new x(r, n, i); }, e.prototype.next = function(r) { this.isStopped || this._next(r); }, e.prototype.error = function(r) { this.isStopped || (this.isStopped = !0, this._error(r)); }, e.prototype.complete = function() { this.isStopped || (this.isStopped = !0, this._complete()); }, e.prototype.unsubscribe = function() { this.closed || (this.isStopped = !0, t.prototype.unsubscribe.call(this), this.destination = null); }, e.prototype._next = function(r) { this.destination.next(r); }, e.prototype._error = function(r) { try { this.destination.error(r); } finally { this.unsubscribe(); } }, e.prototype._complete = function() { try { this.destination.complete(); } finally { this.unsubscribe(); } }, e; }(C), yr = function() { function t(e) { this.partialObserver = e; } return t.prototype.next = function(e) { var r = this.partialObserver; if (r.next) try { r.next(e); } catch (n) { j(n); } }, t.prototype.error = function(e) { var r = this.partialObserver; if (r.error) try { r.error(e); } catch (n) { j(n); } else j(e); }, t.prototype.complete = function() { var e = this.partialObserver; if (e.complete) try { e.complete(); } catch (r) { j(r); } }, t; }(), x = function(t) { E(e, t); function e(r, n, i) { var o = t.call(this) || this, s; return d(r) || !r ? s = { next: r ?? void 0, error: n ?? void 0, complete: i ?? void 0 } : s = r, o.destination = new yr(s), o; } return e; }(F); function j(t) { N(t); } function mr(t) { throw t; } var Sr = { closed: !0, next: z, error: mr, complete: z }, B = function() { return typeof Symbol == "function" && Symbol.observable || "@@observable"; }(); function wr(t) { return t; } function _r(t) { return t.length === 0 ? wr : t.length === 1 ? t[0] : function(r) { return t.reduce(function(n, i) { return i(n); }, r); }; } var y = function() { function t(e) { e && (this._subscribe = e); } return t.prototype.lift = function(e) { var r = new t(); return r.source = this, r.operator = e, r; }, t.prototype.subscribe = function(e, r, n) { var i = this, o = Or(e) ? e : new x(e, r, n); return A(function() { var s = i, c = s.operator, a = s.source; o.add(c ? c.call(o, a) : a ? i._subscribe(o) : i._trySubscribe(o)); }), o; }, t.prototype._trySubscribe = function(e) { try { return this._subscribe(e); } catch (r) { e.error(r); } }, t.prototype.forEach = function(e, r) { var n = this; return r = G(r), new r(function(i, o) { var s = new x({ next: function(c) { try { e(c); } catch (a) { o(a), s.unsubscribe(); } }, error: o, complete: i }); n.subscribe(s); }); }, t.prototype._subscribe = function(e) { var r; return (r = this.source) === null || r === void 0 ? void 0 : r.subscribe(e); }, t.prototype[B] = function() { return this; }, t.prototype.pipe = function() { for (var e = [], r = 0; r < arguments.length; r++) e[r] = arguments[r]; return _r(e)(this); }, t.prototype.toPromise = function(e) { var r = this; return e = G(e), new e(function(n, i) { var o; r.subscribe(function(s) { return o = s; }, function(s) { return i(s); }, function() { return n(o); }); }); }, t.create = function(e) { return new t(e); }, t; }(); function G(t) { var e; return (e = t ?? vr.Promise) !== null && e !== void 0 ? e : Promise; } function Er(t) { return t && d(t.next) && d(t.error) && d(t.complete); } function Or(t) { return t && t instanceof F || Er(t) && J(t); } function gr(t) { return d(t[B]); } function xr(t) { return Symbol.asyncIterator && d(t == null ? void 0 : t[Symbol.asyncIterator]); } function Ir(t) { return new TypeError("You provided " + (t !== null && typeof t == "object" ? "an invalid object" : "'" + t + "'") + " where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable."); } function Tr() { return typeof Symbol != "function" || !Symbol.iterator ? "@@iterator" : Symbol.iterator; } var jr = Tr(); function Ar(t) { return d(t == null ? void 0 : t[jr]); } function Pr(t) { return lr(this, arguments, function() { var r, n, i, o; return H(this, function(s) { switch (s.label) { case 0: r = t.getReader(), s.label = 1; case 1: s.trys.push([1, , 9, 10]), s.label = 2; case 2: return [4, w(r.read())]; case 3: return n = s.sent(), i = n.value, o = n.done, o ? [4, w(void 0)] : [3, 5]; case 4: return [2, s.sent()]; case 5: return [4, w(i)]; case 6: return [4, s.sent()]; case 7: return s.sent(), [3, 2]; case 8: return [3, 10]; case 9: return r.releaseLock(), [7]; case 10: return [2]; } }); }); } function Rr(t) { return d(t == null ? void 0 : t.getReader); } function rr(t) { if (t instanceof y) return t; if (t != null) { if (gr(t)) return Cr(t); if (pr(t)) return Ur(t); if (dr(t)) return kr(t); if (xr(t)) return tr(t); if (Ar(t)) return $r(t); if (Rr(t)) return Lr(t); } throw Ir(t); } function Cr(t) { return new y(function(e) { var r = t[B](); if (d(r.subscribe)) return r.subscribe(e); throw new TypeError("Provided object does not correctly implement Symbol.observable"); }); } function Ur(t) { return new y(function(e) { for (var r = 0; r < t.length && !e.closed; r++) e.next(t[r]); e.complete(); }); } function kr(t) { return new y(function(e) { t.then(function(r) { e.closed || (e.next(r), e.complete()); }, function(r) { return e.error(r); }).then(null, N); }); } function $r(t) { return new y(function(e) { var r, n; try { for (var i = _(t), o = i.next(); !o.done; o = i.next()) { var s = o.value; if (e.next(s), e.closed) return; } } catch (c) { r = { error: c }; } finally { try { o && !o.done && (n = i.return) && n.call(i); } finally { if (r) throw r.error; } } e.complete(); }); } function tr(t) { return new y(function(e) { Wr(t, e).catch(function(r) { return e.error(r); }); }); } function Lr(t) { return tr(Pr(t)); } function Wr(t, e) { var r, n, i, o; return fr(this, void 0, void 0, function() { var s, c; return H(this, function(a) { switch (a.label) { case 0: a.trys.push([0, 5, 6, 11]), r = hr(t), a.label = 1; case 1: return [4, r.next()]; case 2: if (n = a.sent(), !!n.done) return [3, 4]; if (s = n.value, e.next(s), e.closed) return [2]; a.label = 3; case 3: return [3, 1]; case 4: return [3, 11]; case 5: return c = a.sent(), i = { error: c }, [3, 11]; case 6: return a.trys.push([6, , 9, 10]), n && !n.done && (o = r.return) ? [4, o.call(r)] : [3, 8]; case 7: a.sent(), a.label = 8; case 8: return [3, 10]; case 9: if (i) throw i.error; return [7]; case 10: return [7]; case 11: return e.complete(), [2]; } }); }); } function Fr(t, e, r, n, i) { return new Br(t, e, r, n, i); } var Br = function(t) { E(e, t); function e(r, n, i, o, s, c) { var a = t.call(this, r) || this; return a.onFinalize = s, a.shouldUnsubscribe = c, a._next = n ? function(u) { try { n(u); } catch (f) { r.error(f); } } : t.prototype._next, a._error = o ? function(u) { try { o(u); } catch (f) { r.error(f); } finally { this.unsubscribe(); } } : t.prototype._error, a._complete = i ? function() { try { i(); } catch (u) { r.error(u); } finally { this.unsubscribe(); } } : t.prototype._complete, a; } return e.prototype.unsubscribe = function() { var r; if (!this.shouldUnsubscribe || this.shouldUnsubscribe()) { var n = this.closed; t.prototype.unsubscribe.call(this), !n && ((r = this.onFinalize) === null || r === void 0 || r.call(this)); } }, e; }(F), er = { now: function() { return (er.delegate || Date).now(); }, delegate: void 0 }; function Q(t, e) { return Z(function(r, n) { var i = 0; r.subscribe(Fr(n, function(o) { n.next(t.call(e, o, i++)); })); }); } var Yr = K(function(t) { return function() { t(this), this.name = "ObjectUnsubscribedError", this.message = "object unsubscribed"; }; }), Y = function(t) { E(e, t); function e() { var r = t.call(this) || this; return r.closed = !1, r.currentObservers = null, r.observers = [], r.isStopped = !1, r.hasError = !1, r.thrownError = null, r; } return e.prototype.lift = function(r) { var n = new q(this, this); return n.operator = r, n; }, e.prototype._throwIfClosed = function() { if (this.closed) throw new Yr(); }, e.prototype.next = function(r) { var n = this; A(function() { var i, o; if (n._throwIfClosed(), !n.isStopped) { n.currentObservers || (n.currentObservers = Array.from(n.observers)); try { for (var s = _(n.currentObservers), c = s.next(); !c.done; c = s.next()) { var a = c.value; a.next(r); } } catch (u) { i = { error: u }; } finally { try { c && !c.done && (o = s.return) && o.call(s); } finally { if (i) throw i.error; } } } }); }, e.prototype.error = function(r) { var n = this; A(function() { if (n._throwIfClosed(), !n.isStopped) { n.hasError = n.isStopped = !0, n.thrownError = r; for (var i = n.observers; i.length; ) i.shift().error(r); } }); }, e.prototype.complete = function() { var r = this; A(function() { if (r._throwIfClosed(), !r.isStopped) { r.isStopped = !0; for (var n = r.observers; n.length; ) n.shift().complete(); } }); }, e.prototype.unsubscribe = function() { this.isStopped = this.closed = !0, this.observers = this.currentObservers = null; }, Object.defineProperty(e.prototype, "observed", { get: function() { var r; return ((r = this.observers) === null || r === void 0 ? void 0 : r.length) > 0; }, enumerable: !1, configurable: !0 }), e.prototype._trySubscribe = function(r) { return this._throwIfClosed(), t.prototype._trySubscribe.call(this, r); }, e.prototype._subscribe = function(r) { return this._throwIfClosed(), this._checkFinalizedStatuses(r), this._innerSubscribe(r); }, e.prototype._innerSubscribe = function(r) { var n = this, i = this, o = i.hasError, s = i.isStopped, c = i.observers; return o || s ? X : (this.currentObservers = null, c.push(r), new C(function() { n.currentObservers = null, L(c, r); })); }, e.prototype._checkFinalizedStatuses = function(r) { var n = this, i = n.hasError, o = n.thrownError, s = n.isStopped; i ? r.error(o) : s && r.complete(); }, e.prototype.asObservable = function() { var r = new y(); return r.source = this, r; }, e.create = function(r, n) { return new q(r, n); }, e; }(y), q = function(t) { E(e, t); function e(r, n) { var i = t.call(this) || this; return i.destination = r, i.source = n, i; } return e.prototype.next = function(r) { var n, i; (i = (n = this.destination) === null || n === void 0 ? void 0 : n.next) === null || i === void 0 || i.call(n, r); }, e.prototype.error = function(r) { var n, i; (i = (n = this.destination) === null || n === void 0 ? void 0 : n.error) === null || i === void 0 || i.call(n, r); }, e.prototype.complete = function() { var r, n; (n = (r = this.destination) === null || r === void 0 ? void 0 : r.complete) === null || n === void 0 || n.call(r); }, e.prototype._subscribe = function(r) { var n, i; return (i = (n = this.source) === null || n === void 0 ? void 0 : n.subscribe(r)) !== null && i !== void 0 ? i : X; }, e; }(Y), Mr = function(t) { E(e, t); function e(r, n, i) { r === void 0 && (r = 1 / 0), n === void 0 && (n = 1 / 0), i === void 0 && (i = er); var o = t.call(this) || this; return o._bufferSize = r, o._windowTime = n, o._timestampProvider = i, o._buffer = [], o._infiniteTimeWindow = !0, o._infiniteTimeWindow = n === 1 / 0, o._bufferSize = Math.max(1, r), o._windowTime = Math.max(1, n), o; } return e.prototype.next = function(r) { var n = this, i = n.isStopped, o = n._buffer, s = n._infiniteTimeWindow, c = n._timestampProvider, a = n._windowTime; i || (o.push(r), !s && o.push(c.now() + a)), this._trimBuffer(), t.prototype.next.call(this, r); }, e.prototype._subscribe = function(r) { this._throwIfClosed(), this._trimBuffer(); for (var n = this._innerSubscribe(r), i = this, o = i._infiniteTimeWindow, s = i._buffer, c = s.slice(), a = 0; a < c.length && !r.closed; a += o ? 1 : 2) r.next(c[a]); return this._checkFinalizedStatuses(r), n; }, e.prototype._trimBuffer = function() { var r = this, n = r._bufferSize, i = r._timestampProvider, o = r._buffer, s = r._infiniteTimeWindow, c = (s ? 1 : 2) * n; if (n < 1 / 0 && c < o.length && o.splice(0, o.length - c), !s) { for (var a = i.now(), u = 0, f = 1; f < o.length && o[f] <= a; f += 2) u = f; u && o.splice(0, u + 1); } }, e; }(Y); function Dr(t) { t === void 0 && (t = {}); var e = t.connector, r = e === void 0 ? function() { return new Y(); } : e, n = t.resetOnError, i = n === void 0 ? !0 : n, o = t.resetOnComplete, s = o === void 0 ? !0 : o, c = t.resetOnRefCountZero, a = c === void 0 ? !0 : c; return function(u) { var f, p, b, l = 0, v = !1, h = !1, m = function() { p == null || p.unsubscribe(), p = void 0; }, O = function() { m(), f = b = void 0, v = h = !1; }, nr = function() { var g = f; O(), g == null || g.unsubscribe(); }; return Z(function(g, M) { l++, !h && !v && m(); var I = b = b ?? r(); M.add(function() { l--, l === 0 && !h && !v && (p = k(nr, a)); }), I.subscribe(M), !f && l > 0 && (f = new x({ next: function(T) { return I.next(T); }, error: function(T) { h = !0, m(), p = k(O, i, T), I.error(T); }, complete: function() { v = !0, m(), p = k(O, s), I.complete(); } }), rr(g).subscribe(f)); })(u); }; } function k(t, e) { for (var r = [], n = 2; n < arguments.length; n++) r[n - 2] = arguments[n]; if (e === !0) { t(); return; } if (e !== !1) { var i = new x({ next: function() { i.unsubscribe(), t(); } }); return rr(e.apply(void 0, R([], P(r)))).subscribe(i); } } function V(t, e, r) { var n, i = !1; return n = t, Dr({ connector: function() { return new Mr(n, e, r); }, resetOnError: !0, resetOnComplete: !1, resetOnRefCountZero: i }); } function zr(t) { const e = {}; for (const r in t) Object.prototype.hasOwnProperty.call(t, r) && (e[r] = t[r]); return e; } function W(t) { return t.map((e) => zr(e)); } function Hr(t, e, r) { const n = Object.entries(e).map(([i, o]) => i === r ? `${i} ${o} PRIMARY KEY` : `${i} ${o}`).join(", "); return `CREATE TABLE IF NOT EXISTS ${t} (${n})`; } function Kr(t, e) { const r = e.map(() => "?").join(", "); return `INSERT INTO ${t} (${e.join(", ")}) VALUES (${r})`; } function Xr(t, e, r) { const n = e.map((o) => `${o} = ?`).join(", "), i = r.map((o) => `${o} = ?`).join(" AND "); return `UPDATE ${t} SET ${n} WHERE ${i}`; } class Gr { constructor(e) { S(this, "transaction"); S(this, "statements", []); this.db = e, this.transaction = e.transaction((r) => { for (const n of r) this.db.prepare(n.sql).run(n.params); }); } execute(e, r = []) { this.statements.push({ sql: e, params: r }); } query(e, r = []) { const i = this.db.prepare(e).all(r); return W(i); } commit() { this.transaction(this.statements), this.statements = []; } rollback() { this.statements = []; } } class Qr { constructor(e, r) { S(this, "db"); S(this, "changeSubject", new ur()); S(this, "change$", this.changeSubject.asObservable().pipe(V(1))); this.db = new sr(e, r), this.db.function("notify_change", () => (this.changeSubject.next(), !0)); } execute(e, r = {}) { this.db.prepare(e).run(r.params || []), (e.trim().toUpperCase().startsWith("INSERT") || e.trim().toUpperCase().startsWith("UPDATE") || e.trim().toUpperCase().startsWith("DELETE")) && this.changeSubject.next(); } query(e, r = {}) { const i = this.db.prepare(e).all(r.params || []); return cr(W(i)); } queryOne(e, r = {}) { return this.query(e, r).pipe( Q((n) => n[0]) ); } transaction() { return new Gr(this.db); } reactiveQuery(e, r = {}) { return this.change$.pipe( // Map to query results whenever a change is detected Q(() => { const i = this.db.prepare(e).all(r.params || []); return W(i); }), // Share the latest result with new subscribers V(1) ); } close() { this.db.close(); } } function Jr(t, e) { return e && console.log("adapter options", e), new Qr(t, e); } export { Kr as createInsertStatement, Jr as createSQLiteAdapter, Hr as createTableSchema, Xr as createUpdateStatement, zr as rowToObject, W as rowsToObjects };