@digitraffic/common 2025.12.4-1 → 2025.12.15-2

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.
@@ -8,6 +8,8 @@ import { EnvKeys } from "../../aws/runtime/environment.js";
8
8
  import { TrafficType } from "../../types/traffictype.js";
9
9
  const TEST_ENV_VAR = "TEST_ENV_VAR";
10
10
  const TEST_ENV_VALUE = "testValue";
11
+ // AWS::Lambda::Function Template Reference
12
+ // https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-lambda-function.html
11
13
  describe("FunctionBuilder test", () => {
12
14
  function createTemplate(tester, plain = false) {
13
15
  const app = new App();
@@ -91,7 +93,6 @@ describe("FunctionBuilder test", () => {
91
93
  const template = createTemplate((builder) => {
92
94
  builder.withDescription("Does something useful");
93
95
  });
94
- console.debug("JSON:", template.toJSON());
95
96
  template.hasResourceProperties("AWS::Lambda::Function", {
96
97
  Description: "Does something useful",
97
98
  });
@@ -46,8 +46,9 @@ export declare class FunctionBuilder {
46
46
  withAssetCode(path?: string): this;
47
47
  withCode(code: Code): this;
48
48
  /**
49
- * Use given handler(${name}.handler) to run the lambda. Default value is lambdaname.
49
+ * Use given handler(${name}.handler) to run the lambda. Default value is lambdaName.
50
50
  * @param name
51
+ * @param handlerFunctionName
51
52
  * @returns
52
53
  */
53
54
  withHandler(name: string, handlerFunctionName?: string): this;
@@ -79,8 +79,9 @@ export class FunctionBuilder {
79
79
  return this;
80
80
  }
81
81
  /**
82
- * Use given handler(${name}.handler) to run the lambda. Default value is lambdaname.
82
+ * Use given handler(${name}.handler) to run the lambda. Default value is lambdaName.
83
83
  * @param name
84
+ * @param handlerFunctionName
84
85
  * @returns
85
86
  */
86
87
  withHandler(name, handlerFunctionName = "handler") {
@@ -0,0 +1,13 @@
1
+ import type { IDatabase, ITask } from "pg-promise";
2
+ export declare enum DatabaseEnvironmentKeys {
3
+ DB_USER = "DB_USER",
4
+ DB_PASS = "DB_PASS",
5
+ DB_URI = "DB_URI",
6
+ DB_RO_URI = "DB_RO_URI",
7
+ DB_APPLICATION = "DB_APPLICATION"
8
+ }
9
+ export type DTDatabase = IDatabase<unknown>;
10
+ export type DTTransaction = ITask<unknown>;
11
+ export declare function inTransaction<T>(fn: (db: DTTransaction) => Promise<T>, convertNullsToUndefined?: boolean): Promise<T>;
12
+ export declare function inDatabase<T>(fn: (db: DTDatabase) => Promise<T>, convertNullsToUndefined?: boolean): Promise<T>;
13
+ export declare function inDatabaseReadonly<T>(fn: (db: DTDatabase) => Promise<T>, convertNullsToUndefined?: boolean): Promise<T>;
@@ -0,0 +1,72 @@
1
+ import { logger } from "../aws/runtime/dt-logger-default.js";
2
+ import { logException } from "../utils/logging.js";
3
+ import { getEnvVariable, getEnvVariableOrElse } from "../utils/utils.js";
4
+ import { initDbConnection } from "./database.js";
5
+ export var DatabaseEnvironmentKeys;
6
+ (function (DatabaseEnvironmentKeys) {
7
+ DatabaseEnvironmentKeys["DB_USER"] = "DB_USER";
8
+ DatabaseEnvironmentKeys["DB_PASS"] = "DB_PASS";
9
+ DatabaseEnvironmentKeys["DB_URI"] = "DB_URI";
10
+ DatabaseEnvironmentKeys["DB_RO_URI"] = "DB_RO_URI";
11
+ DatabaseEnvironmentKeys["DB_APPLICATION"] = "DB_APPLICATION";
12
+ })(DatabaseEnvironmentKeys || (DatabaseEnvironmentKeys = {}));
13
+ const dbSingletons = new Map();
14
+ function getDbSingleton(readonly, convertNullsToUndefined, options) {
15
+ const username = getEnvVariable(DatabaseEnvironmentKeys.DB_USER);
16
+ const password = getEnvVariable(DatabaseEnvironmentKeys.DB_PASS);
17
+ const dbUrl = readonly
18
+ ? getEnvVariable(DatabaseEnvironmentKeys.DB_RO_URI)
19
+ : getEnvVariable(DatabaseEnvironmentKeys.DB_URI);
20
+ const applicationName = getEnvVariableOrElse(DatabaseEnvironmentKeys.DB_APPLICATION, "unknown-cdk-application");
21
+ const key = JSON.stringify({
22
+ username,
23
+ password,
24
+ applicationName,
25
+ url: dbUrl,
26
+ convertNullsToUndefined,
27
+ options,
28
+ });
29
+ let db = dbSingletons.get(key);
30
+ if (!db) {
31
+ db = initDbConnection(username, password, applicationName, dbUrl, convertNullsToUndefined, options);
32
+ dbSingletons.set(key, db);
33
+ }
34
+ return db;
35
+ }
36
+ // noinspection JSUnusedGlobalSymbols
37
+ export function inTransaction(fn, convertNullsToUndefined = false) {
38
+ return inDatabase((db) => db.tx((t) => fn(t)), convertNullsToUndefined);
39
+ }
40
+ export function inDatabase(fn, convertNullsToUndefined = false) {
41
+ return doInDatabase(false, fn, convertNullsToUndefined);
42
+ }
43
+ export function inDatabaseReadonly(fn, convertNullsToUndefined = false) {
44
+ return doInDatabase(true, fn, convertNullsToUndefined);
45
+ }
46
+ async function doInDatabase(readonly, fn, convertNullsToUndefined) {
47
+ const db = getDbSingleton(readonly, convertNullsToUndefined);
48
+ try {
49
+ // deallocate all prepared statements to allow for connection pooling
50
+ // DISCARD instead of DEALLOCATE as it didn't always clean all prepared statements
51
+ await db.none("DISCARD ALL");
52
+ return await fn(db);
53
+ }
54
+ catch (e) {
55
+ logException(logger, e);
56
+ throw e;
57
+ }
58
+ finally {
59
+ await db.$pool.end();
60
+ }
61
+ }
62
+ // function convertNullColumnsToUndefined(rows: Record<string, unknown>[]) {
63
+ // rows.forEach((row) => {
64
+ // for (const column in row) {
65
+ // const columnValue = row[column];
66
+ // if (columnValue === null) {
67
+ // row[column] = undefined;
68
+ // }
69
+ // }
70
+ // });
71
+ // }
72
+ //# sourceMappingURL=database-singleton.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitraffic/common",
3
- "version": "2025.12.4-1",
3
+ "version": "2025.12.15-2",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "repository": {
@@ -11,6 +11,7 @@
11
11
  "type": "module",
12
12
  "exports": {
13
13
  ".": "./dist/index.js",
14
+ "./dist/database/database-singleton": "./dist/database/database-singleton.js",
14
15
  "./dist/database/database": "./dist/database/database.js",
15
16
  "./dist/database/cached": "./dist/database/cached.js",
16
17
  "./dist/database/models": "./dist/database/models.js",