@carbonorm/carbonnode 3.0.2 → 3.0.3

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,61 @@
1
+ import restRequest from "./restRequest";
2
+ import {iRest} from "./types/ormInterfaces";
3
+
4
+ export function restOrm<
5
+ RestShortTableName extends string = any,
6
+ RestTableInterface extends { [key: string]: any } = any,
7
+ PrimaryKey extends Extract<keyof RestTableInterface, string> = Extract<keyof RestTableInterface, string>,
8
+ CustomAndRequiredFields extends { [key: string]: any } = any,
9
+ RequestTableOverrides extends { [key in keyof RestTableInterface]: any } = { [key in keyof RestTableInterface]: any }
10
+ >(config: Omit<iRest<
11
+ RestShortTableName,
12
+ RestTableInterface,
13
+ PrimaryKey
14
+ >, "requestMethod">) {
15
+ return {
16
+ Get: restRequest<
17
+ "GET",
18
+ RestShortTableName,
19
+ RestTableInterface,
20
+ PrimaryKey,
21
+ CustomAndRequiredFields,
22
+ RequestTableOverrides
23
+ >({
24
+ ...config,
25
+ requestMethod: "GET",
26
+ }),
27
+ Put: restRequest<
28
+ "PUT",
29
+ RestShortTableName,
30
+ RestTableInterface,
31
+ PrimaryKey,
32
+ CustomAndRequiredFields,
33
+ RequestTableOverrides
34
+ >({
35
+ ...config,
36
+ requestMethod: "PUT",
37
+ }),
38
+ Post: restRequest<
39
+ "POST",
40
+ RestShortTableName,
41
+ RestTableInterface,
42
+ PrimaryKey,
43
+ CustomAndRequiredFields,
44
+ RequestTableOverrides
45
+ >({
46
+ ...config,
47
+ requestMethod: "POST",
48
+ }),
49
+ Delete: restRequest<
50
+ "DELETE",
51
+ RestShortTableName,
52
+ RestTableInterface,
53
+ PrimaryKey,
54
+ CustomAndRequiredFields,
55
+ RequestTableOverrides
56
+ >({
57
+ ...config,
58
+ requestMethod: "DELETE",
59
+ }),
60
+ }
61
+ }
@@ -1,41 +1,42 @@
1
1
  import isNode from '../variables/isNode';
2
2
  import {Modify} from "./types/modifyTypes";
3
- import {apiReturn, iAPI, iRest} from "./types/ormInterfaces";
3
+ import {
4
+ apiReturn, DetermineResponseDataType,
5
+ iAPI,
6
+ iRest, iRestMethods
7
+ } from "./types/ormInterfaces";
4
8
 
5
9
  /**
6
10
  * Facade: routes API calls to SQL or HTTP executors based on runtime context.
7
11
  */
8
12
  export default function restRequest<
13
+ RequestMethod extends iRestMethods,
9
14
  RestShortTableName extends string = any,
10
15
  RestTableInterface extends { [key: string]: any } = any,
11
16
  PrimaryKey extends Extract<keyof RestTableInterface, string> = Extract<keyof RestTableInterface, string>,
12
17
  CustomAndRequiredFields extends { [key: string]: any } = any,
13
- RequestTableOverrides extends { [key: string]: any } = { [key in keyof RestTableInterface]: any },
14
- ResponseDataType = any
18
+ RequestTableOverrides extends { [key in keyof RestTableInterface]: any } = { [key in keyof RestTableInterface]: any }
15
19
  >(
16
20
  config: iRest<
17
21
  RestShortTableName,
18
22
  RestTableInterface,
19
- PrimaryKey,
20
- CustomAndRequiredFields,
21
- RequestTableOverrides,
22
- ResponseDataType
23
+ PrimaryKey
23
24
  >
24
25
  ) {
25
26
  return async (
26
27
  request: iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields = {} as iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields
27
- ): Promise<apiReturn<ResponseDataType>> => {
28
+ ): Promise<apiReturn<DetermineResponseDataType<RequestMethod, RestTableInterface>>> => {
28
29
 
29
30
  // SQL path if on Node with a provided pool
30
31
  if (isNode && config.mysqlPool) {
31
32
  const {SqlExecutor} = await import('./executors/SqlExecutor');
32
33
  const executor = new SqlExecutor<
34
+ RequestMethod,
33
35
  RestShortTableName,
34
36
  RestTableInterface,
35
37
  PrimaryKey,
36
38
  CustomAndRequiredFields,
37
- RequestTableOverrides,
38
- ResponseDataType
39
+ RequestTableOverrides
39
40
  >(config, request);
40
41
  return executor.execute();
41
42
  }
@@ -43,13 +44,14 @@ export default function restRequest<
43
44
  // HTTP path fallback
44
45
  const {HttpExecutor} = await import('./executors/HttpExecutor');
45
46
  const http = new HttpExecutor<
47
+ RequestMethod,
46
48
  RestShortTableName,
47
49
  RestTableInterface,
48
50
  PrimaryKey,
49
51
  CustomAndRequiredFields,
50
- RequestTableOverrides,
51
- ResponseDataType
52
+ RequestTableOverrides
52
53
  >(config, request);
53
54
  return http.execute();
54
55
  };
55
56
  }
57
+
@@ -3,6 +3,7 @@ import {Pool} from "mysql2/promise";
3
3
  import {eFetchDependencies} from "./dynamicFetching";
4
4
  import {Modify} from "./modifyTypes";
5
5
  import {JoinType, OrderDirection, SQLComparisonOperator, SQLFunction} from "./mysqlTypes";
6
+ import {CarbonReact} from "@carbonorm/carbonreact";
6
7
 
7
8
  export interface stringMap {
8
9
  [key: string]: string;
@@ -27,13 +28,80 @@ export interface iTypeValidation {
27
28
  SKIP_COLUMN_IN_POST: boolean
28
29
  }
29
30
 
30
- export type iRestReactiveLifecycle<T extends RequestGetPutDeleteBody> = {
31
- beforeProcessing?: (args: { request: T[]; requestMeta?: any }) => void | Promise<void>;
32
- beforeExecution?: (args: { request: T[]; requestMeta?: any }) => void | Promise<void>;
33
- afterExecution?: (args: { response: T[]; request: T[]; responseMeta?: any }) => void | Promise<void>;
34
- afterCommit?: (args: { response: T[]; request: T[]; responseMeta?: any }) => void | Promise<void>;
31
+
32
+ // config, request, response
33
+ export type iRestReactiveLifecycle<
34
+ RequestMethod extends iRestMethods,
35
+ RestShortTableName extends string = any,
36
+ RestTableInterface extends { [key: string]: any } = any,
37
+ PrimaryKey extends keyof RestTableInterface & string = any,
38
+ // TODO - do we ever use these last two ever? should we make usage just any?
39
+ CustomAndRequiredFields extends { [key: string]: any } = any,
40
+ RequestTableOverrides extends { [key: string]: any } = { [key in keyof RestTableInterface]: any }
41
+ > = {
42
+ beforeProcessing?: {
43
+ [key: string]: (args: {
44
+ config: iRest<
45
+ RestShortTableName,
46
+ RestTableInterface,
47
+ PrimaryKey
48
+ >,
49
+ request: iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields;
50
+ }) => void | Promise<void>
51
+ },
52
+ beforeExecution?:
53
+ {
54
+ [key: string]: (args: {
55
+ config: iRest<
56
+ RestShortTableName,
57
+ RestTableInterface,
58
+ PrimaryKey
59
+ >,
60
+ request: iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields
61
+ }) => void | Promise<void>
62
+ };
63
+ afterExecution?:
64
+ {
65
+ [key: string]: (args: {
66
+ config: iRest<
67
+ RestShortTableName,
68
+ RestTableInterface,
69
+ PrimaryKey
70
+ >,
71
+ request: iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields;
72
+ response: AxiosResponse<DetermineResponseDataType<RequestMethod, RestTableInterface>>;
73
+ }) => void | Promise<void>
74
+ };
75
+ afterCommit?: {
76
+ [key: string]: (args: {
77
+ config: iRest<
78
+ RestShortTableName,
79
+ RestTableInterface,
80
+ PrimaryKey
81
+ >,
82
+ request: iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields;
83
+ response: AxiosResponse<DetermineResponseDataType<RequestMethod, RestTableInterface>>;
84
+ }) => void | Promise<void>
85
+ };
35
86
  };
36
87
 
88
+ export type iRestHooks<
89
+ RestShortTableName extends string = any,
90
+ RestTableInterface extends { [key: string]: any } = any,
91
+ PrimaryKey extends keyof RestTableInterface & string = any,
92
+ CustomAndRequiredFields extends { [key: string]: any } = any,
93
+ RequestTableOverrides extends { [key: string]: any } = { [key in keyof RestTableInterface]: any }
94
+ > = {
95
+ [Method in iRestMethods]: iRestReactiveLifecycle<
96
+ Method,
97
+ RestShortTableName,
98
+ RestTableInterface,
99
+ PrimaryKey,
100
+ CustomAndRequiredFields,
101
+ RequestTableOverrides
102
+ >
103
+ }
104
+
37
105
  export interface iConstraint {
38
106
  TABLE: string,
39
107
  COLUMN: string,
@@ -64,7 +132,8 @@ export interface iC6RestfulModel<
64
132
  COLUMNS: tColumns<RestShortTableNames, RestTableInterfaces>;
65
133
  TYPE_VALIDATION: { [key: string]: iTypeValidation };
66
134
  REGEX_VALIDATION: RegExpMap;
67
- LIFECYCLE_HOOKS: iRestReactiveLifecycle<RequestGetPutDeleteBody>[];
135
+ // TODO - I thon think theres a good way to infer the last two generics (overides)
136
+ LIFECYCLE_HOOKS: iRestHooks<RestShortTableNames, RestTableInterfaces, PK>;
68
137
  TABLE_REFERENCES: { [columnName: string]: iConstraint[] };
69
138
  TABLE_REFERENCED_BY: { [columnName: string]: iConstraint[] };
70
139
  }
@@ -278,13 +347,23 @@ export type apiReturn<Response> =
278
347
  | (Response extends iPutC6RestResponse | iDeleteC6RestResponse | iPostC6RestResponse ? null : (() => apiReturn<Response>))
279
348
 
280
349
 
350
+ export type DetermineResponseDataType<
351
+ RequestMethod extends iRestMethods,
352
+ RestTableInterface extends { [key: string]: any }
353
+ > = RequestMethod extends "POST"
354
+ ? iPostC6RestResponse<RestTableInterface>
355
+ : RequestMethod extends "GET"
356
+ ? iGetC6RestResponse<RestTableInterface>
357
+ : RequestMethod extends "PUT"
358
+ ? iPutC6RestResponse<RestTableInterface>
359
+ : RequestMethod extends "DELETE"
360
+ ? iDeleteC6RestResponse<RestTableInterface>
361
+ : any;
362
+
281
363
  export interface iRest<
282
364
  RestShortTableName extends string = any,
283
365
  RestTableInterface extends { [key: string]: any } = any,
284
- PrimaryKey extends Extract<keyof RestTableInterface, string> = Extract<keyof RestTableInterface, string>,
285
- CustomAndRequiredFields extends { [key: string]: any } = any,
286
- RequestTableOverrides = { [key in keyof RestTableInterface]: any },
287
- ResponseDataType = any
366
+ PrimaryKey extends keyof RestTableInterface & string = any
288
367
  > {
289
368
  C6: iC6Object,
290
369
  axios?: AxiosInstance,
@@ -292,18 +371,22 @@ export interface iRest<
292
371
  mysqlPool?: Pool;
293
372
  withCredentials?: boolean,
294
373
  restModel: iC6RestfulModel<RestShortTableName, RestTableInterface, PrimaryKey>,
374
+ reactBootstrap?: CarbonReact<any, any>,
295
375
  requestMethod: iRestMethods,
296
376
  clearCache?: () => void,
297
377
  skipPrimaryCheck?: boolean,
298
- queryCallback: RequestQueryBody<Modify<RestTableInterface, RequestTableOverrides>> | ((request: iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields) => (null | undefined | RequestQueryBody<Modify<RestTableInterface, RequestTableOverrides>>)),
299
- responseCallback?: (response: AxiosResponse<ResponseDataType>,
300
- request: iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields,
301
- success: (ResponseDataType extends iPutC6RestResponse | iDeleteC6RestResponse
302
- ? RequestQueryBody<Modify<RestTableInterface, RequestTableOverrides>>
303
- : string)
304
- | RestTableInterface[PrimaryKey] // Rest PK
305
- | string | number | boolean // Toast and validations
306
- ) => any // keep this set to any, it allows easy arrow functions and the results unused here
378
+ /* queryCallback: RequestQueryBody<Modify<RestTableInterface, RequestTableOverrides>> | ((request: iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields) => (null | undefined | RequestQueryBody<Modify<RestTableInterface, RequestTableOverrides>>)),
379
+ responseCallback?: (
380
+ response: AxiosResponse<DetermineResponseDataType<RequestMethod, RestTableInterface>>,
381
+ request: iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields,
382
+ success: (
383
+ // TODO - make sure this makes sense, or re-implement with the hooks model
384
+ DetermineResponseDataType<RequestMethod, RestTableInterface> extends iPutC6RestResponse | iDeleteC6RestResponse
385
+ ? RequestQueryBody<Modify<RestTableInterface, RequestTableOverrides>>
386
+ : string)
387
+ | RestTableInterface[PrimaryKey] // Rest PK
388
+ | string | number | boolean // Toast and validations
389
+ ) => (RestTableInterface | RestTableInterface[] | void) // keep this set to any, it allows easy arrow functions and the results unused here*/
307
390
  }
308
391
 
309
392
 
@@ -5,6 +5,10 @@ import { AxiosResponse } from "axios";
5
5
  import {toastOptions} from "../../variables/toastOptions";
6
6
  import {iC6RestfulModel} from "../types/ormInterfaces";
7
7
 
8
+
9
+
10
+
11
+
8
12
  export function TestRestfulResponse(response: AxiosResponse | any, success: ((r: AxiosResponse) => (string | void)) | string | undefined, error: ((r: AxiosResponse) => (string | void)) | string | undefined): string | boolean | number {
9
13
 
10
14
  if (undefined === response.data?.['ERROR TYPE']
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@ export { default as axiosInstance } from "./api/axiosInstance";
7
7
  export * from "./api/axiosInstance";
8
8
  export { default as convertForRequestBody } from "./api/convertForRequestBody";
9
9
  export * from "./api/convertForRequestBody";
10
+ export * from "./api/restOrm";
10
11
  export { default as restRequest } from "./api/restRequest";
11
12
  export * from "./api/restRequest";
12
13
  export { default as timeout } from "./api/timeout";
@@ -1,126 +0,0 @@
1
- import {xdescribe, expect, test} from '@jest/globals';
2
- import {checkAllRequestsComplete} from "@carbonorm/carbonnode";
3
- import {act, waitFor} from '@testing-library/react';
4
- import {C6, i{{TABLE_NAME_SHORT_PASCAL_CASE}}, {{TABLE_NAME_SHORT}} } from "{{RELATIVE_OUTPUT_DIR}}/C6";
5
- import {{TABLE_NAME_SHORT_PASCAL_CASE}} from "./{{TABLE_NAME_SHORT_PASCAL_CASE}}";{{#if REACT_IMPORT}}
6
- {{{REACT_IMPORT}}};{{/if}}
7
-
8
- const randomString = Math.random().toString(36).substring(7);
9
- const randomInt = Math.floor(Math.random() * 1000000);
10
- const fillString = 'string' + randomString + randomInt;
11
-
12
- console.log('fillString', fillString);
13
-
14
- /**
15
- {{{TABLE_DEFINITION}}}
16
- **/
17
-
18
- const Test_Data: i{{TABLE_NAME_SHORT_PASCAL_CASE}} = {
19
- {{#each TYPE_VALIDATION}}{{#SKIP_COLUMN_IN_POST}}
20
- {{COLUMN_NAME}}: {{#TYPESCRIPT_TYPE_IS_STRING}}fillString{{#MAX_LENGTH}}.substring(0, {{MAX_LENGTH}}){{/MAX_LENGTH}}{{/TYPESCRIPT_TYPE_IS_STRING}}{{#TYPESCRIPT_TYPE_IS_NUMBER}}randomInt{{/TYPESCRIPT_TYPE_IS_NUMBER}},
21
- {{/SKIP_COLUMN_IN_POST}}{{/each}}
22
- }
23
-
24
- export default Test_Data;
25
-
26
- xdescribe('REST {{TABLE_NAME_SHORT_PASCAL_CASE}} api', () => {
27
-
28
- let testData = Test_Data;
29
-
30
- test('GET POST PUT DELETE', async () => {
31
-
32
- await act(async () => {
33
-
34
- let selectAllResponse = await {{TABLE_NAME_SHORT_PASCAL_CASE}}.Get({})
35
-
36
- if ('function' === typeof selectAllResponse) {
37
- throw Error('selectAllResponse is a promise, this typically means this specific get request has already run during test setup.');
38
- }
39
-
40
- // We don't care if it is filled or not, just that the request can be made.
41
- expect(selectAllResponse?.data?.rest).not.toBeUndefined();
42
-
43
- const postResponse = await {{TABLE_NAME_SHORT_PASCAL_CASE}}.Post(testData);
44
-
45
- console.log('postResponse', postResponse?.data)
46
-
47
- expect(postResponse?.data?.created).not.toBeUndefined();
48
-
49
- const primaryKey = {{TABLE_NAME_SHORT}}.PRIMARY_SHORT[0];
50
-
51
- const postID = postResponse?.data?.created
52
-
53
- const singleRowSelect = await {{TABLE_NAME_SHORT_PASCAL_CASE}}.Get({
54
- [C6.WHERE]: {
55
- [{{TABLE_NAME_SHORT}}[primaryKey.toUpperCase()]]: postID,
56
- }
57
- })
58
-
59
- if ('function' === typeof singleRowSelect) {
60
- throw Error('singleRowSelect is a promise, this is unexpected.');
61
- }
62
-
63
- console.log('singleRowSelect', singleRowSelect?.data)
64
-
65
- // Ensure the expected response datastructure is returned
66
- expect(singleRowSelect?.data?.rest).not.toBeUndefined();
67
-
68
- // Make sure the previously created post is now returned
69
- expect(typeof singleRowSelect?.data?.rest).toEqual('object');
70
-
71
- // todo - make this work correctly with multiple primary keys
72
- const selectedPostId = singleRowSelect?.data?.rest[0][primaryKey]
73
-
74
- expect(selectedPostId).toEqual(postID);
75
-
76
- const multipleRowSelect = await {{TABLE_NAME_SHORT_PASCAL_CASE}}.Get({
77
- [C6.WHERE]: {
78
- [{{TABLE_NAME_SHORT}}[primaryKey.toUpperCase()]]: [C6.IN, [0, postID]],
79
- }
80
- })
81
-
82
- if ('function' === typeof multipleRowSelect) {
83
- throw Error('singleRowSelect is a promise, this is unexpected.');
84
- }
85
-
86
- console.log('singleRowSelect', multipleRowSelect?.data)
87
-
88
- // Ensure the expected response datastructure is returned
89
- expect(multipleRowSelect?.data?.rest).not.toBeUndefined();
90
-
91
- // Make sure the previously created post is now returned
92
- expect(typeof multipleRowSelect?.data?.rest).toEqual('object');
93
-
94
- testData[primaryKey] = postID
95
-
96
- {{#each TYPE_VALIDATION}}
97
- testData.{{COLUMN_NAME}} = {{#TYPESCRIPT_TYPE_IS_STRING}}fillString.substring(0, {{MAX_LENGTH}}){{/TYPESCRIPT_TYPE_IS_STRING}}{{#TYPESCRIPT_TYPE_IS_NUMBER}}randomInt{{/TYPESCRIPT_TYPE_IS_NUMBER}};
98
- {{/each}}
99
-
100
- {{#if REACT_IMPORT}}
101
- // wait for the global state to be updated
102
- expect({{{CARBON_REACT_INSTANCE}}}.state.{{TABLE_NAME_SHORT}}).not.toBeUndefined();
103
- {{/if}}
104
-
105
- const updateResponse = await {{TABLE_NAME_SHORT_PASCAL_CASE}}.Put(testData)
106
-
107
- expect(updateResponse?.data?.updated).not.toBeUndefined();
108
-
109
- const deleteResponse = await {{TABLE_NAME_SHORT_PASCAL_CASE}}.Delete({
110
- [primaryKey]: postID
111
- })
112
-
113
- console.log('deleteResponse', deleteResponse?.data)
114
-
115
- expect(deleteResponse?.data?.deleted).not.toBeUndefined();
116
-
117
- await waitFor(async () => {
118
- expect(checkAllRequestsComplete()).toEqual(true);
119
- }, {timeout: 10000, interval: 1000});
120
-
121
- })
122
-
123
- }, 100000);
124
-
125
- })
126
-
@@ -1,193 +0,0 @@
1
- import {
2
- iPostC6RestResponse,
3
- restRequest,
4
- GET,
5
- POST,
6
- PUT,
7
- DELETE,
8
- iDeleteC6RestResponse,
9
- iGetC6RestResponse,
10
- iPutC6RestResponse,{{#REACT_IMPORT}}
11
- removeInvalidKeys,
12
- iAPI,
13
- Modify{{/REACT_IMPORT}}
14
- } from "@carbonorm/carbonnode";{{#REACT_IMPORT}}
15
- import {AxiosResponse} from "axios";{{/REACT_IMPORT}}
16
- import {C6, {{TABLE_NAME_SHORT}}, i{{TABLE_NAME_SHORT_PASCAL_CASE}}, {{TABLE_NAME_SHORT_PASCAL_CASE}}PrimaryKeys} from "./C6";{{#if REACT_IMPORT}}
17
- {{{REACT_IMPORT}}};{{/if}}
18
-
19
- /**
20
- {{{TABLE_DEFINITION}}}
21
- **/
22
-
23
- type GetCustomAndRequiredFields = {}
24
-
25
- type GetRequestTableOverrides = {}
26
-
27
- // required parameters, optional parameters, parameter type overrides, response, and table names
28
- const Get = restRequest<
29
- '{{TABLE_NAME_SHORT}}',
30
- i{{TABLE_NAME_SHORT_PASCAL_CASE}},
31
- {{TABLE_NAME_SHORT_PASCAL_CASE}}PrimaryKeys,
32
- GetCustomAndRequiredFields,
33
- GetRequestTableOverrides,
34
- iGetC6RestResponse<i{{TABLE_NAME_SHORT_PASCAL_CASE}}>
35
- >({
36
- C6: C6,
37
- restURL: {{{REST_URL_EXPRESSION}}},
38
- restModel: {{TABLE_NAME_SHORT}},
39
- requestMethod: GET,
40
- queryCallback: (request) => {
41
- request.success ??= 'Successfully received {{TABLE_NAME_SHORT}}!'
42
- request.error ??= 'An unknown issue occurred creating the {{TABLE_NAME_SHORT}}!'
43
- return request
44
- },{{#if REACT_IMPORT}}
45
- responseCallback: (response, _request) => {
46
- const responseData = response?.data?.rest;
47
- {{CARBON_REACT_INSTANCE}}.updateRestfulObjectArrays<i{{TABLE_NAME_SHORT_PASCAL_CASE}}>({
48
- dataOrCallback: Array.isArray(responseData) ? responseData : [responseData],
49
- stateKey: "{{TABLE_NAME_SHORT}}",
50
- uniqueObjectId: C6.{{TABLE_NAME_SHORT}}.PRIMARY_SHORT as (keyof i{{TABLE_NAME_SHORT_PASCAL_CASE}})[]
51
- })
52
- }{{/if}}
53
- });
54
-
55
- type PutCustomAndRequiredFields = {}
56
-
57
- type PutRequestTableOverrides = {}
58
-
59
- {{#if REACT_IMPORT}}
60
- export function putState(response: AxiosResponse<iPutC6RestResponse<i{{TABLE_NAME_SHORT_PASCAL_CASE}}>>, request: iAPI<Modify<i{{TABLE_NAME_SHORT_PASCAL_CASE}}, PutRequestTableOverrides>> & PutCustomAndRequiredFields) {
61
- {{CARBON_REACT_INSTANCE}}.updateRestfulObjectArrays<i{{TABLE_NAME_SHORT_PASCAL_CASE}}>({
62
- dataOrCallback: [
63
- removeInvalidKeys<i{{TABLE_NAME_SHORT_PASCAL_CASE}}>({
64
- ...request,
65
- ...response?.data?.rest,
66
- }, C6.TABLES)
67
- ],
68
- stateKey: "{{TABLE_NAME_SHORT}}",
69
- uniqueObjectId: {{TABLE_NAME_SHORT}}.PRIMARY_SHORT as (keyof i{{TABLE_NAME_SHORT_PASCAL_CASE}})[]
70
- })
71
- }
72
- {{/if}}
73
-
74
- const Put = restRequest<
75
- '{{TABLE_NAME_SHORT}}',
76
- i{{TABLE_NAME_SHORT_PASCAL_CASE}},
77
- {{TABLE_NAME_SHORT_PASCAL_CASE}}PrimaryKeys,
78
- GetCustomAndRequiredFields,
79
- GetRequestTableOverrides,
80
- iPutC6RestResponse<i{{TABLE_NAME_SHORT_PASCAL_CASE}}>
81
- >({
82
- C6: C6,
83
- restURL: {{{REST_URL_EXPRESSION}}},
84
- restModel: {{TABLE_NAME_SHORT}},
85
- requestMethod: PUT,
86
- queryCallback: (request) => {
87
- request.success ??= 'Successfully updated {{TABLE_NAME_SHORT}} data!'
88
- request.error ??= 'An unknown issue occurred updating the {{TABLE_NAME_SHORT}} data!'
89
- return request
90
- },{{#if REACT_IMPORT}}
91
- responseCallback: putState{{/if}}
92
- });
93
-
94
- type PostCustomAndRequiredFields = {}
95
-
96
- type PostRequestTableOverrides = {}
97
-
98
- {{#if REACT_IMPORT}}export function postState(
99
- response: AxiosResponse<iPostC6RestResponse<i{{TABLE_NAME_SHORT_PASCAL_CASE}}>>,
100
- request: iAPI<Modify<i{{TABLE_NAME_SHORT_PASCAL_CASE}}, PostRequestTableOverrides>> & PostCustomAndRequiredFields,
101
- success: i{{TABLE_NAME_SHORT_PASCAL_CASE}}[{{TABLE_NAME_SHORT_PASCAL_CASE}}PrimaryKeys] | string | number | boolean | undefined
102
- ) {
103
- if ('number' === typeof id || 'string' === typeof success) {
104
- if (1 !== {{TABLE_NAME_SHORT}}.PRIMARY_SHORT.length) {
105
- console.error("C6 received unexpected result's given the primary key length");
106
- } else {
107
- request[{{TABLE_NAME_SHORT}}.PRIMARY_SHORT[0]] = success
108
- }
109
- }
110
- {{CARBON_REACT_INSTANCE}}.updateRestfulObjectArrays<i{{TABLE_NAME_SHORT_PASCAL_CASE}}>({
111
- dataOrCallback: undefined !== request.dataInsertMultipleRows
112
- ? request.dataInsertMultipleRows.map((request, index) => {
113
- return removeInvalidKeys<i{{TABLE_NAME_SHORT_PASCAL_CASE}}>({
114
- ...request,
115
- ...(index === 0 ? response?.data?.rest : {}),
116
- }, C6.TABLES)
117
- })
118
- : [
119
- removeInvalidKeys<i{{TABLE_NAME_SHORT_PASCAL_CASE}}>({
120
- ...request,
121
- ...response?.data?.rest,
122
- }, C6.TABLES)
123
- ],
124
- stateKey: "{{TABLE_NAME_SHORT}}",
125
- uniqueObjectId: {{TABLE_NAME_SHORT}}.PRIMARY_SHORT as (keyof i{{TABLE_NAME_SHORT_PASCAL_CASE}})[]
126
- })
127
- }{{/if}}
128
-
129
- const Post = restRequest<
130
- '{{TABLE_NAME_SHORT}}',
131
- i{{TABLE_NAME_SHORT_PASCAL_CASE}},
132
- {{TABLE_NAME_SHORT_PASCAL_CASE}}PrimaryKeys,
133
- GetCustomAndRequiredFields,
134
- GetRequestTableOverrides,
135
- iPostC6RestResponse<i{{TABLE_NAME_SHORT_PASCAL_CASE}}>
136
- >({
137
- C6: C6,
138
- restURL: {{{REST_URL_EXPRESSION}}},
139
- restModel: {{TABLE_NAME_SHORT}},
140
- requestMethod: POST,
141
- queryCallback: (request) => {
142
- request.success ??= 'Successfully created the {{TABLE_NAME_SHORT}} data!'
143
- request.error ??= 'An unknown issue occurred creating the {{TABLE_NAME_SHORT}} data!'
144
- return request
145
- },{{#if REACT_IMPORT}}
146
- responseCallback: postState{{/if}}
147
- });
148
-
149
- type DeleteCustomAndRequiredFields = {}
150
-
151
- type DeleteRequestTableOverrides = {}
152
-
153
- {{#if REACT_IMPORT}}
154
- export function deleteState(_response: AxiosResponse<iDeleteC6RestResponse<i{{TABLE_NAME_SHORT_PASCAL_CASE}}>>, request: iAPI<Modify<i{{TABLE_NAME_SHORT_PASCAL_CASE}}, DeleteRequestTableOverrides>> & DeleteCustomAndRequiredFields) {
155
- {{CARBON_REACT_INSTANCE}}.deleteRestfulObjectArrays<i{{TABLE_NAME_SHORT_PASCAL_CASE}}>({
156
- dataOrCallback: [
157
- request
158
- ],
159
- stateKey: "{{TABLE_NAME_SHORT}}",
160
- uniqueObjectId: {{TABLE_NAME_SHORT}}.PRIMARY_SHORT as (keyof i{{TABLE_NAME_SHORT_PASCAL_CASE}})[]
161
- })
162
- }
163
- {{/if}}
164
-
165
- const Delete = restRequest<
166
- '{{TABLE_NAME_SHORT}}',
167
- i{{TABLE_NAME_SHORT_PASCAL_CASE}},
168
- {{TABLE_NAME_SHORT_PASCAL_CASE}}PrimaryKeys,
169
- GetCustomAndRequiredFields,
170
- GetRequestTableOverrides,
171
- iDeleteC6RestResponse<i{{TABLE_NAME_SHORT_PASCAL_CASE}}>
172
- >({
173
- C6: C6,
174
- restURL: {{{REST_URL_EXPRESSION}}},
175
- restModel: {{TABLE_NAME_SHORT}},
176
- requestMethod: DELETE,
177
- queryCallback: (request) => {
178
- request.success ??= 'Successfully removed the {{TABLE_NAME_SHORT}} data!'
179
- request.error ??= 'An unknown issue occurred removing the {{TABLE_NAME_SHORT}} data!'
180
- return request
181
- },{{#if REACT_IMPORT}}
182
- responseCallback: deleteState{{/if}}
183
- });
184
-
185
- const {{TABLE_NAME_SHORT_PASCAL_CASE}} = {
186
- // Export all GET, POST, PUT, DELETE functions for each table
187
- Get,
188
- Post,
189
- Put,
190
- Delete,
191
- }
192
-
193
- export default {{TABLE_NAME_SHORT_PASCAL_CASE}};