@flowerforce/flowerbase 1.0.1-beta.10

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 (292) hide show
  1. package/CHANGELOG.md +0 -0
  2. package/LICENSE +3 -0
  3. package/README.md +18 -0
  4. package/dist/auth/controller.d.ts +8 -0
  5. package/dist/auth/controller.d.ts.map +1 -0
  6. package/dist/auth/controller.js +76 -0
  7. package/dist/auth/dtos.d.ts +6 -0
  8. package/dist/auth/dtos.d.ts.map +1 -0
  9. package/dist/auth/dtos.js +2 -0
  10. package/dist/auth/plugins/jwt.d.ts +14 -0
  11. package/dist/auth/plugins/jwt.d.ts.map +1 -0
  12. package/dist/auth/plugins/jwt.js +68 -0
  13. package/dist/auth/providers/local-userpass/controller.d.ts +8 -0
  14. package/dist/auth/providers/local-userpass/controller.d.ts.map +1 -0
  15. package/dist/auth/providers/local-userpass/controller.js +184 -0
  16. package/dist/auth/providers/local-userpass/dtos.d.ts +35 -0
  17. package/dist/auth/providers/local-userpass/dtos.d.ts.map +1 -0
  18. package/dist/auth/providers/local-userpass/dtos.js +2 -0
  19. package/dist/auth/utils.d.ts +126 -0
  20. package/dist/auth/utils.d.ts.map +1 -0
  21. package/dist/auth/utils.js +125 -0
  22. package/dist/constants.d.ts +20 -0
  23. package/dist/constants.d.ts.map +1 -0
  24. package/dist/constants.js +36 -0
  25. package/dist/features/endpoints/index.d.ts +10 -0
  26. package/dist/features/endpoints/index.d.ts.map +1 -0
  27. package/dist/features/endpoints/index.js +31 -0
  28. package/dist/features/endpoints/interface.d.ts +27 -0
  29. package/dist/features/endpoints/interface.d.ts.map +1 -0
  30. package/dist/features/endpoints/interface.js +2 -0
  31. package/dist/features/endpoints/utils.d.ts +31 -0
  32. package/dist/features/endpoints/utils.d.ts.map +1 -0
  33. package/dist/features/endpoints/utils.js +88 -0
  34. package/dist/features/functions/controller.d.ts +9 -0
  35. package/dist/features/functions/controller.d.ts.map +1 -0
  36. package/dist/features/functions/controller.js +88 -0
  37. package/dist/features/functions/dtos.d.ts +34 -0
  38. package/dist/features/functions/dtos.d.ts.map +1 -0
  39. package/dist/features/functions/dtos.js +2 -0
  40. package/dist/features/functions/index.d.ts +9 -0
  41. package/dist/features/functions/index.d.ts.map +1 -0
  42. package/dist/features/functions/index.js +28 -0
  43. package/dist/features/functions/interface.d.ts +32 -0
  44. package/dist/features/functions/interface.d.ts.map +1 -0
  45. package/dist/features/functions/interface.js +2 -0
  46. package/dist/features/functions/utils.d.ts +23 -0
  47. package/dist/features/functions/utils.d.ts.map +1 -0
  48. package/dist/features/functions/utils.js +75 -0
  49. package/dist/features/rules/index.d.ts +1 -0
  50. package/dist/features/rules/index.d.ts.map +1 -0
  51. package/dist/features/rules/index.js +1 -0
  52. package/dist/features/rules/interface.d.ts +22 -0
  53. package/dist/features/rules/interface.d.ts.map +1 -0
  54. package/dist/features/rules/interface.js +2 -0
  55. package/dist/features/rules/utils.d.ts +3 -0
  56. package/dist/features/rules/utils.d.ts.map +1 -0
  57. package/dist/features/rules/utils.js +31 -0
  58. package/dist/features/triggers/dtos.d.ts +9 -0
  59. package/dist/features/triggers/dtos.d.ts.map +1 -0
  60. package/dist/features/triggers/dtos.js +2 -0
  61. package/dist/features/triggers/index.d.ts +10 -0
  62. package/dist/features/triggers/index.d.ts.map +1 -0
  63. package/dist/features/triggers/index.js +59 -0
  64. package/dist/features/triggers/interface.d.ts +44 -0
  65. package/dist/features/triggers/interface.d.ts.map +1 -0
  66. package/dist/features/triggers/interface.js +2 -0
  67. package/dist/features/triggers/utils.d.ts +16 -0
  68. package/dist/features/triggers/utils.d.ts.map +1 -0
  69. package/dist/features/triggers/utils.js +153 -0
  70. package/dist/index.d.ts +19 -0
  71. package/dist/index.d.ts.map +1 -0
  72. package/dist/index.js +100 -0
  73. package/dist/model.d.ts +2 -0
  74. package/dist/model.d.ts.map +1 -0
  75. package/dist/model.js +2 -0
  76. package/dist/services/api/index.d.ts +36 -0
  77. package/dist/services/api/index.d.ts.map +1 -0
  78. package/dist/services/api/index.js +36 -0
  79. package/dist/services/api/model.d.ts +33 -0
  80. package/dist/services/api/model.d.ts.map +1 -0
  81. package/dist/services/api/model.js +2 -0
  82. package/dist/services/api/utils.d.ts +16 -0
  83. package/dist/services/api/utils.d.ts.map +1 -0
  84. package/dist/services/api/utils.js +45 -0
  85. package/dist/services/aws/index.d.ts +13 -0
  86. package/dist/services/aws/index.d.ts.map +1 -0
  87. package/dist/services/aws/index.js +50 -0
  88. package/dist/services/index.d.ts +41 -0
  89. package/dist/services/index.d.ts.map +1 -0
  90. package/dist/services/index.js +14 -0
  91. package/dist/services/interface.d.ts +3 -0
  92. package/dist/services/interface.d.ts.map +1 -0
  93. package/dist/services/interface.js +2 -0
  94. package/dist/services/mongodb-atlas/index.d.ts +4 -0
  95. package/dist/services/mongodb-atlas/index.d.ts.map +1 -0
  96. package/dist/services/mongodb-atlas/index.js +483 -0
  97. package/dist/services/mongodb-atlas/model.d.ts +39 -0
  98. package/dist/services/mongodb-atlas/model.d.ts.map +1 -0
  99. package/dist/services/mongodb-atlas/model.js +2 -0
  100. package/dist/services/mongodb-atlas/utils.d.ts +8 -0
  101. package/dist/services/mongodb-atlas/utils.d.ts.map +1 -0
  102. package/dist/services/mongodb-atlas/utils.js +33 -0
  103. package/dist/state.d.ts +6 -0
  104. package/dist/state.d.ts.map +1 -0
  105. package/dist/state.js +18 -0
  106. package/dist/utils/context/helpers.d.ts +74 -0
  107. package/dist/utils/context/helpers.d.ts.map +1 -0
  108. package/dist/utils/context/helpers.js +60 -0
  109. package/dist/utils/context/index.d.ts +14 -0
  110. package/dist/utils/context/index.d.ts.map +1 -0
  111. package/dist/utils/context/index.js +65 -0
  112. package/dist/utils/context/interface.d.ts +18 -0
  113. package/dist/utils/context/interface.d.ts.map +1 -0
  114. package/dist/utils/context/interface.js +2 -0
  115. package/dist/utils/crypto/index.d.ts +19 -0
  116. package/dist/utils/crypto/index.d.ts.map +1 -0
  117. package/dist/utils/crypto/index.js +50 -0
  118. package/dist/utils/helpers/someAsync.d.ts +12 -0
  119. package/dist/utils/helpers/someAsync.d.ts.map +1 -0
  120. package/dist/utils/helpers/someAsync.js +56 -0
  121. package/dist/utils/index.d.ts +3 -0
  122. package/dist/utils/index.d.ts.map +1 -0
  123. package/dist/utils/index.js +11 -0
  124. package/dist/utils/initializer/exposeRoutes.d.ts +8 -0
  125. package/dist/utils/initializer/exposeRoutes.d.ts.map +1 -0
  126. package/dist/utils/initializer/exposeRoutes.js +41 -0
  127. package/dist/utils/initializer/registerPlugins.d.ts +19 -0
  128. package/dist/utils/initializer/registerPlugins.d.ts.map +1 -0
  129. package/dist/utils/initializer/registerPlugins.js +96 -0
  130. package/dist/utils/roles/helpers.d.ts +4 -0
  131. package/dist/utils/roles/helpers.d.ts.map +1 -0
  132. package/dist/utils/roles/helpers.js +47 -0
  133. package/dist/utils/roles/interface.d.ts +33 -0
  134. package/dist/utils/roles/interface.d.ts.map +1 -0
  135. package/dist/utils/roles/interface.js +2 -0
  136. package/dist/utils/roles/machines/commonValidators.d.ts +6 -0
  137. package/dist/utils/roles/machines/commonValidators.d.ts.map +1 -0
  138. package/dist/utils/roles/machines/commonValidators.js +34 -0
  139. package/dist/utils/roles/machines/index.d.ts +14 -0
  140. package/dist/utils/roles/machines/index.d.ts.map +1 -0
  141. package/dist/utils/roles/machines/index.js +27 -0
  142. package/dist/utils/roles/machines/interface.d.ts +46 -0
  143. package/dist/utils/roles/machines/interface.d.ts.map +1 -0
  144. package/dist/utils/roles/machines/interface.js +2 -0
  145. package/dist/utils/roles/machines/machine.d.ts +15 -0
  146. package/dist/utils/roles/machines/machine.d.ts.map +1 -0
  147. package/dist/utils/roles/machines/machine.js +97 -0
  148. package/dist/utils/roles/machines/read/A/index.d.ts +3 -0
  149. package/dist/utils/roles/machines/read/A/index.d.ts.map +1 -0
  150. package/dist/utils/roles/machines/read/A/index.js +27 -0
  151. package/dist/utils/roles/machines/read/B/index.d.ts +3 -0
  152. package/dist/utils/roles/machines/read/B/index.d.ts.map +1 -0
  153. package/dist/utils/roles/machines/read/B/index.js +36 -0
  154. package/dist/utils/roles/machines/read/C/index.d.ts +3 -0
  155. package/dist/utils/roles/machines/read/C/index.d.ts.map +1 -0
  156. package/dist/utils/roles/machines/read/C/index.js +38 -0
  157. package/dist/utils/roles/machines/read/D/index.d.ts +3 -0
  158. package/dist/utils/roles/machines/read/D/index.d.ts.map +1 -0
  159. package/dist/utils/roles/machines/read/D/index.js +26 -0
  160. package/dist/utils/roles/machines/read/D/validators.d.ts +4 -0
  161. package/dist/utils/roles/machines/read/D/validators.d.ts.map +1 -0
  162. package/dist/utils/roles/machines/read/D/validators.js +24 -0
  163. package/dist/utils/roles/machines/read/index.d.ts +2 -0
  164. package/dist/utils/roles/machines/read/index.d.ts.map +1 -0
  165. package/dist/utils/roles/machines/read/index.js +8 -0
  166. package/dist/utils/roles/machines/utils.d.ts +37 -0
  167. package/dist/utils/roles/machines/utils.d.ts.map +1 -0
  168. package/dist/utils/roles/machines/utils.js +54 -0
  169. package/dist/utils/roles/machines/write/A/index.d.ts +3 -0
  170. package/dist/utils/roles/machines/write/A/index.d.ts.map +1 -0
  171. package/dist/utils/roles/machines/write/A/index.js +29 -0
  172. package/dist/utils/roles/machines/write/B/index.d.ts +3 -0
  173. package/dist/utils/roles/machines/write/B/index.d.ts.map +1 -0
  174. package/dist/utils/roles/machines/write/B/index.js +47 -0
  175. package/dist/utils/roles/machines/write/C/index.d.ts +3 -0
  176. package/dist/utils/roles/machines/write/C/index.d.ts.map +1 -0
  177. package/dist/utils/roles/machines/write/C/index.js +26 -0
  178. package/dist/utils/roles/machines/write/C/validators.d.ts +4 -0
  179. package/dist/utils/roles/machines/write/C/validators.d.ts.map +1 -0
  180. package/dist/utils/roles/machines/write/C/validators.js +24 -0
  181. package/dist/utils/roles/machines/write/index.d.ts +2 -0
  182. package/dist/utils/roles/machines/write/index.d.ts.map +1 -0
  183. package/dist/utils/roles/machines/write/index.js +7 -0
  184. package/dist/utils/rules-matcher/interface.d.ts +338 -0
  185. package/dist/utils/rules-matcher/interface.d.ts.map +1 -0
  186. package/dist/utils/rules-matcher/interface.js +26 -0
  187. package/dist/utils/rules-matcher/utils.d.ts +11 -0
  188. package/dist/utils/rules-matcher/utils.d.ts.map +1 -0
  189. package/dist/utils/rules-matcher/utils.js +214 -0
  190. package/dist/utils/rules.d.ts +2 -0
  191. package/dist/utils/rules.d.ts.map +1 -0
  192. package/dist/utils/rules.js +22 -0
  193. package/jest.config.ts +24 -0
  194. package/package.json +62 -0
  195. package/project.json +10 -0
  196. package/rollup.config.js +17 -0
  197. package/src/auth/controller.ts +78 -0
  198. package/src/auth/dtos.ts +6 -0
  199. package/src/auth/plugins/jwt.ts +68 -0
  200. package/src/auth/providers/local-userpass/controller.ts +226 -0
  201. package/src/auth/providers/local-userpass/dtos.ts +40 -0
  202. package/src/auth/utils.ts +168 -0
  203. package/src/babel.config.json +3 -0
  204. package/src/constants.ts +24 -0
  205. package/src/fastify.d.ts +28 -0
  206. package/src/features/endpoints/index.ts +27 -0
  207. package/src/features/endpoints/interface.ts +29 -0
  208. package/src/features/endpoints/utils.ts +74 -0
  209. package/src/features/functions/controller.ts +102 -0
  210. package/src/features/functions/dtos.ts +41 -0
  211. package/src/features/functions/index.ts +21 -0
  212. package/src/features/functions/interface.ts +38 -0
  213. package/src/features/functions/utils.ts +82 -0
  214. package/src/features/rules/index.tsx +0 -0
  215. package/src/features/rules/interface.ts +24 -0
  216. package/src/features/rules/utils.ts +20 -0
  217. package/src/features/triggers/dtos.ts +9 -0
  218. package/src/features/triggers/index.ts +36 -0
  219. package/src/features/triggers/interface.ts +44 -0
  220. package/src/features/triggers/utils.ts +157 -0
  221. package/src/global.d.ts +0 -0
  222. package/src/index.ts +90 -0
  223. package/src/model.ts +1 -0
  224. package/src/services/api/index.ts +50 -0
  225. package/src/services/api/model.ts +38 -0
  226. package/src/services/api/utils.ts +39 -0
  227. package/src/services/aws/index.ts +48 -0
  228. package/src/services/index.ts +9 -0
  229. package/src/services/interface.ts +3 -0
  230. package/src/services/mongodb-atlas/index.ts +569 -0
  231. package/src/services/mongodb-atlas/model.ts +67 -0
  232. package/src/services/mongodb-atlas/utils.ts +44 -0
  233. package/src/state.ts +24 -0
  234. package/src/utils/__tests__/STEP_A_STATES.test.ts +54 -0
  235. package/src/utils/__tests__/STEP_B_STATES.test.ts +113 -0
  236. package/src/utils/__tests__/STEP_C_STATES.test.ts +87 -0
  237. package/src/utils/__tests__/STEP_D_STATES.test.ts +93 -0
  238. package/src/utils/__tests__/checkAdditionalFieldsFn.test.ts +45 -0
  239. package/src/utils/__tests__/checkApplyWhen.test.ts +49 -0
  240. package/src/utils/__tests__/checkFieldsPropertyExists.test.ts +47 -0
  241. package/src/utils/__tests__/checkIsValidFieldNameFn.test.ts +190 -0
  242. package/src/utils/__tests__/comparePassword.test.ts +38 -0
  243. package/src/utils/__tests__/evaluateDocumentsFiltersReadFn.test.ts +57 -0
  244. package/src/utils/__tests__/evaluateDocumentsFiltersWriteFn.test.ts +57 -0
  245. package/src/utils/__tests__/evaluateTopLevelReadFn.test.ts +58 -0
  246. package/src/utils/__tests__/evaluateTopLevelWriteFn.test.ts +66 -0
  247. package/src/utils/__tests__/exposeRoutes.test.ts +65 -0
  248. package/src/utils/__tests__/generateContextData.test.ts +75 -0
  249. package/src/utils/__tests__/getDefaultRule.test.ts +29 -0
  250. package/src/utils/__tests__/getKey.test.ts +12 -0
  251. package/src/utils/__tests__/getKeys.test.ts +11 -0
  252. package/src/utils/__tests__/getWinningRole.test.ts +66 -0
  253. package/src/utils/__tests__/hashPassword.test.ts +28 -0
  254. package/src/utils/__tests__/isEmpty.test.ts +17 -0
  255. package/src/utils/__tests__/logMachineInfo.test.ts +15 -0
  256. package/src/utils/__tests__/operators.test.ts +99 -0
  257. package/src/utils/__tests__/readFileContent.test.ts +35 -0
  258. package/src/utils/__tests__/registerPlugins.test.ts +59 -0
  259. package/src/utils/__tests__/rule.test.ts +51 -0
  260. package/src/utils/__tests__/rulesMatcherInterfaces.test.ts +57 -0
  261. package/src/utils/__tests__/rulesMatcherUtils.test.ts +56 -0
  262. package/src/utils/__tests__/someAsync.test.ts +55 -0
  263. package/src/utils/context/helpers.ts +71 -0
  264. package/src/utils/context/index.ts +74 -0
  265. package/src/utils/context/interface.ts +19 -0
  266. package/src/utils/crypto/index.ts +36 -0
  267. package/src/utils/helpers/someAsync.ts +24 -0
  268. package/src/utils/index.ts +5 -0
  269. package/src/utils/initializer/exposeRoutes.ts +26 -0
  270. package/src/utils/initializer/registerPlugins.ts +111 -0
  271. package/src/utils/roles/helpers.ts +47 -0
  272. package/src/utils/roles/interface.ts +42 -0
  273. package/src/utils/roles/machines/commonValidators.ts +24 -0
  274. package/src/utils/roles/machines/index.ts +20 -0
  275. package/src/utils/roles/machines/interface.ts +46 -0
  276. package/src/utils/roles/machines/machine.ts +85 -0
  277. package/src/utils/roles/machines/read/A/index.ts +19 -0
  278. package/src/utils/roles/machines/read/B/index.ts +31 -0
  279. package/src/utils/roles/machines/read/C/index.ts +30 -0
  280. package/src/utils/roles/machines/read/D/index.ts +20 -0
  281. package/src/utils/roles/machines/read/D/validators.ts +24 -0
  282. package/src/utils/roles/machines/read/index.ts +6 -0
  283. package/src/utils/roles/machines/utils.ts +54 -0
  284. package/src/utils/roles/machines/write/A/index.ts +25 -0
  285. package/src/utils/roles/machines/write/B/index.ts +43 -0
  286. package/src/utils/roles/machines/write/C/index.ts +20 -0
  287. package/src/utils/roles/machines/write/C/validators.ts +24 -0
  288. package/src/utils/roles/machines/write/index.ts +5 -0
  289. package/src/utils/rules-matcher/interface.ts +365 -0
  290. package/src/utils/rules-matcher/utils.ts +281 -0
  291. package/src/utils/rules.ts +19 -0
  292. package/tsconfig.json +28 -0
@@ -0,0 +1,23 @@
1
+ import { ExecuteQueryParams, Functions } from './interface';
2
+ /**
3
+ * > Loads the functions config json file
4
+ * @testable
5
+ */
6
+ export declare const loadFunctions: (rootDir?: string) => Promise<Functions>;
7
+ /**
8
+ * > Executes a single query
9
+ * @param currentMethod -> the method that should be called
10
+ * @param query -> the query data
11
+ * @param update -> the update Document that should be deserialized
12
+ */
13
+ export declare const executeQuery: ({ currentMethod, query, update, document }: ExecuteQueryParams) => Promise<{
14
+ find: () => Promise<any[]>;
15
+ findOne: () => Promise<unknown>;
16
+ deleteOne: () => Promise<unknown>;
17
+ insertOne: () => Promise<import("mongodb/mongodb").InsertOneResult<import("bson").Document>>;
18
+ updateOne: () => Promise<unknown> | import("mongodb/mongodb").FindCursor<any> | import("mongodb/mongodb").ChangeStream<import("bson").Document, import("bson").Document> | import("mongodb/mongodb").AggregationCursor<import("bson").Document>;
19
+ aggregate: () => import("mongodb/mongodb").AggregationCursor<import("bson").Document>;
20
+ insertMany: () => Promise<import("mongodb/mongodb").InsertManyResult<import("bson").Document>>;
21
+ updateMany: () => Promise<import("mongodb/mongodb").UpdateResult<import("bson").Document>>;
22
+ }>;
23
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/features/functions/utils.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAE3D;;;GAGG;AACH,eAAO,MAAM,aAAa,GAAU,gBAAuB,KAAG,OAAO,CAAC,SAAS,CAwB9E,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAU,4CAKhC,kBAAkB;;;;;;;;;EAkCpB,CAAA"}
@@ -0,0 +1,75 @@
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 __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
22
+ var __importDefault = (this && this.__importDefault) || function (mod) {
23
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.executeQuery = exports.loadFunctions = void 0;
27
+ const fs_1 = __importDefault(require("fs"));
28
+ const node_path_1 = __importDefault(require("node:path"));
29
+ const bson_1 = require("bson");
30
+ /**
31
+ * > Loads the functions config json file
32
+ * @testable
33
+ */
34
+ const loadFunctions = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* (rootDir = process.cwd()) {
35
+ const fnDir = 'functions';
36
+ const configFile = node_path_1.default.join(rootDir, fnDir, 'config.json');
37
+ const config = JSON.parse(fs_1.default.readFileSync(configFile, 'utf-8'));
38
+ const functions = config.reduce((acc, _a) => {
39
+ var { name } = _a, opts = __rest(_a, ["name"]);
40
+ const extensions = ['.js', '.ts'];
41
+ let code = '';
42
+ const fnPath = extensions
43
+ .map(ext => node_path_1.default.join(rootDir, fnDir, `${name}${ext}`))
44
+ .find(fs_1.default.existsSync);
45
+ if (!fnPath) {
46
+ throw new Error(`File ${name}.js or ${name}.ts not found`);
47
+ }
48
+ code = fs_1.default.readFileSync(fnPath, 'utf-8');
49
+ acc[name] = Object.assign({ code }, opts);
50
+ return acc;
51
+ }, {});
52
+ return functions;
53
+ });
54
+ exports.loadFunctions = loadFunctions;
55
+ /**
56
+ * > Executes a single query
57
+ * @param currentMethod -> the method that should be called
58
+ * @param query -> the query data
59
+ * @param update -> the update Document that should be deserialized
60
+ */
61
+ const executeQuery = (_a) => __awaiter(void 0, [_a], void 0, function* ({ currentMethod, query, update, document }) {
62
+ return {
63
+ find: () => __awaiter(void 0, void 0, void 0, function* () {
64
+ return yield currentMethod(bson_1.EJSON.deserialize(query)).toArray();
65
+ }),
66
+ findOne: () => currentMethod(bson_1.EJSON.deserialize(query)),
67
+ deleteOne: () => currentMethod(bson_1.EJSON.deserialize(query)),
68
+ insertOne: () => currentMethod(bson_1.EJSON.deserialize(document)),
69
+ updateOne: () => currentMethod(bson_1.EJSON.deserialize(query), bson_1.EJSON.deserialize(update)),
70
+ aggregate: () => currentMethod(bson_1.EJSON.deserialize(query)),
71
+ insertMany: () => currentMethod(bson_1.EJSON.deserialize(query)),
72
+ updateMany: () => currentMethod(bson_1.EJSON.deserialize(query), bson_1.EJSON.deserialize(update)),
73
+ };
74
+ });
75
+ exports.executeQuery = executeQuery;
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/features/rules/index.tsx"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1,22 @@
1
+ export interface Filter {
2
+ name: string;
3
+ query: Record<string, unknown>;
4
+ apply_when: Record<string, unknown>;
5
+ }
6
+ export interface Role {
7
+ name: string;
8
+ apply_when: Record<string, unknown>;
9
+ insert: boolean;
10
+ delete: boolean;
11
+ search: boolean;
12
+ read: boolean;
13
+ write: boolean;
14
+ }
15
+ export interface RulesConfig {
16
+ database: string;
17
+ collection: string;
18
+ filters: Filter[];
19
+ roles: Role[];
20
+ }
21
+ export type Rules = Record<string, RulesConfig>;
22
+ //# sourceMappingURL=interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../../src/features/rules/interface.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,MAAM;IACrB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC9B,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACpC;AAED,MAAM,WAAW,IAAI;IACnB,IAAI,EAAE,MAAM,CAAA;IACZ,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACnC,MAAM,EAAE,OAAO,CAAA;IACf,MAAM,EAAE,OAAO,CAAA;IACf,MAAM,EAAE,OAAO,CAAA;IACf,IAAI,EAAE,OAAO,CAAA;IACb,KAAK,EAAE,OAAO,CAAA;CACf;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,MAAM,CAAA;IAChB,UAAU,EAAE,MAAM,CAAA;IAClB,OAAO,EAAE,MAAM,EAAE,CAAA;IACjB,KAAK,EAAE,IAAI,EAAE,CAAA;CACd;AAED,MAAM,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ import { Rules } from './interface';
2
+ export declare const loadRules: (rootDir?: string) => Promise<Rules>;
3
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/features/rules/utils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAe,MAAM,aAAa,CAAA;AAEhD,eAAO,MAAM,SAAS,GAAU,gBAAuB,KAAG,OAAO,CAAC,KAAK,CActE,CAAA"}
@@ -0,0 +1,31 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.loadRules = void 0;
16
+ const fs_1 = __importDefault(require("fs"));
17
+ const node_path_1 = __importDefault(require("node:path"));
18
+ const utils_1 = require("../../utils");
19
+ const loadRules = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* (rootDir = process.cwd()) {
20
+ const rulesRoot = node_path_1.default.join(rootDir, 'data_sources', 'mongodb-atlas');
21
+ const files = fs_1.default.readdirSync(rulesRoot, { recursive: true });
22
+ const rulesFiles = files.filter((x) => x.endsWith('rules.json'));
23
+ const rulesByCollection = rulesFiles.reduce((acc, rulesFile) => {
24
+ const filePath = node_path_1.default.join(rulesRoot, rulesFile);
25
+ const collectionRules = (0, utils_1.readJsonContent)(filePath);
26
+ acc[collectionRules.collection] = collectionRules;
27
+ return acc;
28
+ }, {});
29
+ return rulesByCollection;
30
+ });
31
+ exports.loadRules = loadRules;
@@ -0,0 +1,9 @@
1
+ import { FastifyInstance } from 'fastify';
2
+ import { Functions } from '../functions/interface';
3
+ import { Triggers } from './interface';
4
+ export type ActivateTriggersParams = {
5
+ fastify: FastifyInstance;
6
+ triggersList: Triggers;
7
+ functionsList: Functions;
8
+ };
9
+ //# sourceMappingURL=dtos.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dtos.d.ts","sourceRoot":"","sources":["../../../src/features/triggers/dtos.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAEtC,MAAM,MAAM,sBAAsB,GAAG;IACnC,OAAO,EAAE,eAAe,CAAA;IACxB,YAAY,EAAE,QAAQ,CAAA;IACtB,aAAa,EAAE,SAAS,CAAA;CACzB,CAAA"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ import { ActivateTriggersParams } from './dtos';
2
+ /**
3
+ * > Used to activate all app triggers
4
+ * @testable
5
+ * @param fastify -> the fastify instance
6
+ * @param triggersList -> the list of all triggers
7
+ * @param functionsList -> the list of all functions
8
+ */
9
+ export declare const activateTriggers: ({ fastify, triggersList, functionsList }: ActivateTriggersParams) => Promise<void>;
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/features/triggers/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAE,MAAM,QAAQ,CAAA;AAG/C;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,GAAU,0CAIpC,sBAAsB,kBAiBxB,CAAA"}
@@ -0,0 +1,59 @@
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 __asyncValues = (this && this.__asyncValues) || function (o) {
12
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
+ var m = o[Symbol.asyncIterator], i;
14
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
15
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
16
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.activateTriggers = void 0;
20
+ const services_1 = require("../../services");
21
+ const utils_1 = require("./utils");
22
+ /**
23
+ * > Used to activate all app triggers
24
+ * @testable
25
+ * @param fastify -> the fastify instance
26
+ * @param triggersList -> the list of all triggers
27
+ * @param functionsList -> the list of all functions
28
+ */
29
+ const activateTriggers = (_a) => __awaiter(void 0, [_a], void 0, function* ({ fastify, triggersList, functionsList }) {
30
+ var _b, triggersList_1, triggersList_1_1;
31
+ var _c, e_1, _d, _e;
32
+ console.log("START ACTIVATION TRIGGERS");
33
+ try {
34
+ try {
35
+ for (_b = true, triggersList_1 = __asyncValues(triggersList); triggersList_1_1 = yield triggersList_1.next(), _c = triggersList_1_1.done, !_c; _b = true) {
36
+ _e = triggersList_1_1.value;
37
+ _b = false;
38
+ const trigger = _e;
39
+ const { content } = trigger;
40
+ const { type, config, event_processors } = content;
41
+ const functionName = event_processors.FUNCTION.config.function_name;
42
+ const triggerHandler = functionsList[functionName];
43
+ yield utils_1.TRIGGER_HANDLERS[type]({ config, triggerHandler, app: fastify, services: services_1.services, functionsList });
44
+ }
45
+ }
46
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
47
+ finally {
48
+ try {
49
+ if (!_b && !_c && (_d = triggersList_1.return)) yield _d.call(triggersList_1);
50
+ }
51
+ finally { if (e_1) throw e_1.error; }
52
+ }
53
+ console.log("TRIGGERS ACTIVATION COMPLETED");
54
+ }
55
+ catch (e) {
56
+ console.error('Error while activating triggers', e.message);
57
+ }
58
+ });
59
+ exports.activateTriggers = activateTriggers;
@@ -0,0 +1,44 @@
1
+ import { FastifyInstance } from "fastify";
2
+ import { Services } from "../../services/interface";
3
+ import { Function, Functions } from "../functions/interface";
4
+ export interface Trigger {
5
+ name: string;
6
+ type: TriggerType;
7
+ disabled: boolean;
8
+ config: Config;
9
+ event_processors: {
10
+ FUNCTION: {
11
+ config: {
12
+ function_name: string;
13
+ };
14
+ };
15
+ };
16
+ }
17
+ type Config = {
18
+ collection: string;
19
+ database: string;
20
+ full_document: boolean;
21
+ full_document_before_change: boolean;
22
+ match: Record<string, unknown>;
23
+ operation_types: string[];
24
+ project: Record<string, unknown>;
25
+ service_name: string;
26
+ skip_catchup_events: boolean;
27
+ tolerate_resume_errors: boolean;
28
+ unordered: boolean;
29
+ schedule: string;
30
+ };
31
+ export type TriggerType = "SCHEDULED" | "DATABASE" | "AUTHENTICATION";
32
+ export type Triggers = {
33
+ fileName: string;
34
+ content: Trigger;
35
+ }[];
36
+ export type HandlerParams = {
37
+ config: Config;
38
+ triggerHandler: Function;
39
+ app: FastifyInstance;
40
+ services: Services;
41
+ functionsList: Functions;
42
+ };
43
+ export {};
44
+ //# sourceMappingURL=interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../../src/features/triggers/interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAA;AACnD,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA;AAE5D,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,WAAW,CAAA;IACjB,QAAQ,EAAE,OAAO,CAAA;IACjB,MAAM,EAAE,MAAM,CAAA;IACd,gBAAgB,EAAE;QAChB,QAAQ,EAAE;YACR,MAAM,EAAE;gBACN,aAAa,EAAE,MAAM,CAAA;aACtB,CAAA;SACF,CAAA;KACF,CAAA;CACF;AAED,KAAK,MAAM,GAAG;IACZ,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;IAChB,aAAa,EAAE,OAAO,CAAA;IACtB,2BAA2B,EAAE,OAAO,CAAA;IACpC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC9B,eAAe,EAAE,MAAM,EAAE,CAAA;IACzB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAChC,YAAY,EAAE,MAAM,CAAA;IACpB,mBAAmB,EAAE,OAAO,CAAA;IAC5B,sBAAsB,EAAE,OAAO,CAAA;IAC/B,SAAS,EAAE,OAAO,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,MAAM,MAAM,WAAW,GAAG,WAAW,GAAG,UAAU,GAAG,gBAAgB,CAAA;AACrE,MAAM,MAAM,QAAQ,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,EAAE,CAAA;AAG/D,MAAM,MAAM,aAAa,GAAG;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,EAAE,QAAQ,CAAC;IACzB,GAAG,EAAE,eAAe,CAAC;IACrB,QAAQ,EAAE,QAAQ,CAAC;IACnB,aAAa,EAAE,SAAS,CAAA;CACzB,CAAA"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,16 @@
1
+ import { HandlerParams, Triggers } from './interface';
2
+ /**
3
+ * Loads trigger files from the specified directory and returns them as an array of objects.
4
+ * Each object contains the file name and the parsed JSON content.
5
+ *
6
+ * @testable
7
+ * @param {string} [rootDir=process.cwd()] - The root directory from which to load the triggers. Defaults to the current working directory.
8
+ * @returns {Promise<Triggers>} A promise that resolves to an array of trigger objects.
9
+ */
10
+ export declare const loadTriggers: (rootDir?: string) => Promise<Triggers>;
11
+ export declare const TRIGGER_HANDLERS: {
12
+ SCHEDULED: ({ config, triggerHandler, functionsList, services, app }: HandlerParams) => Promise<void>;
13
+ DATABASE: ({ config, triggerHandler, functionsList, services, app }: HandlerParams) => Promise<void>;
14
+ AUTHENTICATION: ({ config, triggerHandler, functionsList, services, app }: HandlerParams) => Promise<void>;
15
+ };
16
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/features/triggers/utils.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,aAAa,EAAW,QAAQ,EAAE,MAAM,aAAa,CAAA;AAE9D;;;;;;;GAOG;AACH,eAAO,MAAM,YAAY,GAAU,gBAAuB,KAAG,OAAO,CAAC,QAAQ,CAY5E,CAAA;AA4HD,eAAO,MAAM,gBAAgB;0EA9G8D,aAAa;yEAmET,aAAa;+EApDP,aAAa;CAmGjH,CAAA"}
@@ -0,0 +1,153 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.TRIGGER_HANDLERS = exports.loadTriggers = void 0;
16
+ const fs_1 = __importDefault(require("fs"));
17
+ const node_path_1 = __importDefault(require("node:path"));
18
+ const node_cron_1 = __importDefault(require("node-cron"));
19
+ const constants_1 = require("../../constants");
20
+ const utils_1 = require("../../utils");
21
+ const context_1 = require("../../utils/context");
22
+ /**
23
+ * Loads trigger files from the specified directory and returns them as an array of objects.
24
+ * Each object contains the file name and the parsed JSON content.
25
+ *
26
+ * @testable
27
+ * @param {string} [rootDir=process.cwd()] - The root directory from which to load the triggers. Defaults to the current working directory.
28
+ * @returns {Promise<Triggers>} A promise that resolves to an array of trigger objects.
29
+ */
30
+ const loadTriggers = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* (rootDir = process.cwd()) {
31
+ const triggersPath = node_path_1.default.join(rootDir, 'triggers');
32
+ const files = fs_1.default.readdirSync(triggersPath);
33
+ const triggers = files
34
+ .filter((fileName) => fileName.endsWith('.json'))
35
+ .map((fileName) => ({
36
+ fileName,
37
+ content: (0, utils_1.readJsonContent)(node_path_1.default.join(triggersPath, fileName))
38
+ }));
39
+ return triggers;
40
+ });
41
+ exports.loadTriggers = loadTriggers;
42
+ /**
43
+ * Handles the scheduling of a cron job and triggers the appropriate function.
44
+ *
45
+ * @testable
46
+ * @param {Object} params - The parameters for the handler.
47
+ * @param {Object} params.config - Configuration object for the cron trigger.
48
+ * @param {string} params.config.schedule - Cron schedule string (e.g., "* * * * *" for every minute).
49
+ * @param {Function} params.triggerHandler - The function to be triggered when the cron job executes.
50
+ * @param {Array<Function>} params.functionsList - List of available functions.
51
+ * @param {Object} params.services - Services available to the handler.
52
+ * @param {Object} params.app - The app instance for context.
53
+ */
54
+ const handleCronTrigger = (_a) => __awaiter(void 0, [_a], void 0, function* ({ config, triggerHandler, functionsList, services, app }) {
55
+ node_cron_1.default.schedule(config.schedule, () => __awaiter(void 0, void 0, void 0, function* () {
56
+ yield (0, context_1.GenerateContext)({
57
+ args: [],
58
+ app,
59
+ rules: {},
60
+ user: {},
61
+ currentFunction: triggerHandler,
62
+ functionsList,
63
+ services
64
+ });
65
+ }));
66
+ });
67
+ const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, function* ({ config, triggerHandler, functionsList, services, app }) {
68
+ const { database } = config;
69
+ const pipeline = [{
70
+ $match: {
71
+ operationType: { $in: ["INSERT"] }
72
+ }
73
+ }];
74
+ const changeStream = app.mongo.client.db(database).collection(constants_1.AUTH_CONFIG.authCollection).watch(pipeline, {
75
+ fullDocument: 'whenAvailable'
76
+ });
77
+ changeStream.on('change', function (change) {
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ const document = change["fullDocument"]; //TODO -> define user type
80
+ if (document) {
81
+ delete document.password;
82
+ const currentUser = Object.assign({}, document);
83
+ delete currentUser.password;
84
+ yield (0, context_1.GenerateContext)({
85
+ args: [{ user: currentUser }],
86
+ app,
87
+ rules: {},
88
+ user: {},
89
+ currentFunction: triggerHandler,
90
+ functionsList,
91
+ services
92
+ });
93
+ }
94
+ });
95
+ });
96
+ });
97
+ /**
98
+ * Handles a database trigger by watching changes in a specified collection and triggering the appropriate handler.
99
+ *
100
+ * @testable
101
+ * @param {Object} params - The parameters for the handler.
102
+ * @param {Object} params.config - Configuration object for the database trigger.
103
+ * @param {string} params.config.database - The name of the database to watch.
104
+ * @param {string} params.config.collection - The name of the collection to watch.
105
+ * @param {Array<string>} [params.config.operation_types=[]] - List of operation types to watch (e.g., "insert", "update").
106
+ * @param {Object} [params.config.match={}] - Additional match criteria for the change stream.
107
+ * @param {Object} [params.config.project={}] - Projection to apply to the change stream results.
108
+ * @param {boolean} [params.config.full_document] - Whether to include the full document in the change stream results.
109
+ * @param {boolean} [params.config.full_document_before_change] - Whether to include the full document before the change.
110
+ * @param {Function} params.triggerHandler - The function to be triggered on database changes.
111
+ * @param {Array<Function>} params.functionsList - List of available functions.
112
+ * @param {Object} params.services - Services available to the handler.
113
+ * @param {Object} params.app - The app instance for context.
114
+ */
115
+ const handleDataBaseTrigger = (_a) => __awaiter(void 0, [_a], void 0, function* ({ config, triggerHandler, functionsList, services, app }) {
116
+ const { database, collection: collectionName, operation_types = [], match = {}, project = {} } = config;
117
+ const collection = app.mongo.client.db(database).collection(collectionName);
118
+ const pipeline = [
119
+ {
120
+ $match: Object.assign({ operationType: { $in: operation_types.map((op) => op.toLowerCase()) } }, match)
121
+ },
122
+ Object.keys(project).length
123
+ ? {
124
+ $project: project
125
+ }
126
+ : undefined
127
+ ].filter(Boolean);
128
+ const changeStream = collection.watch(pipeline, {
129
+ fullDocument: config.full_document ? 'whenAvailable' : undefined,
130
+ fullDocumentBeforeChange: config.full_document_before_change
131
+ ? 'whenAvailable'
132
+ : undefined
133
+ });
134
+ changeStream.on('change', function (change) {
135
+ return __awaiter(this, void 0, void 0, function* () {
136
+ yield (0, context_1.GenerateContext)({
137
+ args: [change],
138
+ app,
139
+ rules: {},
140
+ user: {},
141
+ currentFunction: triggerHandler,
142
+ functionsList,
143
+ services
144
+ });
145
+ });
146
+ });
147
+ // TODO -> gestire close dello stream
148
+ });
149
+ exports.TRIGGER_HANDLERS = {
150
+ SCHEDULED: handleCronTrigger,
151
+ DATABASE: handleDataBaseTrigger,
152
+ AUTHENTICATION: handleAuthenticationTrigger
153
+ };
@@ -0,0 +1,19 @@
1
+ import 'dotenv/config';
2
+ export * from "./model";
3
+ export type InitializeConfig = {
4
+ projectId: string;
5
+ mongodbUrl?: string;
6
+ jwtSecret?: string;
7
+ port?: number;
8
+ host?: string;
9
+ };
10
+ /**
11
+ * > Used to initialize fastify app
12
+ * @param projectId -> the project id string
13
+ * @param host -> the host string
14
+ * @param jwtSecret -> connection jwt
15
+ * @param port -> the serve port number
16
+ * @param mongodbUrl -> the database connection string
17
+ */
18
+ export declare function initialize({ projectId, host, jwtSecret, port, mongodbUrl }: InitializeConfig): Promise<void>;
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,CAAA;AActB,cAAc,SAAS,CAAC;AAExB,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,EAAE,MAAM,CAAA;IACjB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAAC,EAC/B,SAAS,EACT,IAAI,EACJ,SAAqC,EACrC,IAA0B,EAC1B,UAAuC,EACxC,EAAE,gBAAgB,iBAmDlB"}
package/dist/index.js ADDED
@@ -0,0 +1,100 @@
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
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.initialize = initialize;
30
+ require("dotenv/config");
31
+ const fastify_1 = __importDefault(require("fastify"));
32
+ const constants_1 = require("./constants");
33
+ const endpoints_1 = require("./features/endpoints");
34
+ const utils_1 = require("./features/endpoints/utils");
35
+ const functions_1 = require("./features/functions");
36
+ const utils_2 = require("./features/functions/utils");
37
+ const utils_3 = require("./features/rules/utils");
38
+ const triggers_1 = require("./features/triggers");
39
+ const utils_4 = require("./features/triggers/utils");
40
+ const services_1 = require("./services");
41
+ const state_1 = require("./state");
42
+ const exposeRoutes_1 = require("./utils/initializer/exposeRoutes");
43
+ const registerPlugins_1 = require("./utils/initializer/registerPlugins");
44
+ __exportStar(require("./model"), exports);
45
+ /**
46
+ * > Used to initialize fastify app
47
+ * @param projectId -> the project id string
48
+ * @param host -> the host string
49
+ * @param jwtSecret -> connection jwt
50
+ * @param port -> the serve port number
51
+ * @param mongodbUrl -> the database connection string
52
+ */
53
+ function initialize(_a) {
54
+ return __awaiter(this, arguments, void 0, function* ({ projectId, host, jwtSecret = constants_1.DEFAULT_CONFIG.JWT_SECRET, port = constants_1.DEFAULT_CONFIG.PORT, mongodbUrl = constants_1.DEFAULT_CONFIG.MONGODB_URL }) {
55
+ var _b;
56
+ const fastify = (0, fastify_1.default)({
57
+ logger: !!constants_1.DEFAULT_CONFIG.ENABLE_LOGGER
58
+ });
59
+ const basePath = (_b = require.main) === null || _b === void 0 ? void 0 : _b.path;
60
+ console.log("BASE PATH", basePath);
61
+ console.log("CURRENT PORT", port);
62
+ console.log("CURRENT HOST", host);
63
+ const functionsList = yield (0, utils_2.loadFunctions)(basePath);
64
+ console.log("Functions LOADED");
65
+ const triggersList = yield (0, utils_4.loadTriggers)(basePath);
66
+ console.log("Triggers LOADED");
67
+ const endpointsList = yield (0, utils_1.loadEndpoints)(basePath);
68
+ console.log("Endpoints LOADED");
69
+ const rulesList = yield (0, utils_3.loadRules)(basePath);
70
+ console.log("Rules LOADED");
71
+ const stateConfig = {
72
+ functions: functionsList,
73
+ triggers: triggersList,
74
+ endpoints: endpointsList,
75
+ rules: rulesList,
76
+ app: fastify,
77
+ services: services_1.services
78
+ };
79
+ Object.entries(stateConfig).forEach(([key, value]) => state_1.StateManager.setData(key, value));
80
+ yield (0, registerPlugins_1.registerPlugins)({
81
+ register: fastify.register,
82
+ mongodbUrl,
83
+ jwtSecret,
84
+ functionsList
85
+ });
86
+ console.log("Plugins registration COMPLETED");
87
+ yield (0, exposeRoutes_1.exposeRoutes)(fastify);
88
+ console.log("APP Routes registration COMPLETED");
89
+ yield (0, functions_1.registerFunctions)({ app: fastify, functionsList, rulesList });
90
+ console.log("Functions registration COMPLETED");
91
+ yield (0, endpoints_1.generateEndpoints)({ app: fastify, functionsList, endpointsList });
92
+ console.log("HTTP Endpoints registration COMPLETED");
93
+ fastify.ready(() => {
94
+ console.log("FASTIFY IS READY");
95
+ (triggersList === null || triggersList === void 0 ? void 0 : triggersList.length) > 0 && (0, triggers_1.activateTriggers)({ fastify, triggersList, functionsList });
96
+ });
97
+ yield fastify.listen({ port, host });
98
+ fastify.log.info(`[${projectId}] Server listening on port ${port}`);
99
+ });
100
+ }
@@ -0,0 +1,2 @@
1
+ export { Document, WithId } from 'mongodb';
2
+ //# sourceMappingURL=model.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC"}
package/dist/model.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });