@rebasepro/server-postgres 0.19.2-canary.g9b599d4 → 0.20.0

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.es.js CHANGED
@@ -3552,6 +3552,28 @@ function compareForSort(a, b, nullsLast) {
3552
3552
  * Service for handling all row read operations.
3553
3553
  * Handles fetching, searching, counting, and filtering rows.
3554
3554
  */
3555
+ /**
3556
+ * Which aggregate aliases hold a number that Postgres handed back as a string.
3557
+ *
3558
+ * `count`, `sum` and `avg` always do: bigint and numeric are returned as text
3559
+ * because they do not fit a JS number in general. `min` and `max` are uncast,
3560
+ * so they do it too — but only over a numeric column, and those are the two
3561
+ * functions that also apply to text and dates.
3562
+ *
3563
+ * Decided by the column's DECLARED type, not by trying `Number()` on the value.
3564
+ * `?select=avg(price),max(price)` used to answer `{avg_price: 5,
3565
+ * max_price: "8.5"}` — one column, two functions, two JSON types, and
3566
+ * arithmetic on the second one silently concatenates. Parsing whatever looks
3567
+ * numeric would fix that and break something worse: a `min(sku)` of `"00123"`
3568
+ * would come back as `123`, a different value, and no NaN check would catch it.
3569
+ *
3570
+ * Exported to be tested: the parsing itself needs a database, this decision
3571
+ * does not.
3572
+ */
3573
+ function numericAggregateAliases(aggregates, properties) {
3574
+ const isNumberField = (field) => Boolean(field) && properties[field]?.type === "number";
3575
+ return new Set(aggregates.filter((a) => a.fn === "count" || a.fn === "sum" || a.fn === "avg" || (a.fn === "min" || a.fn === "max") && isNumberField(a.field)).map((a) => a.alias));
3576
+ }
3555
3577
  var FetchService = class FetchService {
3556
3578
  db;
3557
3579
  registry;
@@ -4392,7 +4414,7 @@ var FetchService = class FetchService {
4392
4414
  if (options.limit) query = query.limit(options.limit);
4393
4415
  }
4394
4416
  const rows = await query;
4395
- const numericAliases = new Set(options.aggregates.filter((a) => a.fn === "count" || a.fn === "sum" || a.fn === "avg").map((a) => a.alias));
4417
+ const numericAliases = numericAggregateAliases(options.aggregates, collection?.properties ?? {});
4396
4418
  return rows.map((row) => {
4397
4419
  const out = { ...row };
4398
4420
  for (const alias of numericAliases) {
@@ -5157,7 +5179,7 @@ var PersistService = class {
5157
5179
  } catch (error) {
5158
5180
  throw this.toUserFriendlyError(error, collection.slug, collection);
5159
5181
  }
5160
- const finalEntity = await this.fetchService.fetchOneForRest(collection.slug, savedId, void 0, databaseId);
5182
+ const finalEntity = await this.fetchService.fetchOneForRest(collection.slug, savedId, void 0, databaseId, { withDeleted: true });
5161
5183
  if (!finalEntity) throw new Error("Could not fetch row after save.");
5162
5184
  return finalEntity;
5163
5185
  }