UNPKG

@typeheim/orm-on-fire

Version:
89 lines 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DocReference = void 0; const fire_rx_1 = require("@typeheim/fire-rx"); const singletons_1 = require("../singletons"); class DocReference { constructor(connection, docPath, collectionPath) { this.connection = connection; this.docPath = docPath; this.collectionPath = collectionPath; } static fromNativeRef(docRef) { let doc = new DocReference(singletons_1.OrmOnFire); doc._nativeRef = docRef; return doc; } get() { let subject = new fire_rx_1.StatefulSubject(); this.connection.isInitialized.then((isInitialized) => { if (isInitialized) { this.nativeRef.get().then((snapshot) => { subject.next(snapshot); subject.complete(); }).catch(error => subject.error(error)); } }).catch(error => subject.error(error)); return subject; } set(data) { let promise = new fire_rx_1.ReactivePromise(); this.connection.isInitialized.then((isInitialized) => { if (isInitialized) { this.nativeRef.set(data) .then(() => promise.resolve(true)) .catch(() => promise.resolve(false)); } }).catch(error => promise.reject(error)); return promise; } update(data) { let promise = new fire_rx_1.ReactivePromise(); this.connection.isInitialized.then((isInitialized) => { if (isInitialized) { this.nativeRef.update(data) .then(() => promise.resolve(true)) .catch(() => promise.resolve(false)); } }).catch(error => promise.reject(error)); return promise; } delete() { let promise = new fire_rx_1.ReactivePromise(); this.connection.isInitialized.then((isInitialized) => { if (isInitialized) { this.nativeRef.delete() .then(() => promise.resolve(true)) .catch(() => promise.resolve(false)); } }).catch(error => promise.reject(error)); return promise; } snapshot() { let subject = new fire_rx_1.StatefulSubject(); this.connection.isInitialized.then((isInitialized) => { if (isInitialized) { let unsubscribeSnapshot = this.nativeRef.onSnapshot((snapshot) => { subject.next(snapshot); }); subject.subscribe({ complete: () => unsubscribeSnapshot(), }); } }).catch(error => subject.error(error)); return subject; } get nativeRef() { if (!this._nativeRef) { let baseRef = this.collectionPath ? this.connection.driver.collection(this.collectionPath) : this.connection.driver; // @ts-ignore this._nativeRef = this.docPath ? baseRef.doc(this.docPath) : baseRef.doc(); } return this._nativeRef; } get path() { return this.collectionPath ? `${this.collectionPath}/${this.docPath}` : this.docPath; } } exports.DocReference = DocReference; //# sourceMappingURL=DocReference.js.map