@tmlmobilidade/go-utils-sql 20260911.111.29

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.
@@ -0,0 +1,2 @@
1
+ export * from './registry.generated.js';
2
+ export * from './sql-path.js';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './registry.generated.js';
2
+ export * from './sql-path.js';
@@ -0,0 +1,12 @@
1
+ /**
2
+ * This file is auto-generated by the generate-sql-registry.sh script.
3
+ * Do not edit this file manually. Run `npm run repo:sql` to regenerate.
4
+ *
5
+ * Source: every .sql file under modules/<module>/sql
6
+ */
7
+ export declare const SQL_FILES: {
8
+ readonly hub: readonly ["eta/bootstrap/create-tables.sql", "eta/bootstrap/mv-predict-node-etas.sql", "eta/bootstrap/mv-predict-trip-stop-etas.sql", "eta/bootstrap/mv-sync-curr-vehicle-events.sql", "eta/cleanup/1-delete-out-of-window-curr-rides.sql", "eta/cleanup/2-delete-out-of-window-curr-vehicle-events.sql", "eta/cleanup/3-delete-orphan-curr-waypoints.sql", "eta/cleanup/4-delete-out-of-window-hist-rides.sql", "eta/cleanup/5-delete-orphan-hist-vehicle-events.sql", "eta/cleanup/6-delete-orphan-hist-node-travel-times.sql", "eta/cleanup/7-delete-out-of-window-hist-node-travel-times-aggregation.sql", "eta/loader/aggregate-hist-node-travel-times.sql", "eta/loader/build-hist-node-travel-times.sql", "eta/loader/load-current-waypoints.sql", "eta/loader/load-historical-vehicle-events.sql", "eta/loader/load-rides.sql", "eta/loader/snap-waypoints.sql", "publish-realtime/select-eta-by-stop-gtfs.sql", "publish-realtime/select-eta-by-stop.sql", "publish-realtime/select-eta-by-trip-gtfs.sql", "publish-realtime/select-eta-by-trip.sql", "publish-realtime/select-eta-gtfs.sql", "publish-realtime/select-eta.sql", "publish-realtime/select-vehicle-positions.sql"];
9
+ readonly operation: readonly ["alerts/list-lines.sql", "alerts/list-rides.sql", "alerts/list-stops.sql", "compose-alert/fetch-lines-reference-context.sql", "compose-alert/fetch-rides-reference-context.sql", "compose-alert/fetch-stops-reference-context.sql", "rides/get-ride.sql", "rides/list-rides.sql"];
10
+ readonly performance: readonly ["demand/by-agency-by-operational-date.sql"];
11
+ readonly tracker: readonly ["pt-tml-fertagus-rawdb-stream/find-trip-id.sql", "pt-tml-ml-api-fetch/find-hashed-shape.sql", "pt-tml-ml-api-fetch/find-hashed-trip.sql", "pt-tml-ml-api-fetch/find-rides-for-train.sql"];
12
+ };
@@ -0,0 +1,54 @@
1
+ /**
2
+ * This file is auto-generated by the generate-sql-registry.sh script.
3
+ * Do not edit this file manually. Run `npm run repo:sql` to regenerate.
4
+ *
5
+ * Source: every .sql file under modules/<module>/sql
6
+ */
7
+ /* * */
8
+ export const SQL_FILES = {
9
+ hub: [
10
+ 'eta/bootstrap/create-tables.sql',
11
+ 'eta/bootstrap/mv-predict-node-etas.sql',
12
+ 'eta/bootstrap/mv-predict-trip-stop-etas.sql',
13
+ 'eta/bootstrap/mv-sync-curr-vehicle-events.sql',
14
+ 'eta/cleanup/1-delete-out-of-window-curr-rides.sql',
15
+ 'eta/cleanup/2-delete-out-of-window-curr-vehicle-events.sql',
16
+ 'eta/cleanup/3-delete-orphan-curr-waypoints.sql',
17
+ 'eta/cleanup/4-delete-out-of-window-hist-rides.sql',
18
+ 'eta/cleanup/5-delete-orphan-hist-vehicle-events.sql',
19
+ 'eta/cleanup/6-delete-orphan-hist-node-travel-times.sql',
20
+ 'eta/cleanup/7-delete-out-of-window-hist-node-travel-times-aggregation.sql',
21
+ 'eta/loader/aggregate-hist-node-travel-times.sql',
22
+ 'eta/loader/build-hist-node-travel-times.sql',
23
+ 'eta/loader/load-current-waypoints.sql',
24
+ 'eta/loader/load-historical-vehicle-events.sql',
25
+ 'eta/loader/load-rides.sql',
26
+ 'eta/loader/snap-waypoints.sql',
27
+ 'publish-realtime/select-eta-by-stop-gtfs.sql',
28
+ 'publish-realtime/select-eta-by-stop.sql',
29
+ 'publish-realtime/select-eta-by-trip-gtfs.sql',
30
+ 'publish-realtime/select-eta-by-trip.sql',
31
+ 'publish-realtime/select-eta-gtfs.sql',
32
+ 'publish-realtime/select-eta.sql',
33
+ 'publish-realtime/select-vehicle-positions.sql',
34
+ ],
35
+ operation: [
36
+ 'alerts/list-lines.sql',
37
+ 'alerts/list-rides.sql',
38
+ 'alerts/list-stops.sql',
39
+ 'compose-alert/fetch-lines-reference-context.sql',
40
+ 'compose-alert/fetch-rides-reference-context.sql',
41
+ 'compose-alert/fetch-stops-reference-context.sql',
42
+ 'rides/get-ride.sql',
43
+ 'rides/list-rides.sql',
44
+ ],
45
+ performance: [
46
+ 'demand/by-agency-by-operational-date.sql',
47
+ ],
48
+ tracker: [
49
+ 'pt-tml-fertagus-rawdb-stream/find-trip-id.sql',
50
+ 'pt-tml-ml-api-fetch/find-hashed-shape.sql',
51
+ 'pt-tml-ml-api-fetch/find-hashed-trip.sql',
52
+ 'pt-tml-ml-api-fetch/find-rides-for-train.sql',
53
+ ],
54
+ };
@@ -0,0 +1,23 @@
1
+ import { SQL_FILES } from './registry.generated.js';
2
+ /**
3
+ * A module that ships SQL files under `modules/<module>/sql`.
4
+ */
5
+ export type SqlModule = keyof typeof SQL_FILES;
6
+ /**
7
+ * A SQL file shipped by a given module, relative to `modules/<module>/sql`.
8
+ */
9
+ export type SqlFile<T extends SqlModule> = (typeof SQL_FILES)[T][number] & string;
10
+ /**
11
+ * Returns the absolute path to a `.sql` file shipped by a module.
12
+ *
13
+ * Both arguments are checked against the generated registry, so a renamed or
14
+ * deleted SQL file fails at build time instead of at runtime.
15
+ *
16
+ * @param module The module that owns the SQL file.
17
+ * @param file The file path, relative to `modules/<module>/sql`.
18
+ * @returns The absolute path to the file.
19
+ *
20
+ * @example
21
+ * await labDb.queryFromFile(sqlPath('hub', 'eta/loader/load-rides.sql'));
22
+ */
23
+ export declare function sqlPath<T extends SqlModule>(module: T, file: SqlFile<T>): string;
@@ -0,0 +1,83 @@
1
+ /* * */
2
+ import { existsSync } from 'node:fs';
3
+ import path from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ /* * */
6
+ /**
7
+ * Overrides the directory that contains the `modules/` tree. Only meant for
8
+ * tests and for deployments that do not follow the standard layout.
9
+ */
10
+ const SQL_ROOT_ENV_VAR = 'GO_SQL_ROOT';
11
+ /**
12
+ * SQL files are plain assets, not workspace packages, so they cannot be found
13
+ * through Node module resolution. Instead this file walks up from its own
14
+ * location looking for a directory that holds `modules/<module>/sql`. The
15
+ * target of the search is also its own validation, so no marker file is needed.
16
+ *
17
+ * - Dev / monorepo: this file lives in `packages-new/utils/sql/{src,dist}`, and
18
+ * the walk stops at the repository root.
19
+ * - Docker runner (`nodejs.dockerfile`): this file lives in
20
+ * `/app/packages-new/utils/sql/dist`, and the walk stops at `/app`, where the
21
+ * Dockerfile copies `modules/<module>/sql`.
22
+ */
23
+ const selfDir = path.dirname(fileURLToPath(import.meta.url));
24
+ /** Resolved SQL roots, keyed by module name. */
25
+ const resolvedRoots = new Map();
26
+ /**
27
+ * Builds the list of directories to test, nearest first: the environment
28
+ * override, then every ancestor of this file, then the working directory.
29
+ */
30
+ function candidateBaseDirs() {
31
+ const baseDirs = [];
32
+ const override = process.env[SQL_ROOT_ENV_VAR];
33
+ if (override) {
34
+ baseDirs.push(path.resolve(override));
35
+ }
36
+ let currentDir = selfDir;
37
+ while (true) {
38
+ baseDirs.push(currentDir);
39
+ const parentDir = path.dirname(currentDir);
40
+ if (parentDir === currentDir)
41
+ break;
42
+ currentDir = parentDir;
43
+ }
44
+ baseDirs.push(path.resolve(process.cwd()));
45
+ return baseDirs;
46
+ }
47
+ /**
48
+ * Finds and caches the absolute path to `modules/<module>/sql`.
49
+ * @throws When no candidate directory holds the module's SQL folder.
50
+ */
51
+ function resolveModuleSqlRoot(module) {
52
+ const cachedRoot = resolvedRoots.get(module);
53
+ if (cachedRoot)
54
+ return cachedRoot;
55
+ const searchedPaths = [];
56
+ for (const baseDir of candidateBaseDirs()) {
57
+ const candidateRoot = path.join(baseDir, 'modules', module, 'sql');
58
+ searchedPaths.push(candidateRoot);
59
+ if (existsSync(candidateRoot)) {
60
+ resolvedRoots.set(module, candidateRoot);
61
+ return candidateRoot;
62
+ }
63
+ }
64
+ throw new Error(`Could not locate "modules/${module}/sql". Searched:\n ${searchedPaths.join('\n ')}\n`
65
+ + `Set ${SQL_ROOT_ENV_VAR} to the directory that contains the "modules" tree if it lives elsewhere.`);
66
+ }
67
+ /* * */
68
+ /**
69
+ * Returns the absolute path to a `.sql` file shipped by a module.
70
+ *
71
+ * Both arguments are checked against the generated registry, so a renamed or
72
+ * deleted SQL file fails at build time instead of at runtime.
73
+ *
74
+ * @param module The module that owns the SQL file.
75
+ * @param file The file path, relative to `modules/<module>/sql`.
76
+ * @returns The absolute path to the file.
77
+ *
78
+ * @example
79
+ * await labDb.queryFromFile(sqlPath('hub', 'eta/loader/load-rides.sql'));
80
+ */
81
+ export function sqlPath(module, file) {
82
+ return path.join(resolveModuleSqlRoot(module), file);
83
+ }
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@tmlmobilidade/go-utils-sql",
3
+ "version": "20260911.111.29",
4
+ "author": {
5
+ "email": "iso@tmlmobilidade.pt",
6
+ "name": "TML-ISO"
7
+ },
8
+ "license": "AGPL-3.0-or-later",
9
+ "homepage": "https://go.tmlmobilidade.pt",
10
+ "bugs": {
11
+ "url": "https://github.com/tmlmobilidade/go/issues"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/tmlmobilidade/go.git"
16
+ },
17
+ "keywords": [
18
+ "public transit",
19
+ "tml",
20
+ "transportes metropolitanos de lisboa",
21
+ "go"
22
+ ],
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "type": "module",
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "main": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "scripts": {
33
+ "build": "tsc && resolve-tspaths",
34
+ "lint": "eslint ./src/ && tsc --noEmit",
35
+ "lint:fix": "eslint ./src/ --fix",
36
+ "watch": "tsc-watch --onSuccess 'resolve-tspaths'"
37
+ },
38
+ "devDependencies": {
39
+ "@tmlmobilidade/go-utils-tsconfig": "*",
40
+ "@types/node": "26.4.0",
41
+ "resolve-tspaths": "0.8.23",
42
+ "tsc-watch": "7.2.1",
43
+ "typescript": "6.0.3"
44
+ }
45
+ }