@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
@@ -0,0 +1,8 @@
1
+ import { CacheEntryMeta } from '@appweaver/common';
2
+ import { EvictionIndex } from './eviction-index';
3
+ export declare class LruEvictionIndex implements EvictionIndex {
4
+ add(key: string, meta: CacheEntryMeta): void;
5
+ remove(key: string): void;
6
+ touch(key: string, meta: CacheEntryMeta): void;
7
+ evictionCandidates(count: number, now: number, gracePeriod: number): string[];
8
+ }
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LruEvictionIndex = void 0;
4
+ class LruEvictionIndex {
5
+ /** @internal */
6
+ _nodes = new Map();
7
+ /** @internal */
8
+ _head = null;
9
+ /** @internal */
10
+ _tail = null;
11
+ add(key, meta) {
12
+ if (this._nodes.has(key)) {
13
+ this.remove(key);
14
+ }
15
+ const node = {
16
+ key,
17
+ meta,
18
+ prev: null,
19
+ next: null
20
+ };
21
+ this.appendToTail(node);
22
+ this._nodes.set(key, node);
23
+ }
24
+ remove(key) {
25
+ const node = this._nodes.get(key);
26
+ if (!node) {
27
+ return;
28
+ }
29
+ this.detach(node);
30
+ this._nodes.delete(key);
31
+ }
32
+ touch(key, meta) {
33
+ const node = this._nodes.get(key);
34
+ if (!node) {
35
+ return;
36
+ }
37
+ this.detach(node);
38
+ this.appendToTail(node);
39
+ }
40
+ evictionCandidates(count, now, gracePeriod) {
41
+ const candidates = [];
42
+ let current = this._head;
43
+ while (current && candidates.length < count) {
44
+ if (current.meta.createdAt + gracePeriod < now) {
45
+ candidates.push(current.key);
46
+ }
47
+ current = current.next;
48
+ }
49
+ return candidates;
50
+ }
51
+ /** @internal */
52
+ appendToTail(node) {
53
+ node.prev = this._tail;
54
+ node.next = null;
55
+ if (this._tail) {
56
+ this._tail.next = node;
57
+ }
58
+ else {
59
+ this._head = node;
60
+ }
61
+ this._tail = node;
62
+ }
63
+ /** @internal */
64
+ detach(node) {
65
+ if (node.prev) {
66
+ node.prev.next = node.next;
67
+ }
68
+ else {
69
+ this._head = node.next;
70
+ }
71
+ if (node.next) {
72
+ node.next.prev = node.prev;
73
+ }
74
+ else {
75
+ this._tail = node.prev;
76
+ }
77
+ node.prev = null;
78
+ node.next = null;
79
+ }
80
+ }
81
+ exports.LruEvictionIndex = LruEvictionIndex;
@@ -0,0 +1,2 @@
1
+ export * from './cache-service';
2
+ export * from './caching';
package/cache/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("./cache-service"), exports);
18
+ __exportStar(require("./caching"), exports);
@@ -0,0 +1,4 @@
1
+ import { Cache } from './cache';
2
+ export declare class MemoryCache extends Cache {
3
+ constructor();
4
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MemoryCache = void 0;
4
+ const common_1 = require("@appweaver/common");
5
+ const context_1 = require("../context");
6
+ const cache_1 = require("./cache");
7
+ class MemoryCache extends cache_1.Cache {
8
+ constructor() {
9
+ super((0, context_1.inject)(common_1.Memory));
10
+ }
11
+ }
12
+ exports.MemoryCache = MemoryCache;
@@ -0,0 +1,4 @@
1
+ import { Cache } from './cache';
2
+ export declare class RedisCache extends Cache {
3
+ constructor();
4
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RedisCache = void 0;
4
+ const common_1 = require("@appweaver/common");
5
+ const context_1 = require("../context");
6
+ const cache_1 = require("./cache");
7
+ class RedisCache extends cache_1.Cache {
8
+ constructor() {
9
+ super((0, context_1.inject)(common_1.Redis));
10
+ }
11
+ }
12
+ exports.RedisCache = RedisCache;
@@ -0,0 +1,2 @@
1
+ import { ApplicationContext } from '../types';
2
+ export declare const context: ApplicationContext;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.context = void 0;
4
+ exports.context = {
5
+ server: null,
6
+ resource: {
7
+ models: new Map(),
8
+ services: new Map(),
9
+ policies: new Map(),
10
+ routes: new Map()
11
+ },
12
+ definitions: []
13
+ };
@@ -0,0 +1,98 @@
1
+ import { ConditionalOptional, FunctionType, IResourceService, ResourceModel, ResourcePolicyConfig, ResourceRoutes } from '@appweaver/common';
2
+ import { DefinitionClass, DefinitionEntry, DefinitionMode, DefinitionValue } from '../types';
3
+ /**
4
+ * Defines a resource or a definition value within the application context.
5
+ *
6
+ * This method supports various resource models, services, routes, and policies,
7
+ * determining whether to add or override the definition in the application context
8
+ * based on the provided mode.
9
+ *
10
+ * @param {DefinitionValue} value The resource or definition value to be registered.
11
+ * @param {string | symbol | DefinitionClass | FunctionType | undefined} nameOrClass Optional name or class reference
12
+ * for the definition. If not provided, the name is inferred from the resource or its constructor.
13
+ * @param mode Determines how the definition should be treated:
14
+ * 'ignore' - Adds only if the definition doesn’t already exist.
15
+ * 'override' - Replaces an existing definition if it matches the name.
16
+ * Defaults to 'ignore'.
17
+ * @return void
18
+ */
19
+ export declare function define<T = DefinitionValue, S extends T = T>(value: DefinitionValue, nameOrClass?: string | symbol | DefinitionClass<S> | FunctionType, mode?: DefinitionMode): void;
20
+ /**
21
+ * Loads a class from the specified path, resolves its constructor, and defines it with the provided definition.
22
+ *
23
+ * @param {string} baseDir - The base directory used to resolve relative and local class paths.
24
+ * @param {string} classPath - The path to the module to be loaded. Can be a relative path (`../services/example`),
25
+ * project source path (`@/features/services/example`), or node_modules package (`@appweaver/core/service/example`).
26
+ * @param {DefinitionClass} [definition] - An optional definition object used to define the loaded module.
27
+ * @param {boolean} [required=true] - Indicates whether the module is required. If true, an error is thrown on failure;
28
+ * otherwise, a warning is logged.
29
+ * @return This function does not return a value. It loads a module and defines it if successful.
30
+ */
31
+ export declare function loadProvider(baseDir: string, classPath: string, definition?: DefinitionClass, required?: boolean): void;
32
+ /**
33
+ * Retrieves a definition from the application context based on the provided name.
34
+ * The method handles specific naming conventions for models, services, routes, and policies,
35
+ * and attempts to resolve the appropriate definition. Throws an error if the definition is
36
+ * not found, and it is marked as required.
37
+ *
38
+ * @param {string | symbol | DefinitionClass | FunctionType} nameOrClass - The name or class of the definition to retrieve.
39
+ * Naming conventions such as suffixes 'Model', 'Service', 'Routes', or 'Policy'
40
+ * are supported.
41
+ * @param {boolean} [required=true] - Indicates whether the definition is required. If true
42
+ * and the definition is not found, an error is thrown.
43
+ * @return T - The resolved definition, typed as `T`. Will be `undefined` if the definition
44
+ * is not found and `required` is set to `false`.
45
+ */
46
+ export declare function inject<T = DefinitionValue, R extends boolean = true>(nameOrClass: string | symbol | DefinitionClass<T> | FunctionType, required?: R): ConditionalOptional<R, T>;
47
+ /**
48
+ * Injects all definitions that match the specified name or class.
49
+ *
50
+ * @param {string | symbol | DefinitionClass | FunctionType} nameOrClass The name of the definition or the
51
+ * class constructor to find matching definitions.
52
+ * @return {[]} An array of definitions that match the provided name or class.
53
+ */
54
+ export declare function injectAll<T = DefinitionValue>(nameOrClass: string | symbol | DefinitionClass<T> | FunctionType): T[];
55
+ /**
56
+ * Injects all definitions that satisfy the specified search function criteria.
57
+ *
58
+ * @param {function(DefinitionValue): boolean} searchFunction - A callback function used to filter definitions.
59
+ * It should return true for definitions that match the desired criteria.
60
+ * @return {[]} An array of definitions that meet the criteria specified by the search function.
61
+ */
62
+ export declare function injectAllWhere<T = DefinitionValue>(searchFunction: (definition: DefinitionEntry) => boolean): T[];
63
+ /**
64
+ * Injects a model from the application context based on the provided model name.
65
+ * Throws an error if the model is not found and the required flag is set to true.
66
+ *
67
+ * @param {string} name - The name of the model to inject.
68
+ * @param {boolean} [required=true] - Indicates whether the model is required. If true, an error is thrown when the model is not found.
69
+ * @return {ResourceModel} The injected model from the application context.
70
+ */
71
+ export declare function injectModel<T = ResourceModel, R extends boolean = true>(name: string, required?: R): ConditionalOptional<R, T>;
72
+ /**
73
+ * Injects and retrieves a service instance from the application context based on the provided model name.
74
+ *
75
+ * @param {string} modelName - The name of the model whose service needs to be injected.
76
+ * @param {boolean} [required=true] - Indicates if the service is mandatory. If true and the service is not available, an error is thrown.
77
+ * @return {IResourceService} The service instance corresponding to the specified model name.
78
+ * @throws {Error} If the service cannot be found in the application context and `required` is true.
79
+ */
80
+ export declare function injectService<T = IResourceService, R extends boolean = true>(modelName: string, required?: R): ConditionalOptional<R, T>;
81
+ /**
82
+ * Injects a route into the application context based on the provided route name.
83
+ *
84
+ * @param {string} modelName - The name of the model whose route to retrieve from the application context.
85
+ * @param {boolean} [required=true] - Specifies whether the route is mandatory. If true and the route is not found, an error will be thrown.
86
+ * @return {ResourceRoutes} The retrieved route configuration.
87
+ * @throws {Error} If the route with the specified name is not found, and the `required` parameter is set to true.
88
+ */
89
+ export declare function injectRoutes<R extends boolean = true>(modelName: string, required?: R): ConditionalOptional<R, ResourceRoutes>;
90
+ /**
91
+ * Injects a policy configuration from the application context by its name.
92
+ *
93
+ * @param {string} modelName - The name of the model whose policy to retrieve.
94
+ * @param {boolean} [required=true] - Indicates whether the policy is required. If true and the policy does not exist, an error is thrown.
95
+ * @return {ResourcePolicyConfig} The policy configuration object associated with the provided name.
96
+ * @throws {Error} If the policy is required and not found in the application context.
97
+ */
98
+ export declare function injectPolicy<R extends boolean = true>(modelName: string, required?: R): ConditionalOptional<R, ResourcePolicyConfig>;
@@ -0,0 +1,276 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.define = define;
4
+ exports.loadProvider = loadProvider;
5
+ exports.inject = inject;
6
+ exports.injectAll = injectAll;
7
+ exports.injectAllWhere = injectAllWhere;
8
+ exports.injectModel = injectModel;
9
+ exports.injectService = injectService;
10
+ exports.injectRoutes = injectRoutes;
11
+ exports.injectPolicy = injectPolicy;
12
+ const common_1 = require("@appweaver/common");
13
+ const context_1 = require("./context");
14
+ /**
15
+ * Defines a resource or a definition value within the application context.
16
+ *
17
+ * This method supports various resource models, services, routes, and policies,
18
+ * determining whether to add or override the definition in the application context
19
+ * based on the provided mode.
20
+ *
21
+ * @param {DefinitionValue} value The resource or definition value to be registered.
22
+ * @param {string | symbol | DefinitionClass | FunctionType | undefined} nameOrClass Optional name or class reference
23
+ * for the definition. If not provided, the name is inferred from the resource or its constructor.
24
+ * @param mode Determines how the definition should be treated:
25
+ * 'ignore' - Adds only if the definition doesn’t already exist.
26
+ * 'override' - Replaces an existing definition if it matches the name.
27
+ * Defaults to 'ignore'.
28
+ * @return void
29
+ */
30
+ function define(value, nameOrClass, mode = 'ignore') {
31
+ const definitionName = (0, common_1.isString)(nameOrClass) || (0, common_1.isSymbol)(nameOrClass)
32
+ ? nameOrClass
33
+ : (nameOrClass?.name ??
34
+ value[common_1.RESOURCE_NAME] ??
35
+ value.name ??
36
+ value.constructor?.name);
37
+ if ((0, common_1.isResourceModel)(value)) {
38
+ if (shouldAddDefinition(context_1.context.resource.models, definitionName, mode)) {
39
+ context_1.context.resource.models.set(definitionName, value);
40
+ }
41
+ }
42
+ else if ((0, common_1.isResourceService)(value)) {
43
+ if (shouldAddDefinition(context_1.context.resource.services, definitionName, mode)) {
44
+ context_1.context.resource.services.set(definitionName, value);
45
+ }
46
+ }
47
+ else if ((0, common_1.isResourceRoutes)(value)) {
48
+ if (shouldAddDefinition(context_1.context.resource.routes, definitionName, mode)) {
49
+ context_1.context.resource.routes.set(definitionName, value);
50
+ }
51
+ }
52
+ else if ((0, common_1.isResourcePolicy)(value)) {
53
+ if (shouldAddDefinition(context_1.context.resource.policies, definitionName, mode)) {
54
+ context_1.context.resource.policies.set(definitionName, value);
55
+ }
56
+ }
57
+ else {
58
+ const shouldAdd = shouldAddDefinition(context_1.context.definitions, definitionName, mode);
59
+ if (shouldAdd) {
60
+ const index = mode === 'override'
61
+ ? context_1.context.definitions.findIndex((def) => def.name === definitionName)
62
+ : -1;
63
+ if (index > -1) {
64
+ context_1.context.definitions.splice(index, 1, { name: definitionName, value });
65
+ }
66
+ else {
67
+ context_1.context.definitions.push({ name: definitionName, value });
68
+ }
69
+ }
70
+ }
71
+ }
72
+ /**
73
+ * Loads a class from the specified path, resolves its constructor, and defines it with the provided definition.
74
+ *
75
+ * @param {string} baseDir - The base directory used to resolve relative and local class paths.
76
+ * @param {string} classPath - The path to the module to be loaded. Can be a relative path (`../services/example`),
77
+ * project source path (`@/features/services/example`), or node_modules package (`@appweaver/core/service/example`).
78
+ * @param {DefinitionClass} [definition] - An optional definition object used to define the loaded module.
79
+ * @param {boolean} [required=true] - Indicates whether the module is required. If true, an error is thrown on failure;
80
+ * otherwise, a warning is logged.
81
+ * @return This function does not return a value. It loads a module and defines it if successful.
82
+ */
83
+ function loadProvider(baseDir, classPath, definition, required = true) {
84
+ const { value, error } = (0, common_1.loadModule)(baseDir, classPath);
85
+ // Handle errors or missing values for both required and optional modules
86
+ if (!value || error) {
87
+ const msg = `Loading '${classPath}' module failed`;
88
+ if (required) {
89
+ common_1.logger.error(error, msg);
90
+ throw error ?? new Error(msg);
91
+ }
92
+ common_1.logger.warn(error, msg);
93
+ return;
94
+ }
95
+ // Extract class constructor from the exported value and add it to definitions
96
+ const ctor = Object.values(value).find((exportedValue) => (0, common_1.isConstructor)(exportedValue));
97
+ // Define the first found class constructor, if no constructor found then
98
+ // define anything that was first exported from this file
99
+ define(ctor ?? Object.values(value)[0], definition);
100
+ }
101
+ /**
102
+ * Retrieves a definition from the application context based on the provided name.
103
+ * The method handles specific naming conventions for models, services, routes, and policies,
104
+ * and attempts to resolve the appropriate definition. Throws an error if the definition is
105
+ * not found, and it is marked as required.
106
+ *
107
+ * @param {string | symbol | DefinitionClass | FunctionType} nameOrClass - The name or class of the definition to retrieve.
108
+ * Naming conventions such as suffixes 'Model', 'Service', 'Routes', or 'Policy'
109
+ * are supported.
110
+ * @param {boolean} [required=true] - Indicates whether the definition is required. If true
111
+ * and the definition is not found, an error is thrown.
112
+ * @return T - The resolved definition, typed as `T`. Will be `undefined` if the definition
113
+ * is not found and `required` is set to `false`.
114
+ */
115
+ function inject(nameOrClass, required = true) {
116
+ let definition;
117
+ let name;
118
+ if ((0, common_1.isString)(nameOrClass)) {
119
+ name = nameOrClass;
120
+ if (name.endsWith('Model')) {
121
+ definition = context_1.context.resource.models.get(name.replace(/Model$/, ''));
122
+ }
123
+ else if (name.endsWith('Service')) {
124
+ definition = context_1.context.resource.services.get(name.replace(/Service$/, ''));
125
+ }
126
+ else if (name.endsWith('Routes')) {
127
+ definition = context_1.context.resource.routes.get(name.replace(/Routes$/, ''));
128
+ }
129
+ else if (name.endsWith('Policy')) {
130
+ definition = context_1.context.resource.policies.get(name.replace(/Policy$/, ''));
131
+ }
132
+ else {
133
+ definition = findFirstDefinition(name);
134
+ }
135
+ }
136
+ else {
137
+ name = (0, common_1.isSymbol)(nameOrClass) ? nameOrClass : nameOrClass.name;
138
+ definition = findFirstDefinition(name);
139
+ }
140
+ if (!definition && required) {
141
+ throw new Error(`Definition '${String(name)}' is not defined in the application context`);
142
+ }
143
+ return checkClassAndInit(nameOrClass, definition);
144
+ }
145
+ /**
146
+ * Injects all definitions that match the specified name or class.
147
+ *
148
+ * @param {string | symbol | DefinitionClass | FunctionType} nameOrClass The name of the definition or the
149
+ * class constructor to find matching definitions.
150
+ * @return {[]} An array of definitions that match the provided name or class.
151
+ */
152
+ function injectAll(nameOrClass) {
153
+ const name = (0, common_1.isString)(nameOrClass) || (0, common_1.isSymbol)(nameOrClass)
154
+ ? nameOrClass
155
+ : nameOrClass.name;
156
+ const defs = context_1.context.definitions.filter((def) => def.name === name);
157
+ return defs.map((def) => checkClassAndInit(def.name, def.value));
158
+ }
159
+ /**
160
+ * Injects all definitions that satisfy the specified search function criteria.
161
+ *
162
+ * @param {function(DefinitionValue): boolean} searchFunction - A callback function used to filter definitions.
163
+ * It should return true for definitions that match the desired criteria.
164
+ * @return {[]} An array of definitions that meet the criteria specified by the search function.
165
+ */
166
+ function injectAllWhere(searchFunction) {
167
+ const defs = context_1.context.definitions.filter(searchFunction);
168
+ return defs.map((def) => checkClassAndInit(def.name, def.value));
169
+ }
170
+ /**
171
+ * Injects a model from the application context based on the provided model name.
172
+ * Throws an error if the model is not found and the required flag is set to true.
173
+ *
174
+ * @param {string} name - The name of the model to inject.
175
+ * @param {boolean} [required=true] - Indicates whether the model is required. If true, an error is thrown when the model is not found.
176
+ * @return {ResourceModel} The injected model from the application context.
177
+ */
178
+ function injectModel(name, required = true) {
179
+ const model = context_1.context.resource.models.get(name);
180
+ if (!model && required) {
181
+ throw new Error(`Model '${name}' is not defined in the application context`);
182
+ }
183
+ return model;
184
+ }
185
+ /**
186
+ * Injects and retrieves a service instance from the application context based on the provided model name.
187
+ *
188
+ * @param {string} modelName - The name of the model whose service needs to be injected.
189
+ * @param {boolean} [required=true] - Indicates if the service is mandatory. If true and the service is not available, an error is thrown.
190
+ * @return {IResourceService} The service instance corresponding to the specified model name.
191
+ * @throws {Error} If the service cannot be found in the application context and `required` is true.
192
+ */
193
+ function injectService(modelName, required = true) {
194
+ const service = context_1.context.resource.services.get(modelName);
195
+ if (!service && required) {
196
+ throw new Error(`Service for model '${modelName}' is not defined in the application context`);
197
+ }
198
+ return checkClassAndInit(modelName, service);
199
+ }
200
+ /**
201
+ * Injects a route into the application context based on the provided route name.
202
+ *
203
+ * @param {string} modelName - The name of the model whose route to retrieve from the application context.
204
+ * @param {boolean} [required=true] - Specifies whether the route is mandatory. If true and the route is not found, an error will be thrown.
205
+ * @return {ResourceRoutes} The retrieved route configuration.
206
+ * @throws {Error} If the route with the specified name is not found, and the `required` parameter is set to true.
207
+ */
208
+ function injectRoutes(modelName, required = true) {
209
+ const routes = context_1.context.resource.routes.get(modelName);
210
+ if (!routes && required) {
211
+ throw new Error(`Routes for model '${modelName}' are not defined in the application context`);
212
+ }
213
+ return routes;
214
+ }
215
+ /**
216
+ * Injects a policy configuration from the application context by its name.
217
+ *
218
+ * @param {string} modelName - The name of the model whose policy to retrieve.
219
+ * @param {boolean} [required=true] - Indicates whether the policy is required. If true and the policy does not exist, an error is thrown.
220
+ * @return {ResourcePolicyConfig} The policy configuration object associated with the provided name.
221
+ * @throws {Error} If the policy is required and not found in the application context.
222
+ */
223
+ function injectPolicy(modelName, required = true) {
224
+ const policy = context_1.context.resource.policies.get(modelName);
225
+ if (!policy && required) {
226
+ throw new Error(`Policy for model '${modelName}' is not defined in the application context`);
227
+ }
228
+ return policy;
229
+ }
230
+ /**
231
+ * Checks the existence of a definition in the context store and returns if it should be added or ignored.
232
+ *
233
+ * @param {Map<string | symbol, any> | DefinitionEntry[]} store - The storage object or array where definitions are maintained.
234
+ * @param {string | symbol} name - The name of the definition to check.
235
+ * @param {boolean} mode - How to resolve already defined definitions in context.
236
+ * @return {boolean} Returns true if definition exists, false otherwise.
237
+ */
238
+ function shouldAddDefinition(store, name, mode) {
239
+ if ((((0, common_1.isArray)(store) && findFirstDefinition(name)) ||
240
+ (!(0, common_1.isArray)(store) && store.has(name))) &&
241
+ !['append', 'override'].includes(mode)) {
242
+ const msg = `Definition '${String(name)}' is already present in the application context.`;
243
+ if (mode === 'fail') {
244
+ throw new Error(msg);
245
+ }
246
+ common_1.logger.warn(`${msg} Use 'append' or 'override' to remove this warning.`);
247
+ return false;
248
+ }
249
+ return true;
250
+ }
251
+ /**
252
+ * Finds the first definition object that matches the given name.
253
+ *
254
+ * @param {string | symbol} name - The name of the definition to search for.
255
+ * @return {DefinitionValue|undefined} The first matching definition if found, otherwise undefined.
256
+ */
257
+ function findFirstDefinition(name) {
258
+ return context_1.context.definitions.find((def) => def.name === name)?.value;
259
+ }
260
+ /**
261
+ * Checks the provided class or definition and initializes it if necessary.
262
+ *
263
+ * @param {string | symbol | DefinitionClass | FunctionType} nameOrClass - The name or class to evaluate and potentially initialize.
264
+ * @param {DefinitionValue} [value] - The value to check and initialize if it is a constructor.
265
+ * @return {DefinitionValue | undefined} The initialized value or undefined if no initialization was performed.
266
+ */
267
+ function checkClassAndInit(nameOrClass, value) {
268
+ if (value && (0, common_1.isConstructor)(value)) {
269
+ const instance = new value();
270
+ if (instance) {
271
+ define(instance, nameOrClass, 'override');
272
+ return instance;
273
+ }
274
+ }
275
+ return value;
276
+ }
@@ -0,0 +1,2 @@
1
+ export * from './context';
2
+ export * from './dependency-injection';
@@ -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("./context"), exports);
18
+ __exportStar(require("./dependency-injection"), exports);
@@ -0,0 +1,11 @@
1
+ import { PrismaClient } from '../prisma/client/client';
2
+ /**
3
+ * Creates and returns a PrismaClient instance based on the configured database type.
4
+ *
5
+ * The database type is determined by the `DATABASE_TYPE` configuration value or,
6
+ * if unspecified, by parsing the `DATABASE_URL` configuration.
7
+ * Supported database types include SQLite, PostgresSQL, and MySQL.
8
+ *
9
+ * @return {PrismaClient} A PrismaClient instance configured for the selected database type.
10
+ */
11
+ export declare function createClient(): PrismaClient;