@routier/core 0.0.1-alpha.3 → 0.0.2

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.
@@ -21,7 +21,13 @@ function assertDate(data) {
21
21
  }
22
22
  function assertIsNotNull(data, message) {
23
23
  if (data == null) {
24
- throw new TypeError(message ?? 'Assertion failed, data is null');
24
+ if (message == null) {
25
+ throw new TypeError('Assertion failed, data is null');
26
+ }
27
+ if (typeof message === "string") {
28
+ throw new TypeError(message);
29
+ }
30
+ throw new TypeError(message());
25
31
  }
26
32
  }
27
33
  function assertIsArray(data, message) {
@@ -838,7 +844,11 @@ class EphemeralDataPlugin {
838
844
  done(_results__WEBPACK_IMPORTED_MODULE_1__.PluginEventResult.error(event.id, r.error));
839
845
  return;
840
846
  }
841
- const translated = translator.translate(collection.records);
847
+ const cloned = [];
848
+ for (let i = 0, length = collection.records.length; i < length; i++) {
849
+ cloned.push(event.operation.schema.clone(collection.records[i]));
850
+ }
851
+ const translated = translator.translate(cloned);
842
852
  done(_results__WEBPACK_IMPORTED_MODULE_1__.PluginEventResult.success(event.id, translated));
843
853
  });
844
854
  }
@@ -1478,6 +1488,8 @@ const getMemoryPluginCollectionSize = (plugin, schema) => {
1478
1488
  }
1479
1489
  throw new Error("Cannot get size of collection for MemoryPlugin, not an instance of MemoryPlugin");
1480
1490
  };
1491
+ const WAS_OPTIMISTIC_DB_HYDRATED_KEY = "was-hydrated";
1492
+ const cache = new Set();
1481
1493
  class OptimisticReplicationDbPlugin {
1482
1494
  plugins;
1483
1495
  constructor(plugins) {
@@ -1501,7 +1513,7 @@ class OptimisticReplicationDbPlugin {
1501
1513
  const readPlugin = this.plugins.read;
1502
1514
  const sourcePlugin = this.plugins.source;
1503
1515
  const collectionSize = getMemoryPluginCollectionSize(this.plugins.read, event.operation.schema);
1504
- if (collectionSize === 0) {
1516
+ if (collectionSize === 0 && cache.has(WAS_OPTIMISTIC_DB_HYDRATED_KEY) === false) {
1505
1517
  // nothing is hydrated, let's try and hydrate before querying
1506
1518
  // Memory plugin might not be hydrated, lets hydrate it for the targeted schema only,
1507
1519
  // Other queries will do the same and hydrate if needed
@@ -1539,6 +1551,8 @@ class OptimisticReplicationDbPlugin {
1539
1551
  done(readPersistResult);
1540
1552
  return;
1541
1553
  }
1554
+ // Notify the cache that the db was hydrated
1555
+ cache.add(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
1542
1556
  // requery the read plugin
1543
1557
  readPlugin.query(event, done);
1544
1558
  });
@@ -1883,6 +1897,9 @@ class JsonTranslator extends _DataTranslator__WEBPACK_IMPORTED_MODULE_0__.DataTr
1883
1897
  }
1884
1898
  sum(data, _) {
1885
1899
  (0,_assertions__WEBPACK_IMPORTED_MODULE_1__.assertIsArray)(data, this._formatDataNotArrayError("sum"));
1900
+ if (data.length === 0) {
1901
+ throw new Error("Cannot perform operation on empty array, result query contains no data");
1902
+ }
1886
1903
  const map = this.query.options.getLast("map");
1887
1904
  let sum = 0;
1888
1905
  const field = this._getSelectionField("sum", map);
@@ -1978,15 +1995,26 @@ __webpack_require__.d(__webpack_exports__, {
1978
1995
 
1979
1996
  class SqlTranslator extends _DataTranslator__WEBPACK_IMPORTED_MODULE_0__.DataTranslator {
1980
1997
  count(data, _) {
1998
+ if (Array.isArray(data) && data.length > 0) {
1999
+ // Count should be returned as the property alias on the query
2000
+ return data[0].count;
2001
+ }
1981
2002
  return data;
1982
2003
  }
1983
2004
  min(data, _) {
1984
- return data;
2005
+ return this.shapeResult(data);
1985
2006
  }
1986
2007
  max(data, _) {
1987
- return data;
2008
+ return this.shapeResult(data);
1988
2009
  }
1989
2010
  sum(data, _) {
2011
+ return this.shapeResult(data);
2012
+ }
2013
+ shapeResult(data) {
2014
+ if (Array.isArray(data) && data.length > 0) {
2015
+ // Shape the result
2016
+ return data[0];
2017
+ }
1990
2018
  return data;
1991
2019
  }
1992
2020
  distinct(data, _) {
@@ -2004,8 +2032,29 @@ class SqlTranslator extends _DataTranslator__WEBPACK_IMPORTED_MODULE_0__.DataTra
2004
2032
  sort(data, _) {
2005
2033
  return data;
2006
2034
  }
2007
- map(data, _) {
2008
- return data;
2035
+ map(data, option) {
2036
+ if (Array.isArray(data) == false) {
2037
+ throw new Error("Can only map an array of data");
2038
+ }
2039
+ if (this.query.options.has("count") && data.length === 1) {
2040
+ // data here is the shape of { count: number }[] and will map to nothing.
2041
+ // Return the original data and let count take care of this
2042
+ return data;
2043
+ }
2044
+ const response = [];
2045
+ for (let i = 0, length = data.length; i < length; i++) {
2046
+ for (let j = 0, l = option.value.fields.length; j < l; j++) {
2047
+ const field = option.value.fields[j];
2048
+ if (field.property != null) {
2049
+ const value = field.property.getValue(data[i]);
2050
+ if (value != null) {
2051
+ field.property.setValue(data[i], field.property.deserialize(value));
2052
+ }
2053
+ }
2054
+ }
2055
+ response.push(option.value.selector(data[i]));
2056
+ }
2057
+ return response;
2009
2058
  }
2010
2059
  }
2011
2060