@appweaver/core 1.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 (271) hide show
  1. package/LICENSE +1 -0
  2. package/README.md +7 -0
  3. package/app/application.d.ts +43 -0
  4. package/app/application.js +112 -0
  5. package/app/create-app.d.ts +32 -0
  6. package/app/create-app.js +38 -0
  7. package/app/index.d.ts +2 -0
  8. package/app/index.js +18 -0
  9. package/app/lifecycle-manager.d.ts +20 -0
  10. package/app/lifecycle-manager.js +42 -0
  11. package/app/load-modules.d.ts +10 -0
  12. package/app/load-modules.js +50 -0
  13. package/app/load-providers.d.ts +8 -0
  14. package/app/load-providers.js +33 -0
  15. package/cache/cache-service.d.ts +72 -0
  16. package/cache/cache-service.js +202 -0
  17. package/cache/cache.d.ts +12 -0
  18. package/cache/cache.js +184 -0
  19. package/cache/caching.d.ts +8 -0
  20. package/cache/caching.js +72 -0
  21. package/cache/eviction/eviction-index.d.ts +35 -0
  22. package/cache/eviction/eviction-index.js +2 -0
  23. package/cache/eviction/fifo-eviction-index.d.ts +7 -0
  24. package/cache/eviction/fifo-eviction-index.js +28 -0
  25. package/cache/eviction/index.d.ts +4 -0
  26. package/cache/eviction/index.js +20 -0
  27. package/cache/eviction/lfu-eviction-index.d.ts +8 -0
  28. package/cache/eviction/lfu-eviction-index.js +82 -0
  29. package/cache/eviction/lru-eviction-index.d.ts +8 -0
  30. package/cache/eviction/lru-eviction-index.js +81 -0
  31. package/cache/index.d.ts +2 -0
  32. package/cache/index.js +18 -0
  33. package/cache/memory-cache.d.ts +4 -0
  34. package/cache/memory-cache.js +12 -0
  35. package/cache/redis-cache.d.ts +4 -0
  36. package/cache/redis-cache.js +12 -0
  37. package/context/context.d.ts +2 -0
  38. package/context/context.js +13 -0
  39. package/context/dependency-injection.d.ts +98 -0
  40. package/context/dependency-injection.js +276 -0
  41. package/context/index.d.ts +2 -0
  42. package/context/index.js +18 -0
  43. package/database/create-client.d.ts +11 -0
  44. package/database/create-client.js +142 -0
  45. package/database/index.d.ts +1 -0
  46. package/database/index.js +17 -0
  47. package/database/prisma-database.d.ts +15 -0
  48. package/database/prisma-database.js +53 -0
  49. package/errors/error-handler.d.ts +3 -0
  50. package/errors/error-handler.js +24 -0
  51. package/errors/error-schema.d.ts +32 -0
  52. package/errors/error-schema.js +31 -0
  53. package/errors/http-error.d.ts +21 -0
  54. package/errors/http-error.js +33 -0
  55. package/errors/index.d.ts +3 -0
  56. package/errors/index.js +19 -0
  57. package/events/node-events.d.ts +7 -0
  58. package/events/node-events.js +39 -0
  59. package/export/export-service.d.ts +19 -0
  60. package/export/export-service.js +154 -0
  61. package/export/index.d.ts +1 -0
  62. package/export/index.js +17 -0
  63. package/factory/create-model.d.ts +2 -0
  64. package/factory/create-model.js +366 -0
  65. package/factory/create-policy.d.ts +2 -0
  66. package/factory/create-policy.js +16 -0
  67. package/factory/create-routes.d.ts +2 -0
  68. package/factory/create-routes.js +21 -0
  69. package/factory/create-service.d.ts +3 -0
  70. package/factory/create-service.js +97 -0
  71. package/factory/index.d.ts +4 -0
  72. package/factory/index.js +20 -0
  73. package/health/health-routes.d.ts +2 -0
  74. package/health/health-routes.js +36 -0
  75. package/health/health-schema.d.ts +45 -0
  76. package/health/health-schema.js +52 -0
  77. package/health/health-service.d.ts +38 -0
  78. package/health/health-service.js +82 -0
  79. package/health/index.d.ts +2 -0
  80. package/health/index.js +18 -0
  81. package/index.d.ts +15 -0
  82. package/index.js +31 -0
  83. package/mailer/email-service.d.ts +36 -0
  84. package/mailer/email-service.js +89 -0
  85. package/mailer/index.d.ts +1 -0
  86. package/mailer/index.js +17 -0
  87. package/mailer/json-mailer.d.ts +7 -0
  88. package/mailer/json-mailer.js +32 -0
  89. package/mailer/smtp-mailer.d.ts +7 -0
  90. package/mailer/smtp-mailer.js +43 -0
  91. package/memory/in-memory.d.ts +24 -0
  92. package/memory/in-memory.js +164 -0
  93. package/memory/redis.d.ts +26 -0
  94. package/memory/redis.js +158 -0
  95. package/package.json +68 -0
  96. package/prisma/client/browser.d.ts +34 -0
  97. package/prisma/client/browser.js +56 -0
  98. package/prisma/client/client.d.ts +53 -0
  99. package/prisma/client/client.js +70 -0
  100. package/prisma/client/commonInputTypes.d.ts +395 -0
  101. package/prisma/client/commonInputTypes.js +11 -0
  102. package/prisma/client/enums.d.ts +1 -0
  103. package/prisma/client/enums.js +11 -0
  104. package/prisma/client/internal/class.d.ts +187 -0
  105. package/prisma/client/internal/class.js +85 -0
  106. package/prisma/client/internal/prismaNamespace.d.ts +987 -0
  107. package/prisma/client/internal/prismaNamespace.js +193 -0
  108. package/prisma/client/internal/prismaNamespaceBrowser.d.ts +125 -0
  109. package/prisma/client/internal/prismaNamespaceBrowser.js +164 -0
  110. package/prisma/client/models/ApiKey.d.ts +1167 -0
  111. package/prisma/client/models/ApiKey.js +2 -0
  112. package/prisma/client/models/File.d.ts +1248 -0
  113. package/prisma/client/models/File.js +2 -0
  114. package/prisma/client/models/OneTimeToken.d.ts +1083 -0
  115. package/prisma/client/models/OneTimeToken.js +2 -0
  116. package/prisma/client/models/Permission.d.ts +1209 -0
  117. package/prisma/client/models/Permission.js +2 -0
  118. package/prisma/client/models/Role.d.ts +1209 -0
  119. package/prisma/client/models/Role.js +2 -0
  120. package/prisma/client/models/Seeder.d.ts +1023 -0
  121. package/prisma/client/models/Seeder.js +2 -0
  122. package/prisma/client/models.d.ts +7 -0
  123. package/prisma/client/models.js +2 -0
  124. package/queue/bull-queue.d.ts +37 -0
  125. package/queue/bull-queue.js +207 -0
  126. package/queue/memory-queue.d.ts +54 -0
  127. package/queue/memory-queue.js +283 -0
  128. package/resource/index.d.ts +4 -0
  129. package/resource/index.js +20 -0
  130. package/resource/resource-loader.d.ts +26 -0
  131. package/resource/resource-loader.js +161 -0
  132. package/resource/resource-routes.d.ts +3 -0
  133. package/resource/resource-routes.js +174 -0
  134. package/resource/resource-schema.d.ts +33 -0
  135. package/resource/resource-schema.js +186 -0
  136. package/resource/resource-service.d.ts +89 -0
  137. package/resource/resource-service.js +772 -0
  138. package/resources.d.ts +9 -0
  139. package/resources.js +24 -0
  140. package/scheduler/cron-scheduler.d.ts +12 -0
  141. package/scheduler/cron-scheduler.js +73 -0
  142. package/security/account/account-routes.d.ts +2 -0
  143. package/security/account/account-routes.js +104 -0
  144. package/security/account/account-schema.d.ts +290 -0
  145. package/security/account/account-schema.js +166 -0
  146. package/security/account/account-service.d.ts +88 -0
  147. package/security/account/account-service.js +218 -0
  148. package/security/account/index.d.ts +3 -0
  149. package/security/account/index.js +19 -0
  150. package/security/api-key/api-key-auth.d.ts +4 -0
  151. package/security/api-key/api-key-auth.js +66 -0
  152. package/security/api-key/index.d.ts +1 -0
  153. package/security/api-key/index.js +17 -0
  154. package/security/auth-routes.d.ts +2 -0
  155. package/security/auth-routes.js +80 -0
  156. package/security/auth-schema.d.ts +215 -0
  157. package/security/auth-schema.js +129 -0
  158. package/security/auth-service.d.ts +117 -0
  159. package/security/auth-service.js +272 -0
  160. package/security/auth.d.ts +3 -0
  161. package/security/auth.js +127 -0
  162. package/security/basic/basic-auth.d.ts +4 -0
  163. package/security/basic/basic-auth.js +32 -0
  164. package/security/basic/index.d.ts +1 -0
  165. package/security/basic/index.js +17 -0
  166. package/security/create-auth-resources.d.ts +6 -0
  167. package/security/create-auth-resources.js +129 -0
  168. package/security/helper.d.ts +127 -0
  169. package/security/helper.js +329 -0
  170. package/security/index.d.ts +4 -0
  171. package/security/index.js +20 -0
  172. package/security/jwt/index.d.ts +2 -0
  173. package/security/jwt/index.js +18 -0
  174. package/security/jwt/jwt-auth.d.ts +4 -0
  175. package/security/jwt/jwt-auth.js +57 -0
  176. package/security/jwt/jwt-keys.d.ts +7 -0
  177. package/security/jwt/jwt-keys.js +43 -0
  178. package/security/oauth2/create-oauth2-plugin.d.ts +17 -0
  179. package/security/oauth2/create-oauth2-plugin.js +85 -0
  180. package/security/oauth2/index.d.ts +3 -0
  181. package/security/oauth2/index.js +19 -0
  182. package/security/oauth2/oauth2-custom.d.ts +7 -0
  183. package/security/oauth2/oauth2-custom.js +40 -0
  184. package/security/oauth2/oauth2-facebook.d.ts +1 -0
  185. package/security/oauth2/oauth2-facebook.js +30 -0
  186. package/security/oauth2/oauth2-google.d.ts +1 -0
  187. package/security/oauth2/oauth2-google.js +28 -0
  188. package/security/oauth2/oauth2-schema.d.ts +10 -0
  189. package/security/oauth2/oauth2-schema.js +53 -0
  190. package/security/recaptcha/index.d.ts +2 -0
  191. package/security/recaptcha/index.js +18 -0
  192. package/security/recaptcha/recaptcha-verify.d.ts +1 -0
  193. package/security/recaptcha/recaptcha-verify.js +33 -0
  194. package/security/recaptcha/recaptcha.d.ts +2 -0
  195. package/security/recaptcha/recaptcha.js +22 -0
  196. package/security/resources/api-key/model.d.ts +2 -0
  197. package/security/resources/api-key/model.js +63 -0
  198. package/security/resources/api-key/policy.d.ts +2 -0
  199. package/security/resources/api-key/policy.js +26 -0
  200. package/security/resources/api-key/routes.d.ts +2 -0
  201. package/security/resources/api-key/routes.js +15 -0
  202. package/security/resources/api-key/service.d.ts +3 -0
  203. package/security/resources/api-key/service.js +33 -0
  204. package/security/resources/one-time-token/model.d.ts +2 -0
  205. package/security/resources/one-time-token/model.js +32 -0
  206. package/security/resources/permission/model.d.ts +2 -0
  207. package/security/resources/permission/model.js +16 -0
  208. package/security/resources/role/model.d.ts +2 -0
  209. package/security/resources/role/model.js +22 -0
  210. package/security/store/database-security-store.d.ts +5 -0
  211. package/security/store/database-security-store.js +51 -0
  212. package/security/store/redis-security-store.d.ts +5 -0
  213. package/security/store/redis-security-store.js +32 -0
  214. package/seeder/create-seeder.d.ts +21 -0
  215. package/seeder/create-seeder.js +17 -0
  216. package/seeder/index.d.ts +2 -0
  217. package/seeder/index.js +18 -0
  218. package/seeder/resources/seeder/model.d.ts +2 -0
  219. package/seeder/resources/seeder/model.js +38 -0
  220. package/seeder/seeder.d.ts +20 -0
  221. package/seeder/seeder.js +175 -0
  222. package/server/create-server.d.ts +20 -0
  223. package/server/create-server.js +160 -0
  224. package/server/index.d.ts +4 -0
  225. package/server/index.js +20 -0
  226. package/server/info-route.d.ts +2 -0
  227. package/server/info-route.js +18 -0
  228. package/server/info-schema.d.ts +37 -0
  229. package/server/info-schema.js +19 -0
  230. package/server/register-model.d.ts +2 -0
  231. package/server/register-model.js +12 -0
  232. package/server/register-plugin.d.ts +2 -0
  233. package/server/register-plugin.js +13 -0
  234. package/server/register-route.d.ts +3 -0
  235. package/server/register-route.js +74 -0
  236. package/server/schemas.d.ts +3 -0
  237. package/server/schemas.js +30 -0
  238. package/server/swagger.d.ts +3 -0
  239. package/server/swagger.js +122 -0
  240. package/storage/file-route.d.ts +2 -0
  241. package/storage/file-route.js +39 -0
  242. package/storage/file-schema.d.ts +9 -0
  243. package/storage/file-schema.js +42 -0
  244. package/storage/file-service.d.ts +81 -0
  245. package/storage/file-service.js +384 -0
  246. package/storage/filesystem-storage.d.ts +10 -0
  247. package/storage/filesystem-storage.js +128 -0
  248. package/storage/index.d.ts +2 -0
  249. package/storage/index.js +18 -0
  250. package/storage/resources/file/model.d.ts +2 -0
  251. package/storage/resources/file/model.js +75 -0
  252. package/types/application-context.d.ts +20 -0
  253. package/types/application-context.js +2 -0
  254. package/types/auth.d.ts +31 -0
  255. package/types/auth.js +2 -0
  256. package/types/fastify-extensions.d.ts +24 -0
  257. package/types/fastify-extensions.js +4 -0
  258. package/types/generated.d.ts +193 -0
  259. package/types/generated.js +3 -0
  260. package/types/index.d.ts +4 -0
  261. package/types/index.js +20 -0
  262. package/types/server.d.ts +4 -0
  263. package/types/server.js +2 -0
  264. package/utils/export-util.d.ts +8 -0
  265. package/utils/export-util.js +25 -0
  266. package/utils/file-util.d.ts +64 -0
  267. package/utils/file-util.js +141 -0
  268. package/utils/index.d.ts +3 -0
  269. package/utils/index.js +19 -0
  270. package/utils/schema-util.d.ts +20 -0
  271. package/utils/schema-util.js +37 -0
package/LICENSE ADDED
@@ -0,0 +1 @@
1
+ UNLICENSED
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # Appweaver - Core
2
+
3
+ > Simple, fast, and reliable web application builder tool.
4
+
5
+ ## License
6
+
7
+ UNLICENSED
@@ -0,0 +1,43 @@
1
+ import { Server } from '../types';
2
+ import { LifecycleManager } from './lifecycle-manager';
3
+ /**
4
+ * Represents an application that manages the lifecycle of a server and all
5
+ * defined services.
6
+ */
7
+ export declare class Application extends LifecycleManager {
8
+ private readonly _server;
9
+ private _started;
10
+ constructor(_server: Server);
11
+ /**
12
+ * Retrieves the Fastify instance.
13
+ *
14
+ * @return {Server} The underlying Fastify server instance.
15
+ */
16
+ get server(): Server;
17
+ /**
18
+ * Starts the application by initializing the server and calls onInit lifecycle methods for
19
+ * services that implement the `OnInit` interface. This process also includes logging the environment
20
+ * in which the application is running, freezing the application context to prevent further changes,
21
+ * and starting a server to listen for incoming requests.
22
+ *
23
+ * @return {Promise<string>} A promise that resolves to a string denoting the server URL on a successful start or
24
+ * empty string if server was not started.
25
+ */
26
+ start(startServer?: boolean): Promise<string>;
27
+ /**
28
+ * Stops the server by closing all active connections and releasing resources by calling onDestroy lifecycle
29
+ * methods for services that implement the `OnDestroy` interface.
30
+ * This method should be called to gracefully shut down the server.
31
+ *
32
+ * @return {Promise<void>} A promise that resolves when the server has been successfully stopped.
33
+ */
34
+ stop(): Promise<void>;
35
+ /**
36
+ * Generates and returns the OpenAPI specification in JSON or YAML format.
37
+ *
38
+ * @param {'json' | 'yaml'} [format='json'] - The format in which to generate specification.
39
+ * @return {Promise<string>} A promise that resolves to a JSON or YAML formatted string containing the OpenAPI
40
+ * specification.
41
+ */
42
+ spec(format?: 'json' | 'yaml'): Promise<string>;
43
+ }
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Application = void 0;
4
+ const common_1 = require("@appweaver/common");
5
+ const context_1 = require("../context");
6
+ const lifecycle_manager_1 = require("./lifecycle-manager");
7
+ /**
8
+ * Represents an application that manages the lifecycle of a server and all
9
+ * defined services.
10
+ */
11
+ class Application extends lifecycle_manager_1.LifecycleManager {
12
+ _server;
13
+ _started = false;
14
+ constructor(_server) {
15
+ super();
16
+ this._server = _server;
17
+ }
18
+ /**
19
+ * Retrieves the Fastify instance.
20
+ *
21
+ * @return {Server} The underlying Fastify server instance.
22
+ */
23
+ get server() {
24
+ return this._server;
25
+ }
26
+ /**
27
+ * Starts the application by initializing the server and calls onInit lifecycle methods for
28
+ * services that implement the `OnInit` interface. This process also includes logging the environment
29
+ * in which the application is running, freezing the application context to prevent further changes,
30
+ * and starting a server to listen for incoming requests.
31
+ *
32
+ * @return {Promise<string>} A promise that resolves to a string denoting the server URL on a successful start or
33
+ * empty string if server was not started.
34
+ */
35
+ async start(startServer = true) {
36
+ if (this._started) {
37
+ common_1.logger.warn('Trying to start already started application.');
38
+ return this._server.addresses()[0]?.address ?? '';
39
+ }
40
+ this._started = true;
41
+ common_1.logger.info(`Application started in "${common_1.config.APP_ENV}" environment`);
42
+ await this.init();
43
+ Object.freeze(context_1.context);
44
+ // Add a termination handler for gracefully stopping the application
45
+ process.on('SIGINT', async () => {
46
+ await this.stop();
47
+ process.exit(0);
48
+ });
49
+ // Add unhandled rejection handler
50
+ process.on('unhandledRejection', async (err) => {
51
+ common_1.logger.fatal(err, 'Unhandled rejection');
52
+ try {
53
+ await this.stop();
54
+ }
55
+ catch (e) {
56
+ common_1.logger.error(e, `Error while trying to stop application`);
57
+ }
58
+ process.exit(1);
59
+ });
60
+ // Add uncaught exception handler
61
+ process.on('uncaughtException', async (err) => {
62
+ common_1.logger.fatal(err, 'Uncaught exception');
63
+ try {
64
+ await this.stop();
65
+ }
66
+ catch (e) {
67
+ common_1.logger.error(e, `Error while trying to stop application`);
68
+ }
69
+ process.exit(2);
70
+ });
71
+ if (startServer) {
72
+ return this._server.listen({
73
+ port: common_1.config.SERVER_PORT,
74
+ host: common_1.config.SERVER_HOST
75
+ });
76
+ }
77
+ return '';
78
+ }
79
+ /**
80
+ * Stops the server by closing all active connections and releasing resources by calling onDestroy lifecycle
81
+ * methods for services that implement the `OnDestroy` interface.
82
+ * This method should be called to gracefully shut down the server.
83
+ *
84
+ * @return {Promise<void>} A promise that resolves when the server has been successfully stopped.
85
+ */
86
+ async stop() {
87
+ if (!this._started) {
88
+ common_1.logger.warn('Trying to stop already stopped or not started application.');
89
+ return;
90
+ }
91
+ common_1.logger.info('Application stopped');
92
+ this._started = false;
93
+ await this.destroy();
94
+ await this._server.close();
95
+ }
96
+ /**
97
+ * Generates and returns the OpenAPI specification in JSON or YAML format.
98
+ *
99
+ * @param {'json' | 'yaml'} [format='json'] - The format in which to generate specification.
100
+ * @return {Promise<string>} A promise that resolves to a JSON or YAML formatted string containing the OpenAPI
101
+ * specification.
102
+ */
103
+ async spec(format = 'json') {
104
+ await this._server.ready();
105
+ const document = this._server.swagger({ yaml: format === 'yaml' });
106
+ if (typeof document === 'string') {
107
+ return document;
108
+ }
109
+ return JSON.stringify(document, null, 4);
110
+ }
111
+ }
112
+ exports.Application = Application;
@@ -0,0 +1,32 @@
1
+ import { LoadResourcePaths } from '../resource';
2
+ import { Application } from './application';
3
+ export type CreateAppParams = {
4
+ /** A relative path where the application will load resources and other features.
5
+ * This is usually the directory from which the createApp() function is called.
6
+ * (default: ./dist/src) */
7
+ scanPath?: string;
8
+ /** A boolean flag indicating whether the application should automatically
9
+ * start after being created. (default: true) **/
10
+ autoStart?: boolean;
11
+ /** A boolean flag indicating whether the web server should automatically
12
+ * start after being created. (default: true) **/
13
+ autoStartServer?: boolean;
14
+ /** An object containing the path patterns for loading resources from. */
15
+ resourcePaths?: LoadResourcePaths;
16
+ /** Path pattern used for finding modules that export application logic
17
+ * (default: `./src/"*"/index.ts`) */
18
+ modulePaths?: string;
19
+ /** A boolean flag indicating whether the application should automatically load
20
+ * resources configured and exported using factory functions. (default: true) */
21
+ autoLoadResources?: boolean;
22
+ /** A boolean flag indicating whether the application should automatically load
23
+ * export modules from the project source directory. (default: true) */
24
+ autoLoadModules?: boolean;
25
+ };
26
+ /**
27
+ * Creates and initializes an application instance with the provided parameters.
28
+ *
29
+ * @param {CreateAppParams} [params={}] Configuration parameters for creating the application.
30
+ * @return {Promise<Application>} A promise that resolves to the initialized application instance.
31
+ */
32
+ export declare function createApp(params?: CreateAppParams): Promise<Application>;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createApp = createApp;
4
+ const common_1 = require("@appweaver/common");
5
+ const context_1 = require("../context");
6
+ const server_1 = require("../server");
7
+ const resource_1 = require("../resource");
8
+ const load_providers_1 = require("./load-providers");
9
+ const load_modules_1 = require("./load-modules");
10
+ const application_1 = require("./application");
11
+ /**
12
+ * Creates and initializes an application instance with the provided parameters.
13
+ *
14
+ * @param {CreateAppParams} [params={}] Configuration parameters for creating the application.
15
+ * @return {Promise<Application>} A promise that resolves to the initialized application instance.
16
+ */
17
+ async function createApp(params = {}) {
18
+ common_1.logger.debug({ configFiles: common_1.configFiles }, 'Configuration loaded');
19
+ const scanPath = (0, common_1.resolveSourcePath)(common_1.config.APP_BUILD_PATH, common_1.config.APP_SOURCE_PATH, params.scanPath, './dist/src');
20
+ // Load all defined providers from this project
21
+ (0, load_providers_1.loadProviders)(scanPath);
22
+ // Load resource models, routes, policies and services
23
+ if (params.autoLoadResources !== false) {
24
+ await (0, resource_1.loadResources)(scanPath, params.resourcePaths);
25
+ }
26
+ // Load exported application files
27
+ if (params.autoLoadModules !== false) {
28
+ await (0, load_modules_1.loadModules)(scanPath, params.modulePaths);
29
+ }
30
+ // Create a Fastify server instance
31
+ const server = (0, server_1.createServer)();
32
+ context_1.context.server = server;
33
+ const app = new application_1.Application(server);
34
+ if (params.autoStart !== false) {
35
+ await app.start(params.autoStartServer);
36
+ }
37
+ return app;
38
+ }
package/app/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './application';
2
+ export * from './create-app';
package/app/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./application"), exports);
18
+ __exportStar(require("./create-app"), exports);
@@ -0,0 +1,20 @@
1
+ export declare abstract class LifecycleManager {
2
+ private _initialized;
3
+ private readonly _initServices;
4
+ private readonly _destroyServices;
5
+ /**
6
+ * Initializes the services and sets the initialized state.
7
+ * This method ensures that the initialization logic is executed only once.
8
+ *
9
+ * @return A promise that resolves when all services have completed their initialization.
10
+ */
11
+ init(): Promise<void>;
12
+ /**
13
+ * Releases resources and cleans up any initialized services.
14
+ * This method ensures that all registered services perform their individual cleanup operations.
15
+ * It also sets the initialized state to false.
16
+ *
17
+ * @return A promise that resolves when all cleanup operations are completed.
18
+ */
19
+ destroy(): Promise<void>;
20
+ }
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LifecycleManager = void 0;
4
+ const common_1 = require("@appweaver/common");
5
+ const context_1 = require("../context");
6
+ class LifecycleManager {
7
+ _initialized = false;
8
+ _initServices = (0, context_1.injectAllWhere)((def) => (0, common_1.isLifecycleInit)(def.value));
9
+ _destroyServices = (0, context_1.injectAllWhere)((def) => (0, common_1.isLifecycleDestroy)(def.value));
10
+ /**
11
+ * Initializes the services and sets the initialized state.
12
+ * This method ensures that the initialization logic is executed only once.
13
+ *
14
+ * @return A promise that resolves when all services have completed their initialization.
15
+ */
16
+ async init() {
17
+ if (this._initialized) {
18
+ return;
19
+ }
20
+ await Promise.all([
21
+ ...this._initServices.map((service) => service.onInit())
22
+ ]);
23
+ this._initialized = true;
24
+ }
25
+ /**
26
+ * Releases resources and cleans up any initialized services.
27
+ * This method ensures that all registered services perform their individual cleanup operations.
28
+ * It also sets the initialized state to false.
29
+ *
30
+ * @return A promise that resolves when all cleanup operations are completed.
31
+ */
32
+ async destroy() {
33
+ if (!this._initialized) {
34
+ return;
35
+ }
36
+ await Promise.all([
37
+ ...this._destroyServices.map((service) => service.onDestroy())
38
+ ]);
39
+ this._initialized = false;
40
+ }
41
+ }
42
+ exports.LifecycleManager = LifecycleManager;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Loads and initializes modules exported from the application using the provided base directory and file pattern.
3
+ *
4
+ * @param {string} [baseDir] - The base directory containing the module files to load.
5
+ * @param {string} [moduleFilesPattern] - Path patterns for loading files that export application logic like:
6
+ * models, routes, plugins, scheduled jobs, queue workers, etc.
7
+ * @return {Promise<void>} A promise that resolves when the modules are successfully loaded,
8
+ * or rejects if an error occurs during the process.
9
+ */
10
+ export declare function loadModules(baseDir?: string, moduleFilesPattern?: string): Promise<void>;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.loadModules = loadModules;
7
+ const promises_1 = __importDefault(require("node:fs/promises"));
8
+ const common_1 = require("@appweaver/common");
9
+ /**
10
+ * Loads and initializes modules exported from the application using the provided base directory and file pattern.
11
+ *
12
+ * @param {string} [baseDir] - The base directory containing the module files to load.
13
+ * @param {string} [moduleFilesPattern] - Path patterns for loading files that export application logic like:
14
+ * models, routes, plugins, scheduled jobs, queue workers, etc.
15
+ * @return {Promise<void>} A promise that resolves when the modules are successfully loaded,
16
+ * or rejects if an error occurs during the process.
17
+ */
18
+ async function loadModules(baseDir, moduleFilesPattern) {
19
+ const cwd = baseDir ?? process.cwd();
20
+ const pathPattern = moduleFilesPattern || common_1.config.APP_SCAN_FILES_PATTERN;
21
+ // Find all project files matching provided glob pattern
22
+ const modulePaths = await (0, common_1.findFilesByPattern)(pathPattern, cwd);
23
+ for (const path of modulePaths) {
24
+ await loadModule(path);
25
+ }
26
+ }
27
+ /**
28
+ * Loads a module from the specified file path. Reads the file content and
29
+ * ensures it meets the criteria before attempting to dynamically import the module.
30
+ * Logs an error if module loading fails.
31
+ *
32
+ * @param {string} modulePath - The file path of the module to be loaded.
33
+ * @return {Promise<void>} A promise that resolves when the module is successfully loaded or rejects if an error occurs.
34
+ */
35
+ async function loadModule(modulePath) {
36
+ try {
37
+ const fileContent = await promises_1.default.readFile(modulePath, 'utf8');
38
+ if (fileContent.split('\n').length < 3) {
39
+ return;
40
+ }
41
+ }
42
+ catch (error) {
43
+ common_1.logger.error(error, `Error while loading module`);
44
+ return;
45
+ }
46
+ const { error } = await (0, common_1.importModule)(modulePath);
47
+ if (error) {
48
+ throw error;
49
+ }
50
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Loads and registers the core and infrastructure service definitions.
3
+ * This method initializes and sets up various service components, such as the
4
+ * database, cache, queue system, mailer, storage, and authentication services, among others.
5
+ *
6
+ * @param {string} baseDir - The base directory containing the module files.
7
+ */
8
+ export declare function loadProviders(baseDir: string): void;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadProviders = loadProviders;
4
+ const common_1 = require("@appweaver/common");
5
+ const context_1 = require("../context");
6
+ /**
7
+ * Loads and registers the core and infrastructure service definitions.
8
+ * This method initializes and sets up various service components, such as the
9
+ * database, cache, queue system, mailer, storage, and authentication services, among others.
10
+ *
11
+ * @param {string} baseDir - The base directory containing the module files.
12
+ */
13
+ function loadProviders(baseDir) {
14
+ // Common infrastructure services
15
+ (0, context_1.loadProvider)(baseDir, common_1.config.DATABASE_PROVIDER, common_1.Database);
16
+ (0, context_1.loadProvider)(baseDir, common_1.config.STORAGE_PROVIDER, common_1.Storage);
17
+ (0, context_1.loadProvider)(baseDir, common_1.config.MEMORY_PROVIDER, common_1.Memory);
18
+ (0, context_1.loadProvider)(baseDir, common_1.config.CACHE_PROVIDER, common_1.Cache);
19
+ (0, context_1.loadProvider)(baseDir, common_1.config.EVENTS_PROVIDER, common_1.Events);
20
+ (0, context_1.loadProvider)(baseDir, common_1.config.SECURITY_STORE_PROVIDER, common_1.SecurityStore);
21
+ (0, context_1.loadProvider)(baseDir, common_1.config.REDIS_PROVIDER, common_1.Redis, false);
22
+ (0, context_1.loadProvider)(baseDir, common_1.config.QUEUE_PROVIDER, common_1.Queue, false);
23
+ (0, context_1.loadProvider)(baseDir, common_1.config.MAILER_PROVIDER, common_1.Mailer, false);
24
+ (0, context_1.loadProvider)(baseDir, common_1.config.SCHEDULER_PROVIDER, common_1.Scheduler, false);
25
+ // Core feature services
26
+ (0, context_1.loadProvider)(__dirname, '../mailer/email-service', undefined, false);
27
+ (0, context_1.loadProvider)(__dirname, '../cache/cache-service');
28
+ (0, context_1.loadProvider)(__dirname, '../storage/file-service');
29
+ (0, context_1.loadProvider)(__dirname, '../security/auth-service');
30
+ (0, context_1.loadProvider)(__dirname, '../security/account/account-service');
31
+ (0, context_1.loadProvider)(__dirname, '../export/export-service');
32
+ (0, context_1.loadProvider)(__dirname, '../health/health-service');
33
+ }
@@ -0,0 +1,72 @@
1
+ import { AuthUser, Cache } from '@appweaver/common';
2
+ export declare class CacheService {
3
+ /**
4
+ * Retrieves the cache instance associated with this service.
5
+ *
6
+ * @return {Cache} The current cache instance.
7
+ */
8
+ get cache(): Cache;
9
+ /**
10
+ * Retrieves a cached value associated with the specified key.
11
+ *
12
+ * @param {string} key - The key used to look up the cached value.
13
+ * @return A promise that resolves to the cached value if the key exists, or null if the key is not found.
14
+ */
15
+ getCachedValue<T>(key: string): Promise<T | null>;
16
+ /**
17
+ * Adds a key-value pair to the cache if the key does not already exist.
18
+ * Optionally allows specifying a time-to-live (TTL) for the cached item.
19
+ *
20
+ * @param {string} key - The unique key to identify the value in the cache.
21
+ * @param {Object} value - The value to store in the cache.
22
+ * @param {number} [ttl] - Optional time-to-live for the cache entry in seconds.
23
+ * @param {boolean} [replace=false] - Optional flag that indicates if the existing cache entry should be replaced
24
+ * with the new value and ttl.
25
+ * @return {Promise<boolean>} A promise that resolves to true if the key-value pair was added, or false if the key
26
+ * already exists.
27
+ */
28
+ addToCache(key: string, value: any, ttl?: number, replace?: boolean): Promise<boolean>;
29
+ /**
30
+ * Removes a cached value associated with the specified key from the cache store.
31
+ *
32
+ * @param {string} key - The key associated with the cached value to be removed.
33
+ * @return {Promise<boolean>} A promise that resolves to `true` if the value was successfully removed, or
34
+ * `false` otherwise.
35
+ */
36
+ removeCachedValue(key: string): Promise<boolean>;
37
+ /**
38
+ * Invalidates the cache for the specified model and action type based on the configured cache invalidation strategy.
39
+ *
40
+ * @param {string} modelName - The name of the model whose cache needs to be invalidated.
41
+ * @param {Extract<ActionType, 'create' | 'update' | 'delete' | 'uploadFiles' | 'deleteFiles'>} action - The action type
42
+ * that triggered the cache invalidation (e.g., 'create', 'update', 'delete', 'uploadFiles', or 'fileDelete').
43
+ * @return {Promise<void>} Resolves when the cache invalidation process is completed.
44
+ */
45
+ invalidateCache(modelName: string, action: 'create' | 'update' | 'delete' | 'uploadFiles' | 'deleteFiles'): Promise<void>;
46
+ /**
47
+ * Builds a unique cache key based on the provided input parameters. The key is constructed
48
+ * using properties such as the base key, HTTP method, URL, request body, model name, and any
49
+ * associated relations. It also includes optional user-specific data and cache invalidation settings.
50
+ *
51
+ * @param {Object} data Input data for constructing the cache key.
52
+ * @param {string} data.baseKey The base key that serves as the starting point of the cache key.
53
+ * @param {string} [data.method] The HTTP method (e.g., GET, POST) associated with the key.
54
+ * @param {string} [data.url] The URL associated with the request.
55
+ * @param {string} [data.body] The stringified request body, which will be hashed if present.
56
+ * @param {string} [data.modelName] The name of the model to associate with the cache key.
57
+ * @param {string[]} [data.relations] A list of related model names to associate with the cache key.
58
+ * @param {boolean} [data.skipInvalidation] Whether to skip invalidation logic for this cache key.
59
+ * @param {AuthUser} [data.authUser] The authenticated user information, if applicable, to create user-specific keys.
60
+ * @return {string} The generated cache key string.
61
+ */
62
+ buildCacheKey(data: {
63
+ baseKey: string;
64
+ method?: string;
65
+ url?: string;
66
+ body?: string;
67
+ modelName?: string;
68
+ relations?: string[];
69
+ skipInvalidation?: boolean;
70
+ authUser?: AuthUser;
71
+ }): string;
72
+ }