@react-native-replicache/replicache-generic-sqlite
Version:
> Plug-in React Native compatibility bindings for [Replicache](https://replicache.dev/).
50 lines • 2.21 kB
JavaScript
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); }
export class ReplicacheGenericSQLiteDatabaseManager {
constructor(_dbm) {
this._dbm = _dbm;
_defineProperty(this, "_dbInstances", new Map());
}
async open(name) {
const dbInstance = this._dbInstances.get(name);
if ((dbInstance === null || dbInstance === void 0 ? void 0 : dbInstance.state) === "open") return dbInstance.db;
const newDb = await this._dbm.open(`replicache-${name}.sqlite`);
if (!dbInstance) {
await this._setupSchema(newDb);
this._dbInstances.set(name, {
state: "open",
db: newDb
});
} else {
dbInstance.state = "open";
}
return newDb;
}
async close(name) {
const dbInstance = this._dbInstances.get(name);
if (!dbInstance) return;
await dbInstance.db.close();
dbInstance.state = "closed";
}
async truncate(name) {
const db = await this.open(name);
const tx = db.transaction();
await tx.start(false);
await tx.execute("DELETE FROM entry", []);
await tx.commit();
}
async destroy(name) {
const dbInstances = this._dbInstances.get(name);
if (!dbInstances) return;
await dbInstances.db.destroy();
this._dbInstances.delete(name);
}
async _setupSchema(db) {
const tx = db.transaction();
await tx.start(false);
await tx.execute("CREATE TABLE IF NOT EXISTS entry (key TEXT PRIMARY KEY, value TEXT)", []);
await tx.commit();
}
}
//# sourceMappingURL=replicache-generic-sqlite-database-manager.js.map