@mgarlik/datastore 0.1.22 → 0.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1939,78 +1939,66 @@ var DataStoreProvider_default = DataStoreProvider;
1939
1939
 
1940
1940
  // src/useDataProvider.tsx
1941
1941
  var import_react2 = require("react");
1942
- var getDynamicOptionsConfig = (schema) => {
1943
- if (!schema) return [];
1944
- const configs = [];
1945
- Object.entries(schema).forEach(([fieldKey, schemaItem]) => {
1946
- if (!schemaItem || typeof schemaItem !== "object") return;
1947
- if (schemaItem.options && !Array.isArray(schemaItem.options) && typeof schemaItem.options === "object" && schemaItem.options.dataProvider) {
1948
- configs.push({
1949
- fieldKey,
1950
- dataProvider: schemaItem.options.dataProvider,
1951
- optionKeys: schemaItem.options.optionKeys,
1952
- filter: schemaItem.options.filter
1953
- });
1954
- return;
1955
- }
1956
- if (schemaItem.dataSource?.dataProvider) {
1957
- configs.push({
1958
- fieldKey,
1959
- dataProvider: schemaItem.dataSource.dataProvider,
1960
- optionKeys: schemaItem.optionKeys,
1961
- filter: schemaItem.filter
1962
- });
1963
- }
1964
- });
1965
- return configs;
1942
+ var import_json_filter = require("@mgarlik/json-filter");
1943
+ var findAllDataProviders = (obj) => {
1944
+ if (!obj || typeof obj !== "object") return [];
1945
+ if (obj.dataSource?.dataProvider) return [obj.dataSource.dataProvider];
1946
+ if (Array.isArray(obj)) return obj.flatMap(findAllDataProviders);
1947
+ return Object.values(obj).flatMap(findAllDataProviders);
1966
1948
  };
1967
1949
  var resolveProviderRef = (providerId, providers, presetProviders, params) => {
1968
1950
  if (providers[providerId]) {
1969
- return {
1970
- templateId: providerId,
1971
- runtimeId: providerId
1972
- };
1951
+ return { templateId: providerId, runtimeId: providerId };
1973
1952
  }
1974
1953
  if (presetProviders[providerId]) {
1975
1954
  const renderedProvider = renderJSONTemplate(presetProviders[providerId], params ?? void 0);
1976
- return {
1977
- templateId: providerId,
1978
- runtimeId: renderedProvider.id
1979
- };
1955
+ return { templateId: providerId, runtimeId: renderedProvider.id };
1980
1956
  }
1981
- return {
1982
- templateId: providerId,
1983
- runtimeId: providerId
1984
- };
1957
+ return { templateId: providerId, runtimeId: providerId };
1985
1958
  };
1986
- var createOptionFromDocument = (doc, optionKeys) => {
1987
- const valuePath = optionKeys?.value || "id";
1988
- const optionValue = getValueByDotPath(doc, valuePath, doc.id);
1989
- const labelDefinition = optionKeys?.label;
1990
- let optionLabel = "";
1991
- let optionIcon;
1992
- if (typeof labelDefinition === "string") {
1993
- optionLabel = String(getValueByDotPath(doc, labelDefinition, optionValue ?? ""));
1994
- } else if (labelDefinition && typeof labelDefinition === "object") {
1995
- const labelPath = labelDefinition.title || "name";
1996
- optionLabel = String(getValueByDotPath(doc, labelPath, optionValue ?? ""));
1997
- if (labelDefinition.icon) {
1998
- optionIcon = getValueByDotPath(doc, labelDefinition.icon);
1959
+ var createOptionFromDocument = (doc, map) => {
1960
+ if (!map) {
1961
+ return { ...doc };
1962
+ }
1963
+ return Object.entries(map).reduce(
1964
+ (acc, [targetKey, sourcePath]) => {
1965
+ if (typeof sourcePath === "string") {
1966
+ acc[targetKey] = getValueByDotPath(doc, sourcePath);
1967
+ } else if (sourcePath && typeof sourcePath === "object") {
1968
+ const titlePath = sourcePath.title || sourcePath.name;
1969
+ if (titlePath) acc[targetKey] = getValueByDotPath(doc, titlePath);
1970
+ if (sourcePath.icon) acc[`${targetKey}Icon`] = getValueByDotPath(doc, sourcePath.icon);
1971
+ }
1972
+ return acc;
1973
+ },
1974
+ {}
1975
+ );
1976
+ };
1977
+ var resolveDataSourcesDeep = (obj, providers, documents, presetProviders, params, logLabel = "options") => {
1978
+ if (!obj || typeof obj !== "object") return obj;
1979
+ if (obj.dataSource?.dataProvider) {
1980
+ const providerRef = resolveProviderRef(obj.dataSource.dataProvider, providers, presetProviders, params);
1981
+ const optionProvider = providers[providerRef.runtimeId];
1982
+ if (!optionProvider) {
1983
+ systemLog("usedp", `Nenalezen provider pro ${logLabel}`, obj.dataSource.dataProvider);
1984
+ return obj;
1999
1985
  }
2000
- } else {
2001
- optionLabel = String(optionValue ?? "");
1986
+ let optionDocuments = (optionProvider.data || []).map((docId) => {
1987
+ const docData = documents[docId];
1988
+ if (!docData) return null;
1989
+ return { id: docId, ...docData };
1990
+ }).filter((item) => !!item);
1991
+ if (obj.dataSource.filter) {
1992
+ optionDocuments = (0, import_json_filter.filterJsonDocuments)(optionDocuments, obj.dataSource.filter);
1993
+ }
1994
+ return optionDocuments.map((item) => createOptionFromDocument(item, obj.dataSource.map));
2002
1995
  }
2003
- if (optionIcon) {
2004
- return {
2005
- value: optionValue,
2006
- label: optionLabel,
2007
- icon: optionIcon
2008
- };
1996
+ if (Array.isArray(obj)) {
1997
+ return obj.map((item) => resolveDataSourcesDeep(item, providers, documents, presetProviders, params, logLabel));
2009
1998
  }
2010
- return {
2011
- value: optionValue,
2012
- label: optionLabel
2013
- };
1999
+ return Object.fromEntries(
2000
+ Object.entries(obj).map(([key, val]) => [key, resolveDataSourcesDeep(val, providers, documents, presetProviders, params, logLabel)])
2001
+ );
2014
2002
  };
2015
2003
  var useDataProvider = (id, params, dataFilter, settings) => {
2016
2004
  const [providerId, setProviderId] = (0, import_react2.useState)(null);
@@ -2041,12 +2029,13 @@ var useDataProvider = (id, params, dataFilter, settings) => {
2041
2029
  );
2042
2030
  const dependentProviders = (0, import_react2.useMemo)(() => {
2043
2031
  const providers = getExternalProviders();
2044
- if (!providerId || !providers[providerId]?.schema) return [];
2032
+ if (!providerId || !providers[providerId]) return [];
2045
2033
  const schema = providers[providerId]?.schema;
2046
- const optionConfigs = getDynamicOptionsConfig(schema);
2034
+ const layout = providers[providerId]?.layout;
2035
+ const allProviderIds = [...findAllDataProviders(schema), ...findAllDataProviders(layout)];
2047
2036
  const uniqueProviders = {};
2048
- optionConfigs.forEach((cfg) => {
2049
- const resolved = resolveProviderRef(cfg.dataProvider, providers, presetProviders, params);
2037
+ allProviderIds.forEach((dataProvider) => {
2038
+ const resolved = resolveProviderRef(dataProvider, providers, presetProviders, params);
2050
2039
  if (resolved.runtimeId === providerId) return;
2051
2040
  uniqueProviders[resolved.runtimeId] = resolved;
2052
2041
  });
@@ -2161,7 +2150,7 @@ var useDataProvider = (id, params, dataFilter, settings) => {
2161
2150
  dispatch("deleteProviderItem", { providerId, data, params: params2 }, callback);
2162
2151
  };
2163
2152
  const createItem = (data, callback) => {
2164
- return new Promise((resolve, reject) => {
2153
+ return new Promise((resolve) => {
2165
2154
  const wrappedCallback = (result) => {
2166
2155
  callback?.(result);
2167
2156
  resolve(result);
@@ -2174,10 +2163,7 @@ var useDataProvider = (id, params, dataFilter, settings) => {
2174
2163
  const providers = getExternalProviders();
2175
2164
  const documents = getExternalDocuments();
2176
2165
  if (!providerId || !providers[providerId]) {
2177
- return {
2178
- data: [],
2179
- schema: void 0
2180
- };
2166
+ return { data: [], schema: void 0, layout: void 0 };
2181
2167
  }
2182
2168
  let docs = [];
2183
2169
  providers[providerId]?.data.forEach((docId) => {
@@ -2189,54 +2175,23 @@ var useDataProvider = (id, params, dataFilter, settings) => {
2189
2175
  } else if (settings?.document) {
2190
2176
  dData = recalculateObjectWithDocument(documents[docId], settings.document);
2191
2177
  }
2192
- docs.push({
2193
- id: docId,
2194
- ...dData
2195
- });
2178
+ docs.push({ id: docId, ...dData });
2196
2179
  });
2197
2180
  systemLog("usedp", "KonecVytvoreni dat: ", providerId);
2198
2181
  if (filter) {
2199
2182
  systemLog("usedp", "Jdu filterovat: ", JSON.stringify(filter));
2200
- docs = filterDocuments(docs, filter);
2183
+ docs = (0, import_json_filter.filterJsonDocuments)(docs, filter);
2201
2184
  systemLog("usedp", "Konec filteru: ");
2202
2185
  }
2203
2186
  const schema = providers[providerId].schema;
2204
- let resolvedSchema = schema;
2205
- if (schema) {
2206
- const optionConfigs = getDynamicOptionsConfig(schema);
2207
- if (optionConfigs.length > 0) {
2208
- resolvedSchema = { ...schema };
2209
- optionConfigs.forEach((cfg) => {
2210
- const providerRef = resolveProviderRef(cfg.dataProvider, providers, presetProviders, params);
2211
- const optionProvider = providers[providerRef.runtimeId];
2212
- if (!optionProvider) {
2213
- systemLog("usedp", "Nenalezen provider pro schema.options", cfg.dataProvider);
2214
- return;
2215
- }
2216
- let optionDocuments = (optionProvider.data || []).map((docId) => {
2217
- const docData = documents[docId];
2218
- if (!docData) return null;
2219
- return {
2220
- id: docId,
2221
- ...docData
2222
- };
2223
- }).filter((item) => !!item);
2224
- if (cfg.filter) {
2225
- optionDocuments = filterDocuments(optionDocuments, cfg.filter);
2226
- }
2227
- const computedOptions = optionDocuments.map((item) => createOptionFromDocument(item, cfg.optionKeys));
2228
- const nextItem = {
2229
- ...resolvedSchema[cfg.fieldKey],
2230
- options: computedOptions
2231
- };
2232
- resolvedSchema[cfg.fieldKey] = nextItem;
2233
- });
2234
- }
2235
- }
2187
+ const layout = providers[providerId].layout;
2188
+ const resolvedSchema = resolveDataSourcesDeep(schema, providers, documents, presetProviders, params, "schema");
2189
+ const resolvedLayout = resolveDataSourcesDeep(layout, providers, documents, presetProviders, params, "layout");
2236
2190
  return {
2237
2191
  ...providers[providerId],
2238
2192
  data: docs,
2239
- schema: resolvedSchema
2193
+ schema: resolvedSchema,
2194
+ layout: resolvedLayout
2240
2195
  };
2241
2196
  }, [
2242
2197
  providerId,
@@ -2251,11 +2206,9 @@ var useDataProvider = (id, params, dataFilter, settings) => {
2251
2206
  ]);
2252
2207
  systemLog("usedp", `${providerId} END`);
2253
2208
  return {
2254
- // provider,
2255
2209
  documents: provider.data,
2256
2210
  schema: provider.schema,
2257
2211
  layout: provider.layout,
2258
- // dispatch,
2259
2212
  update,
2260
2213
  updateItem,
2261
2214
  deleteItem,