Skip to content

Commit

Permalink
Allow collection names change
Browse files Browse the repository at this point in the history
  • Loading branch information
demchenkoalex committed Sep 10, 2021
1 parent 066fc2d commit eaa308e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down
3 changes: 2 additions & 1 deletion src/useRoom.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 })
Expand Down
11 changes: 6 additions & 5 deletions src/useRooms.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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) => {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/useUsers.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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()!
Expand Down
21 changes: 17 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()!

Expand Down

0 comments on commit eaa308e

Please sign in to comment.