fastify-auto-sqlapi 0.5.0 → 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 +258 -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 +94 -28
- 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 -62
- 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 +33 -21
- package/dist/lib/read-access.d.ts.map +1 -1
- package/dist/lib/read-access.js +42 -23
- 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
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
import { ConditionBuilder, Expression } from 'node-condition-builder';
|
|
11
11
|
import { ALLOWED_SET } from '../../condition-methods.js';
|
|
12
12
|
import { tenantForTable, buildTenantRowGuard } from '../../tenant.js';
|
|
13
|
+
import { resolveJoinTableConf } from '../../table-helpers.js';
|
|
13
14
|
import { err400, validateSchemaField } from './fields.js';
|
|
14
15
|
import { requireJoin, extractJoinRefs, subqueryAlias } from './joins.js';
|
|
15
16
|
import { buildJoinRefCondition, dispatchConditionMethod } from './conditions.js';
|
|
@@ -34,26 +35,28 @@ export function aggExpr(fn, qualifiedCol) {
|
|
|
34
35
|
* child rows of other tenants, disagreeing with the scoped `result.joinGroup` for the same
|
|
35
36
|
* alias and leaking their existence.
|
|
36
37
|
*/
|
|
37
|
-
export function buildAggOrderExpr(db, dbTables, tableConf, alias, fn, field, joinGroup, tenant) {
|
|
38
|
+
export function buildAggOrderExpr(db, dbTables, tableConf, alias, fn, field, joinGroup, tenant, at = '') {
|
|
38
39
|
const aggFn = AGG_FN[fn];
|
|
39
40
|
if (!aggFn)
|
|
40
|
-
err400(`Invalid aggregation function: ${fn}`);
|
|
41
|
+
err400('invalid_aggregation', at, `Invalid aggregation function: ${fn}`);
|
|
41
42
|
const groupReq = joinGroup?.[alias];
|
|
42
43
|
if (!groupReq)
|
|
43
|
-
err400(`orderBy/conditions reference undeclared joinGroup: ${alias}`);
|
|
44
|
-
const declaredFields = groupReq
|
|
44
|
+
err400('undeclared_aggregation', at, `orderBy/conditions reference undeclared joinGroup: ${alias}`);
|
|
45
|
+
const declaredFields = groupReq[fn];
|
|
45
46
|
if (!Array.isArray(declaredFields) || !declaredFields.includes(field)) {
|
|
46
|
-
err400(`orderBy/conditions reference undeclared aggregation: ${alias}.${fn}.${field}`);
|
|
47
|
+
err400('undeclared_aggregation', at, `orderBy/conditions reference undeclared aggregation: ${alias}.${fn}.${field}`);
|
|
47
48
|
}
|
|
48
|
-
const joinDef = requireJoin(tableConf, alias, false);
|
|
49
|
+
const joinDef = requireJoin(tableConf, alias, false, at);
|
|
49
50
|
const { joinSchema, joinField } = joinDef;
|
|
50
|
-
if (groupReq.
|
|
51
|
-
|
|
51
|
+
if (groupReq.by && groupReq.by !== joinField) {
|
|
52
|
+
// One value per main row is needed: with `by` on a non-FK column a main row has several.
|
|
53
|
+
const clause = at === 'orderBy' ? 'orderBy' : 'a condition';
|
|
54
|
+
err400('invalid_aggregation', at, `Cannot use aggregation ${alias}.${fn}.${field} in ${clause}: joinGroup '${alias}' groups by the non-FK column '${groupReq.by}', not by the correlation FK '${joinField}'`);
|
|
52
55
|
}
|
|
53
56
|
// Pass the join table's conf: without it validateSchemaField skips the readExclude
|
|
54
57
|
// check, and a HAVING-style condition on a hidden field becomes a 400-vs-200 oracle
|
|
55
58
|
// (executeJoinGroup's own validation is skipped when the main result set is empty).
|
|
56
|
-
const fieldCol = validateSchemaField(field, joinSchema, dbTables
|
|
59
|
+
const fieldCol = validateSchemaField(field, joinSchema, resolveJoinTableConf(dbTables, joinDef), at);
|
|
57
60
|
const refs = extractJoinRefs(db, tableConf, joinDef);
|
|
58
61
|
// The subquery FROM is aliased so the correlation to the outer table survives a
|
|
59
62
|
// self-referencing relation; everything inside references the alias.
|
|
@@ -61,7 +64,7 @@ export function buildAggOrderExpr(db, dbTables, tableConf, alias, fn, field, joi
|
|
|
61
64
|
const subRef = db.qi(subAlias);
|
|
62
65
|
let filterWhere = '';
|
|
63
66
|
let filterVals = [];
|
|
64
|
-
const joinTableConf = dbTables
|
|
67
|
+
const joinTableConf = resolveJoinTableConf(dbTables, joinDef);
|
|
65
68
|
// Tenant scope first: it is emitted before the request's own filter, so its values must be
|
|
66
69
|
// bound in that order too.
|
|
67
70
|
const guard = buildTenantRowGuard(db, tenantForTable(tenant, joinTableConf), subAlias);
|
|
@@ -73,7 +76,7 @@ export function buildAggOrderExpr(db, dbTables, tableConf, alias, fn, field, joi
|
|
|
73
76
|
tenantVals.push(...rendered.values);
|
|
74
77
|
}
|
|
75
78
|
if (groupReq.filters || groupReq.conditions?.length) {
|
|
76
|
-
const cb = buildJoinRefCondition(joinTableConf, joinSchema, { filters: groupReq.filters, conditions: groupReq.conditions }, db, subAlias);
|
|
79
|
+
const cb = buildJoinRefCondition(joinTableConf, joinSchema, { filters: groupReq.filters, conditions: groupReq.conditions }, db, subAlias, `joinGroup.${alias}`);
|
|
77
80
|
const fragment = cb.toExpression();
|
|
78
81
|
if (fragment.value) {
|
|
79
82
|
filterWhere = ` AND ${fragment.value}`;
|
|
@@ -88,22 +91,23 @@ export function buildAggOrderExpr(db, dbTables, tableConf, alias, fn, field, joi
|
|
|
88
91
|
/** HAVING-style conditions: the dotted `<alias>.<fn>.<field>` entries of `conditions`. */
|
|
89
92
|
export function appendAggConditions(params, conditions, db, dbTables, tableConf, joinGroup, tenant) {
|
|
90
93
|
let where = '';
|
|
91
|
-
for (const c of conditions) {
|
|
94
|
+
for (const [i, c] of conditions.entries()) {
|
|
92
95
|
if (!c.field.includes('.'))
|
|
93
96
|
continue;
|
|
97
|
+
const here = `conditions.${i}`;
|
|
94
98
|
if (!ALLOWED_SET.has(c.method)) {
|
|
95
|
-
err400(`Invalid condition method: ${c.method}`);
|
|
99
|
+
err400('invalid_method', `${here}.method`, `Invalid condition method: ${c.method}`);
|
|
96
100
|
}
|
|
97
101
|
const parts = c.field.split('.');
|
|
98
102
|
if (parts.length !== 3) {
|
|
99
|
-
err400(`Invalid dotted field in condition: ${c.field} (expected <alias>.<fn>.<field>)`);
|
|
103
|
+
err400('unknown_field', `${here}.field`, `Invalid dotted field in condition: ${c.field} (expected <alias>.<fn>.<field>)`);
|
|
100
104
|
}
|
|
101
105
|
const [alias, fn, field] = parts;
|
|
102
106
|
// The aggregate carries the values of its own filter; passing it as the left-hand side
|
|
103
107
|
// lets the ConditionBuilder place those and the compared value in one step.
|
|
104
|
-
const expr = buildAggOrderExpr(db, dbTables, tableConf, alias, fn, field, joinGroup, tenant);
|
|
108
|
+
const expr = buildAggOrderExpr(db, dbTables, tableConf, alias, fn, field, joinGroup, tenant, `${here}.field`);
|
|
105
109
|
const tmpCb = new ConditionBuilder('AND', db.cbDialect);
|
|
106
|
-
dispatchConditionMethod(tmpCb, c.method, expr, c.params);
|
|
110
|
+
dispatchConditionMethod(tmpCb, c.method, expr, c.params, here);
|
|
107
111
|
where += ` AND ${params.emitCondition(tmpCb, db)}`;
|
|
108
112
|
}
|
|
109
113
|
return where;
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { ConditionBuilder, Expression } from 'node-condition-builder';
|
|
9
9
|
import type { QueryClient } from '../../db.js';
|
|
10
|
-
import type { FilterRecord,
|
|
10
|
+
import type { FilterRecord, TableConfig, JoinRefFilter, SchemaDefinition, SearchCondition } from '../../../types.js';
|
|
11
11
|
/**
|
|
12
12
|
* Apply one condition method to a builder, after checking the request actually supplied the
|
|
13
13
|
* operands that method needs.
|
|
@@ -17,15 +17,19 @@ import type { FilterRecord, ITable, JoinRefFilter, SchemaDefinition, SearchCondi
|
|
|
17
17
|
* error handler can only report as a 500. Arity is checked here rather than at the four call
|
|
18
18
|
* sites so no path can skip it.
|
|
19
19
|
*/
|
|
20
|
-
export declare function dispatchConditionMethod(cb: ConditionBuilder, method: string, colOrExpr: string | Expression, params: unknown[] | undefined): void;
|
|
21
|
-
|
|
20
|
+
export declare function dispatchConditionMethod(cb: ConditionBuilder, method: string, colOrExpr: string | Expression, params: unknown[] | undefined, at?: string): void;
|
|
21
|
+
/**
|
|
22
|
+
* The WHERE of a join reference: its equality `filters` (plain, computed) and its operator
|
|
23
|
+
* `conditions`. `at` is the reference's request path (`joinGroup.<alias>`), for the 400s.
|
|
24
|
+
*/
|
|
25
|
+
export declare function buildJoinRefCondition(joinTableConf: TableConfig | undefined, joinSchema: SchemaDefinition, ref: JoinRefFilter, db: QueryClient, qualifier?: string, at?: string): ConditionBuilder;
|
|
22
26
|
/**
|
|
23
27
|
* Apply non-dotted conditions to the main `condition` ConditionBuilder when
|
|
24
28
|
* possible. For schema fields → straight dispatch. For computed fields → return
|
|
25
29
|
* side-channel clauses to be appended to WHERE later (with correct placeholder
|
|
26
30
|
* offsets), since ConditionBuilder cannot bind values for an LHS expression.
|
|
27
31
|
*/
|
|
28
|
-
export declare function applyConditions(condition: ConditionBuilder, conditions: SearchCondition[], schema: SchemaDefinition, tableConf:
|
|
32
|
+
export declare function applyConditions(condition: ConditionBuilder, conditions: SearchCondition[], schema: SchemaDefinition, tableConf: TableConfig, db: QueryClient): void;
|
|
29
33
|
/** Computed-field equality filters on the main table. */
|
|
30
|
-
export declare function applyComputedFilters(condition: ConditionBuilder, filters: FilterRecord, tableConf:
|
|
34
|
+
export declare function applyComputedFilters(condition: ConditionBuilder, filters: FilterRecord, tableConf: TableConfig, db: QueryClient): void;
|
|
31
35
|
//# sourceMappingURL=conditions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conditions.d.ts","sourceRoot":"","sources":["../../../../src/lib/engine/search/conditions.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACtE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"conditions.d.ts","sourceRoot":"","sources":["../../../../src/lib/engine/search/conditions.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACtE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAa/C,OAAO,KAAK,EAEV,YAAY,EACZ,WAAW,EACX,aAAa,EACb,gBAAgB,EAChB,eAAe,EAChB,MAAM,mBAAmB,CAAC;AAE3B;;;;;;;;GAQG;AACH,wBAAgB,uBAAuB,CACrC,EAAE,EAAE,gBAAgB,EACpB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,GAAG,UAAU,EAC9B,MAAM,EAAE,OAAO,EAAE,GAAG,SAAS,EAC7B,EAAE,SAAK,GACN,IAAI,CA6BN;AA4DD;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,aAAa,EAAE,WAAW,GAAG,SAAS,EACtC,UAAU,EAAE,gBAAgB,EAC5B,GAAG,EAAE,aAAa,EAClB,EAAE,EAAE,WAAW,EACf,SAAS,CAAC,EAAE,MAAM,EAClB,EAAE,SAAK,GACN,gBAAgB,CAuBlB;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAC7B,SAAS,EAAE,gBAAgB,EAC3B,UAAU,EAAE,eAAe,EAAE,EAC7B,MAAM,EAAE,gBAAgB,EACxB,SAAS,EAAE,WAAW,EACtB,EAAE,EAAE,WAAW,GACd,IAAI,CAeN;AAED,yDAAyD;AACzD,wBAAgB,oBAAoB,CAClC,SAAS,EAAE,gBAAgB,EAC3B,OAAO,EAAE,YAAY,EACrB,SAAS,EAAE,WAAW,EACtB,EAAE,EAAE,WAAW,GACd,IAAI,CAIN"}
|
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { ConditionBuilder } from 'node-condition-builder';
|
|
9
9
|
import { assertFiltersReadable, ownField } from '../../read-access.js';
|
|
10
|
+
import { buildTableFilters } from '../../table-helpers.js';
|
|
10
11
|
import { ALLOWED_SET, SINGLE_VALUE_SET, BETWEEN_SET, IN_SET, NULL_SET, } from '../../condition-methods.js';
|
|
11
|
-
import { err400, validateSchemaField, evaluateComputedField, resolveFieldRef, } from './fields.js';
|
|
12
|
+
import { err400, subPath, validateSchemaField, evaluateComputedField, resolveFieldRef, } from './fields.js';
|
|
12
13
|
/**
|
|
13
14
|
* Apply one condition method to a builder, after checking the request actually supplied the
|
|
14
15
|
* operands that method needs.
|
|
@@ -18,14 +19,15 @@ import { err400, validateSchemaField, evaluateComputedField, resolveFieldRef, }
|
|
|
18
19
|
* error handler can only report as a 500. Arity is checked here rather than at the four call
|
|
19
20
|
* sites so no path can skip it.
|
|
20
21
|
*/
|
|
21
|
-
export function dispatchConditionMethod(cb, method, colOrExpr, params) {
|
|
22
|
+
export function dispatchConditionMethod(cb, method, colOrExpr, params, at = '') {
|
|
23
|
+
const paramsPath = subPath(at, 'params');
|
|
22
24
|
if (params !== undefined && !Array.isArray(params)) {
|
|
23
|
-
err400(`Condition method '${method}': 'params' must be an array`);
|
|
25
|
+
err400('invalid_params', paramsPath, `Condition method '${method}': 'params' must be an array`);
|
|
24
26
|
}
|
|
25
27
|
const args = params ?? [];
|
|
26
28
|
const require = (n) => {
|
|
27
29
|
if (args.length < n) {
|
|
28
|
-
err400(`Condition method '${method}' requires ${n} param(s), got ${args.length}`);
|
|
30
|
+
err400('invalid_params', paramsPath, `Condition method '${method}' requires ${n} param(s), got ${args.length}`);
|
|
29
31
|
}
|
|
30
32
|
};
|
|
31
33
|
if (SINGLE_VALUE_SET.has(method)) {
|
|
@@ -41,7 +43,7 @@ export function dispatchConditionMethod(cb, method, colOrExpr, params) {
|
|
|
41
43
|
// An explicit `undefined` stays a documented no-op; any other non-array would make the
|
|
42
44
|
// builder iterate a non-iterable.
|
|
43
45
|
if (args[0] !== undefined && !Array.isArray(args[0])) {
|
|
44
|
-
err400(`Condition method '${method}' requires an array as its first param`);
|
|
46
|
+
err400('invalid_params', paramsPath, `Condition method '${method}' requires an array as its first param`);
|
|
45
47
|
}
|
|
46
48
|
cb[method](colOrExpr, args[0]);
|
|
47
49
|
}
|
|
@@ -71,37 +73,45 @@ function applyComputedEqualityFilters(cb, filters, computed, schema, db, qualifi
|
|
|
71
73
|
cb.isEqual(expr, value);
|
|
72
74
|
}
|
|
73
75
|
}
|
|
74
|
-
/**
|
|
75
|
-
|
|
76
|
+
/**
|
|
77
|
+
* Operator conditions on a join reference, against a schema or a computed field. `at` is the
|
|
78
|
+
* request path of the join reference (`joinMultiple.<alias>`), '' when unknown.
|
|
79
|
+
*/
|
|
80
|
+
function applyJoinRefConditions(cb, conditions, joinTableConf, joinSchema, db, colQualifier, at) {
|
|
76
81
|
const computed = joinTableConf?.computedFields;
|
|
77
|
-
for (const c of conditions) {
|
|
82
|
+
for (const [i, c] of conditions.entries()) {
|
|
83
|
+
const here = subPath(subPath(at, 'conditions'), i);
|
|
78
84
|
if (!ALLOWED_SET.has(c.method)) {
|
|
79
|
-
err400(`Invalid condition method: ${c.method}`);
|
|
85
|
+
err400('invalid_method', subPath(here, 'method'), `Invalid condition method: ${c.method}`);
|
|
80
86
|
}
|
|
81
87
|
// Computed fields resolve to an Expression carrying their own bound values, which the
|
|
82
88
|
// ConditionBuilder places together with the compared value.
|
|
83
89
|
const fn = ownField(computed, c.field);
|
|
84
90
|
const operand = fn
|
|
85
91
|
? evaluateComputedField(c.field, fn, joinSchema, db, colQualifier, '', true)
|
|
86
|
-
: `${db.qi(colQualifier)}.${db.qi(validateSchemaField(c.field, joinSchema, joinTableConf))}`;
|
|
87
|
-
dispatchConditionMethod(cb, c.method, operand, c.params ?? []);
|
|
92
|
+
: `${db.qi(colQualifier)}.${db.qi(validateSchemaField(c.field, joinSchema, joinTableConf, subPath(here, 'field')))}`;
|
|
93
|
+
dispatchConditionMethod(cb, c.method, operand, c.params ?? [], here);
|
|
88
94
|
}
|
|
89
95
|
}
|
|
90
|
-
|
|
91
|
-
|
|
96
|
+
/**
|
|
97
|
+
* The WHERE of a join reference: its equality `filters` (plain, computed) and its operator
|
|
98
|
+
* `conditions`. `at` is the reference's request path (`joinGroup.<alias>`), for the 400s.
|
|
99
|
+
*/
|
|
100
|
+
export function buildJoinRefCondition(joinTableConf, joinSchema, ref, db, qualifier, at = '') {
|
|
101
|
+
assertFiltersReadable(ref.filters, joinTableConf, subPath(at, 'filters'));
|
|
92
102
|
// Filter keys are checked by assertJoinFilterKeys, up front in searchEngine — this side
|
|
93
103
|
// query may never run, so it is not a place a request can be rejected from.
|
|
94
104
|
// Bare table name for plain side-queries; the subquery alias when the condition lands
|
|
95
105
|
// inside an aliased, correlated FROM (there the table name would resolve to the outer query).
|
|
96
106
|
const colQualifier = qualifier ?? joinSchema.tableName;
|
|
97
107
|
const cb = (ref.filters && joinTableConf)
|
|
98
|
-
? joinTableConf
|
|
108
|
+
? buildTableFilters(joinTableConf, ref.filters, db.cbDialect, colQualifier)
|
|
99
109
|
: new ConditionBuilder('AND', db.cbDialect);
|
|
100
110
|
if (ref.filters) {
|
|
101
111
|
applyComputedEqualityFilters(cb, ref.filters, joinTableConf?.computedFields, joinSchema, db, colQualifier);
|
|
102
112
|
}
|
|
103
113
|
if (ref.conditions?.length) {
|
|
104
|
-
applyJoinRefConditions(cb, ref.conditions, joinTableConf, joinSchema, db, colQualifier);
|
|
114
|
+
applyJoinRefConditions(cb, ref.conditions, joinTableConf, joinSchema, db, colQualifier, at);
|
|
105
115
|
}
|
|
106
116
|
return cb;
|
|
107
117
|
}
|
|
@@ -112,20 +122,21 @@ export function buildJoinRefCondition(joinTableConf, joinSchema, ref, db, qualif
|
|
|
112
122
|
* offsets), since ConditionBuilder cannot bind values for an LHS expression.
|
|
113
123
|
*/
|
|
114
124
|
export function applyConditions(condition, conditions, schema, tableConf, db) {
|
|
115
|
-
for (const c of conditions) {
|
|
125
|
+
for (const [i, c] of conditions.entries()) {
|
|
116
126
|
// Skip dot-notation fields — those become aggregation conditions processed later
|
|
117
127
|
if (c.field.includes('.'))
|
|
118
128
|
continue;
|
|
129
|
+
const here = `conditions.${i}`;
|
|
119
130
|
if (!ALLOWED_SET.has(c.method)) {
|
|
120
|
-
err400(`Invalid condition method: ${c.method}`);
|
|
131
|
+
err400('invalid_method', `${here}.method`, `Invalid condition method: ${c.method}`);
|
|
121
132
|
}
|
|
122
133
|
// A computed field resolves to an Expression carrying its own bound values; the
|
|
123
134
|
// ConditionBuilder places them, so no placeholder offset is computed here.
|
|
124
|
-
const ref = resolveFieldRef(c.field, schema, tableConf, db);
|
|
125
|
-
dispatchConditionMethod(condition, c.method, ref.expr, c.params);
|
|
135
|
+
const ref = resolveFieldRef(c.field, schema, tableConf, db, undefined, `${here}.field`);
|
|
136
|
+
dispatchConditionMethod(condition, c.method, ref.expr, c.params, here);
|
|
126
137
|
}
|
|
127
138
|
}
|
|
128
139
|
/** Computed-field equality filters on the main table. */
|
|
129
140
|
export function applyComputedFilters(condition, filters, tableConf, db) {
|
|
130
|
-
applyComputedEqualityFilters(condition, filters, tableConf.computedFields, tableConf.
|
|
141
|
+
applyComputedEqualityFilters(condition, filters, tableConf.computedFields, tableConf.schema, db);
|
|
131
142
|
}
|
|
@@ -7,9 +7,30 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { Expression } from 'node-condition-builder';
|
|
9
9
|
import type { QueryClient } from '../../db.js';
|
|
10
|
-
import type {
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
import type { TableConfig, SchemaDefinition, ComputedFieldFn } from '../../../types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Throw a 400 the client can place: `code` is the stable machine code and `path` the dot path
|
|
13
|
+
* of the offending value in the request ('' when the rejection is not attributable to one
|
|
14
|
+
* input). Both land in the response's `fields`, see `ValidationField`.
|
|
15
|
+
*/
|
|
16
|
+
export declare function err400(code: string, path: string, message: string): never;
|
|
17
|
+
/**
|
|
18
|
+
* Reject a request key retired in 1.0: a client still sending it gets a 400 naming the key
|
|
19
|
+
* instead of a response that silently ignores it.
|
|
20
|
+
*/
|
|
21
|
+
export declare function rejectRetiredKey(input: object | undefined, key: string, at: string, instead: string): void;
|
|
22
|
+
/**
|
|
23
|
+
* Search parameters retired in 1.0, checked at every search entry point: `selectComputed` was
|
|
24
|
+
* folded into `select`, the one-field `computeMin/Max/Sum/Avg` into `compute`.
|
|
25
|
+
*/
|
|
26
|
+
export declare function rejectRetiredSearchKeys(params: object): void;
|
|
27
|
+
/** `at.key`, or '' when `at` is unknown: a child path is only as precise as its parent. */
|
|
28
|
+
export declare function subPath(at: string, key: string | number): string;
|
|
29
|
+
/**
|
|
30
|
+
* The column of a schema field named by the request, or a 400 when the schema has no such
|
|
31
|
+
* field or `tableConf` hides it. `at` is the request path of the reference, for the 400.
|
|
32
|
+
*/
|
|
33
|
+
export declare function validateSchemaField(field: string, schema: SchemaDefinition, tableConf?: TableConfig, at?: string): string;
|
|
13
34
|
/**
|
|
14
35
|
* Resolve a field reference in a request body to a SQL operand. A schema column becomes
|
|
15
36
|
* the quoted column name; a computed field becomes an Expression carrying its own bound
|
|
@@ -19,16 +40,16 @@ export interface ResolvedFieldRef {
|
|
|
19
40
|
expr: string | Expression;
|
|
20
41
|
computed: boolean;
|
|
21
42
|
}
|
|
22
|
-
export declare function resolveFieldRef(field: string, schema: SchemaDefinition, tableConf:
|
|
43
|
+
export declare function resolveFieldRef(field: string, schema: SchemaDefinition, tableConf: TableConfig | undefined, db: QueryClient, alias?: string, at?: string): ResolvedFieldRef;
|
|
23
44
|
/**
|
|
24
45
|
* Evaluate a computed field into an Expression carrying its own bound values.
|
|
25
46
|
*
|
|
26
47
|
* Inside a WHERE clause the ConditionBuilder assigns the placeholder indexes, so bound
|
|
27
48
|
* values are always safe there. `allowBoundValues=false` is for the positions that sit
|
|
28
49
|
* *before* the WHERE values in the parameter order (SELECT projections, aggregations),
|
|
29
|
-
* where they cannot be placed correctly; those still reject with 400
|
|
50
|
+
* where they cannot be placed correctly; those still reject with 400, at request path `at`.
|
|
30
51
|
*/
|
|
31
|
-
export declare function evaluateComputedField(name: string, fn: ComputedFieldFn, schema: SchemaDefinition, db: QueryClient, alias: string | undefined, message: string, allowBoundValues?: boolean): Expression;
|
|
52
|
+
export declare function evaluateComputedField(name: string, fn: ComputedFieldFn, schema: SchemaDefinition, db: QueryClient, alias: string | undefined, message: string, allowBoundValues?: boolean, at?: string): Expression;
|
|
32
53
|
/** Bound values carried by a resolved field reference. */
|
|
33
54
|
export declare function refValues(expr: string | Expression): unknown[];
|
|
34
55
|
/** Render a resolved field reference to SQL, binding any values from `startIndex`. */
|
|
@@ -37,13 +58,25 @@ export declare function renderRef(expr: string | Expression, startIndex: number,
|
|
|
37
58
|
* that are projected by their declared name (skipped by camelcaseObject). */
|
|
38
59
|
export declare function mapRowsToCamelCase(rows: Record<string, unknown>[], schema: SchemaDefinition, computedFieldNames?: Iterable<string>): Record<string, unknown>[];
|
|
39
60
|
/**
|
|
40
|
-
* Build the SELECT columns list for a join fetch:
|
|
41
|
-
*
|
|
42
|
-
*
|
|
61
|
+
* Build the SELECT columns list for a join fetch: the quoted columns of the `select`ed fields,
|
|
62
|
+
* each validated (unknown or `readExclude`d → 400 at `<at>.<i>`). Without a `select`, every
|
|
63
|
+
* readable column of the join table.
|
|
43
64
|
*
|
|
44
|
-
* On a relation narrowed by a `fields` allowlist the
|
|
65
|
+
* On a relation narrowed by a `fields` allowlist the default is always spelled out. Emitting a
|
|
45
66
|
* real `SELECT *` there would fetch the excluded columns and leave them to be dropped by
|
|
46
67
|
* the response serializer — which does not run for a caller of `sqlApi.search()`.
|
|
47
68
|
*/
|
|
48
|
-
export declare function
|
|
69
|
+
export declare function buildJoinSelectColumns(select: string[] | undefined, joinSchema: SchemaDefinition, db: QueryClient, joinTableConf?: TableConfig, restricted?: boolean, at?: string): string;
|
|
70
|
+
/**
|
|
71
|
+
* The main query's column list for a request `select`: the listed schema fields, validated one
|
|
72
|
+
* by one (unknown or `readExclude`d → 400 at `select.<i>`), then `required` ones the request did
|
|
73
|
+
* not list — the primary key, join correlation keys, DISTINCT's ORDER BY fields — when readable.
|
|
74
|
+
* Computed fields in `select` are skipped here: `executeMainQuery` projects them.
|
|
75
|
+
*
|
|
76
|
+
* {@link SELECT_ALL} in the list stands for the default projection — every readable column, as
|
|
77
|
+
* a search without `select` returns — so a client asks for a computed field without copying the
|
|
78
|
+
* readable set: `['*', 'fullName']`. The other names are still validated; a schema field next
|
|
79
|
+
* to it adds nothing. See ADR 0028.
|
|
80
|
+
*/
|
|
81
|
+
export declare function buildMainSelectColumns(select: string[], required: string[], tableConf: TableConfig, db: QueryClient): string;
|
|
49
82
|
//# sourceMappingURL=fields.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fields.d.ts","sourceRoot":"","sources":["../../../../src/lib/engine/search/fields.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"fields.d.ts","sourceRoot":"","sources":["../../../../src/lib/engine/search/fields.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAW/C,OAAO,KAAK,EACV,WAAW,EACX,gBAAgB,EAGhB,eAAe,EAChB,MAAM,mBAAmB,CAAC;AAI3B;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,KAAK,CAEzE;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,EAA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CAER;AAED;6EAC6E;AAC7E,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,EAC/B,MAAM,EAAE,gBAAgB,EACxB,kBAAkB,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,GACpC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAW3B;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,EAC5B,UAAU,EAAE,gBAAgB,EAC5B,EAAE,EAAE,WAAW,EACf,aAAa,CAAC,EAAE,WAAW,EAC3B,UAAU,UAAQ,EAClB,EAAE,SAAK,GACN,MAAM,CAYR;AAKD;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,MAAM,EAAE,EAChB,QAAQ,EAAE,MAAM,EAAE,EAClB,SAAS,EAAE,WAAW,EACtB,EAAE,EAAE,WAAW,GACd,MAAM,CAeR"}
|
|
@@ -7,22 +7,52 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { Expression } from 'node-condition-builder';
|
|
9
9
|
import { camelcaseObject } from '../../naming.js';
|
|
10
|
-
import {
|
|
10
|
+
import { httpError } from '../../errors.js';
|
|
11
|
+
import { assertReadable, readableSelectColumns, readableFieldNames, explicitSelectColumns, hasOwnField, ownField, } from '../../read-access.js';
|
|
11
12
|
// ─── Schema field validation ────────────────────────────────
|
|
12
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Throw a 400 the client can place: `code` is the stable machine code and `path` the dot path
|
|
15
|
+
* of the offending value in the request ('' when the rejection is not attributable to one
|
|
16
|
+
* input). Both land in the response's `fields`, see `ValidationField`.
|
|
17
|
+
*/
|
|
18
|
+
export function err400(code, path, message) {
|
|
19
|
+
throw httpError(400, message, { code, path });
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Reject a request key retired in 1.0: a client still sending it gets a 400 naming the key
|
|
23
|
+
* instead of a response that silently ignores it.
|
|
24
|
+
*/
|
|
25
|
+
export function rejectRetiredKey(input, key, at, instead) {
|
|
26
|
+
if (input !== undefined && Object.hasOwn(input, key)) {
|
|
27
|
+
err400('retired_key', at, `'${key}' was removed in 1.0: ${instead}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Search parameters retired in 1.0, checked at every search entry point: `selectComputed` was
|
|
32
|
+
* folded into `select`, the one-field `computeMin/Max/Sum/Avg` into `compute`.
|
|
33
|
+
*/
|
|
34
|
+
export function rejectRetiredSearchKeys(params) {
|
|
35
|
+
rejectRetiredKey(params, 'selectComputed', 'selectComputed', `list computed fields in 'select', next to '*' for every readable field`);
|
|
36
|
+
for (const fn of ['Min', 'Max', 'Sum', 'Avg']) {
|
|
37
|
+
const key = `compute${fn}`;
|
|
38
|
+
rejectRetiredKey(params, key, key, `use 'compute': { ${fn.toLowerCase()}: [field] }`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** `at.key`, or '' when `at` is unknown: a child path is only as precise as its parent. */
|
|
42
|
+
export function subPath(at, key) {
|
|
43
|
+
return at ? `${at}.${key}` : '';
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The column of a schema field named by the request, or a 400 when the schema has no such
|
|
47
|
+
* field or `tableConf` hides it. `at` is the request path of the reference, for the 400.
|
|
48
|
+
*/
|
|
49
|
+
export function validateSchemaField(field, schema, tableConf, at = '') {
|
|
13
50
|
if (!hasOwnField(schema.fields, field)) {
|
|
14
|
-
|
|
15
|
-
err.statusCode = 400;
|
|
16
|
-
throw err;
|
|
51
|
+
err400('unknown_field', at, `Unknown field: ${field}`);
|
|
17
52
|
}
|
|
18
|
-
assertReadable(tableConf, field);
|
|
53
|
+
assertReadable(tableConf, field, at);
|
|
19
54
|
return schema.col(field);
|
|
20
55
|
}
|
|
21
|
-
export function err400(msg) {
|
|
22
|
-
const e = new Error(msg);
|
|
23
|
-
e.statusCode = 400;
|
|
24
|
-
throw e;
|
|
25
|
-
}
|
|
26
56
|
// ─── Computed fields resolution ─────────────────────────────
|
|
27
57
|
/**
|
|
28
58
|
* Build a ComputedFieldContext bound to a specific table/schema. `alias`
|
|
@@ -34,7 +64,8 @@ function buildComputedContext(db, schema, alias) {
|
|
|
34
64
|
db,
|
|
35
65
|
qiCol(field, opts) {
|
|
36
66
|
if (!hasOwnField(schema.fields, field)) {
|
|
37
|
-
|
|
67
|
+
// A table-configuration mistake surfacing at request time: not attributable to an input.
|
|
68
|
+
err400('unknown_field', '', `Unknown field referenced by computed expression: ${field}`);
|
|
38
69
|
}
|
|
39
70
|
const col = db.qi(schema.col(field));
|
|
40
71
|
// Default to the owning table: the statement may carry joins, and a bare
|
|
@@ -44,9 +75,9 @@ function buildComputedContext(db, schema, alias) {
|
|
|
44
75
|
},
|
|
45
76
|
};
|
|
46
77
|
}
|
|
47
|
-
export function resolveFieldRef(field, schema, tableConf, db, alias) {
|
|
78
|
+
export function resolveFieldRef(field, schema, tableConf, db, alias, at = '') {
|
|
48
79
|
if (hasOwnField(schema.fields, field)) {
|
|
49
|
-
assertReadable(tableConf, field);
|
|
80
|
+
assertReadable(tableConf, field, at);
|
|
50
81
|
const col = db.qi(schema.col(field));
|
|
51
82
|
// Qualified with the alias or the owning table — never bare, so the reference
|
|
52
83
|
// stays unambiguous when the statement carries joins.
|
|
@@ -58,7 +89,7 @@ export function resolveFieldRef(field, schema, tableConf, db, alias) {
|
|
|
58
89
|
const ctx = buildComputedContext(db, schema, alias);
|
|
59
90
|
return { expr: computedExpression(field, fn(ctx)), computed: true };
|
|
60
91
|
}
|
|
61
|
-
err400(`Unknown field: ${field}`);
|
|
92
|
+
err400('unknown_field', at, `Unknown field: ${field}`);
|
|
62
93
|
}
|
|
63
94
|
/**
|
|
64
95
|
* Evaluate a computed field into an Expression carrying its own bound values.
|
|
@@ -66,12 +97,12 @@ export function resolveFieldRef(field, schema, tableConf, db, alias) {
|
|
|
66
97
|
* Inside a WHERE clause the ConditionBuilder assigns the placeholder indexes, so bound
|
|
67
98
|
* values are always safe there. `allowBoundValues=false` is for the positions that sit
|
|
68
99
|
* *before* the WHERE values in the parameter order (SELECT projections, aggregations),
|
|
69
|
-
* where they cannot be placed correctly; those still reject with 400
|
|
100
|
+
* where they cannot be placed correctly; those still reject with 400, at request path `at`.
|
|
70
101
|
*/
|
|
71
|
-
export function evaluateComputedField(name, fn, schema, db, alias, message, allowBoundValues = false) {
|
|
102
|
+
export function evaluateComputedField(name, fn, schema, db, alias, message, allowBoundValues = false, at = '') {
|
|
72
103
|
const ev = fn(buildComputedContext(db, schema, alias));
|
|
73
104
|
if (!allowBoundValues && (ev.values?.length ?? 0) > 0) {
|
|
74
|
-
err400(message);
|
|
105
|
+
err400('bound_values_not_allowed', at, message);
|
|
75
106
|
}
|
|
76
107
|
return computedExpression(name, ev);
|
|
77
108
|
}
|
|
@@ -120,23 +151,58 @@ export function mapRowsToCamelCase(rows, schema, computedFieldNames) {
|
|
|
120
151
|
});
|
|
121
152
|
}
|
|
122
153
|
/**
|
|
123
|
-
* Build the SELECT columns list for a join fetch:
|
|
124
|
-
*
|
|
125
|
-
*
|
|
154
|
+
* Build the SELECT columns list for a join fetch: the quoted columns of the `select`ed fields,
|
|
155
|
+
* each validated (unknown or `readExclude`d → 400 at `<at>.<i>`). Without a `select`, every
|
|
156
|
+
* readable column of the join table.
|
|
126
157
|
*
|
|
127
|
-
* On a relation narrowed by a `fields` allowlist the
|
|
158
|
+
* On a relation narrowed by a `fields` allowlist the default is always spelled out. Emitting a
|
|
128
159
|
* real `SELECT *` there would fetch the excluded columns and leave them to be dropped by
|
|
129
160
|
* the response serializer — which does not run for a caller of `sqlApi.search()`.
|
|
130
161
|
*/
|
|
131
|
-
export function
|
|
132
|
-
if (
|
|
162
|
+
export function buildJoinSelectColumns(select, joinSchema, db, joinTableConf, restricted = false, at = '') {
|
|
163
|
+
if (select === undefined) {
|
|
133
164
|
if (restricted)
|
|
134
165
|
return explicitSelectColumns(joinTableConf, joinSchema, db);
|
|
135
|
-
return readableSelectColumns(joinTableConf, joinSchema, db)
|
|
166
|
+
return readableSelectColumns(joinTableConf, joinSchema, db);
|
|
167
|
+
}
|
|
168
|
+
if (!Array.isArray(select)) {
|
|
169
|
+
throw new TypeError(`${at || 'select'}: 'select' is an array of field names`);
|
|
136
170
|
}
|
|
137
171
|
const table = db.qi(joinSchema.tableName);
|
|
138
|
-
return
|
|
139
|
-
.
|
|
140
|
-
.map((c) => `${table}.${db.qi(validateSchemaField(c.trim(), joinSchema, joinTableConf))}`)
|
|
172
|
+
return select
|
|
173
|
+
.map((name, i) => `${table}.${db.qi(validateSchemaField(name, joinSchema, joinTableConf, subPath(at, i)))}`)
|
|
141
174
|
.join(', ');
|
|
142
175
|
}
|
|
176
|
+
/** In the main `select`, the default projection: every readable column. Not a field name. */
|
|
177
|
+
const SELECT_ALL = '*';
|
|
178
|
+
/**
|
|
179
|
+
* The main query's column list for a request `select`: the listed schema fields, validated one
|
|
180
|
+
* by one (unknown or `readExclude`d → 400 at `select.<i>`), then `required` ones the request did
|
|
181
|
+
* not list — the primary key, join correlation keys, DISTINCT's ORDER BY fields — when readable.
|
|
182
|
+
* Computed fields in `select` are skipped here: `executeMainQuery` projects them.
|
|
183
|
+
*
|
|
184
|
+
* {@link SELECT_ALL} in the list stands for the default projection — every readable column, as
|
|
185
|
+
* a search without `select` returns — so a client asks for a computed field without copying the
|
|
186
|
+
* readable set: `['*', 'fullName']`. The other names are still validated; a schema field next
|
|
187
|
+
* to it adds nothing. See ADR 0028.
|
|
188
|
+
*/
|
|
189
|
+
export function buildMainSelectColumns(select, required, tableConf, db) {
|
|
190
|
+
const schema = tableConf.schema;
|
|
191
|
+
const table = db.qi(schema.tableName);
|
|
192
|
+
const fields = [];
|
|
193
|
+
for (const [i, name] of select.entries()) {
|
|
194
|
+
if (name === SELECT_ALL || ownField(tableConf.computedFields, name))
|
|
195
|
+
continue;
|
|
196
|
+
validateSchemaField(name, schema, tableConf, `select.${i}`);
|
|
197
|
+
if (!fields.includes(name))
|
|
198
|
+
fields.push(name);
|
|
199
|
+
}
|
|
200
|
+
if (select.includes(SELECT_ALL))
|
|
201
|
+
return readableSelectColumns(tableConf, schema, db);
|
|
202
|
+
const readable = readableFieldNames(tableConf, schema);
|
|
203
|
+
for (const name of required) {
|
|
204
|
+
if (!fields.includes(name) && readable.includes(name))
|
|
205
|
+
fields.push(name);
|
|
206
|
+
}
|
|
207
|
+
return fields.map((f) => `${table}.${db.qi(schema.col(f))}`).join(', ');
|
|
208
|
+
}
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
import type { ApiRequest } from '../../../types/request.js';
|
|
10
10
|
import type { QueryClient } from '../../db.js';
|
|
11
11
|
import type { QueryParams } from '../query-params.js';
|
|
12
|
-
import type { DbTables,
|
|
12
|
+
import type { DbTables, TableConfig, JoinFetchRequest, JoinGroupRequest, JoinRefFilter, TenantContext, RowsByAlias } from '../../../types.js';
|
|
13
13
|
interface LeftJoinOnBuild {
|
|
14
14
|
joinClauses: string[];
|
|
15
15
|
values: unknown[];
|
|
@@ -31,14 +31,14 @@ interface LeftJoinFilterBuild {
|
|
|
31
31
|
* Emitted before the WHERE fragments because that is where it lands in the statement — MySQL
|
|
32
32
|
* binds `?` positionally.
|
|
33
33
|
*/
|
|
34
|
-
export declare function buildLeftJoinOnClauses(db: QueryClient, dbTables: DbTables, tableConf:
|
|
34
|
+
export declare function buildLeftJoinOnClauses(db: QueryClient, dbTables: DbTables, tableConf: TableConfig, aliasesNeedingJoin: Set<string>, startIdx: number, tenant?: TenantContext): LeftJoinOnBuild;
|
|
35
35
|
/** The WHERE fragments a `joinLeft` request's own filters and conditions contribute. */
|
|
36
|
-
export declare function buildLeftJoinFilters(db: QueryClient, dbTables: DbTables, tableConf:
|
|
36
|
+
export declare function buildLeftJoinFilters(db: QueryClient, dbTables: DbTables, tableConf: TableConfig, aliasesNeedingJoin: Set<string>, joinLeft: Record<string, JoinFetchRequest> | undefined, startIdx: number): LeftJoinFilterBuild;
|
|
37
37
|
/** joinMultiple: 1:N children, returned as rows under `result.joinMultiple.<alias>`. */
|
|
38
|
-
export declare function executeJoinMultiple(db: QueryClient, dbTables: DbTables, tableConf:
|
|
38
|
+
export declare function executeJoinMultiple(db: QueryClient, dbTables: DbTables, tableConf: TableConfig, mainResults: Record<string, unknown>[], joinMultiple: Record<string, JoinFetchRequest>, tenant?: TenantContext, request?: ApiRequest): Promise<RowsByAlias>;
|
|
39
39
|
/** joinLeft: N:1 parents, returned as rows under `result.joinLeft.<alias>`. */
|
|
40
|
-
export declare function executeJoinLeft(db: QueryClient, dbTables: DbTables, tableConf:
|
|
41
|
-
export declare function executeJoinGroup(db: QueryClient, dbTables: DbTables, tableConf:
|
|
42
|
-
export declare function buildJoinMustExistClauses(params: QueryParams, db: QueryClient, dbTables: DbTables, tableConf:
|
|
40
|
+
export declare function executeJoinLeft(db: QueryClient, dbTables: DbTables, tableConf: TableConfig, mainResults: Record<string, unknown>[], joinLeft: Record<string, JoinFetchRequest>, tenant?: TenantContext, request?: ApiRequest): Promise<RowsByAlias>;
|
|
41
|
+
export declare function executeJoinGroup(db: QueryClient, dbTables: DbTables, tableConf: TableConfig, mainResults: Record<string, unknown>[], joinGroup: Record<string, JoinGroupRequest>, tenant?: TenantContext): Promise<Record<string, Record<string, unknown>>>;
|
|
42
|
+
export declare function buildJoinMustExistClauses(params: QueryParams, db: QueryClient, dbTables: DbTables, tableConf: TableConfig, joinMustExist: Record<string, JoinRefFilter>, tenant?: TenantContext): string;
|
|
43
43
|
export {};
|
|
44
44
|
//# sourceMappingURL=join-families.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"join-families.d.ts","sourceRoot":"","sources":["../../../../src/lib/engine/search/join-families.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAE5D,OAAO,KAAK,EAAE,WAA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