@typeheim/orm-on-fire
Version:
Firestore ORM
53 lines • 2.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Reference = void 0;
const EntityManager_1 = require("../Persistence/EntityManager");
const singletons_1 = require("../singletons");
const EntityQuery_1 = require("../Persistence/EntityQuery");
const fire_rx_1 = require("@typeheim/fire-rx");
const CollectionReference_1 = require("../Persistence/CollectionReference");
const operators_1 = require("../operators");
class Reference {
constructor(entityConstructor, owner) {
this.entityConstructor = entityConstructor;
this.owner = owner;
}
link(reference) {
var _a, _b, _c, _d;
// @ts-ignore
this.docRef = (_a = reference === null || reference === void 0 ? void 0 : reference.__ormOnFire) === null || _a === void 0 ? void 0 : _a.docRef;
let result;
if (((_b = this.owner) === null || _b === void 0 ? void 0 : _b.__ormOnFire) === undefined || ((_d = (_c = this.owner) === null || _c === void 0 ? void 0 : _c.__ormOnFire) === null || _d === void 0 ? void 0 : _d.isNew)) {
result = new fire_rx_1.ReactivePromise();
result.resolve();
}
else {
result = operators_1.save(this.owner);
}
return result;
}
get() {
return new EntityQuery_1.EntityQuery(this.docRef, this.entityBuilder).get();
}
stream() {
return new EntityQuery_1.EntityQuery(this.docRef, this.entityBuilder).stream();
}
get entityBuilder() {
var _a, _b;
if (!this._entityBuilder) {
// @todo move outside of Reference
let metadata = singletons_1.Metadata.entity(this.entityConstructor).get();
let collectionPath = `${(_b = (_a = this.owner) === null || _a === void 0 ? void 0 : _a.__ormOnFire) === null || _b === void 0 ? void 0 : _b.docRef.nativeRef.path}/${metadata.collection}`;
this._entityBuilder = new EntityManager_1.EntityManager(metadata, this.entityConstructor, new CollectionReference_1.CollectionReference(this.entityConstructor, collectionPath));
}
return this._entityBuilder;
}
___attachDockRef(docRef) {
this.docRef = docRef;
}
get ___docReference() {
return this.docRef;
}
}
exports.Reference = Reference;
//# sourceMappingURL=Reference.js.map