@protobase/schema 0.122.0 → 0.123.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.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/prepare.d.ts +43 -0
- package/dist/prepare.js +12 -0
- package/dist/resource.d.ts +20 -1
- package/dist/resource.js +24 -6
- package/package.json +2 -1
package/dist/index.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ import { BucketFields, BucketMeasuresInput, BucketsSpec } from "./buckets/bucket
|
|
|
18
18
|
import { ComputedReveal, RevealCaller } from "./computed/computed-reveal.js";
|
|
19
19
|
import { CreatedOptions, DeletedOptions, EventActor, EventContext, EventHelpers, EventMatch, EventSpec, UndeletedOptions, UpdatedOptions } from "./events/event-helpers.js";
|
|
20
20
|
import { Access, InferRecord, Issue, KeyOf, KeyRefs, RecordKey, RecordOf } from "./resource-types.js";
|
|
21
|
+
import { FieldIssue, PrepareContext, PrepareSession, Preparer } from "./prepare.js";
|
|
21
22
|
import { DerivedSpec, ResourceBuilder, SearchRef, SearchRefs, TenantOptions, Validator, resource } from "./resource.js";
|
|
22
23
|
import { isRecordCondition } from "./record-when.js";
|
|
23
24
|
import { ChartInput, GroupChartInput, PeriodChartInput } from "./view-chart.js";
|
|
@@ -58,4 +59,4 @@ import { CellKind, FileCell, FileDate, FileJson, cellKind, isEmptyFileCell } fro
|
|
|
58
59
|
import { UserTableFieldType, UserTableNameIssue, userTableNameIssue } from "./user-tables/names.js";
|
|
59
60
|
import { ColumnType, ColumnTypeReader, DecimalType, columnTypeReader, inferColumnType, maxShortText, minWholeDigits, widerFieldTypes } from "./user-tables/column-type.js";
|
|
60
61
|
import { fieldNamesOf, tableNameOf } from "./user-tables/column-names.js";
|
|
61
|
-
export { type Access, type AccessAction, type AccessContext, type AccessFn, type AccessResult, type AccessSource, type AccessUser, type ActionInputKind, type ActionInputModel, type ActionModel, type ActionRun, type AnyField, type AppAddress, type ArgumentSpec, type ArgumentType, type ArgumentsSpec, type AssistantAction, type AssistantActionRequest, type AssistantCardPart, type AssistantDiff, type AssistantEvent, type AssistantMessage, type AssistantMessageRequest, type AssistantPart, type AssistantState, type AssistantStep, type AssistantStepState, type AssistantTablePart, type AssistantTextPart, type AssistantTone, type AssistantWidgetPart, type AttachmentsModel, type BadgeShape, type BadgeTone, type BlockElementModel, type BlocksSectionModel, type BoardDragModel, type BottomBarItemModel, type BottomBarModel, type BottomBarNames, type BottomBarSource, type BucketEvery, type BucketFields, type BucketMeasure, type BucketMeasuresInput, type BucketsModel, type BucketsSpec, type CalendarView, type Capability, type CapabilityAction, type CapabilityLimit, type CapabilityScope, type CardFavoriteModel, type CardPhotosModel, type CellKind, type ChartGranularity, type ChartInput, type ChartModel, type ChartRange, type CheckModel, type CheckResult, type CheckedFilter, type ColumnType, type ColumnTypeReader, type CommentsSectionModel, type CompareOp, type ComponentRef, type ComposerSectionModel, type ComputedReveal, type ConnectAddress, type Constraint, type ConversationPageModel, type ConversationThreadModel, type CreateModel, type CreatedOptions, type CustomFallbackItemModel, type CustomSectionModel, type DatePreset, type DecimalType, type DeletedOptions, type DerivedModel, type DerivedSpec, type DerivedTarget, type DurationLiteral, type DurationUnit, type Encrypted, type EvaluateOptions, type EventActor, type EventContext, type EventHelpers, type EventMatch, type EventModel, type EventOperation, type EventSpec, type ExactCapability, type ExportModel, type Field, type FieldAccess, type FieldFormat, type FieldMeta, type FieldModel, type FieldPath, type FieldRefValue, type FieldRefs, type FieldRule, type FieldType, type FieldTypeDef, type FieldView, type FieldViewModel, type FieldViews, type FieldWidgetModel, type FieldsInput, type FileCell, type FileDate, type FileField, type FileFieldModel, type FileJson, type FileOptions, type FileProcessor, type FileProcessorInput, type FilterError, type FilterErrorCode, type FilterExpr, type FilterLayout, type FilterLimits, type FilterOf, type FilterParse, type FilterValue, type FilterWidgetModel, type FormAddress, type FormInput, type FormModel, type GalleryModel, type Grant, type GroupChartInput, type GroupChartModel, type GroupItemModel, type GroupModel, type HeldCapability, type HelpWhen, type HistorySectionModel, type ImageFormat, type ImageVariant, type ImageVariantOptions, type InferRecord, type Issue, type JsonValue, type KeyOf, type KeyRefs, type KeyType, type KeyValue, type LayoutItemModel, type ListColumn, type ListDisplayModel, type ListDragModel, type ListModel, type ListQuery, type Literal, type Lookup, type MapSectionModel, type MapView, type NamedListModel, type NavModel, type NavRecentModel, type NavStartModel, type NowValue, type OrderByCheck, type OrderByItem, type OrderByParse, type PageWidth, type ParentAccess, type ParentModel, type PeriodChartInput, type PeriodChartModel, type PositionAfterModel, type ReactionsModel, type ReadPositionModel, type RecordChartModel, type RecordChartRange, type RecordKey, type RecordMode, type RecordOf, type RecordPageModel, type RecordTrendModel, type RecordWhen, type Ref, type Refs, type RelatedColumnModel, type RelatedFilter, type RelatedModel, type RelatedPresentation, type RelationField, type ResolveOptions, type ResolvedAccess, type ResourceBuilder, type ResourceModel, type RevealCaller, type RoleEntry, type RoleLimit, type Roles, type RolesOptions, type Rollup, type RollupCount, type RollupFn, type RollupSource, type RollupValue, type RowButtonChoice, type RowButtonModel, type RowFilterOperation, type Rule, type SaveFeedback, type SaveSearchModel, type SearchMatch, type SearchRef, type SearchRefs, type SearchResultModel, type SearchesOfModel, type SectionModel, type SectionPresentation, type ShowWhen, type SignInAddress, type SignInPromptModel, type SignInStep, type SignInViewModel, type SortSpec, type Span, type StatusTone, type TabItemModel, type TabModel, type TenantOptions, type ThreadSectionModel, type TimelineView, type TitleFromModel, type ToolCallFields, type UndeletedOptions, type UpdatedOptions, type UserMenuItemModel, type UserMenuModel, type UserMenuSource, type UserTableFieldType, type UserTableNameIssue, type Validator, type ViewBuilder, type ViewModel, type ViewProps, accessWarnings, addressWords, anonymousRole, appPath, argumentsSchema, badgeTones, bottomBar, bottomBarErrors, cellKind, checkFilter, checkOrderBy, checkRecord, columnTypeReader, connectParams, connectPath, connectRoute, createWhere, defineRoles, encrypted, enumLabel, evaluateFilter, f, fieldNamesOf, fieldRef, fieldsUsedBy, fitsKeyType, formPath, formRoute, imageFormats, inferColumnType, isBadgeTone, isEmptyFileCell, isRecordCondition, keyTypes, layout as l, maxBottomBarItems, maxJsonDepth, maxShortText, membersRule, minWholeDigits, parseFilter, parseOrderBy, pickView, printFilter, printOrderBy, readAppPath, readConnectAddress, readFormAddress, readSignInAddress, resolveAccess, resource, restrictModel, rule, signInParams, signInPath, signInRoute, signInSteps, tableNameOf, userMenu, userTableNameIssue, view, where, widerFieldTypes };
|
|
62
|
+
export { type Access, type AccessAction, type AccessContext, type AccessFn, type AccessResult, type AccessSource, type AccessUser, type ActionInputKind, type ActionInputModel, type ActionModel, type ActionRun, type AnyField, type AppAddress, type ArgumentSpec, type ArgumentType, type ArgumentsSpec, type AssistantAction, type AssistantActionRequest, type AssistantCardPart, type AssistantDiff, type AssistantEvent, type AssistantMessage, type AssistantMessageRequest, type AssistantPart, type AssistantState, type AssistantStep, type AssistantStepState, type AssistantTablePart, type AssistantTextPart, type AssistantTone, type AssistantWidgetPart, type AttachmentsModel, type BadgeShape, type BadgeTone, type BlockElementModel, type BlocksSectionModel, type BoardDragModel, type BottomBarItemModel, type BottomBarModel, type BottomBarNames, type BottomBarSource, type BucketEvery, type BucketFields, type BucketMeasure, type BucketMeasuresInput, type BucketsModel, type BucketsSpec, type CalendarView, type Capability, type CapabilityAction, type CapabilityLimit, type CapabilityScope, type CardFavoriteModel, type CardPhotosModel, type CellKind, type ChartGranularity, type ChartInput, type ChartModel, type ChartRange, type CheckModel, type CheckResult, type CheckedFilter, type ColumnType, type ColumnTypeReader, type CommentsSectionModel, type CompareOp, type ComponentRef, type ComposerSectionModel, type ComputedReveal, type ConnectAddress, type Constraint, type ConversationPageModel, type ConversationThreadModel, type CreateModel, type CreatedOptions, type CustomFallbackItemModel, type CustomSectionModel, type DatePreset, type DecimalType, type DeletedOptions, type DerivedModel, type DerivedSpec, type DerivedTarget, type DurationLiteral, type DurationUnit, type Encrypted, type EvaluateOptions, type EventActor, type EventContext, type EventHelpers, type EventMatch, type EventModel, type EventOperation, type EventSpec, type ExactCapability, type ExportModel, type Field, type FieldAccess, type FieldFormat, FieldIssue, type FieldMeta, type FieldModel, type FieldPath, type FieldRefValue, type FieldRefs, type FieldRule, type FieldType, type FieldTypeDef, type FieldView, type FieldViewModel, type FieldViews, type FieldWidgetModel, type FieldsInput, type FileCell, type FileDate, type FileField, type FileFieldModel, type FileJson, type FileOptions, type FileProcessor, type FileProcessorInput, type FilterError, type FilterErrorCode, type FilterExpr, type FilterLayout, type FilterLimits, type FilterOf, type FilterParse, type FilterValue, type FilterWidgetModel, type FormAddress, type FormInput, type FormModel, type GalleryModel, type Grant, type GroupChartInput, type GroupChartModel, type GroupItemModel, type GroupModel, type HeldCapability, type HelpWhen, type HistorySectionModel, type ImageFormat, type ImageVariant, type ImageVariantOptions, type InferRecord, type Issue, type JsonValue, type KeyOf, type KeyRefs, type KeyType, type KeyValue, type LayoutItemModel, type ListColumn, type ListDisplayModel, type ListDragModel, type ListModel, type ListQuery, type Literal, type Lookup, type MapSectionModel, type MapView, type NamedListModel, type NavModel, type NavRecentModel, type NavStartModel, type NowValue, type OrderByCheck, type OrderByItem, type OrderByParse, type PageWidth, type ParentAccess, type ParentModel, type PeriodChartInput, type PeriodChartModel, type PositionAfterModel, type PrepareContext, type PrepareSession, type Preparer, type ReactionsModel, type ReadPositionModel, type RecordChartModel, type RecordChartRange, type RecordKey, type RecordMode, type RecordOf, type RecordPageModel, type RecordTrendModel, type RecordWhen, type Ref, type Refs, type RelatedColumnModel, type RelatedFilter, type RelatedModel, type RelatedPresentation, type RelationField, type ResolveOptions, type ResolvedAccess, type ResourceBuilder, type ResourceModel, type RevealCaller, type RoleEntry, type RoleLimit, type Roles, type RolesOptions, type Rollup, type RollupCount, type RollupFn, type RollupSource, type RollupValue, type RowButtonChoice, type RowButtonModel, type RowFilterOperation, type Rule, type SaveFeedback, type SaveSearchModel, type SearchMatch, type SearchRef, type SearchRefs, type SearchResultModel, type SearchesOfModel, type SectionModel, type SectionPresentation, type ShowWhen, type SignInAddress, type SignInPromptModel, type SignInStep, type SignInViewModel, type SortSpec, type Span, type StatusTone, type TabItemModel, type TabModel, type TenantOptions, type ThreadSectionModel, type TimelineView, type TitleFromModel, type ToolCallFields, type UndeletedOptions, type UpdatedOptions, type UserMenuItemModel, type UserMenuModel, type UserMenuSource, type UserTableFieldType, type UserTableNameIssue, type Validator, type ViewBuilder, type ViewModel, type ViewProps, accessWarnings, addressWords, anonymousRole, appPath, argumentsSchema, badgeTones, bottomBar, bottomBarErrors, cellKind, checkFilter, checkOrderBy, checkRecord, columnTypeReader, connectParams, connectPath, connectRoute, createWhere, defineRoles, encrypted, enumLabel, evaluateFilter, f, fieldNamesOf, fieldRef, fieldsUsedBy, fitsKeyType, formPath, formRoute, imageFormats, inferColumnType, isBadgeTone, isEmptyFileCell, isRecordCondition, keyTypes, layout as l, maxBottomBarItems, maxJsonDepth, maxShortText, membersRule, minWholeDigits, parseFilter, parseOrderBy, pickView, printFilter, printOrderBy, readAppPath, readConnectAddress, readFormAddress, readSignInAddress, resolveAccess, resource, restrictModel, rule, signInParams, signInPath, signInRoute, signInSteps, tableNameOf, userMenu, userTableNameIssue, view, where, widerFieldTypes };
|
package/dist/index.js
CHANGED
|
@@ -26,6 +26,7 @@ import { fitsKeyType, keyTypes } from "./keys.js";
|
|
|
26
26
|
import { addressWords, appPath, readAppPath } from "./app-address.js";
|
|
27
27
|
import { connectParams, connectPath, connectRoute, readConnectAddress } from "./connect-address.js";
|
|
28
28
|
import { readSignInAddress, signInParams, signInPath, signInRoute, signInSteps } from "./sign-in-address.js";
|
|
29
|
+
import { FieldIssue } from "./prepare.js";
|
|
29
30
|
import { encrypted } from "./encrypted-value.js";
|
|
30
31
|
import { rule } from "./access/rule.js";
|
|
31
32
|
import { defineRoles } from "./access/roles.js";
|
|
@@ -35,4 +36,4 @@ import { cellKind, isEmptyFileCell } from "./user-tables/cell-kind.js";
|
|
|
35
36
|
import { columnTypeReader, inferColumnType, maxShortText, minWholeDigits, widerFieldTypes } from "./user-tables/column-type.js";
|
|
36
37
|
import { userTableNameIssue } from "./user-tables/names.js";
|
|
37
38
|
import { fieldNamesOf, tableNameOf } from "./user-tables/column-names.js";
|
|
38
|
-
export { accessWarnings, addressWords, anonymousRole, appPath, argumentsSchema, badgeTones, bottomBar, bottomBarErrors, cellKind, checkFilter, checkOrderBy, checkRecord, columnTypeReader, connectParams, connectPath, connectRoute, createWhere, defineRoles, encrypted, enumLabel, evaluateFilter, f, fieldNamesOf, fieldRef, fieldsUsedBy, fitsKeyType, formPath, formRoute, imageFormats, inferColumnType, isBadgeTone, isEmptyFileCell, isRecordCondition, keyTypes, layout as l, maxBottomBarItems, maxJsonDepth, maxShortText, membersRule, minWholeDigits, parseFilter, parseOrderBy, pickView, printFilter, printOrderBy, readAppPath, readConnectAddress, readFormAddress, readSignInAddress, resolveAccess, resource, restrictModel, rule, signInParams, signInPath, signInRoute, signInSteps, tableNameOf, userMenu, userTableNameIssue, view, where, widerFieldTypes };
|
|
39
|
+
export { FieldIssue, accessWarnings, addressWords, anonymousRole, appPath, argumentsSchema, badgeTones, bottomBar, bottomBarErrors, cellKind, checkFilter, checkOrderBy, checkRecord, columnTypeReader, connectParams, connectPath, connectRoute, createWhere, defineRoles, encrypted, enumLabel, evaluateFilter, f, fieldNamesOf, fieldRef, fieldsUsedBy, fitsKeyType, formPath, formRoute, imageFormats, inferColumnType, isBadgeTone, isEmptyFileCell, isRecordCondition, keyTypes, layout as l, maxBottomBarItems, maxJsonDepth, maxShortText, membersRule, minWholeDigits, parseFilter, parseOrderBy, pickView, printFilter, printOrderBy, readAppPath, readConnectAddress, readFormAddress, readSignInAddress, resolveAccess, resource, restrictModel, rule, signInParams, signInPath, signInRoute, signInSteps, tableNameOf, userMenu, userTableNameIssue, view, where, widerFieldTypes };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { AccessUser } from "./access/types.js";
|
|
2
|
+
import { Kysely } from "kysely";
|
|
3
|
+
//#region src/prepare.d.ts
|
|
4
|
+
/** The caller of a write `.prepare()` runs for, as the server resolved it. */
|
|
5
|
+
export type PrepareSession = {
|
|
6
|
+
/** The user, with the roles they act with. */
|
|
7
|
+
user: AccessUser;
|
|
8
|
+
/** The tenant the write works in, for a resource with `.tenant()`. */
|
|
9
|
+
tenant?: string | number;
|
|
10
|
+
/** A visitor without a token, through a public form. */
|
|
11
|
+
anonymous?: true;
|
|
12
|
+
};
|
|
13
|
+
/** What `.prepare()` gets besides the values: the write it prepares, its caller and its transaction. */
|
|
14
|
+
export type PrepareContext<R> = {
|
|
15
|
+
/** The write being prepared. */
|
|
16
|
+
operation: 'create' | 'update';
|
|
17
|
+
/** The user who writes, with the roles they act with. */
|
|
18
|
+
user: AccessUser;
|
|
19
|
+
/** The caller's session as the server resolved it. */
|
|
20
|
+
session: PrepareSession;
|
|
21
|
+
/** The tenant the write works in, for a resource with `.tenant()`. */
|
|
22
|
+
tenant?: string | number;
|
|
23
|
+
/** The write's transaction: what the hook reads or writes here commits with the record, or not at all. */
|
|
24
|
+
trx: Kysely<any>;
|
|
25
|
+
/** On update, the record as it is stored now. */
|
|
26
|
+
before?: R;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Fills in or rewrites a create's or update's values before they are written, inside the write's transaction: it gets
|
|
30
|
+
* the values as validated and returns the values to write. It throws a {@link FieldIssue} to refuse the write.
|
|
31
|
+
*/
|
|
32
|
+
export type Preparer<R> = (values: Partial<R>, context: PrepareContext<R>) => Partial<R> | Promise<Partial<R>>;
|
|
33
|
+
/** Thrown by `.prepare()` to refuse a write: it answers `422` with the field named. */
|
|
34
|
+
export declare class FieldIssue extends Error {
|
|
35
|
+
/** The field it is about. */
|
|
36
|
+
readonly field: string;
|
|
37
|
+
constructor(
|
|
38
|
+
/** The field it is about. */
|
|
39
|
+
field: string,
|
|
40
|
+
/** What is wrong, for the user. */
|
|
41
|
+
message: string);
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
package/dist/prepare.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/prepare.ts
|
|
2
|
+
/** Thrown by `.prepare()` to refuse a write: it answers `422` with the field named. */
|
|
3
|
+
var FieldIssue = class extends Error {
|
|
4
|
+
field;
|
|
5
|
+
constructor(field, message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.field = field;
|
|
8
|
+
this.name = "FieldIssue";
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
12
|
+
export { FieldIssue };
|
package/dist/resource.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { ComputedReveal } from "./computed/computed-reveal.js";
|
|
|
7
7
|
import { EventHelpers, EventMatch, EventSpec } from "./events/event-helpers.js";
|
|
8
8
|
import { ResourceState } from "./resource-model.js";
|
|
9
9
|
import { Access, Issue, KeyRefs, RecordOf } from "./resource-types.js";
|
|
10
|
+
import { Preparer } from "./prepare.js";
|
|
10
11
|
import "./index.js";
|
|
11
12
|
import { z } from "zod";
|
|
12
13
|
//#region src/resource.d.ts
|
|
@@ -54,6 +55,8 @@ export declare class ResourceBuilder<F extends FieldsInput = {}, K = never, N ex
|
|
|
54
55
|
readonly accessRules: Access<F>;
|
|
55
56
|
/** @internal */
|
|
56
57
|
readonly computedReveals: Readonly<Record<string, ComputedReveal<RecordOf<F>>>>;
|
|
58
|
+
/** @internal */
|
|
59
|
+
readonly preparers: readonly Preparer<RecordOf<F>>[];
|
|
57
60
|
/** The name, for the compiler only, which typed layouts read to know a resource by its name; never set. */
|
|
58
61
|
readonly $name: N;
|
|
59
62
|
/** The fields, for the compiler only, which {@link InferRecord} reads; never set. */
|
|
@@ -69,7 +72,9 @@ export declare class ResourceBuilder<F extends FieldsInput = {}, K = never, N ex
|
|
|
69
72
|
/** @internal */
|
|
70
73
|
accessRules?: Access<F>,
|
|
71
74
|
/** @internal */
|
|
72
|
-
computedReveals?: Readonly<Record<string, ComputedReveal<RecordOf<F
|
|
75
|
+
computedReveals?: Readonly<Record<string, ComputedReveal<RecordOf<F>>>>,
|
|
76
|
+
/** @internal */
|
|
77
|
+
preparers?: readonly Preparer<RecordOf<F>>[]);
|
|
73
78
|
/** The table, with or without its schema: `'shop.repairs'` or `'repairs'`. Required. */
|
|
74
79
|
table(table: string): ResourceBuilder<F, K, N>;
|
|
75
80
|
/**
|
|
@@ -280,6 +285,20 @@ export declare class ResourceBuilder<F extends FieldsInput = {}, K = never, N ex
|
|
|
280
285
|
* ```
|
|
281
286
|
*/
|
|
282
287
|
validate(validator: Validator<F>): ResourceBuilder<F, K, N>;
|
|
288
|
+
/**
|
|
289
|
+
* Fills in or rewrites the values of every create and update on the server before they are written, inside the
|
|
290
|
+
* write's transaction, after the field checks and `.validate()` rules, which check what it returns again: it returns
|
|
291
|
+
* the values to write, and may set read-only fields, such as a generated key, which no request can. It throws a `FieldIssue` to refuse the write with
|
|
292
|
+
* `422` and the field named. It does database and CPU work only; work that waits on the network goes in a job a write
|
|
293
|
+
* hook enqueues. Each call adds one, run in order. See
|
|
294
|
+
* [Preparing a record](docs/website/src/content/docs/reference/data-config.mdx#preparing-a-record).
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* ```ts
|
|
298
|
+
* .prepare((values, { operation }) => (operation === 'create' ? { ...values, token: crypto.randomUUID() } : values))
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
301
|
+
prepare(preparer: Preparer<RecordOf<F>>): ResourceBuilder<F, K, N>;
|
|
283
302
|
/**
|
|
284
303
|
* Rules per operation (`list`, `read`, `create`, `update`, `delete`). A rule returns `true`, `false`, or a filter that
|
|
285
304
|
* limits the rows. An operation without a rule needs its capability when the app has roles. See
|
package/dist/resource.js
CHANGED
|
@@ -14,12 +14,14 @@ var ResourceBuilder = class ResourceBuilder {
|
|
|
14
14
|
validators;
|
|
15
15
|
accessRules;
|
|
16
16
|
computedReveals;
|
|
17
|
+
preparers;
|
|
17
18
|
/** @internal */
|
|
18
|
-
constructor(state, validators = [], accessRules = {}, computedReveals = {}) {
|
|
19
|
+
constructor(state, validators = [], accessRules = {}, computedReveals = {}, preparers = []) {
|
|
19
20
|
this.state = state;
|
|
20
21
|
this.validators = validators;
|
|
21
22
|
this.accessRules = accessRules;
|
|
22
23
|
this.computedReveals = computedReveals;
|
|
24
|
+
this.preparers = preparers;
|
|
23
25
|
}
|
|
24
26
|
/** The table, with or without its schema: `'shop.repairs'` or `'repairs'`. Required. */
|
|
25
27
|
table(table) {
|
|
@@ -75,7 +77,7 @@ var ResourceBuilder = class ResourceBuilder {
|
|
|
75
77
|
return new ResourceBuilder({
|
|
76
78
|
...this.state,
|
|
77
79
|
primaryKey: refs.map((ref) => ref.name)
|
|
78
|
-
}, this.validators, this.accessRules, this.computedReveals);
|
|
80
|
+
}, this.validators, this.accessRules, this.computedReveals, this.preparers);
|
|
79
81
|
}
|
|
80
82
|
/**
|
|
81
83
|
* A nullable timestamp or date that marks a record deleted: deleting sets it, lists leave such rows out and
|
|
@@ -332,7 +334,23 @@ var ResourceBuilder = class ResourceBuilder {
|
|
|
332
334
|
* ```
|
|
333
335
|
*/
|
|
334
336
|
validate(validator) {
|
|
335
|
-
return new ResourceBuilder(this.state, [...this.validators, validator], this.accessRules, this.computedReveals);
|
|
337
|
+
return new ResourceBuilder(this.state, [...this.validators, validator], this.accessRules, this.computedReveals, this.preparers);
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Fills in or rewrites the values of every create and update on the server before they are written, inside the
|
|
341
|
+
* write's transaction, after the field checks and `.validate()` rules, which check what it returns again: it returns
|
|
342
|
+
* the values to write, and may set read-only fields, such as a generated key, which no request can. It throws a `FieldIssue` to refuse the write with
|
|
343
|
+
* `422` and the field named. It does database and CPU work only; work that waits on the network goes in a job a write
|
|
344
|
+
* hook enqueues. Each call adds one, run in order. See
|
|
345
|
+
* [Preparing a record](docs/website/src/content/docs/reference/data-config.mdx#preparing-a-record).
|
|
346
|
+
*
|
|
347
|
+
* @example
|
|
348
|
+
* ```ts
|
|
349
|
+
* .prepare((values, { operation }) => (operation === 'create' ? { ...values, token: crypto.randomUUID() } : values))
|
|
350
|
+
* ```
|
|
351
|
+
*/
|
|
352
|
+
prepare(preparer) {
|
|
353
|
+
return new ResourceBuilder(this.state, this.validators, this.accessRules, this.computedReveals, [...this.preparers, preparer]);
|
|
336
354
|
}
|
|
337
355
|
/**
|
|
338
356
|
* Rules per operation (`list`, `read`, `create`, `update`, `delete`). A rule returns `true`, `false`, or a filter that
|
|
@@ -352,7 +370,7 @@ var ResourceBuilder = class ResourceBuilder {
|
|
|
352
370
|
return new ResourceBuilder(this.state, this.validators, {
|
|
353
371
|
...this.accessRules,
|
|
354
372
|
...rules
|
|
355
|
-
}, this.computedReveals);
|
|
373
|
+
}, this.computedReveals, this.preparers);
|
|
356
374
|
}
|
|
357
375
|
/**
|
|
358
376
|
* Values revealed like sensitive fields, by name, but made when one is asked for instead of read from a column: a
|
|
@@ -375,7 +393,7 @@ var ResourceBuilder = class ResourceBuilder {
|
|
|
375
393
|
return new ResourceBuilder(this.state, this.validators, this.accessRules, {
|
|
376
394
|
...this.computedReveals,
|
|
377
395
|
...reveals
|
|
378
|
-
});
|
|
396
|
+
}, this.preparers);
|
|
379
397
|
}
|
|
380
398
|
/** Role-based rules per field, for fields that declared `.access(...)`. */
|
|
381
399
|
fieldAccess() {
|
|
@@ -405,7 +423,7 @@ var ResourceBuilder = class ResourceBuilder {
|
|
|
405
423
|
return new ResourceBuilder({
|
|
406
424
|
...this.state,
|
|
407
425
|
...patch
|
|
408
|
-
}, this.validators, this.accessRules, this.computedReveals);
|
|
426
|
+
}, this.validators, this.accessRules, this.computedReveals, this.preparers);
|
|
409
427
|
}
|
|
410
428
|
};
|
|
411
429
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/schema",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.123.0",
|
|
4
4
|
"description": "Resource, field, view and access definitions for Protobase configs.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"aip-parsers": "^1.0.0",
|
|
35
35
|
"eventsource-parser": "^4.1.3",
|
|
36
|
+
"kysely": "^0.29.6",
|
|
36
37
|
"semver": "^7.8.5",
|
|
37
38
|
"zod": "^4.6.5"
|
|
38
39
|
},
|