@mgarlik/datastore 0.1.21 → 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
@@ -702,8 +702,6 @@ var buildUri = ({
702
702
  return dataSource.uri || `${dataSource.model || ""}/`;
703
703
  };
704
704
  var getData = async ({ id, dataSource, isPersisting, schema, SQLite, changeTime }) => {
705
- if (dataSource) console.error("[axios] GET Data Source: ", buildUri({ dataSource, method: "read" }));
706
- else console.error("axios No DataSource for ", id);
707
705
  let persistedData = [];
708
706
  var params = { ...dataSource?.params };
709
707
  if (dataSource?.filter) params.filter = dataSource.filter;
@@ -719,7 +717,6 @@ var getData = async ({ id, dataSource, isPersisting, schema, SQLite, changeTime
719
717
  if (changeTime) {
720
718
  params.changeTime = changeTime;
721
719
  } else if (isPersisting && SQLite) {
722
- console.error("[axios] Nacitani dat z SQLite pro ", id, " s parametrem changeTimeFrom: ", params.changeTimeFrom);
723
720
  const qDatum = await SQLite.read(`SELECT * FROM documents WHERE model = '${id}' ORDER BY lastUpdate DESC`);
724
721
  console.warn("[axios] SQLite.read Posledni datum update: ", qDatum[0]?.lastUpdate);
725
722
  if (qDatum[0]?.lastUpdate) params.changeTimeFrom = qDatum[0].lastUpdate;
@@ -738,7 +735,6 @@ var getData = async ({ id, dataSource, isPersisting, schema, SQLite, changeTime
738
735
  }).filter((item) => item !== null);
739
736
  }
740
737
  if (dataSource) {
741
- console.error("[axios] Stahuji data z ", buildUri({ dataSource, method: "read" }), " s parametry ", params);
742
738
  const axiosResponse = await import_axios.default.get(buildUri({ dataSource, method: "read" }), { params: serializeParams(params) });
743
739
  if (schema && !dataSource.readAllFields) {
744
740
  const mapBySchema = (element) => {
@@ -775,14 +771,6 @@ var getData = async ({ id, dataSource, isPersisting, schema, SQLite, changeTime
775
771
  } else {
776
772
  newData = axiosResponse.data.data;
777
773
  }
778
- const dataCount = Array.isArray(newData) ? newData.length : "single";
779
- console.error(
780
- `[axios](SERVER TIME: ${axiosResponse.data.queryTime}) Data:`,
781
- dataCount,
782
- "items, Size:",
783
- JSON.stringify(newData).length,
784
- "Bytes"
785
- );
786
774
  if (isPersisting && SQLite) {
787
775
  await saveData({ providerId: id, newData, SQLite, dateUpdate: axiosResponse.data.queryAt || (/* @__PURE__ */ new Date()).toISOString() });
788
776
  }
@@ -793,7 +781,6 @@ var getData = async ({ id, dataSource, isPersisting, schema, SQLite, changeTime
793
781
  persistedData.forEach((item) => map.set(item.id, item));
794
782
  newData.forEach((item) => map.set(item.id, item));
795
783
  const updatedArray = Array.from(map.values());
796
- console.error("[axios] NEW DATA: ", updatedArray);
797
784
  return updatedArray;
798
785
  } else {
799
786
  return newData || [];
@@ -820,6 +807,7 @@ var patchData = async ({ dataSource, variant, data }) => {
820
807
  return response.data.data;
821
808
  } catch (e) {
822
809
  console.error(`[AXIOS] PATCH DATA: ${e}`);
810
+ return e;
823
811
  }
824
812
  };
825
813
  var deleteData = async ({ dataSource, data }) => {
@@ -1091,7 +1079,6 @@ var DataStoreProvider = (0, import_react.forwardRef)(
1091
1079
  ...rest,
1092
1080
  changeTime: lastUpdate.time
1093
1081
  });
1094
- console.error("[DSP] synchronizeDataStore response:", key, response);
1095
1082
  if (response.length > 0) {
1096
1083
  const updateData = {};
1097
1084
  response.forEach((item) => {
@@ -1379,7 +1366,6 @@ var DataStoreProvider = (0, import_react.forwardRef)(
1379
1366
  schema: dP.schema,
1380
1367
  SQLite: persistenceStorage || void 0
1381
1368
  });
1382
- console.error("[DSP] updateDocument dataFromSource:", model, documentId, dataFromSource);
1383
1369
  data = dataFromSource;
1384
1370
  }
1385
1371
  }
@@ -1784,7 +1770,6 @@ var DataStoreProvider = (0, import_react.forwardRef)(
1784
1770
  isPersisting: !!newItem.isPersisting && !!persistenceStorage,
1785
1771
  SQLite: persistenceStorage || void 0
1786
1772
  });
1787
- console.error("[DSP] registerProvider dataFromSource:", id, dataFromSource);
1788
1773
  var dataList = [];
1789
1774
  var items = {};
1790
1775
  const dataS = Array.isArray(dataFromSource) ? dataFromSource : [dataFromSource];
@@ -1954,78 +1939,66 @@ var DataStoreProvider_default = DataStoreProvider;
1954
1939
 
1955
1940
  // src/useDataProvider.tsx
1956
1941
  var import_react2 = require("react");
1957
- var getDynamicOptionsConfig = (schema) => {
1958
- if (!schema) return [];
1959
- const configs = [];
1960
- Object.entries(schema).forEach(([fieldKey, schemaItem]) => {
1961
- if (!schemaItem || typeof schemaItem !== "object") return;
1962
- if (schemaItem.options && !Array.isArray(schemaItem.options) && typeof schemaItem.options === "object" && schemaItem.options.dataProvider) {
1963
- configs.push({
1964
- fieldKey,
1965
- dataProvider: schemaItem.options.dataProvider,
1966
- optionKeys: schemaItem.options.optionKeys,
1967
- filter: schemaItem.options.filter
1968
- });
1969
- return;
1970
- }
1971
- if (schemaItem.dataSource?.dataProvider) {
1972
- configs.push({
1973
- fieldKey,
1974
- dataProvider: schemaItem.dataSource.dataProvider,
1975
- optionKeys: schemaItem.optionKeys,
1976
- filter: schemaItem.filter
1977
- });
1978
- }
1979
- });
1980
- 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);
1981
1948
  };
1982
1949
  var resolveProviderRef = (providerId, providers, presetProviders, params) => {
1983
1950
  if (providers[providerId]) {
1984
- return {
1985
- templateId: providerId,
1986
- runtimeId: providerId
1987
- };
1951
+ return { templateId: providerId, runtimeId: providerId };
1988
1952
  }
1989
1953
  if (presetProviders[providerId]) {
1990
1954
  const renderedProvider = renderJSONTemplate(presetProviders[providerId], params ?? void 0);
1991
- return {
1992
- templateId: providerId,
1993
- runtimeId: renderedProvider.id
1994
- };
1955
+ return { templateId: providerId, runtimeId: renderedProvider.id };
1995
1956
  }
1996
- return {
1997
- templateId: providerId,
1998
- runtimeId: providerId
1999
- };
1957
+ return { templateId: providerId, runtimeId: providerId };
2000
1958
  };
2001
- var createOptionFromDocument = (doc, optionKeys) => {
2002
- const valuePath = optionKeys?.value || "id";
2003
- const optionValue = getValueByDotPath(doc, valuePath, doc.id);
2004
- const labelDefinition = optionKeys?.label;
2005
- let optionLabel = "";
2006
- let optionIcon;
2007
- if (typeof labelDefinition === "string") {
2008
- optionLabel = String(getValueByDotPath(doc, labelDefinition, optionValue ?? ""));
2009
- } else if (labelDefinition && typeof labelDefinition === "object") {
2010
- const labelPath = labelDefinition.title || "name";
2011
- optionLabel = String(getValueByDotPath(doc, labelPath, optionValue ?? ""));
2012
- if (labelDefinition.icon) {
2013
- 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;
2014
1985
  }
2015
- } else {
2016
- 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));
2017
1995
  }
2018
- if (optionIcon) {
2019
- return {
2020
- value: optionValue,
2021
- label: optionLabel,
2022
- icon: optionIcon
2023
- };
1996
+ if (Array.isArray(obj)) {
1997
+ return obj.map((item) => resolveDataSourcesDeep(item, providers, documents, presetProviders, params, logLabel));
2024
1998
  }
2025
- return {
2026
- value: optionValue,
2027
- label: optionLabel
2028
- };
1999
+ return Object.fromEntries(
2000
+ Object.entries(obj).map(([key, val]) => [key, resolveDataSourcesDeep(val, providers, documents, presetProviders, params, logLabel)])
2001
+ );
2029
2002
  };
2030
2003
  var useDataProvider = (id, params, dataFilter, settings) => {
2031
2004
  const [providerId, setProviderId] = (0, import_react2.useState)(null);
@@ -2056,12 +2029,13 @@ var useDataProvider = (id, params, dataFilter, settings) => {
2056
2029
  );
2057
2030
  const dependentProviders = (0, import_react2.useMemo)(() => {
2058
2031
  const providers = getExternalProviders();
2059
- if (!providerId || !providers[providerId]?.schema) return [];
2032
+ if (!providerId || !providers[providerId]) return [];
2060
2033
  const schema = providers[providerId]?.schema;
2061
- const optionConfigs = getDynamicOptionsConfig(schema);
2034
+ const layout = providers[providerId]?.layout;
2035
+ const allProviderIds = [...findAllDataProviders(schema), ...findAllDataProviders(layout)];
2062
2036
  const uniqueProviders = {};
2063
- optionConfigs.forEach((cfg) => {
2064
- const resolved = resolveProviderRef(cfg.dataProvider, providers, presetProviders, params);
2037
+ allProviderIds.forEach((dataProvider) => {
2038
+ const resolved = resolveProviderRef(dataProvider, providers, presetProviders, params);
2065
2039
  if (resolved.runtimeId === providerId) return;
2066
2040
  uniqueProviders[resolved.runtimeId] = resolved;
2067
2041
  });
@@ -2176,7 +2150,7 @@ var useDataProvider = (id, params, dataFilter, settings) => {
2176
2150
  dispatch("deleteProviderItem", { providerId, data, params: params2 }, callback);
2177
2151
  };
2178
2152
  const createItem = (data, callback) => {
2179
- return new Promise((resolve, reject) => {
2153
+ return new Promise((resolve) => {
2180
2154
  const wrappedCallback = (result) => {
2181
2155
  callback?.(result);
2182
2156
  resolve(result);
@@ -2189,10 +2163,7 @@ var useDataProvider = (id, params, dataFilter, settings) => {
2189
2163
  const providers = getExternalProviders();
2190
2164
  const documents = getExternalDocuments();
2191
2165
  if (!providerId || !providers[providerId]) {
2192
- return {
2193
- data: [],
2194
- schema: void 0
2195
- };
2166
+ return { data: [], schema: void 0, layout: void 0 };
2196
2167
  }
2197
2168
  let docs = [];
2198
2169
  providers[providerId]?.data.forEach((docId) => {
@@ -2204,54 +2175,23 @@ var useDataProvider = (id, params, dataFilter, settings) => {
2204
2175
  } else if (settings?.document) {
2205
2176
  dData = recalculateObjectWithDocument(documents[docId], settings.document);
2206
2177
  }
2207
- docs.push({
2208
- id: docId,
2209
- ...dData
2210
- });
2178
+ docs.push({ id: docId, ...dData });
2211
2179
  });
2212
2180
  systemLog("usedp", "KonecVytvoreni dat: ", providerId);
2213
2181
  if (filter) {
2214
2182
  systemLog("usedp", "Jdu filterovat: ", JSON.stringify(filter));
2215
- docs = filterDocuments(docs, filter);
2183
+ docs = (0, import_json_filter.filterJsonDocuments)(docs, filter);
2216
2184
  systemLog("usedp", "Konec filteru: ");
2217
2185
  }
2218
2186
  const schema = providers[providerId].schema;
2219
- let resolvedSchema = schema;
2220
- if (schema) {
2221
- const optionConfigs = getDynamicOptionsConfig(schema);
2222
- if (optionConfigs.length > 0) {
2223
- resolvedSchema = { ...schema };
2224
- optionConfigs.forEach((cfg) => {
2225
- const providerRef = resolveProviderRef(cfg.dataProvider, providers, presetProviders, params);
2226
- const optionProvider = providers[providerRef.runtimeId];
2227
- if (!optionProvider) {
2228
- systemLog("usedp", "Nenalezen provider pro schema.options", cfg.dataProvider);
2229
- return;
2230
- }
2231
- let optionDocuments = (optionProvider.data || []).map((docId) => {
2232
- const docData = documents[docId];
2233
- if (!docData) return null;
2234
- return {
2235
- id: docId,
2236
- ...docData
2237
- };
2238
- }).filter((item) => !!item);
2239
- if (cfg.filter) {
2240
- optionDocuments = filterDocuments(optionDocuments, cfg.filter);
2241
- }
2242
- const computedOptions = optionDocuments.map((item) => createOptionFromDocument(item, cfg.optionKeys));
2243
- const nextItem = {
2244
- ...resolvedSchema[cfg.fieldKey],
2245
- options: computedOptions
2246
- };
2247
- resolvedSchema[cfg.fieldKey] = nextItem;
2248
- });
2249
- }
2250
- }
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");
2251
2190
  return {
2252
2191
  ...providers[providerId],
2253
2192
  data: docs,
2254
- schema: resolvedSchema
2193
+ schema: resolvedSchema,
2194
+ layout: resolvedLayout
2255
2195
  };
2256
2196
  }, [
2257
2197
  providerId,
@@ -2266,11 +2206,9 @@ var useDataProvider = (id, params, dataFilter, settings) => {
2266
2206
  ]);
2267
2207
  systemLog("usedp", `${providerId} END`);
2268
2208
  return {
2269
- // provider,
2270
2209
  documents: provider.data,
2271
2210
  schema: provider.schema,
2272
2211
  layout: provider.layout,
2273
- // dispatch,
2274
2212
  update,
2275
2213
  updateItem,
2276
2214
  deleteItem,