@protobase/server 0.22.6 → 0.22.8

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.
@@ -2,6 +2,7 @@ import { badRequest } from "./problem.js";
2
2
  import { readTransaction } from "./transactions.js";
3
3
  import { parseParams } from "./params.js";
4
4
  import { labelRequests, relatedLabels } from "./related-labels.js";
5
+ import { splitGroups } from "./aggregate-splits.js";
5
6
  import { scoped, seriesRange } from "./aggregate-service.js";
6
7
  import { z } from "zod";
7
8
  import { groupTotals, series } from "@protobase/query";
@@ -30,7 +31,10 @@ const aggregateParams = z.object({
30
31
  "month",
31
32
  "year"
32
33
  ]).optional(),
33
- time_zone: z.string().optional()
34
+ time_zone: z.string().optional(),
35
+ split_by: name.optional(),
36
+ split_limit: z.coerce.number().int().optional(),
37
+ split_label: name.optional()
34
38
  });
35
39
  const invalid = (parameter, message) => badRequest("invalid-parameter", `Invalid parameter ${parameter}: ${message}`, { errors: [{
36
40
  parameter,
@@ -49,13 +53,20 @@ const measureOf = (params) => {
49
53
  };
50
54
  const optionErrors = (params, dated) => {
51
55
  const given = (names) => names.filter((option) => params[option] !== void 0);
52
- if (dated) return given(["limit", "label"]).map((option) => invalid(option, `Not for a date or timestamp group_by`));
56
+ if (dated) {
57
+ const errors = given(["limit", "label"]).map((option) => invalid(option, `Not for a date or timestamp group_by`));
58
+ if (params.split_by === void 0 && given(["split_limit", "split_label"]).length > 0) errors.push(invalid("split_by", "split_limit and split_label need split_by"));
59
+ return errors;
60
+ }
53
61
  return given([
54
62
  "range",
55
63
  "from",
56
64
  "to",
57
65
  "granularity",
58
- "time_zone"
66
+ "time_zone",
67
+ "split_by",
68
+ "split_limit",
69
+ "split_label"
59
70
  ]).map((option) => invalid(option, "Only for a date or timestamp group_by"));
60
71
  };
61
72
  const isDated = (model, field) => {
@@ -75,6 +86,7 @@ const aggregate = async (deps, entry, session, input) => {
75
86
  const dated = isDated(model, params.group_by);
76
87
  const [problem] = optionErrors(params, dated);
77
88
  if (problem) throw problem;
89
+ if (params.split_by !== void 0 && !Object.hasOwn(model.fields, params.split_by)) throw invalid("split_by", `Unknown field "${params.split_by}"`);
78
90
  const warning = await deps.guard(entry, {
79
91
  ...filter && { filter },
80
92
  groupBy: params.group_by
@@ -82,10 +94,45 @@ const aggregate = async (deps, entry, session, input) => {
82
94
  model,
83
95
  scope
84
96
  });
97
+ const splitWarning = params.split_by === void 0 ? void 0 : await deps.guard(entry, {
98
+ ...filter && { filter },
99
+ groupBy: params.split_by
100
+ }, {
101
+ model,
102
+ scope
103
+ });
85
104
  if (dated) {
86
105
  const granularity = params.granularity ?? "day";
87
106
  const timeZone = params.time_zone ?? "UTC";
88
107
  const range = await seriesRange(deps, access, params.group_by, params, scope);
108
+ if (params.split_by !== void 0) {
109
+ const split = {
110
+ by: params.split_by,
111
+ ...params.split_limit !== void 0 && { limit: params.split_limit },
112
+ ...params.split_label !== void 0 && { label: params.split_label }
113
+ };
114
+ const parts = range ? await splitGroups(deps, session, model, params.group_by, split, {
115
+ range,
116
+ granularity,
117
+ timeZone,
118
+ ...filter && { filter },
119
+ ...measure && { measure },
120
+ scope
121
+ }) : {
122
+ splits: [],
123
+ groups: []
124
+ };
125
+ return {
126
+ body: {
127
+ group_by: params.group_by,
128
+ split_by: params.split_by,
129
+ granularity,
130
+ time_zone: timeZone,
131
+ ...parts
132
+ },
133
+ warning: warning ?? splitWarning
134
+ };
135
+ }
89
136
  const groups = (range ? await readTransaction(deps.db, deps.statementTimeoutMs, (trx) => series(trx, model, params.group_by, {
90
137
  range,
91
138
  granularity,
@@ -0,0 +1,41 @@
1
+ import { readTransaction } from "./transactions.js";
2
+ import { labelRequests, relatedLabels } from "./related-labels.js";
3
+ import { series, splitSeries } from "@protobase/query";
4
+ //#region src/aggregate-splits.ts
5
+ /**
6
+ * The buckets of a date field with each split into its parts: per bucket the shown values that have rows in it, in
7
+ * rank order. The bucket's own totals stay what `series` finds, so what the other values add up to is total minus parts.
8
+ * With `label`, a relation's values carry the `label` field of the record each points at.
9
+ */
10
+ const splitGroups = async (deps, session, model, field, split, options) => {
11
+ const labels = split.label === void 0 ? [] : await labelRequests(deps, session, model, [split.by], `${split.by}.${split.label}`);
12
+ return readTransaction(deps.db, deps.statementTimeoutMs, async (trx) => {
13
+ const buckets = await series(trx, model, field, options);
14
+ const { splits, points } = await splitSeries(trx, model, field, split.by, {
15
+ ...options,
16
+ ...split.limit !== void 0 && { limit: split.limit }
17
+ });
18
+ const names = (await relatedLabels(trx, labels, splits.map((value) => ({ [split.by]: value }))))[split.by] ?? {};
19
+ const partsOf = /* @__PURE__ */ new Map();
20
+ for (const { bucket, split: value, ...totals } of points) partsOf.set(bucket, [...partsOf.get(bucket) ?? [], {
21
+ value,
22
+ ...totals
23
+ }]);
24
+ return {
25
+ splits: splits.map((value) => {
26
+ const label = value === null ? void 0 : names[String(value)];
27
+ return label === void 0 ? { value } : {
28
+ value,
29
+ label
30
+ };
31
+ }),
32
+ groups: buckets.map(({ bucket, ...totals }) => ({
33
+ value: bucket,
34
+ ...totals,
35
+ splits: partsOf.get(bucket) ?? []
36
+ }))
37
+ };
38
+ });
39
+ };
40
+ //#endregion
41
+ export { splitGroups };
@@ -590,7 +590,7 @@ const resourcePaths = (access) => {
590
590
  operationId: `aggregate_${entry.name}`,
591
591
  tags: tag,
592
592
  summary: `Totals per group of ${entry.name} records`,
593
- description: "Groups the records the filter finds, its own terms on `group_by` included, by the value of a field, or by the hour, day, week, month or year of a date or timestamp field with the empty ones included. Each group has its count and, with `sum` or `average`, that of a numeric field, `null` when no record of the group has a value. Value groups come largest first. An index must serve the grouped field.",
593
+ description: "Groups the records the filter finds, its own terms on `group_by` included, by the value of a field, or by the hour, day, week, month or year of a date or timestamp field with the empty ones included. Each group has its count and, with `sum` or `average`, that of a numeric field, `null` when no record of the group has a value. Value groups come largest first. An index must serve the grouped field. A date group can be split: see `split_by`.",
594
594
  parameters: [
595
595
  query("group_by", "A filterable field", { type: "string" }, true),
596
596
  param("filter"),
@@ -620,7 +620,10 @@ const resourcePaths = (access) => {
620
620
  }),
621
621
  query("from", "For a date group_by: the explicit start (ISO 8601); with `to`, replaces range"),
622
622
  query("to", "For a date group_by: the exclusive end (ISO 8601), default now"),
623
- query("time_zone", "For a date group_by: the IANA time zone of the buckets, default UTC")
623
+ query("time_zone", "For a date group_by: the IANA time zone of the buckets, default UTC"),
624
+ query("split_by", "For a date group_by: a filterable field of values that splits each bucket into parts; an index must serve it"),
625
+ query("split_limit", "For a date group_by: how many values of `split_by` to keep, the largest over the range (by the sum, else by count), default 8, at most 100", { type: "integer" }),
626
+ query("split_label", "For a date group_by with a relation `split_by`: the field of the related resource whose value each split carries as `label`")
624
627
  ],
625
628
  responses: {
626
629
  200: {
@@ -631,6 +634,21 @@ const resourcePaths = (access) => {
631
634
  group_by: { type: "string" },
632
635
  granularity: { type: "string" },
633
636
  time_zone: { type: "string" },
637
+ split_by: {
638
+ type: "string",
639
+ description: "Only with `split_by`"
640
+ },
641
+ splits: {
642
+ type: "array",
643
+ description: "Only with `split_by`: the kept values, largest first",
644
+ items: {
645
+ type: "object",
646
+ properties: {
647
+ value: {},
648
+ label: { type: "string" }
649
+ }
650
+ }
651
+ },
634
652
  groups: {
635
653
  type: "array",
636
654
  items: {
@@ -640,7 +658,20 @@ const resourcePaths = (access) => {
640
658
  label: { type: "string" },
641
659
  count: { type: "integer" },
642
660
  sum: { type: ["number", "null"] },
643
- average: { type: ["number", "null"] }
661
+ average: { type: ["number", "null"] },
662
+ splits: {
663
+ type: "array",
664
+ description: "Only with `split_by`: the kept values that have rows in the group, in rank order",
665
+ items: {
666
+ type: "object",
667
+ properties: {
668
+ value: {},
669
+ count: { type: "integer" },
670
+ sum: { type: ["number", "null"] },
671
+ average: { type: ["number", "null"] }
672
+ }
673
+ }
674
+ }
644
675
  }
645
676
  }
646
677
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/server",
3
- "version": "0.22.6",
3
+ "version": "0.22.8",
4
4
  "description": "The Protobase REST API as a Hono app, with Better Auth sign-in.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -31,9 +31,9 @@
31
31
  "dependencies": {
32
32
  "@better-auth/passkey": "^1.7.7",
33
33
  "@better-auth/sso": "^1.7.7",
34
- "@protobase/layout": "0.22.6",
35
- "@protobase/query": "0.22.6",
36
- "@protobase/schema": "0.22.6",
34
+ "@protobase/layout": "0.22.8",
35
+ "@protobase/query": "0.22.8",
36
+ "@protobase/schema": "0.22.8",
37
37
  "@react-email/render": "^2.1.0",
38
38
  "@scalar/hono-api-reference": "^0.12.10",
39
39
  "@sentry/core": "^11.7.0",