fastify-auto-sqlapi 0.5.1 → 1.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/AGENTS.md +5 -5
- package/AGENTS_BACKEND.md +166 -130
- package/AGENTS_FRONTEND.md +60 -38
- package/BREAKING_CHANGES.md +9 -893
- package/CHANGELOG.md +246 -15
- package/README.md +71 -45
- package/dist/bin/generate-schema.js +12 -12
- package/dist/bin/generate-tables.js +1 -2
- package/dist/bin/utils.d.ts +2 -6
- package/dist/bin/utils.d.ts.map +1 -1
- package/dist/bin/utils.js +13 -33
- package/dist/index.d.ts +13 -33
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -37
- package/dist/lib/adapters/mysql-adapter.d.ts +5 -2
- package/dist/lib/adapters/mysql-adapter.d.ts.map +1 -1
- package/dist/lib/adapters/pg-adapter.d.ts +5 -2
- package/dist/lib/adapters/pg-adapter.d.ts.map +1 -1
- package/dist/lib/agent/manifest.d.ts +27 -10
- package/dist/lib/agent/manifest.d.ts.map +1 -1
- package/dist/lib/agent/manifest.js +43 -26
- package/dist/lib/agent/tool-schemas.d.ts +5 -4
- package/dist/lib/agent/tool-schemas.d.ts.map +1 -1
- package/dist/lib/agent/tool-schemas.js +5 -5
- package/dist/lib/cli/config.d.ts +37 -2
- package/dist/lib/cli/config.d.ts.map +1 -1
- package/dist/lib/cli/config.js +106 -16
- package/dist/lib/cli/mysql-introspect.d.ts +6 -1
- package/dist/lib/cli/mysql-introspect.d.ts.map +1 -1
- package/dist/lib/cli/mysql-introspect.js +15 -15
- package/dist/lib/cli/pg-introspect.d.ts +0 -1
- package/dist/lib/cli/pg-introspect.d.ts.map +1 -1
- package/dist/lib/cli/pg-introspect.js +2 -13
- package/dist/lib/cli/tables-codegen.d.ts +0 -4
- package/dist/lib/cli/tables-codegen.d.ts.map +1 -1
- package/dist/lib/cli/tables-codegen.js +24 -87
- package/dist/lib/db.d.ts +42 -17
- package/dist/lib/db.d.ts.map +1 -1
- package/dist/lib/db.js +114 -40
- package/dist/lib/dialect.d.ts +5 -0
- package/dist/lib/dialect.d.ts.map +1 -1
- package/dist/lib/dialect.js +3 -0
- package/dist/lib/engine/bulk/bulk-delete.d.ts +5 -0
- package/dist/lib/engine/bulk/bulk-delete.d.ts.map +1 -1
- package/dist/lib/engine/bulk/bulk-delete.js +66 -30
- package/dist/lib/engine/bulk/bulk-upsert.d.ts.map +1 -1
- package/dist/lib/engine/bulk/bulk-upsert.js +45 -26
- package/dist/lib/engine/read-hooks.d.ts +3 -3
- package/dist/lib/engine/read-hooks.d.ts.map +1 -1
- package/dist/lib/engine/read-hooks.js +3 -3
- package/dist/lib/engine/rest/delete.d.ts.map +1 -1
- package/dist/lib/engine/rest/delete.js +12 -18
- package/dist/lib/engine/rest/get.js +8 -8
- package/dist/lib/engine/rest/insert.d.ts.map +1 -1
- package/dist/lib/engine/rest/insert.js +31 -18
- package/dist/lib/engine/rest/update.d.ts.map +1 -1
- package/dist/lib/engine/rest/update.js +34 -17
- package/dist/lib/engine/search/aggregations.d.ts +3 -3
- package/dist/lib/engine/search/aggregations.d.ts.map +1 -1
- package/dist/lib/engine/search/aggregations.js +20 -16
- package/dist/lib/engine/search/conditions.d.ts +9 -5
- package/dist/lib/engine/search/conditions.d.ts.map +1 -1
- package/dist/lib/engine/search/conditions.js +31 -20
- package/dist/lib/engine/search/fields.d.ts +44 -11
- package/dist/lib/engine/search/fields.d.ts.map +1 -1
- package/dist/lib/engine/search/fields.js +93 -27
- package/dist/lib/engine/search/join-families.d.ts +7 -7
- package/dist/lib/engine/search/join-families.d.ts.map +1 -1
- package/dist/lib/engine/search/join-families.js +84 -75
- package/dist/lib/engine/search/joins.d.ts +15 -7
- package/dist/lib/engine/search/joins.d.ts.map +1 -1
- package/dist/lib/engine/search/joins.js +23 -13
- package/dist/lib/engine/search/order-by.d.ts +11 -12
- package/dist/lib/engine/search/order-by.d.ts.map +1 -1
- package/dist/lib/engine/search/order-by.js +24 -28
- package/dist/lib/engine/search/search.d.ts.map +1 -1
- package/dist/lib/engine/search/search.js +111 -64
- package/dist/lib/engine/validate.d.ts +21 -3
- package/dist/lib/engine/validate.d.ts.map +1 -1
- package/dist/lib/engine/validate.js +106 -16
- package/dist/lib/engine/write-helpers.d.ts +69 -16
- package/dist/lib/engine/write-helpers.d.ts.map +1 -1
- package/dist/lib/engine/write-helpers.js +257 -105
- package/dist/lib/errors.d.ts +42 -10
- package/dist/lib/errors.d.ts.map +1 -1
- package/dist/lib/errors.js +69 -15
- package/dist/lib/nullable.d.ts +3 -2
- package/dist/lib/nullable.d.ts.map +1 -1
- package/dist/lib/nullable.js +14 -5
- package/dist/lib/read-access.d.ts +25 -16
- package/dist/lib/read-access.d.ts.map +1 -1
- package/dist/lib/read-access.js +33 -17
- package/dist/lib/schema/bulk-delete.d.ts +13 -7
- package/dist/lib/schema/bulk-delete.d.ts.map +1 -1
- package/dist/lib/schema/bulk-delete.js +18 -8
- package/dist/lib/schema/bulk-upsert.d.ts +3 -3
- package/dist/lib/schema/bulk-upsert.d.ts.map +1 -1
- package/dist/lib/schema/bulk-upsert.js +9 -14
- package/dist/lib/schema/get.d.ts +1 -1
- package/dist/lib/schema/get.d.ts.map +1 -1
- package/dist/lib/schema/get.js +2 -2
- package/dist/lib/schema/helpers.d.ts +41 -23
- package/dist/lib/schema/helpers.d.ts.map +1 -1
- package/dist/lib/schema/helpers.js +80 -33
- package/dist/lib/schema/insert.d.ts +1 -1
- package/dist/lib/schema/insert.d.ts.map +1 -1
- package/dist/lib/schema/insert.js +12 -20
- package/dist/lib/schema/search.d.ts +2 -13
- package/dist/lib/schema/search.d.ts.map +1 -1
- package/dist/lib/schema/search.js +64 -61
- package/dist/lib/schema/update.d.ts +1 -1
- package/dist/lib/schema/update.d.ts.map +1 -1
- package/dist/lib/schema/update.js +6 -13
- package/dist/lib/sql-api.d.ts +47 -25
- package/dist/lib/sql-api.d.ts.map +1 -1
- package/dist/lib/sql-api.js +70 -27
- package/dist/lib/table-helpers.d.ts +92 -14
- package/dist/lib/table-helpers.d.ts.map +1 -1
- package/dist/lib/table-helpers.js +367 -128
- package/dist/lib/tenant.d.ts +16 -22
- package/dist/lib/tenant.d.ts.map +1 -1
- package/dist/lib/tenant.js +38 -54
- package/dist/lib/write-access.d.ts +15 -9
- package/dist/lib/write-access.d.ts.map +1 -1
- package/dist/lib/write-access.js +12 -4
- package/dist/routes/auto/agent-manifest.routes.d.ts +1 -1
- package/dist/routes/auto/agent-manifest.routes.js +2 -2
- package/dist/routes/auto/bulk-delete.routes.d.ts +2 -2
- package/dist/routes/auto/bulk-delete.routes.d.ts.map +1 -1
- package/dist/routes/auto/bulk-delete.routes.js +1 -7
- package/dist/routes/auto/bulk-upsert.routes.d.ts +2 -2
- package/dist/routes/auto/bulk-upsert.routes.d.ts.map +1 -1
- package/dist/routes/auto/bulk-upsert.routes.js +2 -2
- package/dist/routes/auto/delete.routes.d.ts +2 -2
- package/dist/routes/auto/delete.routes.d.ts.map +1 -1
- package/dist/routes/auto/delete.routes.js +8 -10
- package/dist/routes/auto/get.routes.d.ts +2 -2
- package/dist/routes/auto/get.routes.d.ts.map +1 -1
- package/dist/routes/auto/get.routes.js +5 -5
- package/dist/routes/auto/insert.routes.d.ts +2 -2
- package/dist/routes/auto/insert.routes.d.ts.map +1 -1
- package/dist/routes/auto/insert.routes.js +1 -7
- package/dist/routes/auto/plugin.d.ts +6 -0
- package/dist/routes/auto/plugin.d.ts.map +1 -1
- package/dist/routes/auto/plugin.js +60 -36
- package/dist/routes/auto/route-helpers.d.ts +26 -12
- package/dist/routes/auto/route-helpers.d.ts.map +1 -1
- package/dist/routes/auto/route-helpers.js +50 -19
- package/dist/routes/auto/search.routes.d.ts +2 -2
- package/dist/routes/auto/search.routes.d.ts.map +1 -1
- package/dist/routes/auto/search.routes.js +36 -24
- package/dist/routes/auto/update.routes.d.ts +2 -2
- package/dist/routes/auto/update.routes.d.ts.map +1 -1
- package/dist/routes/auto/update.routes.js +1 -8
- package/dist/routes/sql-api-decorator.d.ts +30 -12
- package/dist/routes/sql-api-decorator.d.ts.map +1 -1
- package/dist/routes/sql-api-decorator.js +44 -27
- package/dist/types/cli.d.ts +17 -6
- package/dist/types/cli.d.ts.map +1 -1
- package/dist/types/computed.d.ts +1 -1
- package/dist/types/computed.d.ts.map +1 -1
- package/dist/types/conditions.d.ts +3 -1
- package/dist/types/conditions.d.ts.map +1 -1
- package/dist/types/db.d.ts +44 -4
- package/dist/types/db.d.ts.map +1 -1
- package/dist/types/dml.d.ts +26 -17
- package/dist/types/dml.d.ts.map +1 -1
- package/dist/types/hooks.d.ts +62 -0
- package/dist/types/hooks.d.ts.map +1 -0
- package/dist/types/hooks.js +1 -0
- package/dist/types/join.d.ts +25 -8
- package/dist/types/join.d.ts.map +1 -1
- package/dist/types/plugin.d.ts +58 -19
- package/dist/types/plugin.d.ts.map +1 -1
- package/dist/types/plugin.js +2 -0
- package/dist/types/request.d.ts +2 -2
- package/dist/types/schema.d.ts +3 -3
- package/dist/types/schema.d.ts.map +1 -1
- package/dist/types/search.d.ts +28 -13
- package/dist/types/search.d.ts.map +1 -1
- package/dist/types/search.js +2 -1
- package/dist/types/table.d.ts +109 -94
- package/dist/types/table.d.ts.map +1 -1
- package/dist/types/table.js +7 -2
- package/dist/types/tenant.d.ts +28 -2
- package/dist/types/tenant.d.ts.map +1 -1
- package/dist/types/tenant.js +7 -1
- package/dist/types/validation.d.ts +69 -13
- package/dist/types/validation.d.ts.map +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/docs/GLOSSARY.md +11 -5
- package/docs/adr/0003-bulk-not-transactional.md +2 -1
- package/docs/adr/0006-raw-db-errors.md +2 -1
- package/docs/adr/0013-sanitized-db-errors.md +2 -1
- package/docs/adr/0014-what-follows-a-write-join.md +1 -1
- package/docs/adr/0022-stay-on-0x-while-every-consumer-is-first-party.md +88 -0
- package/docs/adr/0023-a-write-runs-in-a-transaction-or-not-at-all.md +60 -0
- package/docs/adr/0024-the-consumer-maps-driver-codes-through-dberrors.md +78 -0
- package/docs/adr/0025-validatebulk-replaces-validate-in-a-bulk-upsert.md +46 -0
- package/docs/adr/0026-update-carries-its-primary-key-in-main.md +43 -0
- package/docs/adr/0027-the-1-0-contract.md +74 -0
- package/docs/adr/0028-select-star-is-the-default-projection.md +64 -0
- package/docs/adr/0029-a-write-names-its-child-rows-by-action.md +96 -0
- package/docs/adr/0030-a-child-row-is-validated-by-its-own-table.md +117 -0
- package/docs/adr/0031-an-insert-excluded-upsert-key-addresses-an-existing-row.md +58 -0
- package/docs/adr/0032-tenant-scope-names-schema-fields.md +56 -0
- package/docs/adr/README.md +12 -1
- package/docs/upgrading/0.x.md +906 -0
- package/docs/upgrading/1.0.md +628 -0
- package/package.json +4 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAc,KAAK,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,SAAS,CAAC;AAEvE,OAAO,KAAK,EAAE,WAAW,EAAE,gBAAgB,EAAE,QAAQ,EAAkB,MAAM,gBAAgB,CAAC;AAM9F;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,SAAS,EAAE,WAAW,GAAG,SAAS,EAAE,MAAM,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAQ3I;AAED,4FAA4F;AAC5F,eAAO,MAAM,iBAAiB,oEAAkE,CAAC;AAEjG;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CAAC,SAAS,EAAE,WAAW,GAAG,OAAO,CAGxD;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,SAAS,EAAE,WAAW,GAAG,SAAS,EAClC,MAAM,EAAE,gBAAgB,GACvB,IAAI,CAMN;AAkCD,kFAAkF;AAClF,wBAAgB,UAAU,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAGnD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,SAAS,EAAE,WAAW,GAAG,SAAS,GACjC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CASzB;AAuCD;;;;;;;;;;GAUG;AACH,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,SAAS,EAAE,WAAW,EACtB,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE;IAAE,UAAU,EAAE,OAAO,CAAA;CAAE,GAC/B,IAAI,CAcN;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,SAAS,EAAE,WAAW,EACtB,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE;IAAE,SAAS,EAAE,KAAK,GAAG,UAAU,CAAA;CAAE,GACzC,IAAI,CASN;AAaD,4DAA4D;AAC5D,wBAAgB,cAAc,CAC5B,MAAM,EAAE,gBAAgB,EACxB,SAAS,EAAE,WAAW,GAAG,SAAS,GACjC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAOzB"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { Type,
|
|
1
|
+
import { Type, IsOptional } from 'typebox';
|
|
2
|
+
import { primaryAsString } from '../../types.js';
|
|
2
3
|
import { readableFieldNames } from '../read-access.js';
|
|
3
|
-
import { writableFields } from '../write-access.js';
|
|
4
|
+
import { writableFields, insertExcludedFields } from '../write-access.js';
|
|
4
5
|
import { tenantOwnColumns } from '../tenant.js';
|
|
5
|
-
import {
|
|
6
|
+
import { resolveJoinTableConf, resolvedTenantScope } from '../table-helpers.js';
|
|
6
7
|
/**
|
|
7
8
|
* Build a PK-only schema for response shapes (single or composite PK).
|
|
8
9
|
* Falls back to `fallback` field if tableConf is undefined.
|
|
@@ -16,14 +17,32 @@ export function pkSchema(tableConf, schema, fallback) {
|
|
|
16
17
|
}
|
|
17
18
|
return result;
|
|
18
19
|
}
|
|
20
|
+
/** `action` of a write response: what the statement did to the main row (`WriteAction`). */
|
|
21
|
+
export const WriteActionSchema = Type.Union([Type.Literal('inserted'), Type.Literal('updated')]);
|
|
22
|
+
/**
|
|
23
|
+
* Path params of the single-record routes, `GET|DELETE /rest/:table/:id`.
|
|
24
|
+
*
|
|
25
|
+
* `:id` is validated against the primary key field's own type rather than as free text: a
|
|
26
|
+
* path parameter is a string on the wire, and Ajv's coercion turns `"7"` into the number an
|
|
27
|
+
* integer PK expects while `"abc"` becomes a structured 400 on `id` instead of a driver
|
|
28
|
+
* error the route can only report as a 500. The PK is Optional in a generated Schema when the
|
|
29
|
+
* database fills it, so the modifier is stripped here: the path always carries the value.
|
|
30
|
+
*
|
|
31
|
+
* The parameter is named `id` whatever the PK field is called; the response names the real
|
|
32
|
+
* field (see `pkSchema`).
|
|
33
|
+
*/
|
|
34
|
+
export function pkParams(tableConf) {
|
|
35
|
+
const pkField = primaryAsString(tableConf.primary);
|
|
36
|
+
return Type.Object({ id: asRequired(tableConf.schema.fields[pkField] ?? Type.String()) });
|
|
37
|
+
}
|
|
19
38
|
/**
|
|
20
39
|
* Make the tenant column(s) of a scoped table Optional in a write body. The engine injects the
|
|
21
40
|
* value for a single-tenant caller and validates the one a multi-tenant or `anyOf` caller sends,
|
|
22
41
|
* so the body must offer the field without demanding it — the same reason a secondary's FK is
|
|
23
|
-
* Optional.
|
|
42
|
+
* Optional. The scope is resolved to its columns, and each field matched through `col()`.
|
|
24
43
|
*/
|
|
25
44
|
export function relaxTenantColumns(fields, tableConf, schema) {
|
|
26
|
-
const columns = tenantOwnColumns(tableConf
|
|
45
|
+
const columns = tenantOwnColumns(resolvedTenantScope(tableConf));
|
|
27
46
|
if (columns.length === 0)
|
|
28
47
|
return;
|
|
29
48
|
for (const field of Object.keys(fields)) {
|
|
@@ -34,7 +53,7 @@ export function relaxTenantColumns(fields, tableConf, schema) {
|
|
|
34
53
|
/**
|
|
35
54
|
* Build secondary table fields for body schemas:
|
|
36
55
|
* - FK field becomes Optional (auto-filled from main)
|
|
37
|
-
* -
|
|
56
|
+
* - insertExclude fields become Optional
|
|
38
57
|
* - tenant column(s) become Optional (injected or validated by the engine)
|
|
39
58
|
*/
|
|
40
59
|
function buildSecondaryFields(joinSchema, joinField, secondaryTableConf) {
|
|
@@ -42,11 +61,9 @@ function buildSecondaryFields(joinSchema, joinField, secondaryTableConf) {
|
|
|
42
61
|
if (joinField in fields) {
|
|
43
62
|
fields[joinField] = Type.Optional(fields[joinField]);
|
|
44
63
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
fields[field] = Type.Optional(fields[field]);
|
|
49
|
-
}
|
|
64
|
+
for (const field of insertExcludedFields(secondaryTableConf)) {
|
|
65
|
+
if (field in fields) {
|
|
66
|
+
fields[field] = Type.Optional(fields[field]);
|
|
50
67
|
}
|
|
51
68
|
}
|
|
52
69
|
relaxTenantColumns(fields, secondaryTableConf, joinSchema);
|
|
@@ -54,7 +71,7 @@ function buildSecondaryFields(joinSchema, joinField, secondaryTableConf) {
|
|
|
54
71
|
}
|
|
55
72
|
/** The same schema without the Optional modifier, i.e. as a required property. */
|
|
56
73
|
export function asRequired(schema) {
|
|
57
|
-
if (!(
|
|
74
|
+
if (!IsOptional(schema))
|
|
58
75
|
return schema;
|
|
59
76
|
return Type.Required(Type.Object({ value: schema })).properties.value;
|
|
60
77
|
}
|
|
@@ -94,40 +111,70 @@ export function applySchemaOverrides(fields, tableConf) {
|
|
|
94
111
|
*/
|
|
95
112
|
function buildJoinAliasMap(tableConf, dbTables, build, options = {}) {
|
|
96
113
|
const out = {};
|
|
97
|
-
if (!tableConf.
|
|
114
|
+
if (!tableConf.writeJoins?.length)
|
|
98
115
|
return out;
|
|
99
116
|
const objOpts = options.strictItems ? { additionalProperties: false } : {};
|
|
100
|
-
for (const j of tableConf.
|
|
101
|
-
|
|
117
|
+
for (const j of tableConf.writeJoins) {
|
|
118
|
+
if (options.include && !options.include(j))
|
|
119
|
+
continue;
|
|
120
|
+
const sc = resolveJoinTableConf(dbTables, j);
|
|
102
121
|
const item = Type.Object(build(j, sc), objOpts);
|
|
103
|
-
|
|
122
|
+
// Partial rebuilds the object and drops the source's options: hand them over again.
|
|
123
|
+
out[j.alias] = Type.Array(options.itemsPartial ? Type.Partial(item, objOpts) : item);
|
|
104
124
|
}
|
|
105
125
|
return out;
|
|
106
126
|
}
|
|
127
|
+
/** An alias map as an Optional, open object: see `attachRequestSections`. */
|
|
128
|
+
function aliasSection(map) {
|
|
129
|
+
return Type.Optional(Type.Partial(Type.Object(map)));
|
|
130
|
+
}
|
|
131
|
+
/** A relation `replace` can address: one with an `upsertKey` to tell its rows apart (ADR 0029). */
|
|
132
|
+
const replaceable = (join) => join.upsertKey !== undefined;
|
|
107
133
|
/**
|
|
108
|
-
* Attach
|
|
109
|
-
*
|
|
110
|
-
*
|
|
134
|
+
* Attach the child-row sections of a write request to a body schema, in place: `upsert` for
|
|
135
|
+
* every write join, `delete` (update and bulk upsert, `withDelete`) with every field Optional —
|
|
136
|
+
* the engine fills in the FK, the client names the key — and `replace` for the relations with
|
|
137
|
+
* an `upsertKey`, omitted when none has one.
|
|
111
138
|
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
139
|
+
* Every alias container stays open: an alias the table does not declare, or one `replace`
|
|
140
|
+
* cannot address, must reach the engine, which refuses it with a 400 at that alias
|
|
141
|
+
* (`assertWriteJoinAliases`). Closed, Fastify's default Ajv (`removeAdditional`) would strip
|
|
142
|
+
* it, and the write would answer success with the child rows unwritten.
|
|
115
143
|
*/
|
|
116
|
-
export function
|
|
117
|
-
if (!tableConf.
|
|
144
|
+
export function attachRequestSections(target, tableConf, dbTables, options) {
|
|
145
|
+
if (!tableConf.writeJoins?.length)
|
|
118
146
|
return;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
147
|
+
const strict = { strictItems: true };
|
|
148
|
+
target.upsert = aliasSection(buildJoinAliasMap(tableConf, dbTables, writeJoinBodyFields, strict));
|
|
149
|
+
if (options.withDelete) {
|
|
150
|
+
target.delete = aliasSection(buildJoinAliasMap(tableConf, dbTables, ({ joinSchema }) => joinSchema.fields, { itemsPartial: true }));
|
|
151
|
+
}
|
|
152
|
+
if (tableConf.writeJoins.some(replaceable)) {
|
|
153
|
+
target.replace = aliasSection(buildJoinAliasMap(tableConf, dbTables, writeJoinBodyFields, { ...strict, include: replaceable }));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Attach the outcome sections of a write response, in place: `secondaries`, the key of every
|
|
158
|
+
* child row written, and `deletions`, the child rows removed. `deletions` lists every write
|
|
159
|
+
* join (`'all'`, update and bulk upsert) or only those a `replace` can address (`'replaced'`,
|
|
160
|
+
* insert — the only removals an insert makes), and is omitted when none qualifies.
|
|
161
|
+
*/
|
|
162
|
+
export function attachOutcomeSections(target, tableConf, dbTables, options) {
|
|
163
|
+
if (!tableConf.writeJoins?.length)
|
|
164
|
+
return;
|
|
165
|
+
target.secondaries = aliasSection(buildJoinAliasMap(tableConf, dbTables, writeJoinResponseFields));
|
|
166
|
+
const include = options.deletions === 'all' ? undefined : replaceable;
|
|
167
|
+
if (!include || tableConf.writeJoins.some(include)) {
|
|
168
|
+
target.deletions = aliasSection(buildJoinAliasMap(tableConf, dbTables, ({ joinSchema }) => joinSchema.fields, { itemsPartial: true, include }));
|
|
125
169
|
}
|
|
126
170
|
}
|
|
127
171
|
/** JoinFieldsBuilder for body schemas: includes all join fields with FK + excluded made Optional. */
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
|
|
172
|
+
const writeJoinBodyFields = (j, sc) => buildSecondaryFields(j.joinSchema, j.joinField, sc);
|
|
173
|
+
/**
|
|
174
|
+
* JoinFieldsBuilder for response schemas: the child's primary key — its table config's, else
|
|
175
|
+
* its Schema's, else the join field, as the engine reports it.
|
|
176
|
+
*/
|
|
177
|
+
const writeJoinResponseFields = (j, sc) => pkSchema(sc, j.joinSchema, j.joinSchema.primaryKey ?? j.joinField);
|
|
131
178
|
/** Schema fields minus the ones hidden by `readExclude`. */
|
|
132
179
|
export function readableFields(schema, tableConf) {
|
|
133
180
|
if (!tableConf?.readExclude?.length)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type TObject } from '
|
|
1
|
+
import { type TObject } from 'typebox';
|
|
2
2
|
import type { DbTables } from '../../types.js';
|
|
3
3
|
export declare function InsertTableBody(dbTables: DbTables, tableName: string): TObject;
|
|
4
4
|
export declare function InsertTableResponse(dbTables: DbTables, tableName: string): TObject;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"insert.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/insert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,OAAO,EAAgB,MAAM,
|
|
1
|
+
{"version":3,"file":"insert.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/insert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,OAAO,EAAgB,MAAM,SAAS,CAAC;AAE3D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAW/C,wBAAgB,eAAe,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CA2B9E;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAWlF"}
|
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
import { Type } from '
|
|
1
|
+
import { Type } from 'typebox';
|
|
2
2
|
import { primaryAsString } from '../../types.js';
|
|
3
|
-
import { writableFields } from '../write-access.js';
|
|
4
|
-
import { pkSchema, applySchemaOverrides,
|
|
3
|
+
import { writableFields, insertExcludedFields } from '../write-access.js';
|
|
4
|
+
import { pkSchema, applySchemaOverrides, attachRequestSections, attachOutcomeSections, relaxTenantColumns, WriteActionSchema, } from './helpers.js';
|
|
5
5
|
export function InsertTableBody(dbTables, tableName) {
|
|
6
6
|
const tableConf = dbTables[tableName];
|
|
7
|
-
const schema = tableConf.
|
|
8
|
-
// Main: full validation, but
|
|
7
|
+
const schema = tableConf.schema;
|
|
8
|
+
// Main: full validation, but insertExclude fields become Optional. Fields the
|
|
9
9
|
// database computes are not offered at all — sending one is rejected here (400) rather
|
|
10
10
|
// than by the driver (500).
|
|
11
11
|
const mainFields = writableFields(applySchemaOverrides({ ...schema.fields }, tableConf), tableConf, schema);
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
mainFields[field] = Type.Optional(mainFields[field]);
|
|
16
|
-
}
|
|
12
|
+
for (const field of insertExcludedFields(tableConf)) {
|
|
13
|
+
if (field in mainFields)
|
|
14
|
+
mainFields[field] = Type.Optional(mainFields[field]);
|
|
17
15
|
}
|
|
18
16
|
// The tenant column is the engine's to fill or check, never the client's to be forced to send.
|
|
19
17
|
relaxTenantColumns(mainFields, tableConf, schema);
|
|
@@ -22,21 +20,15 @@ export function InsertTableBody(dbTables, tableName) {
|
|
|
22
20
|
// rejected (400) instead of silently reaching the INSERT as columns (mass assignment).
|
|
23
21
|
main: Type.Object(mainFields, { additionalProperties: false }),
|
|
24
22
|
};
|
|
25
|
-
|
|
26
|
-
withDeletions: false,
|
|
27
|
-
secondaryFields: writeJoinBodyFields,
|
|
28
|
-
strictItems: true,
|
|
29
|
-
});
|
|
23
|
+
attachRequestSections(bodyProperties, tableConf, dbTables, { withDelete: false });
|
|
30
24
|
return Type.Object(bodyProperties);
|
|
31
25
|
}
|
|
32
26
|
export function InsertTableResponse(dbTables, tableName) {
|
|
33
27
|
const tableConf = dbTables[tableName];
|
|
34
28
|
const responseProperties = {
|
|
35
|
-
main: Type.Object(pkSchema(tableConf, tableConf.
|
|
29
|
+
main: Type.Object(pkSchema(tableConf, tableConf.schema, primaryAsString(tableConf.primary))),
|
|
30
|
+
action: WriteActionSchema,
|
|
36
31
|
};
|
|
37
|
-
|
|
38
|
-
withDeletions: false,
|
|
39
|
-
secondaryFields: writeJoinResponseFields,
|
|
40
|
-
});
|
|
32
|
+
attachOutcomeSections(responseProperties, tableConf, dbTables, { deletions: 'replaced' });
|
|
41
33
|
return Type.Object(responseProperties);
|
|
42
34
|
}
|
|
@@ -1,16 +1,5 @@
|
|
|
1
|
-
import { type TObject } from '
|
|
1
|
+
import { type TObject } from 'typebox';
|
|
2
2
|
import type { DbTables } from '../../types.js';
|
|
3
|
-
export declare function SearchTableBodyPost(dbTables: DbTables, tableName: string): TObject;
|
|
4
|
-
/**
|
|
5
|
-
* Querystring schema, parameterized on the plugin's `maxItemsPerPage`.
|
|
6
|
-
*
|
|
7
|
-
* The `itemsPerPage` default MUST NOT exceed the cap: Ajv injects the default before any
|
|
8
|
-
* runtime check runs, so a fixed default of 500 combined with `maxItemsPerPage: 100` would
|
|
9
|
-
* make every request without an explicit `itemsPerPage` fail with 400. The default is
|
|
10
|
-
* therefore `min(500, cap)` and the cap doubles as the schema `maximum` (structured 400).
|
|
11
|
-
*/
|
|
12
|
-
export declare function SearchTableQuery(maxItemsPerPage?: number): TObject;
|
|
13
|
-
/** Default-cap variant, kept for granular composition and backward compatibility. */
|
|
14
|
-
export declare const SearchTableQueryString: TObject<import("@sinclair/typebox").TProperties>;
|
|
3
|
+
export declare function SearchTableBodyPost(dbTables: DbTables, tableName: string, maxItemsPerPage?: number): TObject;
|
|
15
4
|
export declare function SearchTableResponse(dbTables: DbTables, tableName: string): TObject;
|
|
16
5
|
//# sourceMappingURL=search.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/search.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,OAAO,EAAgB,MAAM,
|
|
1
|
+
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/search.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,OAAO,EAAgB,MAAM,SAAS,CAAC;AAM3D,OAAO,KAAK,EAAE,QAAQ,EAAe,MAAM,gBAAgB,CAAC;AA4K5D,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,QAAQ,EAClB,SAAS,EAAE,MAAM,EACjB,eAAe,GAAE,MAAmC,GACnD,OAAO,CAgDT;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CA6ClF"}
|
|
@@ -1,7 +1,21 @@
|
|
|
1
|
-
import { Type } from '
|
|
1
|
+
import { Type } from 'typebox';
|
|
2
2
|
import { ALLOWED_METHODS, MAX_CONDITIONS, MAX_ORDER_BY_LENGTH } from '../condition-methods.js';
|
|
3
3
|
import { readableFields } from './helpers.js';
|
|
4
|
-
import {
|
|
4
|
+
import { resolveJoinTableConf } from '../table-helpers.js';
|
|
5
|
+
import { extraFilterFields } from '../read-access.js';
|
|
6
|
+
import { DEFAULT_MAX_ITEMS_PER_PAGE, COMPUTE_FNS } from '../../types.js';
|
|
7
|
+
// ─── orderBy / paginator / compute ──
|
|
8
|
+
const orderByField = () => Type.Optional(Type.String({ maxLength: MAX_ORDER_BY_LENGTH }));
|
|
9
|
+
const pageField = () => Type.Optional(Type.Integer({ minimum: 1 }));
|
|
10
|
+
/**
|
|
11
|
+
* No schema default on purpose: Ajv injects defaults before the handler runs, and a request
|
|
12
|
+
* without a paginator must stay observable as such (LIMIT `maxItemsPerPage`, no COUNT, no
|
|
13
|
+
* `pagination` block). The 500-row default for `page` alone is applied by the route. The cap
|
|
14
|
+
* doubles as the schema `maximum`, so exceeding it is a structured 400.
|
|
15
|
+
*/
|
|
16
|
+
const itemsPerPageField = (maxItemsPerPage) => Type.Optional(Type.Integer({ minimum: 1, maximum: maxItemsPerPage }));
|
|
17
|
+
/** `{ min?: string[], max?: string[], sum?: string[], avg?: string[] }`. */
|
|
18
|
+
const computeObject = () => Type.Object(Object.fromEntries(COMPUTE_FNS.map((fn) => [fn, Type.Optional(Type.Array(Type.String()))])));
|
|
5
19
|
/**
|
|
6
20
|
* A `filters` map.
|
|
7
21
|
*
|
|
@@ -39,6 +53,8 @@ function computedFieldTypes(tableConf) {
|
|
|
39
53
|
}
|
|
40
54
|
return out;
|
|
41
55
|
}
|
|
56
|
+
/** A projection: field names, validated by the engine (see the main `select`). */
|
|
57
|
+
const SelectSchema = Type.Array(Type.String());
|
|
42
58
|
function buildJoinBodyProps(dbTables, tableConf, conditionItemSchema) {
|
|
43
59
|
const props = {
|
|
44
60
|
joinMustExist: {},
|
|
@@ -46,9 +62,9 @@ function buildJoinBodyProps(dbTables, tableConf, conditionItemSchema) {
|
|
|
46
62
|
joinGroup: {},
|
|
47
63
|
joinLeft: {},
|
|
48
64
|
};
|
|
49
|
-
for (const joinDef of tableConf.
|
|
65
|
+
for (const joinDef of tableConf.readJoins ?? []) {
|
|
50
66
|
const { joinSchema, alias, unique } = joinDef;
|
|
51
|
-
const joinTableConf = dbTables
|
|
67
|
+
const joinTableConf = resolveJoinTableConf(dbTables, joinDef);
|
|
52
68
|
const joinSchemaFields = joinTableConf
|
|
53
69
|
? { ...readableFields(joinSchema, joinTableConf), ...computedFieldTypes(joinTableConf) }
|
|
54
70
|
: { ...joinSchema.fields };
|
|
@@ -56,7 +72,7 @@ function buildJoinBodyProps(dbTables, tableConf, conditionItemSchema) {
|
|
|
56
72
|
// own `filters()` — buildJoinRefCondition, i.e. the unique:false paths. joinLeft
|
|
57
73
|
// builds its condition inline (buildLeftJoinClauses) and handles schema and computed
|
|
58
74
|
// fields only, so advertising extraFilters there would promise a silent no-op.
|
|
59
|
-
const joinFilterFields = { ...joinSchemaFields, ...(joinTableConf
|
|
75
|
+
const joinFilterFields = { ...joinSchemaFields, ...extraFilterFields(joinTableConf) };
|
|
60
76
|
const joinRefShape = {
|
|
61
77
|
filters: Type.Optional(filtersObject(joinFilterFields)),
|
|
62
78
|
conditions: Type.Optional(Type.Array(conditionItemSchema, { maxItems: MAX_CONDITIONS })),
|
|
@@ -65,25 +81,23 @@ function buildJoinBodyProps(dbTables, tableConf, conditionItemSchema) {
|
|
|
65
81
|
props.joinLeft[alias] = Type.Object({
|
|
66
82
|
filters: Type.Optional(filtersObject(joinSchemaFields)),
|
|
67
83
|
conditions: Type.Optional(Type.Array(conditionItemSchema, { maxItems: MAX_CONDITIONS })),
|
|
68
|
-
|
|
84
|
+
select: Type.Optional(SelectSchema),
|
|
69
85
|
});
|
|
70
86
|
continue;
|
|
71
87
|
}
|
|
72
88
|
props.joinMustExist[alias] = Type.Object(joinRefShape);
|
|
73
89
|
props.joinMultiple[alias] = Type.Object({
|
|
74
90
|
...joinRefShape,
|
|
75
|
-
|
|
91
|
+
select: Type.Optional(SelectSchema),
|
|
76
92
|
});
|
|
77
93
|
props.joinGroup[alias] = Type.Object({
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
count: Type.Optional(Type.Array(Type.String())),
|
|
86
|
-
}),
|
|
94
|
+
by: Type.Optional(Type.String()),
|
|
95
|
+
distinctCount: Type.Optional(Type.Array(Type.String())),
|
|
96
|
+
min: Type.Optional(Type.Array(Type.String())),
|
|
97
|
+
max: Type.Optional(Type.Array(Type.String())),
|
|
98
|
+
sum: Type.Optional(Type.Array(Type.String())),
|
|
99
|
+
avg: Type.Optional(Type.Array(Type.String())),
|
|
100
|
+
count: Type.Optional(Type.Array(Type.String())),
|
|
87
101
|
filters: Type.Optional(filtersObject(joinFilterFields)),
|
|
88
102
|
conditions: Type.Optional(Type.Array(conditionItemSchema, { maxItems: MAX_CONDITIONS })),
|
|
89
103
|
});
|
|
@@ -95,9 +109,9 @@ function buildJoinResponseProps(dbTables, tableConf) {
|
|
|
95
109
|
const joinMultipleProps = {};
|
|
96
110
|
const joinLeftProps = {};
|
|
97
111
|
const joinGroupProps = {};
|
|
98
|
-
for (const joinDef of tableConf.
|
|
112
|
+
for (const joinDef of tableConf.readJoins ?? []) {
|
|
99
113
|
const { joinSchema, alias, unique } = joinDef;
|
|
100
|
-
const itemArray = Type.Array(Type.Partial(Type.Object(readableFields(joinSchema, dbTables
|
|
114
|
+
const itemArray = Type.Array(Type.Partial(Type.Object(readableFields(joinSchema, resolveJoinTableConf(dbTables, joinDef)))));
|
|
101
115
|
if (unique) {
|
|
102
116
|
joinLeftProps[alias] = itemArray;
|
|
103
117
|
}
|
|
@@ -108,18 +122,25 @@ function buildJoinResponseProps(dbTables, tableConf) {
|
|
|
108
122
|
}
|
|
109
123
|
return { joinMultipleProps, joinLeftProps, joinGroupProps };
|
|
110
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* One joinGroup alias in the response: always `rows`, one per group (a single row without
|
|
127
|
+
* `by`), each carrying `by` when grouped and the aggregates nested by function, as in the
|
|
128
|
+
* dotted notation `alias.fn.field`.
|
|
129
|
+
*/
|
|
111
130
|
const JoinGroupResultItem = Type.Object({
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
131
|
+
rows: Type.Array(Type.Object({
|
|
132
|
+
by: Type.Optional(Type.Any()),
|
|
133
|
+
sum: Type.Optional(Type.Record(Type.String(), Type.Any())),
|
|
134
|
+
min: Type.Optional(Type.Record(Type.String(), Type.Any())),
|
|
135
|
+
max: Type.Optional(Type.Record(Type.String(), Type.Any())),
|
|
136
|
+
avg: Type.Optional(Type.Record(Type.String(), Type.Any())),
|
|
137
|
+
count: Type.Optional(Type.Record(Type.String(), Type.Any())),
|
|
138
|
+
distinctCount: Type.Optional(Type.Record(Type.String(), Type.Any())),
|
|
139
|
+
})),
|
|
119
140
|
});
|
|
120
|
-
export function SearchTableBodyPost(dbTables, tableName) {
|
|
141
|
+
export function SearchTableBodyPost(dbTables, tableName, maxItemsPerPage = DEFAULT_MAX_ITEMS_PER_PAGE) {
|
|
121
142
|
const tableConf = dbTables[tableName];
|
|
122
|
-
const schema = tableConf.
|
|
143
|
+
const schema = tableConf.schema;
|
|
123
144
|
const methodEnum = Type.Union(ALLOWED_METHODS.map((m) => Type.Literal(m)));
|
|
124
145
|
const conditionItemSchema = Type.Object({
|
|
125
146
|
field: Type.String(),
|
|
@@ -129,7 +150,7 @@ export function SearchTableBodyPost(dbTables, tableName) {
|
|
|
129
150
|
const computedTypes = computedFieldTypes(tableConf);
|
|
130
151
|
const filterFields = {
|
|
131
152
|
...readableFields(schema, tableConf),
|
|
132
|
-
...tableConf
|
|
153
|
+
...extraFilterFields(tableConf),
|
|
133
154
|
...computedTypes,
|
|
134
155
|
};
|
|
135
156
|
const filtersSchema = Type.Optional(filtersObject(filterFields));
|
|
@@ -137,10 +158,18 @@ export function SearchTableBodyPost(dbTables, tableName) {
|
|
|
137
158
|
const bodyProperties = {
|
|
138
159
|
filters: filtersSchema,
|
|
139
160
|
conditions: Type.Optional(Type.Array(conditionItemSchema, { maxItems: MAX_CONDITIONS })),
|
|
161
|
+
// Main projection, schema and computed fields alike, '*' for every readable field. Names
|
|
162
|
+
// are validated by the engine (readExclude included), not here: a closed enum would be
|
|
163
|
+
// stripped by `removeAdditional` the same way a closed filters map is.
|
|
164
|
+
select: Type.Optional(SelectSchema),
|
|
165
|
+
// Ordering, paging and aggregates belong with the rest of the request, in the body only.
|
|
166
|
+
orderBy: orderByField(),
|
|
167
|
+
paginator: Type.Optional(Type.Object({
|
|
168
|
+
page: pageField(),
|
|
169
|
+
itemsPerPage: itemsPerPageField(maxItemsPerPage),
|
|
170
|
+
})),
|
|
171
|
+
compute: Type.Optional(computeObject()),
|
|
140
172
|
};
|
|
141
|
-
if (Object.keys(computedTypes).length > 0) {
|
|
142
|
-
bodyProperties.selectComputed = Type.Optional(Type.Array(Type.String()));
|
|
143
|
-
}
|
|
144
173
|
if (Object.keys(joins.joinMustExist).length > 0) {
|
|
145
174
|
bodyProperties.joinMustExist = Type.Optional(Type.Partial(Type.Object(joins.joinMustExist)));
|
|
146
175
|
bodyProperties.joinMultiple = Type.Optional(Type.Partial(Type.Object(joins.joinMultiple)));
|
|
@@ -151,40 +180,14 @@ export function SearchTableBodyPost(dbTables, tableName) {
|
|
|
151
180
|
}
|
|
152
181
|
return Type.Object(bodyProperties);
|
|
153
182
|
}
|
|
154
|
-
/**
|
|
155
|
-
* Querystring schema, parameterized on the plugin's `maxItemsPerPage`.
|
|
156
|
-
*
|
|
157
|
-
* The `itemsPerPage` default MUST NOT exceed the cap: Ajv injects the default before any
|
|
158
|
-
* runtime check runs, so a fixed default of 500 combined with `maxItemsPerPage: 100` would
|
|
159
|
-
* make every request without an explicit `itemsPerPage` fail with 400. The default is
|
|
160
|
-
* therefore `min(500, cap)` and the cap doubles as the schema `maximum` (structured 400).
|
|
161
|
-
*/
|
|
162
|
-
export function SearchTableQuery(maxItemsPerPage = DEFAULT_MAX_ITEMS_PER_PAGE) {
|
|
163
|
-
return Type.Object({
|
|
164
|
-
orderBy: Type.Optional(Type.String({ maxLength: MAX_ORDER_BY_LENGTH })),
|
|
165
|
-
page: Type.Optional(Type.Integer({ minimum: 1 })),
|
|
166
|
-
itemsPerPage: Type.Optional(Type.Integer({
|
|
167
|
-
minimum: 1,
|
|
168
|
-
maximum: maxItemsPerPage,
|
|
169
|
-
default: Math.min(500, maxItemsPerPage),
|
|
170
|
-
})),
|
|
171
|
-
computeMin: Type.Optional(Type.String()),
|
|
172
|
-
computeMax: Type.Optional(Type.String()),
|
|
173
|
-
computeSum: Type.Optional(Type.String()),
|
|
174
|
-
computeAvg: Type.Optional(Type.String()),
|
|
175
|
-
// selectComputed list goes in the body (POST), not querystring — see SearchTableBodyPost.
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
/** Default-cap variant, kept for granular composition and backward compatibility. */
|
|
179
|
-
export const SearchTableQueryString = SearchTableQuery();
|
|
180
183
|
export function SearchTableResponse(dbTables, tableName) {
|
|
181
184
|
const tableConf = dbTables[tableName];
|
|
182
|
-
// Computed fields are present in main rows only when
|
|
183
|
-
//
|
|
185
|
+
// Computed fields are present in main rows only when listed in the request's `select`,
|
|
186
|
+
// so they're optional/Partial here.
|
|
184
187
|
const computedTypes = computedFieldTypes(tableConf);
|
|
185
188
|
// Not narrowed by schemaOverrides: an override rules what this API accepts from now on,
|
|
186
189
|
// while the rows already stored predate it (see applySchemaOverrides).
|
|
187
|
-
const mainReadable = readableFields(tableConf.
|
|
190
|
+
const mainReadable = readableFields(tableConf.schema, tableConf);
|
|
188
191
|
const mainItem = Object.keys(computedTypes).length > 0
|
|
189
192
|
? Type.Partial(Type.Object({ ...mainReadable, ...computedTypes }))
|
|
190
193
|
: Type.Partial(Type.Object(mainReadable));
|
|
@@ -200,10 +203,10 @@ export function SearchTableResponse(dbTables, tableName) {
|
|
|
200
203
|
if (Object.keys(joinLeftProps).length > 0) {
|
|
201
204
|
responseProps.joinLeft = Type.Optional(Type.Partial(Type.Object(joinLeftProps)));
|
|
202
205
|
}
|
|
206
|
+
responseProps.computed = Type.Optional(Type.Object(Object.fromEntries(COMPUTE_FNS.map((fn) => [fn, Type.Optional(Type.Record(Type.String(), Type.Any()))]))));
|
|
203
207
|
responseProps.pagination = Type.Optional(Type.Object({
|
|
204
208
|
total: Type.Integer(),
|
|
205
209
|
pages: Type.Integer(),
|
|
206
|
-
computed: Type.Optional(Type.Record(Type.String(), Type.Unknown())),
|
|
207
210
|
paginator: Type.Object({
|
|
208
211
|
page: Type.Integer(),
|
|
209
212
|
itemsPerPage: Type.Integer(),
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type TObject } from '
|
|
1
|
+
import { type TObject } from 'typebox';
|
|
2
2
|
import type { DbTables } from '../../types.js';
|
|
3
3
|
export declare function UpdateTableBody(dbTables: DbTables, tableName: string): TObject;
|
|
4
4
|
export declare function UpdateTableResponse(dbTables: DbTables, tableName: string): TObject;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"update.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/update.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,OAAO,EAAgB,MAAM,
|
|
1
|
+
{"version":3,"file":"update.d.ts","sourceRoot":"","sources":["../../../src/lib/schema/update.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,OAAO,EAAgB,MAAM,SAAS,CAAC;AAE3D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAU/C,wBAAgB,eAAe,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CA2B9E;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAUlF"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Type } from '
|
|
1
|
+
import { Type } from 'typebox';
|
|
2
2
|
import { primaryAsString } from '../../types.js';
|
|
3
3
|
import { writableFields } from '../write-access.js';
|
|
4
|
-
import { pkSchema, applySchemaOverrides, asRequired,
|
|
4
|
+
import { pkSchema, applySchemaOverrides, asRequired, attachRequestSections, attachOutcomeSections, } from './helpers.js';
|
|
5
5
|
export function UpdateTableBody(dbTables, tableName) {
|
|
6
6
|
const tableConf = dbTables[tableName];
|
|
7
|
-
const schema = tableConf.
|
|
7
|
+
const schema = tableConf.schema;
|
|
8
8
|
// Main: PK required, all other fields optional (overrides applied before Optional wrap)
|
|
9
9
|
const baseFields = writableFields(applySchemaOverrides({ ...schema.fields }, tableConf), tableConf, schema);
|
|
10
10
|
const mainFields = {};
|
|
@@ -19,21 +19,14 @@ export function UpdateTableBody(dbTables, tableName) {
|
|
|
19
19
|
// rejected (400) instead of silently reaching the UPDATE as columns (mass assignment).
|
|
20
20
|
main: Type.Object(mainFields, { additionalProperties: false }),
|
|
21
21
|
};
|
|
22
|
-
|
|
23
|
-
withDeletions: true,
|
|
24
|
-
secondaryFields: writeJoinBodyFields,
|
|
25
|
-
strictItems: true,
|
|
26
|
-
});
|
|
22
|
+
attachRequestSections(bodyProperties, tableConf, dbTables, { withDelete: true });
|
|
27
23
|
return Type.Object(bodyProperties);
|
|
28
24
|
}
|
|
29
25
|
export function UpdateTableResponse(dbTables, tableName) {
|
|
30
26
|
const tableConf = dbTables[tableName];
|
|
31
27
|
const responseProperties = {
|
|
32
|
-
main: Type.Object(pkSchema(tableConf, tableConf.
|
|
28
|
+
main: Type.Object(pkSchema(tableConf, tableConf.schema, primaryAsString(tableConf.primary))),
|
|
33
29
|
};
|
|
34
|
-
|
|
35
|
-
withDeletions: true,
|
|
36
|
-
secondaryFields: writeJoinResponseFields,
|
|
37
|
-
});
|
|
30
|
+
attachOutcomeSections(responseProperties, tableConf, dbTables, { deletions: 'all' });
|
|
38
31
|
return Type.Object(responseProperties);
|
|
39
32
|
}
|