@nativescript/firebase-firestore
Version:
NativeScript Firebase - Firestore
1,245 lines • 40.2 kB
JavaScript
import { GetOptions, DocumentChangeType, parseOnSnapshotArgs } from './common';
export { GetOptions, DocumentChangeType };
const main_queue = dispatch_get_current_queue();
import { firebase, FirebaseApp, FirebaseError } from '@nativescript/firebase-core';
import '@nativescript/core';
let defaultFirestore;
const fb = firebase();
Object.defineProperty(fb, 'firestore', {
value: (app) => {
if (!app) {
if (!defaultFirestore) {
defaultFirestore = new Firestore();
}
return defaultFirestore;
}
return new Firestore(app);
},
writable: false,
});
function deserializeField(value) {
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
return value;
}
if (value instanceof FIRTimestamp) {
return Timestamp.fromNative(value);
}
if (value instanceof FIRGeoPoint) {
return GeoPoint.fromNative(value);
}
if (value instanceof FIRFieldPath) {
return FieldPath.fromNative(value);
}
if (value instanceof FIRFieldValue) {
return FieldValue.fromNative(value);
}
if (value instanceof FIRDocumentReference) {
return DocumentReference.fromNative(value);
}
if (value instanceof FIRCollectionReference) {
return CollectionReference.fromNative(value);
}
if (value instanceof NSNull) {
return null;
}
if (value instanceof NSArray) {
let array = [];
for (let i = 0, n = value.count; i < n; i++) {
array[i] = deserializeField(value.objectAtIndex(i));
}
return array;
}
if (value instanceof NSDictionary) {
let dict = {};
for (let i = 0, n = value.allKeys.count; i < n; i++) {
let key = value.allKeys.objectAtIndex(i);
dict[key] = deserializeField(value.objectForKey(key));
}
return dict;
}
if (value instanceof NSData) {
return Bytes.fromNative(value);
}
return value;
}
function serializeItems(value) {
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
return value;
}
if (value instanceof Date) {
return NSDate.dateWithTimeIntervalSince1970(value.getTime() / 1000);
}
if (value instanceof Timestamp) {
return value.native;
}
if (value instanceof GeoPoint) {
return value.native;
}
if (value instanceof FieldPath) {
return value.native;
}
if (value instanceof FieldValue) {
return value.native;
}
if (value instanceof DocumentReference) {
return value.native;
}
if (value instanceof CollectionReference) {
return value.native;
}
if (value === null) {
return NSNull.new();
}
if (Array.isArray(value)) {
return value.map((item) => serializeItems(item));
}
if (value && typeof value === 'object') {
const keys = Object.keys(value);
let dict = {};
keys.forEach((key) => {
dict[key] = serializeItems(value[key]);
});
return dict;
}
if (value instanceof Bytes) {
return value.native;
}
return value;
}
function createDictionary(dataOrField, fieldPathValue, moreFieldsAndValues) {
const data = NSMutableDictionary.alloc().init();
const assignKeyValue = (key, val) => {
// check the map for weak object
if (typeof key === 'string' && key.includes('FIRFieldPath')) {
const ref = fp_refs.get(key)?.get?.();
if (ref) {
data.setObjectForKey(serializeItems(val), ref);
return;
}
}
data.setObjectForKey(serializeItems(val), key instanceof FieldPath ? key.native : key);
};
if (!fieldPathValue && !moreFieldsAndValues) {
Object.entries(dataOrField).forEach((item) => assignKeyValue(item[0], item[1]));
}
else {
assignKeyValue(dataOrField, fieldPathValue);
if (Array.isArray(moreFieldsAndValues)) {
Object.entries(Object.fromEntries(moreFieldsAndValues)).forEach(([key, value]) => assignKeyValue(key, value));
}
}
return data;
}
export class Transaction {
static fromNative(transaction) {
if (transaction instanceof FIRTransaction) {
const tran = new Transaction();
tran._native = transaction;
return tran;
}
return null;
}
delete(documentRef) {
return Transaction.fromNative(this._native.deleteDocument(documentRef.native));
}
get(documentRef) {
// TODO check error returned
return new Promise((resolve, reject) => {
try {
resolve(DocumentSnapshot.fromNative(this._native.getDocumentError(documentRef.native)));
}
catch (e) {
reject(e);
}
});
}
update(documentRef, field, value, moreFieldsAndValues) {
const data = createDictionary(field, value, moreFieldsAndValues);
return Transaction.fromNative(this._native.updateDataForDocument(data, documentRef?.native));
}
set(documentRef, data, options) {
let transaction;
if (options) {
if (typeof options?.merge === 'boolean') {
transaction = this.native.setDataForDocumentMerge(serializeItems(data), documentRef.native, options.merge);
}
else if (options.mergeFields) {
if (Array.isArray(options.mergeFields)) {
if (typeof options.mergeFields[0] === 'string') {
transaction = this.native.setDataForDocumentMergeFields(serializeItems(data), documentRef.native, options.mergeFields);
}
else {
transaction = this.native.setDataForDocumentMergeFields(serializeItems(data), documentRef.native, options.mergeFields.map((field) => field.native));
}
}
}
}
else {
transaction = this.native.setDataForDocument(serializeItems(data), documentRef.native);
}
return Transaction.fromNative(transaction);
}
get ios() {
return this.native;
}
get native() {
return this._native;
}
}
export class SnapshotMetadata {
static fromNative(metadata) {
if (metadata instanceof FIRSnapshotMetadata) {
const meta = new SnapshotMetadata();
meta._native = metadata;
return meta;
}
return null;
}
get fromCache() {
return this.native.fromCache;
}
get hasPendingWrites() {
return this.native.pendingWrites;
}
toJSON() {
return {
fromCache: this.fromCache,
hasPendingWrites: this.hasPendingWrites,
};
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
export class DocumentSnapshot {
static fromNative(snapshot) {
if (snapshot instanceof FIRDocumentSnapshot) {
const ss = new DocumentSnapshot();
ss._native = snapshot;
return ss;
}
return null;
}
get exists() {
return this.native.exists;
}
get id() {
return this.native.documentID;
}
get metadata() {
return SnapshotMetadata.fromNative(this.native.metadata);
}
get ref() {
return DocumentReference.fromNative(this.native.reference);
}
data() {
return deserializeField(this.native.data());
}
get(fieldPath) {
if (fieldPath instanceof FieldPath) {
return deserializeField(this.native.valueForField(fieldPath.native));
}
else {
return deserializeField(this.native.valueForField(fieldPath));
}
}
toJSON() {
return {
exists: this.exists,
id: this.id,
metadata: this.metadata,
ref: this.ref,
data: this.data,
};
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
export class DocumentChange {
static fromNative(change) {
if (change instanceof FIRDocumentChange) {
const documentChange = new DocumentChange();
documentChange._native = change;
return documentChange;
}
return null;
}
get doc() {
return QueryDocumentSnapshot.fromNative(this.native.document);
}
get newIndex() {
return this.native.newIndex;
}
get oldIndex() {
return this.native.oldIndex;
}
get type() {
switch (this.native.type) {
case 0 /* FIRDocumentChangeType.Added */:
return DocumentChangeType.Added;
case 1 /* FIRDocumentChangeType.Modified */:
return DocumentChangeType.Modified;
case 2 /* FIRDocumentChangeType.Removed */:
return DocumentChangeType.Removed;
}
}
toJSON() {
return {
doc: this.doc,
newIndex: this.newIndex,
oldIndex: this.oldIndex,
type: this.type,
};
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
export class Query {
static fromNative(query) {
if (query instanceof FIRQuery) {
const nativeQuery = new Query();
nativeQuery._native = query;
return nativeQuery;
}
return null;
}
endAt(fieldValues) {
if (Array.isArray(fieldValues)) {
return Query.fromNative(this.native.queryEndingAtValues(fieldValues.map((value) => value?.native || value)));
}
else {
return Query.fromNative(this.native.queryEndingAtDocument(fieldValues?.native));
}
}
endBefore(fieldValues) {
if (Array.isArray(fieldValues)) {
return Query.fromNative(this.native.queryEndingBeforeValues(fieldValues.map((value) => value.native)));
}
else {
return Query.fromNative(this.native.queryEndingBeforeDocument(fieldValues?.native));
}
}
get(options) {
let source;
switch (options) {
case GetOptions.Cache:
source = 2 /* FIRFirestoreSource.Cache */;
break;
case GetOptions.Server:
source = 1 /* FIRFirestoreSource.Server */;
break;
default:
source = 0 /* FIRFirestoreSource.Default */;
break;
}
return new Promise((resolve, reject) => {
this.native.getDocumentsWithSourceCompletion(source, (snap, error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}
else {
resolve(QuerySnapshot.fromNative(snap));
}
});
});
}
limit(limit) {
return Query.fromNative(this.native.queryLimitedTo(limit));
}
limitToLast(limitToLast) {
return Query.fromNative(this.native.queryLimitedToLast(limitToLast));
}
onSnapshot(...args) {
const { includeMetadataChanges, ...handlers } = parseOnSnapshotArgs(args);
const listener = this.native.addSnapshotListenerWithIncludeMetadataChangesListener(includeMetadataChanges, (querySnapshot, error) => {
if (error) {
handlers.error?.(FirebaseError.fromNative(error));
}
else {
handlers.next?.(QuerySnapshot.fromNative(querySnapshot));
}
});
return () => listener?.remove?.();
}
orderBy(fieldPath, directionStr = 'asc') {
if (fieldPath instanceof FieldPath) {
if (directionStr === 'asc') {
return Query.fromNative(this.native.queryOrderedByFieldPath(fieldPath.native));
}
else {
return Query.fromNative(this.native.queryOrderedByFieldPathDescending(fieldPath.native, true));
}
}
else {
if (directionStr === 'asc') {
return Query.fromNative(this.native.queryOrderedByField(fieldPath));
}
else {
return Query.fromNative(this.native.queryOrderedByFieldDescending(fieldPath, true));
}
}
}
startAfter(fieldValues) {
if (Array.isArray(fieldValues)) {
return Query.fromNative(this.native.queryStartingAfterValues(fieldValues.map((value) => value.native)));
}
else {
return Query.fromNative(this.native.queryStartingAfterDocument(fieldValues?.native));
}
}
startAt(fieldValues) {
if (Array.isArray(fieldValues)) {
return Query.fromNative(this.native.queryStartingAtValues(fieldValues.map((value) => value.native)));
}
else {
return Query.fromNative(this.native.queryStartingAtDocument(fieldValues?.native));
}
}
where(fieldPath, opStr, value) {
let query;
if (fieldPath instanceof FieldPath) {
switch (opStr) {
case '!=':
query = this.native.queryWhereFieldPathIsNotEqualTo(fieldPath.native, serializeItems(value));
break;
case '<':
query = this.native.queryWhereFieldPathIsLessThan(fieldPath.native, serializeItems(value));
break;
case '>':
query = this.native.queryWhereFieldPathIsGreaterThan(fieldPath.native, serializeItems(value));
break;
case '<=':
query = this.native.queryWhereFieldPathIsLessThanOrEqualTo(fieldPath.native, serializeItems(value));
break;
case '>=':
query = this.native.queryWhereFieldPathIsGreaterThanOrEqualTo(fieldPath.native, serializeItems(value));
break;
case '==':
query = this.native.queryWhereFieldPathIsEqualTo(fieldPath.native, serializeItems(value));
break;
case 'array-contains':
query = this.native.queryWhereFieldPathArrayContains(fieldPath.native, serializeItems(value));
break;
case 'array-contains-any':
query = this.native.queryWhereFieldPathArrayContainsAny(fieldPath.native, serializeItems(value));
break;
case 'in':
query = this.native.queryWhereFieldPathIn(fieldPath.native, serializeItems(value));
break;
case 'not-in':
query = this.native.queryWhereFieldPathNotIn(fieldPath.native, serializeItems(value));
break;
}
}
else {
switch (opStr) {
case '!=':
query = this.native.queryWhereFieldIsNotEqualTo(fieldPath, serializeItems(value));
break;
case '<':
query = this.native.queryWhereFieldIsLessThan(fieldPath, serializeItems(value));
break;
case '>':
query = this.native.queryWhereFieldIsGreaterThan(fieldPath, serializeItems(value));
break;
case '<=':
query = this.native.queryWhereFieldIsLessThanOrEqualTo(fieldPath, serializeItems(value));
break;
case '>=':
query = this.native.queryWhereFieldIsGreaterThanOrEqualTo(fieldPath, serializeItems(value));
break;
case '==':
query = this.native.queryWhereFieldIsEqualTo(fieldPath, serializeItems(value));
break;
case 'array-contains':
query = this.native.queryWhereFieldArrayContains(fieldPath, serializeItems(value));
break;
case 'array-contains-any':
query = this.native.queryWhereFieldArrayContainsAny(fieldPath, serializeItems(value));
break;
case 'in':
query = this.native.queryWhereFieldIn(fieldPath, serializeItems(value));
break;
case 'not-in':
query = this.native.queryWhereFieldNotIn(fieldPath, serializeItems(value));
break;
}
}
return Query.fromNative(query);
}
isEqual(other) {
return this.native.isEqual(other?.native);
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
export class QueryDocumentSnapshot extends DocumentSnapshot {
static fromNative(snapshot) {
if (snapshot instanceof FIRQueryDocumentSnapshot) {
const ss = new QueryDocumentSnapshot();
ss._native = snapshot;
return ss;
}
return null;
}
data() {
return deserializeField(this._native.data());
}
get(fieldPath) {
if (fieldPath instanceof FieldPath) {
return deserializeField(this.native.valueForField(fieldPath.native));
}
else {
return deserializeField(this.native.valueForField(fieldPath));
}
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
export class QuerySnapshot {
static fromNative(snapshot) {
if (snapshot instanceof FIRQuerySnapshot) {
const ss = new QuerySnapshot();
ss._native = snapshot;
return ss;
}
return null;
}
get docs() {
const documents = this.native.documents;
const count = documents.count;
const docs = [];
for (let i = 0; i < count; i++) {
docs.push(QueryDocumentSnapshot.fromNative(documents.objectAtIndex(i)));
}
return docs;
}
get empty() {
return this.native.empty;
}
get metadata() {
return SnapshotMetadata.fromNative(this.native.metadata);
}
get query() {
return Query.fromNative(this.native.query);
}
get size() {
return this.native.count;
}
docChanges(options) {
let changes;
if (typeof options?.includeMetadataChanges === 'boolean') {
changes = this.native.documentChangesWithIncludeMetadataChanges(options.includeMetadataChanges);
}
else {
changes = this.native.documentChanges;
}
const count = changes.count;
const docChanges = [];
for (let i = 0; i < count; i++) {
docChanges.push(DocumentChange.fromNative(changes.objectAtIndex(i)));
}
return docChanges;
}
forEach(callback, thisArg) {
if (typeof callback === 'function') {
const cb = thisArg ? callback.bind(thisArg) : callback;
const count = this.native.count;
for (let i = 0; i < count; i++) {
cb(QueryDocumentSnapshot.fromNative(this.native.documents.objectAtIndex(i)));
}
}
}
toJSON() {
return {
docs: this.docs,
empty: this.empty,
metadata: this.metadata,
size: this.size,
};
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
export class CollectionReference extends Query {
static fromNative(collection) {
if (collection instanceof FIRCollectionReference) {
const nativeCollection = new CollectionReference();
nativeCollection._native = collection;
return nativeCollection;
}
return null;
}
get id() {
return this.native.collectionID;
}
get parent() {
return DocumentReference.fromNative(this.native.parent);
}
get path() {
return this.native.path;
}
add(data) {
return new Promise((resolve, reject) => {
const ref = this.native.addDocumentWithDataCompletion(serializeItems(data), (error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}
else {
resolve(DocumentReference.fromNative(ref));
}
});
});
}
doc(documentPath) {
return DocumentReference.fromNative(documentPath ? this.native.documentWithPath(documentPath) : this.native.documentWithAutoID());
}
toJSON() {
return {
id: this.id,
path: this.path,
parent: this.parent,
};
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
export class DocumentReference {
static fromNative(document) {
if (document instanceof FIRDocumentReference) {
const doc = new DocumentReference();
doc._native = document;
return doc;
}
return null;
}
get firestore() {
return Firestore.fromNative(this.native.firestore);
}
get id() {
return this.native.documentID;
}
get parent() {
return CollectionReference.fromNative(this.native.parent);
}
get path() {
return this.native.path;
}
collection(collectionPath) {
return CollectionReference.fromNative(this.native.collectionWithPath(collectionPath));
}
delete() {
return new Promise((resolve, reject) => {
this.native.deleteDocumentWithCompletion((error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}
else {
resolve();
}
});
});
}
get(options) {
return new Promise((resolve, reject) => {
let source;
switch (options) {
case GetOptions.Cache:
source = 2 /* FIRFirestoreSource.Cache */;
break;
case GetOptions.Server:
source = 1 /* FIRFirestoreSource.Server */;
break;
default:
source = 0 /* FIRFirestoreSource.Default */;
break;
}
this.native.getDocumentWithSourceCompletion(source, (ss, error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}
else {
resolve(DocumentSnapshot.fromNative(ss));
}
});
});
}
onSnapshot(...args) {
const { includeMetadataChanges, ...handlers } = parseOnSnapshotArgs(args);
const listener = this.native.addSnapshotListenerWithIncludeMetadataChangesListener(includeMetadataChanges, (docSnapshot, error) => {
if (error) {
handlers.error?.(FirebaseError.fromNative(error));
}
else {
handlers.next?.(DocumentSnapshot.fromNative(docSnapshot));
}
});
return () => listener?.remove?.();
}
set(data, options) {
return new Promise((resolve, reject) => {
if (options) {
if (typeof options?.merge === 'boolean') {
this.native.setDataMergeCompletion(serializeItems(data), options.merge, (error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}
else {
resolve();
}
});
}
else if (options.mergeFields) {
if (Array.isArray(options.mergeFields)) {
if (typeof options.mergeFields[0] === 'string') {
this.native.setDataMergeFieldsCompletion(serializeItems(data), options.mergeFields, (error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}
else {
resolve();
}
});
}
else {
this.native.setDataMergeFieldsCompletion(serializeItems(data), options.mergeFields.map((field) => field.native), (error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}
else {
resolve();
}
});
}
}
}
}
else {
this.native.setDataCompletion(serializeItems(data), (error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}
else {
resolve();
}
});
}
});
}
update(field, value, moreFieldsAndValues) {
return new Promise((resolve, reject) => {
const data = createDictionary(field, value, moreFieldsAndValues);
this._native.updateDataCompletion(data, (error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}
else {
resolve();
}
});
});
}
toJSON() {
return {
id: this.id,
path: this.path,
parent: this.parent,
};
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
const fp_refs = new Map();
export class FieldPath {
constructor(fieldNames, native = false) {
if (!native) {
this._native = FIRFieldPath.alloc().initWithFields(fieldNames);
this._addToMap();
}
}
_addToMap() {
fp_refs.set(this.native.description, new WeakRef(this._native));
}
static fromNative(field) {
if (field instanceof FIRFieldPath) {
const path = new FieldPath([], true);
path._native = field;
path._addToMap();
return path;
}
return null;
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
static documentId() {
return FieldPath.fromNative(FIRFieldPath.documentID());
}
toString() {
return this.native.description;
}
}
export class FieldValue {
static fromNative(field) {
if (field instanceof FIRFieldValue) {
const value = new FieldValue();
value._native = field;
return value;
}
return null;
}
static arrayRemove(elements) {
return FieldValue.fromNative(FIRFieldValue.fieldValueForArrayRemove(elements.map((element) => element?.native || element)));
}
static arrayUnion(elements) {
return FieldValue.fromNative(FIRFieldValue.fieldValueForArrayUnion(elements.map((element) => element?.native || element)));
}
static delete() {
return FieldValue.fromNative(FIRFieldValue.fieldValueForDelete());
}
static increment(n) {
const useDouble = n % 1 === 0;
if (useDouble) {
return FieldValue.fromNative(FIRFieldValue.fieldValueForDoubleIncrement(n));
}
return FieldValue.fromNative(FIRFieldValue.fieldValueForIntegerIncrement(n));
}
static serverTimestamp() {
return FieldValue.fromNative(FIRFieldValue.fieldValueForServerTimestamp());
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
export class GeoPoint {
constructor(latitude, longitude, native = false) {
if (!native) {
this._native = FIRGeoPoint.alloc().initWithLatitudeLongitude(latitude, longitude);
}
}
static fromNative(point) {
if (point instanceof FIRGeoPoint) {
const geo = new GeoPoint(0, 0, true);
geo._native = point;
return geo;
}
return null;
}
get latitude() {
return this.native.latitude;
}
get longitude() {
return this.native.longitude;
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
toJSON() {
return {
latitude: this.latitude,
longitude: this.longitude,
};
}
}
export class Timestamp {
constructor(seconds, nanoseconds, native = false) {
if (!native) {
this._native = FIRTimestamp.timestampWithSecondsNanoseconds(seconds, nanoseconds);
}
}
static fromNative(timestamp) {
if (timestamp instanceof FIRTimestamp) {
const ts = new Timestamp(0, 0, true);
ts._native = timestamp;
return ts;
}
return null;
}
static fromDate(date) {
if (date instanceof Date) {
const ts = new Timestamp(0, 0, true);
ts._native = FIRTimestamp.timestampWithDate(date);
return ts;
}
return null;
}
static fromMillis(milliseconds) {
const ts = new Timestamp(0, 0, true);
ts._native = FIRTimestamp.timestampWithSecondsNanoseconds(milliseconds / 1000, 0);
return ts;
}
static now() {
const ts = new Timestamp(0, 0, true);
ts._native = FIRTimestamp.timestamp();
return ts;
}
get nanoseconds() {
return this.native.nanoseconds;
}
get seconds() {
return this.native.seconds;
}
isEqual(ts) {
return this.native.compare(ts.native) === 0 /* NSComparisonResult.OrderedSame */;
}
toDate() {
return this.native.dateValue();
}
toMillis() {
return this.native.seconds * 1000;
}
valueOf() {
return this.native.dateValue().valueOf().toString();
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
toJSON() {
return {
nanoseconds: this.nanoseconds,
seconds: this.seconds,
};
}
}
export class WriteBatch {
static fromNative(batch) {
if (batch instanceof FIRWriteBatch) {
const b = new WriteBatch();
b._native = batch;
return b;
}
return null;
}
commit() {
return new Promise((resolve, reject) => {
this.native.commitWithCompletion((error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}
else {
resolve();
}
});
});
}
delete(documentRef) {
return WriteBatch.fromNative(this.native.deleteDocument(documentRef.native));
}
set(documentRef, data, options) {
if (options) {
if (typeof options?.merge === 'boolean') {
return WriteBatch.fromNative(this.native.setDataForDocumentMerge(serializeItems(data), documentRef.native, options.merge));
}
if (options.mergeFields) {
if (Array.isArray(options.mergeFields)) {
if (typeof options.mergeFields[0] === 'string') {
return WriteBatch.fromNative(this.native.setDataForDocumentMergeFields(serializeItems(data), documentRef.native, options.mergeFields));
}
return WriteBatch.fromNative(this.native.setDataForDocumentMergeFields(serializeItems(data), documentRef.native, options.mergeFields.map((field) => field.native)));
}
}
return null;
}
else {
return WriteBatch.fromNative(this.native.setDataForDocument(serializeItems(data), documentRef.native));
}
}
update(documentRef, field, value, moreFieldsAndValues) {
const data = createDictionary(field, value, moreFieldsAndValues);
return WriteBatch.fromNative(this._native.updateDataForDocument(data, documentRef?.native));
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
export class Settings {
static fromNative(ffs, firestore = undefined) {
if (ffs instanceof FIRFirestoreSettings) {
const settings = new Settings();
settings._native = ffs;
settings._firestore = firestore ?? undefined;
return settings;
}
return null;
}
_updateStoreSettings() {
if (this._firestore !== undefined) {
this._firestore.settings = this.native;
}
}
get cacheSizeBytes() {
return this.native.cacheSizeBytes;
}
set cacheSizeBytes(value) {
this.native.cacheSizeBytes = value;
this._updateStoreSettings();
}
get host() {
return this.native.host;
}
set host(value) {
this.native.host = value;
this._updateStoreSettings();
}
get persistence() {
return this.native.persistenceEnabled;
}
set persistence(value) {
this.native.persistenceEnabled = value;
this._updateStoreSettings();
}
get ssl() {
return this.native.sslEnabled;
}
set ssl(value) {
this.native.sslEnabled = value;
this._updateStoreSettings();
}
toJSON() {
return {
cacheSizeBytes: this.cacheSizeBytes,
host: this.host,
ignoreUndefinedProperties: this.ignoreUndefinedProperties,
persistence: this.persistence,
ssl: this.ssl,
};
}
get ios() {
return this.native;
}
get native() {
return this._native;
}
}
export class Bytes {
static fromNative(data) {
if (data instanceof NSData) {
const nsData = new Bytes();
nsData._native = data;
return nsData;
}
return null;
}
static fromBase64String(base64) {
if (typeof base64 === 'string') {
let b64 = base64;
if (base64.startsWith('data:')) {
b64 = base64.split(',')[1];
}
const bytes = new Bytes();
bytes._native = NSData.alloc().initWithBase64EncodedStringOptions(b64, 1 /* NSDataBase64DecodingOptions.IgnoreUnknownCharacters */);
return bytes;
}
return null;
}
static fromUint8Array(array) {
if (!(array instanceof Uint8Array)) {
throw new Error('Bytes.fromUint8Array expects an instance of Uint8Array');
}
const nsData = new Bytes();
nsData._native = NSData.dataWithData(array);
return nsData;
}
toBase64() {
return this.native.base64EncodedStringWithOptions(1 /* NSDataBase64EncodingOptions.Encoding64CharacterLineLength */);
}
toUint8Array() {
return new Uint8Array(interop.bufferFromData(NSData.dataWithData(this._native)));
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
export class Firestore {
constructor(app) {
if (app) {
this._native = FIRFirestore.firestoreForApp(app.native);
}
else {
if (defaultFirestore) {
return defaultFirestore;
}
defaultFirestore = this;
this._native = FIRFirestore.firestore();
}
}
static fromNative(store) {
if (store instanceof FIRFirestore) {
const firestore = new Firestore();
firestore._native = store;
return firestore;
}
return null;
}
useEmulator(host, port) {
this.native.useEmulatorWithHostPort(host, port);
}
batch() {
return WriteBatch.fromNative(this.native.batch());
}
collection(collectionPath) {
return CollectionReference.fromNative(this.native.collectionWithPath(collectionPath));
}
clearPersistence() {
return new Promise((resolve, reject) => {
this.native.clearPersistenceWithCompletion((error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}
else {
resolve();
}
});
});
}
collectionGroup(collectionId) {
return Query.fromNative(this.native.collectionGroupWithID(collectionId));
}
disableNetwork() {
return new Promise((resolve, reject) => {
this.native.disableNetworkWithCompletion((error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}
else {
resolve();
}
});
});
}
doc(documentPath) {
return DocumentReference.fromNative(this.native.documentWithPath(documentPath));
}
enableNetwork() {
return new Promise((resolve, reject) => {
this.native.enableNetworkWithCompletion((error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}
else {
resolve();
}
});
});
}
runTransaction(updateFunction) {
return new Promise((resolve, reject) => {
this.native.runTransactionWithBlockCompletion((transaction, error) => {
const lock = NSLock.new();
let returnValue;
dispatch_async(main_queue, () => {
updateFunction(Transaction.fromNative(transaction))
.then((value) => {
returnValue = value?.native || value || null;
})
.catch((e) => {
return null;
});
});
lock.lock();
}, (done, done_error) => {
if (done_error) {
reject(FirebaseError.fromNative(done_error));
}
else {
resolve(deserializeField(done));
}
});
});
}
get settings() {
return Settings.fromNative(this.native?.settings, this.native);
}
set settings(value) {
if (this.native) {
this.native.settings = value?.native;
}
}
terminate() {
return new Promise((resolve, reject) => {
this.native.terminateWithCompletion((error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}
else {
resolve();
}
});
});
}
waitForPendingWrites() {
return new Promise((resolve, reject) => {
this.native.waitForPendingWritesWithCompletion((error) => {
if (error) {
reject(FirebaseError.fromNative(error));
}
else {
resolve();
}
});
});
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
get app() {
if (!this._app) {
// @ts-ignore
this._app = FirebaseApp.fromNative(this.native.app);
}
return this._app;
}
}
//# sourceMappingURL=index.ios.js.map