UNPKG

firestore-in-memory

Version:

A drop-in, in-memory implementation of Firestore useful for testing

65 lines (53 loc) 1.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _fp = require("lodash/fp"); class Transaction { constructor() { this.log = []; this.mutations = []; } async get(ref) { if (!(0, _fp.every)({ op: 'get' }, this.log)) { throw new Error('Cannot call get in a transaction after set or update'); } this.log.push({ op: 'get', path: ref.path }); return ref.get(); } set(ref, data, opts) { this.log.push({ op: 'set', path: ref.path, data, opts }); this.mutations.push(async () => ref.set(data, opts)); return this; } update(ref, data) { this.log.push({ op: 'update', path: ref.path, data }); this.mutations.push(async () => ref.update(data)); return this; } async commit() { const commitNext = (promise, remaining) => promise && promise().then(() => { if ((0, _fp.size)(remaining)) { return commitNext((0, _fp.first)(remaining), (0, _fp.drop)(1, remaining)); } return Promise.resolve(); }); return commitNext((0, _fp.first)(this.mutations), (0, _fp.drop)(1, this.mutations)); } } exports.default = Transaction;