@creator.co/wapi 1.2.3 → 1.2.5

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 (121) hide show
  1. package/.github/workflows/npmpublish.yml +2 -5
  2. package/README.md +1 -3
  3. package/dist/index.d.ts +11 -0
  4. package/dist/index.js +24 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/jest.config.d.ts +3 -0
  7. package/dist/jest.config.js +34 -0
  8. package/dist/jest.config.js.map +1 -0
  9. package/dist/package.json +70 -0
  10. package/dist/src/API/Request.d.ts +140 -0
  11. package/dist/src/API/Request.js +182 -0
  12. package/dist/src/API/Request.js.map +1 -0
  13. package/dist/src/API/Response.d.ts +256 -0
  14. package/dist/src/API/Response.js +398 -0
  15. package/dist/src/API/Response.js.map +1 -0
  16. package/dist/src/API/Utils.d.ts +63 -0
  17. package/dist/src/API/Utils.js +104 -0
  18. package/dist/src/API/Utils.js.map +1 -0
  19. package/dist/src/BaseEvent/EventProcessor.d.ts +81 -0
  20. package/dist/src/BaseEvent/EventProcessor.js +182 -0
  21. package/dist/src/BaseEvent/EventProcessor.js.map +1 -0
  22. package/dist/src/BaseEvent/Process.d.ts +74 -0
  23. package/dist/src/BaseEvent/Process.js +142 -0
  24. package/dist/src/BaseEvent/Process.js.map +1 -0
  25. package/dist/src/BaseEvent/Transaction.d.ts +39 -0
  26. package/dist/src/BaseEvent/Transaction.js +354 -0
  27. package/dist/src/BaseEvent/Transaction.js.map +1 -0
  28. package/dist/src/Config/Configuration.d.ts +131 -0
  29. package/dist/src/Config/Configuration.js +153 -0
  30. package/dist/src/Config/Configuration.js.map +1 -0
  31. package/dist/src/Config/EnvironmentVar.d.ts +101 -0
  32. package/dist/src/Config/EnvironmentVar.js +213 -0
  33. package/dist/src/Config/EnvironmentVar.js.map +1 -0
  34. package/dist/src/Crypto/Crypto.d.ts +57 -0
  35. package/dist/src/Crypto/Crypto.js +126 -0
  36. package/dist/src/Crypto/Crypto.js.map +1 -0
  37. package/dist/src/Crypto/JWT.d.ts +64 -0
  38. package/dist/src/Crypto/JWT.js +74 -0
  39. package/dist/src/Crypto/JWT.js.map +1 -0
  40. package/dist/src/Database/Database.d.ts +18 -0
  41. package/dist/src/Database/Database.js +18 -0
  42. package/dist/src/Database/Database.js.map +1 -0
  43. package/dist/src/Database/DatabaseManager.d.ts +32 -0
  44. package/dist/src/Database/DatabaseManager.js +50 -0
  45. package/dist/src/Database/DatabaseManager.js.map +1 -0
  46. package/dist/src/Database/DatabaseTransaction.d.ts +65 -0
  47. package/dist/src/Database/DatabaseTransaction.js +183 -0
  48. package/dist/src/Database/DatabaseTransaction.js.map +1 -0
  49. package/dist/src/Database/integrations/knex/KnexDatabase.d.ts +22 -0
  50. package/dist/src/Database/integrations/knex/KnexDatabase.js +108 -0
  51. package/dist/src/Database/integrations/knex/KnexDatabase.js.map +1 -0
  52. package/dist/src/Database/integrations/knex/KnexTransaction.d.ts +37 -0
  53. package/dist/src/Database/integrations/knex/KnexTransaction.js +60 -0
  54. package/dist/src/Database/integrations/knex/KnexTransaction.js.map +1 -0
  55. package/dist/src/Database/integrations/pgsql/PostgresDatabase.d.ts +30 -0
  56. package/dist/src/Database/integrations/pgsql/PostgresDatabase.js +108 -0
  57. package/dist/src/Database/integrations/pgsql/PostgresDatabase.js.map +1 -0
  58. package/dist/src/Database/integrations/pgsql/PostgresTransaction.d.ts +37 -0
  59. package/dist/src/Database/integrations/pgsql/PostgresTransaction.js +60 -0
  60. package/dist/src/Database/integrations/pgsql/PostgresTransaction.js.map +1 -0
  61. package/dist/src/Globals.d.ts +161 -0
  62. package/dist/src/Globals.js +173 -0
  63. package/dist/src/Globals.js.map +1 -0
  64. package/dist/src/Logger/Logger.d.ts +180 -0
  65. package/dist/src/Logger/Logger.js +412 -0
  66. package/dist/src/Logger/Logger.js.map +1 -0
  67. package/dist/src/Mailer/Mailer.d.ts +107 -0
  68. package/dist/src/Mailer/Mailer.js +313 -0
  69. package/dist/src/Mailer/Mailer.js.map +1 -0
  70. package/dist/src/Publisher/Publisher.d.ts +47 -0
  71. package/dist/src/Publisher/Publisher.js +141 -0
  72. package/dist/src/Publisher/Publisher.js.map +1 -0
  73. package/dist/src/Server/RouteResolver.d.ts +41 -0
  74. package/dist/src/Server/RouteResolver.js +135 -0
  75. package/dist/src/Server/RouteResolver.js.map +1 -0
  76. package/dist/src/Server/Router.d.ts +104 -0
  77. package/dist/src/Server/Router.js +45 -0
  78. package/dist/src/Server/Router.js.map +1 -0
  79. package/dist/src/Server/lib/ContainerServer.d.ts +58 -0
  80. package/dist/src/Server/lib/ContainerServer.js +143 -0
  81. package/dist/src/Server/lib/ContainerServer.js.map +1 -0
  82. package/dist/src/Server/lib/Server.d.ts +60 -0
  83. package/dist/src/Server/lib/Server.js +137 -0
  84. package/dist/src/Server/lib/Server.js.map +1 -0
  85. package/dist/src/Server/lib/container/GenericHandler.d.ts +4 -0
  86. package/dist/src/Server/lib/container/GenericHandler.js +138 -0
  87. package/dist/src/Server/lib/container/GenericHandler.js.map +1 -0
  88. package/dist/src/Server/lib/container/GenericHandlerEvent.d.ts +67 -0
  89. package/dist/src/Server/lib/container/GenericHandlerEvent.js +189 -0
  90. package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +1 -0
  91. package/dist/src/Server/lib/container/HealthHandler.d.ts +3 -0
  92. package/dist/src/Server/lib/container/HealthHandler.js +45 -0
  93. package/dist/src/Server/lib/container/HealthHandler.js.map +1 -0
  94. package/dist/src/Server/lib/container/Proxy.d.ts +95 -0
  95. package/dist/src/Server/lib/container/Proxy.js +202 -0
  96. package/dist/src/Server/lib/container/Proxy.js.map +1 -0
  97. package/dist/src/Server/lib/container/Utils.d.ts +18 -0
  98. package/dist/src/Server/lib/container/Utils.js +84 -0
  99. package/dist/src/Server/lib/container/Utils.js.map +1 -0
  100. package/dist/src/Validation/Validator.d.ts +21 -0
  101. package/dist/src/Validation/Validator.js +48 -0
  102. package/dist/src/Validation/Validator.js.map +1 -0
  103. package/jest.config.ts +2 -9
  104. package/package.json +10 -4
  105. package/src/BaseEvent/Transaction.ts +48 -18
  106. package/src/Database/Database.ts +19 -0
  107. package/src/Database/DatabaseManager.ts +51 -0
  108. package/src/Database/DatabaseTransaction.ts +118 -0
  109. package/src/Database/integrations/knex/KnexDatabase.ts +47 -0
  110. package/src/Database/integrations/knex/KnexTransaction.ts +51 -0
  111. package/src/Database/integrations/pgsql/PostgresDatabase.ts +49 -0
  112. package/src/Database/integrations/pgsql/PostgresTransaction.ts +54 -0
  113. package/src/Database/types.d.ts +49 -0
  114. package/src/Server/lib/container/Proxy.ts +2 -1
  115. package/tests/BaseEvent/Transaction.test.ts +59 -0
  116. package/tests/Database/DatabaseManager.test.ts +55 -0
  117. package/tests/Database/integrations/knex/KnexDatabase.test.ts +53 -0
  118. package/tests/Database/integrations/knex/KnexTransaction.test.ts +133 -0
  119. package/tests/Database/integrations/pg/PostgresDatabase.test.ts +50 -0
  120. package/tests/Database/integrations/pg/PostgresTransaction.test.ts +51 -0
  121. package/tsconfig.json +5 -0
@@ -0,0 +1,64 @@
1
+ import * as jwt from 'jsonwebtoken';
2
+ /**
3
+ * ${1:Description placeholder}
4
+ *
5
+ * @typedef {JWTValidationResponse}
6
+ */
7
+ type JWTValidationResponse = {
8
+ isValid: true;
9
+ decodedToken: jwt.JwtPayload;
10
+ } | {
11
+ isValid: false;
12
+ isExpired?: boolean;
13
+ };
14
+ /**
15
+ * ${1:Description placeholder}
16
+ *
17
+ * @export
18
+ * @class JWT
19
+ * @typedef {JWT}
20
+ */
21
+ export default class JWT {
22
+ /**
23
+ * ${1:Description placeholder}
24
+ *
25
+ * @private
26
+ * @readonly
27
+ * @type {string}
28
+ */
29
+ private readonly tokenSecret;
30
+ /**
31
+ * ${1:Description placeholder}
32
+ *
33
+ * @private
34
+ * @readonly
35
+ * @type {string}
36
+ */
37
+ private readonly defaultExpiration;
38
+ /**
39
+ * Creates an instance of JWT.
40
+ *
41
+ * @constructor
42
+ * @param {string} tokenSecret
43
+ * @param {?string} [defaultExpiration]
44
+ */
45
+ constructor(tokenSecret: string, defaultExpiration?: string);
46
+ /**
47
+ * ${1:Description placeholder}
48
+ *
49
+ * @param {object} data
50
+ * @param {?string} [expiration]
51
+ * @param {?string} [overrideToken]
52
+ * @param {?*} [opts]
53
+ * @returns {string}
54
+ */
55
+ createToken(data: object, expiration?: string, overrideToken?: string, opts?: any): string;
56
+ /**
57
+ * ${1:Description placeholder}
58
+ *
59
+ * @param {string} token
60
+ * @returns {JWTValidationResponse}
61
+ */
62
+ validateToken(token: string): JWTValidationResponse;
63
+ }
64
+ export {};
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ var jwt = require("jsonwebtoken");
15
+ /**
16
+ * ${1:Description placeholder}
17
+ *
18
+ * @export
19
+ * @class JWT
20
+ * @typedef {JWT}
21
+ */
22
+ var JWT = /** @class */ (function () {
23
+ /**
24
+ * Creates an instance of JWT.
25
+ *
26
+ * @constructor
27
+ * @param {string} tokenSecret
28
+ * @param {?string} [defaultExpiration]
29
+ */
30
+ function JWT(tokenSecret, defaultExpiration) {
31
+ this.tokenSecret = tokenSecret;
32
+ this.defaultExpiration = defaultExpiration;
33
+ }
34
+ //
35
+ /**
36
+ * ${1:Description placeholder}
37
+ *
38
+ * @param {object} data
39
+ * @param {?string} [expiration]
40
+ * @param {?string} [overrideToken]
41
+ * @param {?*} [opts]
42
+ * @returns {string}
43
+ */
44
+ JWT.prototype.createToken = function (data, expiration, overrideToken, opts) {
45
+ var exp = expiration || this.defaultExpiration;
46
+ return jwt.sign(data, overrideToken || this.tokenSecret, exp ? __assign({ expiresIn: exp }, (opts || {})) : opts || {});
47
+ };
48
+ /**
49
+ * ${1:Description placeholder}
50
+ *
51
+ * @param {string} token
52
+ * @returns {JWTValidationResponse}
53
+ */
54
+ JWT.prototype.validateToken = function (token) {
55
+ try {
56
+ // Check if is valid
57
+ var isValid = jwt.verify(token, this.tokenSecret);
58
+ if (isValid) {
59
+ var payload = jwt.decode(token, { json: true });
60
+ if (payload)
61
+ return { isValid: true, decodedToken: payload };
62
+ }
63
+ }
64
+ catch (err) {
65
+ console.error(err);
66
+ if (err instanceof jwt.TokenExpiredError)
67
+ return { isValid: false, isExpired: true };
68
+ }
69
+ return { isValid: false };
70
+ };
71
+ return JWT;
72
+ }());
73
+ exports.default = JWT;
74
+ //# sourceMappingURL=JWT.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JWT.js","sourceRoot":"","sources":["../../../src/Crypto/JWT.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,kCAAmC;AAcnC;;;;;;GAMG;AACH;IAiBE;;;;;;OAMG;IACH,aAAY,WAAmB,EAAE,iBAA0B;QACzD,IAAI,CAAC,WAAW,GAAG,WAAW,CAAA;QAC9B,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAA;IAC5C,CAAC;IACD,EAAE;IACF;;;;;;;;OAQG;IACH,yBAAW,GAAX,UAAY,IAAY,EAAE,UAAmB,EAAE,aAAsB,EAAE,IAAU;QAC/E,IAAM,GAAG,GAAG,UAAU,IAAI,IAAI,CAAC,iBAAiB,CAAA;QAChD,OAAO,GAAG,CAAC,IAAI,CACb,IAAI,EACJ,aAAa,IAAI,IAAI,CAAC,WAAW,EACjC,GAAG,CAAC,CAAC,YAAG,SAAS,EAAE,GAAG,IAAK,CAAC,IAAI,IAAI,EAAE,CAAC,EAAG,CAAC,CAAC,IAAI,IAAI,EAAE,CACvD,CAAA;IACH,CAAC;IACD;;;;;OAKG;IACH,2BAAa,GAAb,UAAc,KAAa;QACzB,IAAI;YACF,oBAAoB;YACpB,IAAM,OAAO,GAAG,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,WAAW,CAAC,CAAA;YACnD,IAAI,OAAO,EAAE;gBACX,IAAM,OAAO,GAAG,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAA;gBACjD,IAAI,OAAO;oBAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,YAAY,EAAE,OAAO,EAAE,CAAA;aAC7D;SACF;QAAC,OAAO,GAAG,EAAE;YACZ,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YAClB,IAAI,GAAG,YAAY,GAAG,CAAC,iBAAiB;gBAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,CAAA;SACrF;QACD,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAA;IAC3B,CAAC;IACH,UAAC;AAAD,CAAC,AAlED,IAkEC"}
@@ -0,0 +1,18 @@
1
+ import { DatabaseTransaction } from './DatabaseTransaction';
2
+ import type { DbConfig } from './types';
3
+ /**
4
+ * Abstract class representing a database.
5
+ * @template T - The type of database transaction.
6
+ */
7
+ export declare abstract class Database<T extends DatabaseTransaction> {
8
+ readonly config: DbConfig<any>;
9
+ /**
10
+ * Creates a database instance, maintaining a reference to the database configuration.
11
+ */
12
+ protected constructor(config: DbConfig<any>);
13
+ /**
14
+ * Returns a promise resolving to a new instance of the transaction impl.
15
+ * @returns A promise that resolves to a transaction instance.
16
+ */
17
+ abstract transaction(): Promise<T>;
18
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Database = void 0;
4
+ /**
5
+ * Abstract class representing a database.
6
+ * @template T - The type of database transaction.
7
+ */
8
+ var Database = /** @class */ (function () {
9
+ /**
10
+ * Creates a database instance, maintaining a reference to the database configuration.
11
+ */
12
+ function Database(config) {
13
+ this.config = config;
14
+ }
15
+ return Database;
16
+ }());
17
+ exports.Database = Database;
18
+ //# sourceMappingURL=Database.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Database.js","sourceRoot":"","sources":["../../../src/Database/Database.ts"],"names":[],"mappings":";;;AAGA;;;GAGG;AACH;IACE;;OAEG;IACH,kBAAsC,MAAqB;QAArB,WAAM,GAAN,MAAM,CAAe;IAAG,CAAC;IAOjE,eAAC;AAAD,CAAC,AAXD,IAWC;AAXqB,4BAAQ"}
@@ -0,0 +1,32 @@
1
+ import { KnexDatabase } from './integrations/knex/KnexDatabase';
2
+ import { PostgresDatabase } from './integrations/pgsql/PostgresDatabase';
3
+ import type { DatabaseImplType, DatabaseType, DbConfig } from './types';
4
+ /**
5
+ * An object that maps database names to their corresponding database classes.
6
+ * @type {Object}
7
+ * @property {KnexDatabase} knex - The Knex database class.
8
+ * @property {PostgresDatabase} pg - The Postgres database class.
9
+ */
10
+ export declare const DATABASES: {
11
+ knex: typeof KnexDatabase;
12
+ pg: typeof PostgresDatabase;
13
+ };
14
+ /**
15
+ * A class that manages databases and provides methods for creating and accessing database instances.
16
+ */
17
+ export declare class DatabaseManager {
18
+ /**
19
+ * The singleton instance of the DatabaseManager class.
20
+ */
21
+ static readonly INSTANCE: DatabaseManager;
22
+ private databases;
23
+ private instances;
24
+ /**
25
+ * Creates a new instance of a database based on the provided configuration.
26
+ * @param {DbConfig<S>} config - The configuration object for the database.
27
+ * @returns {DatabaseImplType<S>} - The created database instance.
28
+ * @template S - The type of the database.
29
+ */
30
+ create<S extends DatabaseType>(config: DbConfig<S>): DatabaseImplType<S>;
31
+ private instantiateDb;
32
+ }
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DatabaseManager = exports.DATABASES = void 0;
4
+ var hash = require("object-hash");
5
+ var KnexDatabase_1 = require("./integrations/knex/KnexDatabase");
6
+ var PostgresDatabase_1 = require("./integrations/pgsql/PostgresDatabase");
7
+ /**
8
+ * An object that maps database names to their corresponding database classes.
9
+ * @type {Object}
10
+ * @property {KnexDatabase} knex - The Knex database class.
11
+ * @property {PostgresDatabase} pg - The Postgres database class.
12
+ */
13
+ exports.DATABASES = {
14
+ knex: KnexDatabase_1.KnexDatabase,
15
+ pg: PostgresDatabase_1.PostgresDatabase,
16
+ };
17
+ /**
18
+ * A class that manages databases and provides methods for creating and accessing database instances.
19
+ */
20
+ var DatabaseManager = /** @class */ (function () {
21
+ function DatabaseManager() {
22
+ this.databases = exports.DATABASES;
23
+ this.instances = {};
24
+ }
25
+ /**
26
+ * Creates a new instance of a database based on the provided configuration.
27
+ * @param {DbConfig<S>} config - The configuration object for the database.
28
+ * @returns {DatabaseImplType<S>} - The created database instance.
29
+ * @template S - The type of the database.
30
+ */
31
+ DatabaseManager.prototype.create = function (config) {
32
+ var configHash = hash(config);
33
+ if (this.instances[configHash]) {
34
+ return this.instances[configHash];
35
+ }
36
+ var instance = this.instantiateDb(config);
37
+ this.instances[configHash] = instance;
38
+ return instance;
39
+ };
40
+ DatabaseManager.prototype.instantiateDb = function (config) {
41
+ return new this.databases[config.type](config);
42
+ };
43
+ /**
44
+ * The singleton instance of the DatabaseManager class.
45
+ */
46
+ DatabaseManager.INSTANCE = new DatabaseManager();
47
+ return DatabaseManager;
48
+ }());
49
+ exports.DatabaseManager = DatabaseManager;
50
+ //# sourceMappingURL=DatabaseManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DatabaseManager.js","sourceRoot":"","sources":["../../../src/Database/DatabaseManager.ts"],"names":[],"mappings":";;;AAAA,kCAAmC;AAEnC,iEAA+D;AAC/D,0EAAwE;AAGxE;;;;;GAKG;AACU,QAAA,SAAS,GAAG;IACvB,IAAI,EAAE,2BAAY;IAClB,EAAE,EAAE,mCAAgB;CACrB,CAAA;AAED;;GAEG;AACH;IAAA;QAKU,cAAS,GAAqB,iBAAS,CAAA;QAEvC,cAAS,GAA2C,EAAE,CAAA;IAuBhE,CAAC;IArBC;;;;;OAKG;IACI,gCAAM,GAAb,UAAsC,MAAmB;QACvD,IAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC,CAAA;QAC/B,IAAI,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE;YAC9B,OAAO,IAAI,CAAC,SAAS,CAAC,UAAU,CAAQ,CAAA;SACzC;QACD,IAAM,QAAQ,GAAG,IAAI,CAAC,aAAa,CAAC,MAAa,CAAC,CAAA;QAClD,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,GAAG,QAAQ,CAAA;QACrC,OAAO,QAAe,CAAA;IACxB,CAAC;IAEO,uCAAa,GAArB,UACE,MAAyC;QAEzC,OAAO,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAa,CAAC,CAAA;IACvD,CAAC;IA5BD;;OAEG;IACoB,wBAAQ,GAAG,IAAI,eAAe,EAAE,AAAxB,CAAwB;IA0BzD,sBAAC;CAAA,AA9BD,IA8BC;AA9BY,0CAAe"}
@@ -0,0 +1,65 @@
1
+ import { Database } from './Database';
2
+ /**
3
+ * Abstract class representing a database transaction.
4
+ * @class DatabaseTransaction
5
+ */
6
+ export declare abstract class DatabaseTransaction {
7
+ private _isOpen;
8
+ protected delegate: any;
9
+ protected database: Database<any>;
10
+ /**
11
+ * Returns a boolean value indicating whether the transaction is open or not.
12
+ * @returns {boolean} - true if the object is open, false otherwise.
13
+ */
14
+ get isOpen(): boolean;
15
+ /**
16
+ * Constructs a new instance of the class.
17
+ * @param {any} delegate - The delegate object.
18
+ * @param {Database<any>} database - The database object.
19
+ * @protected
20
+ */
21
+ protected constructor(delegate: any, database: Database<any>);
22
+ /**
23
+ * Wraps an instance of a subclass of DatabaseTransaction with a Proxy object.
24
+ * The Proxy object intercepts method calls and delegates to the target instance.
25
+ * If the method is on the target instance, it is called directly.
26
+ * If the method is not on the target instance and the transaction is open, it is called on the target's delegate implementation.
27
+ * If the method is not on the target instance and the transaction is closed, an error is thrown.
28
+ * @param {T} subclass - The subclass instance to wrap with a Proxy.
29
+ * @returns {T} - The wrapped subclass instance.
30
+ * @throws {Error} - If the target instance is closed.
31
+ */
32
+ protected static wrapInstance<T extends DatabaseTransaction>(subclass: T): T;
33
+ /**
34
+ * Commits the transaction.
35
+ * @throws {Error} - If the transaction is already closed.
36
+ * @returns None
37
+ */
38
+ commit(): Promise<void>;
39
+ /**
40
+ * Rollbacks the current transaction.
41
+ * @throws {Error} - If the transaction is already closed.
42
+ * @returns {Promise<void>} - A promise that resolves once the rollback is complete.
43
+ */
44
+ rollback(): Promise<void>;
45
+ /**
46
+ * Closes the transaction after a successful execution.
47
+ * @returns {Promise<void>} - A promise that resolves once the necessary actions are completed.
48
+ */
49
+ closeSuccess(): Promise<void>;
50
+ /**
51
+ * Closes the transaction after a failed execution.
52
+ * @returns {Promise<void>} - A promise that resolves once the necessary actions are completed.
53
+ */
54
+ closeFailure(): Promise<void>;
55
+ /**
56
+ * An abstract method that performs the commit operation.
57
+ * @returns {Promise<any>} A promise that resolves when the commit operation is completed.
58
+ */
59
+ protected abstract doCommit(): Promise<any>;
60
+ /**
61
+ * An abstract method that performs a rollback operation.
62
+ * @returns A Promise that resolves when the rollback operation is complete.
63
+ */
64
+ protected abstract doRollback(): Promise<any>;
65
+ }
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.DatabaseTransaction = void 0;
40
+ /**
41
+ * Abstract class representing a database transaction.
42
+ * @class DatabaseTransaction
43
+ */
44
+ var DatabaseTransaction = /** @class */ (function () {
45
+ /**
46
+ * Constructs a new instance of the class.
47
+ * @param {any} delegate - The delegate object.
48
+ * @param {Database<any>} database - The database object.
49
+ * @protected
50
+ */
51
+ function DatabaseTransaction(delegate, database) {
52
+ this._isOpen = true;
53
+ this.delegate = delegate;
54
+ this.database = database;
55
+ }
56
+ Object.defineProperty(DatabaseTransaction.prototype, "isOpen", {
57
+ /**
58
+ * Returns a boolean value indicating whether the transaction is open or not.
59
+ * @returns {boolean} - true if the object is open, false otherwise.
60
+ */
61
+ get: function () {
62
+ return this._isOpen;
63
+ },
64
+ enumerable: false,
65
+ configurable: true
66
+ });
67
+ /**
68
+ * Wraps an instance of a subclass of DatabaseTransaction with a Proxy object.
69
+ * The Proxy object intercepts method calls and delegates to the target instance.
70
+ * If the method is on the target instance, it is called directly.
71
+ * If the method is not on the target instance and the transaction is open, it is called on the target's delegate implementation.
72
+ * If the method is not on the target instance and the transaction is closed, an error is thrown.
73
+ * @param {T} subclass - The subclass instance to wrap with a Proxy.
74
+ * @returns {T} - The wrapped subclass instance.
75
+ * @throws {Error} - If the target instance is closed.
76
+ */
77
+ DatabaseTransaction.wrapInstance = function (subclass) {
78
+ var bind = function (target, key) {
79
+ return typeof target[key] === 'function' ? target[key].bind(target) : target[key];
80
+ };
81
+ return new Proxy(subclass, {
82
+ get: function (target, p) {
83
+ if (target[p] !== undefined) {
84
+ return bind(target, p);
85
+ }
86
+ else if (target._isOpen) {
87
+ return bind(target.delegate, p);
88
+ }
89
+ return undefined;
90
+ },
91
+ });
92
+ };
93
+ /**
94
+ * Commits the transaction.
95
+ * @throws {Error} - If the transaction is already closed.
96
+ * @returns None
97
+ */
98
+ DatabaseTransaction.prototype.commit = function () {
99
+ return __awaiter(this, void 0, void 0, function () {
100
+ return __generator(this, function (_a) {
101
+ switch (_a.label) {
102
+ case 0:
103
+ if (!this._isOpen) {
104
+ throw new Error('Cannot commit, transaction is already closed!');
105
+ }
106
+ return [4 /*yield*/, this.doCommit()];
107
+ case 1:
108
+ _a.sent();
109
+ this._isOpen = false;
110
+ return [2 /*return*/];
111
+ }
112
+ });
113
+ });
114
+ };
115
+ /**
116
+ * Rollbacks the current transaction.
117
+ * @throws {Error} - If the transaction is already closed.
118
+ * @returns {Promise<void>} - A promise that resolves once the rollback is complete.
119
+ */
120
+ DatabaseTransaction.prototype.rollback = function () {
121
+ return __awaiter(this, void 0, void 0, function () {
122
+ return __generator(this, function (_a) {
123
+ switch (_a.label) {
124
+ case 0:
125
+ if (!this._isOpen) {
126
+ throw new Error('Cannot rollback, transaction is already closed!');
127
+ }
128
+ return [4 /*yield*/, this.doRollback()];
129
+ case 1:
130
+ _a.sent();
131
+ this._isOpen = false;
132
+ return [2 /*return*/];
133
+ }
134
+ });
135
+ });
136
+ };
137
+ /**
138
+ * Closes the transaction after a successful execution.
139
+ * @returns {Promise<void>} - A promise that resolves once the necessary actions are completed.
140
+ */
141
+ DatabaseTransaction.prototype.closeSuccess = function () {
142
+ return __awaiter(this, void 0, void 0, function () {
143
+ return __generator(this, function (_a) {
144
+ switch (_a.label) {
145
+ case 0:
146
+ if (!this._isOpen) return [3 /*break*/, 4];
147
+ if (!this.database.config.autoCommit) return [3 /*break*/, 2];
148
+ return [4 /*yield*/, this.doCommit()];
149
+ case 1:
150
+ _a.sent();
151
+ return [3 /*break*/, 4];
152
+ case 2: return [4 /*yield*/, this.doRollback()];
153
+ case 3:
154
+ _a.sent();
155
+ _a.label = 4;
156
+ case 4: return [2 /*return*/];
157
+ }
158
+ });
159
+ });
160
+ };
161
+ /**
162
+ * Closes the transaction after a failed execution.
163
+ * @returns {Promise<void>} - A promise that resolves once the necessary actions are completed.
164
+ */
165
+ DatabaseTransaction.prototype.closeFailure = function () {
166
+ return __awaiter(this, void 0, void 0, function () {
167
+ return __generator(this, function (_a) {
168
+ switch (_a.label) {
169
+ case 0:
170
+ if (!this._isOpen) return [3 /*break*/, 2];
171
+ return [4 /*yield*/, this.doRollback()];
172
+ case 1:
173
+ _a.sent();
174
+ _a.label = 2;
175
+ case 2: return [2 /*return*/];
176
+ }
177
+ });
178
+ });
179
+ };
180
+ return DatabaseTransaction;
181
+ }());
182
+ exports.DatabaseTransaction = DatabaseTransaction;
183
+ //# sourceMappingURL=DatabaseTransaction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DatabaseTransaction.js","sourceRoot":"","sources":["../../../src/Database/DatabaseTransaction.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA;;;GAGG;AACH;IAaE;;;;;OAKG;IACH,6BAAsB,QAAa,EAAE,QAAuB;QAlBpD,YAAO,GAAY,IAAI,CAAA;QAmB7B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;IAC1B,CAAC;IAbD,sBAAW,uCAAM;QAJjB;;;WAGG;aACH;YACE,OAAO,IAAI,CAAC,OAAO,CAAA;QACrB,CAAC;;;OAAA;IAaD;;;;;;;;;OASG;IACc,gCAAY,GAA7B,UAA6D,QAAW;QACtE,IAAM,IAAI,GAAG,UAAC,MAAW,EAAE,GAAoB;YAC7C,OAAA,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC;QAA1E,CAA0E,CAAA;QAE5E,OAAO,IAAI,KAAK,CAAI,QAAQ,EAAE;YAC5B,GAAG,YAAC,MAAS,EAAE,CAAkB;gBAC/B,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE;oBAC3B,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;iBACvB;qBAAM,IAAI,MAAM,CAAC,OAAO,EAAE;oBACzB,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAA;iBAChC;gBACD,OAAO,SAAS,CAAA;YAClB,CAAC;SACF,CAAC,CAAA;IACJ,CAAC;IAED;;;;OAIG;IACU,oCAAM,GAAnB;;;;;wBACE,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;4BACjB,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAA;yBACjE;wBACD,qBAAM,IAAI,CAAC,QAAQ,EAAE,EAAA;;wBAArB,SAAqB,CAAA;wBACrB,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;;;;;KACrB;IAED;;;;OAIG;IACU,sCAAQ,GAArB;;;;;wBACE,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;4BACjB,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAA;yBACnE;wBACD,qBAAM,IAAI,CAAC,UAAU,EAAE,EAAA;;wBAAvB,SAAuB,CAAA;wBACvB,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;;;;;KACrB;IAED;;;OAGG;IACU,0CAAY,GAAzB;;;;;6BACM,IAAI,CAAC,OAAO,EAAZ,wBAAY;6BACV,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,UAAU,EAA/B,wBAA+B;wBACjC,qBAAM,IAAI,CAAC,QAAQ,EAAE,EAAA;;wBAArB,SAAqB,CAAA;;4BAErB,qBAAM,IAAI,CAAC,UAAU,EAAE,EAAA;;wBAAvB,SAAuB,CAAA;;;;;;KAG5B;IAED;;;OAGG;IACU,0CAAY,GAAzB;;;;;6BACM,IAAI,CAAC,OAAO,EAAZ,wBAAY;wBACd,qBAAM,IAAI,CAAC,UAAU,EAAE,EAAA;;wBAAvB,SAAuB,CAAA;;;;;;KAE1B;IAaH,0BAAC;AAAD,CAAC,AA/GD,IA+GC;AA/GqB,kDAAmB"}
@@ -0,0 +1,22 @@
1
+ import { KnexTransaction } from './KnexTransaction';
2
+ import { Database } from '../../Database';
3
+ import type { DbConfig } from '../../types';
4
+ /**
5
+ * Represents a Knex database connection.
6
+ * @extends Database<KnexTransaction>
7
+ */
8
+ export declare class KnexDatabase extends Database<KnexTransaction> {
9
+ private static knexProvider;
10
+ private client;
11
+ /**
12
+ * Constructs a new instance of the database class using the provided configuration.
13
+ * @param {DbConfig<'knex'>} config - The configuration object for the database.
14
+ * @returns None
15
+ */
16
+ constructor(config: DbConfig<'knex'>);
17
+ /**
18
+ * Initiates a transaction using the underlying database client.
19
+ * @returns {Promise<KnexTransaction>} A promise that resolves to a KnexTransaction object representing the transaction.
20
+ */
21
+ transaction(): Promise<KnexTransaction>;
22
+ }
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ Object.defineProperty(exports, "__esModule", { value: true });
54
+ exports.KnexDatabase = void 0;
55
+ var knex_1 = require("knex");
56
+ var KnexTransaction_1 = require("./KnexTransaction");
57
+ var Database_1 = require("../../Database");
58
+ /**
59
+ * Represents a Knex database connection.
60
+ * @extends Database<KnexTransaction>
61
+ */
62
+ var KnexDatabase = /** @class */ (function (_super) {
63
+ __extends(KnexDatabase, _super);
64
+ /**
65
+ * Constructs a new instance of the database class using the provided configuration.
66
+ * @param {DbConfig<'knex'>} config - The configuration object for the database.
67
+ * @returns None
68
+ */
69
+ function KnexDatabase(config) {
70
+ var _this = _super.call(this, config) || this;
71
+ _this.client = KnexDatabase.knexProvider({
72
+ client: config.driver,
73
+ connection: {
74
+ host: config.host,
75
+ port: config.port,
76
+ user: config.username,
77
+ password: config.password,
78
+ database: config.database,
79
+ },
80
+ pool: {
81
+ min: 1,
82
+ max: config.maxConnections,
83
+ },
84
+ });
85
+ return _this;
86
+ }
87
+ /**
88
+ * Initiates a transaction using the underlying database client.
89
+ * @returns {Promise<KnexTransaction>} A promise that resolves to a KnexTransaction object representing the transaction.
90
+ */
91
+ KnexDatabase.prototype.transaction = function () {
92
+ return __awaiter(this, void 0, void 0, function () {
93
+ var delegate;
94
+ return __generator(this, function (_a) {
95
+ switch (_a.label) {
96
+ case 0: return [4 /*yield*/, this.client.transaction()];
97
+ case 1:
98
+ delegate = _a.sent();
99
+ return [2 /*return*/, KnexTransaction_1.KnexTransactionImpl.wrapDelegate(delegate, this)];
100
+ }
101
+ });
102
+ });
103
+ };
104
+ KnexDatabase.knexProvider = knex_1.default;
105
+ return KnexDatabase;
106
+ }(Database_1.Database));
107
+ exports.KnexDatabase = KnexDatabase;
108
+ //# sourceMappingURL=KnexDatabase.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KnexDatabase.js","sourceRoot":"","sources":["../../../../../src/Database/integrations/knex/KnexDatabase.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6BAAiC;AAEjC,qDAAwE;AACxE,2CAAyC;AAGzC;;;GAGG;AACH;IAAkC,gCAAyB;IAKzD;;;;OAIG;IACH,sBAAmB,MAAwB;QAA3C,YACE,kBAAM,MAAM,CAAC,SAed;QAdC,KAAI,CAAC,MAAM,GAAG,YAAY,CAAC,YAAY,CAAC;YACtC,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,UAAU,EAAE;gBACV,IAAI,EAAE,MAAM,CAAC,IAAI;gBACjB,IAAI,EAAE,MAAM,CAAC,IAAI;gBACjB,IAAI,EAAE,MAAM,CAAC,QAAQ;gBACrB,QAAQ,EAAE,MAAM,CAAC,QAAQ;gBACzB,QAAQ,EAAE,MAAM,CAAC,QAAQ;aAC1B;YACD,IAAI,EAAE;gBACJ,GAAG,EAAE,CAAC;gBACN,GAAG,EAAE,MAAM,CAAC,cAAc;aAC3B;SACF,CAAC,CAAA;;IACJ,CAAC;IAED;;;OAGG;IACmB,kCAAW,GAAjC;;;;;4BACmB,qBAAM,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,EAAA;;wBAA1C,QAAQ,GAAG,SAA+B;wBAChD,sBAAO,qCAAmB,CAAC,YAAY,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAA;;;;KACxD;IAlCc,yBAAY,GAAiE,cAAI,CAAA;IAmClG,mBAAC;CAAA,AApCD,CAAkC,mBAAQ,GAoCzC;AApCY,oCAAY"}