@odatano/core 2.0.0-rc.16 → 2.0.0-rc.17

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@odatano/core",
3
- "version": "2.0.0-rc.16",
3
+ "version": "2.0.0-rc.17",
4
4
  "description": "SAP CAP plugin for Cardano blockchain OData V4 integration",
5
5
  "imports": {
6
6
  "#cds-models/*": "./@cds-models/*/index.js"
package/srv/server.js CHANGED
@@ -29,6 +29,7 @@ const errors_1 = require("./utils/errors");
29
29
  const network_context_1 = require("./utils/network-context");
30
30
  const db_sanitize_1 = require("./utils/db-sanitize");
31
31
  const db_indexes_1 = require("./utils/db-indexes");
32
+ const pg_order_nulls_1 = require("./utils/pg-order-nulls");
32
33
  const crawler_1 = require("./blockchain/crawler");
33
34
  const wallet_worker_1 = require("./blockchain/wallet-worker");
34
35
  const agent_grants_config_1 = require("./utils/agent-grants-config");
@@ -181,6 +182,9 @@ async function initializeFromConfig(config, protocolParams, hsmConfig) {
181
182
  return;
182
183
  }
183
184
  hsmConfigInstance = hsmConfig;
185
+ // ORDER BY on Postgres without a NULLS clause for key / NOT NULL columns, so
186
+ // the indexes below serve `$top` and `$orderby` reads (srv/utils/pg-order-nulls.ts).
187
+ (0, pg_order_nulls_1.installPostgresOrderNulls)();
184
188
  // The secondary indexes the model cannot declare (srv/utils/db-indexes.ts):
185
189
  // idempotent, never fatal, before anything reads.
186
190
  try {
@@ -0,0 +1,14 @@
1
+ interface OrderTerm {
2
+ nulls?: string;
3
+ element?: {
4
+ key?: boolean;
5
+ notNull?: boolean;
6
+ '@cds.valid.from'?: boolean;
7
+ };
8
+ }
9
+ /** Strips the null placement from the rendered terms whose column cannot be NULL. */
10
+ export declare function stripNullsForNotNull(orderBy: OrderTerm[], rendered: string[]): string[];
11
+ /** Wraps the driver's `_orderBy` once; returns false when the driver or the hook is missing. */
12
+ export declare function installPostgresOrderNulls(): boolean;
13
+ export {};
14
+ //# sourceMappingURL=pg-order-nulls.d.ts.map
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.stripNullsForNotNull = stripNullsForNotNull;
7
+ exports.installPostgresOrderNulls = installPostgresOrderNulls;
8
+ /**
9
+ * ORDER BY on PostgreSQL: no NULLS clause for NOT NULL columns.
10
+ *
11
+ * @cap-js/postgres renders every ordering term with an explicit null placement,
12
+ * `ASC NULLS FIRST` and `DESC NULLS LAST`, to give SQLite's and HANA's null
13
+ * order on Postgres too. A Postgres btree index is `ASC NULLS LAST` (read
14
+ * backwards `DESC NULLS FIRST`), the opposite placement, so the planner
15
+ * cannot use an index for such an ORDER BY and sorts the whole table before
16
+ * the LIMIT. CAP orders every `$top` read by the entity key, so on the hosted
17
+ * box `Blocks?$top=1` took 0.8 to 7.6 s over 2M rows in NIGHTGATE and every
18
+ * `$top` on a large ODATANO table sorted the same way (measured 2026-09-23,
19
+ * `ORDER BY "$b".ID ASC NULLS FIRST LIMIT 1`, parallel seq scan + top-N sort).
20
+ *
21
+ * A key column, a `not null` column and a temporal `@cds.valid.from` column
22
+ * (part of the persisted primary key, NOT NULL, but neither `key` nor
23
+ * `notNull` in the linked model) hold no NULL, so the placement is
24
+ * meaningless there and is dropped; the plain `ASC` / `DESC` then matches the
25
+ * primary key and the secondary indexes. Nullable columns keep the clause (the
26
+ * semantics stay SQLite's); an explicit `nulls` on the term is always kept.
27
+ * Installed once at start on the Postgres renderer class; a driver without the
28
+ * hook (or no Postgres driver at all) leaves everything as it is, with a
29
+ * warning when the deployment runs on Postgres.
30
+ */
31
+ const cds_1 = __importDefault(require("@sap/cds"));
32
+ const logger = cds_1.default.log('PgOrderNulls');
33
+ const INSTALLED = Symbol.for('odatano.pgOrderNulls');
34
+ /** Strips the null placement from the rendered terms whose column cannot be NULL. */
35
+ function stripNullsForNotNull(orderBy, rendered) {
36
+ return rendered.map((sql, i) => {
37
+ const c = orderBy[i];
38
+ if (!c || c.nulls)
39
+ return sql;
40
+ const el = c.element;
41
+ if (el && (el.key === true || el.notNull === true || el['@cds.valid.from'] === true))
42
+ return sql.replace(/ NULLS (FIRST|LAST)$/, '');
43
+ return sql;
44
+ });
45
+ }
46
+ /** Wraps the driver's `_orderBy` once; returns false when the driver or the hook is missing. */
47
+ function installPostgresOrderNulls() {
48
+ let PostgresService;
49
+ try {
50
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
51
+ PostgresService = require('@cap-js/postgres/lib/PostgresService');
52
+ }
53
+ catch (err) {
54
+ // No Postgres driver in this deployment, or a driver that no longer exposes
55
+ // the module: silent unless the database IS Postgres.
56
+ const dbKind = String((cds_1.default.env?.requires?.db)?.kind ?? '');
57
+ if (dbKind === 'postgres')
58
+ logger.warn(`@cap-js/postgres/lib/PostgresService not loadable, ORDER BY keeps the NULLS clause: ${String(err?.message ?? err)}`);
59
+ return false;
60
+ }
61
+ const proto = PostgresService.CQN2SQL?.prototype;
62
+ const orig = proto?._orderBy;
63
+ if (!proto || typeof orig !== 'function') {
64
+ logger.warn('@cap-js/postgres has no _orderBy hook; ORDER BY keeps the NULLS clause (indexes will not serve ordered reads)');
65
+ return false;
66
+ }
67
+ if (proto[INSTALLED])
68
+ return true;
69
+ proto._orderBy = function (orderBy, ...rest) {
70
+ return stripNullsForNotNull(orderBy, orig.call(this, orderBy, ...rest));
71
+ };
72
+ proto[INSTALLED] = true;
73
+ logger.info('ORDER BY on Postgres: NULLS clause dropped for key and NOT NULL columns');
74
+ return true;
75
+ }
76
+ //# sourceMappingURL=pg-order-nulls.js.map