@odatano/core 2.0.0-rc.16 → 2.0.0-rc.18
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 +1 -1
- package/srv/server.js +15 -8
- package/srv/utils/pg-order-nulls.d.ts +14 -0
- package/srv/utils/pg-order-nulls.js +76 -0
package/package.json
CHANGED
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");
|
|
@@ -77,6 +78,20 @@ let bootstrapError = null;
|
|
|
77
78
|
*/
|
|
78
79
|
async function initializeAppContext(config, protocolParams, hsmConfig) {
|
|
79
80
|
logger.debug('Initializing blockchain components...');
|
|
81
|
+
// Database first, on every start path: the standalone served hook and the
|
|
82
|
+
// plugin's initializeFromConfig both come through here (rc.17 had these in
|
|
83
|
+
// initializeFromConfig only, so the standalone image never ran them).
|
|
84
|
+
// ORDER BY on Postgres without a NULLS clause for key / NOT NULL columns, so
|
|
85
|
+
// the indexes serve `$top` and `$orderby` reads (srv/utils/pg-order-nulls.ts).
|
|
86
|
+
(0, pg_order_nulls_1.installPostgresOrderNulls)();
|
|
87
|
+
// The secondary indexes the model cannot declare (srv/utils/db-indexes.ts):
|
|
88
|
+
// idempotent, never fatal, before anything reads.
|
|
89
|
+
try {
|
|
90
|
+
await (0, db_indexes_1.ensureDbIndexes)();
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
logger.warn('secondary indexes not ensured (non-fatal):', err);
|
|
94
|
+
}
|
|
80
95
|
// Create CardanoClient from configuration
|
|
81
96
|
const cardanoClient = new cardano_client_1.CardanoClient(config);
|
|
82
97
|
// Publish the active network for leaf utilities (e.g. network-aware validators)
|
|
@@ -181,14 +196,6 @@ async function initializeFromConfig(config, protocolParams, hsmConfig) {
|
|
|
181
196
|
return;
|
|
182
197
|
}
|
|
183
198
|
hsmConfigInstance = hsmConfig;
|
|
184
|
-
// The secondary indexes the model cannot declare (srv/utils/db-indexes.ts):
|
|
185
|
-
// idempotent, never fatal, before anything reads.
|
|
186
|
-
try {
|
|
187
|
-
await (0, db_indexes_1.ensureDbIndexes)();
|
|
188
|
-
}
|
|
189
|
-
catch (err) {
|
|
190
|
-
logger.warn('secondary indexes not ensured (non-fatal):', err);
|
|
191
|
-
}
|
|
192
199
|
try {
|
|
193
200
|
appContext = await initializeAppContext(config, protocolParams, hsmConfig);
|
|
194
201
|
bootstrapError = null;
|
|
@@ -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
|