Skip to main content
Version: 2.8.y

Meta Type

This section discusses how to create parent and children MetaType.

Create Child MetaType

To create child Meta Type, pass the parent type into the 4th argument of the child. In this example, we create 3 generations of collection.

metaType.ts
import { MetaTypeCreator, getFirelord, getFirestore } from 'firelordjs'

const db = getFirestore()

export type Parent = MetaTypeCreator<
{
a: number
},
'collectionIDLevel1', // collectionId type
string, // docId type
null // you can skip this, top collection has no parent
>

export type Child = MetaTypeCreator<
{
a: 'a' | 'b' | 'c'
b: { c: number }
},
'collectionIDLevel2', // collectionId type
string, // docId type
Parent // child's parent is Parent
>

export type GrandChild = MetaTypeCreator<
{
e: { f: boolean }[]
},
'collectionIDLevel3', // collectionId type
string, // docId type
Child // grandChild's parent is child
>
firelordRef.ts
import { Parent, Child, GrandChild } from './metaType.ts'

export const parentRef = getFirelord<Parent>(
db,
'collectionIDLevel1' // parent col ID
)

export const childRef = getFirelord<Child>(
db,
'collectionIDLevel1', // parent col ID
'collectionIDLevel2' // child col ID
)

export const grandChildRef = getFirelord<GrandChild>(
db,
'collectionIDLevel1', // parent col ID
'collectionIDLevel2', // child col ID
'collectionIDLevel3' // grandChild col ID
)
doc.ts
import { parentRef, childRef, grandChildRef } from './firelordRef.ts'

// doc ref require all ANCESTORS' and its own document ID
// doc ids start from oldest ancestor's doc id till its own doc id
export const parentDocRef = parentRef.doc(
'docIDLevel1' // parent doc ID
)
export const childDocRef = childRef.doc(
'docIDLevel1', // parent doc ID
'docIDLevel2' // child doc ID
)
export const grandChildDocRef = grandChildRef.doc(
'docIDLevel1', // parent doc ID
'docIDLevel2', // child doc ID
'docIDLevel3' // grand child doc ID
)
col.ts
import { parentRef, childRef, grandChildRef } from './firelordRef.ts'

// collection ref requires only ancestors' doc id
// doc ids start from oldest ancestor's doc id till parent's doc id
export const parentColRef = parentRef.collection()
export const childColRef = childRef.collection(
'docIDLevel1' // parent doc ID
)
export const grandChildColRef = grandChildRef.collection(
'docIDLevel1', // parent doc ID
'docIDLevel2' // child doc ID
)
colGroup.ts
import { parentRef, childRef, grandChildRef } from './firelordRef.ts'

// collection group doesn't need any kind of ID.
export const parentColGroupRef = parentRef.collectionGroup()
export const childColGroupRef = childRef.collectionGroup()
export const grandChildColGroupRef = grandChildRef.collectionGroup()
tip

You only need document IDs to create docRef and collectionRef.

You only need to write collection ID when you create MetaType and FirelordRef, you never need to write any collection ID afterward.

Please don't create files like doc.ts, col.ts, and colGroup.ts, they are not practical and are for explanation only. However we encourage you to create files like metatype.ts and firelordRef.ts.

Know Your Ancestors

All children can track back all their ancestors type.

import { grandChildDocRef } from './doc.ts'
// ancestor type awareness
grandChildDocRef.parent // GrandChild CollectionReference
grandChildDocRef.parent.parent // Child DocumentReference
grandChildDocRef.parent.parent.parent // Child CollectionReference
grandChildDocRef.parent.parent.parent.parent // Parent DocumentReference
grandChildDocRef.parent.parent.parent.parent.parent // Parent CollectionReference
grandChildDocRef.parent.parent.parent.parent.parent.parent // null

Type Checking

FirelordJS check for the type and number of your collection ID and document ID.

Unknown collection ID trigger type error

Missing collection ID trigger type error

Index Accessor

Explore MetaType with index accessor, this is useful if you want to create abstraction.

Explore MetaType with index accessor.

IndexesDescriptions
basethe original type
readdata() type of getDoc,getDocs and onSnapshot operations.
readJSONstrip all methods, leave only valid JSON type. Useful if reading from local storage.
compareinput type of where,orderBy,startAt... query clauses.
writeinput type of setDoc, createDoc and addDoc operations.
writeFlatteninput type of updateDoc operation.
writeMergeinput type of setDoc merge operation.
collectionIDcollection ID type
collectionPathcollection full path type
docIDdocument ID type
docPathdocument full path type
parentParent MetaType if exist, else Null
ancestorsall ancestors MetaType in array, starting from itself, then Parent till to the oldest ancestor