@diagonal-hq/cli 0.8.1 → 0.9.1
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 +90 -11
- package/dist/cli.mjs +1285 -684
- package/dist/fields.generated.d.ts +7 -1
- package/dist/index.d.ts +85 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -53,11 +53,11 @@ The build reads each file with a parser. It runs only the workflow tests.
|
|
|
53
53
|
- The second argument of `definePage`, and the default export of a layout or component file, must be one function.
|
|
54
54
|
- Each page has a key that is unique in the workspace: a camelCase name of at most 64 letters and digits, such as `members`. The page's id comes from the key. Without a `key` option, the key comes from the file path below `pages/`: `pages/members/[id].tsx` is `membersId`, `pages/404.tsx` is `page404`, and `pages/index.tsx` is `home`. Set `key` when the page must keep its identity and its `Pages.<key>` links after a file move. Two pages with the same key fail at the second file.
|
|
55
55
|
- Pages, layouts, and components link to a page with `Pages.<key>`: `Pages.members.url()`, `Pages.member.url({ id })`, and `Pages.members.navigate()`. `Pages` needs no import, and `import { Pages } from '@diagonal/pages'` also works. The check fails at an unknown key and at a call without the route parameters of the page.
|
|
56
|
-
- Pages, layouts, and components go through the page lint gate: syntax, scope, the component return shape, and a type check against the page Sdk contract and the workspace resources. A page names a resource by slug, for example `Sdk.resource(Resources.Members)`.
|
|
56
|
+
- Pages, layouts, and components go through the page lint gate: syntax, scope, the component return shape, and a type check against the page Sdk contract and the workspace resources. A page names a resource by slug, for example `Sdk.resource(Resources.Members.slug)`.
|
|
57
57
|
- Each `workflows/<name>.ts` is a script workflow. It has one `export const config = { title, trigger, runAs? } satisfies WorkflowConfig` and one `export default defineWorkflow(...)`. The config must be a static literal. The build validates it: the trigger fields, the resources (see [Resource references](#resource-references)), the workflow paths of `workflowEvent` triggers, and the run-as rules. A `recordEvent`, `recordSchedule`, `scheduled`, or `workflowEvent` workflow needs `runAs`.
|
|
58
58
|
- The build removes the config and the `@diagonal-hq/cli` imports from the workflow, with spaces in their place, and compiles the rest with the workflow script compiler against the workspace resources. A compiler error has the line and column of the workflow file.
|
|
59
59
|
- `workflows/<name>.test.ts` tests `workflows/<name>.ts` with `node:test` and `node:assert`. It imports the workflow, `runWorkflow` and `MockTransport` from `@diagonal/workflow/testing`, and `Resources` from `@app/generated/resources`. The build transpiles the test and the workflows and runs them in one Node process. The process can read only its own temporary directory, cannot start processes, and has an empty environment. Each failing test is an error at the test. A test file without a workflow is an error.
|
|
60
|
-
- The build ignores the files at the root other than `diagonal.config.ts` and `assets.ts`, dotfiles, and `node_modules`. It does not build the files in `generated
|
|
60
|
+
- The build ignores the files at the root other than `diagonal.config.ts` and `assets.ts`, dotfiles, and `node_modules`. It does not build the files in `generated/`. It writes `generated/resources.ts` in every build.
|
|
61
61
|
|
|
62
62
|
- The build does not read `notifications/` or `email-layouts/` yet. The editor type-checks the shape of their definitions. See [Notifications](#notifications).
|
|
63
63
|
|
|
@@ -78,19 +78,44 @@ A uid is unique among the definitions of one type. Two definitions of one type w
|
|
|
78
78
|
|
|
79
79
|
The file name is the resource slug: `resources/job-postings.ts` declares the resource `job-postings`. A file name must start with a lowercase letter and contain only a-z, 0-9, `_`, and `-`. A resource has no `id` in source. The platform derives the stored id from the workspace and the slug, so each branch and environment has its own ids.
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
The build writes `generated/resources.ts` from the resource files. `diagonal check` writes it on your machine, so the editor knows the types of `Resources`. The file is a build output: the workspace `.gitignore` lists it, and a deploy writes its own copy.
|
|
82
82
|
|
|
83
83
|
```ts
|
|
84
84
|
// AUTO-GENERATED by `diagonal check`. Do not edit.
|
|
85
85
|
export const Resources = {
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
Members: {
|
|
87
|
+
slug: 'members',
|
|
88
|
+
name: 'Members',
|
|
89
|
+
singularName: 'Member',
|
|
90
|
+
displayField: 'name',
|
|
91
|
+
fields: {
|
|
92
|
+
plan: {
|
|
93
|
+
key: 'plan',
|
|
94
|
+
type: 'select',
|
|
95
|
+
label: 'Plan',
|
|
96
|
+
description: null,
|
|
97
|
+
required: false,
|
|
98
|
+
options: [
|
|
99
|
+
{
|
|
100
|
+
label: 'Monthly',
|
|
101
|
+
value: 'monthly'
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
Roles: {
|
|
108
|
+
slug: 'roles',
|
|
109
|
+
platform: true
|
|
110
|
+
}
|
|
88
111
|
} as const;
|
|
89
112
|
```
|
|
90
113
|
|
|
91
|
-
Each property is the studly case of a slug
|
|
114
|
+
Each property is the studly case of a slug. Its value is the metadata that the resource file declares: the slug, the name, the singular name, the display field, and for each field its key, type, label, description, and required flag, with the options of a select field and the target slug of a relation field. It holds no policies, capabilities, indexes, or field guards and setters. A platform resource file declares only its slug. Two slugs with the same name, such as `job-postings` and `job_postings`, are an error.
|
|
92
115
|
|
|
93
|
-
|
|
116
|
+
Name a resource with `Resources.<Name>.slug` from `@app/generated/resources` everywhere: `field.relation({ resource: Resources.User.slug })`, a workflow `trigger.resource` and `runAs.resource`, a page `authorizer.resource` and `params.<name>.resource`, `Sdk.resource(Resources.User.slug)`, `data.search({ resource: Resources.User.slug })`, and `v.record(Resources.User.slug)`. The build reads `Resources.<Name>.slug` like a literal. An unknown name is an error at its location, and the editor reports it too. A bare `Resources.User` in one of these places is an error.
|
|
117
|
+
|
|
118
|
+
Read the other metadata in pages and workflows, for example `Resources.User.name` or `Resources.User.fields.plan.options`. The build replaces each such reference with its value, so a page carries only the metadata that it reads, and a compiled workflow changes only when a value that it reads changes. A page cannot use `Resources` as a whole, for example in `Object.keys(Resources)`, or re-export it.
|
|
94
119
|
|
|
95
120
|
A slug string, such as `resource: 'user'`, builds with a warning. A resource id, a resource name, a singular name, or a slug without a file is an error.
|
|
96
121
|
|
|
@@ -135,10 +160,10 @@ import { defineNotificationType, field } from '@diagonal-hq/cli';
|
|
|
135
160
|
export default defineNotificationType({
|
|
136
161
|
key: 'member-welcome',
|
|
137
162
|
name: 'Member welcome',
|
|
138
|
-
recipient: { resource: Resources.Members },
|
|
163
|
+
recipient: { resource: Resources.Members.slug },
|
|
139
164
|
variables: {
|
|
140
165
|
joined_on: field.date({ label: 'Joined on', required: true }),
|
|
141
|
-
first_event: { resource: Resources.Events, label: 'First event' }
|
|
166
|
+
first_event: { resource: Resources.Events.slug, label: 'First event' }
|
|
142
167
|
},
|
|
143
168
|
defaultTemplate: 'standard'
|
|
144
169
|
});
|
|
@@ -198,7 +223,7 @@ export default defineNotificationSchedule({
|
|
|
198
223
|
name: 'Week one check-in',
|
|
199
224
|
type: 'member-welcome',
|
|
200
225
|
template: 'standard',
|
|
201
|
-
source: { resource: Resources.Members },
|
|
226
|
+
source: { resource: Resources.Members.slug },
|
|
202
227
|
dateField: 'joined_on',
|
|
203
228
|
offsets: ['P7D'],
|
|
204
229
|
filters: { operator: 'and', conditions: [{ field: 'status', operator: '=', value: 'active' }] },
|
|
@@ -260,7 +285,7 @@ A builder takes these options:
|
|
|
260
285
|
- `getter`: a resolver tree that transforms the stored value when it is read.
|
|
261
286
|
- `setter`: a resolver tree that computes the stored value before each create and update. `setterWhenEmpty: true` runs it only when the value is null or empty.
|
|
262
287
|
|
|
263
|
-
`field.select` takes `options` and `field.relation` takes `resource` (`Resources.<Name
|
|
288
|
+
`field.select` takes `options` and `field.relation` takes `resource` (`Resources.<Name>.slug`) and `type` as options. Other settings go in `settings`. A setting that an option writes, such as `settings.unique`, is refused. A relation can declare `settings.inverse: { key, label?, settings? }`. The build then adds the inverse relation field to the target resource. A target that declares a field with the same key is an error.
|
|
264
289
|
|
|
265
290
|
The platform deploy validates every field again with the rules of the resource builder, so a value that the build cannot check, such as an unknown guard rule, fails the deploy with the file path.
|
|
266
291
|
|
|
@@ -300,6 +325,60 @@ export default defineResource({
|
|
|
300
325
|
- Provisioning can create a field, for example `accounting_entity`. The files cannot declare a field with the same key later. The deploy refuses it until you delete the field in the resource builder or use another key.
|
|
301
326
|
- A capability change in the resource builder is drift. The next deploy reports it and restores the capability from the files.
|
|
302
327
|
|
|
328
|
+
## Policies
|
|
329
|
+
|
|
330
|
+
A policy grants access to the records of its resource. Each policy option maps to one option of a stored policy:
|
|
331
|
+
|
|
332
|
+
```ts
|
|
333
|
+
import { Resources } from '@app/generated/resources';
|
|
334
|
+
|
|
335
|
+
export default defineResource({
|
|
336
|
+
name: 'Posts',
|
|
337
|
+
singularName: 'Post',
|
|
338
|
+
fields: {
|
|
339
|
+
title: field.text({ required: true }),
|
|
340
|
+
body: field.text(),
|
|
341
|
+
author: field.relation({ resource: Resources.Members.slug, type: 'belongs_to' })
|
|
342
|
+
},
|
|
343
|
+
policies: [
|
|
344
|
+
{
|
|
345
|
+
name: 'Visitors read titles',
|
|
346
|
+
scope: ['index', 'show'],
|
|
347
|
+
access: 'public',
|
|
348
|
+
fields: ['id', 'title']
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
name: 'Authors edit their posts',
|
|
352
|
+
scope: ['update', 'destroy'],
|
|
353
|
+
resource: Resources.Members.slug,
|
|
354
|
+
filter: {
|
|
355
|
+
operator: 'and',
|
|
356
|
+
conditions: [
|
|
357
|
+
{
|
|
358
|
+
field: 'author.id',
|
|
359
|
+
operator: '=',
|
|
360
|
+
value: { $type: 'user.current', $value: { key: 'id' } }
|
|
361
|
+
}
|
|
362
|
+
]
|
|
363
|
+
},
|
|
364
|
+
fields: ['title', 'body']
|
|
365
|
+
}
|
|
366
|
+
]
|
|
367
|
+
});
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
- `scope` is `'all'` or a list of operations: `index`, `show`, `count`, `aggregate`, `store`, `storeMany`, `update`, `destroy`, `restore`, and `validate`.
|
|
371
|
+
- `access` is `'public'` or `'protected'`. The default is `'protected'`. A public policy grants access to every visitor, and it cannot set `resource` or `authorizer`.
|
|
372
|
+
- `resource` is the resource that members sign in as, as `Resources.<Name>.slug`. The resource must have the `authenticatable` capability. A protected policy without `resource` grants access only to system administrators.
|
|
373
|
+
- `authorizer` is a filter tree that the member's own record must match, for example `{ operator: 'and', conditions: [{ field: 'permission_slugs', operator: 'in', value: ['moderate'] }] }`.
|
|
374
|
+
- `filter` is a filter tree that limits the records that the policy covers. Compare a relation to the signed-in member with `<relation>.id` and the resolver `{ $type: 'user.current', $value: { key: 'id' } }`.
|
|
375
|
+
- `fields` is an allow-list of field keys. A key can be a relation path, such as `author.name`. An item can also be `{ key, condition }`, where `condition` is a resolver that returns true when the field shows. A field that the list leaves out is hidden from reads and refused on writes. Without `fields`, the policy covers every field.
|
|
376
|
+
- `enabled: false` keeps the policy but grants nothing. The default is `true`.
|
|
377
|
+
|
|
378
|
+
A filter tree has the shape of a record search filter: `{ operator: 'and' | 'or' | 'nor', conditions }`. Each condition is `{ field, operator, value, case_insensitive }` or a nested group. The policy id comes from the resource and the policy name, so each policy of a resource has a different name.
|
|
379
|
+
|
|
380
|
+
The build checks the shape of each option, the filter operators, and the resource that members sign in as. The deploy checks each policy with the rules of the policy API against the deployed schema: the filter paths, the authorizer paths, the field keys, and the field conditions. A policy that fails rejects the deploy, and the previous deployment keeps serving. A field that capability provisioning creates, such as the roles relation of an Authenticatable resource, exists only after the first deploy of that capability.
|
|
381
|
+
|
|
303
382
|
A workflow script reads the capabilities of each resource from `@diagonal/workflow/workspace`. An input that needs a capability, such as the `resource` of `notification.setConsent`, accepts only a resource that declares it.
|
|
304
383
|
|
|
305
384
|
## Application asset references
|