Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let [reshard, slices] = ShardedCounterController.balanceWorkers(shardingInfo);
if (reshard) {
console.log("Resharding workers, new workers: " +
slices.length +
" prev num workers: " +
query.docs.length);
query.docs.forEach((snap) => t.delete(snap.ref));
slices.forEach((slice, index) => {
t.set(this.workersRef.doc(ShardedCounterController.encodeWorkerKey(index)), {
slice: slice,
timestamp: firestore_1.FieldValue.serverTimestamp(),
});
});
t.set(this.controllerDocRef, {
workers: slices,
timestamp: firestore_1.FieldValue.serverTimestamp(),
});
}
else {
// Check workers that haven't updated stats for over 90s - they most likely failed.
let failures = 0;
query.docs.forEach((snap) => {
if (timestamp / 1000 - snap.updateTime.seconds > 90) {
t.set(snap.ref, { timestamp: firestore_1.FieldValue.serverTimestamp() }, { merge: true });
failures++;
}
});
console.log("Detected " + failures + " failed workers.");
t.set(this.controllerDocRef, { timestamp: firestore_1.FieldValue.serverTimestamp() }, { merge: true });
}
}));
});
var data = document.data();
var subCollection = data[key_1] || { "count": 0 };
var oldCount = subCollection["count"] || 0;
count = oldCount + 1;
transaction.update(parentRef_1, (_a = {}, _a[key_1] = { "count": count }, _a));
var _a;
});
})];
case 2:
_a.sent();
this._count = count;
batch = firestore.batch();
collectionReference = this.reference.doc(newMember.id);
console.log(collectionReference.path);
value = {
createdAt: FirebaseFirestore.FieldValue.serverTimestamp(),
updatedAt: FirebaseFirestore.FieldValue.serverTimestamp()
};
batch.create(collectionReference, value);
return [2 /*return*/, batch.update(reference, newMember.value()).commit()];
case 3:
error_3 = _a.sent();
return [2 /*return*/, error_3];
case 4: return [3 /*break*/, 6];
case 5:
this.objects.push(newMember);
return [2 /*return*/];
case 6: return [2 /*return*/];
}
});
});
var subCollection = data[key_1] || { "count": 0 };
var oldCount = subCollection["count"] || 0;
count = oldCount + 1;
transaction.update(parentRef_1, (_a = {}, _a[key_1] = { "count": count }, _a));
var _a;
});
})];
case 2:
_a.sent();
this._count = count;
batch = firestore.batch();
collectionReference = this.reference.doc(newMember.id);
console.log(collectionReference.path);
value = {
createdAt: FirebaseFirestore.FieldValue.serverTimestamp(),
updatedAt: FirebaseFirestore.FieldValue.serverTimestamp()
};
batch.create(collectionReference, value);
return [2 /*return*/, batch.update(reference, newMember.value()).commit()];
case 3:
error_3 = _a.sent();
return [2 /*return*/, error_3];
case 4: return [3 /*break*/, 6];
case 5:
this.objects.push(newMember);
return [2 /*return*/];
case 6: return [2 /*return*/];
}
});
});
yield this.db.runTransaction((t) => __awaiter(this, void 0, void 0, function* () {
try {
const snap = yield t.get(this.metadoc.ref);
if (snap.exists && deepEqual(snap.data(), this.metadata)) {
t.update(snap.ref, {
timestamp: firestore_1.FieldValue.serverTimestamp(),
stats: stats,
});
}
}
catch (err) {
console.log("Failed to save writer stats.", err);
}
}));
}
query.docs.forEach((snap) => {
if (timestamp / 1000 - snap.updateTime.seconds > 90) {
t.set(snap.ref, { timestamp: firestore_1.FieldValue.serverTimestamp() }, { merge: true });
failures++;
}
});
console.log("Detected " + failures + " failed workers.");
insertions.forEach(function (document) {
var value = {
updatedAt: FirebaseFirestore.FieldValue.serverTimestamp()
};
if (!document.isSaved) {
value["createdAt"] = FirebaseFirestore.FieldValue.serverTimestamp();
batch.set(document.reference, document.value());
}
var reference = self.reference.doc(document.id);
batch.set(reference, value);
});
var deletions = this._deletions.filter(function (item) { return _this._insertions.indexOf(item) < 0; });
value(): any {
const values: any = this.rawValue()
if (this.isSaved) {
values["updatedAt"] = FirebaseFirestore.FieldValue.serverTimestamp()
} else {
values["createdAt"] = this.createdAt || FirebaseFirestore.FieldValue.serverTimestamp()
values["updatedAt"] = this.updatedAt || FirebaseFirestore.FieldValue.serverTimestamp()
}
return values
}
for (const key of properties) {
const descriptor = Object.getOwnPropertyDescriptor(this, key)
if (descriptor) {
const value = descriptor.get()
if (isCollection(value)) {
const collection: AnySubCollection = value as AnySubCollection
collection.setParent(this, key)
const batchable: Batchable = value as Batchable
batchable.pack(BatchType.save, batchID, _batch)
}
}
}
return _batch
case BatchType.update:
const updateValues = this._updateValues
updateValues["updatedAt"] = FirebaseFirestore.FieldValue.serverTimestamp()
_batch.update(reference, updateValues)
for (const key of properties) {
const descriptor = Object.getOwnPropertyDescriptor(this, key)
if (descriptor) {
const value = descriptor.get()
if (isCollection(value)) {
const collection: AnySubCollection = value as AnySubCollection
collection.setParent(this, key)
const batchable: Batchable = value as Batchable
batchable.pack(BatchType.update, batchID, _batch)
}
}
}
return _batch
case BatchType.delete:
_batch.delete(reference)
toPartialUpdate(uuidv4) {
if (this.isNoop())
return {};
return {
_updates_: firestore_1.FieldValue.arrayUnion({
_id_: uuidv4(),
_data_: this.data,
}),
};
}
toPartialShard(uuidv4) {
insertions.forEach(function (document) {
var value = {
updatedAt: FirebaseFirestore.FieldValue.serverTimestamp()
};
if (!document.isSaved) {
value["createdAt"] = FirebaseFirestore.FieldValue.serverTimestamp();
batch.set(document.reference, document.value());
}
var reference = self.reference.doc(document.id);
batch.set(reference, value);
});
var deletions = this._deletions.filter(function (item) { return _this._insertions.indexOf(item) < 0; });