@skaile/forge-layer-data-browser 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +90 -2
- package/app/app.config.ts +5 -0
- package/app/components/DataFilterBuilder.vue +198 -0
- package/app/components/DataRelationSelect.vue +94 -0
- package/app/components/DataRowSlideover.vue +185 -0
- package/app/components/DataValue.vue +49 -0
- package/app/composables/useDataBrowser.ts +8 -0
- package/app/middleware/owner-only.ts +9 -0
- package/app/pages/data/[entity].vue +201 -0
- package/app/pages/data/index.vue +68 -0
- package/app/utils/dataBrowser.ts +233 -0
- package/nuxt.config.ts +8 -0
- package/package.json +39 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matthias Ortegro
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,91 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @skaile/forge-layer-data-browser
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A Nuxt layer for apps built on [forge-template-app](https://github.com/skaile-ai/forge/tree/main/templates/app): the
|
|
4
|
+
owner's view of every entity the app exposes through the generic `data.*` actions (`server/db/model.ts`). It adds no
|
|
5
|
+
actions and no tables: it reads `data.describe` and calls `data.list` / `get` / `aggregate` / `create` / `update` /
|
|
6
|
+
`delete` like any other client, so the server's checks apply unchanged. It ships as source; the app `extends` it.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
bun add @skaile/forge-layer-data-browser
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
// nuxt.config.ts
|
|
16
|
+
export default defineNuxtConfig({
|
|
17
|
+
extends: ['@skaile/forge-layer-data-browser'],
|
|
18
|
+
})
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```css
|
|
22
|
+
/* app/assets/css/main.css: Tailwind skips node_modules, so name the layer's sources */
|
|
23
|
+
@source "../../../node_modules/@skaile/forge-layer-data-browser/app";
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Remove the `extends` entry (and the `@source` line) to drop it; the app does not import from the layer.
|
|
27
|
+
|
|
28
|
+
## Host contract
|
|
29
|
+
|
|
30
|
+
The layer runs on the core of forge-template-app and uses these parts of it by name. An app copied from the template
|
|
31
|
+
has all of them; an app that renamed or removed one overrides the layer file that uses it.
|
|
32
|
+
|
|
33
|
+
| Kind | Used | Where in the template |
|
|
34
|
+
|---|---|---|
|
|
35
|
+
| Explicit imports | `~/utils/format` (`fromDateTimeInput`, `toDateTimeInput`), `~/utils/jsonSchema` (`labelOfKey`, `JsonSchema`) | `app/utils/` |
|
|
36
|
+
| Composables | `useDataApi`, `useMe`, `useDates`, `useConfirm`, `useDiscardGuard`, `useFormDirty` | `app/composables/` |
|
|
37
|
+
| Utils | `applyServerError`, `actionErrorMessage` | `app/utils/formErrors.ts`, `app/composables/useActions.ts` |
|
|
38
|
+
| Components | `AppHeader`, `SchemaForm`, `FormDiscardPrompt`, `FormSaveHint` | `app/components/` |
|
|
39
|
+
| App config | `appShell.nav` entries with `roles` (read by `useAppShell`) | `app/utils/appShell.ts` |
|
|
40
|
+
| Actions | `data.describe`, `data.list`, `data.get`, `data.aggregate`, `data.create`, `data.update`, `data.delete`, `me.get` | `server/utils/actions/` |
|
|
41
|
+
|
|
42
|
+
Peers: `nuxt ^4`, `@nuxt/ui ^4.11`, `vue ^3.5`, `@skaile/forge-data ^0.2.1` (types).
|
|
43
|
+
|
|
44
|
+
## Overrides
|
|
45
|
+
|
|
46
|
+
A Nuxt layer is overridden file by file: a file at the same path in the app wins. Copy the one file you need to change
|
|
47
|
+
(e.g. `app/components/DataValue.vue` to render a field type differently, or `app/app.config.ts` to move the nav entry)
|
|
48
|
+
into the app and keep the rest from the package, so fixes in the other files still arrive with updates.
|
|
49
|
+
|
|
50
|
+
## What you get
|
|
51
|
+
|
|
52
|
+
- `/data` (nav order 80, owners only): the entities with their description, row count (`data.aggregate`) and whether
|
|
53
|
+
they are read-only or owner-only.
|
|
54
|
+
- `/data/<entity>`: a table with one column per described field (computed ones included, hidden ones never described;
|
|
55
|
+
foreign keys show the related row's label via `expand`), search over the entity's search fields, sort by clicking a
|
|
56
|
+
sortable column (asc, desc, default), pages of 25 with the total, and a filter builder: rows of field (own fields and
|
|
57
|
+
one-level relation paths such as `project.name`), operator (by field type; datetimes get ranges, not equality) and
|
|
58
|
+
value, ANDed into `where`. "Is any of" on a foreign key picks rows of the target; on numbers it keeps only the tags
|
|
59
|
+
that are numbers.
|
|
60
|
+
- A slideover per row and "New <entity>": the core's `SchemaForm` with a schema built from the field descriptors
|
|
61
|
+
(`rowSchemaOf`). Foreign keys pick a row of the target by its `labelField` (searchable once there are more than 10),
|
|
62
|
+
datetimes are entered and shown on the app's clock (`app.timezone`, `useDates().timeZone`) and sent as ISO, nullable
|
|
63
|
+
fields can be cleared to `null`, enums are selects, long text a textarea, read-only and computed fields are shown
|
|
64
|
+
disabled. Create, save and delete appear only where the entity's `ops` allow them; delete asks first. A 400 that names
|
|
65
|
+
a field (`details.field` of the error, else `'title' must not be empty`) shows under that field, other errors as a
|
|
66
|
+
toast.
|
|
67
|
+
|
|
68
|
+
## Owners only
|
|
69
|
+
|
|
70
|
+
- UI: the nav entry carries `roles: ['owner']` (`useAppShell` hides it from everyone else) and both pages use the
|
|
71
|
+
`owner-only` route middleware, which sends non-owners to the dashboard (`me.get` › `principal.roles`).
|
|
72
|
+
- Server: the browser is a UI convenience, a client like any other. Members reach the same rows of every entity the app
|
|
73
|
+
exposes through `data.*` (HTTP, CLI, MCP, the curated pages); hiding `/data` from them restricts nothing. An entity
|
|
74
|
+
meant for owners only says so in its `defineEntity`: `ops: { ownerOnly: true }`; forge-data's service then answers 403
|
|
75
|
+
to every non-owner on every `data.*` action and leaves it out of their `data.describe`.
|
|
76
|
+
|
|
77
|
+
## Files
|
|
78
|
+
|
|
79
|
+
| What | Where |
|
|
80
|
+
|---|---|
|
|
81
|
+
| Pages | `app/pages/data/index.vue`, `app/pages/data/[entity].vue` |
|
|
82
|
+
| Filter builder, relation picker, row slideover, cell | `app/components/Data*.vue` |
|
|
83
|
+
| The shared model (`data.describe`); rows via the core's `useDataApi()` | `app/composables/useDataBrowser.ts` |
|
|
84
|
+
| Operators per type, filter rows → `where`, form schema, changed values | `app/utils/dataBrowser.ts` |
|
|
85
|
+
| Route guard | `app/middleware/owner-only.ts` |
|
|
86
|
+
|
|
87
|
+
## Tests
|
|
88
|
+
|
|
89
|
+
The logic needs the host's model, so its tests live in the template (`templates/app/test/app/data-browser.test.ts`) and run
|
|
90
|
+
against the installed package. `bun scripts/check-templates.ts --local app` checks an unpublished change of this
|
|
91
|
+
package against the template.
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { Cond, DataModel, EntityDescriptor, FilterOp } from '@skaile/forge-data'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Condition rows (field, operator, value), ANDed: the `where` of `data.list`.
|
|
6
|
+
* Fields are the entity's filterable paths, operators those of the field's
|
|
7
|
+
* type; the value control follows the field (enum select, toggle, date-time
|
|
8
|
+
* on the app's clock sent as ISO, number, row picker for a foreign key, a
|
|
9
|
+
* multi-select for in/nin on enums and foreign keys, tags otherwise).
|
|
10
|
+
* Emits the complete conditions, debounced.
|
|
11
|
+
*/
|
|
12
|
+
const props = defineProps<{ model: DataModel; entity: EntityDescriptor }>()
|
|
13
|
+
const conditions = defineModel<Cond[]>({ required: true })
|
|
14
|
+
const { timeZone } = useDates()
|
|
15
|
+
|
|
16
|
+
interface ConditionRow extends FilterRow {
|
|
17
|
+
key: number
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const paths = computed(() => filterPathsOf(props.model, props.entity))
|
|
21
|
+
const pathItems = computed(() => paths.value.map(path => ({ value: path.path, label: path.label })))
|
|
22
|
+
const pathOf = (path: string) => paths.value.find(p => p.path === path)
|
|
23
|
+
|
|
24
|
+
let nextKey = 0
|
|
25
|
+
const rows = ref<ConditionRow[]>(
|
|
26
|
+
conditions.value.map(cond => ({ key: nextKey++, ...filterRowOf(cond, pathOf(cond.field), timeZone.value) })),
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
/** Each row with its path descriptor and the choices that follow from it, resolved once per change. */
|
|
30
|
+
const views = computed(() =>
|
|
31
|
+
rows.value.map(row => {
|
|
32
|
+
const path = pathOf(row.path)
|
|
33
|
+
return {
|
|
34
|
+
row,
|
|
35
|
+
path,
|
|
36
|
+
ops: path ? filterOpsOf(path.field).map(op => ({ value: op, label: FILTER_OP_LABELS[op] })) : [],
|
|
37
|
+
enumItems: (path?.field.enum ?? []).map(value => ({ value, label: value })),
|
|
38
|
+
}
|
|
39
|
+
}),
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
/** The value a fresh row starts with for this field and operator. */
|
|
43
|
+
function initialValue(path: string, op: FilterOp): unknown {
|
|
44
|
+
if (isNullOp(op)) return undefined
|
|
45
|
+
if (isListOp(op)) return []
|
|
46
|
+
return pathOf(path)?.field.type === 'boolean' ? true : undefined
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A new row starts on the label field (`title`, `name`) when it is filterable. */
|
|
50
|
+
function add() {
|
|
51
|
+
const first = pathOf(props.entity.labelField) ?? paths.value[0]
|
|
52
|
+
if (!first) return
|
|
53
|
+
const op = filterOpsOf(first.field)[0]!
|
|
54
|
+
rows.value.push({ key: nextKey++, path: first.path, op, value: initialValue(first.path, op) })
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function setPath(row: ConditionRow, path: string) {
|
|
58
|
+
const field = pathOf(path)?.field
|
|
59
|
+
if (!field) return
|
|
60
|
+
row.path = path
|
|
61
|
+
row.op = filterOpsOf(field)[0]!
|
|
62
|
+
row.value = initialValue(path, row.op)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function setOp(row: ConditionRow, op: FilterOp) {
|
|
66
|
+
const keepsValue = isListOp(op) === isListOp(row.op) && !isNullOp(op) && !isNullOp(row.op)
|
|
67
|
+
row.op = op
|
|
68
|
+
if (!keepsValue) row.value = initialValue(row.path, op)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function remove(row: ConditionRow) {
|
|
72
|
+
rows.value = rows.value.filter(r => r.key !== row.key)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
watchDebounced(
|
|
76
|
+
[rows, timeZone],
|
|
77
|
+
([current, zone]) => {
|
|
78
|
+
conditions.value = conditionsOf(current, paths.value, zone)
|
|
79
|
+
},
|
|
80
|
+
{ deep: true, debounce: 300 },
|
|
81
|
+
)
|
|
82
|
+
</script>
|
|
83
|
+
|
|
84
|
+
<template>
|
|
85
|
+
<div class="space-y-2">
|
|
86
|
+
<div
|
|
87
|
+
v-for="{ row, path, ops, enumItems } in views"
|
|
88
|
+
:key="row.key"
|
|
89
|
+
class="flex flex-wrap items-center gap-2"
|
|
90
|
+
data-testid="data-filter-row"
|
|
91
|
+
>
|
|
92
|
+
<USelectMenu
|
|
93
|
+
:model-value="row.path"
|
|
94
|
+
:items="pathItems"
|
|
95
|
+
value-key="value"
|
|
96
|
+
class="w-52"
|
|
97
|
+
aria-label="Field"
|
|
98
|
+
@update:model-value="value => setPath(row, value)"
|
|
99
|
+
/>
|
|
100
|
+
<USelect
|
|
101
|
+
:model-value="row.op"
|
|
102
|
+
:items="ops"
|
|
103
|
+
class="w-36"
|
|
104
|
+
aria-label="Operator"
|
|
105
|
+
@update:model-value="value => setOp(row, value as FilterOp)"
|
|
106
|
+
/>
|
|
107
|
+
<template v-if="!isNullOp(row.op)">
|
|
108
|
+
<template v-if="isListOp(row.op)">
|
|
109
|
+
<USelectMenu
|
|
110
|
+
v-if="path?.field.enum"
|
|
111
|
+
:model-value="row.value as string[]"
|
|
112
|
+
:items="enumItems"
|
|
113
|
+
value-key="value"
|
|
114
|
+
multiple
|
|
115
|
+
class="w-64"
|
|
116
|
+
aria-label="Values"
|
|
117
|
+
@update:model-value="value => row.value = value"
|
|
118
|
+
/>
|
|
119
|
+
<DataRelationSelect
|
|
120
|
+
v-else-if="path?.target"
|
|
121
|
+
:model-value="row.value as string[]"
|
|
122
|
+
:target="path.target"
|
|
123
|
+
multiple
|
|
124
|
+
class="w-64"
|
|
125
|
+
@update:model-value="value => row.value = value"
|
|
126
|
+
/>
|
|
127
|
+
<UInputTags
|
|
128
|
+
v-else
|
|
129
|
+
:model-value="row.value as string[]"
|
|
130
|
+
placeholder="Value, Enter"
|
|
131
|
+
class="w-64"
|
|
132
|
+
aria-label="Values"
|
|
133
|
+
@update:model-value="value => row.value = value"
|
|
134
|
+
/>
|
|
135
|
+
</template>
|
|
136
|
+
<USwitch
|
|
137
|
+
v-else-if="path?.field.type === 'boolean'"
|
|
138
|
+
:model-value="row.value as boolean"
|
|
139
|
+
:label="row.value ? 'true' : 'false'"
|
|
140
|
+
@update:model-value="value => row.value = value"
|
|
141
|
+
/>
|
|
142
|
+
<USelect
|
|
143
|
+
v-else-if="path?.field.enum"
|
|
144
|
+
:model-value="row.value as string"
|
|
145
|
+
:items="enumItems"
|
|
146
|
+
class="w-48"
|
|
147
|
+
aria-label="Value"
|
|
148
|
+
@update:model-value="value => row.value = value"
|
|
149
|
+
/>
|
|
150
|
+
<UInput
|
|
151
|
+
v-else-if="path?.field.type === 'datetime'"
|
|
152
|
+
type="datetime-local"
|
|
153
|
+
:model-value="typeof row.value === 'string' ? row.value : undefined"
|
|
154
|
+
class="w-56"
|
|
155
|
+
aria-label="Value"
|
|
156
|
+
@update:model-value="value => row.value = value"
|
|
157
|
+
/>
|
|
158
|
+
<UInputNumber
|
|
159
|
+
v-else-if="path?.field.type === 'integer' || path?.field.type === 'number'"
|
|
160
|
+
:model-value="row.value as number"
|
|
161
|
+
class="w-40"
|
|
162
|
+
aria-label="Value"
|
|
163
|
+
@update:model-value="value => row.value = value"
|
|
164
|
+
/>
|
|
165
|
+
<DataRelationSelect
|
|
166
|
+
v-else-if="path?.target"
|
|
167
|
+
:model-value="typeof row.value === 'string' ? row.value : undefined"
|
|
168
|
+
:target="path.target"
|
|
169
|
+
class="w-56"
|
|
170
|
+
@update:model-value="value => row.value = value"
|
|
171
|
+
/>
|
|
172
|
+
<UInput
|
|
173
|
+
v-else
|
|
174
|
+
:model-value="row.value as string"
|
|
175
|
+
class="w-56"
|
|
176
|
+
aria-label="Value"
|
|
177
|
+
@update:model-value="value => row.value = value"
|
|
178
|
+
/>
|
|
179
|
+
</template>
|
|
180
|
+
<UButton
|
|
181
|
+
icon="i-lucide-x"
|
|
182
|
+
color="neutral"
|
|
183
|
+
variant="ghost"
|
|
184
|
+
aria-label="Remove condition"
|
|
185
|
+
@click="remove(row)"
|
|
186
|
+
/>
|
|
187
|
+
</div>
|
|
188
|
+
<UButton
|
|
189
|
+
icon="i-lucide-plus"
|
|
190
|
+
label="Add condition"
|
|
191
|
+
color="neutral"
|
|
192
|
+
variant="soft"
|
|
193
|
+
size="sm"
|
|
194
|
+
data-testid="data-filter-add"
|
|
195
|
+
@click="add"
|
|
196
|
+
/>
|
|
197
|
+
</div>
|
|
198
|
+
</template>
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { EntityDescriptor, Row } from '@skaile/forge-data'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Picks a row of `target` by its label field (a foreign key's value), or with
|
|
6
|
+
* `multiple` several (their ids, for `in`/`nin` filters): lists the first rows
|
|
7
|
+
* through `data.list`, and once there are more than fit the list searches on
|
|
8
|
+
* the server (the entity's search fields, else the label).
|
|
9
|
+
*/
|
|
10
|
+
const props = defineProps<{
|
|
11
|
+
target: EntityDescriptor
|
|
12
|
+
multiple?: boolean
|
|
13
|
+
disabled?: boolean
|
|
14
|
+
nullable?: boolean
|
|
15
|
+
placeholder?: string
|
|
16
|
+
}>()
|
|
17
|
+
const model = defineModel<string | string[] | null | undefined>()
|
|
18
|
+
|
|
19
|
+
const LIMIT = 50
|
|
20
|
+
const api = useDataApi()
|
|
21
|
+
const term = ref('')
|
|
22
|
+
const search = refDebounced(term, 250)
|
|
23
|
+
|
|
24
|
+
const labelField = computed(() => props.target.fields.find(field => field.key === props.target.labelField))
|
|
25
|
+
const { data, status } = useAsyncData(
|
|
26
|
+
() => `data-browser:relation:${props.target.name}:${search.value}`,
|
|
27
|
+
() => {
|
|
28
|
+
const value = search.value.trim()
|
|
29
|
+
const byLabel =
|
|
30
|
+
value &&
|
|
31
|
+
props.target.search.length === 0 &&
|
|
32
|
+
(labelField.value?.type === 'string' || labelField.value?.type === 'text')
|
|
33
|
+
return api.list(props.target.name, {
|
|
34
|
+
limit: LIMIT,
|
|
35
|
+
sort: labelField.value?.sortable ? [{ field: props.target.labelField, order: 'asc' }] : undefined,
|
|
36
|
+
search: value && props.target.search.length > 0 ? value : undefined,
|
|
37
|
+
where: byLabel ? [{ field: props.target.labelField, op: 'contains', value }] : undefined,
|
|
38
|
+
})
|
|
39
|
+
},
|
|
40
|
+
{ server: false, lazy: true },
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
// More rows than one page, or already searching: offer the search input.
|
|
44
|
+
const searchable = ref(false)
|
|
45
|
+
watch(data, page => {
|
|
46
|
+
if (page && page.total > 10) searchable.value = true
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
const idOf = (row: Row) => String(row[props.target.primaryKey])
|
|
50
|
+
const ids = computed(() => (Array.isArray(model.value) ? model.value : model.value ? [model.value] : []))
|
|
51
|
+
|
|
52
|
+
// The rows of selected ids the listed page does not contain, loaded once each.
|
|
53
|
+
const selected = ref<Row[]>([])
|
|
54
|
+
watch(
|
|
55
|
+
ids,
|
|
56
|
+
async current => {
|
|
57
|
+
const known = new Set([...(data.value?.items ?? []), ...selected.value].map(idOf))
|
|
58
|
+
const missing = current.filter(id => !known.has(id))
|
|
59
|
+
if (missing.length === 0) return
|
|
60
|
+
const rows = await Promise.all(missing.map(id => api.get(props.target.name, id).catch(() => null)))
|
|
61
|
+
selected.value = [...selected.value, ...rows.filter((row): row is Row => row !== null)]
|
|
62
|
+
},
|
|
63
|
+
{ immediate: true },
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
const items = computed(() => {
|
|
67
|
+
const rows = data.value?.items ?? []
|
|
68
|
+
const listed = new Set(rows.map(idOf))
|
|
69
|
+
const all = [...selected.value.filter(row => ids.value.includes(idOf(row)) && !listed.has(idOf(row))), ...rows]
|
|
70
|
+
return all.map(row => ({ value: idOf(row), label: String(row[props.target.labelField] ?? idOf(row)) }))
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
function update(value: string | string[] | null | undefined) {
|
|
74
|
+
model.value = props.multiple ? (Array.isArray(value) ? value : []) : ((value as string | null | undefined) ?? null)
|
|
75
|
+
}
|
|
76
|
+
</script>
|
|
77
|
+
|
|
78
|
+
<template>
|
|
79
|
+
<USelectMenu
|
|
80
|
+
v-model:search-term="term"
|
|
81
|
+
:model-value="multiple ? ids : (model as string | null | undefined) ?? undefined"
|
|
82
|
+
:items="items"
|
|
83
|
+
value-key="value"
|
|
84
|
+
:multiple="multiple"
|
|
85
|
+
:search-input="searchable"
|
|
86
|
+
ignore-filter
|
|
87
|
+
:clear="nullable"
|
|
88
|
+
:loading="status === 'pending'"
|
|
89
|
+
:disabled="disabled"
|
|
90
|
+
:placeholder="placeholder ?? `Choose ${target.label.toLowerCase()}`"
|
|
91
|
+
class="w-full max-w-sm"
|
|
92
|
+
@update:model-value="update"
|
|
93
|
+
/>
|
|
94
|
+
</template>
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { DataModel, EntityDescriptor, Row } from '@skaile/forge-data'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Edits one row (`row`) or creates one (`row` null) of any entity through
|
|
6
|
+
* `SchemaForm`, built from the field descriptors. Foreign keys pick a row of
|
|
7
|
+
* their target. Only what the entity's `ops` allow is offered; a 400 naming a
|
|
8
|
+
* field shows under that field, anything else as a toast. The form rules of
|
|
9
|
+
* `FormModal` apply (CLINICO MOD-1/2/3, EDIT-5/7): a mask click never closes
|
|
10
|
+
* it, footer `[Delete] ··· [Cancel] [Save]`,
|
|
11
|
+
* closing with unsaved input asks in place, Save waits for the missing
|
|
12
|
+
* fields, Delete asks through the shell's confirm.
|
|
13
|
+
*/
|
|
14
|
+
const props = defineProps<{ model: DataModel; entity: EntityDescriptor; row: Row | null }>()
|
|
15
|
+
const open = defineModel<boolean>('open', { default: false })
|
|
16
|
+
const emit = defineEmits<{ saved: [row: Row]; deleted: [] }>()
|
|
17
|
+
|
|
18
|
+
const api = useDataApi()
|
|
19
|
+
const toast = useToast()
|
|
20
|
+
const { confirm } = useConfirm()
|
|
21
|
+
const form = useTemplateRef('form')
|
|
22
|
+
|
|
23
|
+
const mode = computed(() => {
|
|
24
|
+
if (!props.row) return props.entity.ops.create ? 'create' : 'view'
|
|
25
|
+
return props.entity.ops.update ? 'edit' : 'view'
|
|
26
|
+
})
|
|
27
|
+
const schema = computed(() => rowSchemaOf(props.entity, mode.value))
|
|
28
|
+
const state = ref<Record<string, unknown>>({})
|
|
29
|
+
const { dirty, markClean } = useFormDirty(state)
|
|
30
|
+
watch(
|
|
31
|
+
[open, () => props.row],
|
|
32
|
+
([isOpen]) => {
|
|
33
|
+
if (!isOpen) return
|
|
34
|
+
state.value = formStateOf(props.entity, props.row)
|
|
35
|
+
markClean()
|
|
36
|
+
},
|
|
37
|
+
{ immediate: true },
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
const relations = computed(() =>
|
|
41
|
+
props.entity.relations.flatMap(relation => {
|
|
42
|
+
const target = props.model.entities.find(entity => entity.name === relation.target)
|
|
43
|
+
return target ? [{ field: relation.field, target }] : []
|
|
44
|
+
}),
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
const rowId = computed(() => (props.row ? String(props.row[props.entity.primaryKey]) : ''))
|
|
48
|
+
const title = computed(() => {
|
|
49
|
+
if (!props.row) return `New ${props.entity.label.toLowerCase()}`
|
|
50
|
+
return String(props.row[props.entity.labelField] ?? rowId.value)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
const saving = ref(false)
|
|
54
|
+
const deleting = ref(false)
|
|
55
|
+
const { discarding, requestClose, onOpenUpdate } = useDiscardGuard(open, {
|
|
56
|
+
dirty,
|
|
57
|
+
busy: () => saving.value || deleting.value,
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
async function save(values: Record<string, unknown>) {
|
|
61
|
+
const payload = writableValuesOf(props.entity, values, props.row)
|
|
62
|
+
if (props.row && Object.keys(payload).length === 0) {
|
|
63
|
+
toast.add({ title: 'Nothing changed', color: 'neutral' })
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
saving.value = true
|
|
67
|
+
try {
|
|
68
|
+
const saved = props.row
|
|
69
|
+
? await api.update(props.entity.name, rowId.value, payload)
|
|
70
|
+
: await api.create(props.entity.name, payload)
|
|
71
|
+
toast.add({ title: props.row ? `${props.entity.label} saved` : `${props.entity.label} created`, color: 'success' })
|
|
72
|
+
emit('saved', saved)
|
|
73
|
+
open.value = false
|
|
74
|
+
} catch (error) {
|
|
75
|
+
applyServerError(error, {
|
|
76
|
+
form: form.value,
|
|
77
|
+
fields: Object.keys(schema.value.properties ?? {}),
|
|
78
|
+
toast,
|
|
79
|
+
title: `Could not save the ${props.entity.label.toLowerCase()}`,
|
|
80
|
+
})
|
|
81
|
+
} finally {
|
|
82
|
+
saving.value = false
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function remove() {
|
|
87
|
+
const noun = props.entity.label.toLowerCase()
|
|
88
|
+
const accepted = await confirm({
|
|
89
|
+
title: `Delete this ${noun}?`,
|
|
90
|
+
description: `“${title.value}” is removed for good, with whatever the database deletes along with it.`,
|
|
91
|
+
confirmLabel: `Delete ${noun}`,
|
|
92
|
+
})
|
|
93
|
+
if (!accepted) return
|
|
94
|
+
deleting.value = true
|
|
95
|
+
try {
|
|
96
|
+
await api.delete(props.entity.name, rowId.value)
|
|
97
|
+
toast.add({ title: `${props.entity.label} deleted`, color: 'success' })
|
|
98
|
+
open.value = false
|
|
99
|
+
emit('deleted')
|
|
100
|
+
} catch (error) {
|
|
101
|
+
toast.add({ title: `Could not delete the ${noun}`, description: actionErrorMessage(error), color: 'error' })
|
|
102
|
+
} finally {
|
|
103
|
+
deleting.value = false
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
</script>
|
|
107
|
+
|
|
108
|
+
<template>
|
|
109
|
+
<USlideover
|
|
110
|
+
:open="open"
|
|
111
|
+
:title="title"
|
|
112
|
+
:description="row ? `${entity.label} ${rowId}` : entity.description"
|
|
113
|
+
:ui="{ content: 'max-w-xl' }"
|
|
114
|
+
:content="{ onInteractOutside: (event: Event) => event.preventDefault() }"
|
|
115
|
+
@update:open="onOpenUpdate"
|
|
116
|
+
>
|
|
117
|
+
<template #body>
|
|
118
|
+
<SchemaForm
|
|
119
|
+
ref="form"
|
|
120
|
+
v-model:model="state"
|
|
121
|
+
:schema="schema"
|
|
122
|
+
data-testid="data-row-form"
|
|
123
|
+
@submit="save"
|
|
124
|
+
>
|
|
125
|
+
<template
|
|
126
|
+
v-for="relation in relations"
|
|
127
|
+
:key="relation.field"
|
|
128
|
+
#[`field-${relation.field}`]="slot"
|
|
129
|
+
>
|
|
130
|
+
<DataRelationSelect
|
|
131
|
+
:model-value="typeof slot.value === 'string' || slot.value === null ? slot.value : undefined"
|
|
132
|
+
:target="relation.target"
|
|
133
|
+
:disabled="slot.disabled"
|
|
134
|
+
:nullable="slot.nullable"
|
|
135
|
+
@update:model-value="slot.update"
|
|
136
|
+
/>
|
|
137
|
+
</template>
|
|
138
|
+
<template #actions="{ missing }">
|
|
139
|
+
<FormDiscardPrompt
|
|
140
|
+
v-if="discarding"
|
|
141
|
+
@keep="discarding = false"
|
|
142
|
+
@discard="open = false"
|
|
143
|
+
/>
|
|
144
|
+
<div
|
|
145
|
+
v-else
|
|
146
|
+
class="flex flex-wrap items-center gap-2"
|
|
147
|
+
>
|
|
148
|
+
<UButton
|
|
149
|
+
v-if="row && entity.ops.delete"
|
|
150
|
+
label="Delete"
|
|
151
|
+
icon="i-lucide-trash-2"
|
|
152
|
+
color="error"
|
|
153
|
+
variant="ghost"
|
|
154
|
+
:loading="deleting"
|
|
155
|
+
:disabled="saving"
|
|
156
|
+
data-testid="data-row-delete"
|
|
157
|
+
@click="remove"
|
|
158
|
+
/>
|
|
159
|
+
<div class="ml-auto flex flex-wrap items-center justify-end gap-2">
|
|
160
|
+
<FormSaveHint
|
|
161
|
+
v-if="mode !== 'view'"
|
|
162
|
+
:missing="missing"
|
|
163
|
+
/>
|
|
164
|
+
<UButton
|
|
165
|
+
:label="mode === 'view' ? 'Close' : 'Cancel'"
|
|
166
|
+
color="neutral"
|
|
167
|
+
variant="ghost"
|
|
168
|
+
:disabled="saving || deleting"
|
|
169
|
+
@click="requestClose"
|
|
170
|
+
/>
|
|
171
|
+
<UButton
|
|
172
|
+
v-if="mode !== 'view'"
|
|
173
|
+
type="submit"
|
|
174
|
+
:label="row ? 'Save' : 'Create'"
|
|
175
|
+
:loading="saving"
|
|
176
|
+
:disabled="missing.length > 0"
|
|
177
|
+
data-testid="data-row-save"
|
|
178
|
+
/>
|
|
179
|
+
</div>
|
|
180
|
+
</div>
|
|
181
|
+
</template>
|
|
182
|
+
</SchemaForm>
|
|
183
|
+
</template>
|
|
184
|
+
</USlideover>
|
|
185
|
+
</template>
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { FieldDescriptor } from '@skaile/forge-data'
|
|
3
|
+
|
|
4
|
+
/** One cell of the data table, formatted by the field's type; a foreign key shows its expanded label. */
|
|
5
|
+
const props = defineProps<{
|
|
6
|
+
field: FieldDescriptor
|
|
7
|
+
value: unknown
|
|
8
|
+
relation?: { id: string; label: string | null } | null
|
|
9
|
+
}>()
|
|
10
|
+
|
|
11
|
+
const { formatDateTime } = useDates()
|
|
12
|
+
|
|
13
|
+
const text = computed(() => {
|
|
14
|
+
const { field, value, relation } = props
|
|
15
|
+
if (relation) return relation.label ?? relation.id
|
|
16
|
+
if (field.type === 'datetime') return formatDateTime(value as string)
|
|
17
|
+
if (field.type === 'string[]') return (value as string[]).join(', ')
|
|
18
|
+
if (field.type === 'json') return JSON.stringify(value)
|
|
19
|
+
return String(value)
|
|
20
|
+
})
|
|
21
|
+
</script>
|
|
22
|
+
|
|
23
|
+
<template>
|
|
24
|
+
<span
|
|
25
|
+
v-if="value === null || value === undefined"
|
|
26
|
+
class="text-dimmed"
|
|
27
|
+
>—</span>
|
|
28
|
+
<UIcon
|
|
29
|
+
v-else-if="field.type === 'boolean'"
|
|
30
|
+
:name="value ? 'i-lucide-check' : 'i-lucide-minus'"
|
|
31
|
+
:class="value ? 'size-4 text-success' : 'size-4 text-dimmed'"
|
|
32
|
+
/>
|
|
33
|
+
<UBadge
|
|
34
|
+
v-else-if="field.enum"
|
|
35
|
+
color="neutral"
|
|
36
|
+
variant="subtle"
|
|
37
|
+
:label="text"
|
|
38
|
+
/>
|
|
39
|
+
<code
|
|
40
|
+
v-else-if="field.type === 'json'"
|
|
41
|
+
class="block max-w-xs truncate text-xs"
|
|
42
|
+
>{{ text }}</code>
|
|
43
|
+
<span
|
|
44
|
+
v-else
|
|
45
|
+
class="block max-w-xs truncate"
|
|
46
|
+
:class="{ 'font-mono text-xs text-muted': field.type === 'uuid' && !relation }"
|
|
47
|
+
:title="text"
|
|
48
|
+
>{{ text }}</span>
|
|
49
|
+
</template>
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The model as the signed-in principal may see it (`data.describe`), shared by
|
|
3
|
+
* the browser's pages. Rows go through the core's untyped `useDataApi()`.
|
|
4
|
+
*/
|
|
5
|
+
export function useDataModel() {
|
|
6
|
+
const api = useDataApi()
|
|
7
|
+
return useAsyncData('data-browser:model', () => api.describe(), { dedupe: 'defer' })
|
|
8
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The data browser is for owners (`me.get` › `principal.roles`); everyone else
|
|
3
|
+
* goes to the dashboard. The server still decides every call on its own:
|
|
4
|
+
* owner-only entities (`ops.ownerOnly`) answer 403 to anyone else.
|
|
5
|
+
*/
|
|
6
|
+
export default defineNuxtRouteMiddleware(async () => {
|
|
7
|
+
const { data: me } = await useMe()
|
|
8
|
+
if (!me.value?.principal.roles.includes('owner')) return navigateTo('/')
|
|
9
|
+
})
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { TableColumn } from '@nuxt/ui'
|
|
3
|
+
import type { Cond, FieldDescriptor, Row, Sort } from '@skaile/forge-data'
|
|
4
|
+
import { h } from 'vue'
|
|
5
|
+
|
|
6
|
+
definePageMeta({ middleware: 'owner-only', key: route => route.path })
|
|
7
|
+
|
|
8
|
+
const route = useRoute()
|
|
9
|
+
const api = useDataApi()
|
|
10
|
+
const { data: model } = await useDataModel()
|
|
11
|
+
|
|
12
|
+
const found = model.value?.entities.find(e => e.name === route.params.entity)
|
|
13
|
+
if (!found || !model.value)
|
|
14
|
+
throw createError({ statusCode: 404, statusMessage: `No entity '${route.params.entity}'`, fatal: true })
|
|
15
|
+
const entity = found
|
|
16
|
+
const dataModel = model.value
|
|
17
|
+
useHead({ title: `${entityTitleOf(entity)} · Data` })
|
|
18
|
+
|
|
19
|
+
const PAGE_SIZE = 25
|
|
20
|
+
const q = ref('')
|
|
21
|
+
const search = refDebounced(q, 250)
|
|
22
|
+
const sort = ref<Sort | null>(null)
|
|
23
|
+
const where = ref<Cond[]>([])
|
|
24
|
+
const page = ref(1)
|
|
25
|
+
const showFilters = ref(false)
|
|
26
|
+
watch([search, sort, where], () => {
|
|
27
|
+
page.value = 1
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
// Every readable to-one relation is expanded, so foreign-key columns show labels.
|
|
31
|
+
const expand = entity.relations.map(relation => relation.name)
|
|
32
|
+
|
|
33
|
+
const { data, status, error, refresh } = await useAsyncData(
|
|
34
|
+
`data-browser:list:${entity.name}`,
|
|
35
|
+
() =>
|
|
36
|
+
api.list(entity.name, {
|
|
37
|
+
where: where.value.length > 0 ? where.value : undefined,
|
|
38
|
+
search: search.value.trim() || undefined,
|
|
39
|
+
sort: sort.value ? [sort.value] : undefined,
|
|
40
|
+
expand: expand.length > 0 ? expand : undefined,
|
|
41
|
+
limit: PAGE_SIZE,
|
|
42
|
+
offset: (page.value - 1) * PAGE_SIZE,
|
|
43
|
+
}),
|
|
44
|
+
{ watch: [search, sort, where, page] },
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
/** Header click: ascending, descending, then back to the entity's default order. */
|
|
48
|
+
function toggleSort(field: string) {
|
|
49
|
+
const current = sort.value?.field === field ? sort.value.order : undefined
|
|
50
|
+
sort.value = current === undefined ? { field, order: 'asc' } : current === 'asc' ? { field, order: 'desc' } : null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const UButton = resolveComponent('UButton')
|
|
54
|
+
const DataValue = resolveComponent('DataValue')
|
|
55
|
+
|
|
56
|
+
function headerOf(field: FieldDescriptor, label: string) {
|
|
57
|
+
if (!field.sortable) return label
|
|
58
|
+
const order = sort.value?.field === field.key ? sort.value.order : undefined
|
|
59
|
+
return h(UButton, {
|
|
60
|
+
label,
|
|
61
|
+
color: 'neutral',
|
|
62
|
+
variant: 'ghost',
|
|
63
|
+
class: '-mx-2.5',
|
|
64
|
+
trailingIcon:
|
|
65
|
+
order === 'asc'
|
|
66
|
+
? 'i-lucide-arrow-up-narrow-wide'
|
|
67
|
+
: order === 'desc'
|
|
68
|
+
? 'i-lucide-arrow-down-wide-narrow'
|
|
69
|
+
: 'i-lucide-arrow-up-down',
|
|
70
|
+
'aria-label': `Sort by ${label}`,
|
|
71
|
+
onClick: () => toggleSort(field.key),
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const columns = computed<TableColumn<Row>[]>(() =>
|
|
76
|
+
entity.fields.map(field => {
|
|
77
|
+
const relation = relationOfField(entity, field.key)
|
|
78
|
+
const label = relation ? relationLabelOf(entity, relation) : field.label
|
|
79
|
+
return {
|
|
80
|
+
id: field.key,
|
|
81
|
+
accessorFn: row => row[field.key],
|
|
82
|
+
header: () => headerOf(field, label),
|
|
83
|
+
cell: ({ row }) =>
|
|
84
|
+
h(DataValue, {
|
|
85
|
+
field,
|
|
86
|
+
value: row.original[field.key],
|
|
87
|
+
relation: relation ? row.original[relation.name] : undefined,
|
|
88
|
+
}),
|
|
89
|
+
}
|
|
90
|
+
}),
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
const selected = ref<Row | null>(null)
|
|
94
|
+
const formOpen = ref(false)
|
|
95
|
+
function openRow(row: Row | null) {
|
|
96
|
+
selected.value = row
|
|
97
|
+
formOpen.value = true
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const range = computed(() => {
|
|
101
|
+
const total = data.value?.total ?? 0
|
|
102
|
+
if (total === 0) return 'No rows'
|
|
103
|
+
const first = (page.value - 1) * PAGE_SIZE + 1
|
|
104
|
+
return `${first}–${Math.min(first + PAGE_SIZE - 1, total)} of ${total}`
|
|
105
|
+
})
|
|
106
|
+
</script>
|
|
107
|
+
|
|
108
|
+
<template>
|
|
109
|
+
<UDashboardPanel :id="`data-${entity.name}`">
|
|
110
|
+
<template #header>
|
|
111
|
+
<AppHeader
|
|
112
|
+
:title="entityTitleOf(entity)"
|
|
113
|
+
:badge="data?.total"
|
|
114
|
+
back="/data"
|
|
115
|
+
back-label="All entities"
|
|
116
|
+
>
|
|
117
|
+
<template
|
|
118
|
+
v-if="entity.ops.create"
|
|
119
|
+
#actions
|
|
120
|
+
>
|
|
121
|
+
<UButton
|
|
122
|
+
icon="i-lucide-plus"
|
|
123
|
+
:label="`New ${entity.label.toLowerCase()}`"
|
|
124
|
+
data-testid="data-new"
|
|
125
|
+
@click="openRow(null)"
|
|
126
|
+
/>
|
|
127
|
+
</template>
|
|
128
|
+
</AppHeader>
|
|
129
|
+
<UDashboardToolbar>
|
|
130
|
+
<template #left>
|
|
131
|
+
<UInput
|
|
132
|
+
v-if="entity.search.length > 0"
|
|
133
|
+
v-model="q"
|
|
134
|
+
icon="i-lucide-search"
|
|
135
|
+
:placeholder="`Search ${entity.search.join(', ')}…`"
|
|
136
|
+
class="w-64"
|
|
137
|
+
data-testid="data-search"
|
|
138
|
+
/>
|
|
139
|
+
<UButton
|
|
140
|
+
icon="i-lucide-list-filter"
|
|
141
|
+
:label="where.length > 0 ? `Filter (${where.length})` : 'Filter'"
|
|
142
|
+
color="neutral"
|
|
143
|
+
:variant="showFilters ? 'soft' : 'ghost'"
|
|
144
|
+
data-testid="data-filter-toggle"
|
|
145
|
+
@click="showFilters = !showFilters"
|
|
146
|
+
/>
|
|
147
|
+
</template>
|
|
148
|
+
<template #right>
|
|
149
|
+
<span class="text-sm text-muted">{{ range }}</span>
|
|
150
|
+
</template>
|
|
151
|
+
</UDashboardToolbar>
|
|
152
|
+
</template>
|
|
153
|
+
|
|
154
|
+
<template #body>
|
|
155
|
+
<p class="text-sm text-muted">
|
|
156
|
+
{{ entity.description }}
|
|
157
|
+
</p>
|
|
158
|
+
<DataFilterBuilder
|
|
159
|
+
v-show="showFilters"
|
|
160
|
+
v-model="where"
|
|
161
|
+
:model="dataModel"
|
|
162
|
+
:entity="entity"
|
|
163
|
+
/>
|
|
164
|
+
<UAlert
|
|
165
|
+
v-if="error"
|
|
166
|
+
color="error"
|
|
167
|
+
variant="subtle"
|
|
168
|
+
icon="i-lucide-circle-alert"
|
|
169
|
+
:title="actionErrorMessage(error)"
|
|
170
|
+
/>
|
|
171
|
+
<UTable
|
|
172
|
+
:data="data?.items ?? []"
|
|
173
|
+
:columns="columns"
|
|
174
|
+
:loading="status === 'pending'"
|
|
175
|
+
empty="No rows match."
|
|
176
|
+
class="flex-1"
|
|
177
|
+
:ui="{ tr: 'cursor-pointer' }"
|
|
178
|
+
data-testid="data-table"
|
|
179
|
+
@select="(_event, row) => openRow(row.original)"
|
|
180
|
+
/>
|
|
181
|
+
<div
|
|
182
|
+
v-if="(data?.total ?? 0) > PAGE_SIZE"
|
|
183
|
+
class="flex justify-end"
|
|
184
|
+
>
|
|
185
|
+
<UPagination
|
|
186
|
+
v-model:page="page"
|
|
187
|
+
:total="data?.total ?? 0"
|
|
188
|
+
:items-per-page="PAGE_SIZE"
|
|
189
|
+
/>
|
|
190
|
+
</div>
|
|
191
|
+
<DataRowSlideover
|
|
192
|
+
v-model:open="formOpen"
|
|
193
|
+
:model="dataModel"
|
|
194
|
+
:entity="entity"
|
|
195
|
+
:row="selected"
|
|
196
|
+
@saved="refresh"
|
|
197
|
+
@deleted="refresh"
|
|
198
|
+
/>
|
|
199
|
+
</template>
|
|
200
|
+
</UDashboardPanel>
|
|
201
|
+
</template>
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
definePageMeta({ middleware: 'owner-only' })
|
|
3
|
+
useHead({ title: 'Data' })
|
|
4
|
+
|
|
5
|
+
const api = useDataApi()
|
|
6
|
+
const { data: model } = await useDataModel()
|
|
7
|
+
|
|
8
|
+
// Row counts, one `data.aggregate` per entity.
|
|
9
|
+
const { data: counts } = await useAsyncData(
|
|
10
|
+
'data-browser:counts',
|
|
11
|
+
async () => {
|
|
12
|
+
const entities = model.value?.entities ?? []
|
|
13
|
+
const totals = await Promise.all(
|
|
14
|
+
entities.map(entity => api.aggregate(entity.name).then(result => aggregateCount(result))),
|
|
15
|
+
)
|
|
16
|
+
return Object.fromEntries(entities.map((entity, index) => [entity.name, totals[index]!]))
|
|
17
|
+
},
|
|
18
|
+
{ watch: [model] },
|
|
19
|
+
)
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<template>
|
|
23
|
+
<UDashboardPanel id="data">
|
|
24
|
+
<template #header>
|
|
25
|
+
<AppHeader title="Data" />
|
|
26
|
+
</template>
|
|
27
|
+
|
|
28
|
+
<template #body>
|
|
29
|
+
<p class="text-sm text-muted">
|
|
30
|
+
Every entity the app exposes through the <code>data.*</code> actions, with the same checks as the API and the agents.
|
|
31
|
+
</p>
|
|
32
|
+
<UPageGrid class="lg:grid-cols-3">
|
|
33
|
+
<UPageCard
|
|
34
|
+
v-for="entity in model?.entities ?? []"
|
|
35
|
+
:key="entity.name"
|
|
36
|
+
:to="`/data/${entity.name}`"
|
|
37
|
+
:title="entityTitleOf(entity)"
|
|
38
|
+
:description="entity.description"
|
|
39
|
+
icon="i-lucide-table-2"
|
|
40
|
+
variant="subtle"
|
|
41
|
+
:data-testid="`data-entity-${entity.name}`"
|
|
42
|
+
>
|
|
43
|
+
<template #footer>
|
|
44
|
+
<div class="flex flex-wrap items-center gap-2">
|
|
45
|
+
<UBadge
|
|
46
|
+
color="neutral"
|
|
47
|
+
variant="outline"
|
|
48
|
+
:label="counts ? `${counts[entity.name]} ${counts[entity.name] === 1 ? 'row' : 'rows'}` : '… rows'"
|
|
49
|
+
/>
|
|
50
|
+
<UBadge
|
|
51
|
+
v-if="!entity.ops.create && !entity.ops.update && !entity.ops.delete"
|
|
52
|
+
color="neutral"
|
|
53
|
+
variant="subtle"
|
|
54
|
+
label="read-only"
|
|
55
|
+
/>
|
|
56
|
+
<UBadge
|
|
57
|
+
v-if="entity.ops.ownerOnly"
|
|
58
|
+
color="warning"
|
|
59
|
+
variant="subtle"
|
|
60
|
+
label="owner only"
|
|
61
|
+
/>
|
|
62
|
+
</div>
|
|
63
|
+
</template>
|
|
64
|
+
</UPageCard>
|
|
65
|
+
</UPageGrid>
|
|
66
|
+
</template>
|
|
67
|
+
</UDashboardPanel>
|
|
68
|
+
</template>
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Cond,
|
|
3
|
+
DataModel,
|
|
4
|
+
EntityDescriptor,
|
|
5
|
+
FieldDescriptor,
|
|
6
|
+
FieldType,
|
|
7
|
+
FilterOp,
|
|
8
|
+
RelationDescriptor,
|
|
9
|
+
Row,
|
|
10
|
+
} from '@skaile/forge-data'
|
|
11
|
+
// Explicit imports (Nuxt would auto-import them) so the layer's unit tests run without Nuxt.
|
|
12
|
+
import { fromDateTimeInput, toDateTimeInput } from '~/utils/format'
|
|
13
|
+
import type { JsonSchema } from '~/utils/jsonSchema'
|
|
14
|
+
import { labelOfKey } from '~/utils/jsonSchema'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Operators the filter builder offers per field type: a subset of what the
|
|
18
|
+
* server accepts (forge-data `opsFor`); enums get eq/ne/in/nin, and the null
|
|
19
|
+
* checks only appear on nullable fields (json has nothing else). Datetimes
|
|
20
|
+
* get ranges only: an instant is never equal to a minute-precision input.
|
|
21
|
+
*/
|
|
22
|
+
const OPS_BY_TYPE: Record<FieldType, FilterOp[]> = {
|
|
23
|
+
string: ['contains', 'eq', 'ne', 'startsWith', 'in', 'nin', 'isNull', 'notNull'],
|
|
24
|
+
text: ['contains', 'eq', 'ne', 'startsWith', 'isNull', 'notNull'],
|
|
25
|
+
uuid: ['eq', 'ne', 'in', 'nin', 'isNull', 'notNull'],
|
|
26
|
+
boolean: ['eq', 'ne', 'isNull', 'notNull'],
|
|
27
|
+
integer: ['eq', 'ne', 'gt', 'gte', 'lt', 'lte', 'in', 'nin', 'isNull', 'notNull'],
|
|
28
|
+
number: ['eq', 'ne', 'gt', 'gte', 'lt', 'lte', 'in', 'nin', 'isNull', 'notNull'],
|
|
29
|
+
datetime: ['gte', 'lt', 'gt', 'lte', 'isNull', 'notNull'],
|
|
30
|
+
'string[]': ['contains', 'isNull', 'notNull'],
|
|
31
|
+
json: ['isNull', 'notNull'],
|
|
32
|
+
}
|
|
33
|
+
const ENUM_OPS: FilterOp[] = ['eq', 'ne', 'in', 'nin', 'isNull', 'notNull']
|
|
34
|
+
|
|
35
|
+
export const FILTER_OP_LABELS: Record<FilterOp, string> = {
|
|
36
|
+
eq: 'is',
|
|
37
|
+
ne: 'is not',
|
|
38
|
+
gt: '>',
|
|
39
|
+
gte: '≥',
|
|
40
|
+
lt: '<',
|
|
41
|
+
lte: '≤',
|
|
42
|
+
in: 'is any of',
|
|
43
|
+
nin: 'is none of',
|
|
44
|
+
contains: 'contains',
|
|
45
|
+
startsWith: 'starts with',
|
|
46
|
+
isNull: 'is empty',
|
|
47
|
+
notNull: 'is not empty',
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function filterOpsOf(field: FieldDescriptor): FilterOp[] {
|
|
51
|
+
const ops = field.enum ? ENUM_OPS : OPS_BY_TYPE[field.type]
|
|
52
|
+
if (field.nullable || field.type === 'json') return ops
|
|
53
|
+
return ops.filter(op => op !== 'isNull' && op !== 'notNull')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** One choice of the filter builder's field select: an own field or a one-level relation path (`project.name`). */
|
|
57
|
+
export interface FilterPath {
|
|
58
|
+
path: string
|
|
59
|
+
label: string
|
|
60
|
+
field: FieldDescriptor
|
|
61
|
+
/** The target entity when the path is an own foreign key (value picked from its rows). */
|
|
62
|
+
target?: EntityDescriptor
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A relation's heading: its foreign key's label without the id (`Project id` → `Project`, `Created by` stays). */
|
|
66
|
+
export function relationLabelOf(entity: EntityDescriptor, relation: RelationDescriptor): string {
|
|
67
|
+
const label = entity.fields.find(field => field.key === relation.field)?.label ?? labelOfKey(relation.name)
|
|
68
|
+
return label.replace(/ id$/i, '')
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function filterPathsOf(model: DataModel, entity: EntityDescriptor): FilterPath[] {
|
|
72
|
+
const entityNamed = (name: string) => model.entities.find(e => e.name === name)
|
|
73
|
+
return entity.filterable.flatMap((path): FilterPath[] => {
|
|
74
|
+
const dot = path.indexOf('.')
|
|
75
|
+
if (dot < 0) {
|
|
76
|
+
const field = entity.fields.find(f => f.key === path)
|
|
77
|
+
if (!field || filterOpsOf(field).length === 0) return []
|
|
78
|
+
const relation = entity.relations.find(r => r.field === path)
|
|
79
|
+
return [{ path, label: field.label, field, target: relation && entityNamed(relation.target) }]
|
|
80
|
+
}
|
|
81
|
+
const relation = entity.relations.find(r => r.name === path.slice(0, dot))
|
|
82
|
+
const field = relation && entityNamed(relation.target)?.fields.find(f => f.key === path.slice(dot + 1))
|
|
83
|
+
if (!field || filterOpsOf(field).length === 0) return []
|
|
84
|
+
return [{ path, label: `${relationLabelOf(entity, relation)} › ${field.label}`, field }]
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const isListOp = (op: FilterOp) => op === 'in' || op === 'nin'
|
|
89
|
+
export const isNullOp = (op: FilterOp) => op === 'isNull' || op === 'notNull'
|
|
90
|
+
|
|
91
|
+
/** A condition the server can take: null checks need no value, `in`/`nin` at least one. */
|
|
92
|
+
function isCompleteCond(cond: Cond): boolean {
|
|
93
|
+
if (isNullOp(cond.op)) return true
|
|
94
|
+
if (isListOp(cond.op)) return Array.isArray(cond.value) && cond.value.length > 0
|
|
95
|
+
return cond.value !== undefined && cond.value !== null && cond.value !== ''
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* One row of the filter builder as its controls hold it: `in`/`nin` values as
|
|
100
|
+
* picked (tags are strings), datetimes as `datetime-local` text on the app's
|
|
101
|
+
* clock.
|
|
102
|
+
*/
|
|
103
|
+
export interface FilterRow {
|
|
104
|
+
path: string
|
|
105
|
+
op: FilterOp
|
|
106
|
+
value: unknown
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The condition `row` stands for, or `undefined` while it is incomplete. */
|
|
110
|
+
function condOf(row: FilterRow, path: FilterPath | undefined, timeZone: string): Cond | undefined {
|
|
111
|
+
if (!path) return undefined
|
|
112
|
+
if (isNullOp(row.op)) return { field: row.path, op: row.op }
|
|
113
|
+
const type = path.field.type
|
|
114
|
+
let value = row.value
|
|
115
|
+
if (isListOp(row.op)) {
|
|
116
|
+
const values = Array.isArray(row.value) ? row.value : []
|
|
117
|
+
// Tags on a number field: only what reads as a finite number, never NaN (sent as null).
|
|
118
|
+
value =
|
|
119
|
+
type === 'integer' || type === 'number'
|
|
120
|
+
? values
|
|
121
|
+
.filter(v => String(v).trim() !== '')
|
|
122
|
+
.map(Number)
|
|
123
|
+
.filter(Number.isFinite)
|
|
124
|
+
: values
|
|
125
|
+
} else if (type === 'datetime') {
|
|
126
|
+
value = typeof row.value === 'string' ? fromDateTimeInput(row.value, timeZone) : undefined
|
|
127
|
+
}
|
|
128
|
+
const cond = { field: row.path, op: row.op, value }
|
|
129
|
+
return isCompleteCond(cond) ? cond : undefined
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The `where` of the builder's rows: the complete ones as conditions (null
|
|
134
|
+
* checks without a value, numeric `in`/`nin` as numbers, datetimes as ISO
|
|
135
|
+
* instants read on `timeZone`'s clock); incomplete rows are left out.
|
|
136
|
+
*/
|
|
137
|
+
export function conditionsOf(rows: FilterRow[], paths: FilterPath[], timeZone: string): Cond[] {
|
|
138
|
+
const byPath = new Map(paths.map(path => [path.path, path]))
|
|
139
|
+
return rows.flatMap(row => condOf(row, byPath.get(row.path), timeZone) ?? [])
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** The builder row of a condition (the inverse of `conditionsOf`), for the conditions it starts with. */
|
|
143
|
+
export function filterRowOf(cond: Cond, path: FilterPath | undefined, timeZone: string): FilterRow {
|
|
144
|
+
const local = path?.field.type === 'datetime' && !isListOp(cond.op) && typeof cond.value === 'string'
|
|
145
|
+
return { path: cond.field, op: cond.op, value: local ? toDateTimeInput(cond.value as string, timeZone) : cond.value }
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Plural title of an entity for headings (`tasks` → `Tasks`). */
|
|
149
|
+
export const entityTitleOf = (entity: EntityDescriptor) => labelOfKey(entity.name)
|
|
150
|
+
|
|
151
|
+
/** The own to-one relation whose foreign key is `field`, if any. */
|
|
152
|
+
export const relationOfField = (entity: EntityDescriptor, field: string): RelationDescriptor | undefined =>
|
|
153
|
+
entity.relations.find(relation => relation.field === field)
|
|
154
|
+
|
|
155
|
+
type FormMode = 'create' | 'edit' | 'view'
|
|
156
|
+
|
|
157
|
+
/** Whether the form may change `field`: not read-only, computed, or (once created) the primary key. */
|
|
158
|
+
function isWritable(field: FieldDescriptor, mode: FormMode): boolean {
|
|
159
|
+
return mode !== 'view' && !field.readOnly && !field.computed && !(mode === 'edit' && field.primaryKey)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function jsonTypeOf(field: FieldDescriptor): JsonSchema {
|
|
163
|
+
switch (field.type) {
|
|
164
|
+
case 'integer':
|
|
165
|
+
return { type: 'integer' }
|
|
166
|
+
case 'number':
|
|
167
|
+
return { type: 'number' }
|
|
168
|
+
case 'boolean':
|
|
169
|
+
return { type: 'boolean' }
|
|
170
|
+
case 'datetime':
|
|
171
|
+
return { type: 'string', format: 'date-time' }
|
|
172
|
+
case 'string[]':
|
|
173
|
+
return { type: 'array', items: { type: 'string' } }
|
|
174
|
+
case 'json':
|
|
175
|
+
return {}
|
|
176
|
+
default:
|
|
177
|
+
return field.enum ? { type: 'string', enum: field.enum } : { type: 'string' }
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* The JSON schema `SchemaForm` renders for a row of `entity`: one property per
|
|
183
|
+
* field `data.describe` returns, hidden ones never among them (on create only
|
|
184
|
+
* the writable ones, the rest has no value yet),
|
|
185
|
+
* nullable where the column is, read-only where the form may not write,
|
|
186
|
+
* required where it has neither a default nor `null`. Foreign keys are titled
|
|
187
|
+
* after their relation (`Project`, not `Project id`).
|
|
188
|
+
*/
|
|
189
|
+
export function rowSchemaOf(entity: EntityDescriptor, mode: FormMode): JsonSchema {
|
|
190
|
+
const properties: Record<string, JsonSchema> = {}
|
|
191
|
+
const required: string[] = []
|
|
192
|
+
for (const field of entity.fields) {
|
|
193
|
+
const relation = relationOfField(entity, field.key)
|
|
194
|
+
const base = jsonTypeOf(field)
|
|
195
|
+
const writable = isWritable(field, mode)
|
|
196
|
+
if (mode === 'create' && !writable) continue
|
|
197
|
+
const nullable =
|
|
198
|
+
field.nullable && base.type !== undefined
|
|
199
|
+
? { type: [base.type as string, 'null'], ...(base.enum ? { enum: [...base.enum, null] } : {}) }
|
|
200
|
+
: {}
|
|
201
|
+
properties[field.key] = {
|
|
202
|
+
...base,
|
|
203
|
+
...nullable,
|
|
204
|
+
title: relation ? relationLabelOf(entity, relation) : field.label,
|
|
205
|
+
...(field.description ? { description: field.description } : {}),
|
|
206
|
+
...(writable ? {} : { readOnly: true }),
|
|
207
|
+
...(field.long ? { 'x-multiline': true } : {}),
|
|
208
|
+
}
|
|
209
|
+
if (writable && !field.nullable && !field.hasDefault) required.push(field.key)
|
|
210
|
+
}
|
|
211
|
+
return { type: 'object', properties, required }
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** The form state of a row (`null`: a new one). */
|
|
215
|
+
export function formStateOf(entity: EntityDescriptor, row: Row | null): Record<string, unknown> {
|
|
216
|
+
return Object.fromEntries(entity.fields.map(field => [field.key, row ? row[field.key] : undefined]))
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* What to send: on create the writable fields that have a value, on edit the
|
|
221
|
+
* writable fields that differ from `row` (the server would ignore read-only
|
|
222
|
+
* ones, but an edit that changes nothing is a 400).
|
|
223
|
+
*/
|
|
224
|
+
export function writableValuesOf(entity: EntityDescriptor, values: Record<string, unknown>, row: Row | null): Row {
|
|
225
|
+
const out: Row = {}
|
|
226
|
+
for (const field of entity.fields) {
|
|
227
|
+
if (!isWritable(field, row ? 'edit' : 'create')) continue
|
|
228
|
+
const value = values[field.key]
|
|
229
|
+
if (row ? JSON.stringify(value ?? null) !== JSON.stringify(row[field.key] ?? null) : value !== undefined)
|
|
230
|
+
out[field.key] = value
|
|
231
|
+
}
|
|
232
|
+
return out
|
|
233
|
+
}
|
package/nuxt.config.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { defineNuxtConfig } from 'nuxt/config'
|
|
2
|
+
|
|
3
|
+
// Layer: `extends: ['@skaile/forge-layer-data-browser']` in an app built on forge-template-app.
|
|
4
|
+
//
|
|
5
|
+
// The owner's view of every entity `data.describe` exposes (/data), with a filterable table and a generic row form.
|
|
6
|
+
// It adds no actions and no tables; it uses the host's `data.*` actions, `SchemaForm` and the other parts of the
|
|
7
|
+
// host contract in README.md. Remove the `extends` entry to drop it.
|
|
8
|
+
export default defineNuxtConfig({})
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@skaile/forge-layer-data-browser",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Nuxt layer for apps built on forge-template-app: the owner's view of every entity data.describe exposes (/data), with a filterable table, a filter builder and a generic row form.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./nuxt.config.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"nuxt.config.ts",
|
|
9
|
+
"app",
|
|
10
|
+
"README.md",
|
|
11
|
+
"CHANGELOG.md",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/skaile-ai/forge.git",
|
|
17
|
+
"directory": "packages/layer-data-browser"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"nuxt",
|
|
21
|
+
"nuxt-layer",
|
|
22
|
+
"forge-data",
|
|
23
|
+
"skaile"
|
|
24
|
+
],
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"scripts": {
|
|
30
|
+
"check": "bun ../../scripts/check-package.ts"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@nuxt/ui": "^4.11.1",
|
|
34
|
+
"@skaile/forge-data": "^0.2.1",
|
|
35
|
+
"nuxt": "^4.0.0",
|
|
36
|
+
"vue": "^3.5.0"
|
|
37
|
+
},
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=20"
|
|
40
|
+
}
|
|
41
|
+
}
|