@carbonorm/carbonnode 3.0.0 → 3.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.
Files changed (41) 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 +18 -0
  4. package/dist/api/executors/HttpExecutor.d.ts +15 -0
  5. package/dist/api/executors/SqlExecutor.d.ts +11 -0
  6. package/dist/api/restRequest.d.ts +5 -164
  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 +223 -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/logger.d.ts +7 -0
  14. package/dist/api/utils/sortAndSerializeQueryObject.d.ts +1 -0
  15. package/dist/api/utils/testHelpers.d.ts +1 -0
  16. package/dist/api/utils/toastNotifier.d.ts +2 -0
  17. package/dist/index.cjs.js +614 -605
  18. package/dist/index.cjs.js.map +1 -1
  19. package/dist/index.d.ts +14 -2
  20. package/dist/index.esm.js +603 -607
  21. package/dist/index.esm.js.map +1 -1
  22. package/package.json +22 -6
  23. package/src/api/builders/sqlBuilder.ts +173 -0
  24. package/src/api/convertForRequestBody.ts +1 -2
  25. package/src/api/executors/Executor.ts +26 -0
  26. package/src/api/executors/HttpExecutor.ts +790 -0
  27. package/src/api/executors/SqlExecutor.ts +90 -0
  28. package/src/api/restRequest.ts +20 -1273
  29. package/src/api/types/dynamicFetching.ts +10 -0
  30. package/src/api/types/modifyTypes.ts +25 -0
  31. package/src/api/types/mysqlTypes.ts +33 -0
  32. package/src/api/types/ormInterfaces.ts +287 -0
  33. package/src/api/utils/apiHelpers.ts +83 -0
  34. package/src/api/utils/cacheManager.ts +67 -0
  35. package/src/api/utils/logger.ts +24 -0
  36. package/src/api/utils/sortAndSerializeQueryObject.ts +12 -0
  37. package/src/api/utils/testHelpers.ts +24 -0
  38. package/src/api/utils/toastNotifier.ts +11 -0
  39. package/src/index.ts +14 -2
  40. package/src/api/carbonSqlExecutor.ts +0 -279
  41. package/src/api/interfaces/ormInterfaces.ts +0 -87
@@ -0,0 +1,90 @@
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<CustomAndRequiredFields extends object, RestTableInterfaces extends object, RequestTableOverrides extends object, ResponseDataType, RestShortTableNames extends string>
8
+ extends Executor<CustomAndRequiredFields, RestTableInterfaces, RequestTableOverrides, ResponseDataType, RestShortTableNames> {
9
+
10
+ public execute(): Promise<apiReturn<ResponseDataType>> {
11
+ switch (this.config.requestMethod) {
12
+ case 'GET':
13
+ return (this.select(
14
+ this.config.tableName as RestShortTableNames,
15
+ undefined,
16
+ this.request
17
+ ) as Promise<any>).then(rows => ({rest: rows})) as any;
18
+ case 'POST':
19
+ return this.insert(this.config.tableName as RestShortTableNames, this.request) as any;
20
+ case 'PUT':
21
+ return this.update(this.config.tableName as RestShortTableNames, undefined, this.request) as any;
22
+ case 'DELETE':
23
+ return this.delete(this.config.tableName as RestShortTableNames, undefined, this.request) as any;
24
+ }
25
+ }
26
+
27
+ private async withConnection<T>(cb: (conn: PoolConnection) => Promise<T>): Promise<T> {
28
+ const conn = await this.config.mysqlPool!.getConnection();
29
+ try {
30
+ return await cb(conn);
31
+ } finally {
32
+ conn.release();
33
+ }
34
+ }
35
+
36
+ async select<RestShortTableNames>(table: RestShortTableNames, primary: string | undefined, args: any) {
37
+ const sql = buildSelectQuery<RestShortTableNames>(table, primary, args);
38
+ return await this.withConnection(async (conn) => {
39
+ console.log(sql)
40
+ const [rows] = await conn.query<RowDataPacket[]>(sql);
41
+ return rows;
42
+ });
43
+ }
44
+
45
+ async insert<RestShortTableNames>(table: RestShortTableNames, data: any) {
46
+ const keys = Object.keys(data);
47
+ const values = keys.map(k => data[k]);
48
+ const placeholders = keys.map(() => '?').join(', ');
49
+ const sql = `INSERT INTO \`${table}\` (${keys.join(', ')}) VALUES (${placeholders})`;
50
+ return await this.withConnection(async (conn) => {
51
+ const [result] = await conn.execute(sql, values);
52
+ return result;
53
+ });
54
+ }
55
+
56
+ async update<RestShortTableNames>(table: RestShortTableNames, primary: string | undefined, data: any) {
57
+ if (!primary) {
58
+ throw new Error('Primary key is required for update');
59
+ }
60
+
61
+ const keys = Object.keys(data);
62
+ const values = keys.map(k => data[k]);
63
+ const updates = keys.map(k => `\`${k}\` = ?`).join(', ');
64
+ const sql = `UPDATE \`${table}\` SET ${updates} WHERE \`${primary}\` = ?`;
65
+ values.push(data[primary]);
66
+
67
+ return await this.withConnection(async (conn) => {
68
+ const [result] = await conn.execute(sql, values);
69
+ return result;
70
+ });
71
+ }
72
+
73
+ async delete<RestShortTableNames>(table: RestShortTableNames, primary: string | undefined, args: any) {
74
+ if (!primary || !args?.[primary]) {
75
+ throw new Error('Primary key and value required for delete');
76
+ }
77
+
78
+ const sql = `DELETE FROM \`${table}\` WHERE \`${primary}\` = ?`;
79
+ return await this.withConnection(async (conn) => {
80
+ const [result] = await conn.execute(sql, [args[primary]]);
81
+ return result;
82
+ });
83
+ }
84
+
85
+ }
86
+
87
+
88
+
89
+
90
+