@tmlmobilidade/databases 20260323.400.54

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.
Files changed (57) hide show
  1. package/dist/clients/go-clickhouse.d.ts +30 -0
  2. package/dist/clients/go-clickhouse.js +110 -0
  3. package/dist/clients/go-mongo.d.ts +30 -0
  4. package/dist/clients/go-mongo.js +117 -0
  5. package/dist/clients/index.d.ts +5 -0
  6. package/dist/clients/index.js +5 -0
  7. package/dist/clients/pcgidb-ticketing.d.ts +30 -0
  8. package/dist/clients/pcgidb-ticketing.js +117 -0
  9. package/dist/clients/pcgidb-validations.d.ts +29 -0
  10. package/dist/clients/pcgidb-validations.js +116 -0
  11. package/dist/clients/rawdb.d.ts +29 -0
  12. package/dist/clients/rawdb.js +116 -0
  13. package/dist/index.d.ts +4 -0
  14. package/dist/index.js +4 -0
  15. package/dist/interfaces/index.d.ts +2 -0
  16. package/dist/interfaces/index.js +2 -0
  17. package/dist/interfaces/simplified-apex/index.d.ts +4 -0
  18. package/dist/interfaces/simplified-apex/index.js +4 -0
  19. package/dist/interfaces/simplified-apex/simplified-apex-locations.d.ts +38 -0
  20. package/dist/interfaces/simplified-apex/simplified-apex-locations.js +57 -0
  21. package/dist/interfaces/simplified-apex/simplified-apex-on-board-refunds.d.ts +48 -0
  22. package/dist/interfaces/simplified-apex/simplified-apex-on-board-refunds.js +67 -0
  23. package/dist/interfaces/simplified-apex/simplified-apex-on-board-sales.d.ts +49 -0
  24. package/dist/interfaces/simplified-apex/simplified-apex-on-board-sales.js +68 -0
  25. package/dist/interfaces/simplified-apex/simplified-apex-validations.d.ts +47 -0
  26. package/dist/interfaces/simplified-apex/simplified-apex-validations.js +67 -0
  27. package/dist/interfaces/simplified-vehicle-events/index.d.ts +1 -0
  28. package/dist/interfaces/simplified-vehicle-events/index.js +1 -0
  29. package/dist/interfaces/simplified-vehicle-events/simplified-vehicle-events.d.ts +39 -0
  30. package/dist/interfaces/simplified-vehicle-events/simplified-vehicle-events.js +61 -0
  31. package/dist/templates/clickhouse.d.ts +107 -0
  32. package/dist/templates/clickhouse.js +201 -0
  33. package/dist/types/clickhouse/column.d.ts +36 -0
  34. package/dist/types/clickhouse/column.js +2 -0
  35. package/dist/types/clickhouse/data-types.d.ts +5 -0
  36. package/dist/types/clickhouse/data-types.js +1 -0
  37. package/dist/types/clickhouse/index.d.ts +3 -0
  38. package/dist/types/clickhouse/index.js +3 -0
  39. package/dist/types/clickhouse/table-engines.d.ts +7 -0
  40. package/dist/types/clickhouse/table-engines.js +1 -0
  41. package/dist/types/index.d.ts +1 -0
  42. package/dist/types/index.js +1 -0
  43. package/dist/utils/get-clickhouse-param-type.d.ts +7 -0
  44. package/dist/utils/get-clickhouse-param-type.js +16 -0
  45. package/dist/utils/index.d.ts +3 -0
  46. package/dist/utils/index.js +3 -0
  47. package/dist/utils/prepare-named-query-params.d.ts +16 -0
  48. package/dist/utils/prepare-named-query-params.js +47 -0
  49. package/dist/utils/prepare-positional-query-params.d.ts +13 -0
  50. package/dist/utils/prepare-positional-query-params.js +36 -0
  51. package/dist/utils/query-from-file.d.ts +17 -0
  52. package/dist/utils/query-from-file.js +42 -0
  53. package/dist/utils/query-from-string.d.ts +14 -0
  54. package/dist/utils/query-from-string.js +31 -0
  55. package/dist/utils/validate-sql-param.d.ts +12 -0
  56. package/dist/utils/validate-sql-param.js +23 -0
  57. package/package.json +53 -0
@@ -0,0 +1,42 @@
1
+ /* * */
2
+ import { prepareNamedQueryParams } from './prepare-named-query-params.js';
3
+ import { Logger } from '@tmlmobilidade/logger';
4
+ import { readFile } from 'node:fs/promises';
5
+ /**
6
+ * Executes a query from a .sql file with optional parameter substitutions.
7
+ * @param client The ClickHouse client to use for executing the query.
8
+ * @param filePath Absolute or relative path to the .sql file.
9
+ * @param params Optional key-value substitutions applied to the query (replaces {key} placeholders).
10
+ * @returns Query result rows typed as `T`.
11
+ * @example
12
+ * // Given a SQL file "get_users.sql" with the content:
13
+ * // SELECT * FROM users WHERE created_at >= {start_date} AND created_at <= {end_date}
14
+ *
15
+ * const users = await clickhouseService.queryFromFile<User>('get_users.sql', {
16
+ * start_date: '2024-01-01',
17
+ * end_date: '2024-12-31',
18
+ * });
19
+ */
20
+ export async function queryFromFile(client, filePath, params) {
21
+ let sql;
22
+ try {
23
+ sql = await readFile(filePath, { encoding: 'utf-8' });
24
+ }
25
+ catch (error) {
26
+ Logger.error(`CLICKHOUSE: Error @ queryFromFile(): Failed to read SQL file "${filePath}": ${error.message}`);
27
+ throw error;
28
+ }
29
+ const { query, queryParams } = prepareNamedQueryParams(sql, params, filePath);
30
+ try {
31
+ const result = await client.query({
32
+ format: 'JSONEachRow',
33
+ query,
34
+ query_params: queryParams,
35
+ });
36
+ return result.json();
37
+ }
38
+ catch (error) {
39
+ Logger.error(`CLICKHOUSE: Error @ queryFromFile(): Failed to execute query from file "${filePath}": ${error.message}`);
40
+ throw error;
41
+ }
42
+ }
@@ -0,0 +1,14 @@
1
+ import { type ClickHouseClient } from '@clickhouse/client';
2
+ /**
3
+ * Executes a query from a string.
4
+ * @param client The ClickHouse client to use for executing the query.
5
+ * @param query The SQL query to execute, with optional {key} placeholders for parameters.
6
+ * @param params Optional key-value substitutions applied to the query (replaces {key} placeholders).
7
+ * @returns Query result rows typed as `T`.
8
+ * @example
9
+ * const users = await queryFromString<User>(clickhouseClient,
10
+ * 'SELECT * FROM users WHERE created_at >= {start_date} AND created_at <= {end_date}',
11
+ * { start_date: '2024-01-01', end_date: '2024-12-31' }
12
+ * );
13
+ */
14
+ export declare function queryFromString<T>(client: ClickHouseClient, query: string, params?: Record<string, number | string>): Promise<T[]>;
@@ -0,0 +1,31 @@
1
+ /* * */
2
+ import { preparePositionalQueryParams } from './prepare-positional-query-params.js';
3
+ import { Logger } from '@tmlmobilidade/logger';
4
+ /**
5
+ * Executes a query from a string.
6
+ * @param client The ClickHouse client to use for executing the query.
7
+ * @param query The SQL query to execute, with optional {key} placeholders for parameters.
8
+ * @param params Optional key-value substitutions applied to the query (replaces {key} placeholders).
9
+ * @returns Query result rows typed as `T`.
10
+ * @example
11
+ * const users = await queryFromString<User>(clickhouseClient,
12
+ * 'SELECT * FROM users WHERE created_at >= {start_date} AND created_at <= {end_date}',
13
+ * { start_date: '2024-01-01', end_date: '2024-12-31' }
14
+ * );
15
+ */
16
+ export async function queryFromString(client, query, params) {
17
+ // Validate query param keys and prepare the query statement
18
+ const preparedQuery = preparePositionalQueryParams(query, params);
19
+ try {
20
+ const result = await client.query({
21
+ format: 'JSONEachRow',
22
+ query: preparedQuery.query,
23
+ query_params: preparedQuery.query_params,
24
+ });
25
+ return result.json();
26
+ }
27
+ catch (error) {
28
+ Logger.error(`CLICKHOUSE: Error @ queryFromString(): Failed to execute query "${query}": ${error.message}`);
29
+ throw error;
30
+ }
31
+ }
@@ -0,0 +1,12 @@
1
+ export declare const safeSqlParamRegex: RegExp;
2
+ /**
3
+ * Validates that a given string is a safe SQL query parameter key,
4
+ * which must start with a letter or underscore and can only contain letters,
5
+ * numbers, or underscores. This is important to prevent SQL injection vulnerabilities
6
+ * when using dynamic query parameters.
7
+ * @param key The query parameter key to validate.
8
+ * @param throwOnInvalid Whether to throw an error if the key is invalid (default is true).
9
+ * @throws Will throw an error if the key is invalid and throwOnInvalid is true.
10
+ * @returns The validated query parameter key, or false if invalid and throwOnInvalid is false.
11
+ */
12
+ export declare function validateSqlParam(key: string, throwOnInvalid?: boolean): false | string;
@@ -0,0 +1,23 @@
1
+ /* * */
2
+ export const safeSqlParamRegex = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
3
+ /**
4
+ * Validates that a given string is a safe SQL query parameter key,
5
+ * which must start with a letter or underscore and can only contain letters,
6
+ * numbers, or underscores. This is important to prevent SQL injection vulnerabilities
7
+ * when using dynamic query parameters.
8
+ * @param key The query parameter key to validate.
9
+ * @param throwOnInvalid Whether to throw an error if the key is invalid (default is true).
10
+ * @throws Will throw an error if the key is invalid and throwOnInvalid is true.
11
+ * @returns The validated query parameter key, or false if invalid and throwOnInvalid is false.
12
+ */
13
+ export function validateSqlParam(key, throwOnInvalid = true) {
14
+ // Check if the key matches the safe SQL parameter regex
15
+ if (safeSqlParamRegex.test(key))
16
+ return key;
17
+ // If the key is invalid, either throw an error
18
+ // or return false based on the throwOnInvalid flag
19
+ if (throwOnInvalid)
20
+ throw new Error(`Invalid query parameter key: ${key}. Keys must start with a letter or underscore, followed by letters, numbers, or underscores.`);
21
+ else
22
+ return false;
23
+ }
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@tmlmobilidade/databases",
3
+ "version": "20260323.400.54",
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
+ "dependencies": {
39
+ "@clickhouse/client": "1.18.2",
40
+ "@tmlmobilidade/logger": "*",
41
+ "@tmlmobilidade/ssh": "*",
42
+ "@tmlmobilidade/types": "*",
43
+ "@tmlmobilidade/utils": "*",
44
+ "mongodb": "7.1.0"
45
+ },
46
+ "devDependencies": {
47
+ "@tmlmobilidade/tsconfig": "*",
48
+ "@types/node": "25.5.0",
49
+ "resolve-tspaths": "0.8.23",
50
+ "tsc-watch": "7.2.0",
51
+ "typescript": "5.9.3"
52
+ }
53
+ }