Skip to content

ResourceBuilder

Defined in: packages/schema/src/resource.ts:31

A resource as resource(name) builds it. Every method returns a new builder, so a resource is one chain. See resource().

F extends FieldsInput = { }

K = never

readonly $fields: F

Defined in: packages/schema/src/resource.ts:33

The fields, for the compiler only, which InferRecord reads; never set.


readonly $key: K

Defined in: packages/schema/src/resource.ts:35

The fields of the key, for the compiler only, which RecordKey reads; never set.

access(rules): ResourceBuilder<F, K>

Defined in: packages/schema/src/resource.ts:187

Rules per operation (list, read, create, update, delete). A rule returns true, false, or a filter that limits the rows. An operation without a rule needs its capability when the app has roles. See Access.

Access<F>

ResourceBuilder<F, K>

.access({
read: roles.can('work_orders.read'),
update: roles.can('work_orders.update').and((_ctx, record) => (record as { status: string }).status !== 'done'),
delete: roles.is('admin'),
})

comments(): ResourceBuilder<F, K>

Defined in: packages/schema/src/resource.ts:151

Lets the people who may read a record comment on it, and mention others who may read it too, who get an email. The record page shows the thread with l.comments(), and admins list every comment as Comments. See Record comments.

ResourceBuilder<F, K>


eventMatches(): Record<string, EventMatch<any>>

Defined in: packages/schema/src/resource.ts:156

The code predicates (match) of its events, by event name.

Record<string, EventMatch<any>>


events(define): ResourceBuilder<F, K>

Defined in: packages/schema/src/resource.ts:133

The events changes to its records raise, captured in the database for every write, from the API or elsewhere. See Declaring events.

(e, r) => EventSpec[]

ResourceBuilder<F, K>

.events((e, r) => [
...e.crud(),
e.becomes('large', 'total > 1000'),
e.updated('statusChanged', { changed: [r.status] }),
e.updated('reopened', { from: 'status = "cancelled"', when: 'status != "cancelled"' }),
])

fieldAccess(): Record<string, FieldAccess>

Defined in: packages/schema/src/resource.ts:192

Role-based rules per field, for fields that declared .access(...).

Record<string, FieldAccess>


fields<N>(fields): ResourceBuilder<N, never>

Defined in: packages/schema/src/resource.ts:56

The columns, by field name, each built with f. name: null ignores a column on purpose. The column is the snake_case of the name unless .column() says otherwise. Required, and called before the methods that pick fields.

N extends FieldsInput

N

ResourceBuilder<N, never>


history(): ResourceBuilder<F, K>

Defined in: packages/schema/src/resource.ts:142

Keeps every change of its records, from the API or elsewhere: the fields before and after, and who made it. The record page shows it as History, and admins list every change as Activity. See Record history.

ResourceBuilder<F, K>


ignoredColumns(): string[]

Defined in: packages/schema/src/resource.ts:214

The columns of the fields given as null, which the resource ignores on purpose.

string[]


owner(pick): ResourceBuilder<F, K>

Defined in: packages/schema/src/resource.ts:99

The field holding the owning user’s id, which .own capability scopes filter on.

(r) => Ref

ResourceBuilder<F, K>


position(pick, o?): ResourceBuilder<F, K>

Defined in: packages/schema/src/resource.ts:114

The integer field holding the records’ order, 1 the first, which a list’s Sort button changes by dragging the records; a move renumbers them 1, 2, 3, … in one batch reorder. With within, a relation, each parent’s records have an order of their own, as an invoice’s items do, sorted in the parent’s related section. The field must be sortable and within filterable. See Sorting by hand.

(r) => Ref

(r) => Ref

ResourceBuilder<F, K>

.position((r) => r.position, { within: (r) => r.invoice_id })

primaryKey<P>(pick): ResourceBuilder<F, P>

Defined in: packages/schema/src/resource.ts:61

The key: one field, or a list for a key of several columns ((r) => [r.order_id, r.line_no]). Required.

P extends KeyRefs

(r) => P

ResourceBuilder<F, P>


recordSchema(): ZodType<{ [K in string | number | symbol]: { [K in string | number | symbol]: ActiveFields<F>[K] extends Field<V> ? V : never }[K] }, unknown, $ZodTypeInternals<{ [K in string | number | symbol]: { [K in string | number | symbol]: ActiveFields<F>[K] extends Field<V> ? V : never }[K] }, unknown>>

Defined in: packages/schema/src/resource.ts:204

The Zod schema of a whole record, from its fields’ schemas.

ZodType<{ [K in string | number | symbol]: { [K in string | number | symbol]: ActiveFields<F>[K] extends Field<V> ? V : never }[K] }, unknown, $ZodTypeInternals<{ [K in string | number | symbol]: { [K in string | number | symbol]: ActiveFields<F>[K] extends Field<V> ? V : never }[K] }, unknown>>


search(pick): ResourceBuilder<F, K>

Defined in: packages/schema/src/resource.ts:91

Fields matched by search("...") and bare words in filters. Authoritative over the UI’s list search defaults. r.phone.digitsEnd() matches only the end of the field’s digits, for phone numbers.

(r) => SearchRef[]

ResourceBuilder<F, K>


softDelete(pick): ResourceBuilder<F, K>

Defined in: packages/schema/src/resource.ts:75

A nullable timestamp or date that marks a record deleted: deleting sets it, lists leave such rows out and :undelete clears it.

(r) => Ref

ResourceBuilder<F, K>


table(table): ResourceBuilder<F, K>

Defined in: packages/schema/src/resource.ts:48

The table, with or without its schema: 'shop.repairs' or 'repairs'. Required.

string

ResourceBuilder<F, K>


tenant(pick): ResourceBuilder<F, K>

Defined in: packages/schema/src/resource.ts:83

The field that separates tenants: every query is limited to the caller’s tenant, creates set it, and a request body may not.

(r) => Ref

ResourceBuilder<F, K>


toModel(): ResourceModel

Defined in: packages/schema/src/resource.ts:199

The resource as the server and the app read it. Throws when the table, fields or key are missing, or a field option conflicts with another.

ResourceModel


validate(validator): ResourceBuilder<F, K>

Defined in: packages/schema/src/resource.ts:169

A rule over the whole record, run after the field checks on every create and update. Its issues answer 400 with the field named. Each call adds a rule.

Validator<F>

ResourceBuilder<F, K>

.validate((repair) => (repair.status === 'collected' && !repair.paid ? [{ field: 'paid', message: 'A bike leaves the shop paid' }] : undefined))