@typeheim/orm-on-fire
Version:
Firestore ORM
137 lines • 7.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CollectionReference = exports.CollectionRefType = void 0;
const fire_rx_1 = require("@typeheim/fire-rx");
const DocReference_1 = require("./DocReference");
const Query_1 = require("../Contracts/Query");
var CollectionRefType;
(function (CollectionRefType) {
CollectionRefType[CollectionRefType["Basic"] = 0] = "Basic";
CollectionRefType[CollectionRefType["Group"] = 1] = "Group";
})(CollectionRefType = exports.CollectionRefType || (exports.CollectionRefType = {}));
class CollectionReference {
constructor(connection, collectionPath, type = CollectionRefType.Basic) {
this.connection = connection;
this.collectionPath = collectionPath;
this.type = type;
}
get(queryState) {
let promise = new fire_rx_1.ReactivePromise();
this.connection.isInitialized.then((isInitialized) => {
var _a;
if (isInitialized) {
if (((_a = queryState === null || queryState === void 0 ? void 0 : queryState.indexes) === null || _a === void 0 ? void 0 : _a.length) > 0) {
this.fetchIndex(queryState).then((snapshot) => {
let ids = snapshot.docs.map(snapshot => snapshot.id);
this.buildQuery(queryState).where('__name__', 'in', ids).get().then((snapshot) => {
promise.resolve(snapshot);
}).catch(error => promise.reject(error));
}).catch(error => promise.reject(error));
}
else {
this.buildQuery(queryState).get().then((snapshot) => {
promise.resolve(snapshot);
}).catch(error => promise.reject(error));
}
}
});
return promise;
}
snapshot(queryState) {
let subject = new fire_rx_1.StatefulSubject();
this.connection.isInitialized.then((isInitialized) => {
var _a;
if (isInitialized) {
if (((_a = queryState === null || queryState === void 0 ? void 0 : queryState.indexes) === null || _a === void 0 ? void 0 : _a.length) > 0) {
this.fetchIndex(queryState).get().then((snapshot) => {
let ids = snapshot.docs.map(snapshot => snapshot.id);
this.buildQuery(queryState).where('__name__', 'in', ids).onSnapshot(snapshot => subject.next(snapshot), error => subject.error(error), () => subject.complete());
}).catch(error => subject.error(error));
}
else {
this.buildQuery(queryState).onSnapshot(snapshot => subject.next(snapshot), error => subject.error(error), () => subject.complete());
}
}
});
return subject;
}
fetchIndex(queryState) {
let collection = this.buildNativeIndexCollection();
let query = collection;
queryState.indexes.forEach(index => {
query = collection.where(index.fieldName, index.operator, index.compareValue);
});
return query.get();
}
buildQuery(queryState) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
let collection = this.buildNativeCollection();
let query = collection;
if (queryState) {
if (((_a = queryState.conditions) === null || _a === void 0 ? void 0 : _a.length) > 0) {
queryState.conditions.forEach(condition => {
query = query.where(condition.fieldName, condition.operator, condition.compareValue);
});
}
if (((_b = queryState.orderBy) === null || _b === void 0 ? void 0 : _b.length) > 0) {
queryState.orderBy.forEach(orderCondition => {
if (orderCondition.sortOrder === Query_1.SortOrder.Descending) {
query = query.orderBy(orderCondition.field, 'desc');
}
else {
query = query.orderBy(orderCondition.field);
}
});
}
if (queryState.limit > 0) {
query = query.limit(queryState.limit);
}
if (queryState.startAt !== undefined) {
if ((_d = (_c = queryState.startAt) === null || _c === void 0 ? void 0 : _c.__ormOnFire) === null || _d === void 0 ? void 0 : _d.docRef) {
query = query.startAt((_g = (_f = (_e = queryState.startAt) === null || _e === void 0 ? void 0 : _e.__ormOnFire) === null || _f === void 0 ? void 0 : _f.docRef) === null || _g === void 0 ? void 0 : _g.nativeRef);
}
else {
query = query.startAt(queryState.startAt);
}
}
if (queryState.startAfter !== undefined) {
if ((_j = (_h = queryState.startAfter) === null || _h === void 0 ? void 0 : _h.__ormOnFire) === null || _j === void 0 ? void 0 : _j.docRef) {
query = query.startAfter((_m = (_l = (_k = queryState.startAfter) === null || _k === void 0 ? void 0 : _k.__ormOnFire) === null || _l === void 0 ? void 0 : _l.docRef) === null || _m === void 0 ? void 0 : _m.nativeRef);
}
else {
query = query.startAfter(queryState.startAfter);
}
}
if (queryState.endAt !== undefined) {
if ((_p = (_o = queryState.endAt) === null || _o === void 0 ? void 0 : _o.__ormOnFire) === null || _p === void 0 ? void 0 : _p.docRef) {
query = query.endAt((_s = (_r = (_q = queryState.endAt) === null || _q === void 0 ? void 0 : _q.__ormOnFire) === null || _r === void 0 ? void 0 : _r.docRef) === null || _s === void 0 ? void 0 : _s.nativeRef);
}
else {
query = query.endAt(queryState.endAt);
}
}
if (queryState.endBefore !== undefined) {
if ((_u = (_t = queryState.endBefore) === null || _t === void 0 ? void 0 : _t.__ormOnFire) === null || _u === void 0 ? void 0 : _u.docRef) {
query = query.endBefore((_x = (_w = (_v = queryState.endBefore) === null || _v === void 0 ? void 0 : _v.__ormOnFire) === null || _w === void 0 ? void 0 : _w.docRef) === null || _x === void 0 ? void 0 : _x.nativeRef);
}
else {
query = query.endBefore(queryState.endBefore);
}
}
}
return query;
}
buildNativeCollection() {
return this.type === CollectionRefType.Basic ?
this.connection.driver.collection(this.collectionPath) :
this.connection.driver.collectionGroup(this.collectionPath);
}
buildNativeIndexCollection() {
return this.connection.driver.collection(`of-metadata/${this.collectionPath}/indexes`);
}
doc(docPath) {
return new DocReference_1.DocReference(this.connection, docPath, this.collectionPath);
}
}
exports.CollectionReference = CollectionReference;
//# sourceMappingURL=CollectionReference.js.map