Skip to content

Update to Firebase Modular SDK #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,27 @@
},
"homepage": "https://github.com/nandorojo/swr-firestore#readme",
"devDependencies": {
"@commitlint/config-conventional": "^8.3.4",
"@firebase/firestore-types": "^1.10.1",
"@commitlint/config-conventional": "^12.1.4",
"@react-native-community/bob": "^0.10.1",
"@react-native-community/eslint-config": "^0.0.7",
"@release-it/conventional-changelog": "^1.1.0",
"@types/jest": "^25.1.2",
"@react-native-community/eslint-config": "^2.0.0",
"@release-it/conventional-changelog": "^3.0.0",
"@types/jest": "^26.0.23",
"@types/lodash.get": "^4.4.6",
"@types/lodash.set": "^4.3.6",
"@types/react": "^16.9.19",
"@types/react-native": "0.61.10",
"commitlint": "^8.3.4",
"eslint": "^6.8.0",
"eslint-config-nando": "^1.0.9",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-prettier": "^3.1.2",
"firebase": "^7.14.1",
"jest": "^25.1.0",
"prettier": "^1.19.1",
"@types/react": "^17.0.10",
"@types/react-native": "0.64.10",
"commitlint": "^12.1.4",
"eslint": "^7.28.0",
"eslint-config-nando": "^1.1.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"firebase": "9.0.0-beta.2",
"jest": "^27.0.4",
"prettier": "^2.3.1",
"react": "~16.9.0",
"react-native": "~0.61.5",
"release-it": "^12.6.3",
"typescript": "^4.1.0-dev.20201015"
"react-native": "~0.64.2",
"release-it": "^14.8.0",
"typescript": "^4.3.2"
},
"peerDependencies": {
"react": "*"
Expand Down Expand Up @@ -105,6 +104,6 @@
"dependencies": {
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2",
"swr": "^0.2.0"
"swr": "^0.5.6"
}
}
}
24 changes: 5 additions & 19 deletions src/classes/Fuego.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import * as firebase from 'firebase/app'

// import 'firebase/firestore'
// import 'firebase/auth'
// import 'firebase/functions'

type Config = Parameters<typeof firebase.initializeApp>[0]

import { FirebaseOptions, getApp, getApps, initializeApp } from "firebase/app"
import { getFirestore, FirebaseFirestore } from "firebase/firestore";
export class Fuego {
public db: ReturnType<firebase.app.App['firestore']>
public auth: typeof firebase.auth
public functions: typeof firebase.functions
public storage: typeof firebase.storage
constructor(config: Config) {
this.db = !firebase.apps.length
? firebase.initializeApp(config).firestore()
: firebase.app().firestore()
this.auth = firebase.auth
this.functions = firebase.functions
this.storage = firebase.storage
public db: FirebaseFirestore
constructor(config: FirebaseOptions) {
this.db = !getApps().length ? getFirestore(initializeApp(config)) : getFirestore(getApp())
}
}
68 changes: 32 additions & 36 deletions src/hooks/static-mutations.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { mutate } from 'swr'
import { SetOptions } from '@firebase/firestore-types'
import type { SetOptions } from 'firebase/firestore'
import { doc, setDoc, updateDoc, deleteDoc } from 'firebase/firestore'
import { fuego } from '../context'
import { empty } from '../helpers/empty'
import { collectionCache } from '../classes/Cache'
import { Document } from '../types/Document'

type MergeType = {
merge?: boolean
}

/**
* Function that, when called, refreshes all queries that match this document path.
*
Expand All @@ -21,7 +28,7 @@ const revalidateDocument = (path: string) => {
*/
const revalidateCollection = (path: string) => {
const promises: Promise<any>[] = []
collectionCache.getSWRKeysFromCollectionPath(path).forEach(key => {
collectionCache.getSWRKeysFromCollectionPath(path).forEach((key) => {
promises.push(mutate(key))
})
return Promise.all(promises)
Expand All @@ -38,13 +45,7 @@ const set = <Data extends object = {}, Doc extends Document = Document<Data>>(
) => {
if (path === null) return null

const isDocument =
path
.trim()
.split('/')
.filter(Boolean).length %
2 ===
0
const isDocument = path.trim().split('/').filter(Boolean).length % 2 === 0

if (!isDocument)
throw new Error(
Expand All @@ -53,11 +54,16 @@ const set = <Data extends object = {}, Doc extends Document = Document<Data>>(
data: ${JSON.stringify(data)}`
)

const shouldMerge = (
options: SetOptions | undefined
): options is MergeType => {
return !!(options as MergeType)?.merge
}
if (!ignoreLocalMutation) {
mutate(
path,
(prevState = empty.object) => {
if (!options?.merge) return data
if (shouldMerge(options)) return data
return {
...prevState,
...data,
Expand All @@ -71,19 +77,19 @@ data: ${JSON.stringify(data)}`
const docId = collection.pop() // remove last item, which is the /doc-id
collection = collection.join('/')

collectionCache.getSWRKeysFromCollectionPath(collection).forEach(key => {
collectionCache.getSWRKeysFromCollectionPath(collection).forEach((key) => {
mutate(
key,
(currentState: Doc[] = empty.array) => {
// don't mutate the current state if it doesn't include this doc
// why? to prevent creating a new reference of the state
// creating a new reference could trigger unnecessary re-renders
if (!currentState.some(doc => doc.id === docId)) {
if (!currentState.some((doc) => doc.id === docId)) {
return currentState
}
return currentState.map((document = empty.object as Doc) => {
if (document.id === docId) {
if (!options?.merge) return document
if (shouldMerge(options)) return document
return { ...document, ...data }
}
return document
Expand All @@ -92,8 +98,10 @@ data: ${JSON.stringify(data)}`
false
)
})

return fuego.db.doc(path).set(data, options)
if (!options) {
return setDoc(doc(fuego.db, path), data)
}
return setDoc(doc(fuego.db, path), data, options)
}

const update = <
Expand All @@ -108,13 +116,7 @@ const update = <
ignoreLocalMutation = false
) => {
if (path === null) return null
const isDocument =
path
.trim()
.split('/')
.filter(Boolean).length %
2 ===
0
const isDocument = path.trim().split('/').filter(Boolean).length % 2 === 0

if (!isDocument)
throw new Error(
Expand All @@ -140,12 +142,12 @@ data: ${JSON.stringify(data)}`
const docId = collection.pop() // remove last item, which is the /doc-id
collection = collection.join('/')

collectionCache.getSWRKeysFromCollectionPath(collection).forEach(key => {
collectionCache.getSWRKeysFromCollectionPath(collection).forEach((key) => {
mutate(
key,
(currentState: Doc[] = empty.array): Doc[] => {
// don't mutate the current state if it doesn't include this doc
if (!currentState.some(doc => doc.id === docId)) {
if (!currentState.some((doc) => doc.id === docId)) {
return currentState
}
return currentState.map((document = empty.object as Doc) => {
Expand All @@ -158,7 +160,7 @@ data: ${JSON.stringify(data)}`
false
)
})
return fuego.db.doc(path).update(data)
return updateDoc(doc(fuego.db, path), data)
}

const deleteDocument = <
Expand All @@ -173,13 +175,7 @@ const deleteDocument = <
) => {
if (path === null) return null

const isDocument =
path
.trim()
.split('/')
.filter(Boolean).length %
2 ===
0
const isDocument = path.trim().split('/').filter(Boolean).length % 2 === 0

if (!isDocument)
throw new Error(
Expand All @@ -193,17 +189,17 @@ const deleteDocument = <
const docId = collection.pop() // remove last item, which is the /doc-id
collection = collection.join('/')

collectionCache.getSWRKeysFromCollectionPath(collection).forEach(key => {
collectionCache.getSWRKeysFromCollectionPath(collection).forEach((key) => {
mutate(
key,
(currentState: Doc[] = empty.array) => {
// don't mutate the current state if it doesn't include this doc
// why? to prevent creating a new reference of the state
// creating a new reference could trigger unnecessary re-renders
if (!currentState.some(doc => doc && doc.id === docId)) {
if (!currentState.some((doc) => doc && doc.id === docId)) {
return currentState
}
return currentState.filter(document => {
return currentState.filter((document) => {
if (!document) return false
if (document.id === docId) {
// delete this doc
Expand All @@ -217,7 +213,7 @@ const deleteDocument = <
})
}

return fuego.db.doc(path).delete()
return deleteDoc(doc(fuego.db, path))
}

export { set, update, revalidateDocument, revalidateCollection, deleteDocument }
Loading