From eaa308e8147b89bec9e486b48a01081c4e8ea15f Mon Sep 17 00:00:00 2001 From: Alex Demchenko Date: Fri, 10 Sep 2021 20:49:44 +0200 Subject: [PATCH] Allow collection names change --- src/types.ts | 9 +++++++++ src/useRoom.ts | 3 ++- src/useRooms.ts | 11 ++++++----- src/useUsers.ts | 5 +++-- src/utils.ts | 21 +++++++++++++++++---- 5 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/types.ts b/src/types.ts index 5f2bcc0..bf19540 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,14 @@ import { ImageURISource } from 'react-native' +/** Interface that represents the chat config. Can be used for setting custom names + * for rooms and users collections. Call `setConfig` before doing anything else if + * you want to change the default collection names. When using custom names don't forget + * to update your security rules and indexes. */ +export interface FirebaseChatCoreConfig { + roomsCollectionName: string + usersCollectionName: string +} + export namespace MessageType { export type Any = Custom | File | Image | Text | Unsupported export type PartialAny = diff --git a/src/useRoom.ts b/src/useRoom.ts index 3a0e722..f90488a 100644 --- a/src/useRoom.ts +++ b/src/useRoom.ts @@ -1,6 +1,7 @@ import firestore from '@react-native-firebase/firestore' import * as React from 'react' +import { ROOMS_COLLECTION_NAME } from '.' import { Room } from './types' import { useFirebaseUser } from './useFirebaseUser' import { processRoomDocument } from './utils' @@ -14,7 +15,7 @@ export const useRoom = (initialRoom: Room) => { if (!firebaseUser) return return firestore() - .collection('rooms') + .collection(ROOMS_COLLECTION_NAME) .doc(initialRoom.id) .onSnapshot(async (doc) => { const newRoom = await processRoomDocument({ doc, firebaseUser }) diff --git a/src/useRooms.ts b/src/useRooms.ts index dd5451b..9a4ad0a 100644 --- a/src/useRooms.ts +++ b/src/useRooms.ts @@ -1,6 +1,7 @@ import firestore from '@react-native-firebase/firestore' import * as React from 'react' +import { ROOMS_COLLECTION_NAME } from '.' import { Room, User } from './types' import { useFirebaseUser } from './useFirebaseUser' import { fetchUser, processRoomsQuery } from './utils' @@ -27,11 +28,11 @@ export const useRooms = (orderByUpdatedAt?: boolean) => { const collection = orderByUpdatedAt ? firestore() - .collection('rooms') + .collection(ROOMS_COLLECTION_NAME) .where('userIds', 'array-contains', firebaseUser.uid) .orderBy('updatedAt', 'desc') : firestore() - .collection('rooms') + .collection(ROOMS_COLLECTION_NAME) .where('userIds', 'array-contains', firebaseUser.uid) return collection.onSnapshot(async (query) => { @@ -63,7 +64,7 @@ export const useRooms = (orderByUpdatedAt?: boolean) => { const roomUsers = [currentUser].concat(users) const room = await firestore() - .collection('rooms') + .collection(ROOMS_COLLECTION_NAME) .add({ createdAt: firestore.FieldValue.serverTimestamp(), imageUrl, @@ -96,7 +97,7 @@ export const useRooms = (orderByUpdatedAt?: boolean) => { if (!firebaseUser) return const query = await firestore() - .collection('rooms') + .collection(ROOMS_COLLECTION_NAME) .where('userIds', 'array-contains', firebaseUser.uid) .get() @@ -120,7 +121,7 @@ export const useRooms = (orderByUpdatedAt?: boolean) => { const users = [currentUser].concat(otherUser) const room = await firestore() - .collection('rooms') + .collection(ROOMS_COLLECTION_NAME) .add({ createdAt: firestore.FieldValue.serverTimestamp(), imageUrl: undefined, diff --git a/src/useUsers.ts b/src/useUsers.ts index e3aeaa7..fc75f8d 100644 --- a/src/useUsers.ts +++ b/src/useUsers.ts @@ -1,6 +1,7 @@ import firestore from '@react-native-firebase/firestore' import * as React from 'react' +import { USERS_COLLECTION_NAME } from '.' import { User } from './types' import { useFirebaseUser } from './useFirebaseUser' @@ -16,11 +17,11 @@ export const useUsers = () => { } return firestore() - .collection('users') + .collection(USERS_COLLECTION_NAME) .onSnapshot((query) => { const newUsers: User[] = [] - query.forEach((doc) => { + query?.forEach((doc) => { if (firebaseUser.uid === doc.id) return const data = doc.data()! diff --git a/src/utils.ts b/src/utils.ts index 5941e7b..95b2d6b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,11 +3,21 @@ import firestore, { FirebaseFirestoreTypes, } from '@react-native-firebase/firestore' -import { Room, User } from './types' +import { FirebaseChatCoreConfig, Room, User } from './types' + +export let ROOMS_COLLECTION_NAME = 'rooms' +export let USERS_COLLECTION_NAME = 'users' + +/** Sets custom config to change default names for rooms + * and users collections. Also see {@link FirebaseChatCoreConfig}. */ +export const setConfig = (config: FirebaseChatCoreConfig) => { + ROOMS_COLLECTION_NAME = config.roomsCollectionName + USERS_COLLECTION_NAME = config.usersCollectionName +} /** Creates {@link User} in Firebase to store name and avatar used on rooms list */ export const createUserInFirestore = async (user: User) => { - await firestore().collection('users').doc(user.id).set({ + await firestore().collection(USERS_COLLECTION_NAME).doc(user.id).set({ createdAt: firestore.FieldValue.serverTimestamp(), firstName: user.firstName, imageUrl: user.imageUrl, @@ -21,12 +31,15 @@ export const createUserInFirestore = async (user: User) => { /** Removes {@link User} from `users` collection in Firebase */ export const deleteUserFromFirestore = async (userId: string) => { - await firestore().collection('users').doc(userId).delete() + await firestore().collection(USERS_COLLECTION_NAME).doc(userId).delete() } /** Fetches user from Firebase and returns a promise */ export const fetchUser = async (userId: string, role?: User['role']) => { - const doc = await firestore().collection('users').doc(userId).get() + const doc = await firestore() + .collection(USERS_COLLECTION_NAME) + .doc(userId) + .get() const data = doc.data()!