How to use the @google-cloud/firestore.FieldValue function in @google-cloud/firestore

To help you get started, we’ve selected a few @google-cloud/firestore examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github firebase / extensions / firestore-counter / functions / lib / controller.js View on Github external
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 });
                }
            }));
        });
github 1amageek / pring-admin.ts / pring.js View on Github external
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*/];
                    }
                });
            });
github 1amageek / pring-admin.ts / pring.js View on Github external
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*/];
                    }
                });
            });
github firebase / extensions / firestore-counter / functions / lib / worker.js View on Github external
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);
                        }
                    }));
                }
github firebase / extensions / firestore-counter / functions / lib / controller.js View on Github external
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.");
github 1amageek / pring-admin.ts / src / pring.js View on Github external
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; });
github 1amageek / pring-admin.ts / src / base.ts View on Github external
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
    }
github 1amageek / pring-admin.ts / src / base.ts View on Github external
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)
github firebase / extensions / firestore-counter / functions / lib / aggregator.js View on Github external
toPartialUpdate(uuidv4) {
        if (this.isNoop())
            return {};
        return {
            _updates_: firestore_1.FieldValue.arrayUnion({
                _id_: uuidv4(),
                _data_: this.data,
            }),
        };
    }
    toPartialShard(uuidv4) {
github 1amageek / pring-admin.ts / src / pring.js View on Github external
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; });