@carbonorm/carbonnode 3.0.0 → 3.0.2

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 (47) hide show
  1. package/dist/api/builders/sqlBuilder.d.ts +3 -0
  2. package/dist/api/convertForRequestBody.d.ts +1 -1
  3. package/dist/api/executors/Executor.d.ts +16 -0
  4. package/dist/api/executors/HttpExecutor.d.ts +13 -0
  5. package/dist/api/executors/SqlExecutor.d.ts +19 -0
  6. package/dist/api/restRequest.d.ts +9 -166
  7. package/dist/api/types/dynamicFetching.d.ts +10 -0
  8. package/dist/api/types/modifyTypes.d.ts +9 -0
  9. package/dist/api/types/mysqlTypes.d.ts +4 -0
  10. package/dist/api/types/ormInterfaces.d.ts +219 -0
  11. package/dist/api/utils/apiHelpers.d.ts +9 -0
  12. package/dist/api/utils/cacheManager.d.ts +10 -0
  13. package/dist/api/utils/determineRuntimeJsType.d.ts +5 -0
  14. package/dist/api/utils/logger.d.ts +7 -0
  15. package/dist/api/utils/sortAndSerializeQueryObject.d.ts +1 -0
  16. package/dist/api/utils/testHelpers.d.ts +1 -0
  17. package/dist/api/utils/toastNotifier.d.ts +2 -0
  18. package/dist/index.cjs.js +665 -614
  19. package/dist/index.cjs.js.map +1 -1
  20. package/dist/index.d.ts +15 -2
  21. package/dist/index.esm.js +655 -618
  22. package/dist/index.esm.js.map +1 -1
  23. package/package.json +22 -6
  24. package/scripts/assets/handlebars/C6.ts.handlebars +13 -5
  25. package/scripts/assets/handlebars/Table.ts.handlebars +44 -12
  26. package/scripts/generateRestBindings.cjs +1 -1
  27. package/scripts/generateRestBindings.ts +1 -1
  28. package/src/api/builders/sqlBuilder.ts +173 -0
  29. package/src/api/convertForRequestBody.ts +2 -3
  30. package/src/api/executors/Executor.ts +28 -0
  31. package/src/api/executors/HttpExecutor.ts +794 -0
  32. package/src/api/executors/SqlExecutor.ts +104 -0
  33. package/src/api/restRequest.ts +50 -1287
  34. package/src/api/types/dynamicFetching.ts +10 -0
  35. package/src/api/types/modifyTypes.ts +25 -0
  36. package/src/api/types/mysqlTypes.ts +33 -0
  37. package/src/api/types/ormInterfaces.ts +310 -0
  38. package/src/api/utils/apiHelpers.ts +82 -0
  39. package/src/api/utils/cacheManager.ts +67 -0
  40. package/src/api/utils/determineRuntimeJsType.ts +46 -0
  41. package/src/api/utils/logger.ts +24 -0
  42. package/src/api/utils/sortAndSerializeQueryObject.ts +12 -0
  43. package/src/api/utils/testHelpers.ts +24 -0
  44. package/src/api/utils/toastNotifier.ts +11 -0
  45. package/src/index.ts +15 -2
  46. package/src/api/carbonSqlExecutor.ts +0 -279
  47. package/src/api/interfaces/ormInterfaces.ts +0 -87
@@ -0,0 +1,104 @@
1
+ import {apiReturn} from "@carbonorm/carbonnode";
2
+ import { PoolConnection, RowDataPacket } from 'mysql2/promise';
3
+ import {buildSelectQuery} from "../builders/sqlBuilder";
4
+ import {Executor} from "./Executor";
5
+
6
+
7
+ export class SqlExecutor<
8
+ RestShortTableName extends string = any,
9
+ RestTableInterface extends { [key: string]: any } = any,
10
+ PrimaryKey extends Extract<keyof RestTableInterface, string> = Extract<keyof RestTableInterface, string>,
11
+ CustomAndRequiredFields extends { [key: string]: any } = any,
12
+ RequestTableOverrides extends { [key: string]: any; } = { [key in keyof RestTableInterface]: any },
13
+ ResponseDataType = any
14
+ >
15
+ extends Executor<
16
+ RestShortTableName,
17
+ RestTableInterface,
18
+ PrimaryKey,
19
+ CustomAndRequiredFields,
20
+ RequestTableOverrides,
21
+ ResponseDataType
22
+ > {
23
+
24
+ public execute(): Promise<apiReturn<ResponseDataType>> {
25
+ switch (this.config.requestMethod) {
26
+ case 'GET':
27
+ return (this.select(
28
+ this.config.restModel.TABLE_NAME,
29
+ undefined,
30
+ this.request
31
+ ) as Promise<any>).then(rows => ({rest: rows})) as any;
32
+ case 'POST':
33
+ return this.insert(this.config.restModel.TABLE_NAME, this.request) as any;
34
+ case 'PUT':
35
+ return this.update(this.config.restModel.TABLE_NAME, undefined, this.request) as any;
36
+ case 'DELETE':
37
+ return this.delete(this.config.restModel.TABLE_NAME, undefined, this.request) as any;
38
+ }
39
+ }
40
+
41
+ private async withConnection<T>(cb: (conn: PoolConnection) => Promise<T>): Promise<T> {
42
+ const conn = await this.config.mysqlPool!.getConnection();
43
+ try {
44
+ return await cb(conn);
45
+ } finally {
46
+ conn.release();
47
+ }
48
+ }
49
+
50
+ async select<RestShortTableNames>(table: RestShortTableNames, primary: string | undefined, args: any) {
51
+ const sql = buildSelectQuery<RestShortTableNames>(table, primary, args);
52
+ return await this.withConnection(async (conn) => {
53
+ console.log(sql)
54
+ const [rows] = await conn.query<RowDataPacket[]>(sql);
55
+ return rows;
56
+ });
57
+ }
58
+
59
+ async insert<RestShortTableNames>(table: RestShortTableNames, data: any) {
60
+ const keys = Object.keys(data);
61
+ const values = keys.map(k => data[k]);
62
+ const placeholders = keys.map(() => '?').join(', ');
63
+ const sql = `INSERT INTO \`${table}\` (${keys.join(', ')}) VALUES (${placeholders})`;
64
+ return await this.withConnection(async (conn) => {
65
+ const [result] = await conn.execute(sql, values);
66
+ return result;
67
+ });
68
+ }
69
+
70
+ async update<RestShortTableNames>(table: RestShortTableNames, primary: string | undefined, data: any) {
71
+ if (!primary) {
72
+ throw new Error('Primary key is required for update');
73
+ }
74
+
75
+ const keys = Object.keys(data);
76
+ const values = keys.map(k => data[k]);
77
+ const updates = keys.map(k => `\`${k}\` = ?`).join(', ');
78
+ const sql = `UPDATE \`${table}\` SET ${updates} WHERE \`${primary}\` = ?`;
79
+ values.push(data[primary]);
80
+
81
+ return await this.withConnection(async (conn) => {
82
+ const [result] = await conn.execute(sql, values);
83
+ return result;
84
+ });
85
+ }
86
+
87
+ async delete<RestShortTableNames>(table: RestShortTableNames, primary: string | undefined, args: any) {
88
+ if (!primary || !args?.[primary]) {
89
+ throw new Error('Primary key and value required for delete');
90
+ }
91
+
92
+ const sql = `DELETE FROM \`${table}\` WHERE \`${primary}\` = ?`;
93
+ return await this.withConnection(async (conn) => {
94
+ const [result] = await conn.execute(sql, [args[primary]]);
95
+ return result;
96
+ });
97
+ }
98
+
99
+ }
100
+
101
+
102
+
103
+
104
+