@magnetarjs/plugin-firestore-admin
Version:
Magnetar plugin firestore-admin
26 lines (25 loc) • 1.44 kB
JavaScript
import { batchSyncFactory, getFirestoreDocPath, } from '@magnetarjs/utils-firestore';
import { mapGetOrSet } from 'getorset-anything';
import { isFullString, isNumber } from 'is-what';
import { applySyncBatch, createWriteBatch } from '../helpers/batchHelpers.js';
export function insertActionFactory(batchSyncMap, firestorePluginOptions) {
return async function ({ payload, collectionPath, docId, actionConfig, pluginModuleConfig, }) {
const { db } = firestorePluginOptions;
let _docId = docId;
if (!_docId) {
// we don't have a _docId, so we need to retrieve it from the payload or generate one
_docId = isFullString(payload['id'])
? payload['id']
: isNumber(payload['id'])
? `${payload['id']}`
: db.collection('random').doc().id;
}
const documentPath = getFirestoreDocPath(collectionPath, _docId, pluginModuleConfig, firestorePluginOptions); // prettier-ignore
const syncDebounceMs = isNumber(actionConfig.syncDebounceMs)
? actionConfig.syncDebounceMs
: pluginModuleConfig.syncDebounceMs;
const batchSync = mapGetOrSet(batchSyncMap, collectionPath, () => batchSyncFactory(firestorePluginOptions, createWriteBatch, applySyncBatch));
const result = await batchSync.insert(documentPath, payload, syncDebounceMs);
return [_docId, result];
};
}