Last active
July 18, 2025 14:09
-
-
Save JamieCurnow/650ea759c277757ae5665ea52400713b to your computer and use it in GitHub Desktop.
Using Firestore with Typescript - including update helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This Gist is part of a medium article - read here: | |
* https://jamiecurnow.medium.com/using-firestore-with-more-typescript-8058b6a88674 | |
*/ | |
// import firstore (obviously) | |
import { firestore } from 'firebase-admin' | |
import type { DocumentData, WithFieldValue } from 'firebase-admin/firestore' | |
// Here's the helper type for paths: | |
type PathImpl<T, K extends keyof T> = K extends string | |
? T[K] extends Record<string, any> | undefined | |
? T[K] extends ArrayLike<any> | |
? K | `${K}.${PathImpl<NonNullable<T[K]>, Exclude<keyof NonNullable<T[K]>, keyof any[]>>}` | |
: K | `${K}.${PathImpl<NonNullable<T[K]>, keyof NonNullable<T[K]>>}` | |
: K | |
: never | |
type Path<T> = PathImpl<T, keyof T> | keyof T | |
type PathValue<T, P extends Path<T>> = P extends `${infer K}.${infer Rest}` | |
? K extends keyof T | |
? Rest extends Path<NonNullable<T[K]>> | |
? PathValue<NonNullable<T[K]>, Rest> | |
: never | |
: never | |
: P extends keyof T | |
? NonNullable<T[P]> | |
: never | |
export type UpdateData<T extends object> = Partial<{ | |
[TKey in Path<T>]: PathValue<T, TKey> | |
}> | |
// Import or define your types | |
// import { User } from '~/@types' | |
interface User { | |
name: string | |
email: string | |
address: { | |
line1: string | |
line2: string | |
postcode: string | |
verified: false | |
timeAtAddress: { | |
days: string | |
months: string | |
hours: string | |
} | |
} | |
} | |
interface Post { | |
something: boolean | |
somethingElse: boolean | |
} | |
// This helper function pipes your types through a firestore converter | |
const converter = <T>() => ({ | |
toFirestore: (data: WithFieldValue<T>) => data, | |
fromFirestore: (snap: FirebaseFirestore.QueryDocumentSnapshot) => snap.data() as T | |
}) | |
// This helper function exposes a 'typed' version of firestore().collection(collectionPath) | |
// Pass it a collectionPath string as the path to the collection in firestore | |
// Pass it a type argument representing the 'type' (schema) of the docs in the collection | |
const dataPoint = <T extends WithFieldValue<DocumentData>>(collectionPath: string) => | |
firestore().collection(collectionPath).withConverter(converter<T>()) | |
// Construct a database helper object | |
const db = { | |
// list your collections here | |
users: dataPoint<User>('users'), | |
userPosts: (userId: string) => dataPoint<Post>(`users/${userId}/posts`) | |
} | |
// export your helper | |
export { db } | |
export default db | |
/** | |
* Some examples of how to use: | |
*/ | |
const example = async (id: string) => { | |
// firestore just as you know it, but with types | |
const userDoc = await db.users.doc(id).get() | |
const user = userDoc.data() | |
if (!user) return | |
const { address } = user | |
return address.line1 | |
} | |
const createExample = async (userId: string) => { | |
await db.userPosts(userId).doc().create({ | |
something: false, | |
somethingElse: true | |
}) | |
} | |
const updateExample = async (id: string) => { | |
const updates: UpdateData<User> = { | |
'address.timeAtAddress.days': '', | |
name: '' | |
} | |
await db.users.doc(id).update(updates) | |
} |
Thanks @JamieCurnow . It is a nice solution.
I have one question for User. I tried to use class User
, not interface User
, it also works. Because class
can set the default value, but interface can not, I want to init the field default value, so I choose to use class
, but the issue is that nest class can not be added into firestore.
class Post {
something: boolean = false
somethingElse: boolean = false
}
class User {
name: string = ""
email: string = ""
post: Post = new Post()
}
when I want to add new User()
to firestore, it said that:
Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the \"new\" operator).
but if I change the code to
type Post {
something: boolean
somethingElse: boolean
}
class User {
name: string = ""
email: string = ""
post: Post = {
something: boolean = false
somethingElse: boolean = false
}
it works. but this code is not very good. do you know how to do this?
I just try it again like this: it works
class Post {
something: boolean = false
somethingElse: boolean = false
}
class User {
name: string = ""
email: string = ""
post: Post = {...new Post()}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@JamieCurnow Thanks, it works perfectly now!