2017-09-28 12:48:28 +00:00
|
|
|
#import "RNFirebaseFirestore.h"
|
|
|
|
|
2017-10-03 16:23:28 +00:00
|
|
|
#if __has_include(<FirebaseFirestore/FirebaseFirestore.h>)
|
2017-09-28 12:48:28 +00:00
|
|
|
|
|
|
|
#import <Firebase.h>
|
|
|
|
#import "RNFirebaseEvents.h"
|
|
|
|
#import "RNFirebaseFirestoreCollectionReference.h"
|
|
|
|
#import "RNFirebaseFirestoreDocumentReference.h"
|
|
|
|
|
|
|
|
@implementation RNFirebaseFirestore
|
|
|
|
RCT_EXPORT_MODULE();
|
|
|
|
|
|
|
|
- (id)init {
|
|
|
|
self = [super init];
|
|
|
|
if (self != nil) {
|
2017-10-03 16:23:28 +00:00
|
|
|
|
2017-09-28 12:48:28 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(collectionGet:(NSString *) appName
|
|
|
|
path:(NSString *) path
|
|
|
|
filters:(NSArray *) filters
|
|
|
|
orders:(NSArray *) orders
|
|
|
|
options:(NSDictionary *) options
|
|
|
|
resolver:(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock) reject) {
|
|
|
|
[[self getCollectionForAppPath:appName path:path filters:filters orders:orders options:options] get:resolve rejecter:reject];
|
|
|
|
}
|
|
|
|
|
2017-10-03 09:12:25 +00:00
|
|
|
RCT_EXPORT_METHOD(collectionOffSnapshot:(NSString *) appName
|
|
|
|
path:(NSString *) path
|
|
|
|
filters:(NSArray *) filters
|
|
|
|
orders:(NSArray *) orders
|
|
|
|
options:(NSDictionary *) options
|
|
|
|
listenerId:(nonnull NSString *) listenerId) {
|
|
|
|
[RNFirebaseFirestoreCollectionReference offSnapshot:listenerId];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(collectionOnSnapshot:(NSString *) appName
|
|
|
|
path:(NSString *) path
|
|
|
|
filters:(NSArray *) filters
|
|
|
|
orders:(NSArray *) orders
|
|
|
|
options:(NSDictionary *) options
|
|
|
|
listenerId:(nonnull NSString *) listenerId) {
|
|
|
|
RNFirebaseFirestoreCollectionReference *ref = [self getCollectionForAppPath:appName path:path filters:filters orders:orders options:options];
|
|
|
|
[ref onSnapshot:listenerId];
|
|
|
|
}
|
|
|
|
|
2017-09-28 12:48:28 +00:00
|
|
|
RCT_EXPORT_METHOD(documentBatch:(NSString *) appName
|
|
|
|
writes:(NSArray *) writes
|
|
|
|
commitOptions:(NSDictionary *) commitOptions
|
|
|
|
resolver:(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock) reject) {
|
|
|
|
FIRFirestore *firestore = [RNFirebaseFirestore getFirestoreForApp:appName];
|
|
|
|
FIRWriteBatch *batch = [firestore batch];
|
2017-10-03 16:23:28 +00:00
|
|
|
|
2017-09-28 12:48:28 +00:00
|
|
|
for (NSDictionary *write in writes) {
|
|
|
|
NSString *type = write[@"type"];
|
|
|
|
NSString *path = write[@"path"];
|
|
|
|
NSDictionary *data = write[@"data"];
|
2017-10-03 16:23:28 +00:00
|
|
|
|
2017-09-28 12:48:28 +00:00
|
|
|
FIRDocumentReference *ref = [firestore documentWithPath:path];
|
2017-10-03 16:23:28 +00:00
|
|
|
|
2017-09-28 12:48:28 +00:00
|
|
|
if ([type isEqualToString:@"DELETE"]) {
|
|
|
|
batch = [batch deleteDocument:ref];
|
|
|
|
} else if ([type isEqualToString:@"SET"]) {
|
|
|
|
NSDictionary *options = write[@"options"];
|
|
|
|
if (options && options[@"merge"]) {
|
|
|
|
batch = [batch setData:data forDocument:ref options:[FIRSetOptions merge]];
|
|
|
|
} else {
|
|
|
|
batch = [batch setData:data forDocument:ref];
|
|
|
|
}
|
|
|
|
} else if ([type isEqualToString:@"UPDATE"]) {
|
|
|
|
batch = [batch updateData:data forDocument:ref];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[batch commitWithCompletion:^(NSError * _Nullable error) {
|
|
|
|
if (error) {
|
|
|
|
[RNFirebaseFirestore promiseRejectException:reject error:error];
|
|
|
|
} else {
|
|
|
|
NSMutableArray *result = [[NSMutableArray alloc] init];
|
|
|
|
for (NSDictionary *write in writes) {
|
|
|
|
// Missing fields from web SDK
|
|
|
|
// writeTime
|
|
|
|
[result addObject:@{}];
|
|
|
|
}
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(documentCollections:(NSString *) appName
|
|
|
|
path:(NSString *) path
|
|
|
|
resolver:(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock) reject) {
|
|
|
|
[[self getDocumentForAppPath:appName path:path] get:resolve rejecter:reject];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(documentCreate:(NSString *) appName
|
|
|
|
path:(NSString *) path
|
|
|
|
data:(NSDictionary *) data
|
|
|
|
resolver:(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock) reject) {
|
|
|
|
[[self getDocumentForAppPath:appName path:path] create:data resolver:resolve rejecter:reject];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(documentDelete:(NSString *) appName
|
|
|
|
path:(NSString *) path
|
|
|
|
options:(NSDictionary *) options
|
|
|
|
resolver:(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock) reject) {
|
|
|
|
[[self getDocumentForAppPath:appName path:path] delete:options resolver:resolve rejecter:reject];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(documentGet:(NSString *) appName
|
|
|
|
path:(NSString *) path
|
|
|
|
resolver:(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock) reject) {
|
|
|
|
[[self getDocumentForAppPath:appName path:path] get:resolve rejecter:reject];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(documentGetAll:(NSString *) appName
|
|
|
|
documents:(NSString *) documents
|
|
|
|
resolver:(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock) reject) {
|
|
|
|
// Not supported on iOS out of the box
|
|
|
|
}
|
|
|
|
|
2017-10-02 14:45:07 +00:00
|
|
|
RCT_EXPORT_METHOD(documentOffSnapshot:(NSString *) appName
|
|
|
|
path:(NSString *) path
|
2017-10-03 09:12:25 +00:00
|
|
|
listenerId:(nonnull NSString *) listenerId) {
|
|
|
|
[RNFirebaseFirestoreDocumentReference offSnapshot:listenerId];
|
2017-10-02 14:45:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(documentOnSnapshot:(NSString *) appName
|
|
|
|
path:(NSString *) path
|
2017-10-03 09:12:25 +00:00
|
|
|
listenerId:(nonnull NSString *) listenerId) {
|
|
|
|
RNFirebaseFirestoreDocumentReference *ref = [self getDocumentForAppPath:appName path:path];
|
2017-10-02 14:45:07 +00:00
|
|
|
[ref onSnapshot:listenerId];
|
|
|
|
}
|
|
|
|
|
2017-09-28 12:48:28 +00:00
|
|
|
RCT_EXPORT_METHOD(documentSet:(NSString *) appName
|
|
|
|
path:(NSString *) path
|
|
|
|
data:(NSDictionary *) data
|
|
|
|
options:(NSDictionary *) options
|
|
|
|
resolver:(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock) reject) {
|
|
|
|
[[self getDocumentForAppPath:appName path:path] set:data options:options resolver:resolve rejecter:reject];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(documentUpdate:(NSString *) appName
|
|
|
|
path:(NSString *) path
|
|
|
|
data:(NSDictionary *) data
|
|
|
|
resolver:(RCTPromiseResolveBlock) resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock) reject) {
|
|
|
|
[[self getDocumentForAppPath:appName path:path] update:data resolver:resolve rejecter:reject];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* INTERNALS/UTILS
|
|
|
|
*/
|
|
|
|
+ (void)promiseRejectException:(RCTPromiseRejectBlock)reject error:(NSError *)error {
|
2017-10-02 14:45:07 +00:00
|
|
|
NSDictionary *jsError = [RNFirebaseFirestore getJSError:error];
|
|
|
|
reject([jsError valueForKey:@"code"], [jsError valueForKey:@"message"], error);
|
2017-09-28 12:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (FIRFirestore *)getFirestoreForApp:(NSString *)appName {
|
|
|
|
FIRApp *app = [FIRApp appNamed:appName];
|
|
|
|
return [FIRFirestore firestoreForApp:app];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (RNFirebaseFirestoreCollectionReference *)getCollectionForAppPath:(NSString *)appName path:(NSString *)path filters:(NSArray *)filters orders:(NSArray *)orders options:(NSDictionary *)options {
|
2017-10-03 09:12:25 +00:00
|
|
|
return [[RNFirebaseFirestoreCollectionReference alloc] initWithPathAndModifiers:self app:appName path:path filters:filters orders:orders options:options];
|
2017-10-02 14:45:07 +00:00
|
|
|
}
|
|
|
|
|
2017-09-28 12:48:28 +00:00
|
|
|
- (RNFirebaseFirestoreDocumentReference *)getDocumentForAppPath:(NSString *)appName path:(NSString *)path {
|
2017-10-02 14:45:07 +00:00
|
|
|
return [[RNFirebaseFirestoreDocumentReference alloc] initWithPath:self app:appName path:path];
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Move to error util for use in other modules
|
|
|
|
+ (NSString *)getMessageWithService:(NSString *)message service:(NSString *)service fullCode:(NSString *)fullCode {
|
|
|
|
return [NSString stringWithFormat:@"%@: %@ (%@).", service, message, [fullCode lowercaseString]];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *)getCodeWithService:(NSString *)service code:(NSString *)code {
|
|
|
|
return [NSString stringWithFormat:@"%@/%@", [service lowercaseString], [code lowercaseString]];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSDictionary *)getJSError:(NSError *)nativeError {
|
|
|
|
NSMutableDictionary *errorMap = [[NSMutableDictionary alloc] init];
|
|
|
|
[errorMap setValue:@(nativeError.code) forKey:@"nativeErrorCode"];
|
|
|
|
[errorMap setValue:[nativeError localizedDescription] forKey:@"nativeErrorMessage"];
|
2017-10-03 16:23:28 +00:00
|
|
|
|
2017-10-02 14:45:07 +00:00
|
|
|
NSString *code;
|
|
|
|
NSString *message;
|
|
|
|
NSString *service = @"Firestore";
|
2017-10-03 16:23:28 +00:00
|
|
|
|
2017-10-02 14:45:07 +00:00
|
|
|
// TODO: Proper error codes
|
|
|
|
switch (nativeError.code) {
|
|
|
|
default:
|
|
|
|
code = [RNFirebaseFirestore getCodeWithService:service code:@"unknown"];
|
|
|
|
message = [RNFirebaseFirestore getMessageWithService:@"An unknown error occurred." service:service fullCode:code];
|
|
|
|
break;
|
|
|
|
}
|
2017-10-03 16:23:28 +00:00
|
|
|
|
2017-10-02 14:45:07 +00:00
|
|
|
[errorMap setValue:code forKey:@"code"];
|
|
|
|
[errorMap setValue:message forKey:@"message"];
|
2017-10-03 16:23:28 +00:00
|
|
|
|
2017-10-02 14:45:07 +00:00
|
|
|
return errorMap;
|
2017-09-28 12:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray<NSString *> *)supportedEvents {
|
2017-10-02 14:45:07 +00:00
|
|
|
return @[FIRESTORE_COLLECTION_SYNC_EVENT, FIRESTORE_DOCUMENT_SYNC_EVENT];
|
2017-09-28 12:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#else
|
|
|
|
@implementation RNFirebaseFirestore
|
|
|
|
@end
|
|
|
|
#endif
|