@carbonorm/carbonnode 2.0.34 → 3.0.0

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 (77) hide show
  1. package/dist/api/carbonSqlExecutor.d.ts +17 -0
  2. package/dist/api/interfaces/ormInterfaces.d.ts +22 -1
  3. package/dist/api/rest/Blog_Categories.d.ts +37 -0
  4. package/dist/api/rest/Blog_Categories.test.d.ts +11 -0
  5. package/dist/api/rest/Blog_Images.d.ts +37 -0
  6. package/dist/api/rest/Blog_Images.test.d.ts +15 -0
  7. package/dist/api/rest/Blog_Post_Categories.d.ts +37 -0
  8. package/dist/api/rest/Blog_Post_Categories.test.d.ts +13 -0
  9. package/dist/api/rest/Blog_Post_Tags.d.ts +37 -0
  10. package/dist/api/rest/Blog_Post_Tags.test.d.ts +13 -0
  11. package/dist/api/rest/Blog_Posts.d.ts +37 -0
  12. package/dist/api/rest/Blog_Posts.test.d.ts +21 -0
  13. package/dist/api/rest/Blog_Tags.d.ts +37 -0
  14. package/dist/api/rest/Blog_Tags.test.d.ts +11 -0
  15. package/dist/api/rest/C6.d.ts +1000 -0
  16. package/dist/api/rest/Cache.d.ts +37 -0
  17. package/dist/api/rest/Cache.test.d.ts +12 -0
  18. package/dist/api/rest/Cities.d.ts +37 -0
  19. package/dist/api/rest/Cities.test.d.ts +17 -0
  20. package/dist/api/rest/Counties.d.ts +37 -0
  21. package/dist/api/rest/Counties.test.d.ts +18 -0
  22. package/dist/api/rest/Countries.d.ts +37 -0
  23. package/dist/api/rest/Countries.test.d.ts +24 -0
  24. package/dist/api/rest/Geometries.d.ts +37 -0
  25. package/dist/api/rest/Geometries.test.d.ts +16 -0
  26. package/dist/api/rest/Images.d.ts +37 -0
  27. package/dist/api/rest/Images.test.d.ts +16 -0
  28. package/dist/api/rest/Land_Section_Info.d.ts +37 -0
  29. package/dist/api/rest/Land_Section_Info.test.d.ts +15 -0
  30. package/dist/api/rest/Neighborhoods.d.ts +37 -0
  31. package/dist/api/rest/Neighborhoods.test.d.ts +12 -0
  32. package/dist/api/rest/Parcel_Building_Details.d.ts +37 -0
  33. package/dist/api/rest/Parcel_Building_Details.test.d.ts +40 -0
  34. package/dist/api/rest/Parcel_Neighborhoods.d.ts +37 -0
  35. package/dist/api/rest/Parcel_Neighborhoods.test.d.ts +15 -0
  36. package/dist/api/rest/Parcel_Owners.d.ts +37 -0
  37. package/dist/api/rest/Parcel_Owners.test.d.ts +23 -0
  38. package/dist/api/rest/Parcel_Sales.d.ts +37 -0
  39. package/dist/api/rest/Parcel_Sales.test.d.ts +19 -0
  40. package/dist/api/rest/Parcel_Tax_History.d.ts +37 -0
  41. package/dist/api/rest/Parcel_Tax_History.test.d.ts +24 -0
  42. package/dist/api/rest/Parcels.d.ts +37 -0
  43. package/dist/api/rest/Parcels.test.d.ts +42 -0
  44. package/dist/api/rest/Payment_Charge_Logs.d.ts +37 -0
  45. package/dist/api/rest/Payment_Charge_Logs.test.d.ts +17 -0
  46. package/dist/api/rest/Payment_Subscriptions.d.ts +37 -0
  47. package/dist/api/rest/Payment_Subscriptions.test.d.ts +20 -0
  48. package/dist/api/rest/Property_Units.d.ts +37 -0
  49. package/dist/api/rest/Property_Units.test.d.ts +55 -0
  50. package/dist/api/rest/Sources.d.ts +37 -0
  51. package/dist/api/rest/Sources.test.d.ts +17 -0
  52. package/dist/api/rest/States.d.ts +37 -0
  53. package/dist/api/rest/States.test.d.ts +20 -0
  54. package/dist/api/rest/Tax_Districts.d.ts +37 -0
  55. package/dist/api/rest/Tax_Districts.test.d.ts +12 -0
  56. package/dist/api/rest/Users.d.ts +37 -0
  57. package/dist/api/rest/Users.test.d.ts +14 -0
  58. package/dist/api/rest/Valuation_Reports.d.ts +37 -0
  59. package/dist/api/rest/Valuation_Reports.test.d.ts +36 -0
  60. package/dist/api/rest/Zip_Codes.d.ts +37 -0
  61. package/dist/api/rest/Zip_Codes.test.d.ts +15 -0
  62. package/dist/api/restRequest.d.ts +39 -16
  63. package/dist/index.cjs.js +881 -454
  64. package/dist/index.cjs.js.map +1 -1
  65. package/dist/index.d.ts +3 -0
  66. package/dist/index.esm.js +881 -456
  67. package/dist/index.esm.js.map +1 -1
  68. package/dist/variables/isNode.d.ts +2 -0
  69. package/package.json +14 -4
  70. package/scripts/assets/handlebars/C6.ts.handlebars +5 -1
  71. package/scripts/generateRestBindings.cjs +89 -23
  72. package/scripts/generateRestBindings.ts +100 -27
  73. package/src/api/carbonSqlExecutor.ts +279 -0
  74. package/src/api/interfaces/ormInterfaces.ts +9 -1
  75. package/src/api/restRequest.ts +164 -19
  76. package/src/index.ts +3 -0
  77. package/src/variables/isNode.ts +3 -0
@@ -2,8 +2,10 @@ import axiosInstance from "api/axiosInstance";
2
2
  import convertForRequestBody from "api/convertForRequestBody";
3
3
  import {iC6RestfulModel, iConstraint, iDynamicApiImport} from "api/interfaces/ormInterfaces";
4
4
  import {AxiosInstance, AxiosPromise, AxiosResponse} from "axios";
5
+ import {Pool} from "mysql2/promise";
5
6
 
6
7
  import {toast} from "react-toastify";
8
+ import isNode from 'variables/isNode'; // simple check: `typeof window==='undefined'`
7
9
  import isLocal from "variables/isLocal";
8
10
  import isTest from "variables/isTest";
9
11
  import isVerbose from "variables/isVerbose";
@@ -267,23 +269,116 @@ export const DELETE = 'DELETE';
267
269
 
268
270
  export type iRestMethods = 'GET' | 'POST' | 'PUT' | 'DELETE';
269
271
 
272
+ // ========================
273
+ // 🔧 SQL Operator & Helpers
274
+ // ========================
275
+
276
+ export type SQLFunction =
277
+ | 'COUNT'
278
+ | 'GROUP_CONCAT'
279
+ | 'MAX'
280
+ | 'MIN'
281
+ | 'SUM'
282
+ | 'DISTINCT';
283
+
284
+ export type SQLComparisonOperator =
285
+ | '='
286
+ | '!='
287
+ | '<'
288
+ | '<='
289
+ | '>'
290
+ | '>='
291
+ | 'IN'
292
+ | 'NOT IN'
293
+ | 'LIKE'
294
+ | 'IS NULL'
295
+ | 'IS NOT NULL'
296
+ | 'BETWEEN'
297
+ | 'LESS_THAN'
298
+ | 'GREATER_THAN';
299
+
300
+ export type JoinType = 'INNER' | 'LEFT_OUTER' | 'RIGHT_OUTER';
301
+
302
+ export type OrderDirection = 'ASC' | 'DESC';
303
+
304
+
305
+ // ========================
306
+ // 📦 SELECT
307
+ // ========================
308
+
309
+ export type SubSelect<T = any> = {
310
+ subSelect: true;
311
+ table: string; // could be enum’d to known table names
312
+ args: RequestGetPutDeleteBody<T>;
313
+ alias: string;
314
+ };
315
+
316
+ export type SelectField<T = any> =
317
+ | keyof T
318
+ | [keyof T, 'AS', string]
319
+ | [SQLFunction, keyof T]
320
+ | [SQLFunction, keyof T, string] // With alias
321
+ | SubSelect<T>; // Fully nested sub-select
322
+
323
+
324
+ // ========================
325
+ // 🧠 WHERE (Recursive)
326
+ // ========================
327
+
328
+ export type WhereClause<T = any> =
329
+ | Partial<T>
330
+ | LogicalGroup<T>
331
+ | ComparisonClause<T>;
332
+
333
+ export type LogicalGroup<T = any> = {
334
+ [logicalGroup: string]: Array<WhereClause<T>>;
335
+ };
336
+
337
+ export type ComparisonClause<T = any> = [keyof T, SQLComparisonOperator, any];
338
+
339
+
340
+ // ========================
341
+ // 🔗 JOIN
342
+ // ========================
343
+
344
+ export type JoinTableCondition<T = any> =
345
+ | Partial<T>
346
+ | WhereClause<T>[]
347
+ | ComparisonClause<T>[];
348
+
349
+ export type JoinClause<T = any> = {
350
+ [table: string]: JoinTableCondition<T>;
351
+ };
352
+
353
+ export type Join<T = any> = {
354
+ [K in JoinType]?: JoinClause<T>;
355
+ };
356
+
357
+
358
+ // ========================
359
+ // 📄 PAGINATION
360
+ // ========================
361
+
362
+ export type Pagination<T = any> = {
363
+ PAGE?: number;
364
+ LIMIT?: number | null;
365
+ ORDER?: Partial<Record<keyof T, OrderDirection>>;
366
+ };
367
+
368
+
369
+ // ========================
370
+ // 🌐 MAIN API TYPE
371
+ // ========================
372
+
373
+ export type RequestGetPutDeleteBody<T = any> = {
374
+ SELECT?: SelectField<T>[];
375
+ UPDATE?: Partial<T>;
376
+ DELETE?: boolean;
377
+ WHERE?: WhereClause<T>;
378
+ JOIN?: Join<T>;
379
+ PAGINATION?: Pagination<T>;
380
+ };
270
381
 
271
- //wip
272
- export type RequestGetPutDeleteBody = {
273
- SELECT?: any,
274
- UPDATE?: any,
275
- DELETE?: any,
276
- WHERE?: any,
277
- JOIN?: {
278
- LEFT?: any,
279
- RIGHT?: any,
280
- INNER?: any,
281
- },
282
- PAGINATION?: {
283
- PAGE?: number,
284
- LIMIT?: number,
285
- }
286
- }
287
382
 
288
383
  export type RequestQueryBody<RestTableInterfaces extends { [key: string]: any }> =
289
384
  iAPI<RestTableInterfaces>
@@ -350,6 +445,7 @@ interface iRest<CustomAndRequiredFields extends { [key: string]: any }, RestTabl
350
445
  C6: iC6Object,
351
446
  axios?: AxiosInstance,
352
447
  restURL?: string,
448
+ mysqlPool?: Pool;
353
449
  withCredentials?: boolean,
354
450
  tableName: RestShortTableNames | RestShortTableNames[],
355
451
  requestMethod: iRestMethods,
@@ -389,6 +485,7 @@ export default function restRequest<
389
485
  C6,
390
486
  axios = axiosInstance,
391
487
  restURL = '/rest/',
488
+ mysqlPool,
392
489
  withCredentials = true,
393
490
  tableName,
394
491
  requestMethod = GET,
@@ -423,7 +520,7 @@ export default function restRequest<
423
520
 
424
521
  }
425
522
 
426
- return (request: iAPI<Modify<RestTableInterfaces, RequestTableOverrides>> & CustomAndRequiredFields = {} as iAPI<Modify<RestTableInterfaces, RequestTableOverrides>> & CustomAndRequiredFields) => {
523
+ return async (request: iAPI<Modify<RestTableInterfaces, RequestTableOverrides>> & CustomAndRequiredFields = {} as iAPI<Modify<RestTableInterfaces, RequestTableOverrides>> & CustomAndRequiredFields) => {
427
524
 
428
525
  console.groupCollapsed('%c API: (' + requestMethod + ') Request for (' + operatingTable + ')', 'color: #0c0')
429
526
 
@@ -480,7 +577,7 @@ export default function restRequest<
480
577
  }
481
578
 
482
579
  // this could return itself with a new page number, or undefined if the end is reached
483
- function apiRequest(): apiReturn<ResponseDataType> {
580
+ async function apiRequest(): Promise<apiReturn<ResponseDataType>> {
484
581
 
485
582
  request.cacheResults ??= (C6.GET === requestMethod)
486
583
 
@@ -663,6 +760,54 @@ export default function restRequest<
663
760
 
664
761
  }
665
762
 
763
+ // this is node check is for react bundlers to skip this import all together
764
+ if (isNode && mysqlPool) {
765
+ const { CarbonSqlExecutor } = await import('./carbonSqlExecutor');
766
+ const engine = new CarbonSqlExecutor(mysqlPool, C6);
767
+ switch (requestMethod) {
768
+ case GET:
769
+ return engine
770
+ .select(tableName, undefined, request)
771
+ .then((rows) => {
772
+ // mirror the front‐end shape
773
+ const serverResponse: iGetC6RestResponse<typeof rows> = {
774
+ rest: rows,
775
+ session: undefined,
776
+ sql: true,
777
+ };
778
+ return serverResponse;
779
+ }) as apiReturn<ResponseDataType>;
780
+
781
+ case POST:
782
+ return engine
783
+ .insert(tableName, request)
784
+ .then((created) => ({
785
+ rest: created,
786
+ })) as apiReturn<ResponseDataType>;
787
+
788
+ case PUT:
789
+ return engine
790
+ .update(tableName, undefined, request)
791
+ .then((updatedResult) => ({
792
+ rest: updatedResult,
793
+ rowCount: (updatedResult as any).affectedRows ?? 0,
794
+ })) as apiReturn<ResponseDataType>;
795
+
796
+ case DELETE:
797
+ return engine
798
+ .delete(tableName, undefined, request)
799
+ .then((deletedResult) => ({
800
+ rest: deletedResult,
801
+ rowCount: (deletedResult as any).affectedRows ?? 0,
802
+ deleted: true,
803
+ })) as apiReturn<ResponseDataType>;
804
+
805
+ default:
806
+ throw new Error(`Unsupported method: ${requestMethod}`);
807
+ }
808
+ }
809
+
810
+
666
811
  // A part of me exists that wants to remove this, but it's a good feature
667
812
  // this allows developers the ability to cache requests based on primary key
668
813
  // for tables like `photos` this can be a huge performance boost
@@ -1139,7 +1284,7 @@ export default function restRequest<
1139
1284
 
1140
1285
  }
1141
1286
 
1142
- return apiRequest()
1287
+ return await apiRequest()
1143
1288
 
1144
1289
  }
1145
1290
 
package/src/index.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  export * from "./api/C6Constants";
6
6
  export { default as axiosInstance } from "./api/axiosInstance";
7
7
  export * from "./api/axiosInstance";
8
+ export * from "./api/carbonSqlExecutor";
8
9
  export { default as convertForRequestBody } from "./api/convertForRequestBody";
9
10
  export * from "./api/convertForRequestBody";
10
11
  export { default as restRequest } from "./api/restRequest";
@@ -15,6 +16,8 @@ export * from "./api/interfaces/ormInterfaces";
15
16
  export * from "./variables/getEnvVar";
16
17
  export { default as isLocal } from "./variables/isLocal";
17
18
  export * from "./variables/isLocal";
19
+ export { default as isNode } from "./variables/isNode";
20
+ export * from "./variables/isNode";
18
21
  export { default as isTest } from "./variables/isTest";
19
22
  export * from "./variables/isTest";
20
23
  export { default as isVerbose } from "./variables/isVerbose";
@@ -0,0 +1,3 @@
1
+ const isNode = typeof process !== 'undefined' && !!process.versions?.node;
2
+
3
+ export default isNode;