@carbonorm/carbonnode 3.11.0 → 4.0.1
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/README.md +123 -493
- package/dist/api/orm/builders/ConditionBuilder.d.ts +5 -0
- package/dist/api/orm/queries/PostQueryBuilder.d.ts +1 -1
- package/dist/api/restOrm.d.ts +4 -4
- package/dist/api/types/ormInterfaces.d.ts +32 -12
- package/dist/api/utils/cacheManager.d.ts +7 -8
- package/dist/index.cjs.js +289 -140
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +285 -139
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/cacheManager.test.ts +67 -0
- package/src/__tests__/expressServer.e2e.test.ts +104 -2
- package/src/__tests__/fixtures/c6.fixture.ts +2 -0
- package/src/__tests__/httpExecutorSingular.e2e.test.ts +35 -4
- package/src/__tests__/sakila-db/C6.js +1 -1
- package/src/__tests__/sakila-db/C6.ts +1 -1
- package/src/__tests__/sqlBuilders.test.ts +45 -0
- package/src/api/axiosInstance.ts +29 -0
- package/src/api/executors/HttpExecutor.ts +73 -97
- package/src/api/handlers/ExpressHandler.ts +30 -7
- package/src/api/orm/builders/ConditionBuilder.ts +74 -0
- package/src/api/orm/queries/PostQueryBuilder.ts +4 -2
- package/src/api/orm/queries/UpdateQueryBuilder.ts +3 -1
- package/src/api/types/ormInterfaces.ts +32 -18
- package/src/api/utils/cacheManager.ts +75 -34
- package/src/api/utils/testHelpers.ts +5 -3
- package/src/variables/isNode.ts +1 -8
|
@@ -1,46 +1,87 @@
|
|
|
1
|
-
import {AxiosPromise} from "axios";
|
|
1
|
+
import { AxiosPromise } from "axios";
|
|
2
2
|
import { iCacheAPI } from "../types/ormInterfaces";
|
|
3
3
|
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
4
|
+
// -----------------------------------------------------------------------------
|
|
5
|
+
// Cache Storage
|
|
6
|
+
// -----------------------------------------------------------------------------
|
|
7
|
+
export const apiRequestCache = new Map<string, iCacheAPI>();
|
|
8
|
+
export const userCustomClearCache: (() => void)[] = [];
|
|
9
|
+
|
|
10
|
+
// -----------------------------------------------------------------------------
|
|
11
|
+
// Cache Key Generator (safe, fixed-size ~40 chars)
|
|
12
|
+
// -----------------------------------------------------------------------------
|
|
13
|
+
// -----------------------------------------------------------------------------
|
|
14
|
+
// Browser-safe deterministic hash (FNV-1a)
|
|
15
|
+
// -----------------------------------------------------------------------------
|
|
16
|
+
function fnv1a(str: string): string {
|
|
17
|
+
let h = 0x811c9dc5;
|
|
18
|
+
for (let i = 0; i < str.length; i++) {
|
|
19
|
+
h ^= str.charCodeAt(i);
|
|
20
|
+
h = (h * 0x01000193) >>> 0;
|
|
20
21
|
}
|
|
21
|
-
|
|
22
|
-
userCustomClearCache.map((f) => 'function' === typeof f && f());
|
|
23
|
-
|
|
24
|
-
userCustomClearCache = apiRequestCache = []
|
|
25
|
-
|
|
22
|
+
return h.toString(16);
|
|
26
23
|
}
|
|
27
24
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
25
|
+
function makeCacheKey(
|
|
26
|
+
method: string,
|
|
27
|
+
tableName: string | string[],
|
|
28
|
+
requestData: unknown,
|
|
29
|
+
): string {
|
|
30
|
+
const raw = JSON.stringify([method, tableName, requestData]);
|
|
31
|
+
return fnv1a(raw);
|
|
32
|
+
}
|
|
33
33
|
|
|
34
|
+
// -----------------------------------------------------------------------------
|
|
35
|
+
// Clear Cache (no shared-array bugs)
|
|
36
|
+
// -----------------------------------------------------------------------------
|
|
37
|
+
export function clearCache(props?: { ignoreWarning?: boolean }): void {
|
|
38
|
+
if (!props?.ignoreWarning) {
|
|
39
|
+
console.warn(
|
|
40
|
+
"The REST API clearCache should only be used with extreme care!",
|
|
41
|
+
);
|
|
34
42
|
}
|
|
35
43
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
44
|
+
for (const fn of userCustomClearCache) {
|
|
45
|
+
try {
|
|
46
|
+
fn();
|
|
47
|
+
} catch {}
|
|
48
|
+
}
|
|
41
49
|
|
|
42
|
-
|
|
50
|
+
apiRequestCache.clear();
|
|
51
|
+
}
|
|
43
52
|
|
|
44
|
-
|
|
53
|
+
// -----------------------------------------------------------------------------
|
|
54
|
+
// Check Cache (dedupe via hashed key)
|
|
55
|
+
// -----------------------------------------------------------------------------
|
|
56
|
+
export function checkCache<ResponseDataType = any>(
|
|
57
|
+
method: string,
|
|
58
|
+
tableName: string | string[],
|
|
59
|
+
requestData: any,
|
|
60
|
+
): AxiosPromise<ResponseDataType> | false {
|
|
61
|
+
const key = makeCacheKey(method, tableName, requestData);
|
|
62
|
+
const cached = apiRequestCache.get(key);
|
|
63
|
+
|
|
64
|
+
if (!cached) return false;
|
|
65
|
+
|
|
66
|
+
console.groupCollapsed(
|
|
67
|
+
`%c API cache hit for ${method} ${tableName}`,
|
|
68
|
+
"color:#0c0",
|
|
69
|
+
);
|
|
70
|
+
console.log("Request Data:", requestData);
|
|
71
|
+
console.groupEnd();
|
|
72
|
+
|
|
73
|
+
return cached.request;
|
|
74
|
+
}
|
|
45
75
|
|
|
76
|
+
// -----------------------------------------------------------------------------
|
|
77
|
+
// Store Cache Entry (drop-in compatible)
|
|
78
|
+
// -----------------------------------------------------------------------------
|
|
79
|
+
export function setCache<ResponseDataType = any>(
|
|
80
|
+
method: string,
|
|
81
|
+
tableName: string | string[],
|
|
82
|
+
requestData: any,
|
|
83
|
+
cacheEntry: iCacheAPI<ResponseDataType>,
|
|
84
|
+
): void {
|
|
85
|
+
const key = makeCacheKey(method, tableName, requestData);
|
|
86
|
+
apiRequestCache.set(key, cacheEntry);
|
|
46
87
|
}
|
|
@@ -2,13 +2,15 @@ import {apiRequestCache} from "./cacheManager";
|
|
|
2
2
|
|
|
3
3
|
export function checkAllRequestsComplete(): true | (string[]) {
|
|
4
4
|
|
|
5
|
-
const
|
|
5
|
+
const cacheEntries = Array.from(apiRequestCache.values())
|
|
6
|
+
|
|
7
|
+
const stillRunning = cacheEntries.filter((cache) => undefined === cache.response)
|
|
6
8
|
|
|
7
9
|
if (stillRunning.length !== 0) {
|
|
8
10
|
|
|
9
11
|
if (document === null || document === undefined) {
|
|
10
12
|
|
|
11
|
-
throw new Error('document is undefined while waiting for API requests to complete (' + JSON.stringify(
|
|
13
|
+
throw new Error('document is undefined while waiting for API requests to complete (' + JSON.stringify(cacheEntries) + ')')
|
|
12
14
|
|
|
13
15
|
}
|
|
14
16
|
|
|
@@ -21,4 +23,4 @@ export function checkAllRequestsComplete(): true | (string[]) {
|
|
|
21
23
|
|
|
22
24
|
return true
|
|
23
25
|
|
|
24
|
-
}
|
|
26
|
+
}
|
package/src/variables/isNode.ts
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
const isNode = () => {
|
|
2
|
-
|
|
3
|
-
console.log('Checking if running in Node.js environment...');
|
|
4
|
-
|
|
5
|
-
const isNodeEnv = typeof process !== 'undefined' && !!process.versions?.node;
|
|
6
|
-
|
|
7
|
-
console.log(`Is Node.js environment: ${isNodeEnv}`);
|
|
8
|
-
|
|
9
|
-
return isNodeEnv;
|
|
2
|
+
return typeof process !== 'undefined' && !!process.versions?.node;
|
|
10
3
|
}
|
|
11
4
|
|
|
12
5
|
export default isNode;
|