UNPKG

@berish/orm-web-db-adapter

Version:

Адаптер базы данных по сети для @berish/orm

72 lines 2.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebDBReceiver = void 0; const emitter_1 = require("@berish/emitter"); const orm_1 = require("@berish/orm"); class WebDBReceiver { constructor(manager) { this._manager = null; this._cacheEmitter = new emitter_1.CacheEmitter(); this._subscribeEventHashes = []; this.count = (...[query]) => { return this.manager.db.count(query); }; this.get = (...[query]) => { return this.manager.db.get(query); }; this.create = (...[table, items]) => { return this.manager.db.create(table, items); }; this.update = (...[table, items]) => { return this.manager.db.update(table, items); }; this.delete = (...[queryData]) => { return this.manager.db.delete(queryData); }; this.index = (...[table, indexName, keys]) => { return this.manager.db.index(table, indexName, keys); }; this.find = (...[queryData]) => { return this.manager.db.find(queryData); }; this.subscribe = (...[queryData, callback]) => { const queryHash = orm_1.Query.getHash(queryData); const eventHash = this._cacheEmitter.subscribe(`subscribe_${queryHash}`, (callback) => { const unsubscribe = this.manager.db.subscribe(queryData, (oldValue, newValue) => callback({ oldValue, newValue })); return () => { unsubscribe(); }; }, ({ oldValue, newValue }) => callback(oldValue, newValue)); this._subscribeEventHashes.push(eventHash); return () => { this._subscribeEventHashes = this._subscribeEventHashes.filter((m) => m !== eventHash); this._cacheEmitter.unsubscribe(eventHash); }; }; this.close = () => { // this._subscriptionEmitter.offAll(); for (const hash of this._subscribeEventHashes) { this._cacheEmitter.unsubscribe(hash); } this._subscribeEventHashes = []; }; this._manager = manager; } get manager() { return this._manager; } get API() { return { get: this.get, create: this.create, update: this.update, delete: this.delete, count: this.count, find: this.find, index: this.index, subscribe: this.subscribe, }; } } exports.WebDBReceiver = WebDBReceiver; //# sourceMappingURL=webDBReceiver.js.map