import { doc , collection } from 'firebase/firestore' ; import { PluginInsertAction , PluginInsertActionPayload , SyncBatch } from '@magnetarjs/core' ; import { FirestoreModuleConfig , FirestorePluginOptions } from '../CreatePlugin' ; import { isFullString , isNumber } from 'is-what' ; import { BatchSync } from '../helpers/batchSync' ; import { getFirestoreDocPath } from '../helpers/pathHelpers' ; export function insertActionFactory ( batchSync , firestorePluginOptions ) { return async function ( { payload , collectionPath , docId , actionConfig , pluginModuleConfig , } ) : Promise < [ string , SyncBatch ] > { const { db } = firestorePluginOptions ; let _docId = docId ; if ( ! _docId ) { _docId = isFullString ( payload . id ) || isNumber ( payload . id ) ? String ( payload . id ) : doc ( collection ( db , 'random' ) ) . id ; } const documentPath = getFirestoreDocPath ( collectionPath , _docId as string , pluginModuleConfig , firestorePluginOptions ) ; const syncDebounceMs = isNumber ( actionConfig . syncDebounceMs ) ? actionConfig . syncDebounceMs : pluginModuleConfig . syncDebounceMs ; const result = await batchSync . insert ( documentPath , payload , syncDebounceMs ) ; return [ _docId as string , result ] ; } ; }