@carbonorm/carbonnode 3.7.6 → 3.7.8

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 (38) hide show
  1. package/README.md +2 -1
  2. package/dist/api/orm/builders/AggregateBuilder.d.ts +1 -0
  3. package/dist/api/types/ormInterfaces.d.ts +2 -2
  4. package/dist/api/utils/cacheManager.d.ts +1 -1
  5. package/dist/api/utils/normalizeSingularRequest.d.ts +10 -0
  6. package/dist/index.cjs.js +182 -32
  7. package/dist/index.cjs.js.map +1 -1
  8. package/dist/index.d.ts +1 -0
  9. package/dist/index.esm.js +183 -34
  10. package/dist/index.esm.js.map +1 -1
  11. package/package.json +11 -6
  12. package/scripts/assets/handlebars/C6.test.ts.handlebars +55 -80
  13. package/scripts/assets/handlebars/C6.ts.handlebars +28 -2
  14. package/scripts/generateRestBindings.cjs +17 -6
  15. package/scripts/generateRestBindings.ts +22 -8
  16. package/src/__tests__/fixtures/c6.fixture.ts +74 -0
  17. package/src/__tests__/normalizeSingularRequest.test.ts +105 -0
  18. package/src/__tests__/sakila-db/C6.js +1487 -0
  19. package/src/__tests__/sakila-db/C6.test.ts +63 -0
  20. package/src/__tests__/sakila-db/C6.ts +2206 -0
  21. package/src/__tests__/sakila-db/sakila-data.sql +46444 -0
  22. package/src/__tests__/sakila-db/sakila-schema.sql +686 -0
  23. package/src/__tests__/sakila-db/sakila.mwb +0 -0
  24. package/src/__tests__/sakila.generated.test.ts +46 -0
  25. package/src/__tests__/sqlBuilders.complex.test.ts +134 -0
  26. package/src/__tests__/sqlBuilders.test.ts +121 -0
  27. package/src/api/convertForRequestBody.ts +1 -1
  28. package/src/api/executors/HttpExecutor.ts +14 -3
  29. package/src/api/executors/SqlExecutor.ts +14 -1
  30. package/src/api/orm/builders/AggregateBuilder.ts +3 -0
  31. package/src/api/orm/builders/ConditionBuilder.ts +34 -11
  32. package/src/api/orm/builders/PaginationBuilder.ts +10 -4
  33. package/src/api/orm/queries/SelectQueryBuilder.ts +3 -0
  34. package/src/api/orm/queries/UpdateQueryBuilder.ts +2 -1
  35. package/src/api/types/ormInterfaces.ts +3 -4
  36. package/src/api/utils/cacheManager.ts +1 -1
  37. package/src/api/utils/normalizeSingularRequest.ts +144 -0
  38. package/src/index.ts +1 -0
@@ -0,0 +1,144 @@
1
+ import { C6Constants as C6C } from "../C6Constants";
2
+ import { C6RestfulModel, iRestMethods, RequestQueryBody } from "../types/ormInterfaces";
3
+
4
+ /**
5
+ * Converts a singular T-shaped request into complex ORM format for GET/PUT/DELETE
6
+ * Enforces that all primary keys are present for singular syntax and that the table has PKs.
7
+ * Optionally accepts a previously removed primary key (key/value) to reconstruct WHERE.
8
+ */
9
+ export function normalizeSingularRequest<
10
+ Method extends iRestMethods,
11
+ T extends Record<string, any>,
12
+ Custom extends Record<string, any> = {},
13
+ Overrides extends Record<string, any> = {}
14
+ > (
15
+ requestMethod: Method,
16
+ request: RequestQueryBody<Method, T, Custom, Overrides>,
17
+ restModel: C6RestfulModel<string, T, any>,
18
+ removedPrimary?: { key: string; value: any }
19
+ ): RequestQueryBody<Method, T, Custom, Overrides> {
20
+ if (request == null || typeof request !== 'object') return request;
21
+
22
+ const specialKeys = new Set([
23
+ C6C.SELECT,
24
+ C6C.UPDATE,
25
+ C6C.DELETE,
26
+ C6C.WHERE,
27
+ C6C.JOIN,
28
+ C6C.PAGINATION,
29
+ ]);
30
+
31
+ // Determine if the request is already complex (has any special key besides PAGINATION)
32
+ const keys = Object.keys(request as any);
33
+ const hasComplexKeys = keys.some(k => k !== C6C.PAGINATION && specialKeys.has(k));
34
+ if (hasComplexKeys) return request; // already complex
35
+
36
+ // We treat it as singular when it's not complex.
37
+ // For GET, PUT, DELETE only
38
+ if (!(requestMethod === C6C.GET || requestMethod === C6C.PUT || requestMethod === C6C.DELETE)) {
39
+ return request;
40
+ }
41
+
42
+ const pkShorts: string[] = Array.isArray(restModel.PRIMARY_SHORT) ? [...restModel.PRIMARY_SHORT] : [];
43
+ if (!pkShorts.length) {
44
+ // For GET requests, do not enforce primary key presence; treat as a collection query.
45
+ if (requestMethod === C6C.GET) return request;
46
+ throw new Error(`Table (${restModel.TABLE_NAME}) has no primary key; singular request syntax is not allowed.`);
47
+ }
48
+
49
+ // Build pk map from request + possibly removed primary key
50
+ const pkValues: Record<string, any> = {};
51
+ for (const pk of pkShorts) {
52
+ const fromRequest = (request as any)[pk];
53
+ if (fromRequest !== undefined && fromRequest !== null) {
54
+ pkValues[pk] = fromRequest;
55
+ continue;
56
+ }
57
+ if (removedPrimary && removedPrimary.key === pk) {
58
+ pkValues[pk] = removedPrimary.value;
59
+ continue;
60
+ }
61
+ }
62
+
63
+ const missing = pkShorts.filter(pk => !(pk in pkValues));
64
+ if (missing.length) {
65
+ // For GET requests, if not all PKs are provided, treat as a collection query and leave as-is.
66
+ if (requestMethod === C6C.GET) {
67
+ return request;
68
+ }
69
+ throw new Error(`Singular request requires all primary key(s) [${pkShorts.join(', ')}] for table (${restModel.TABLE_NAME}). Missing: [${missing.join(', ')}]`);
70
+ }
71
+
72
+ // Strip API metadata that should remain at root
73
+ const {
74
+ dataInsertMultipleRows,
75
+ cacheResults,
76
+ fetchDependencies,
77
+ debug,
78
+ success,
79
+ error,
80
+ ...rest
81
+ } = request as any;
82
+
83
+ if (requestMethod === C6C.GET) {
84
+ const normalized: any = {
85
+ WHERE: { ...pkValues },
86
+ };
87
+ // Preserve pagination if any was added previously
88
+ if ((request as any)[C6C.PAGINATION]) {
89
+ normalized[C6C.PAGINATION] = (request as any)[C6C.PAGINATION];
90
+ }
91
+ return {
92
+ ...normalized,
93
+ dataInsertMultipleRows,
94
+ cacheResults,
95
+ fetchDependencies,
96
+ debug,
97
+ success,
98
+ error,
99
+ } as any;
100
+ }
101
+
102
+ if (requestMethod === C6C.DELETE) {
103
+ const normalized: any = {
104
+ [C6C.DELETE]: true,
105
+ WHERE: { ...pkValues },
106
+ };
107
+ return {
108
+ ...normalized,
109
+ dataInsertMultipleRows,
110
+ cacheResults,
111
+ fetchDependencies,
112
+ debug,
113
+ success,
114
+ error,
115
+ } as any;
116
+ }
117
+
118
+ // PUT
119
+ const updateBody: Record<string, any> = {};
120
+ for (const k of Object.keys(rest)) {
121
+ if (pkShorts.includes(k)) continue; // don't update PK columns
122
+ // Skip special request keys if any slipped through
123
+ if (specialKeys.has(k)) continue;
124
+ updateBody[k] = (rest as any)[k];
125
+ }
126
+ if (Object.keys(updateBody).length === 0) {
127
+ throw new Error(`Singular PUT request for table (${restModel.TABLE_NAME}) must include at least one non-primary field to update.`);
128
+ }
129
+
130
+ const normalized: any = {
131
+ [C6C.UPDATE]: updateBody,
132
+ WHERE: { ...pkValues },
133
+ };
134
+
135
+ return {
136
+ ...normalized,
137
+ dataInsertMultipleRows,
138
+ cacheResults,
139
+ fetchDependencies,
140
+ debug,
141
+ success,
142
+ error,
143
+ } as any;
144
+ }
package/src/index.ts CHANGED
@@ -35,6 +35,7 @@ export * from "./api/utils/apiHelpers";
35
35
  export * from "./api/utils/cacheManager";
36
36
  export * from "./api/utils/determineRuntimeJsType";
37
37
  export * from "./api/utils/logger";
38
+ export * from "./api/utils/normalizeSingularRequest";
38
39
  export * from "./api/utils/sortAndSerializeQueryObject";
39
40
  export * from "./api/utils/testHelpers";
40
41
  export * from "./api/utils/toastNotifier";