@flowerforce/flowerbase 1.0.1-beta.3
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.
- package/CHANGELOG.md +0 -0
- package/LICENSE +3 -0
- package/README.md +18 -0
- package/dist/auth/controller.d.ts +8 -0
- package/dist/auth/controller.d.ts.map +1 -0
- package/dist/auth/controller.js +76 -0
- package/dist/auth/dtos.d.ts +6 -0
- package/dist/auth/dtos.d.ts.map +1 -0
- package/dist/auth/dtos.js +2 -0
- package/dist/auth/plugins/jwt.d.ts +14 -0
- package/dist/auth/plugins/jwt.d.ts.map +1 -0
- package/dist/auth/plugins/jwt.js +68 -0
- package/dist/auth/providers/local-userpass/controller.d.ts +8 -0
- package/dist/auth/providers/local-userpass/controller.d.ts.map +1 -0
- package/dist/auth/providers/local-userpass/controller.js +184 -0
- package/dist/auth/providers/local-userpass/dtos.d.ts +35 -0
- package/dist/auth/providers/local-userpass/dtos.d.ts.map +1 -0
- package/dist/auth/providers/local-userpass/dtos.js +2 -0
- package/dist/auth/utils.d.ts +126 -0
- package/dist/auth/utils.d.ts.map +1 -0
- package/dist/auth/utils.js +122 -0
- package/dist/constants.d.ts +18 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +34 -0
- package/dist/features/endpoints/index.d.ts +10 -0
- package/dist/features/endpoints/index.d.ts.map +1 -0
- package/dist/features/endpoints/index.js +31 -0
- package/dist/features/endpoints/interface.d.ts +27 -0
- package/dist/features/endpoints/interface.d.ts.map +1 -0
- package/dist/features/endpoints/interface.js +2 -0
- package/dist/features/endpoints/utils.d.ts +31 -0
- package/dist/features/endpoints/utils.d.ts.map +1 -0
- package/dist/features/endpoints/utils.js +85 -0
- package/dist/features/functions/controller.d.ts +9 -0
- package/dist/features/functions/controller.d.ts.map +1 -0
- package/dist/features/functions/controller.js +88 -0
- package/dist/features/functions/dtos.d.ts +34 -0
- package/dist/features/functions/dtos.d.ts.map +1 -0
- package/dist/features/functions/dtos.js +2 -0
- package/dist/features/functions/index.d.ts +9 -0
- package/dist/features/functions/index.d.ts.map +1 -0
- package/dist/features/functions/index.js +28 -0
- package/dist/features/functions/interface.d.ts +32 -0
- package/dist/features/functions/interface.d.ts.map +1 -0
- package/dist/features/functions/interface.js +2 -0
- package/dist/features/functions/utils.d.ts +23 -0
- package/dist/features/functions/utils.d.ts.map +1 -0
- package/dist/features/functions/utils.js +75 -0
- package/dist/features/rules/index.d.ts +1 -0
- package/dist/features/rules/index.d.ts.map +1 -0
- package/dist/features/rules/index.js +1 -0
- package/dist/features/rules/interface.d.ts +22 -0
- package/dist/features/rules/interface.d.ts.map +1 -0
- package/dist/features/rules/interface.js +2 -0
- package/dist/features/rules/utils.d.ts +3 -0
- package/dist/features/rules/utils.d.ts.map +1 -0
- package/dist/features/rules/utils.js +31 -0
- package/dist/features/triggers/dtos.d.ts +9 -0
- package/dist/features/triggers/dtos.d.ts.map +1 -0
- package/dist/features/triggers/dtos.js +2 -0
- package/dist/features/triggers/index.d.ts +10 -0
- package/dist/features/triggers/index.d.ts.map +1 -0
- package/dist/features/triggers/index.js +57 -0
- package/dist/features/triggers/interface.d.ts +44 -0
- package/dist/features/triggers/interface.d.ts.map +1 -0
- package/dist/features/triggers/interface.js +2 -0
- package/dist/features/triggers/utils.d.ts +16 -0
- package/dist/features/triggers/utils.d.ts.map +1 -0
- package/dist/features/triggers/utils.js +153 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +84 -0
- package/dist/model.d.ts +2 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +2 -0
- package/dist/services/api/index.d.ts +36 -0
- package/dist/services/api/index.d.ts.map +1 -0
- package/dist/services/api/index.js +36 -0
- package/dist/services/api/model.d.ts +33 -0
- package/dist/services/api/model.d.ts.map +1 -0
- package/dist/services/api/model.js +2 -0
- package/dist/services/api/utils.d.ts +16 -0
- package/dist/services/api/utils.d.ts.map +1 -0
- package/dist/services/api/utils.js +45 -0
- package/dist/services/aws/index.d.ts +13 -0
- package/dist/services/aws/index.d.ts.map +1 -0
- package/dist/services/aws/index.js +50 -0
- package/dist/services/index.d.ts +41 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +14 -0
- package/dist/services/interface.d.ts +3 -0
- package/dist/services/interface.d.ts.map +1 -0
- package/dist/services/interface.js +2 -0
- package/dist/services/mongodb-atlas/index.d.ts +4 -0
- package/dist/services/mongodb-atlas/index.d.ts.map +1 -0
- package/dist/services/mongodb-atlas/index.js +483 -0
- package/dist/services/mongodb-atlas/model.d.ts +39 -0
- package/dist/services/mongodb-atlas/model.d.ts.map +1 -0
- package/dist/services/mongodb-atlas/model.js +2 -0
- package/dist/services/mongodb-atlas/utils.d.ts +8 -0
- package/dist/services/mongodb-atlas/utils.d.ts.map +1 -0
- package/dist/services/mongodb-atlas/utils.js +33 -0
- package/dist/state.d.ts +6 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +18 -0
- package/dist/utils/context/helpers.d.ts +74 -0
- package/dist/utils/context/helpers.d.ts.map +1 -0
- package/dist/utils/context/helpers.js +60 -0
- package/dist/utils/context/index.d.ts +14 -0
- package/dist/utils/context/index.d.ts.map +1 -0
- package/dist/utils/context/index.js +50 -0
- package/dist/utils/context/interface.d.ts +18 -0
- package/dist/utils/context/interface.d.ts.map +1 -0
- package/dist/utils/context/interface.js +2 -0
- package/dist/utils/crypto/index.d.ts +19 -0
- package/dist/utils/crypto/index.d.ts.map +1 -0
- package/dist/utils/crypto/index.js +50 -0
- package/dist/utils/helpers/someAsync.d.ts +12 -0
- package/dist/utils/helpers/someAsync.d.ts.map +1 -0
- package/dist/utils/helpers/someAsync.js +56 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +11 -0
- package/dist/utils/initializer/exposeRoutes.d.ts +8 -0
- package/dist/utils/initializer/exposeRoutes.d.ts.map +1 -0
- package/dist/utils/initializer/exposeRoutes.js +41 -0
- package/dist/utils/initializer/registerPlugins.d.ts +19 -0
- package/dist/utils/initializer/registerPlugins.d.ts.map +1 -0
- package/dist/utils/initializer/registerPlugins.js +84 -0
- package/dist/utils/roles/helpers.d.ts +4 -0
- package/dist/utils/roles/helpers.d.ts.map +1 -0
- package/dist/utils/roles/helpers.js +47 -0
- package/dist/utils/roles/interface.d.ts +33 -0
- package/dist/utils/roles/interface.d.ts.map +1 -0
- package/dist/utils/roles/interface.js +2 -0
- package/dist/utils/roles/machines/commonValidators.d.ts +6 -0
- package/dist/utils/roles/machines/commonValidators.d.ts.map +1 -0
- package/dist/utils/roles/machines/commonValidators.js +34 -0
- package/dist/utils/roles/machines/index.d.ts +14 -0
- package/dist/utils/roles/machines/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/index.js +27 -0
- package/dist/utils/roles/machines/interface.d.ts +46 -0
- package/dist/utils/roles/machines/interface.d.ts.map +1 -0
- package/dist/utils/roles/machines/interface.js +2 -0
- package/dist/utils/roles/machines/machine.d.ts +15 -0
- package/dist/utils/roles/machines/machine.d.ts.map +1 -0
- package/dist/utils/roles/machines/machine.js +97 -0
- package/dist/utils/roles/machines/read/A/index.d.ts +3 -0
- package/dist/utils/roles/machines/read/A/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/A/index.js +27 -0
- package/dist/utils/roles/machines/read/B/index.d.ts +3 -0
- package/dist/utils/roles/machines/read/B/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/B/index.js +36 -0
- package/dist/utils/roles/machines/read/C/index.d.ts +3 -0
- package/dist/utils/roles/machines/read/C/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/C/index.js +38 -0
- package/dist/utils/roles/machines/read/D/index.d.ts +3 -0
- package/dist/utils/roles/machines/read/D/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/D/index.js +26 -0
- package/dist/utils/roles/machines/read/D/validators.d.ts +4 -0
- package/dist/utils/roles/machines/read/D/validators.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/D/validators.js +24 -0
- package/dist/utils/roles/machines/read/index.d.ts +2 -0
- package/dist/utils/roles/machines/read/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/index.js +8 -0
- package/dist/utils/roles/machines/utils.d.ts +37 -0
- package/dist/utils/roles/machines/utils.d.ts.map +1 -0
- package/dist/utils/roles/machines/utils.js +54 -0
- package/dist/utils/roles/machines/write/A/index.d.ts +3 -0
- package/dist/utils/roles/machines/write/A/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/write/A/index.js +29 -0
- package/dist/utils/roles/machines/write/B/index.d.ts +3 -0
- package/dist/utils/roles/machines/write/B/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/write/B/index.js +47 -0
- package/dist/utils/roles/machines/write/C/index.d.ts +3 -0
- package/dist/utils/roles/machines/write/C/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/write/C/index.js +26 -0
- package/dist/utils/roles/machines/write/C/validators.d.ts +4 -0
- package/dist/utils/roles/machines/write/C/validators.d.ts.map +1 -0
- package/dist/utils/roles/machines/write/C/validators.js +24 -0
- package/dist/utils/roles/machines/write/index.d.ts +2 -0
- package/dist/utils/roles/machines/write/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/write/index.js +7 -0
- package/dist/utils/rules-matcher/interface.d.ts +338 -0
- package/dist/utils/rules-matcher/interface.d.ts.map +1 -0
- package/dist/utils/rules-matcher/interface.js +26 -0
- package/dist/utils/rules-matcher/utils.d.ts +11 -0
- package/dist/utils/rules-matcher/utils.d.ts.map +1 -0
- package/dist/utils/rules-matcher/utils.js +214 -0
- package/dist/utils/rules.d.ts +2 -0
- package/dist/utils/rules.d.ts.map +1 -0
- package/dist/utils/rules.js +22 -0
- package/jest.config.ts +24 -0
- package/package.json +63 -0
- package/project.json +10 -0
- package/rollup.config.js +17 -0
- package/src/auth/controller.ts +78 -0
- package/src/auth/dtos.ts +6 -0
- package/src/auth/plugins/jwt.ts +68 -0
- package/src/auth/providers/local-userpass/controller.ts +226 -0
- package/src/auth/providers/local-userpass/dtos.ts +40 -0
- package/src/auth/utils.ts +165 -0
- package/src/babel.config.json +3 -0
- package/src/constants.ts +22 -0
- package/src/fastify.d.ts +28 -0
- package/src/features/endpoints/index.ts +27 -0
- package/src/features/endpoints/interface.ts +29 -0
- package/src/features/endpoints/utils.ts +72 -0
- package/src/features/functions/controller.ts +102 -0
- package/src/features/functions/dtos.ts +41 -0
- package/src/features/functions/index.ts +21 -0
- package/src/features/functions/interface.ts +38 -0
- package/src/features/functions/utils.ts +82 -0
- package/src/features/rules/index.tsx +0 -0
- package/src/features/rules/interface.ts +24 -0
- package/src/features/rules/utils.ts +20 -0
- package/src/features/triggers/dtos.ts +9 -0
- package/src/features/triggers/index.ts +34 -0
- package/src/features/triggers/interface.ts +44 -0
- package/src/features/triggers/utils.ts +157 -0
- package/src/global.d.ts +0 -0
- package/src/index.ts +75 -0
- package/src/model.ts +1 -0
- package/src/services/api/index.ts +50 -0
- package/src/services/api/model.ts +38 -0
- package/src/services/api/utils.ts +39 -0
- package/src/services/aws/index.ts +48 -0
- package/src/services/index.ts +9 -0
- package/src/services/interface.ts +3 -0
- package/src/services/mongodb-atlas/index.ts +569 -0
- package/src/services/mongodb-atlas/model.ts +67 -0
- package/src/services/mongodb-atlas/utils.ts +44 -0
- package/src/state.ts +24 -0
- package/src/utils/__tests__/STEP_A_STATES.test.ts +54 -0
- package/src/utils/__tests__/STEP_B_STATES.test.ts +113 -0
- package/src/utils/__tests__/STEP_C_STATES.test.ts +87 -0
- package/src/utils/__tests__/STEP_D_STATES.test.ts +93 -0
- package/src/utils/__tests__/checkAdditionalFieldsFn.test.ts +45 -0
- package/src/utils/__tests__/checkApplyWhen.test.ts +49 -0
- package/src/utils/__tests__/checkFieldsPropertyExists.test.ts +47 -0
- package/src/utils/__tests__/checkIsValidFieldNameFn.test.ts +190 -0
- package/src/utils/__tests__/comparePassword.test.ts +38 -0
- package/src/utils/__tests__/evaluateDocumentsFiltersReadFn.test.ts +57 -0
- package/src/utils/__tests__/evaluateDocumentsFiltersWriteFn.test.ts +57 -0
- package/src/utils/__tests__/evaluateTopLevelReadFn.test.ts +58 -0
- package/src/utils/__tests__/evaluateTopLevelWriteFn.test.ts +66 -0
- package/src/utils/__tests__/exposeRoutes.test.ts +65 -0
- package/src/utils/__tests__/generateContextData.test.ts +75 -0
- package/src/utils/__tests__/getDefaultRule.test.ts +29 -0
- package/src/utils/__tests__/getKey.test.ts +12 -0
- package/src/utils/__tests__/getKeys.test.ts +11 -0
- package/src/utils/__tests__/getWinningRole.test.ts +66 -0
- package/src/utils/__tests__/hashPassword.test.ts +28 -0
- package/src/utils/__tests__/isEmpty.test.ts +17 -0
- package/src/utils/__tests__/logMachineInfo.test.ts +15 -0
- package/src/utils/__tests__/operators.test.ts +99 -0
- package/src/utils/__tests__/readFileContent.test.ts +35 -0
- package/src/utils/__tests__/registerPlugins.test.ts +59 -0
- package/src/utils/__tests__/rule.test.ts +51 -0
- package/src/utils/__tests__/rulesMatcherInterfaces.test.ts +57 -0
- package/src/utils/__tests__/rulesMatcherUtils.test.ts +56 -0
- package/src/utils/__tests__/someAsync.test.ts +55 -0
- package/src/utils/context/helpers.ts +71 -0
- package/src/utils/context/index.ts +52 -0
- package/src/utils/context/interface.ts +19 -0
- package/src/utils/crypto/index.ts +36 -0
- package/src/utils/helpers/someAsync.ts +24 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/initializer/exposeRoutes.ts +26 -0
- package/src/utils/initializer/registerPlugins.ts +97 -0
- package/src/utils/roles/helpers.ts +47 -0
- package/src/utils/roles/interface.ts +42 -0
- package/src/utils/roles/machines/commonValidators.ts +24 -0
- package/src/utils/roles/machines/index.ts +20 -0
- package/src/utils/roles/machines/interface.ts +46 -0
- package/src/utils/roles/machines/machine.ts +85 -0
- package/src/utils/roles/machines/read/A/index.ts +19 -0
- package/src/utils/roles/machines/read/B/index.ts +31 -0
- package/src/utils/roles/machines/read/C/index.ts +30 -0
- package/src/utils/roles/machines/read/D/index.ts +20 -0
- package/src/utils/roles/machines/read/D/validators.ts +24 -0
- package/src/utils/roles/machines/read/index.ts +6 -0
- package/src/utils/roles/machines/utils.ts +54 -0
- package/src/utils/roles/machines/write/A/index.ts +25 -0
- package/src/utils/roles/machines/write/B/index.ts +43 -0
- package/src/utils/roles/machines/write/C/index.ts +20 -0
- package/src/utils/roles/machines/write/C/validators.ts +24 -0
- package/src/utils/roles/machines/write/index.ts +5 -0
- package/src/utils/rules-matcher/interface.ts +365 -0
- package/src/utils/rules-matcher/utils.ts +281 -0
- package/src/utils/rules.ts +19 -0
- package/tsconfig.json +28 -0
|
@@ -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 @@
|
|
|
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,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,kBAexB,CAAA"}
|
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
try {
|
|
33
|
+
try {
|
|
34
|
+
for (_b = true, triggersList_1 = __asyncValues(triggersList); triggersList_1_1 = yield triggersList_1.next(), _c = triggersList_1_1.done, !_c; _b = true) {
|
|
35
|
+
_e = triggersList_1_1.value;
|
|
36
|
+
_b = false;
|
|
37
|
+
const trigger = _e;
|
|
38
|
+
const { content } = trigger;
|
|
39
|
+
const { type, config, event_processors } = content;
|
|
40
|
+
const functionName = event_processors.FUNCTION.config.function_name;
|
|
41
|
+
const triggerHandler = functionsList[functionName];
|
|
42
|
+
yield utils_1.TRIGGER_HANDLERS[type]({ config, triggerHandler, app: fastify, services: services_1.services, functionsList });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
46
|
+
finally {
|
|
47
|
+
try {
|
|
48
|
+
if (!_b && !_c && (_d = triggersList_1.return)) yield _d.call(triggersList_1);
|
|
49
|
+
}
|
|
50
|
+
finally { if (e_1) throw e_1.error; }
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
console.error('Error while activating triggers', e.message);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
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,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
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -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,iBAoClB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
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
|
+
const fastify = (0, fastify_1.default)({
|
|
56
|
+
logger: false
|
|
57
|
+
});
|
|
58
|
+
const functionsList = yield (0, utils_2.loadFunctions)();
|
|
59
|
+
const triggersList = yield (0, utils_4.loadTriggers)();
|
|
60
|
+
const endpointsList = yield (0, utils_1.loadEndpoints)();
|
|
61
|
+
const rulesList = yield (0, utils_3.loadRules)();
|
|
62
|
+
const stateConfig = {
|
|
63
|
+
functions: functionsList,
|
|
64
|
+
triggers: triggersList,
|
|
65
|
+
endpoints: endpointsList,
|
|
66
|
+
rules: rulesList,
|
|
67
|
+
app: fastify,
|
|
68
|
+
services: services_1.services
|
|
69
|
+
};
|
|
70
|
+
Object.entries(stateConfig).forEach(([key, value]) => state_1.StateManager.setData(key, value));
|
|
71
|
+
yield (0, registerPlugins_1.registerPlugins)({
|
|
72
|
+
register: fastify.register,
|
|
73
|
+
mongodbUrl,
|
|
74
|
+
jwtSecret,
|
|
75
|
+
functionsList
|
|
76
|
+
});
|
|
77
|
+
yield (0, exposeRoutes_1.exposeRoutes)(fastify);
|
|
78
|
+
yield (0, functions_1.registerFunctions)({ app: fastify, functionsList, rulesList });
|
|
79
|
+
yield (0, endpoints_1.generateEndpoints)({ app: fastify, functionsList, endpointsList });
|
|
80
|
+
fastify.ready(() => (0, triggers_1.activateTriggers)({ fastify, triggersList, functionsList }));
|
|
81
|
+
yield fastify.listen({ port, host });
|
|
82
|
+
fastify.log.info(`[${projectId}] Server listening on port ${port}`);
|
|
83
|
+
});
|
|
84
|
+
}
|
package/dist/model.d.ts
ADDED
|
@@ -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,36 @@
|
|
|
1
|
+
import { DeleteParams, GetParams, PostParams, PutParams } from './model';
|
|
2
|
+
/**
|
|
3
|
+
* > This service is the Api Client that can be imported from the context
|
|
4
|
+
*/
|
|
5
|
+
declare const Api: () => {
|
|
6
|
+
get: <T = null>({ url, headers }: GetParams) => Promise<{
|
|
7
|
+
status: number;
|
|
8
|
+
headers: import("undici/types/header").IncomingHttpHeaders;
|
|
9
|
+
body: {
|
|
10
|
+
message: string;
|
|
11
|
+
};
|
|
12
|
+
} | import("undici").Dispatcher.ResponseData<T>>;
|
|
13
|
+
post: <T = null>({ scheme, host, path, url: currentUrl, headers, body, encodeBodyAsJSON }: PostParams) => Promise<{
|
|
14
|
+
status: number;
|
|
15
|
+
headers: import("undici/types/header").IncomingHttpHeaders;
|
|
16
|
+
body: {
|
|
17
|
+
message: string;
|
|
18
|
+
};
|
|
19
|
+
} | import("undici").Dispatcher.ResponseData<T>>;
|
|
20
|
+
put: <T = null>({ scheme, host, path, url: currentUrl, headers, body, encodeBodyAsJSON }: PutParams) => Promise<{
|
|
21
|
+
status: number;
|
|
22
|
+
headers: import("undici/types/header").IncomingHttpHeaders;
|
|
23
|
+
body: {
|
|
24
|
+
message: string;
|
|
25
|
+
};
|
|
26
|
+
} | import("undici").Dispatcher.ResponseData<T>>;
|
|
27
|
+
delete: <T = null>({ scheme, host, path, url: currentUrl, headers }: DeleteParams) => Promise<{
|
|
28
|
+
status: number;
|
|
29
|
+
headers: import("undici/types/header").IncomingHttpHeaders;
|
|
30
|
+
body: {
|
|
31
|
+
message: string;
|
|
32
|
+
};
|
|
33
|
+
} | import("undici").Dispatcher.ResponseData<T>>;
|
|
34
|
+
};
|
|
35
|
+
export default Api;
|
|
36
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/api/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAGxE;;GAEG;AACH,QAAA,MAAM,GAAG;UACK,CAAC,2BAAgC,SAAS;;;;;;;WAGzC,CAAC,mFAQX,UAAU;;;;;;;UAKD,CAAC,mFAQV,SAAS;;;;;;;aAKG,CAAC,2DAMb,YAAY;;;;;;;CAIf,CAAA;AAEF,eAAe,GAAG,CAAA"}
|