@koolbase/core 12.51.0 → 12.53.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.
@@ -53,14 +53,31 @@ export type KoolbaseGroupBy = {
53
53
  * `count` counts RECORDS, so it takes no field and skips nothing. The others
54
54
  * read a field, and a record whose value is missing or not a number is
55
55
  * skipped and reported in the result's accounting.
56
+ *
57
+ * `sum_product` adds up the product of two fields -- amount times
58
+ * probability -- over the records where BOTH are numbers. `byScale: 100`
59
+ * says `by` is a percentage stored 0 to 100, so 40 multiplies as 0.4; when
60
+ * the field is declared a percent or probability, its declared scale is used
61
+ * and need not be sent.
56
62
  */
57
63
  export type KoolbaseMeasure = {
58
64
  aggregate: 'count';
59
65
  field?: never;
66
+ by?: never;
67
+ byScale?: never;
60
68
  as: string;
61
69
  } | {
62
70
  aggregate: 'sum' | 'avg' | 'min' | 'max';
63
71
  field: string;
72
+ by?: never;
73
+ byScale?: never;
74
+ as: string;
75
+ } | {
76
+ aggregate: 'sum_product';
77
+ field: string;
78
+ /** The second field: each record contributes field times by. */
79
+ by: string;
80
+ byScale?: 1 | 100;
64
81
  as: string;
65
82
  };
66
83
  /** One aggregation request. */
@@ -87,6 +104,15 @@ export interface KoolbaseAggregateGroup {
87
104
  category: string;
88
105
  /** Keyed by each measure's `as`. Null where a group had nothing to measure. */
89
106
  values: Record<string, number | null>;
107
+ /**
108
+ * Every value as an exact decimal string, computed without floating point:
109
+ * `"1234.50"`, never 1234.4999999. Same keys and the same nulls as
110
+ * `values`. Read money from here, and format the string: do not turn it
111
+ * back into a number first. A sum, min, max and sum_product are exact; an
112
+ * average is given to 12 decimal places; a count is an integer. Empty
113
+ * against an API from before exact totals.
114
+ */
115
+ exact: Record<string, string | null>;
90
116
  /**
91
117
  * The collection's count minimum (#299), when this group's count was below
92
118
  * it: every value is null, and what to show is "Fewer than N" -- never 0,
@@ -107,6 +133,12 @@ export interface KoolbaseAggregateAccounting {
107
133
  skipped: number;
108
134
  /** Present when skipped > 0. */
109
135
  skippedReason?: 'missing' | 'not_numeric' | 'mixed';
136
+ /**
137
+ * `sum_product` only, when skipped > 0: how many skipped records had no
138
+ * usable number in each of its two fields. A record lacking both counts
139
+ * under both.
140
+ */
141
+ skippedByField?: Record<string, number>;
110
142
  }
111
143
  /** The whole answer, including what could not be counted. */
112
144
  export interface KoolbaseAggregateResult {
@@ -718,6 +718,9 @@ class KoolbaseDatabase {
718
718
  measures: request.measures.map((m) => ({
719
719
  aggregate: m.aggregate,
720
720
  ...(m.field ? { field: m.field } : {}),
721
+ // sum_product: the second field, and its scale when given.
722
+ ...(m.by ? { by: m.by } : {}),
723
+ ...(m.byScale ? { by_scale: m.byScale } : {}),
721
724
  as: m.as,
722
725
  })),
723
726
  });
@@ -727,12 +730,17 @@ class KoolbaseDatabase {
727
730
  counted: Number(a.counted ?? 0),
728
731
  skipped: Number(a.skipped ?? 0),
729
732
  skippedReason: a.skipped_reason,
733
+ ...(a.skipped_by_field && typeof a.skipped_by_field === 'object'
734
+ ? { skippedByField: a.skipped_by_field }
735
+ : {}),
730
736
  };
731
737
  }
732
738
  return {
733
739
  groups: (raw.groups ?? []).map((g) => ({
734
740
  category: String(g.category ?? ''),
735
741
  values: g.values ?? {},
742
+ // Exact decimals, as strings (C5): money is read from here.
743
+ exact: g.exact ?? {},
736
744
  // Below the collection's count minimum (#299): "Fewer than N".
737
745
  ...(typeof g.fewer_than === 'number' ? { fewerThan: g.fewer_than } : {}),
738
746
  })),
@@ -36,6 +36,9 @@ export type KbValueJson = {
36
36
  kind: 'range';
37
37
  from: KbValueJson;
38
38
  to: KbValueJson;
39
+ } | {
40
+ kind: 'field';
41
+ field: string;
39
42
  };
40
43
  export type KbWhereNode = {
41
44
  op: 'and' | 'or';
@@ -81,6 +84,17 @@ export declare const KbValue: {
81
84
  callerUserId: () => KbValueJson;
82
85
  /** One of the signed-in user's claims, as the server set it. Refused (caller_claim_missing) without it. */
83
86
  callerClaim: (name: string) => KbValueJson;
87
+ /**
88
+ * Another field of the same record, to compare this one with:
89
+ * `KbField('stock').lt(KbValue.field('reorder_level'))`. Both fields must be
90
+ * declared and their types must fit -- number with number, a date with a
91
+ * date or a date and time, a choice with a choice of the same values, a
92
+ * user with a user (or created_by), a reference with a reference to the
93
+ * same collection. With eq, neq, gt, gte, lt, lte, before, after,
94
+ * onOrBefore, onOrAfter and on. A record missing either value never
95
+ * matches. One hop through a reference is allowed: `'order_id.due_date'`.
96
+ */
97
+ field: (name: string) => KbValueJson;
84
98
  };
85
99
  /** A record field to filter on: a declared field, or id, created_at, updated_at, created_by. */
86
100
  export declare function KbField(name: string): {
@@ -81,6 +81,17 @@ exports.KbValue = {
81
81
  callerUserId: () => ({ kind: 'caller', path: 'user_id' }),
82
82
  /** One of the signed-in user's claims, as the server set it. Refused (caller_claim_missing) without it. */
83
83
  callerClaim: (name) => ({ kind: 'caller', path: `claims.${name}` }),
84
+ /**
85
+ * Another field of the same record, to compare this one with:
86
+ * `KbField('stock').lt(KbValue.field('reorder_level'))`. Both fields must be
87
+ * declared and their types must fit -- number with number, a date with a
88
+ * date or a date and time, a choice with a choice of the same values, a
89
+ * user with a user (or created_by), a reference with a reference to the
90
+ * same collection. With eq, neq, gt, gte, lt, lte, before, after,
91
+ * onOrBefore, onOrAfter and on. A record missing either value never
92
+ * matches. One hop through a reference is allowed: `'order_id.due_date'`.
93
+ */
94
+ field: (name) => ({ kind: 'field', field: name }),
84
95
  };
85
96
  /** A record field to filter on: a declared field, or id, created_at, updated_at, created_by. */
86
97
  function KbField(name) {
@@ -53,14 +53,31 @@ export type KoolbaseGroupBy = {
53
53
  * `count` counts RECORDS, so it takes no field and skips nothing. The others
54
54
  * read a field, and a record whose value is missing or not a number is
55
55
  * skipped and reported in the result's accounting.
56
+ *
57
+ * `sum_product` adds up the product of two fields -- amount times
58
+ * probability -- over the records where BOTH are numbers. `byScale: 100`
59
+ * says `by` is a percentage stored 0 to 100, so 40 multiplies as 0.4; when
60
+ * the field is declared a percent or probability, its declared scale is used
61
+ * and need not be sent.
56
62
  */
57
63
  export type KoolbaseMeasure = {
58
64
  aggregate: 'count';
59
65
  field?: never;
66
+ by?: never;
67
+ byScale?: never;
60
68
  as: string;
61
69
  } | {
62
70
  aggregate: 'sum' | 'avg' | 'min' | 'max';
63
71
  field: string;
72
+ by?: never;
73
+ byScale?: never;
74
+ as: string;
75
+ } | {
76
+ aggregate: 'sum_product';
77
+ field: string;
78
+ /** The second field: each record contributes field times by. */
79
+ by: string;
80
+ byScale?: 1 | 100;
64
81
  as: string;
65
82
  };
66
83
  /** One aggregation request. */
@@ -87,6 +104,15 @@ export interface KoolbaseAggregateGroup {
87
104
  category: string;
88
105
  /** Keyed by each measure's `as`. Null where a group had nothing to measure. */
89
106
  values: Record<string, number | null>;
107
+ /**
108
+ * Every value as an exact decimal string, computed without floating point:
109
+ * `"1234.50"`, never 1234.4999999. Same keys and the same nulls as
110
+ * `values`. Read money from here, and format the string: do not turn it
111
+ * back into a number first. A sum, min, max and sum_product are exact; an
112
+ * average is given to 12 decimal places; a count is an integer. Empty
113
+ * against an API from before exact totals.
114
+ */
115
+ exact: Record<string, string | null>;
90
116
  /**
91
117
  * The collection's count minimum (#299), when this group's count was below
92
118
  * it: every value is null, and what to show is "Fewer than N" -- never 0,
@@ -107,6 +133,12 @@ export interface KoolbaseAggregateAccounting {
107
133
  skipped: number;
108
134
  /** Present when skipped > 0. */
109
135
  skippedReason?: 'missing' | 'not_numeric' | 'mixed';
136
+ /**
137
+ * `sum_product` only, when skipped > 0: how many skipped records had no
138
+ * usable number in each of its two fields. A record lacking both counts
139
+ * under both.
140
+ */
141
+ skippedByField?: Record<string, number>;
110
142
  }
111
143
  /** The whole answer, including what could not be counted. */
112
144
  export interface KoolbaseAggregateResult {
@@ -715,6 +715,9 @@ export class KoolbaseDatabase {
715
715
  measures: request.measures.map((m) => ({
716
716
  aggregate: m.aggregate,
717
717
  ...(m.field ? { field: m.field } : {}),
718
+ // sum_product: the second field, and its scale when given.
719
+ ...(m.by ? { by: m.by } : {}),
720
+ ...(m.byScale ? { by_scale: m.byScale } : {}),
718
721
  as: m.as,
719
722
  })),
720
723
  });
@@ -724,12 +727,17 @@ export class KoolbaseDatabase {
724
727
  counted: Number(a.counted ?? 0),
725
728
  skipped: Number(a.skipped ?? 0),
726
729
  skippedReason: a.skipped_reason,
730
+ ...(a.skipped_by_field && typeof a.skipped_by_field === 'object'
731
+ ? { skippedByField: a.skipped_by_field }
732
+ : {}),
727
733
  };
728
734
  }
729
735
  return {
730
736
  groups: (raw.groups ?? []).map((g) => ({
731
737
  category: String(g.category ?? ''),
732
738
  values: g.values ?? {},
739
+ // Exact decimals, as strings (C5): money is read from here.
740
+ exact: g.exact ?? {},
733
741
  // Below the collection's count minimum (#299): "Fewer than N".
734
742
  ...(typeof g.fewer_than === 'number' ? { fewerThan: g.fewer_than } : {}),
735
743
  })),
@@ -36,6 +36,9 @@ export type KbValueJson = {
36
36
  kind: 'range';
37
37
  from: KbValueJson;
38
38
  to: KbValueJson;
39
+ } | {
40
+ kind: 'field';
41
+ field: string;
39
42
  };
40
43
  export type KbWhereNode = {
41
44
  op: 'and' | 'or';
@@ -81,6 +84,17 @@ export declare const KbValue: {
81
84
  callerUserId: () => KbValueJson;
82
85
  /** One of the signed-in user's claims, as the server set it. Refused (caller_claim_missing) without it. */
83
86
  callerClaim: (name: string) => KbValueJson;
87
+ /**
88
+ * Another field of the same record, to compare this one with:
89
+ * `KbField('stock').lt(KbValue.field('reorder_level'))`. Both fields must be
90
+ * declared and their types must fit -- number with number, a date with a
91
+ * date or a date and time, a choice with a choice of the same values, a
92
+ * user with a user (or created_by), a reference with a reference to the
93
+ * same collection. With eq, neq, gt, gte, lt, lte, before, after,
94
+ * onOrBefore, onOrAfter and on. A record missing either value never
95
+ * matches. One hop through a reference is allowed: `'order_id.due_date'`.
96
+ */
97
+ field: (name: string) => KbValueJson;
84
98
  };
85
99
  /** A record field to filter on: a declared field, or id, created_at, updated_at, created_by. */
86
100
  export declare function KbField(name: string): {
@@ -75,6 +75,17 @@ export const KbValue = {
75
75
  callerUserId: () => ({ kind: 'caller', path: 'user_id' }),
76
76
  /** One of the signed-in user's claims, as the server set it. Refused (caller_claim_missing) without it. */
77
77
  callerClaim: (name) => ({ kind: 'caller', path: `claims.${name}` }),
78
+ /**
79
+ * Another field of the same record, to compare this one with:
80
+ * `KbField('stock').lt(KbValue.field('reorder_level'))`. Both fields must be
81
+ * declared and their types must fit -- number with number, a date with a
82
+ * date or a date and time, a choice with a choice of the same values, a
83
+ * user with a user (or created_by), a reference with a reference to the
84
+ * same collection. With eq, neq, gt, gte, lt, lte, before, after,
85
+ * onOrBefore, onOrAfter and on. A record missing either value never
86
+ * matches. One hop through a reference is allowed: `'order_id.due_date'`.
87
+ */
88
+ field: (name) => ({ kind: 'field', field: name }),
78
89
  };
79
90
  /** A record field to filter on: a declared field, or id, created_at, updated_at, created_by. */
80
91
  export function KbField(name) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koolbase/core",
3
- "version": "12.51.0",
3
+ "version": "12.53.0",
4
4
  "description": "Koolbase SDK core — shared behaviour behind @koolbase/react-native and @koolbase/js. Install one of those, not this.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "react-native": "./dist/esm/index.js",