@puls-atlas/cli
Version:
The Puls Atlas CLI tool for managing Atlas projects
51 lines • 2.9 kB
JavaScript
import { normalizeOptionalString } from '../../utils/value.js';
const DEFAULT_SEARCH_EVENTARC_DATABASE = '(default)';
const DEFAULT_SEARCH_EVENTARC_EVENT_TYPE = 'google.cloud.firestore.document.v1.written';
const DEFAULT_SEARCH_EVENTARC_PATH_PREFIX = '/events/firestore';
const DEFAULT_SEARCH_EVENTARC_TRIGGER_SERVICE_ACCOUNT_ID = 'atlas-search-eventarc';
const DEFAULT_SEARCH_EVENTARC_TRIGGER_SERVICE_ACCOUNT_DISPLAY_NAME = 'Atlas Search Eventarc Trigger';
const normalizePathPattern = value => {
const normalizedValue = normalizeOptionalString(value);
if (!normalizedValue) {
return null;
}
return normalizedValue.replace(/^\/+/, '');
};
const toTriggerSlug = value => String(value).trim().toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/-+/g, '-').replace(/^-+|-+$/g, '');
const truncateTriggerName = value => value.slice(0, 63).replace(/-+$/g, '');
export const resolveSearchCollectionSourcePathPattern = (collectionName, collectionConfig) => normalizePathPattern(collectionConfig?.sourcePathPattern) ?? `${collectionName}/*`;
export const createSearchEventarcServicePath = (collectionName, pathPrefix = DEFAULT_SEARCH_EVENTARC_PATH_PREFIX) => {
const normalizedPathPrefix = normalizeOptionalString(pathPrefix) ?? DEFAULT_SEARCH_EVENTARC_PATH_PREFIX;
return `${normalizedPathPrefix.replace(/\/+$/g, '')}/${encodeURIComponent(collectionName)}`;
};
export const createSearchEventarcTriggerName = (collectionName, namePrefix = 'atlas-search') => {
const slug = toTriggerSlug(collectionName) || 'collection';
const normalizedNamePrefix = toTriggerSlug(namePrefix) || 'atlas-search';
return truncateTriggerName(`${normalizedNamePrefix}-${slug}-sync`);
};
export const getEnabledSearchEventarcCollectionPlans = searchConfig => {
const collections = searchConfig?.collections ?? {};
const indexes = searchConfig?.indexes ?? {};
return Object.entries(collections).filter(([, collectionConfig]) => collectionConfig?.enabled !== false).map(([collectionName, collectionConfig]) => ({
collectionConfig,
collectionName,
validIndexNames: (collectionConfig?.indexes ?? []).filter(indexName => indexes[indexName])
})).filter(({
validIndexNames
}) => validIndexNames.length > 0).map(({
collectionConfig,
collectionName
}) => ({
collectionName,
documentPathPattern: resolveSearchCollectionSourcePathPattern(collectionName, collectionConfig),
servicePath: createSearchEventarcServicePath(collectionName),
triggerName: createSearchEventarcTriggerName(collectionName)
}));
};
export const SEARCH_EVENTARC_DEFAULTS = {
database: DEFAULT_SEARCH_EVENTARC_DATABASE,
eventType: DEFAULT_SEARCH_EVENTARC_EVENT_TYPE,
pathPrefix: DEFAULT_SEARCH_EVENTARC_PATH_PREFIX,
triggerServiceAccountDisplayName: DEFAULT_SEARCH_EVENTARC_TRIGGER_SERVICE_ACCOUNT_DISPLAY_NAME,
triggerServiceAccountId: DEFAULT_SEARCH_EVENTARC_TRIGGER_SERVICE_ACCOUNT_ID
};