@cogover/sdk 0.12.1 → 0.13.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/README.md +61 -12
- package/dist/crypto.js +93 -23
- package/dist/data.d.ts +160 -13
- package/dist/data.js +30 -14
- package/dist/index.d.ts +1 -1
- package/dist/internal/base64.js +19 -4
- package/dist/internal/record-reads.js +183 -0
- package/dist/internal/utf8.js +0 -36
- package/dist/records.d.ts +18 -0
- package/docs/en/api-reference/background-jobs.md +4 -1
- package/docs/en/api-reference/cryptography.md +8 -4
- package/docs/en/api-reference/overview.md +9 -2
- package/docs/en/api-reference/record-triggers.md +12 -0
- package/docs/en/api-reference/records.md +378 -33
- package/docs/en/api-reference/state-and-locks.md +3 -0
- package/docs/en/usage-guide.md +66 -1
- package/package.json +1 -1
- package/scripts/generate-workspace-types.mjs +19 -7
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { CogoverApiError, ValidationError } from "../errors.js";
|
|
2
|
+
import { isRecord } from "./values.js";
|
|
3
|
+
/** Refusal of a read without a valid `fields` option; Cogover refuses such a read with the same message. */
|
|
4
|
+
export const FIELDS_REQUIRED = 'fields is required: pass field slugs or "*" (@cogover/sdk 0.13.0+)';
|
|
5
|
+
/** Refusal of a grouped aggregate on a client from `asUser()`; Cogover refuses it with the same message. */
|
|
6
|
+
export const AS_USER_GROUP_BY = "aggregate with groupBy is not supported for asUser(); use the caller or asSystem()";
|
|
7
|
+
/** Refusal of a metric that a client from `asUser()` cannot compute; Cogover refuses it with the same message. */
|
|
8
|
+
const AS_USER_METRICS = "asUser() without groupBy supports only count on id and count, sum, avg, min and max on "
|
|
9
|
+
+ "number fields; use the caller or asSystem()";
|
|
10
|
+
export const MAX_METRICS = 20;
|
|
11
|
+
export const MAX_GROUP_BY_FIELDS = 3;
|
|
12
|
+
export const MAX_GROUPS = 5000;
|
|
13
|
+
const FIELD_SLUG = /^[A-Za-z_][A-Za-z0-9_]*$/u;
|
|
14
|
+
const METRIC_NAME = /^[A-Za-z][A-Za-z0-9_]{0,63}$/u;
|
|
15
|
+
const METRIC_OPERATIONS = new Set(["count", "sum", "avg", "min", "max", "countDistinct"]);
|
|
16
|
+
/** Validates the `fields` option of `get`, `getMany` and `list`. */
|
|
17
|
+
export function normalizeFieldSelection(value) {
|
|
18
|
+
if (value === "*") {
|
|
19
|
+
return "*";
|
|
20
|
+
}
|
|
21
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
22
|
+
throw new ValidationError(FIELDS_REQUIRED);
|
|
23
|
+
}
|
|
24
|
+
const slugs = new Set();
|
|
25
|
+
for (const item of value) {
|
|
26
|
+
const slug = typeof item === "string" ? item.trim() : "";
|
|
27
|
+
// `["*"]` lands here too: "*" is only accepted on its own, not inside the list.
|
|
28
|
+
if (!FIELD_SLUG.test(slug)) {
|
|
29
|
+
throw new ValidationError(FIELDS_REQUIRED);
|
|
30
|
+
}
|
|
31
|
+
slugs.add(slug);
|
|
32
|
+
}
|
|
33
|
+
return [...slugs];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Validates the `expandLookups` option against the requested fields. `undefined` means that no lookup is expanded;
|
|
37
|
+
* Cogover checks that each named field is a lookup and that the linked fields exist.
|
|
38
|
+
*/
|
|
39
|
+
export function normalizeExpandLookups(value, fields) {
|
|
40
|
+
if (value === undefined || value === false) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
if (value === true) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
if (!isRecord(value)) {
|
|
47
|
+
throw new ValidationError('expandLookups must be true or an object that maps lookup field slugs to field slugs or "*"');
|
|
48
|
+
}
|
|
49
|
+
const lookupFields = Object.keys(value);
|
|
50
|
+
if (lookupFields.length === 0) {
|
|
51
|
+
throw new ValidationError("expandLookups must name at least one lookup field");
|
|
52
|
+
}
|
|
53
|
+
// No prototype, so that a field slug such as `__proto__` stays an ordinary key.
|
|
54
|
+
const normalized = Object.create(null);
|
|
55
|
+
for (const lookupField of lookupFields) {
|
|
56
|
+
if (!FIELD_SLUG.test(lookupField)) {
|
|
57
|
+
throw new ValidationError(`Invalid expandLookups field slug '${lookupField}'`);
|
|
58
|
+
}
|
|
59
|
+
if (fields !== "*" && !fields.includes(lookupField)) {
|
|
60
|
+
throw new ValidationError(`expandLookups field '${lookupField}' must also be listed in fields`);
|
|
61
|
+
}
|
|
62
|
+
const linkedFields = value[lookupField];
|
|
63
|
+
if (linkedFields === "*") {
|
|
64
|
+
normalized[lookupField] = "*";
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const invalid = () => new ValidationError(`expandLookups.${lookupField} must be "*" or a non-empty array of field slugs`);
|
|
68
|
+
if (!Array.isArray(linkedFields) || linkedFields.length === 0) {
|
|
69
|
+
throw invalid();
|
|
70
|
+
}
|
|
71
|
+
const slugs = new Set();
|
|
72
|
+
for (const item of linkedFields) {
|
|
73
|
+
const slug = typeof item === "string" ? item.trim() : "";
|
|
74
|
+
if (!FIELD_SLUG.test(slug)) {
|
|
75
|
+
throw invalid();
|
|
76
|
+
}
|
|
77
|
+
slugs.add(slug);
|
|
78
|
+
}
|
|
79
|
+
normalized[lookupField] = [...slugs];
|
|
80
|
+
}
|
|
81
|
+
return normalized;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Validates the options of `records.aggregate` and converts each metric to `{ op, field }`. `delegated` is true for
|
|
85
|
+
* a client from `asUser()`, which cannot group.
|
|
86
|
+
*/
|
|
87
|
+
export function normalizeAggregateOptions(options, delegated) {
|
|
88
|
+
if (!isRecord(options)) {
|
|
89
|
+
throw new ValidationError("aggregate() requires an options object with metrics");
|
|
90
|
+
}
|
|
91
|
+
const { where, groupBy, metrics, limit } = options;
|
|
92
|
+
if (groupBy !== undefined && delegated) {
|
|
93
|
+
throw new ValidationError(AS_USER_GROUP_BY);
|
|
94
|
+
}
|
|
95
|
+
const normalizedMetrics = normalizeMetrics(metrics);
|
|
96
|
+
if (delegated) {
|
|
97
|
+
// The other limits of asUser() depend on the field types, which Cogover checks.
|
|
98
|
+
const distinct = Object.keys(normalizedMetrics).find(name => normalizedMetrics[name].op === "countDistinct");
|
|
99
|
+
if (distinct !== undefined) {
|
|
100
|
+
throw new ValidationError(`metrics.${distinct}: ${AS_USER_METRICS}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (groupBy === undefined) {
|
|
104
|
+
if (limit !== undefined) {
|
|
105
|
+
throw new ValidationError("aggregate() accepts limit only together with groupBy");
|
|
106
|
+
}
|
|
107
|
+
return where === undefined ? { metrics: normalizedMetrics } : { where, metrics: normalizedMetrics };
|
|
108
|
+
}
|
|
109
|
+
const normalizedGroupBy = normalizeGroupBy(groupBy);
|
|
110
|
+
if (limit !== undefined
|
|
111
|
+
&& (typeof limit !== "number" || !Number.isSafeInteger(limit) || limit < 1 || limit > MAX_GROUPS)) {
|
|
112
|
+
throw new ValidationError(`aggregate() limit must be an integer from 1 to ${MAX_GROUPS}`);
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
...(where === undefined ? {} : { where }),
|
|
116
|
+
groupBy: normalizedGroupBy,
|
|
117
|
+
metrics: normalizedMetrics,
|
|
118
|
+
...(limit === undefined ? {} : { limit }),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function normalizeMetrics(value) {
|
|
122
|
+
const names = isRecord(value) ? Object.keys(value) : [];
|
|
123
|
+
if (!isRecord(value) || names.length === 0 || names.length > MAX_METRICS) {
|
|
124
|
+
throw new ValidationError(`aggregate() requires metrics: an object with 1 to ${MAX_METRICS} named metrics`);
|
|
125
|
+
}
|
|
126
|
+
const normalized = Object.create(null);
|
|
127
|
+
for (const name of names) {
|
|
128
|
+
if (!METRIC_NAME.test(name)) {
|
|
129
|
+
throw new ValidationError(`Invalid metric name '${name}': use a letter followed by up to 63 letters, digits or underscores`);
|
|
130
|
+
}
|
|
131
|
+
const metric = value[name];
|
|
132
|
+
const operations = isRecord(metric) ? Object.keys(metric) : [];
|
|
133
|
+
const op = operations[0];
|
|
134
|
+
const field = isRecord(metric) && op !== undefined && typeof metric[op] === "string"
|
|
135
|
+
? metric[op].trim()
|
|
136
|
+
: "";
|
|
137
|
+
if (operations.length !== 1 || op === undefined || !METRIC_OPERATIONS.has(op) || !FIELD_SLUG.test(field)) {
|
|
138
|
+
throw new ValidationError(`Metric '${name}' must be exactly one of { count }, { sum }, { avg }, { min }, `
|
|
139
|
+
+ "{ max } or { countDistinct } with a field slug");
|
|
140
|
+
}
|
|
141
|
+
normalized[name] = { op, field };
|
|
142
|
+
}
|
|
143
|
+
return normalized;
|
|
144
|
+
}
|
|
145
|
+
function normalizeGroupBy(value) {
|
|
146
|
+
const message = `aggregate() groupBy must list 1 to ${MAX_GROUP_BY_FIELDS} field slugs`;
|
|
147
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
148
|
+
throw new ValidationError(message);
|
|
149
|
+
}
|
|
150
|
+
const slugs = new Set();
|
|
151
|
+
for (const item of value) {
|
|
152
|
+
const slug = typeof item === "string" ? item.trim() : "";
|
|
153
|
+
if (!FIELD_SLUG.test(slug)) {
|
|
154
|
+
throw new ValidationError(`Invalid groupBy field slug '${String(item)}'`);
|
|
155
|
+
}
|
|
156
|
+
slugs.add(slug);
|
|
157
|
+
}
|
|
158
|
+
if (slugs.size > MAX_GROUP_BY_FIELDS) {
|
|
159
|
+
throw new ValidationError(message);
|
|
160
|
+
}
|
|
161
|
+
return [...slugs];
|
|
162
|
+
}
|
|
163
|
+
/** Checks the shape of a `records.aggregate` result before the script sees it. */
|
|
164
|
+
export function requireAggregateResult(result, request) {
|
|
165
|
+
const metricNames = Object.keys(request.metrics);
|
|
166
|
+
const validValues = (values) => isRecord(values) && metricNames.every(name => {
|
|
167
|
+
const value = values[name];
|
|
168
|
+
return value === null || (typeof value === "number" && Number.isFinite(value));
|
|
169
|
+
});
|
|
170
|
+
const valid = isRecord(result) && (request.groupBy === undefined
|
|
171
|
+
? validValues(result.values)
|
|
172
|
+
: Array.isArray(result.groups) && typeof result.truncated === "boolean"
|
|
173
|
+
&& result.groups.every(group => isRecord(group) && validValues(group.values) && isRecord(group.key)
|
|
174
|
+
&& request.groupBy.every(slug => isGroupKeyValue(group.key[slug]))));
|
|
175
|
+
if (!valid) {
|
|
176
|
+
throw new CogoverApiError("INVALID_BRIDGE_RESPONSE", "records.aggregate returned an invalid result");
|
|
177
|
+
}
|
|
178
|
+
return result;
|
|
179
|
+
}
|
|
180
|
+
function isGroupKeyValue(value) {
|
|
181
|
+
return typeof value === "string" || typeof value === "boolean"
|
|
182
|
+
|| (typeof value === "number" && Number.isFinite(value));
|
|
183
|
+
}
|
package/dist/internal/utf8.js
CHANGED
|
@@ -18,39 +18,3 @@ export function utf8ByteLength(value) {
|
|
|
18
18
|
return value.length + matchedUnits(TWO_BYTE_UNITS) + 2 * matchedUnits(THREE_BYTE_UNITS)
|
|
19
19
|
+ pairedUnits + 2 * (surrogateUnits - pairedUnits);
|
|
20
20
|
}
|
|
21
|
-
/** UTF-8 encoding without TextEncoder, which the sandbox does not provide; a lone surrogate becomes U+FFFD. */
|
|
22
|
-
export function encodeUtf8(value) {
|
|
23
|
-
const bytes = new Uint8Array(utf8ByteLength(value));
|
|
24
|
-
let position = 0;
|
|
25
|
-
for (let index = 0; index < value.length; index++) {
|
|
26
|
-
let code = value.charCodeAt(index);
|
|
27
|
-
if (code >= 0xd800 && code <= 0xdbff && index + 1 < value.length) {
|
|
28
|
-
const low = value.charCodeAt(index + 1);
|
|
29
|
-
if (low >= 0xdc00 && low <= 0xdfff) {
|
|
30
|
-
code = 0x10000 + ((code - 0xd800) << 10) + (low - 0xdc00);
|
|
31
|
-
index++;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
if (code >= 0xd800 && code <= 0xdfff)
|
|
35
|
-
code = 0xfffd;
|
|
36
|
-
if (code < 0x80) {
|
|
37
|
-
bytes[position++] = code;
|
|
38
|
-
}
|
|
39
|
-
else if (code < 0x800) {
|
|
40
|
-
bytes[position++] = 0xc0 | (code >> 6);
|
|
41
|
-
bytes[position++] = 0x80 | (code & 63);
|
|
42
|
-
}
|
|
43
|
-
else if (code < 0x10000) {
|
|
44
|
-
bytes[position++] = 0xe0 | (code >> 12);
|
|
45
|
-
bytes[position++] = 0x80 | ((code >> 6) & 63);
|
|
46
|
-
bytes[position++] = 0x80 | (code & 63);
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
bytes[position++] = 0xf0 | (code >> 18);
|
|
50
|
-
bytes[position++] = 0x80 | ((code >> 12) & 63);
|
|
51
|
-
bytes[position++] = 0x80 | ((code >> 6) & 63);
|
|
52
|
-
bytes[position++] = 0x80 | (code & 63);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return bytes;
|
|
56
|
-
}
|
package/dist/records.d.ts
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
|
+
import type { WorkspaceObjects } from "./workspace.js";
|
|
1
2
|
export type CogoverRecordId = string & {
|
|
2
3
|
readonly __cogoverRecordIdBrand: unique symbol;
|
|
3
4
|
};
|
|
5
|
+
/** Fields of a linked record: the target Object's fields when it is declared in `WorkspaceObjects`. */
|
|
6
|
+
type LinkedRecordFields<TObjectSlug extends string> = TObjectSlug extends keyof WorkspaceObjects ? Readonly<Partial<WorkspaceObjects[TObjectSlug]>> : Readonly<Record<string, unknown>>;
|
|
7
|
+
/** Value of a lookup or reference field: the linked record. */
|
|
4
8
|
export interface RecordReference<TObjectSlug extends string = string> {
|
|
5
9
|
readonly id: CogoverRecordId;
|
|
10
|
+
/**
|
|
11
|
+
* Name of the linked record. A record read through `data` without `expandLookups` has `""` here, except in a
|
|
12
|
+
* `reference` field, which stores the name with the record; so does a linked record that could not be read.
|
|
13
|
+
* The `new` and `old` values of a record trigger always carry the name.
|
|
14
|
+
*/
|
|
6
15
|
readonly name: string;
|
|
7
16
|
readonly objectSlug?: TObjectSlug;
|
|
17
|
+
/**
|
|
18
|
+
* Fields of the linked record, present only when the read expanded this lookup with `expandLookups` and the
|
|
19
|
+
* linked record could be read. Lookups inside these fields are not expanded.
|
|
20
|
+
*/
|
|
21
|
+
readonly fields?: LinkedRecordFields<TObjectSlug>;
|
|
8
22
|
}
|
|
9
23
|
export interface UrlValue {
|
|
10
24
|
readonly url: string;
|
|
@@ -25,6 +39,10 @@ export interface CogoverRecord<TFields> {
|
|
|
25
39
|
readonly updatedAt: number;
|
|
26
40
|
readonly createdBy?: {
|
|
27
41
|
readonly id: string;
|
|
42
|
+
/**
|
|
43
|
+
* `""` unless the read used `expandLookups`; then the `name` field of the creator's personnel record,
|
|
44
|
+
* which can be a generated code rather than the person's full name.
|
|
45
|
+
*/
|
|
28
46
|
readonly name: string;
|
|
29
47
|
};
|
|
30
48
|
};
|
|
@@ -251,7 +251,10 @@ is not reached. When jobs are not enabled for the Workspace, the error is
|
|
|
251
251
|
is `"system"`, and `data.object()` works only if the project's approved identity
|
|
252
252
|
policy sets `allowInternalSystem: true`; otherwise its calls throw
|
|
253
253
|
`PermissionDeniedError` with `details.reason` `"IDENTITY_NOT_GRANTED"`.
|
|
254
|
-
`data.asUser()` and `data.asSystem()` follow the identity policy as usual.
|
|
254
|
+
`data.asUser()` and `data.asSystem()` follow the identity policy as usual. In a run
|
|
255
|
+
that has a user, `records.aggregate` through `data.object()` has the limits of
|
|
256
|
+
`data.asUser()`; use `data.asSystem()` to group (see
|
|
257
|
+
[Aggregates](records.md#aggregates)).
|
|
255
258
|
- **Capabilities.** A job may read and write records, call `fetch`, use `state` and
|
|
256
259
|
`locks`, send notifications and email, enqueue jobs, read secrets, and use `crypto`,
|
|
257
260
|
within the runtime limits of the job lane.
|
|
@@ -91,10 +91,13 @@ const token = await crypto.randomUUID();
|
|
|
91
91
|
- `randomBytes(length)` returns `length` cryptographically secure random bytes;
|
|
92
92
|
`length` is an integer from 1 through 1024.
|
|
93
93
|
- `randomUUID()` returns a random version 4 UUID such as
|
|
94
|
-
`"3f9c2d6e-8b1a-4c7d-9e2f-1a2b3c4d5e6f"`.
|
|
94
|
+
`"3f9c2d6e-8b1a-4c7d-9e2f-1a2b3c4d5e6f"`. Random bytes are fetched 1,024 at a
|
|
95
|
+
time, so one capability call covers 64 UUIDs.
|
|
95
96
|
- `timingSafeEqual(a, b)` compares two strings or byte arrays in constant time and
|
|
96
|
-
returns `false` for different lengths. Strings are compared as UTF-8 bytes.
|
|
97
|
-
|
|
97
|
+
returns `false` for different lengths. Strings are compared as UTF-8 bytes. Each
|
|
98
|
+
value is limited to 256 KiB, a string measured in UTF-8 bytes; a larger one
|
|
99
|
+
throws `ValidationError`. Use it for every comparison of a signature, token, or
|
|
100
|
+
key.
|
|
98
101
|
|
|
99
102
|
### Keys
|
|
100
103
|
|
|
@@ -308,7 +311,8 @@ close to 256 KiB together with other arguments, therefore throws `ValidationErro
|
|
|
308
311
|
before the call even though each value is within its own limit. The key, algorithm, and option errors that
|
|
309
312
|
Cogover detects, such as a key of the wrong size or type, also throw
|
|
310
313
|
`ValidationError`. Every operation except `timingSafeEqual` is a capability call
|
|
311
|
-
and counts towards the invocation's limits
|
|
314
|
+
and counts towards the invocation's limits; `randomUUID` makes one call for every
|
|
315
|
+
64 UUIDs.
|
|
312
316
|
|
|
313
317
|
The operations work in every context, including before-change triggers, with a key
|
|
314
318
|
given in the call. A `{ secret }` key follows the rules of `secrets.get`: a
|
|
@@ -12,7 +12,7 @@ The reference is split into the following pages. Each page covers one area of th
|
|
|
12
12
|
| Page | Covers |
|
|
13
13
|
|---|---|
|
|
14
14
|
| [Scripts and HTTP routes](scripts-and-routes.md) | `defineScript`; HTTP routers and request context; Invocation identity and workspace; HTTP response API; Logging |
|
|
15
|
-
| [Records and schema](records.md) | Record API; Filters and sorting; Record execution identities; Schema API; Workspace declarations |
|
|
15
|
+
| [Records and schema](records.md) | Record API; Choosing fields; Linked records; Aggregates; Filters and sorting; Record execution identities; Schema API; Workspace declarations |
|
|
16
16
|
| [Record triggers](record-triggers.md) | Record triggers |
|
|
17
17
|
| [Background jobs](background-jobs.md) | Background jobs |
|
|
18
18
|
| [Outbound HTTP and secrets](fetch-and-secrets.md) | Outbound HTTP `fetch`; Secrets and credentials |
|
|
@@ -89,6 +89,12 @@ throws it to report a temporary failure, and Cogover runs the handler again late
|
|
|
89
89
|
meaning: a script that lets it escape returns HTTP 422, and a before-change trigger
|
|
90
90
|
that throws it rejects the write like any other failure.
|
|
91
91
|
|
|
92
|
+
A record read (`get`, `getMany`, or `list`) without a valid `fields` option throws
|
|
93
|
+
`ValidationError` with the message `fields is required: pass field slugs or "*"
|
|
94
|
+
(@cogover/sdk 0.13.0+)`. Cogover returns the same error to a project version that was
|
|
95
|
+
published with an older SDK and reads without `fields` (see
|
|
96
|
+
[Choosing fields](records.md#choosing-fields)).
|
|
97
|
+
|
|
92
98
|
`jobs.enqueue`, `secrets.get`, and `crypto` fail with `ValidationError`,
|
|
93
99
|
`PermissionDeniedError`, or `RateLimitError` as described in their sections, and
|
|
94
100
|
with `CogoverApiError` whose `code` is `JOBS_DISABLED` or `SECRETS_DISABLED` when
|
|
@@ -179,7 +185,8 @@ set by the platform and may differ between environments.
|
|
|
179
185
|
262,144 bytes. A larger request is refused before it is sent with `ValidationError`,
|
|
180
186
|
or with `CogoverApiError` `code: "FETCH_REQUEST_TOO_LARGE"` for `fetch`; `details.limit`
|
|
181
187
|
is the limit. A capability result larger than 1,048,576 bytes is withheld the same
|
|
182
|
-
way (`FETCH_RESPONSE_TOO_LARGE` for `fetch`): ask for fewer records or
|
|
188
|
+
way (`FETCH_RESPONSE_TOO_LARGE` for `fetch`): ask for fewer records, or name the
|
|
189
|
+
fields instead of `"*"`. The
|
|
183
190
|
operation itself did run, so a write may have completed.
|
|
184
191
|
- When the Workspace already has too many executions waiting, a new HTTP call is
|
|
185
192
|
refused with HTTP 429 and `code: "RATE_LIMITED"` before anything runs; retry it later.
|
|
@@ -287,6 +287,14 @@ trigger that only declares `"delete"` has `new: null`. `TriggerNewValues` and
|
|
|
287
287
|
`TriggerOldValues` make every field optional because a field that is not listed in
|
|
288
288
|
`fields`, or that has no value, is `undefined`.
|
|
289
289
|
|
|
290
|
+
Lookup and reference values in `new` and `old` carry the linked record's `name`. This
|
|
291
|
+
differs from records read through `data`, whose lookup values have `name: ""` unless
|
|
292
|
+
the read uses `expandLookups` (see [Linked records](records.md#linked-records)). Reads
|
|
293
|
+
through `data` in a trigger follow the same rules as in a script, including the
|
|
294
|
+
required `fields`. For a change made by a user, `data.object()` reads as that user, so
|
|
295
|
+
its `records.aggregate` has the limits of `data.asUser()`: use `data.asSystem()` to
|
|
296
|
+
group (see [Aggregates](records.md#aggregates)).
|
|
297
|
+
|
|
290
298
|
### Validating and changing records
|
|
291
299
|
|
|
292
300
|
This section applies to before-change triggers. In an after-change trigger the
|
|
@@ -387,6 +395,10 @@ a request that only validates input without saving.
|
|
|
387
395
|
receives `msg: "BEFORE_CHANGE_TRIGGER_FAILED"` without internal details.
|
|
388
396
|
- **Time.** Each call must finish within `timeoutMs`. All before-change triggers of
|
|
389
397
|
one write share a total budget of 8 seconds.
|
|
398
|
+
- **Reads delay the write.** The user's write waits while the handler reads. Each
|
|
399
|
+
expanded lookup reads another Object, and `records.aggregate`, which is allowed, is
|
|
400
|
+
computed from a search index that is about one second behind: it does not count the
|
|
401
|
+
change being made, and may miss changes saved just before it. Keep such calls few.
|
|
390
402
|
|
|
391
403
|
### After-change execution rules
|
|
392
404
|
|