meta-to-blocks 0.1.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 +89 -0
- package/convert.d.ts +19 -0
- package/convert.js +196 -0
- package/esm/convert.js +190 -0
- package/esm/index.js +4 -0
- package/esm/naming.js +19 -0
- package/esm/schema.js +239 -0
- package/esm/types.js +10 -0
- package/index.d.ts +4 -0
- package/index.js +20 -0
- package/naming.d.ts +8 -0
- package/naming.js +22 -0
- package/package.json +52 -0
- package/schema.d.ts +32 -0
- package/schema.js +247 -0
- package/types.d.ts +157 -0
- package/types.js +11 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Constructive
|
|
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
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# meta-to-blocks
|
|
2
|
+
|
|
3
|
+
<p align="center" width="100%">
|
|
4
|
+
<img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
Lower Constructive `_meta` database metadata into
|
|
8
|
+
[`blocks-schema`](../blocks-schema) UI documents: generated form, list, and
|
|
9
|
+
detail screens with no hand-authored JSON. Columns lower through JSON Schema, so
|
|
10
|
+
this shares the widget-rule pipeline with
|
|
11
|
+
[`json-schema-to-blocks`](../json-schema-to-blocks). Pure functions, no React.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add meta-to-blocks blocks-schema
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Built with `makage` and published from `dist`, so deep imports resolve without
|
|
20
|
+
an exports map:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { tableToFormDocument } from 'meta-to-blocks';
|
|
24
|
+
import { tableToSchema } from 'meta-to-blocks/schema';
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { tableToFormDocument, tableToListDocument, tableToDetailDocument } from 'meta-to-blocks';
|
|
31
|
+
|
|
32
|
+
const form = tableToFormDocument(table); // Page > Form > field nodes
|
|
33
|
+
const list = tableToListDocument(table); // Page > DataTable
|
|
34
|
+
const detail = tableToDetailDocument(table); // Page > DetailPanel > RelationList*
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Create forms omit the columns the database fills in (primary keys, audit
|
|
38
|
+
timestamps); `mode: 'update'` includes them and disables the primary key.
|
|
39
|
+
|
|
40
|
+
## Customization
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
tableToFormDocument(table, {
|
|
44
|
+
title: 'New post',
|
|
45
|
+
mode: 'create',
|
|
46
|
+
omitFields: ['search_vector'],
|
|
47
|
+
fieldOrder: ['title', 'body'],
|
|
48
|
+
fields: {
|
|
49
|
+
body: { widget: 'MarkdownEditor', label: 'Post body' },
|
|
50
|
+
author_id: { description: 'Who wrote it' }
|
|
51
|
+
},
|
|
52
|
+
rules: [(schema) => (schema.format === 'uuid' ? { type: 'Input' } : undefined)]
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Custom `rules` run ahead of the JSON Schema defaults unless
|
|
57
|
+
`replaceDefaultRules` is set.
|
|
58
|
+
|
|
59
|
+
## Lowering
|
|
60
|
+
|
|
61
|
+
| `_meta` | Result |
|
|
62
|
+
| --- | --- |
|
|
63
|
+
| Scalar encoding, then Postgres type, then GraphQL scalar | JSON Schema type/format, most specific source first |
|
|
64
|
+
| `enumValues` | `enum`, so a small set lowers to `RadioGroup` and a larger one to `Select` |
|
|
65
|
+
| `character varying(n)` | `maxLength: n` |
|
|
66
|
+
| `isNotNull && !hasDefault` | required field |
|
|
67
|
+
| nullable column | type union with `null` |
|
|
68
|
+
| array column | repeatable `Section` |
|
|
69
|
+
| foreign key | `Select` with `relation` props, labelled without the id suffix |
|
|
70
|
+
| `hasMany` / `manyToMany` relation | `RelationList` on the detail screen |
|
|
71
|
+
|
|
72
|
+
## API
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
tableToFormDocument(table, options?): UIDocument
|
|
76
|
+
tableToListDocument(table, options?): UIDocument
|
|
77
|
+
tableToDetailDocument(table, options?): UIDocument
|
|
78
|
+
tableToNodes(table, options?): UINode[]
|
|
79
|
+
tableToSchema(table, options?): JSONSchema
|
|
80
|
+
fieldToSchema(table, field, override?): JSONSchema
|
|
81
|
+
typeToSchema(type): JSONSchema
|
|
82
|
+
selectFields(table, options?): MetaField[]
|
|
83
|
+
isSystemField(field): boolean
|
|
84
|
+
titleize(name): string
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
MIT — see the repository [LICENSE](../../LICENSE).
|
package/convert.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `_meta` → documents: the three screens a table implies.
|
|
3
|
+
*
|
|
4
|
+
* Forms lower through JSON Schema so `json-schema-to-blocks` picks the widgets;
|
|
5
|
+
* list and detail screens are block nodes (`DataTable`, `DetailPanel`,
|
|
6
|
+
* `RelationList`) whose props carry the table facts a renderer needs to fetch
|
|
7
|
+
* rows. Everything returned is a plain `UIDocument`: persist it, hand-edit it,
|
|
8
|
+
* or layer overrides on it with `composeDocument`.
|
|
9
|
+
*/
|
|
10
|
+
import { type UIDocument, type UINode } from 'blocks-schema';
|
|
11
|
+
import type { DetailOptions, FormOptions, ListOptions, MetaTable } from './types';
|
|
12
|
+
/** Form field nodes for a table, without a document envelope. */
|
|
13
|
+
export declare function tableToNodes(table: MetaTable, options?: FormOptions): UINode[];
|
|
14
|
+
/** Create/edit screen: `Page > Form > fields`. */
|
|
15
|
+
export declare function tableToFormDocument(table: MetaTable, options?: FormOptions): UIDocument;
|
|
16
|
+
/** List screen: `Page > DataTable`. */
|
|
17
|
+
export declare function tableToListDocument(table: MetaTable, options?: ListOptions): UIDocument;
|
|
18
|
+
/** Detail screen: `Page > DetailPanel` plus a `RelationList` per child relation. */
|
|
19
|
+
export declare function tableToDetailDocument(table: MetaTable, options?: DetailOptions): UIDocument;
|
package/convert.js
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* `_meta` → documents: the three screens a table implies.
|
|
4
|
+
*
|
|
5
|
+
* Forms lower through JSON Schema so `json-schema-to-blocks` picks the widgets;
|
|
6
|
+
* list and detail screens are block nodes (`DataTable`, `DetailPanel`,
|
|
7
|
+
* `RelationList`) whose props carry the table facts a renderer needs to fetch
|
|
8
|
+
* rows. Everything returned is a plain `UIDocument`: persist it, hand-edit it,
|
|
9
|
+
* or layer overrides on it with `composeDocument`.
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.tableToNodes = tableToNodes;
|
|
13
|
+
exports.tableToFormDocument = tableToFormDocument;
|
|
14
|
+
exports.tableToListDocument = tableToListDocument;
|
|
15
|
+
exports.tableToDetailDocument = tableToDetailDocument;
|
|
16
|
+
const blocks_schema_1 = require("blocks-schema");
|
|
17
|
+
const json_schema_to_blocks_1 = require("json-schema-to-blocks");
|
|
18
|
+
const schema_1 = require("./schema");
|
|
19
|
+
const naming_1 = require("./naming");
|
|
20
|
+
/** Columns too wide or too structural to sit in a table cell by default. */
|
|
21
|
+
const WIDE_FORMATS = new Set(['json', 'data-url']);
|
|
22
|
+
const DEFAULT_MAX_COLUMNS = 8;
|
|
23
|
+
function screenTitle(table, options) {
|
|
24
|
+
return options.title ?? (0, naming_1.titleize)(table.name);
|
|
25
|
+
}
|
|
26
|
+
function documentId(table, options, kind) {
|
|
27
|
+
return options.id ?? `${table.name}-${kind}`;
|
|
28
|
+
}
|
|
29
|
+
/** Facts a renderer needs to query the table, carried on the block node. */
|
|
30
|
+
function tableProps(table) {
|
|
31
|
+
return {
|
|
32
|
+
table: table.name,
|
|
33
|
+
...(table.schemaName ? { schemaName: table.schemaName } : {}),
|
|
34
|
+
...(table.query?.all ? { query: table.query.all } : {}),
|
|
35
|
+
...(table.query?.one ? { queryOne: table.query.one } : {}),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function primaryKeyFields(table) {
|
|
39
|
+
const fromConstraints = (0, schema_1.structuredConstraints)(table)?.primaryKey?.fields;
|
|
40
|
+
const fromLegacy = table.primaryKeyConstraints?.find((entry) => entry?.fields)?.fields;
|
|
41
|
+
const fields = fromConstraints ?? fromLegacy;
|
|
42
|
+
const names = (fields ?? []).map((field) => field?.name).filter((name) => Boolean(name));
|
|
43
|
+
if (names.length > 0)
|
|
44
|
+
return names;
|
|
45
|
+
return (table.fields ?? [])
|
|
46
|
+
.filter((field) => Boolean(field?.isPrimaryKey))
|
|
47
|
+
.map((field) => field.name);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The columns worth showing in a grid: narrow scalars first, capped, because a
|
|
51
|
+
* generated list with forty columns is unusable.
|
|
52
|
+
*/
|
|
53
|
+
function listColumns(table, options) {
|
|
54
|
+
const fields = (0, schema_1.selectFields)(table, { ...options, includeSystemFields: options.includeSystemFields ?? true });
|
|
55
|
+
if (options.columns?.length) {
|
|
56
|
+
const wanted = new Map(fields.map((field) => [field.name, field]));
|
|
57
|
+
return options.columns.map((name) => wanted.get(name)).filter((field) => Boolean(field));
|
|
58
|
+
}
|
|
59
|
+
const narrow = fields.filter((field) => {
|
|
60
|
+
const schema = (0, schema_1.typeToSchema)(field.type);
|
|
61
|
+
return !WIDE_FORMATS.has(String(schema.format ?? ''));
|
|
62
|
+
});
|
|
63
|
+
return narrow.slice(0, options.maxColumns ?? DEFAULT_MAX_COLUMNS);
|
|
64
|
+
}
|
|
65
|
+
function columnNode(field, override) {
|
|
66
|
+
const schema = (0, schema_1.typeToSchema)(field.type);
|
|
67
|
+
return {
|
|
68
|
+
name: field.name,
|
|
69
|
+
label: override?.label ?? (0, naming_1.titleize)(field.name),
|
|
70
|
+
type: schema.format ?? schema.type,
|
|
71
|
+
...(field.isPrimaryKey ? { primaryKey: true } : {}),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/** Form field nodes for a table, without a document envelope. */
|
|
75
|
+
function tableToNodes(table, options = {}) {
|
|
76
|
+
// A create form omits database-maintained columns; an update form shows the
|
|
77
|
+
// primary key but does not let it be edited.
|
|
78
|
+
const includeSystemFields = options.includeSystemFields ?? options.mode === 'update';
|
|
79
|
+
const schema = (0, schema_1.tableToSchema)(table, { ...options, includeSystemFields, id: documentId(table, options, 'form') });
|
|
80
|
+
const nodes = (0, json_schema_to_blocks_1.schemaToNodes)(schema, {
|
|
81
|
+
rules: options.rules,
|
|
82
|
+
replaceDefaultRules: options.replaceDefaultRules,
|
|
83
|
+
});
|
|
84
|
+
if (options.mode !== 'update')
|
|
85
|
+
return nodes;
|
|
86
|
+
const keys = new Set(primaryKeyFields(table));
|
|
87
|
+
return nodes.map((node) => keys.has(String(node.props.name)) ? { ...node, props: { ...node.props, disabled: true } } : node);
|
|
88
|
+
}
|
|
89
|
+
/** Create/edit screen: `Page > Form > fields`. */
|
|
90
|
+
function tableToFormDocument(table, options = {}) {
|
|
91
|
+
const fields = tableToNodes(table, options);
|
|
92
|
+
const form = {
|
|
93
|
+
type: 'Form',
|
|
94
|
+
key: 'form',
|
|
95
|
+
props: {
|
|
96
|
+
name: table.name,
|
|
97
|
+
...(options.submitLabel ? { submitLabel: options.submitLabel } : {}),
|
|
98
|
+
...(options.mode === 'update' ? { mode: 'update' } : {}),
|
|
99
|
+
},
|
|
100
|
+
children: fields,
|
|
101
|
+
};
|
|
102
|
+
return (0, blocks_schema_1.createDocument)({
|
|
103
|
+
type: 'Page',
|
|
104
|
+
key: options.rootKey ?? 'page',
|
|
105
|
+
props: { title: screenTitle(table, options) },
|
|
106
|
+
children: options.form === false ? fields : [form],
|
|
107
|
+
}, {
|
|
108
|
+
id: documentId(table, options, 'form'),
|
|
109
|
+
meta: { source: 'meta-to-blocks', table: table.name, kind: 'form' },
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
/** List screen: `Page > DataTable`. */
|
|
113
|
+
function tableToListDocument(table, options = {}) {
|
|
114
|
+
const columns = listColumns(table, options).map((field) => columnNode(field, options.fields?.[field.name]));
|
|
115
|
+
return (0, blocks_schema_1.createDocument)({
|
|
116
|
+
type: 'Page',
|
|
117
|
+
key: options.rootKey ?? 'page',
|
|
118
|
+
props: { title: screenTitle(table, options) },
|
|
119
|
+
children: [
|
|
120
|
+
{
|
|
121
|
+
type: 'DataTable',
|
|
122
|
+
key: `${table.name}.list`,
|
|
123
|
+
props: {
|
|
124
|
+
...tableProps(table),
|
|
125
|
+
columns,
|
|
126
|
+
...(primaryKeyFields(table).length > 0 ? { rowKey: primaryKeyFields(table) } : {}),
|
|
127
|
+
},
|
|
128
|
+
children: [],
|
|
129
|
+
},
|
|
130
|
+
],
|
|
131
|
+
}, {
|
|
132
|
+
id: documentId(table, options, 'list'),
|
|
133
|
+
meta: { source: 'meta-to-blocks', table: table.name, kind: 'list' },
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/** Detail screen: `Page > DetailPanel` plus a `RelationList` per child relation. */
|
|
137
|
+
function tableToDetailDocument(table, options = {}) {
|
|
138
|
+
const fields = (0, schema_1.selectFields)(table, { ...options, includeSystemFields: options.includeSystemFields ?? true });
|
|
139
|
+
const panel = {
|
|
140
|
+
type: 'DetailPanel',
|
|
141
|
+
key: `${table.name}.detail`,
|
|
142
|
+
props: {
|
|
143
|
+
...tableProps(table),
|
|
144
|
+
fields: fields.map((field) => columnNode(field, options.fields?.[field.name])),
|
|
145
|
+
...(primaryKeyFields(table).length > 0 ? { rowKey: primaryKeyFields(table) } : {}),
|
|
146
|
+
},
|
|
147
|
+
children: [],
|
|
148
|
+
};
|
|
149
|
+
const children = [panel];
|
|
150
|
+
if (options.relations !== false) {
|
|
151
|
+
const hasMany = table.relations?.hasMany ?? [];
|
|
152
|
+
for (const relation of hasMany) {
|
|
153
|
+
const related = relation?.referencedBy?.name;
|
|
154
|
+
if (!related)
|
|
155
|
+
continue;
|
|
156
|
+
children.push({
|
|
157
|
+
type: 'RelationList',
|
|
158
|
+
key: `${table.name}.${relation.fieldName ?? related}`,
|
|
159
|
+
props: {
|
|
160
|
+
label: (0, naming_1.titleize)(relation.fieldName ?? related),
|
|
161
|
+
table: related,
|
|
162
|
+
relation: 'hasMany',
|
|
163
|
+
...(relation.keys?.length
|
|
164
|
+
? { foreignKey: relation.keys.map((key) => key?.name).filter(Boolean) }
|
|
165
|
+
: {}),
|
|
166
|
+
},
|
|
167
|
+
children: [],
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
for (const relation of table.relations?.manyToMany ?? []) {
|
|
171
|
+
const related = relation?.rightTable?.name;
|
|
172
|
+
if (!related)
|
|
173
|
+
continue;
|
|
174
|
+
children.push({
|
|
175
|
+
type: 'RelationList',
|
|
176
|
+
key: `${table.name}.${relation.fieldName ?? related}`,
|
|
177
|
+
props: {
|
|
178
|
+
label: (0, naming_1.titleize)(relation.fieldName ?? related),
|
|
179
|
+
table: related,
|
|
180
|
+
relation: 'manyToMany',
|
|
181
|
+
...(relation.junctionTable?.name ? { junctionTable: relation.junctionTable.name } : {}),
|
|
182
|
+
},
|
|
183
|
+
children: [],
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return (0, blocks_schema_1.createDocument)({
|
|
188
|
+
type: 'Page',
|
|
189
|
+
key: options.rootKey ?? 'page',
|
|
190
|
+
props: { title: screenTitle(table, options) },
|
|
191
|
+
children,
|
|
192
|
+
}, {
|
|
193
|
+
id: documentId(table, options, 'detail'),
|
|
194
|
+
meta: { source: 'meta-to-blocks', table: table.name, kind: 'detail' },
|
|
195
|
+
});
|
|
196
|
+
}
|
package/esm/convert.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `_meta` → documents: the three screens a table implies.
|
|
3
|
+
*
|
|
4
|
+
* Forms lower through JSON Schema so `json-schema-to-blocks` picks the widgets;
|
|
5
|
+
* list and detail screens are block nodes (`DataTable`, `DetailPanel`,
|
|
6
|
+
* `RelationList`) whose props carry the table facts a renderer needs to fetch
|
|
7
|
+
* rows. Everything returned is a plain `UIDocument`: persist it, hand-edit it,
|
|
8
|
+
* or layer overrides on it with `composeDocument`.
|
|
9
|
+
*/
|
|
10
|
+
import { createDocument } from 'blocks-schema';
|
|
11
|
+
import { schemaToNodes } from 'json-schema-to-blocks';
|
|
12
|
+
import { selectFields, structuredConstraints, tableToSchema, typeToSchema } from './schema';
|
|
13
|
+
import { titleize } from './naming';
|
|
14
|
+
/** Columns too wide or too structural to sit in a table cell by default. */
|
|
15
|
+
const WIDE_FORMATS = new Set(['json', 'data-url']);
|
|
16
|
+
const DEFAULT_MAX_COLUMNS = 8;
|
|
17
|
+
function screenTitle(table, options) {
|
|
18
|
+
return options.title ?? titleize(table.name);
|
|
19
|
+
}
|
|
20
|
+
function documentId(table, options, kind) {
|
|
21
|
+
return options.id ?? `${table.name}-${kind}`;
|
|
22
|
+
}
|
|
23
|
+
/** Facts a renderer needs to query the table, carried on the block node. */
|
|
24
|
+
function tableProps(table) {
|
|
25
|
+
return {
|
|
26
|
+
table: table.name,
|
|
27
|
+
...(table.schemaName ? { schemaName: table.schemaName } : {}),
|
|
28
|
+
...(table.query?.all ? { query: table.query.all } : {}),
|
|
29
|
+
...(table.query?.one ? { queryOne: table.query.one } : {}),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function primaryKeyFields(table) {
|
|
33
|
+
const fromConstraints = structuredConstraints(table)?.primaryKey?.fields;
|
|
34
|
+
const fromLegacy = table.primaryKeyConstraints?.find((entry) => entry?.fields)?.fields;
|
|
35
|
+
const fields = fromConstraints ?? fromLegacy;
|
|
36
|
+
const names = (fields ?? []).map((field) => field?.name).filter((name) => Boolean(name));
|
|
37
|
+
if (names.length > 0)
|
|
38
|
+
return names;
|
|
39
|
+
return (table.fields ?? [])
|
|
40
|
+
.filter((field) => Boolean(field?.isPrimaryKey))
|
|
41
|
+
.map((field) => field.name);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The columns worth showing in a grid: narrow scalars first, capped, because a
|
|
45
|
+
* generated list with forty columns is unusable.
|
|
46
|
+
*/
|
|
47
|
+
function listColumns(table, options) {
|
|
48
|
+
const fields = selectFields(table, { ...options, includeSystemFields: options.includeSystemFields ?? true });
|
|
49
|
+
if (options.columns?.length) {
|
|
50
|
+
const wanted = new Map(fields.map((field) => [field.name, field]));
|
|
51
|
+
return options.columns.map((name) => wanted.get(name)).filter((field) => Boolean(field));
|
|
52
|
+
}
|
|
53
|
+
const narrow = fields.filter((field) => {
|
|
54
|
+
const schema = typeToSchema(field.type);
|
|
55
|
+
return !WIDE_FORMATS.has(String(schema.format ?? ''));
|
|
56
|
+
});
|
|
57
|
+
return narrow.slice(0, options.maxColumns ?? DEFAULT_MAX_COLUMNS);
|
|
58
|
+
}
|
|
59
|
+
function columnNode(field, override) {
|
|
60
|
+
const schema = typeToSchema(field.type);
|
|
61
|
+
return {
|
|
62
|
+
name: field.name,
|
|
63
|
+
label: override?.label ?? titleize(field.name),
|
|
64
|
+
type: schema.format ?? schema.type,
|
|
65
|
+
...(field.isPrimaryKey ? { primaryKey: true } : {}),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/** Form field nodes for a table, without a document envelope. */
|
|
69
|
+
export function tableToNodes(table, options = {}) {
|
|
70
|
+
// A create form omits database-maintained columns; an update form shows the
|
|
71
|
+
// primary key but does not let it be edited.
|
|
72
|
+
const includeSystemFields = options.includeSystemFields ?? options.mode === 'update';
|
|
73
|
+
const schema = tableToSchema(table, { ...options, includeSystemFields, id: documentId(table, options, 'form') });
|
|
74
|
+
const nodes = schemaToNodes(schema, {
|
|
75
|
+
rules: options.rules,
|
|
76
|
+
replaceDefaultRules: options.replaceDefaultRules,
|
|
77
|
+
});
|
|
78
|
+
if (options.mode !== 'update')
|
|
79
|
+
return nodes;
|
|
80
|
+
const keys = new Set(primaryKeyFields(table));
|
|
81
|
+
return nodes.map((node) => keys.has(String(node.props.name)) ? { ...node, props: { ...node.props, disabled: true } } : node);
|
|
82
|
+
}
|
|
83
|
+
/** Create/edit screen: `Page > Form > fields`. */
|
|
84
|
+
export function tableToFormDocument(table, options = {}) {
|
|
85
|
+
const fields = tableToNodes(table, options);
|
|
86
|
+
const form = {
|
|
87
|
+
type: 'Form',
|
|
88
|
+
key: 'form',
|
|
89
|
+
props: {
|
|
90
|
+
name: table.name,
|
|
91
|
+
...(options.submitLabel ? { submitLabel: options.submitLabel } : {}),
|
|
92
|
+
...(options.mode === 'update' ? { mode: 'update' } : {}),
|
|
93
|
+
},
|
|
94
|
+
children: fields,
|
|
95
|
+
};
|
|
96
|
+
return createDocument({
|
|
97
|
+
type: 'Page',
|
|
98
|
+
key: options.rootKey ?? 'page',
|
|
99
|
+
props: { title: screenTitle(table, options) },
|
|
100
|
+
children: options.form === false ? fields : [form],
|
|
101
|
+
}, {
|
|
102
|
+
id: documentId(table, options, 'form'),
|
|
103
|
+
meta: { source: 'meta-to-blocks', table: table.name, kind: 'form' },
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/** List screen: `Page > DataTable`. */
|
|
107
|
+
export function tableToListDocument(table, options = {}) {
|
|
108
|
+
const columns = listColumns(table, options).map((field) => columnNode(field, options.fields?.[field.name]));
|
|
109
|
+
return createDocument({
|
|
110
|
+
type: 'Page',
|
|
111
|
+
key: options.rootKey ?? 'page',
|
|
112
|
+
props: { title: screenTitle(table, options) },
|
|
113
|
+
children: [
|
|
114
|
+
{
|
|
115
|
+
type: 'DataTable',
|
|
116
|
+
key: `${table.name}.list`,
|
|
117
|
+
props: {
|
|
118
|
+
...tableProps(table),
|
|
119
|
+
columns,
|
|
120
|
+
...(primaryKeyFields(table).length > 0 ? { rowKey: primaryKeyFields(table) } : {}),
|
|
121
|
+
},
|
|
122
|
+
children: [],
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
}, {
|
|
126
|
+
id: documentId(table, options, 'list'),
|
|
127
|
+
meta: { source: 'meta-to-blocks', table: table.name, kind: 'list' },
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/** Detail screen: `Page > DetailPanel` plus a `RelationList` per child relation. */
|
|
131
|
+
export function tableToDetailDocument(table, options = {}) {
|
|
132
|
+
const fields = selectFields(table, { ...options, includeSystemFields: options.includeSystemFields ?? true });
|
|
133
|
+
const panel = {
|
|
134
|
+
type: 'DetailPanel',
|
|
135
|
+
key: `${table.name}.detail`,
|
|
136
|
+
props: {
|
|
137
|
+
...tableProps(table),
|
|
138
|
+
fields: fields.map((field) => columnNode(field, options.fields?.[field.name])),
|
|
139
|
+
...(primaryKeyFields(table).length > 0 ? { rowKey: primaryKeyFields(table) } : {}),
|
|
140
|
+
},
|
|
141
|
+
children: [],
|
|
142
|
+
};
|
|
143
|
+
const children = [panel];
|
|
144
|
+
if (options.relations !== false) {
|
|
145
|
+
const hasMany = table.relations?.hasMany ?? [];
|
|
146
|
+
for (const relation of hasMany) {
|
|
147
|
+
const related = relation?.referencedBy?.name;
|
|
148
|
+
if (!related)
|
|
149
|
+
continue;
|
|
150
|
+
children.push({
|
|
151
|
+
type: 'RelationList',
|
|
152
|
+
key: `${table.name}.${relation.fieldName ?? related}`,
|
|
153
|
+
props: {
|
|
154
|
+
label: titleize(relation.fieldName ?? related),
|
|
155
|
+
table: related,
|
|
156
|
+
relation: 'hasMany',
|
|
157
|
+
...(relation.keys?.length
|
|
158
|
+
? { foreignKey: relation.keys.map((key) => key?.name).filter(Boolean) }
|
|
159
|
+
: {}),
|
|
160
|
+
},
|
|
161
|
+
children: [],
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
for (const relation of table.relations?.manyToMany ?? []) {
|
|
165
|
+
const related = relation?.rightTable?.name;
|
|
166
|
+
if (!related)
|
|
167
|
+
continue;
|
|
168
|
+
children.push({
|
|
169
|
+
type: 'RelationList',
|
|
170
|
+
key: `${table.name}.${relation.fieldName ?? related}`,
|
|
171
|
+
props: {
|
|
172
|
+
label: titleize(relation.fieldName ?? related),
|
|
173
|
+
table: related,
|
|
174
|
+
relation: 'manyToMany',
|
|
175
|
+
...(relation.junctionTable?.name ? { junctionTable: relation.junctionTable.name } : {}),
|
|
176
|
+
},
|
|
177
|
+
children: [],
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return createDocument({
|
|
182
|
+
type: 'Page',
|
|
183
|
+
key: options.rootKey ?? 'page',
|
|
184
|
+
props: { title: screenTitle(table, options) },
|
|
185
|
+
children,
|
|
186
|
+
}, {
|
|
187
|
+
id: documentId(table, options, 'detail'),
|
|
188
|
+
meta: { source: 'meta-to-blocks', table: table.name, kind: 'detail' },
|
|
189
|
+
});
|
|
190
|
+
}
|
package/esm/index.js
ADDED
package/esm/naming.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Column and table names → human labels.
|
|
3
|
+
*
|
|
4
|
+
* Sentence case, not title case, and all-caps words are left alone so acronyms
|
|
5
|
+
* survive (`api_url` → `Api URL`). Trailing `_id`/`Id` is dropped because a
|
|
6
|
+
* foreign key renders as a picker over the referenced row, not as an id.
|
|
7
|
+
*/
|
|
8
|
+
export function titleize(name) {
|
|
9
|
+
if (!name)
|
|
10
|
+
return '';
|
|
11
|
+
const words = name
|
|
12
|
+
.replace(/[_-]+/g, ' ')
|
|
13
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
|
14
|
+
.trim()
|
|
15
|
+
.split(/\s+/)
|
|
16
|
+
.map((word) => (word === word.toUpperCase() ? word : word.toLowerCase()));
|
|
17
|
+
const trimmed = words.length > 1 && words[words.length - 1].toLowerCase() === 'id' ? words.slice(0, -1) : words;
|
|
18
|
+
return trimmed.join(' ').replace(/^./, (character) => character.toUpperCase());
|
|
19
|
+
}
|