@react-native-replicache/replicache-generic-sqlite
Version:
> Plug-in React Native compatibility bindings for [Replicache](https://replicache.dev/).
47 lines (46 loc) • 2.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ReplicacheGenericSQLiteReadImpl = void 0;
var _deepFreeze = require("@react-native-replicache/deep-freeze");
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); }
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 = (0, _deepFreeze.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;
}
}
exports.ReplicacheGenericSQLiteReadImpl = ReplicacheGenericSQLiteReadImpl;
//# sourceMappingURL=replicache-generic-sqlite-read-impl.js.map