firewalk
Version:
A collection traversal library for Firestore
141 lines • 7.51 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _BasicBatchMigratorImpl_instances, _a, _BasicBatchMigratorImpl_MAX_BATCH_WRITE_DOC_COUNT, _BasicBatchMigratorImpl_validateTraverserCompatibility, _BasicBatchMigratorImpl_migrate;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasicBatchMigratorImpl = void 0;
const errors_1 = require("../../errors");
const utils_1 = require("../utils");
const abstract_1 = require("./abstract");
const errors_2 = require("../errors");
class BasicBatchMigratorImpl extends abstract_1.AbstractMigrator {
constructor(traverser, registeredCallbacks, migrationPredicates) {
super(registeredCallbacks, migrationPredicates);
_BasicBatchMigratorImpl_instances.add(this);
this.traverser = traverser;
__classPrivateFieldGet(this, _BasicBatchMigratorImpl_instances, "m", _BasicBatchMigratorImpl_validateTraverserCompatibility).call(this);
}
withPredicate(predicate) {
return new _a(this.traverser, this.registeredCallbacks, [
...this.migrationPredicates,
predicate,
]);
}
withTraverser(traverser) {
return new _a(traverser, this.registeredCallbacks, this.migrationPredicates);
}
set(data, options) {
return __awaiter(this, void 0, void 0, function* () {
return __classPrivateFieldGet(this, _BasicBatchMigratorImpl_instances, "m", _BasicBatchMigratorImpl_migrate).call(this, (writeBatch, doc) => {
if (options === undefined) {
// Signature 2
writeBatch.set(doc.ref, data);
}
else {
// Signature 1
writeBatch.set(doc.ref, data, options);
}
});
});
}
setWithDerivedData(getData, options) {
return __awaiter(this, void 0, void 0, function* () {
return __classPrivateFieldGet(this, _BasicBatchMigratorImpl_instances, "m", _BasicBatchMigratorImpl_migrate).call(this, (writeBatch, doc) => {
if (options === undefined) {
// Signature 2
const data = getData(doc);
writeBatch.set(doc.ref, data);
}
else {
// Signature 1
const data = getData(doc);
writeBatch.set(doc.ref, data, options);
}
});
});
}
update(dataOrField, preconditionOrValue, ...moreFieldsOrPrecondition) {
return __classPrivateFieldGet(this, _BasicBatchMigratorImpl_instances, "m", _BasicBatchMigratorImpl_migrate).call(this, (writeBatch, doc) => {
if (typeof dataOrField === 'string' ||
dataOrField instanceof this.firestoreConstructor.FieldPath) {
// Signature 2
const field = dataOrField;
const value = preconditionOrValue;
writeBatch.update(doc.ref, field, value, ...moreFieldsOrPrecondition);
}
else if (dataOrField !== undefined) {
// Signature 1
const data = dataOrField;
const precondition = preconditionOrValue;
if (precondition === undefined) {
writeBatch.update(doc.ref, data);
}
else {
writeBatch.update(doc.ref, data, precondition);
}
}
else {
throw new errors_2.IllegalArgumentError(`Unsupported signature detected. The 'dataOrField' argument cannot be undefined. The 'dataOrField' argument must be a string, a FieldPath, or an object.`);
}
});
}
updateWithDerivedData(getData, precondition) {
return __classPrivateFieldGet(this, _BasicBatchMigratorImpl_instances, "m", _BasicBatchMigratorImpl_migrate).call(this, (writeBatch, doc) => {
const data = getData(doc);
if (Array.isArray(data)) {
// Signature 2
writeBatch.update(doc.ref, ...data);
}
else if (data !== undefined) {
// Signature 1
if (precondition === undefined) {
writeBatch.update(doc.ref, data);
}
else {
writeBatch.update(doc.ref, data, precondition);
}
}
else {
throw new errors_2.IllegalArgumentError(`Unsupported signature detected. The 'data' argument cannot be undefined. The 'data' argument must be an array, an object, or a valid Firestore update signature.`);
}
});
}
}
exports.BasicBatchMigratorImpl = BasicBatchMigratorImpl;
_a = BasicBatchMigratorImpl, _BasicBatchMigratorImpl_instances = new WeakSet(), _BasicBatchMigratorImpl_validateTraverserCompatibility = function _BasicBatchMigratorImpl_validateTraverserCompatibility() {
const { batchSize } = this.traverser.traversalConfig;
const maxBatchWriteDocCount = __classPrivateFieldGet(_a, _a, "f", _BasicBatchMigratorImpl_MAX_BATCH_WRITE_DOC_COUNT);
if (typeof batchSize === 'number' &&
(!(0, utils_1.isPositiveInteger)(batchSize) || batchSize > maxBatchWriteDocCount)) {
throw new errors_1.InvalidConfigError(`The 'batchSize' field in the traversal config of a BatchMigrator's traverser must be a positive integer less than or equal to ${maxBatchWriteDocCount}. In Firestore, each write batch can write to a maximum of ${maxBatchWriteDocCount} documents.`);
}
}, _BasicBatchMigratorImpl_migrate = function _BasicBatchMigratorImpl_migrate(migrateDoc) {
return __awaiter(this, void 0, void 0, function* () {
return this.migrateWithTraverser((batchDocs) => __awaiter(this, void 0, void 0, function* () {
let migratedDocCount = 0;
const writeBatch = this.firestoreInstance.batch();
batchDocs.forEach((doc) => {
if (this.shouldMigrateDoc(doc)) {
migrateDoc(writeBatch, doc);
migratedDocCount++;
}
});
yield writeBatch.commit();
return migratedDocCount;
}));
});
};
_BasicBatchMigratorImpl_MAX_BATCH_WRITE_DOC_COUNT = { value: 500 };
//# sourceMappingURL=BasicBatchMigratorImpl.js.map