@typeheim/orm-on-fire
Version:
Firestore ORM
30 lines • 1.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DocInitializer = void 0;
const fire_rx_1 = require("@typeheim/fire-rx");
// @todo try decoupling from direct use of entity manager
class DocInitializer {
constructor(entity, entityManager) {
this.entity = entity;
this.entityManager = entityManager;
}
addTo(collectionRef) {
let data = this.entityManager.extractDataFromEntity(this.entity);
// type safety check to ensure dock path will be a string
if (this.entity['id'] && typeof this.entity['id'] !== 'string') {
this.entity['id'] = '' + this.entity['id'];
}
let documentReference = this.entity['id'] ? collectionRef.doc(this.entity['id']) : collectionRef.doc();
let promise = new fire_rx_1.ReactivePromise();
documentReference.set(data).then(() => {
this.entity['id'] = documentReference.nativeRef.id;
this.entityManager.refreshNewEntity(this.entity, documentReference.nativeRef);
promise.resolve(true);
}).catch(error => {
promise.reject(error);
});
return promise;
}
}
exports.DocInitializer = DocInitializer;
//# sourceMappingURL=DocInitializer.js.map