UNPKG

@magnetarjs/utils-firestore

Version:
37 lines (36 loc) 1.73 kB
import { isCollectionModule } from '@magnetarjs/utils'; import { isFullString } from 'is-what'; import { throwIfInvalidFirestorePath } from './throwFns.js'; export function getFirestoreDocPath(collectionPath, docId, firestoreModuleConfig, firestorePluginOptions) { let documentPath; // if firestorePath is set on the module level, always return this const { firestorePath } = firestoreModuleConfig; if (isFullString(firestorePath)) { documentPath = isCollectionModule(firestorePath) ? [firestorePath, docId].join('/') : firestorePath; } else { // else, return the modulePath only if this option is enabled in the global firestorePluginOptions const { useModulePathsForFirestore } = firestorePluginOptions; const modulePath = [collectionPath, docId].join('/'); documentPath = (useModulePathsForFirestore ? modulePath : firestorePath); } throwIfInvalidFirestorePath(documentPath, 'doc'); return documentPath; } export function getFirestoreCollectionPath(_collectionPath, firestoreModuleConfig, firestorePluginOptions) { let collectionPath; // if firestorePath is set on the module level, always return this const { firestorePath } = firestoreModuleConfig; if (isFullString(firestorePath)) { collectionPath = firestorePath; } else { // else, return the modulePath only if this option is enabled in the global firestorePluginOptions const { useModulePathsForFirestore } = firestorePluginOptions; collectionPath = (useModulePathsForFirestore ? _collectionPath : firestorePath); } throwIfInvalidFirestorePath(collectionPath, 'collection'); return collectionPath; }