@carbonorm/carbonnode 3.0.2 → 3.0.4
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/dist/api/convertForRequestBody.d.ts +7 -3
- package/dist/api/executors/Executor.d.ts +9 -9
- package/dist/api/executors/HttpExecutor.d.ts +9 -5
- package/dist/api/executors/SqlExecutor.d.ts +5 -5
- package/dist/api/restOrm.d.ts +15 -0
- package/dist/api/restRequest.d.ts +4 -5
- package/dist/api/types/ormInterfaces.d.ts +165 -139
- package/dist/index.cjs.js +263 -160
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.esm.js +264 -161
- package/dist/index.esm.js.map +1 -1
- package/package.json +5 -4
- package/scripts/assets/handlebars/C6.test.ts.handlebars +88 -0
- package/scripts/assets/handlebars/C6.ts.handlebars +45 -12
- package/scripts/generateRestBindings.cjs +3 -7
- package/scripts/generateRestBindings.ts +3 -14
- package/src/api/convertForRequestBody.ts +62 -90
- package/src/api/executors/Executor.ts +67 -13
- package/src/api/executors/HttpExecutor.ts +224 -90
- package/src/api/executors/SqlExecutor.ts +6 -6
- package/src/api/restOrm.ts +61 -0
- package/src/api/restRequest.ts +19 -14
- package/src/api/types/ormInterfaces.ts +208 -246
- package/src/api/utils/apiHelpers.ts +4 -0
- package/src/index.ts +1 -0
- package/scripts/assets/handlebars/Table.test.ts.handlebars +0 -126
- package/scripts/assets/handlebars/Table.ts.handlebars +0 -193
|
@@ -1,109 +1,81 @@
|
|
|
1
|
-
import {C6Constants} from "api/C6Constants";
|
|
2
|
-
import {iC6Object, iC6RestfulModel} from "./types/ormInterfaces";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { C6Constants } from "api/C6Constants";
|
|
2
|
+
import {iC6Object, iC6RestfulModel, iRestMethods, RequestQueryBody} from "./types/ormInterfaces";
|
|
3
|
+
|
|
4
|
+
export default function <
|
|
5
|
+
RequestMethod extends iRestMethods,
|
|
6
|
+
RestTableInterface extends { [key: string]: any },
|
|
7
|
+
CustomAndRequiredFields extends { [key: string]: any } = {},
|
|
8
|
+
RequestTableOverrides extends { [K in keyof RestTableInterface]?: any } = {}
|
|
9
|
+
>(
|
|
10
|
+
restfulObject: RequestQueryBody<RequestMethod, RestTableInterface, CustomAndRequiredFields, RequestTableOverrides>,
|
|
11
|
+
tableName: string | string[],
|
|
12
|
+
C6: iC6Object,
|
|
13
|
+
regexErrorHandler: (message: string) => void = alert
|
|
14
|
+
) {
|
|
15
|
+
const payload: Record<string, any> = {};
|
|
9
16
|
const tableNames = Array.isArray(tableName) ? tableName : [tableName];
|
|
10
17
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (undefined === tableDefinition) {
|
|
18
|
-
|
|
19
|
-
console.error(`Table name (${tableName}) is not found in the C6.TABLES object.`, C6.TABLES);
|
|
20
|
-
|
|
21
|
-
throw new Error(`Table name (${tableName}) is not found in the C6.TABLES object.`);
|
|
22
|
-
|
|
18
|
+
const tableDefinitions: (iC6RestfulModel<any, any, any> & any)[] = tableNames.map((name) => {
|
|
19
|
+
const tableDefinition = Object.values(C6.TABLES).find((t) => t.TABLE_NAME === name);
|
|
20
|
+
if (!tableDefinition) {
|
|
21
|
+
console.error(`Table name (${name}) is not found in the C6.TABLES object.`, C6.TABLES);
|
|
22
|
+
throw new Error(`Table name (${name}) is not found in the C6.TABLES object.`);
|
|
23
23
|
}
|
|
24
|
+
return tableDefinition;
|
|
25
|
+
});
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
.sort()
|
|
49
|
-
.reduce((acc, key) => ({
|
|
50
|
-
...acc, [key]: restfulObject[value][key]
|
|
51
|
-
}), {})
|
|
52
|
-
}
|
|
53
|
-
return
|
|
54
|
-
default:
|
|
27
|
+
for (const tableDefinition of tableDefinitions) {
|
|
28
|
+
for (const value of Object.keys(restfulObject)) {
|
|
29
|
+
const shortReference = value.toUpperCase();
|
|
30
|
+
|
|
31
|
+
if ([
|
|
32
|
+
C6Constants.GET,
|
|
33
|
+
C6Constants.POST,
|
|
34
|
+
C6Constants.UPDATE,
|
|
35
|
+
C6Constants.REPLACE,
|
|
36
|
+
C6Constants.DELETE,
|
|
37
|
+
C6Constants.WHERE,
|
|
38
|
+
C6Constants.JOIN,
|
|
39
|
+
C6Constants.PAGINATION
|
|
40
|
+
].includes(value)) {
|
|
41
|
+
const val = restfulObject[value];
|
|
42
|
+
if (Array.isArray(val)) {
|
|
43
|
+
payload[value] = val.sort();
|
|
44
|
+
} else if (typeof val === 'object' && val !== null) {
|
|
45
|
+
payload[value] = Object.keys(val)
|
|
46
|
+
.sort()
|
|
47
|
+
.reduce((acc, key) => ({ ...acc, [key]: val[key] }), {});
|
|
48
|
+
}
|
|
49
|
+
continue;
|
|
55
50
|
}
|
|
56
51
|
|
|
57
52
|
if (shortReference in tableDefinition) {
|
|
58
|
-
|
|
59
53
|
const longName = tableDefinition[shortReference];
|
|
54
|
+
const columnValue = restfulObject[value];
|
|
55
|
+
payload[longName] = columnValue;
|
|
60
56
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const regexValidations = tableDefinition.REGEX_VALIDATION[longName]
|
|
57
|
+
const regexValidations = tableDefinition.REGEX_VALIDATION[longName];
|
|
64
58
|
|
|
65
59
|
if (regexValidations instanceof RegExp) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
regexErrorHandler('Failed to match regex (' + regexValidations + ') for column (' + longName + ')')
|
|
70
|
-
|
|
71
|
-
throw Error('Failed to match regex (' + regexValidations + ') for column (' + longName + ')')
|
|
72
|
-
|
|
60
|
+
if (!regexValidations.test(columnValue)) {
|
|
61
|
+
regexErrorHandler(`Failed to match regex (${regexValidations}) for column (${longName})`);
|
|
62
|
+
throw new Error(`Failed to match regex (${regexValidations}) for column (${longName})`);
|
|
73
63
|
}
|
|
74
|
-
|
|
75
64
|
} else if (typeof regexValidations === 'object' && regexValidations !== null) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const devErrorMessage = 'Failed to match regex (' + regex + ') for column (' + longName + ')';
|
|
84
|
-
|
|
85
|
-
regexErrorHandler(errorMessage ?? devErrorMessage)
|
|
86
|
-
|
|
87
|
-
throw Error(devErrorMessage)
|
|
88
|
-
|
|
65
|
+
for (const errorMessage in regexValidations) {
|
|
66
|
+
const regex: RegExp = regexValidations[errorMessage];
|
|
67
|
+
if (!regex.test(columnValue)) {
|
|
68
|
+
const devErrorMessage = `Failed to match regex (${regex}) for column (${longName})`;
|
|
69
|
+
regexErrorHandler(errorMessage || devErrorMessage);
|
|
70
|
+
throw new Error(devErrorMessage);
|
|
89
71
|
}
|
|
90
|
-
|
|
91
|
-
})
|
|
92
|
-
|
|
72
|
+
}
|
|
93
73
|
}
|
|
94
|
-
|
|
95
74
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
return true;
|
|
100
|
-
|
|
101
|
-
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
102
77
|
|
|
103
78
|
return Object.keys(payload)
|
|
104
79
|
.sort()
|
|
105
|
-
.reduce((acc, key) => ({
|
|
106
|
-
|
|
107
|
-
}), {})
|
|
108
|
-
|
|
109
|
-
};
|
|
80
|
+
.reduce((acc, key) => ({ ...acc, [key]: payload[key] }), {});
|
|
81
|
+
}
|
|
@@ -1,28 +1,82 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
apiReturn,
|
|
3
|
+
DetermineResponseDataType,
|
|
4
|
+
iRest,
|
|
5
|
+
iRestMethods,
|
|
6
|
+
iRestReactiveLifecycle,
|
|
7
|
+
RequestQueryBody
|
|
8
|
+
} from "@carbonorm/carbonnode";
|
|
9
|
+
import isVerbose from "../../variables/isVerbose";
|
|
3
10
|
|
|
4
11
|
export abstract class Executor<
|
|
12
|
+
RequestMethod extends iRestMethods,
|
|
5
13
|
RestShortTableName extends string = any,
|
|
6
14
|
RestTableInterface extends { [key: string]: any } = any,
|
|
7
15
|
PrimaryKey extends Extract<keyof RestTableInterface, string> = Extract<keyof RestTableInterface, string>,
|
|
8
16
|
CustomAndRequiredFields extends { [key: string]: any } = any,
|
|
9
|
-
RequestTableOverrides extends { [key
|
|
10
|
-
ResponseDataType = any
|
|
17
|
+
RequestTableOverrides extends { [key in keyof RestTableInterface]: any } = { [key in keyof RestTableInterface]: any }
|
|
11
18
|
> {
|
|
12
|
-
|
|
13
19
|
public constructor(
|
|
14
20
|
protected config: iRest<
|
|
21
|
+
RestShortTableName,
|
|
22
|
+
RestTableInterface,
|
|
23
|
+
PrimaryKey
|
|
24
|
+
>,
|
|
25
|
+
protected request: RequestQueryBody<
|
|
26
|
+
RequestMethod,
|
|
27
|
+
RestTableInterface,
|
|
28
|
+
CustomAndRequiredFields,
|
|
29
|
+
RequestTableOverrides
|
|
30
|
+
>
|
|
31
|
+
) {}
|
|
32
|
+
|
|
33
|
+
abstract execute(): Promise<apiReturn<DetermineResponseDataType<RequestMethod, RestTableInterface>>>;
|
|
34
|
+
|
|
35
|
+
async runLifecycleHooks<
|
|
36
|
+
Phase extends keyof iRestReactiveLifecycle<
|
|
37
|
+
RequestMethod,
|
|
15
38
|
RestShortTableName,
|
|
16
39
|
RestTableInterface,
|
|
17
40
|
PrimaryKey,
|
|
18
41
|
CustomAndRequiredFields,
|
|
19
|
-
RequestTableOverrides
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
42
|
+
RequestTableOverrides
|
|
43
|
+
>
|
|
44
|
+
>(
|
|
45
|
+
phase: Phase,
|
|
46
|
+
args: Parameters<NonNullable<
|
|
47
|
+
iRestReactiveLifecycle<
|
|
48
|
+
RequestMethod,
|
|
49
|
+
RestShortTableName,
|
|
50
|
+
RestTableInterface,
|
|
51
|
+
PrimaryKey,
|
|
52
|
+
CustomAndRequiredFields,
|
|
53
|
+
RequestTableOverrides
|
|
54
|
+
>[Phase]
|
|
55
|
+
>[string]>[0]
|
|
56
|
+
): Promise<void> {
|
|
57
|
+
const lifecycleGroup = this.config.restModel.LIFECYCLE_HOOKS[this.config.requestMethod]?.[phase];
|
|
58
|
+
|
|
59
|
+
if (!lifecycleGroup) return;
|
|
25
60
|
|
|
26
|
-
|
|
61
|
+
for (const [key, fn] of Object.entries(lifecycleGroup)) {
|
|
62
|
+
if (typeof fn === "function") {
|
|
63
|
+
if (isVerbose || (args.request as any).debug) {
|
|
64
|
+
console.groupCollapsed(`[LIFECYCLE] ${this.config.requestMethod}.${String(phase)}:${key}`);
|
|
65
|
+
console.log("config:", args.config);
|
|
66
|
+
console.log("request:", args.request);
|
|
67
|
+
if ("response" in args) {
|
|
68
|
+
console.log("response:", args.response);
|
|
69
|
+
}
|
|
70
|
+
console.groupEnd();
|
|
71
|
+
}
|
|
27
72
|
|
|
28
|
-
|
|
73
|
+
try {
|
|
74
|
+
await fn(args);
|
|
75
|
+
} catch (err) {
|
|
76
|
+
console.error(`[LIFECYCLE ERROR] ${this.config.requestMethod}.${String(phase)}:${key}`, err);
|
|
77
|
+
throw err;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|