@routier/core 0.0.1-alpha.3 → 0.0.1

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
  }
@@ -1883,6 +1893,9 @@ class JsonTranslator extends _DataTranslator__WEBPACK_IMPORTED_MODULE_0__.DataTr
1883
1893
  }
1884
1894
  sum(data, _) {
1885
1895
  (0,_assertions__WEBPACK_IMPORTED_MODULE_1__.assertIsArray)(data, this._formatDataNotArrayError("sum"));
1896
+ if (data.length === 0) {
1897
+ throw new Error("Cannot perform operation on empty array, result query contains no data");
1898
+ }
1886
1899
  const map = this.query.options.getLast("map");
1887
1900
  let sum = 0;
1888
1901
  const field = this._getSelectionField("sum", map);
@@ -1978,15 +1991,26 @@ __webpack_require__.d(__webpack_exports__, {
1978
1991
 
1979
1992
  class SqlTranslator extends _DataTranslator__WEBPACK_IMPORTED_MODULE_0__.DataTranslator {
1980
1993
  count(data, _) {
1994
+ if (Array.isArray(data) && data.length > 0) {
1995
+ // Count should be returned as the property alias on the query
1996
+ return data[0].count;
1997
+ }
1981
1998
  return data;
1982
1999
  }
1983
2000
  min(data, _) {
1984
- return data;
2001
+ return this.shapeResult(data);
1985
2002
  }
1986
2003
  max(data, _) {
1987
- return data;
2004
+ return this.shapeResult(data);
1988
2005
  }
1989
2006
  sum(data, _) {
2007
+ return this.shapeResult(data);
2008
+ }
2009
+ shapeResult(data) {
2010
+ if (Array.isArray(data) && data.length > 0) {
2011
+ // Shape the result
2012
+ return data[0];
2013
+ }
1990
2014
  return data;
1991
2015
  }
1992
2016
  distinct(data, _) {
@@ -2004,8 +2028,29 @@ class SqlTranslator extends _DataTranslator__WEBPACK_IMPORTED_MODULE_0__.DataTra
2004
2028
  sort(data, _) {
2005
2029
  return data;
2006
2030
  }
2007
- map(data, _) {
2008
- return data;
2031
+ map(data, option) {
2032
+ if (Array.isArray(data) == false) {
2033
+ throw new Error("Can only map an array of data");
2034
+ }
2035
+ if (this.query.options.has("count") && data.length === 1) {
2036
+ // data here is the shape of { count: number }[] and will map to nothing.
2037
+ // Return the original data and let count take care of this
2038
+ return data;
2039
+ }
2040
+ const response = [];
2041
+ for (let i = 0, length = data.length; i < length; i++) {
2042
+ for (let j = 0, l = option.value.fields.length; j < l; j++) {
2043
+ const field = option.value.fields[j];
2044
+ if (field.property != null) {
2045
+ const value = field.property.getValue(data[i]);
2046
+ if (value != null) {
2047
+ field.property.setValue(data[i], field.property.deserialize(value));
2048
+ }
2049
+ }
2050
+ }
2051
+ response.push(option.value.selector(data[i]));
2052
+ }
2053
+ return response;
2009
2054
  }
2010
2055
  }
2011
2056