@magnetarjs/core
Version:
Magnetar core library.
81 lines (80 loc) • 3.72 kB
JavaScript
import { isFunction, isPlainObject } from 'is-what';
/**
* Extracts the PluginModuleConfig from the ModuleConfig
*
* @export
* @param {ModuleConfig} moduleConfig
* @param {string} storeName
* @returns {PluginModuleConfig}
*/
export function getPluginModuleConfig(moduleConfig, storeName) {
const { query, where, orderBy, limit, startAfter, configPerStore = {} } = moduleConfig;
const extraStoreConfig = isPlainObject(configPerStore[storeName]) ? configPerStore[storeName] : {};
return { ...extraStoreConfig, query, where, orderBy, limit, startAfter };
}
/**
* Executes 'setupModule' function per store, when the collection or doc is instantiated.
*
* @export
* @param {GlobalConfig['stores']} globalConfigStores
* @param {[string, string | undefined]} [collectionPath, docId]
* @param {ModuleConfig} moduleConfig
*/
export function executeSetupModulePerStore(globalConfigStores, [collectionPath, docId], moduleConfig) {
for (const storeName in globalConfigStores) {
const { setupModule } = globalConfigStores[storeName] ?? {};
if (isFunction(setupModule)) {
const pluginModuleConfig = getPluginModuleConfig(moduleConfig, storeName);
setupModule({ collectionPath, docId, pluginModuleConfig });
}
}
}
/** Executes the `getModuleData` function from the store specified as 'cache' */
export function getDataFromDataStore(moduleConfig, globalConfig, collectionPath, docId) {
const getModuleData = globalConfig.stores['cache']?.getModuleData;
if (!getModuleData) {
throw new Error('The data store did not provide a getModuleData function!');
}
const pluginModuleConfig = getPluginModuleConfig(moduleConfig, 'cache');
return getModuleData({ collectionPath, docId, pluginModuleConfig });
}
/** Executes the `getModuleData` function from the store specified as 'cache' */
export function getExistsFromDataStore(globalConfig, collectionPath, docId) {
const getModuleExists = globalConfig.stores['cache']?.getModuleExists;
if (!getModuleExists) {
throw new Error('The data store did not provide a getModuleExists function!');
}
return getModuleExists({ collectionPath, docId });
}
/** Executes the `getModuleCount` function from the store specified as 'cache' */
export function getCountFromDataStore(moduleConfig, globalConfig, collectionPath) {
const getModuleCount = globalConfig.stores['cache']?.getModuleCount;
if (!getModuleCount) {
throw new Error('The data store did not provide a getModuleCount function!');
}
const pluginModuleConfig = getPluginModuleConfig(moduleConfig, 'cache');
return getModuleCount({ collectionPath, pluginModuleConfig });
}
/** Executes the `getModuleAggregate` function from the store specified as 'cache' */
export function getAggregateFromDataStore(kind, moduleConfig, globalConfig, collectionPath) {
const getModuleAggregate = globalConfig.stores['cache']?.getModuleAggregate;
if (!getModuleAggregate) {
throw new Error('The data store did not provide a getModuleAggregate function!');
}
const pluginModuleConfig = getPluginModuleConfig(moduleConfig, 'cache');
return getModuleAggregate(kind, { collectionPath, pluginModuleConfig });
}
/**
* Returns an object as proxy which will execute the given functions upon prop access for the params passed here
*/
export function proxify(target, propExecutionDic) {
const dataHandler = {
get: function (target, key, proxyRef) {
if (key in propExecutionDic) {
return propExecutionDic[key]?.();
}
return Reflect.get(target, key, proxyRef);
},
};
return new Proxy(target, dataHandler);
}