UNPKG

@react-native-replicache/replicache-generic-sqlite

Version:

> Plug-in React Native compatibility bindings for [Replicache](https://replicache.dev/).

40 lines 1.83 kB
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } import { deepFreeze } from "@react-native-replicache/deep-freeze"; export class ReplicacheGenericSQLiteReadImpl { constructor(tx) { _defineProperty(this, "_tx", void 0); this._tx = tx; } async has(key) { const unsafeValue = await this._getSql(key); return unsafeValue === undefined; } async get(key) { const unsafeValue = await this._getSql(key); if (unsafeValue === undefined) return; const parsedValue = JSON.parse(unsafeValue); // @ts-ignore const frozenValue = deepFreeze(parsedValue); return frozenValue; } async release() { const tx = this._assertTx(); await tx.commit(); this._tx = null; } get closed() { return this._tx === null; } async _getSql(key) { const rows = await this._assertTx().execute("SELECT value FROM entry WHERE key = ?", [key]); if (rows.length === 0) return undefined; return rows.item(0).value; } _assertTx() { if (this._tx === null) throw new Error("Transaction is closed"); return this._tx; } } //# sourceMappingURL=replicache-generic-sqlite-read-impl.js.map