@ikonintegration/ikapi 4.0.1 → 5.0.0

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 (236) hide show
  1. package/.eslintignore +3 -0
  2. package/.eslintrc.cjs +81 -0
  3. package/.github/workflows/npmpublish.yml +8 -19
  4. package/.github/workflows/prs.yml +12 -0
  5. package/README.md +89 -99
  6. package/dist/index.d.ts +16 -0
  7. package/dist/index.js +27 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/package-lock.json +11881 -0
  10. package/dist/package.json +81 -0
  11. package/dist/src/API/Request.d.ts +125 -0
  12. package/dist/src/API/Request.js +185 -0
  13. package/dist/src/API/Request.js.map +1 -0
  14. package/dist/src/API/Response.d.ts +188 -0
  15. package/dist/src/API/Response.js +270 -0
  16. package/dist/src/API/Response.js.map +1 -0
  17. package/dist/src/BaseEvent/DynamoTransaction.d.ts +70 -0
  18. package/dist/src/BaseEvent/DynamoTransaction.js +104 -0
  19. package/dist/src/BaseEvent/DynamoTransaction.js.map +1 -0
  20. package/dist/src/BaseEvent/EventProcessor.d.ts +58 -0
  21. package/dist/src/BaseEvent/EventProcessor.js +101 -0
  22. package/dist/src/BaseEvent/EventProcessor.js.map +1 -0
  23. package/dist/src/BaseEvent/Process.d.ts +50 -0
  24. package/dist/src/BaseEvent/Process.js +64 -0
  25. package/dist/src/BaseEvent/Process.js.map +1 -0
  26. package/dist/src/BaseEvent/StepTransaction.d.ts +23 -0
  27. package/dist/src/BaseEvent/StepTransaction.js +27 -0
  28. package/dist/src/BaseEvent/StepTransaction.js.map +1 -0
  29. package/dist/src/BaseEvent/Transaction.d.ts +149 -0
  30. package/dist/src/BaseEvent/Transaction.js +224 -0
  31. package/dist/src/BaseEvent/Transaction.js.map +1 -0
  32. package/dist/src/Cache/Redis.d.ts +29 -0
  33. package/dist/src/Cache/Redis.js +80 -0
  34. package/dist/src/Cache/Redis.js.map +1 -0
  35. package/dist/src/Cache/types.d.ts +31 -0
  36. package/dist/src/Cache/types.js +2 -0
  37. package/dist/src/Cache/types.js.map +1 -0
  38. package/dist/src/Config/Configuration.d.ts +123 -0
  39. package/dist/src/Config/Configuration.js +109 -0
  40. package/dist/src/Config/Configuration.js.map +1 -0
  41. package/dist/src/Config/EnvironmentVar.d.ts +74 -0
  42. package/dist/src/Config/EnvironmentVar.js +138 -0
  43. package/dist/src/Config/EnvironmentVar.js.map +1 -0
  44. package/dist/src/Crypto/Crypto.d.ts +45 -0
  45. package/dist/src/Crypto/Crypto.js +72 -0
  46. package/dist/src/Crypto/Crypto.js.map +1 -0
  47. package/dist/src/Database/Database.d.ts +21 -0
  48. package/dist/src/Database/Database.js +15 -0
  49. package/dist/src/Database/Database.js.map +1 -0
  50. package/dist/src/Database/DatabaseManager.d.ts +47 -0
  51. package/dist/src/Database/DatabaseManager.js +60 -0
  52. package/dist/src/Database/DatabaseManager.js.map +1 -0
  53. package/dist/src/Database/DatabaseTransaction.d.ts +101 -0
  54. package/dist/src/Database/DatabaseTransaction.js +126 -0
  55. package/dist/src/Database/DatabaseTransaction.js.map +1 -0
  56. package/dist/src/Database/index.d.ts +10 -0
  57. package/dist/src/Database/index.js +15 -0
  58. package/dist/src/Database/index.js.map +1 -0
  59. package/dist/src/Database/integrations/dynamo/DynamoDatabase.d.ts +35 -0
  60. package/dist/src/Database/integrations/dynamo/DynamoDatabase.js +59 -0
  61. package/dist/src/Database/integrations/dynamo/DynamoDatabase.js.map +1 -0
  62. package/dist/src/Database/integrations/kysely/KyselyDatabase.d.ts +66 -0
  63. package/dist/src/Database/integrations/kysely/KyselyDatabase.js +86 -0
  64. package/dist/src/Database/integrations/kysely/KyselyDatabase.js.map +1 -0
  65. package/dist/src/Database/integrations/kysely/KyselyTransaction.d.ts +70 -0
  66. package/dist/src/Database/integrations/kysely/KyselyTransaction.js +118 -0
  67. package/dist/src/Database/integrations/kysely/KyselyTransaction.js.map +1 -0
  68. package/dist/src/Database/integrations/pgsql/PostgresDatabase.d.ts +36 -0
  69. package/dist/src/Database/integrations/pgsql/PostgresDatabase.js +54 -0
  70. package/dist/src/Database/integrations/pgsql/PostgresDatabase.js.map +1 -0
  71. package/dist/src/Database/integrations/pgsql/PostgresTransaction.d.ts +63 -0
  72. package/dist/src/Database/integrations/pgsql/PostgresTransaction.js +61 -0
  73. package/dist/src/Database/integrations/pgsql/PostgresTransaction.js.map +1 -0
  74. package/dist/src/Database/types.d.ts +76 -0
  75. package/dist/src/Database/types.js +2 -0
  76. package/dist/src/Database/types.js.map +1 -0
  77. package/dist/src/Globals.d.ts +93 -0
  78. package/dist/src/Globals.js +99 -0
  79. package/dist/src/Globals.js.map +1 -0
  80. package/dist/src/Logger/Logger.d.ts +161 -0
  81. package/dist/src/Logger/Logger.js +299 -0
  82. package/dist/src/Logger/Logger.js.map +1 -0
  83. package/dist/src/Mailer/Mailer.d.ts +78 -0
  84. package/dist/src/Mailer/Mailer.js +182 -0
  85. package/dist/src/Mailer/Mailer.js.map +1 -0
  86. package/dist/src/Publisher/Publisher.d.ts +39 -0
  87. package/dist/src/Publisher/Publisher.js +77 -0
  88. package/dist/src/Publisher/Publisher.js.map +1 -0
  89. package/dist/src/Server/RouteResolver.d.ts +33 -0
  90. package/dist/src/Server/RouteResolver.js +100 -0
  91. package/dist/src/Server/RouteResolver.js.map +1 -0
  92. package/dist/src/Server/Router.d.ts +157 -0
  93. package/dist/src/Server/Router.js +32 -0
  94. package/dist/src/Server/Router.js.map +1 -0
  95. package/dist/src/Server/lib/ContainerServer.d.ts +42 -0
  96. package/dist/src/Server/lib/ContainerServer.js +66 -0
  97. package/dist/src/Server/lib/ContainerServer.js.map +1 -0
  98. package/dist/src/Server/lib/Server.d.ts +45 -0
  99. package/dist/src/Server/lib/Server.js +93 -0
  100. package/dist/src/Server/lib/Server.js.map +1 -0
  101. package/dist/src/Server/lib/container/GenericHandler.d.ts +9 -0
  102. package/dist/src/Server/lib/container/GenericHandler.js +82 -0
  103. package/dist/src/Server/lib/container/GenericHandler.js.map +1 -0
  104. package/dist/src/Server/lib/container/GenericHandlerEvent.d.ts +52 -0
  105. package/dist/src/Server/lib/container/GenericHandlerEvent.js +132 -0
  106. package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +1 -0
  107. package/dist/src/Server/lib/container/HealthHandler.d.ts +9 -0
  108. package/dist/src/Server/lib/container/HealthHandler.js +19 -0
  109. package/dist/src/Server/lib/container/HealthHandler.js.map +1 -0
  110. package/dist/src/Server/lib/container/Proxy.d.ts +67 -0
  111. package/dist/src/Server/lib/container/Proxy.js +143 -0
  112. package/dist/src/Server/lib/container/Proxy.js.map +1 -0
  113. package/dist/src/Server/lib/container/Utils.d.ts +14 -0
  114. package/dist/src/Server/lib/container/Utils.js +37 -0
  115. package/dist/src/Server/lib/container/Utils.js.map +1 -0
  116. package/dist/src/Util/AsyncSingleton.d.ts +31 -0
  117. package/dist/src/Util/AsyncSingleton.js +83 -0
  118. package/dist/src/Util/AsyncSingleton.js.map +1 -0
  119. package/dist/src/Util/Utils.d.ts +61 -0
  120. package/dist/src/Util/Utils.js +147 -0
  121. package/dist/src/Util/Utils.js.map +1 -0
  122. package/dist/src/Validation/Validator.d.ts +17 -0
  123. package/dist/src/Validation/Validator.js +39 -0
  124. package/dist/src/Validation/Validator.js.map +1 -0
  125. package/dist/tsconfig.tsbuildinfo +1 -0
  126. package/index.ts +41 -0
  127. package/jest.config.ts +37 -0
  128. package/jest.smoke.config.ts +34 -0
  129. package/package.json +66 -22
  130. package/src/API/Request.ts +214 -0
  131. package/src/API/Response.ts +370 -0
  132. package/src/BaseEvent/DynamoTransaction.ts +175 -0
  133. package/src/BaseEvent/EventProcessor.ts +140 -0
  134. package/src/BaseEvent/Process.ts +78 -0
  135. package/src/BaseEvent/StepTransaction.ts +35 -0
  136. package/src/BaseEvent/Transaction.ts +323 -0
  137. package/src/Cache/Redis.ts +89 -0
  138. package/src/Cache/types.ts +33 -0
  139. package/src/Config/Configuration.ts +199 -0
  140. package/src/Config/EnvironmentVar.ts +142 -0
  141. package/src/Crypto/Crypto.ts +89 -0
  142. package/src/Database/Database.ts +22 -0
  143. package/src/Database/DatabaseManager.ts +67 -0
  144. package/src/Database/DatabaseTransaction.ts +170 -0
  145. package/src/Database/index.ts +27 -0
  146. package/src/Database/integrations/dynamo/DynamoDatabase.ts +58 -0
  147. package/src/Database/integrations/kysely/KyselyDatabase.ts +99 -0
  148. package/src/Database/integrations/kysely/KyselyTransaction.ts +172 -0
  149. package/src/Database/integrations/pgsql/PostgresDatabase.ts +56 -0
  150. package/src/Database/integrations/pgsql/PostgresTransaction.ts +87 -0
  151. package/src/Database/types.ts +85 -0
  152. package/src/Globals.ts +103 -0
  153. package/src/Logger/Logger.ts +363 -0
  154. package/src/Mailer/Mailer.ts +217 -0
  155. package/src/Publisher/Publisher.ts +96 -0
  156. package/src/Server/RouteResolver.ts +124 -0
  157. package/src/Server/Router.ts +200 -0
  158. package/src/Server/lib/ContainerServer.ts +65 -0
  159. package/src/Server/lib/Server.ts +109 -0
  160. package/src/Server/lib/container/GenericHandler.ts +76 -0
  161. package/src/Server/lib/container/GenericHandlerEvent.ts +154 -0
  162. package/src/Server/lib/container/HealthHandler.ts +11 -0
  163. package/src/Server/lib/container/Proxy.ts +172 -0
  164. package/src/Server/lib/container/Utils.ts +33 -0
  165. package/src/Util/AsyncSingleton.ts +86 -0
  166. package/src/Util/Utils.ts +131 -0
  167. package/src/Validation/Validator.ts +45 -0
  168. package/tests/API/Request.test.ts +273 -0
  169. package/tests/API/Response.test.ts +367 -0
  170. package/tests/BaseEvent/DynamoTransaction.test.ts +272 -0
  171. package/tests/BaseEvent/EventProcessor.test.ts +263 -0
  172. package/tests/BaseEvent/Process.test.ts +47 -0
  173. package/tests/BaseEvent/StepTransaction.test.ts +44 -0
  174. package/tests/BaseEvent/Transaction.test.ts +402 -0
  175. package/tests/Cache/Redis-client.test.ts +90 -0
  176. package/tests/Cache/Redis-cluster.test.ts +100 -0
  177. package/tests/Config/Config.test.ts +205 -0
  178. package/tests/Config/EnvironmentVar.test.ts +251 -0
  179. package/tests/Crypto/Crypto.test.ts +88 -0
  180. package/tests/Database/DatabaseManager.test.ts +79 -0
  181. package/tests/Database/integrations/dynamo/DynamoDatabase.test.ts +44 -0
  182. package/tests/Database/integrations/kysely/KyselyDatabase.test.ts +113 -0
  183. package/tests/Database/integrations/kysely/KyselyTransaction.test.ts +119 -0
  184. package/tests/Database/integrations/pg/PostgresDatabase.test.ts +76 -0
  185. package/tests/Database/integrations/pg/PostgresTransaction.test.ts +118 -0
  186. package/tests/Logger/Logger.test.ts +215 -0
  187. package/tests/Mailer/Mailer.test.ts +59 -0
  188. package/tests/Publisher/Publisher.test.ts +60 -0
  189. package/tests/Server/RouteResolver.test.ts +116 -0
  190. package/tests/Server/Router.test.ts +39 -0
  191. package/tests/Server/lib/ContainerServer.test.ts +531 -0
  192. package/tests/Server/lib/Server.test.ts +12 -0
  193. package/tests/Server/lib/container/GenericHandler.test.ts +131 -0
  194. package/tests/Server/lib/container/GenericHandlerEvent.test.ts +103 -0
  195. package/tests/Server/lib/container/HealthHandler.test.ts +30 -0
  196. package/tests/Server/lib/container/Proxy.test.ts +268 -0
  197. package/tests/Server/lib/container/Utils.test.ts +47 -0
  198. package/tests/Test.utils.ts +78 -0
  199. package/tests/Utils/Utils.test.ts +229 -0
  200. package/tests/Validation/Validator.test.ts +82 -0
  201. package/tsconfig.json +26 -0
  202. package/tsconfig.smoke.json +26 -0
  203. package/index.js +0 -88
  204. package/src/API/IKRequest.js +0 -52
  205. package/src/API/IKResponse.js +0 -119
  206. package/src/API/IKUtils.js +0 -51
  207. package/src/BaseEvent/IKProcess.js +0 -77
  208. package/src/BaseEvent/IKTransaction.js +0 -139
  209. package/src/Cache/Prototype/IKCache.js +0 -17
  210. package/src/Cache/Redis/IKRedis.js +0 -148
  211. package/src/Database/DDB/IKDB.js +0 -56
  212. package/src/Database/DDB/IKDBBaseExpression.js +0 -130
  213. package/src/Database/DDB/IKDBBaseQuery.js +0 -151
  214. package/src/Database/DDB/IKDBQueryBatchGet.js +0 -37
  215. package/src/Database/DDB/IKDBQueryBatchWrite.js +0 -64
  216. package/src/Database/DDB/IKDBQueryDelete.js +0 -34
  217. package/src/Database/DDB/IKDBQueryGet.js +0 -48
  218. package/src/Database/DDB/IKDBQueryPut.js +0 -87
  219. package/src/Database/DDB/IKDBQueryScan.js +0 -45
  220. package/src/Database/DDB/IKDBQueryTransactionalWrite.js +0 -69
  221. package/src/Database/DDB/IKDBQueryUpdate.js +0 -221
  222. package/src/Database/DDB/_IKDBQueryTransactionalRead.js +0 -46
  223. package/src/Database/PSQL/IKDB.js +0 -41
  224. package/src/Database/PSQL/IKDBBaseQuery.js +0 -26
  225. package/src/Database/Prototype/IKDB.js +0 -21
  226. package/src/Database/Prototype/IKDBBaseQuery.js +0 -14
  227. package/src/IKDynamoStream.js +0 -42
  228. package/src/IKEventProcessor.js +0 -42
  229. package/src/IKGlobals.js +0 -24
  230. package/src/IKRouter.js +0 -47
  231. package/src/IKStepTransaction.js +0 -14
  232. package/src/Logger/IKLogger.js +0 -136
  233. package/src/Mailer/IKMailer.js +0 -69
  234. package/src/Publisher/IKPublisher.js +0 -44
  235. package/src/Tracker/IKExecutionTracker.js +0 -79
  236. package/src/Validation/IKValidation.js +0 -76
@@ -0,0 +1,27 @@
1
+ import { Database } from './Database.js'
2
+ import { DatabaseManager } from './DatabaseManager.js'
3
+ import { DatabaseTransaction } from './DatabaseTransaction.js'
4
+ import { DynamoDatabase } from './integrations/dynamo/DynamoDatabase.js'
5
+ import { KyselyDatabase } from './integrations/kysely/KyselyDatabase.js'
6
+ import { KyselyTransaction } from './integrations/kysely/KyselyTransaction.js'
7
+ import { PostgresDatabase } from './integrations/pgsql/PostgresDatabase.js'
8
+ import { PostgresTransaction } from './integrations/pgsql/PostgresTransaction.js'
9
+ import { DbConfig } from './types.js'
10
+
11
+ export {
12
+ // Abstracts
13
+ DatabaseTransaction,
14
+ Database,
15
+ // 'Entrypoints'
16
+ DatabaseManager,
17
+ DbConfig,
18
+ // Postgres
19
+ PostgresTransaction,
20
+ PostgresDatabase,
21
+ // Kysely
22
+ KyselyTransaction,
23
+ KyselyDatabase,
24
+ // Dynamo
25
+ // DynamoTransaction,
26
+ DynamoDatabase,
27
+ }
@@ -0,0 +1,58 @@
1
+ import { Agent } from 'https'
2
+
3
+ import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
4
+ import { NodeHttpHandler } from '@smithy/node-http-handler'
5
+
6
+ import { Database } from '../../Database.js'
7
+ import type { DbConfig } from '../../types.js'
8
+
9
+ /**
10
+ * Represents a DynamoDB database connection that extends a generic Database class.
11
+ * @extends Database<any>
12
+ */
13
+ export class DynamoDatabase extends Database<any> {
14
+ /**
15
+ * A private static property that holds an instance of the DynamoDBClient class.
16
+ * This property can be used to interact with DynamoDB services.
17
+ */
18
+ private static dynamoProvider = DynamoDBClient
19
+ /**
20
+ * A readonly property representing the DynamoDB client used to interact with DynamoDB.
21
+ */
22
+ public readonly client: DynamoDBClient
23
+
24
+ /**
25
+ * Constructor for creating a new instance of a class that interacts with a DynamoDB database.
26
+ * @param {DbConfig<'dynamo'>} config - The configuration object for DynamoDB.
27
+ * @returns None
28
+ */
29
+ constructor(config: DbConfig<'dynamo'>) {
30
+ super(config)
31
+ this.client = this.providerFactory(config)
32
+ }
33
+
34
+ /**
35
+ * Override method to handle a transaction asynchronously.
36
+ * @returns A Promise that resolves to null.
37
+ */
38
+ public override async transaction(): Promise<any> {
39
+ return Promise.resolve(null)
40
+ }
41
+
42
+ /**
43
+ * Creates a provider for DynamoDB based on the provided configuration.
44
+ * @param {DbConfig<'dynamo'>} config - The configuration object for DynamoDB.
45
+ * @returns A DynamoDB provider instance based on the provided configuration.
46
+ */
47
+ private providerFactory(config: DbConfig<'dynamo'>) {
48
+ return new DynamoDatabase.dynamoProvider({
49
+ region: config.region,
50
+ maxAttempts: config.maxAttempts || 3,
51
+ requestHandler: new NodeHttpHandler({
52
+ connectionTimeout: config.connectionTimeout || 60000,
53
+ socketTimeout: config.connectionTimeout || 60000,
54
+ httpsAgent: new Agent({ keepAlive: false, maxSockets: 50, rejectUnauthorized: true }),
55
+ }),
56
+ })
57
+ }
58
+ }
@@ -0,0 +1,99 @@
1
+ import { CamelCasePlugin, Kysely, PostgresDialect } from 'kysely'
2
+ import pg from 'pg'
3
+
4
+ import { KyselyTransaction, KyselyTransactionImpl } from './KyselyTransaction.js'
5
+ import { Database } from '../../Database.js'
6
+ import type { DbBaseConfig, DbConfig } from '../../types.js'
7
+
8
+ /**
9
+ * Represents a database connection using the Kysely library with support for transactions.
10
+ * @template DBSchema - The schema type for the database.
11
+ */
12
+ export class KyselyDatabase<DBSchema = never> extends Database<KyselyTransaction<DBSchema>> {
13
+ /**
14
+ * Represents a PostgreSQL provider for querying data using the PostgresDialect.
15
+ * @type {PostgresDialect}
16
+ */
17
+ private static kyselyPgProvider = PostgresDialect
18
+ /**
19
+ * A public static property that provides access to the Kysely class.
20
+ * This property can be accessed without creating an instance of the class.
21
+ */
22
+ private static kyselyProvider = Kysely
23
+ /**
24
+ * A static property that represents a connection pool for PostgreSQL database connections.
25
+ * @type {Pool}
26
+ */
27
+ private static pgProvider = pg.Pool
28
+ /**
29
+ * Represents a PostgreSQL client using the PostgresDialect.
30
+ */
31
+ private readonly pgClient: PostgresDialect
32
+ /**
33
+ * Represents a client for querying the database with the specified schema.
34
+ * @type {Kysely<DBSchema>} client - The client for querying the database.
35
+ */
36
+ public readonly client: Kysely<DBSchema>
37
+ /**
38
+ * Optional property representing the Postgres dialect for reading in a database client.
39
+ */
40
+ private readonly pgReadClient?: PostgresDialect
41
+ /**
42
+ * Represents a client for reading from the database.
43
+ * @type {Kysely<DBSchema> | undefined} - The client for reading from the database.
44
+ */
45
+ public readonly readClient?: Kysely<DBSchema>
46
+
47
+ /**
48
+ * Constructor for creating a database connection using the provided configuration.
49
+ * @param {DbConfig<'kysely'>} config - The configuration object for the database connection.
50
+ * @returns None
51
+ */
52
+ constructor(config: DbConfig<'kysely'>) {
53
+ super(config)
54
+ this.pgClient = this.dialectFactory(config)
55
+ this.client = this.providerFactory(this.pgClient)
56
+ if (config.readReplica) {
57
+ this.pgReadClient = this.dialectFactory(config.readReplica)
58
+ this.readClient = this.providerFactory(this.pgReadClient)
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Override method that starts a new transaction using the provided client and read client.
64
+ * @returns A Promise that resolves to a KyselyTransaction object for the specified database schema.
65
+ */
66
+ public override async transaction(): Promise<KyselyTransaction<DBSchema>> {
67
+ return KyselyTransactionImpl.newTransaction<DBSchema>(this.client, this, this.readClient)
68
+ }
69
+
70
+ /**
71
+ * Creates a database dialect based on the provided configuration.
72
+ * @param {DbBaseConfig} config - The configuration object for the database connection.
73
+ * @returns A database dialect instance based on the provided configuration.
74
+ */
75
+ private dialectFactory(config: DbBaseConfig) {
76
+ return new KyselyDatabase.kyselyPgProvider({
77
+ pool: new KyselyDatabase.pgProvider({
78
+ host: config.host,
79
+ port: config.port,
80
+ user: config.username,
81
+ password: config.password,
82
+ database: config.database,
83
+ max: config.maxConnections,
84
+ }),
85
+ })
86
+ }
87
+
88
+ /**
89
+ * Creates a provider for interacting with a database using the specified Postgres dialect.
90
+ * @param {PostgresDialect} dialect - The Postgres dialect to use for the database connection.
91
+ * @returns A database provider for interacting with the database.
92
+ */
93
+ private providerFactory(dialect: PostgresDialect) {
94
+ return new KyselyDatabase.kyselyProvider<DBSchema>({
95
+ dialect,
96
+ plugins: [new CamelCasePlugin()],
97
+ })
98
+ }
99
+ }
@@ -0,0 +1,172 @@
1
+ import { Kysely, Transaction } from 'kysely'
2
+
3
+ import { KyselyDatabase } from './KyselyDatabase.js'
4
+ import { Database } from '../../Database.js'
5
+ import { DatabaseTransaction } from '../../DatabaseTransaction.js'
6
+
7
+ /**
8
+ * Represents a transaction for querying a database with a specific schema.
9
+ * @typeparam DBSchema - The schema of the database.
10
+ * @type {KyselyTransactionImpl<DBSchema> & Transaction<DBSchema>}
11
+ */
12
+ export type KyselyTransaction<DBSchema> = KyselyTransactionImpl<DBSchema> & Transaction<DBSchema>
13
+ /**
14
+ * Represents a transaction in a Postgres database.
15
+ */
16
+ export class KyselyTransactionImpl<DBSchema> extends DatabaseTransaction {
17
+ /**
18
+ * A readonly property representing a writer for a Kysely object.
19
+ * @type {Kysely<any>}
20
+ */
21
+ public readonly writer: Kysely<any>
22
+ /**
23
+ * A readonly property representing a reader of type Kysely<any>.
24
+ * This property allows reading data of any type using the Kysely interface.
25
+ */
26
+ public readonly reader: Kysely<any>
27
+ /**
28
+ * A protected property representing a transaction of type any.
29
+ */
30
+ protected transaction: Transaction<any>
31
+ /**
32
+ * A protected property representing a database instance.
33
+ * @type {Database<KyselyTransaction<DBSchema>>}
34
+ */
35
+ protected database: Database<KyselyTransaction<DBSchema>>
36
+ /**
37
+ * A private property that represents a deferred object.
38
+ * @type {Deferred<any>}
39
+ */
40
+ private txWrapper: Deferred<any>
41
+ /**
42
+ * Constructs a new instance of the class.
43
+ * @param {Kysely<DBSchema>} delegate - The delegate object for the database query.
44
+ * @param {KyselyDatabase<DBSchema>} database - The database object for the query.
45
+ * @param {Kysely<DBSchema>} [reader] - An optional reader object for the query.
46
+ * @returns None
47
+ */
48
+ private constructor(
49
+ delegate: Kysely<DBSchema>,
50
+ database: KyselyDatabase<DBSchema>,
51
+ reader?: Kysely<DBSchema>
52
+ ) {
53
+ super(delegate, database, reader)
54
+ }
55
+
56
+ /**
57
+ * Creates a new database transaction for the given database schema.
58
+ * @param {Kysely<DBSchema>} connection - The connection object for the transaction.
59
+ * @param {KyselyDatabase<DBSchema>} database - The database object for the transaction.
60
+ * @param {Kysely<DBSchema>} [reader] - An optional reader object for the transaction.
61
+ * @returns {Promise<KyselyTransaction<DBSchema>>} A promise that resolves to the created transaction.
62
+ */
63
+ public static async newTransaction<DBSchema>(
64
+ connection: Kysely<DBSchema>,
65
+ database: KyselyDatabase<DBSchema>,
66
+ reader?: Kysely<DBSchema>
67
+ ): Promise<KyselyTransaction<DBSchema>> {
68
+ const tx = new KyselyTransactionImpl<DBSchema>(connection, database, reader)
69
+ await tx.begin()
70
+ return DatabaseTransaction.proxyInstance(tx) as any
71
+ }
72
+
73
+ /**
74
+ * Asynchronously begins a transaction using a writer and resolves the transaction once it is executed.
75
+ * @returns A Promise that resolves with the transaction object once the transaction is executed.
76
+ */
77
+ protected doBegin = async () => {
78
+ this.txWrapper = new Deferred<any>()
79
+ return new Promise(resolve => {
80
+ this.writer
81
+ .transaction()
82
+ .execute(async trx => {
83
+ this.transaction = trx
84
+ resolve(trx) // resolve with tx
85
+ return this.txWrapper.promise // wait for wrapper to be solved
86
+ })
87
+ .catch(() => {
88
+ // Don't do anything here. Just swallow the exception.
89
+ })
90
+ })
91
+ }
92
+
93
+ /**
94
+ * Executes the commit operation by resolving the transaction wrapper and returning a resolved Promise.
95
+ * @returns A Promise that resolves to null.
96
+ */
97
+ protected doCommit = () => {
98
+ this.txWrapper.resolve(null)
99
+ return Promise.resolve()
100
+ }
101
+
102
+ /**
103
+ * Performs a rollback operation by rejecting the transaction wrapper with an error.
104
+ * @returns A resolved Promise after the rollback operation is completed.
105
+ */
106
+ protected doRollback = () => {
107
+ this.txWrapper.reject(new Error('Rollback'))
108
+ return Promise.resolve()
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Represents a deferred promise that can be resolved or rejected at a later time.
114
+ * @template T - The type of the value that the promise will resolve to.
115
+ */
116
+ class Deferred<T> {
117
+ /**
118
+ * A private readonly Promise object that resolves to type T.
119
+ */
120
+ private readonly _promise: Promise<T>
121
+ /**
122
+ * A private property that holds a function to resolve a Promise with a value of type T.
123
+ * @param {T | PromiseLike<T>} value - The value or promise to be resolved.
124
+ */
125
+ private _resolve?: (value: T | PromiseLike<T>) => void
126
+ /**
127
+ * A function that can be called to reject a promise with an optional reason.
128
+ * @param {any} [reason] - An optional reason for rejecting the promise.
129
+ */
130
+ private _reject?: (reason?: any) => void
131
+
132
+ /**
133
+ * Constructor for creating a new Promise instance with resolve and reject functions.
134
+ * @constructor
135
+ */
136
+ constructor() {
137
+ this._promise = new Promise<T>((resolve, reject) => {
138
+ this._reject = reject
139
+ this._resolve = resolve
140
+ })
141
+ }
142
+
143
+ /**
144
+ * Getter method to retrieve the Promise object.
145
+ * @returns {Promise<T>} A Promise object of type T.
146
+ */
147
+ public get promise(): Promise<T> {
148
+ return this._promise
149
+ }
150
+
151
+ /**
152
+ * Resolves the Promise with the given value.
153
+ * @param {T | PromiseLike<T>} value - The value to resolve the Promise with.
154
+ * @returns void
155
+ */
156
+ public resolve(value: T | PromiseLike<T>): void {
157
+ if (this._resolve) {
158
+ this._resolve(value)
159
+ }
160
+ }
161
+
162
+ /**
163
+ * Rejects the Promise with the given reason, if the reject function is available.
164
+ * @param {any} reason - The reason for rejecting the Promise.
165
+ * @returns void
166
+ */
167
+ public reject(reason?: any): void {
168
+ if (this._reject) {
169
+ this._reject(reason)
170
+ }
171
+ }
172
+ }
@@ -0,0 +1,56 @@
1
+ import pg from 'pg'
2
+
3
+ import { PostgresTransaction, PostgresTransactionImpl } from './PostgresTransaction.js'
4
+ import { Database } from '../../Database.js'
5
+ import type { DbBaseConfig, DbConfig } from '../../types.js'
6
+
7
+ /**
8
+ * Represents a Postgres database connection that extends the Database class.
9
+ * @extends Database<PostgresTransaction>
10
+ */
11
+ export class PostgresDatabase extends Database<PostgresTransaction> {
12
+ /**
13
+ * A public static property that represents a connection pool for a PostgreSQL database.
14
+ * This property is used to manage and provide connections to the PostgreSQL database.
15
+ */
16
+ private static pgProvider = pg.Pool
17
+ /**
18
+ * Represents a connection pool to manage multiple client connections to the database.
19
+ */
20
+ public readonly client: pg.Pool
21
+ /**
22
+ * A private property that represents a connection pool for clients.
23
+ * @type {Pool | undefined}
24
+ */
25
+ public readonly readClient?: pg.Pool
26
+
27
+ /**
28
+ * Constructor for creating a new instance of a database connection using Postgres.
29
+ * @param {DbConfig<'pg'>} config - The configuration object for the Postgres database.
30
+ * @returns None
31
+ */
32
+ constructor(config: DbConfig<'pg'>) {
33
+ super(config)
34
+ this.client = this.providerFactory(config)
35
+ if (config.readReplica) this.readClient = this.providerFactory(config.readReplica)
36
+ }
37
+
38
+ /**
39
+ * Creates a new Postgres transaction using the provided client and read client.
40
+ * @returns {Promise<PostgresTransaction>} A promise that resolves to a new PostgresTransaction object.
41
+ */
42
+ public override async transaction(): Promise<PostgresTransaction> {
43
+ return PostgresTransactionImpl.newTransaction(this.client, this, this.readClient)
44
+ }
45
+
46
+ private providerFactory(config: DbBaseConfig) {
47
+ return new PostgresDatabase.pgProvider({
48
+ host: config.host,
49
+ port: config.port,
50
+ user: config.username,
51
+ password: config.password,
52
+ database: config.database,
53
+ max: config.maxConnections,
54
+ })
55
+ }
56
+ }
@@ -0,0 +1,87 @@
1
+ import * as pg from 'pg'
2
+
3
+ import { PostgresDatabase } from './PostgresDatabase.js'
4
+ import { Database } from '../../Database.js'
5
+ import { DatabaseTransaction } from '../../DatabaseTransaction.js'
6
+
7
+ /**
8
+ * A type alias representing a Postgres transaction. It extends the `PostgresTransactionImpl`
9
+ * interface and includes the `PoolClient` interface.
10
+ */
11
+ export type PostgresTransaction = PostgresTransactionImpl & pg.PoolClient
12
+
13
+ /**
14
+ * Represents a transaction in a Postgres database.
15
+ */
16
+ export class PostgresTransactionImpl extends DatabaseTransaction {
17
+ /**
18
+ * A Pool object used for writing operations.
19
+ * @readonly
20
+ */
21
+ public readonly writer: pg.Pool
22
+ /**
23
+ * A readonly property representing a pool reader.
24
+ */
25
+ public readonly reader: pg.Pool
26
+ /**
27
+ * Represents a database transaction using a PoolClient.
28
+ * @type {PoolClient}
29
+ */
30
+ protected transaction: pg.PoolClient
31
+ /**
32
+ * A protected property representing a database of type Database<PostgresTransaction>.
33
+ * This property is used to interact with a Postgres database using transactions.
34
+ */
35
+ protected database: Database<PostgresTransaction>
36
+ /**
37
+ * Constructs a new instance of the class with the provided writer, database, and optional reader.
38
+ * @param {Pool} writer - The writer pool for database operations.
39
+ * @param {PostgresDatabase} database - The Postgres database instance.
40
+ * @param {Pool} [reader] - The reader pool for database operations (optional).
41
+ */
42
+ private constructor(writer: pg.Pool, database: PostgresDatabase, reader?: pg.Pool) {
43
+ super(writer, database, reader)
44
+ }
45
+
46
+ /**
47
+ * Creates a new database transaction using the provided writer and database instances.
48
+ * @param {Pool} writer - The writer instance for the transaction.
49
+ * @param {PostgresDatabase} database - The database instance for the transaction.
50
+ * @param {Pool} [reader] - Optional reader instance for the transaction.
51
+ * @returns {Promise<PostgresTransaction>} A promise that resolves to a new PostgresTransaction instance.
52
+ */
53
+ public static async newTransaction(
54
+ writer: pg.Pool,
55
+ database: PostgresDatabase,
56
+ reader?: pg.Pool
57
+ ): Promise<PostgresTransaction> {
58
+ const tx = new PostgresTransactionImpl(writer, database, reader)
59
+ await tx.begin() // defaults to opened
60
+ return DatabaseTransaction.proxyInstance(tx) as any
61
+ }
62
+
63
+ /**
64
+ * Initiates a transaction by connecting to the writer and executing a 'BEGIN' query.
65
+ * @returns {Promise<void>} A promise that resolves when the transaction is successfully initiated.
66
+ */
67
+ protected doBegin = async () => {
68
+ this.transaction = await this.writer.connect()
69
+ return this.transaction.query('BEGIN')
70
+ }
71
+
72
+ /**
73
+ * Executes a COMMIT query to commit the current transaction.
74
+ * @returns A Promise that resolves when the COMMIT query is successfully executed.
75
+ */
76
+ protected doCommit = () => {
77
+ return this.transaction.query('COMMIT')
78
+ }
79
+
80
+ /**
81
+ * Rolls back the current transaction by executing a 'ROLLBACK' query.
82
+ * @returns A Promise that resolves when the rollback is successful.
83
+ */
84
+ protected doRollback = () => {
85
+ return this.transaction.query('ROLLBACK')
86
+ }
87
+ }
@@ -0,0 +1,85 @@
1
+ import { Database } from './Database.js'
2
+ import { DATABASES } from './DatabaseManager.js'
3
+ import { DynamoDatabase } from './integrations/dynamo/DynamoDatabase.js'
4
+ import { KyselyDatabase } from './integrations/kysely/KyselyDatabase.js'
5
+ import { PostgresDatabase } from './integrations/pgsql/PostgresDatabase.js'
6
+
7
+ /**
8
+ * Represents the possible types of databases available in the system.
9
+ * @typedef {keyof typeof DATABASES} DatabaseType
10
+ */
11
+ export type DatabaseType = keyof typeof DATABASES
12
+
13
+ /**
14
+ * Defines a type for a database class that includes instances for Postgres, and Kysely databases.
15
+ * @template T - The type of data the KyselyDatabase instance will handle.
16
+ * @property {new (...args: any[]) => PostgresDatabase} pg - Constructor for PostgresDatabase instances.
17
+ * @property {new (...args: any[]) => KyselyDatabase<T>} kysely - Constructor for KyselyDatabase instances with type T.
18
+ */
19
+ export type DatabaseClass<T> = {
20
+ pg: new (...args: any[]) => PostgresDatabase
21
+ kysely: new (...args: any[]) => KyselyDatabase<T>
22
+ dynamo: new (...args: any[]) => DynamoDatabase
23
+ }
24
+
25
+ /**
26
+ * Represents a specific implementation type for a database based on the provided DatabaseType.
27
+ * @param {DatabaseType} Type - The type of database to implement.
28
+ * @param {T} T - The generic type for the database implementation.
29
+ * @returns An instance of the specified database class for the given type.
30
+ */
31
+ export type DatabaseImplType<Type extends DatabaseType, T> = InstanceType<DatabaseClass<T>[Type]>
32
+
33
+ /**
34
+ * Defines the type of transaction for a specific database type and schema.
35
+ * @param {DatabaseType} Type - The type of the database.
36
+ * @param {DBSchema} DBSchema - The schema of the database.
37
+ * @returns The transaction type associated with the database type and schema.
38
+ */
39
+ export type DatabaseTransactionType<Type extends DatabaseType, DBSchema> =
40
+ DatabaseImplType<Type, DBSchema> extends Database<infer TransactionType> ? TransactionType : never
41
+
42
+ /**
43
+ * Defines the configuration options for different types of databases.
44
+ * @typedef {Object} DbBaseConfig
45
+ * @property {string} host - The host of the database.
46
+ * @property {number} port - The port number of the database.
47
+ * @property {string} username - The username for accessing the database.
48
+ * @property {string} password - The password for accessing the database.
49
+ * @property {string} database - The name of the database.
50
+ * @property {boolean} autoCommit - Flag indicating whether auto-commit is enabled.
51
+ * @property {number} maxConnections - The maximum number of connections allowed.
52
+ *
53
+ * @typedef {Object} DbConfig
54
+ * @property {string} type - The type of
55
+ */
56
+ export type DbBaseConfig = {
57
+ host: string
58
+ port: number
59
+ username: string
60
+ password: string
61
+ database: string
62
+ autoCommit: boolean
63
+ maxConnections: number
64
+ }
65
+ /**
66
+ * Defines the configuration options for different types of databases.
67
+ * @template S - The type of database (e.g., 'dynamo', 'pg', 'kysely')
68
+ * @typedef {Object} DbConfig
69
+ * @property {DbBaseConfig} DbBaseConfig - The base configuration for the database.
70
+ * @property {S} type - The type of the database.
71
+ * @property {DbBaseConfig} [readReplica] - The read replica configuration for '
72
+ */
73
+ export type DbConfig<S extends DatabaseType> = { type: S } & (S extends 'pg' | 'kysely'
74
+ ? DbBaseConfig & {
75
+ readReplica?: DbBaseConfig
76
+ }
77
+ : unknown) &
78
+ (S extends 'dynamo'
79
+ ? {
80
+ region: string
81
+ tableName: string
82
+ maxAttempts?: number
83
+ connectionTimeout?: number
84
+ }
85
+ : unknown)
package/src/Globals.ts ADDED
@@ -0,0 +1,103 @@
1
+ import dotenv from 'dotenv'
2
+
3
+ import Utils from './Util/Utils.js'
4
+
5
+ // important for dev env to load .env file
6
+ dotenv.config()
7
+
8
+ /**
9
+ * Class containing global constants and configurations for the application.
10
+ */
11
+ export default class Globals {
12
+ /**
13
+ * A constant string representing an error message for input validation failure.
14
+ */
15
+ public static ErrorResponseValidationFail = 'Input validation failed: ' //400
16
+ /**
17
+ * A constant string representing an error response for an invalid server response.
18
+ */
19
+ public static ErrorResponseInvalidServerResponse = 'No valid response, this is a system error.' //400
20
+ /**
21
+ * The error message for an unhandled error when processing a request.
22
+ */
23
+ public static ErrorResponseUnhandledError = 'Unhandled error when processing request.' //400
24
+ /**
25
+ * A static string representing an error response when there are no records to be processed.
26
+ */
27
+ public static ErrorResponseNoRecords = 'No events to be processed.' //400
28
+ /**
29
+ * Represents an error code for a missing parameter.
30
+ * @type {string}
31
+ */
32
+ public static ErrorCode_MissingParam = 'MISSING_PARAM'
33
+ /**
34
+ * Represents an error code for invalid input.
35
+ * @type {string}
36
+ */
37
+ public static ErrorCode_InvalidInput = 'INVALID_INPUT'
38
+ /**
39
+ * Represents an error code for an API error.
40
+ * @type {string}
41
+ */
42
+ public static ErrorCode_APIError = 'API_ERROR'
43
+ /**
44
+ * Represents the error code for when there are no records found.
45
+ * @type {string}
46
+ */
47
+ public static ErrorCode_NoRecords = 'EMPTY_EVENT'
48
+
49
+ /**
50
+ * Retrieves the default port number for HTTP listeners.
51
+ * The port number is obtained from the environment variable "PORT" and parsed as an integer.
52
+ * If the environment variable is not set or cannot be parsed as an integer, the default port number 9000 is used.
53
+ * @returns {number} - The default port number for HTTP listeners.
54
+ */
55
+ public static Listener_HTTP_DefaultPort = Utils.parseIntNullIfNaN(process.env.PORT) || 9000
56
+ /**
57
+ * The default host for the HTTP listener.
58
+ */
59
+ public static Listener_HTTP_DefaultHost = 'localhost'
60
+ /**
61
+ * The HTTP proxy route listener for all routes.
62
+ * @type {string}
63
+ */
64
+ public static Listener_HTTP_ProxyRoute = '*'
65
+ /**
66
+ * Retrieves the default timeout value for HTTP listeners.
67
+ * @returns {number} The default timeout value in milliseconds.
68
+ */
69
+ public static Listener_HTTP_DefaultTimeout = Utils.parseIntNullIfNaN(process.env.TIMEOUT) || 30000
70
+ /**
71
+ * The default health check route for the HTTP listener.
72
+ * @type {string}
73
+ */
74
+ public static Listener_HTTP_DefaultHealthCheckRoute = process.env.HEALTH_ROUTE || '/health'
75
+
76
+ /**
77
+ * The response message for an exception that occurred during request execution in the Proxy.
78
+ */
79
+ public static Resp_MSG_EXCEPTION = '[Proxy]: Exception during request execution!'
80
+ /**
81
+ * The response code for an exception that occurred during execution.
82
+ */
83
+ public static Resp_CODE_EXCEPTION = 'EXEC_EXCEPTION'
84
+ /**
85
+ * The HTTP response status code for an exception scenario.
86
+ */
87
+ public static Resp_STATUSCODE_EXCEPTION = 502
88
+
89
+ /**
90
+ * The error message for an invalid response from the server.
91
+ */
92
+ public static Resp_MSG_INVALIDRESP = '[Proxy]: Invalid response from server!'
93
+ /**
94
+ * Represents the response code for an invalid response.
95
+ */
96
+ public static Resp_CODE_INVALIDRESP = 'EMPTY_RESPONSE'
97
+ /**
98
+ * The HTTP response status code for an invalid response.
99
+ * @type {number}
100
+ * @default 400
101
+ */
102
+ public static Resp_STATUSCODE_INVALIDRESP = 400
103
+ }