I have implemented a Firestore database and defined a schema to organize my data:
type FirestoreCollection<T> = {
documentType: T;
subcollections?: {
[key: string]: FirestoreCollection<object>;
};
};
type FirestoreSchema<
T extends { [key: string]: U },
U = FirestoreCollection<object>
> = T;
export type FirestoreModel = FirestoreSchema<{
'chat-messages': {
documentType: ChatMessage;
};
chats: {
documentType: Chat;
subcollections: {
'chat-participants': {
documentType: ChatParticipant;
};
};
};
}>;
The schema is functioning as expected.
I am aiming to define a type for a function that accepts an array of arguments, where each argument follows this structure:
{ collection: string, id: string }
I want the function to detect if the current argument is not a subcollection of the previous argument and raise a type error accordingly.
In essence, I desire the function to operate in the following way:
// This should work because it's a valid root level document
getDocument(
{ collection: 'chat-messages', id: '123' }
);
// This should work as 'chat-participants' is a subcollection of 'chats'
getDocument(
{ collection: 'chats', id: '123' }
{ collection: 'chat-participants', id: '456' }
);
// This should throw an error since 'chats' is not a subcollection of 'chat-messages'
getDocument(
{ collection: 'chat-messages', id: '123' },
{ collection: 'chats', id: '456' }
);
// This should also result in an error since 'invalid' is not a collection at the root
getDocument({ collection: 'invalid', id: '123' })
Any suggestions?