@typeheim/orm-on-fire
Version:
Firestore ORM
173 lines • 6.47 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CollectionQuery = void 0;
const ChangedEntities_1 = require("../Data/ChangedEntities");
const FieldFilter_1 = require("./FieldFilter");
const Query_1 = require("../Contracts/Query");
const EntityStream_1 = require("../Data/EntityStream");
const operators_1 = require("rxjs/operators");
const EntityPromise_1 = require("../Data/EntityPromise");
const IndexFilter_1 = require("./IndexFilter");
class CollectionQuery {
constructor(collectionReference, entityManager, filterFields) {
this.collectionReference = collectionReference;
this.entityManager = entityManager;
this.filterFields = filterFields;
this.queryState = {
conditions: [],
indexes: [],
limit: -1,
asIds: false,
orderBy: [],
exclude: [],
};
}
exclude(ids) {
if (typeof ids == 'string') {
this.queryState.exclude.push(ids);
}
else {
this.queryState.exclude = ids;
}
return this;
}
map(operator) {
this.queryState.map = operator;
return this;
}
debounceUpdates(dueTime) {
this.queryState.debounceUpdatesTime = dueTime;
return this;
}
useIndex(indexFunction) {
let filter = {};
this.filterFields.forEach(field => {
filter[field.name] = new IndexFilter_1.IndexFilter(this.queryState, field.name);
});
indexFunction(filter);
return this;
}
asIds() {
this.queryState.asIds = true;
return this;
}
filter(filterFunction) {
let filter = {};
filter['id'] = new FieldFilter_1.FieldFilter(this.queryState, 'id');
this.filterFields.forEach(field => {
filter[field.name] = new FieldFilter_1.FieldFilter(this.queryState, field.name);
});
filterFunction(filter);
return this;
}
limit(limit) {
this.queryState.limit = limit;
return this;
}
orderBy(field, sortOrder = Query_1.SortOrder.Ascending) {
this.queryState.orderBy.push({
field,
sortOrder,
});
return this;
}
startAt(position) {
this.queryState.startAt = position;
return this;
}
startAfter(position) {
this.queryState.startAfter = position;
return this;
}
endAt(position) {
this.queryState.endAt = position;
return this;
}
endBefore(position) {
this.queryState.endBefore = position;
return this;
}
get() {
let entitiesPromise = new EntityPromise_1.EntityPromise();
this.collectionReference.get(this.queryState).then((querySnapshot) => {
var _a;
let entities = [];
let docs;
if (this.queryState.exclude) {
docs = querySnapshot.docs.filter(doc => !this.queryState.exclude.includes(doc.id));
}
else {
docs = querySnapshot;
}
if ((_a = this.queryState) === null || _a === void 0 ? void 0 : _a.asIds) {
docs.forEach((docSnapshot) => {
entities.push(docSnapshot.id);
});
}
else {
docs.forEach((docSnapshot) => {
entities.push(this.entityManager.fromSnapshot(docSnapshot));
});
}
if (this.queryState.map) {
entities = this.queryState.map(entities);
}
entitiesPromise.resolve(entities);
}).catch(error => entitiesPromise.reject(error));
return entitiesPromise;
}
changes() {
let snapshotStream = this.collectionReference.snapshot(this.queryState);
let dataStream = this.queryState.debounceUpdatesTime ? snapshotStream.pipe(operators_1.debounceTime(this.queryState.debounceUpdatesTime)) : snapshotStream;
let source = dataStream.pipe(operators_1.map((querySnapshot) => {
var _a;
let entityChanges = [];
querySnapshot.docChanges().forEach((change) => {
var _a;
if (((_a = this.queryState) === null || _a === void 0 ? void 0 : _a.asIds) && change.doc.exists) {
entityChanges.push(change.doc.id);
}
else {
let entity = this.entityManager.fromSnapshot(change.doc);
if (entity) {
entityChanges.push({
type: change.type,
entity,
});
}
}
});
if ((_a = this.queryState) === null || _a === void 0 ? void 0 : _a.map) {
entityChanges = this.queryState.map(entityChanges);
}
return new ChangedEntities_1.ChangedEntities(entityChanges);
}));
return new EntityStream_1.EntityStream(source, snapshotStream);
}
stream() {
let snapshotStream = this.collectionReference.snapshot(this.queryState);
let dataStream = this.queryState.debounceUpdatesTime ? snapshotStream.pipe(operators_1.debounceTime(this.queryState.debounceUpdatesTime)) : snapshotStream;
let source = dataStream.pipe(operators_1.map((querySnapshot) => {
let entities = [];
querySnapshot.forEach((docSnapshot) => {
var _a;
if (((_a = this.queryState) === null || _a === void 0 ? void 0 : _a.asIds) && docSnapshot.exists) {
entities.push(docSnapshot.id);
}
else {
let entity = this.entityManager.fromSnapshot(docSnapshot);
if (entity) {
entities.push(entity);
}
}
});
if (this.queryState.map) {
entities = this.queryState.map(entities);
}
return entities;
}));
return new EntityStream_1.EntityStream(source, snapshotStream);
}
}
exports.CollectionQuery = CollectionQuery;
//# sourceMappingURL=CollectionQuery.js.map