@carbonorm/carbonnode 3.7.12 → 3.7.14

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.
@@ -0,0 +1,94 @@
1
+ import mysql from "mysql2/promise";
2
+ import axios from "axios";
3
+ import { AddressInfo } from "net";
4
+ import {describe, it, expect, beforeAll, afterAll} from "vitest";
5
+ import {Actor, C6, GLOBAL_REST_PARAMETERS} from "./sakila-db/C6.js";
6
+ import {C6C} from "../api/C6Constants";
7
+ import createTestServer from "../api/handlers/createTestServer";
8
+
9
+ let pool: mysql.Pool;
10
+ let server: any;
11
+
12
+ beforeAll(async () => {
13
+ pool = mysql.createPool({
14
+ host: "127.0.0.1",
15
+ user: "root",
16
+ password: "password",
17
+ database: "sakila",
18
+ });
19
+
20
+ const app = createTestServer({C6, mysqlPool: pool});
21
+ server = app.listen(0);
22
+ await new Promise(resolve => server.on('listening', resolve));
23
+ const {port} = server.address() as AddressInfo;
24
+
25
+ GLOBAL_REST_PARAMETERS.restURL = `http://127.0.0.1:${port}/rest/`;
26
+ GLOBAL_REST_PARAMETERS.axios = axios;
27
+ GLOBAL_REST_PARAMETERS.verbose = false;
28
+ // ensure HTTP executor is used
29
+ // @ts-ignore
30
+ delete GLOBAL_REST_PARAMETERS.mysqlPool;
31
+ });
32
+
33
+ afterAll(async () => {
34
+ await new Promise(resolve => server.close(resolve));
35
+ await pool.end();
36
+ });
37
+
38
+ describe("ExpressHandler e2e", () => {
39
+ it("handles GET requests", async () => {
40
+ const data = await Actor.Get({
41
+ [C6C.PAGINATION]: {
42
+ [C6C.LIMIT]: 1
43
+ },
44
+ });
45
+ expect(Array.isArray(data?.rest)).toBe(true);
46
+ expect(data?.rest?.length).toBeGreaterThan(0);
47
+ });
48
+
49
+
50
+ it("handles empty get requests", async () => {
51
+ const data = await Actor.Get({});
52
+ expect(Array.isArray(data?.rest)).toBe(true);
53
+ expect(data?.rest?.length).toBeGreaterThan(0);
54
+ });
55
+
56
+ it("handles POST, GET by id, PUT, and DELETE", async () => {
57
+ const first_name = `Test${Date.now()}`;
58
+ const last_name = `User${Date.now()}`;
59
+
60
+ await Actor.Post({
61
+ first_name,
62
+ last_name,
63
+ } as any);
64
+
65
+ let data = await Actor.Get({
66
+ [C6C.WHERE]: { [Actor.FIRST_NAME]: first_name, [Actor.LAST_NAME]: last_name },
67
+ [C6C.PAGINATION]: { [C6C.LIMIT]: 1 },
68
+ } as any);
69
+ expect(data?.rest).toHaveLength(1);
70
+ const testId = data?.rest[0].actor_id;
71
+
72
+
73
+ await Actor.Put({
74
+ [C6C.WHERE]: { [Actor.ACTOR_ID]: testId },
75
+ [C6C.UPDATE]: { first_name: "Updated" },
76
+ } as any);
77
+ data = await Actor.Get({
78
+ [C6C.WHERE]: { [Actor.ACTOR_ID]: testId },
79
+ } as any);
80
+ expect(data?.rest).toHaveLength(1);
81
+ expect(data?.rest[0].first_name).toBe("Updated");
82
+
83
+ await Actor.Delete({
84
+ [C6C.WHERE]: { [Actor.ACTOR_ID]: testId },
85
+ [C6C.DELETE]: true,
86
+ } as any);
87
+ data = await Actor.Get({
88
+ [C6C.WHERE]: { [Actor.ACTOR_ID]: testId },
89
+ cacheResults: false,
90
+ } as any);
91
+ expect(Array.isArray(data?.rest)).toBe(true);
92
+ expect(data?.rest.length).toBe(0);
93
+ });
94
+ });
@@ -1304,7 +1304,7 @@ export const TABLES = {
1304
1304
  };
1305
1305
  export const C6 = {
1306
1306
  ...C6Constants,
1307
- C6VERSION: '3.7.12',
1307
+ C6VERSION: '3.7.14',
1308
1308
  IMPORT: async (tableName) => {
1309
1309
  tableName = tableName.toLowerCase();
1310
1310
  // if tableName is not a key in the TABLES object then throw an error
@@ -1940,7 +1940,7 @@ export type RestTableInterfaces = iActor
1940
1940
 
1941
1941
  export const C6 : iC6Object<RestTableInterfaces> = {
1942
1942
  ...C6Constants,
1943
- C6VERSION: '3.7.12',
1943
+ C6VERSION: '3.7.14',
1944
1944
  IMPORT: async (tableName: string) : Promise<iDynamicApiImport> => {
1945
1945
 
1946
1946
  tableName = tableName.toLowerCase();
@@ -15,11 +15,11 @@ import {
15
15
  PUT, RequestQueryBody
16
16
  } from "../types/ormInterfaces";
17
17
  import {removeInvalidKeys, removePrefixIfExists, TestRestfulResponse} from "../utils/apiHelpers";
18
- import { normalizeSingularRequest } from "../utils/normalizeSingularRequest";
19
18
  import {apiRequestCache, checkCache, userCustomClearCache} from "../utils/cacheManager";
20
19
  import {sortAndSerializeQueryObject} from "../utils/sortAndSerializeQueryObject";
21
20
  import {Executor} from "./Executor";
22
21
  import {toastOptions, toastOptionsDevs} from "variables/toastOptions";
22
+ import {normalizeSingularRequest} from "../utils/normalizeSingularRequest";
23
23
 
24
24
  export class HttpExecutor<
25
25
  G extends OrmGenerics
@@ -130,8 +130,6 @@ export class HttpExecutor<
130
130
  clearCache,
131
131
  } = this.config
132
132
 
133
- console.log('this.config', this.config)
134
-
135
133
  await this.runLifecycleHooks<"beforeProcessing">(
136
134
  "beforeProcessing", {
137
135
  config: this.config,
@@ -790,7 +788,7 @@ export class HttpExecutor<
790
788
  accumulator.push(row['entity_tag']);
791
789
  }
792
790
  return accumulator;
793
- }, []).map((entityTag) => entityTag.split('\\').pop().toLowerCase());
791
+ }, []).map((entityTag) => entityTag.split('\\')?.pop()?.toLowerCase()!);
794
792
 
795
793
  const shouldContinue = referencesTables.find((referencesTable) => tableToFetch.endsWith(referencesTable))
796
794
 
@@ -0,0 +1,15 @@
1
+ import express, {Express} from "express";
2
+ import {Pool} from "mysql2/promise";
3
+ import {iC6Object} from "api/types/ormInterfaces";
4
+ import {ExpressHandler} from "./ExpressHandler";
5
+
6
+ export function createTestServer({C6, mysqlPool}: {C6: iC6Object; mysqlPool: Pool;}): Express {
7
+ const app = express();
8
+ app.set('query parser', 'extended');
9
+ app.use(express.json());
10
+ app.all("/rest/:table", ExpressHandler({C6, mysqlPool}));
11
+ app.all("/rest/:table/:primary", ExpressHandler({C6, mysqlPool}));
12
+ return app;
13
+ }
14
+
15
+ export default createTestServer;
@@ -127,7 +127,8 @@ export interface iGetC6RestResponse<
127
127
  ResponseDataType extends { [key: string]: any },
128
128
  ResponseDataOverrides = {}
129
129
  > extends iC6RestResponse<
130
- Modify<ResponseDataType, ResponseDataOverrides> | Modify<ResponseDataType, ResponseDataOverrides>[]
130
+ // TODO - We removed Modify<ResponseDataType, ResponseDataOverrides> |
131
+ Modify<ResponseDataType, ResponseDataOverrides>[]
131
132
  > {
132
133
  next?: () => Promise<DetermineResponseDataType<"GET", ResponseDataType, ResponseDataOverrides>>;
133
134
  }
package/src/index.ts CHANGED
@@ -16,6 +16,8 @@ export * from "./api/executors/Executor";
16
16
  export * from "./api/executors/HttpExecutor";
17
17
  export * from "./api/executors/SqlExecutor";
18
18
  export * from "./api/handlers/ExpressHandler";
19
+ export { default as createTestServer } from "./api/handlers/createTestServer";
20
+ export * from "./api/handlers/createTestServer";
19
21
  export * from "./api/orm/queryHelpers";
20
22
  export * from "./api/orm/builders/AggregateBuilder";
21
23
  export * from "./api/orm/builders/ConditionBuilder";