@magnetarjs/core
Version:
Magnetar core library.
88 lines (87 loc) • 4.2 kB
JavaScript
import { merge, mergeAndConcat } from 'merge-anything';
import { executeSetupModulePerStore, getAggregateFromDataStore, getCountFromDataStore, getDataFromDataStore, proxify, } from './helpers/moduleHelpers.js';
import { handleFetchPerStore, } from './moduleActions/handleFetchPerStore.js';
import { handleStreamPerStore } from './moduleActions/handleStreamPerStore.js';
import { handleWritePerStore, } from './moduleActions/handleWritePerStore.js';
export function createCollectionWithContext(collectionPath, moduleConfig, globalConfig, docFn, collectionFn, streamAndFetchPromises, fetchMeta) {
const { writeLockMap, fetchPromises, cacheStream, streaming, closeStream, closeAllStreams } = streamAndFetchPromises; // prettier-ignore
const id = collectionPath.split('/').slice(-1)[0] ?? '';
const path = collectionPath;
const doc = ((docId, _moduleConfig = {}) => {
return docFn(`${path}/${docId}`, merge(moduleConfig, _moduleConfig));
});
const writeParams = {
collectionPath,
_docId: undefined,
moduleConfig,
globalConfig,
writeLockMap,
docFn,
collectionFn,
};
const fetchParams = {
collectionPath,
_docId: undefined,
moduleConfig,
globalConfig,
fetchPromises,
writeLockMap,
docFn,
collectionFn,
setLastFetched: fetchMeta.set,
};
const insert = handleWritePerStore(writeParams, 'insert'); //prettier-ignore
const _delete = handleWritePerStore(writeParams, 'delete'); //prettier-ignore
const fetch = handleFetchPerStore(fetchParams, 'fetch'); //prettier-ignore
const fetchCount = handleFetchPerStore(fetchParams, 'fetchCount'); // prettier-ignore
const fetchSum = handleFetchPerStore(fetchParams, 'fetchSum'); // prettier-ignore
const fetchAverage = handleFetchPerStore(fetchParams, 'fetchAverage'); // prettier-ignore
const stream = handleStreamPerStore([collectionPath, undefined], moduleConfig, globalConfig, 'write', streaming, cacheStream, writeLockMap); // prettier-ignore
const actions = { stream, fetch, fetchCount, fetchSum, fetchAverage, insert, delete: _delete };
// Every store will have its 'setupModule' function executed
executeSetupModulePerStore(globalConfig.stores, [collectionPath, undefined], moduleConfig);
function query(query) {
const moduleConfigWithClause = mergeAndConcat(moduleConfig, { query: [query] });
return collectionFn(path, moduleConfigWithClause);
}
function where(fieldPath, operator, value) {
const whereClause = [fieldPath, operator, value];
const moduleConfigWithClause = mergeAndConcat(moduleConfig, { where: [whereClause] });
return collectionFn(path, moduleConfigWithClause);
}
function orderBy(fieldPath, direction = 'asc') {
const orderByClause = [fieldPath, direction];
const moduleConfigWithClause = mergeAndConcat(moduleConfig, { orderBy: [orderByClause] });
return collectionFn(path, moduleConfigWithClause);
}
function limit(limitCount) {
return collectionFn(path, { ...moduleConfig, limit: limitCount });
}
function startAfter(...values) {
if (values[0] === undefined)
return collectionFn(path, moduleConfig);
const isDoc = values[0] && typeof values[0] === 'object';
return collectionFn(path, {
...moduleConfig,
startAfter: isDoc ? values[0] : values,
});
}
const queryFns = { query, where, orderBy, limit, startAfter };
const moduleInstance = {
doc,
id,
path,
streaming,
closeStream,
closeAllStreams,
...actions,
...queryFns,
};
return proxify(moduleInstance, {
count: () => getCountFromDataStore(moduleConfig, globalConfig, collectionPath),
sum: () => getAggregateFromDataStore('sum', moduleConfig, globalConfig, collectionPath),
average: () => getAggregateFromDataStore('average', moduleConfig, globalConfig, collectionPath),
data: () => getDataFromDataStore(moduleConfig, globalConfig, collectionPath),
fetched: fetchMeta.get,
});
}