fastify-auto-sqlapi 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -9,6 +9,18 @@ Migration instructions for breaking changes live in **[BREAKING_CHANGES.md](./BR
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ### Fixed
13
+
14
+ - **A search with `selectComputed` and a JOIN on the main query returned the joined table's
15
+ columns in the main rows.** A filtered or ordered `joinLeft`, or a tenant scope with
16
+ `through`, adds a JOIN to the main statement; the `selectComputed` path built its
17
+ projection as a bare `*, <expr> AS "x"`, which bypassed the qualification `db.select`
18
+ applies to a plain `*`. On a column name shared with the joined table (`id`, `name`,
19
+ `createdAt`…) the driver keeps the last value read, so every main row carried the parent's
20
+ id and its extra columns. The main projection is now always `"table".*` or the table's
21
+ readable columns, qualified, whatever else is projected. Searches without `selectComputed`
22
+ were not affected.
23
+
12
24
  ## [0.5.0]
13
25
 
14
26
  ### Changed
@@ -132,7 +132,7 @@ export function buildSelectionColumns(selection, joinSchema, db, joinTableConf,
132
132
  if (selection === '*') {
133
133
  if (restricted)
134
134
  return explicitSelectColumns(joinTableConf, joinSchema, db);
135
- return readableSelectColumns(joinTableConf, joinSchema, db) ?? '*';
135
+ return readableSelectColumns(joinTableConf, joinSchema, db);
136
136
  }
137
137
  const table = db.qi(joinSchema.tableName);
138
138
  return selection
@@ -1 +1 @@
1
- {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../../../src/lib/engine/search/search.ts"],"names":[],"mappings":"AAiCA,OAAO,KAAK,EACV,QAAQ,EACR,YAAY,EACZ,YAAY,EAGb,MAAM,mBAAmB,CAAC;AAyP3B,wBAAsB,YAAY,CAChC,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,YAAY,GACnB,OAAO,CAAC,YAAY,CAAC,CA4FvB"}
1
+ {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../../../src/lib/engine/search/search.ts"],"names":[],"mappings":"AAiCA,OAAO,KAAK,EACV,QAAQ,EACR,YAAY,EACZ,YAAY,EAGb,MAAM,mBAAmB,CAAC;AA2P3B,wBAAsB,YAAY,CAChC,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,YAAY,GACnB,OAAO,CAAC,YAAY,CAAC,CA4FvB"}
@@ -48,13 +48,15 @@ async function executeMainQuery(db, tableConf, where, values, orderBy, paginator
48
48
  const limit = paginator
49
49
  ? `${paginator.itemsPerPage} OFFSET ${(paginator.page - 1) * paginator.itemsPerPage}`
50
50
  : (maxRows != null ? String(maxRows) : null);
51
- // Base projection: '*' unless the table hides fields via readExclude.
51
+ // Base projection: the main table's own columns, qualified ("hour".* or the readable list).
52
+ // Never a bare `*`: the statement may carry a LEFT JOIN (filtered/ordered joinLeft) or the
53
+ // tenant's INNER JOIN, and a bare `*` would hand the joined table's columns to the row.
52
54
  const readableColumns = readableSelectColumns(tableConf, tableConf.Schema, db);
53
55
  // Optional computed projections — bound values, if any, are NOT yet supported
54
56
  // here (would require placeholder-aware composition with WHERE values).
55
57
  let columns = readableColumns;
56
58
  if (selectComputed?.length) {
57
- const projections = [readableColumns ?? '*'];
59
+ const projections = [readableColumns];
58
60
  for (const name of selectComputed) {
59
61
  const fn = ownField(tableConf.computedFields, name);
60
62
  if (!fn)
@@ -51,14 +51,17 @@ export declare function assertKnownFilterKeys(filters: Record<string, unknown> |
51
51
  /** Schema field names that may be read, in schema declaration order. */
52
52
  export declare function readableFieldNames(tableConf: ITable | undefined, schema: SchemaDefinition): string[];
53
53
  /**
54
- * SELECT column list for a table with read exclusions, or `undefined` when the
55
- * table has none (callers keep their existing `*` projection).
54
+ * The SELECT projection of a table's own rows: its readable columns spelled out when the
55
+ * table has `readExclude`, `"table".*` otherwise.
56
56
  *
57
- * Columns are qualified with the table name: read queries may carry a tenant
58
- * `through` INNER JOIN, where a bare column name shared with the through table
59
- * would be ambiguous.
57
+ * Always qualified with the table name, never a bare `*`. A read query may carry a JOIN — a
58
+ * filtered or ordered `joinLeft`, a tenant `through` INNER JOIN — and a bare `*` would
59
+ * project the joined table's columns into the row too; on a name clash (`id`, `name`,
60
+ * `createdAt`…) the driver keeps the LAST value read, the joined one, so every main row
61
+ * would carry the parent's id. Callers compose this string further (computed projections),
62
+ * which is why the qualification has to be in the string itself and not only in `db.select`.
60
63
  */
61
- export declare function readableSelectColumns(tableConf: ITable | undefined, schema: SchemaDefinition, db: QueryClient): string | undefined;
64
+ export declare function readableSelectColumns(tableConf: ITable | undefined, schema: SchemaDefinition, db: QueryClient): string;
62
65
  /**
63
66
  * The same list, always spelled out. Used where `*` is not an acceptable fallback: a
64
67
  * relation narrowed by a `fields` allowlist must project the allowlist, not the table.
@@ -1 +1 @@
1
- {"version":3,"file":"read-access.d.ts","sourceRoot":"","sources":["../../src/lib/read-access.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,KAAK,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAG5D;;;;;;;;GAQG;AAEH;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CACzB,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,EACxC,GAAG,EAAE,MAAM,GACV,OAAO,CAET;AAED,sEAAsE;AACtE,wBAAgB,QAAQ,CAAC,CAAC,EACxB,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,SAAS,EAClC,GAAG,EAAE,MAAM,GACV,CAAC,GAAG,SAAS,CAEf;AAMD,+DAA+D;AAC/D,wBAAgB,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAIjF;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,EAC5C,SAAS,EAAE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;AAC/D,wBAAgB,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAIjF;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,EAC5C,SAAS,EAAE,MAAM,GAAG,SAAS,GAC5B,IAAI,CAKN;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,EAC5C,MAAM,EAAE,gBAAgB,EACxB,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,iBAAiB,UAAO,GACvB,IAAI,CAkBN;AAED,wEAAwE;AACxE,wBAAgB,kBAAkB,CAChC,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,MAAM,EAAE,gBAAgB,GACvB,MAAM,EAAE,CAIV;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,qBAAqB,CACnC,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,MAAM,EAAE,gBAAgB,EACxB,EAAE,EAAE,WAAW,GACd,MAAM,CAGR;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,MAAM,EAAE,gBAAgB,EACxB,EAAE,EAAE,WAAW,GACd,MAAM,CAKR"}
@@ -92,16 +92,19 @@ export function readableFieldNames(tableConf, schema) {
92
92
  return fields.filter((f) => !isReadExcluded(tableConf, f));
93
93
  }
94
94
  /**
95
- * SELECT column list for a table with read exclusions, or `undefined` when the
96
- * table has none (callers keep their existing `*` projection).
95
+ * The SELECT projection of a table's own rows: its readable columns spelled out when the
96
+ * table has `readExclude`, `"table".*` otherwise.
97
97
  *
98
- * Columns are qualified with the table name: read queries may carry a tenant
99
- * `through` INNER JOIN, where a bare column name shared with the through table
100
- * would be ambiguous.
98
+ * Always qualified with the table name, never a bare `*`. A read query may carry a JOIN — a
99
+ * filtered or ordered `joinLeft`, a tenant `through` INNER JOIN — and a bare `*` would
100
+ * project the joined table's columns into the row too; on a name clash (`id`, `name`,
101
+ * `createdAt`…) the driver keeps the LAST value read, the joined one, so every main row
102
+ * would carry the parent's id. Callers compose this string further (computed projections),
103
+ * which is why the qualification has to be in the string itself and not only in `db.select`.
101
104
  */
102
105
  export function readableSelectColumns(tableConf, schema, db) {
103
106
  if (!tableConf?.readExclude?.length)
104
- return undefined;
107
+ return `${db.qi(schema.tableName)}.*`;
105
108
  return explicitSelectColumns(tableConf, schema, db);
106
109
  }
107
110
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify-auto-sqlapi",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Fastify plugin for automatic CRUD API generation from PostgreSQL, MySQL and MariaDB tables",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",