react-native-firebase-compiled
Version:
A well tested, feature rich Firebase implementation for React Native, supporting iOS & Android. Individual module support for Admob, Analytics, Auth, Crash Reporting, Cloud Firestore, Database, Dynamic Links, Functions, Messaging (FCM), Remote Config, Sto
152 lines (121 loc) • 4.33 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _utils = require("./utils");
var _serialize = require("./utils/serialize");
var _DocumentSnapshot = _interopRequireDefault(require("./DocumentSnapshot"));
var _native = require("../../utils/native");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
// TODO docs state all get requests must be made FIRST before any modifications
// TODO so need to validate that
/**
* @class Transaction
*/
class Transaction {
constructor(firestore, meta) {
_defineProperty(this, "_pendingResult", void 0);
_defineProperty(this, "_firestore", void 0);
_defineProperty(this, "_meta", void 0);
_defineProperty(this, "_commandBuffer", void 0);
this._meta = meta;
this._commandBuffer = [];
this._firestore = firestore;
this._pendingResult = undefined;
}
/**
* -------------
* INTERNAL API
* -------------
*/
/**
* Clears the command buffer and any pending result in prep for
* the next transaction iteration attempt.
*
* @private
*/
_prepare() {
this._commandBuffer = [];
this._pendingResult = undefined;
}
/**
* -------------
* PUBLIC API
* -------------
*/
/**
* Reads the document referenced by the provided DocumentReference.
*
* @param documentRef DocumentReference A reference to the document to be retrieved. Value must not be null.
*
* @returns Promise<DocumentSnapshot>
*/
get(documentRef) {
// todo validate doc ref
return (0, _native.getNativeModule)(this._firestore).transactionGetDocument(this._meta.id, documentRef.path).then(result => new _DocumentSnapshot.default(this._firestore, result));
}
/**
* Writes to the document referred to by the provided DocumentReference.
* If the document does not exist yet, it will be created. If you pass options,
* the provided data can be merged into the existing document.
*
* @param documentRef DocumentReference A reference to the document to be created. Value must not be null.
* @param data Object An object of the fields and values for the document.
* @param options SetOptions An object to configure the set behavior.
* Pass {merge: true} to only replace the values specified in the data argument.
* Fields omitted will remain untouched.
*
* @returns {Transaction}
*/
set(documentRef, data, options) {
// todo validate doc ref
// todo validate data is object
this._commandBuffer.push({
type: 'set',
path: documentRef.path,
data: (0, _serialize.buildNativeMap)(data),
options: options || {}
});
return this;
}
/**
* Updates fields in the document referred to by this DocumentReference.
* The update will fail if applied to a document that does not exist. Nested
* fields can be updated by providing dot-separated field path strings or by providing FieldPath objects.
*
* @param documentRef DocumentReference A reference to the document to be updated. Value must not be null.
* @param args any Either an object containing all of the fields and values to update,
* or a series of arguments alternating between fields (as string or FieldPath
* objects) and values.
*
* @returns {Transaction}
*/
update(documentRef, ...args) {
// todo validate doc ref
const data = (0, _utils.parseUpdateArgs)(args, 'Transaction.update');
this._commandBuffer.push({
type: 'update',
path: documentRef.path,
data: (0, _serialize.buildNativeMap)(data)
});
return this;
}
/**
* Deletes the document referred to by the provided DocumentReference.
*
* @param documentRef DocumentReference A reference to the document to be deleted. Value must not be null.
*
* @returns {Transaction}
*/
delete(documentRef) {
// todo validate doc ref
this._commandBuffer.push({
type: 'delete',
path: documentRef.path
});
return this;
}
}
exports.default = Transaction;