@carbonorm/carbonnode 3.0.15 → 3.1.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 (46) hide show
  1. package/dist/api/C6Constants.d.ts +183 -0
  2. package/dist/api/builders/queryHelpers.d.ts +4 -0
  3. package/dist/api/executors/Executor.d.ts +9 -15
  4. package/dist/api/executors/HttpExecutor.d.ts +7 -14
  5. package/dist/api/executors/SqlExecutor.d.ts +9 -12
  6. package/dist/api/orm/SqlBuilder.d.ts +17 -0
  7. package/dist/api/orm/builders/AggregateBuilder.d.ts +5 -0
  8. package/dist/api/orm/builders/ConditionBuilder.d.ts +11 -0
  9. package/dist/api/orm/builders/JoinBuilder.d.ts +5 -0
  10. package/dist/api/orm/builders/PaginationBuilder.d.ts +5 -0
  11. package/dist/api/orm/queries/DeleteQueryBuilder.d.ts +6 -0
  12. package/dist/api/orm/queries/SelectQueryBuilder.d.ts +6 -0
  13. package/dist/api/orm/queries/UpdateQueryBuilder.d.ts +6 -0
  14. package/dist/api/orm/queryHelpers.d.ts +4 -0
  15. package/dist/api/orm/utils/sqlUtils.d.ts +7 -0
  16. package/dist/api/restOrm.d.ts +3 -10
  17. package/dist/api/restRequest.d.ts +3 -10
  18. package/dist/api/types/ormGenerics.d.ts +13 -0
  19. package/dist/api/types/ormInterfaces.d.ts +23 -40
  20. package/dist/index.cjs.js +363 -407
  21. package/dist/index.cjs.js.map +1 -1
  22. package/dist/index.d.ts +10 -1
  23. package/dist/index.esm.js +351 -407
  24. package/dist/index.esm.js.map +1 -1
  25. package/package.json +1 -1
  26. package/scripts/assets/handlebars/C6.ts.handlebars +2 -4
  27. package/src/api/C6Constants.ts +37 -2
  28. package/src/api/executors/Executor.ts +18 -36
  29. package/src/api/executors/HttpExecutor.ts +46 -59
  30. package/src/api/executors/SqlExecutor.ts +17 -29
  31. package/src/api/handlers/ExpressHandler.ts +1 -1
  32. package/src/api/orm/builders/AggregateBuilder.ts +38 -0
  33. package/src/api/orm/builders/ConditionBuilder.ts +113 -0
  34. package/src/api/orm/builders/JoinBuilder.ts +25 -0
  35. package/src/api/orm/builders/PaginationBuilder.ts +28 -0
  36. package/src/api/orm/queries/DeleteQueryBuilder.ts +28 -0
  37. package/src/api/orm/queries/SelectQueryBuilder.ts +49 -0
  38. package/src/api/orm/queries/UpdateQueryBuilder.ts +42 -0
  39. package/src/api/orm/queryHelpers.ts +18 -0
  40. package/src/api/orm/utils/sqlUtils.ts +24 -0
  41. package/src/api/restOrm.ts +4 -14
  42. package/src/api/restRequest.ts +16 -34
  43. package/src/api/types/ormGenerics.ts +18 -0
  44. package/src/api/types/ormInterfaces.ts +31 -46
  45. package/src/index.ts +10 -1
  46. package/src/api/builders/sqlBuilder.ts +0 -454
@@ -0,0 +1,42 @@
1
+ import {OrmGenerics} from "../../types/ormGenerics";
2
+ import { PaginationBuilder } from '../builders/PaginationBuilder';
3
+ import {SqlBuilderResult} from "../utils/sqlUtils";
4
+
5
+ export class UpdateQueryBuilder<G extends OrmGenerics> extends PaginationBuilder<G>{
6
+
7
+ build(
8
+ table: string,
9
+ data: Record<string, any>,
10
+ args: any = {}
11
+ ): SqlBuilderResult {
12
+ const params = this.useNamedParams ? {} : [];
13
+ let sql = `UPDATE \`${table}\``;
14
+
15
+ if (args.JOIN) {
16
+ sql += this.buildJoinClauses(args.JOIN, params);
17
+ }
18
+
19
+ const setClauses = Object.entries(data).map(([col, val]) => {
20
+ if (Array.isArray(params)) {
21
+ params.push(val);
22
+ return `\`${col}\` = ?`;
23
+ } else {
24
+ const key = `param${Object.keys(params).length}`;
25
+ params[key] = val;
26
+ return `\`${col}\` = :${key}`;
27
+ }
28
+ });
29
+
30
+ sql += ` SET ${setClauses.join(', ')}`;
31
+
32
+ if (args.WHERE) {
33
+ sql += this.buildWhereClause(args.WHERE, params);
34
+ }
35
+
36
+ if (args.PAGINATION) {
37
+ sql += this.buildPaginationClause(args.PAGINATION);
38
+ }
39
+
40
+ return { sql, params };
41
+ }
42
+ }
@@ -0,0 +1,18 @@
1
+ // Alias a table name with a given alias
2
+ import {C6C} from "../C6Constants";
3
+
4
+ export const A = (tableName: string, alias: string): string =>
5
+ `${tableName} ${alias}`;
6
+
7
+ // Qualify a column constant (e.g. 'property_units.parcel_id') to an alias
8
+ export const F = (qualifiedCol: string, alias: string): string =>
9
+ `${alias}.${qualifiedCol.split('.').pop()}`;
10
+
11
+ // Equal join condition using full-qualified column constants
12
+ export const fieldEq = (leftCol: string, rightCol: string, leftAlias: string, rightAlias: string): Record<string, string> => ({
13
+ [F(leftCol, leftAlias)]: F(rightCol, rightAlias)
14
+ });
15
+
16
+ // ST_Distance_Sphere for aliased fields
17
+ export const distSphere = (fromCol: string, toCol: string, fromAlias: string, toAlias: string): any[] =>
18
+ [C6C.ST_DISTANCE_SPHERE, F(fromCol, fromAlias), F(toCol, toAlias)];
@@ -0,0 +1,24 @@
1
+
2
+
3
+
4
+ export interface SqlBuilderResult {
5
+ sql: string;
6
+ params: any[] | { [key: string]: any }; // params can be an array or an object for named placeholders
7
+ }
8
+
9
+
10
+ export function convertHexIfBinary(
11
+ _col: string,
12
+ val: any,
13
+ columnDef?: any
14
+ ): any {
15
+ if (
16
+ typeof val === 'string' &&
17
+ /^[0-9a-fA-F]{32}$/.test(val) &&
18
+ typeof columnDef === 'object' &&
19
+ columnDef.MYSQL_TYPE.toUpperCase().includes('BINARY')
20
+ ) {
21
+ return Buffer.from(val, 'hex');
22
+ }
23
+ return val;
24
+ }
@@ -1,27 +1,17 @@
1
1
  import restRequest from "./restRequest";
2
+ import {OrmGenerics} from "./types/ormGenerics";
2
3
  import { iRest, iRestMethods } from "./types/ormInterfaces";
3
4
 
4
5
  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<RestShortTableName, RestTableInterface, PrimaryKey>, "requestMethod">) {
6
+ G extends Omit<OrmGenerics, "requestMethod">
7
+ >(config: () => Omit<iRest<G['RestShortTableName'], G['RestTableInterface'], G['PrimaryKey']>, "requestMethod">) {
11
8
 
12
9
  const methods: iRestMethods[] = ["GET", "PUT", "POST", "DELETE"];
13
10
 
14
11
  return Object.fromEntries(
15
12
  methods.map(method => [
16
13
  method[0] + method.slice(1).toLowerCase(), // Capitalize e.g. "Get"
17
- restRequest<
18
- typeof method,
19
- RestShortTableName,
20
- RestTableInterface,
21
- PrimaryKey,
22
- CustomAndRequiredFields,
23
- RequestTableOverrides
24
- >(() => ({
14
+ restRequest<G>(() => ({
25
15
  ...config(),
26
16
  requestMethod: method as iRestMethods,
27
17
  }))
@@ -1,65 +1,47 @@
1
1
  import isNode from '../variables/isNode';
2
+ import {OrmGenerics} from "./types/ormGenerics";
2
3
  import {
3
4
  apiReturn, DetermineResponseDataType,
4
- iRest, iRestMethods, RequestQueryBody
5
+ iRest, RequestQueryBody
5
6
  } from "./types/ormInterfaces";
6
7
 
7
8
  /**
8
9
  * Facade: routes API calls to SQL or HTTP executors based on runtime context.
9
10
  */
10
11
  export default function restRequest<
11
- RequestMethod extends iRestMethods,
12
- RestShortTableName extends string = any,
13
- RestTableInterface extends { [key: string]: any } = any,
14
- PrimaryKey extends Extract<keyof RestTableInterface, string> = Extract<keyof RestTableInterface, string>,
15
- CustomAndRequiredFields extends { [key: string]: any } = any,
16
- RequestTableOverrides extends { [key in keyof RestTableInterface]: any } = { [key in keyof RestTableInterface]: any }
12
+ G extends OrmGenerics
17
13
  >(
18
14
  configX: (() => iRest<
19
- RestShortTableName,
20
- RestTableInterface,
21
- PrimaryKey
15
+ G['RestTableInterface'],
16
+ G['RestShortTableName'],
17
+ G['PrimaryKey']
22
18
  >) | iRest<
23
- RestShortTableName,
24
- RestTableInterface,
25
- PrimaryKey
19
+ G['RestShortTableName'],
20
+ G['RestTableInterface'],
21
+ G['PrimaryKey']
26
22
  >
27
23
  ) {
28
24
  return async (
29
25
  request: RequestQueryBody<
30
- RequestMethod,
31
- RestTableInterface,
32
- CustomAndRequiredFields,
33
- RequestTableOverrides
26
+ G['RequestMethod'],
27
+ G['RestTableInterface'],
28
+ G['CustomAndRequiredFields'],
29
+ G['RequestTableOverrides']
34
30
  >,
35
- ): Promise<apiReturn<DetermineResponseDataType<RequestMethod, RestTableInterface>>> => {
31
+ ): Promise<apiReturn<DetermineResponseDataType<G['RequestMethod'], G['RestTableInterface']>>> => {
36
32
 
37
33
  const config = typeof configX === "function" ? configX() : configX;
38
34
 
39
35
  // SQL path if on Node with a provided pool
40
36
  if (isNode() && config.mysqlPool) {
41
37
  const {SqlExecutor} = await import('./executors/SqlExecutor');
42
- const executor = new SqlExecutor<
43
- RequestMethod,
44
- RestShortTableName,
45
- RestTableInterface,
46
- PrimaryKey,
47
- CustomAndRequiredFields,
48
- RequestTableOverrides
49
- >(config, request);
38
+ const executor = new SqlExecutor<G>(config, request);
50
39
  return executor.execute();
51
40
  }
52
41
 
53
42
  // HTTP path fallback
54
43
  const {HttpExecutor} = await import('./executors/HttpExecutor');
55
- const http = new HttpExecutor<
56
- RequestMethod,
57
- RestShortTableName,
58
- RestTableInterface,
59
- PrimaryKey,
60
- CustomAndRequiredFields,
61
- RequestTableOverrides
62
- >(config, request);
44
+ const http = new HttpExecutor<G>(config, request);
63
45
  return http.execute();
64
46
  };
65
47
  }
@@ -0,0 +1,18 @@
1
+ // types/ormGenerics.ts
2
+ import { iRestMethods } from './ormInterfaces';
3
+
4
+ export type OrmGenerics<
5
+ RequestMethod extends iRestMethods = iRestMethods,
6
+ RestShortTableName extends string | string[] = any,
7
+ RestTableInterface extends Record<string, any> = any,
8
+ PrimaryKey extends keyof RestTableInterface & string = keyof RestTableInterface & string,
9
+ CustomAndRequiredFields extends Record<string, any> = any,
10
+ RequestTableOverrides extends { [key in keyof RestTableInterface]: any } = { [key in keyof RestTableInterface]: any }
11
+ > = {
12
+ RequestMethod: RequestMethod;
13
+ RestShortTableName: RestShortTableName;
14
+ RestTableInterface: RestTableInterface;
15
+ PrimaryKey: PrimaryKey;
16
+ CustomAndRequiredFields: CustomAndRequiredFields;
17
+ RequestTableOverrides: RequestTableOverrides;
18
+ };
@@ -6,6 +6,7 @@ import { eFetchDependencies } from "./dynamicFetching";
6
6
  import { Modify } from "./modifyTypes";
7
7
  import { JoinType, OrderDirection, SQLComparisonOperator, SQLFunction } from "./mysqlTypes";
8
8
  import { CarbonReact } from "@carbonorm/carbonreact";
9
+ import {OrmGenerics} from "./ormGenerics";
9
10
 
10
11
  export type iRestMethods = 'GET' | 'POST' | 'PUT' | 'DELETE';
11
12
  export const POST = 'POST';
@@ -132,8 +133,8 @@ export type DetermineResponseDataType<
132
133
 
133
134
  export interface iRest<
134
135
  RestShortTableName extends string = any,
135
- RestTableInterface extends { [key: string]: any } = any,
136
- PrimaryKey extends keyof RestTableInterface & string = any
136
+ RestTableInterface extends Record<string, any> = any,
137
+ PrimaryKey extends keyof RestTableInterface & string = keyof RestTableInterface & string
137
138
  > {
138
139
  C6: iC6Object;
139
140
  axios?: AxiosInstance;
@@ -159,69 +160,53 @@ export type tColumns<TableName extends string, T extends { [key: string]: any }>
159
160
 
160
161
  export type tPrimaryKeys<TableName extends string, PK extends string> = `${TableName}.${PK}`;
161
162
 
162
- export interface iC6RestfulModel<RestShortTableNames extends string, RestTableInterfaces extends { [key: string]: any }, PK extends keyof RestTableInterfaces & string> {
163
- TABLE_NAME: RestShortTableNames;
164
- PRIMARY: tPrimaryKeys<RestShortTableNames, PK>[];
165
- PRIMARY_SHORT: PK[];
166
- COLUMNS: tColumns<RestShortTableNames, RestTableInterfaces>;
163
+ export interface iC6RestfulModel<
164
+ RestShortTableName extends string,
165
+ RestTableInterface extends Record<string, any> = any,
166
+ PrimaryKey extends keyof RestTableInterface & string = keyof RestTableInterface & string,
167
+ > {
168
+ TABLE_NAME: RestShortTableName;
169
+ PRIMARY: tPrimaryKeys<RestShortTableName, PrimaryKey>[];
170
+ PRIMARY_SHORT: PrimaryKey[];
171
+ COLUMNS: tColumns<RestShortTableName, RestTableInterface>;
167
172
  TYPE_VALIDATION: { [key: string]: iTypeValidation };
168
173
  REGEX_VALIDATION: RegExpMap;
169
- LIFECYCLE_HOOKS: iRestHooks<RestShortTableNames, RestTableInterfaces, PK>;
174
+ LIFECYCLE_HOOKS: iRestHooks<OrmGenerics<any, RestShortTableName, RestTableInterface, PrimaryKey>>;
170
175
  TABLE_REFERENCES: { [columnName: string]: iConstraint[] };
171
176
  TABLE_REFERENCED_BY: { [columnName: string]: iConstraint[] };
172
177
  }
173
178
 
174
- export type iRestReactiveLifecycle<
175
- Method extends iRestMethods,
176
- RestShortTableName extends string,
177
- RestTableInterface extends { [key: string]: any },
178
- PrimaryKey extends keyof RestTableInterface & string,
179
- CustomAndRequiredFields extends { [key: string]: any },
180
- RequestTableOverrides extends { [key: string]: any }
181
- > = {
179
+ export type iRestReactiveLifecycle<G extends OrmGenerics> = {
182
180
  beforeProcessing?: {
183
181
  [key: string]: (args: {
184
- config: iRest<RestShortTableName, RestTableInterface, PrimaryKey>;
185
- request: RequestQueryBody<Method, RestTableInterface, CustomAndRequiredFields, RequestTableOverrides>;
182
+ config: iRest<G['RestShortTableName'], G['RestTableInterface'], G['PrimaryKey']>;
183
+ request: RequestQueryBody<G['RequestMethod'], G['RestTableInterface'], G['CustomAndRequiredFields'], G['RequestTableOverrides']>;
186
184
  }) => void | Promise<void>;
187
185
  };
188
186
  beforeExecution?: {
189
187
  [key: string]: (args: {
190
- config: iRest<RestShortTableName, RestTableInterface, PrimaryKey>;
191
- request: RequestQueryBody<Method, RestTableInterface, CustomAndRequiredFields, RequestTableOverrides>;
188
+ config: iRest<G['RestShortTableName'], G['RestTableInterface'], G['PrimaryKey']>;
189
+ request: RequestQueryBody<G['RequestMethod'], G['RestTableInterface'], G['CustomAndRequiredFields'], G['RequestTableOverrides']>;
192
190
  }) => void | Promise<void>;
193
191
  };
194
192
  afterExecution?: {
195
193
  [key: string]: (args: {
196
- config: iRest<RestShortTableName, RestTableInterface, PrimaryKey>;
197
- request: RequestQueryBody<Method, RestTableInterface, CustomAndRequiredFields, RequestTableOverrides>;
198
- response: AxiosResponse<DetermineResponseDataType<Method, RestTableInterface>>;
194
+ config: iRest<G['RestShortTableName'], G['RestTableInterface'], G['PrimaryKey']>;
195
+ request: RequestQueryBody<G['RequestMethod'], G['RestTableInterface'], G['CustomAndRequiredFields'], G['RequestTableOverrides']>;
196
+ response: AxiosResponse<DetermineResponseDataType<G['RequestMethod'], G['RestTableInterface']>>;
199
197
  }) => void | Promise<void>;
200
198
  };
201
199
  afterCommit?: {
202
200
  [key: string]: (args: {
203
- config: iRest<RestShortTableName, RestTableInterface, PrimaryKey>;
204
- request: RequestQueryBody<Method, RestTableInterface, CustomAndRequiredFields, RequestTableOverrides>;
205
- response: AxiosResponse<DetermineResponseDataType<Method, RestTableInterface>>;
201
+ config: iRest<G['RestShortTableName'], G['RestTableInterface'], G['PrimaryKey']>;
202
+ request: RequestQueryBody<G['RequestMethod'], G['RestTableInterface'], G['CustomAndRequiredFields'], G['RequestTableOverrides']>;
203
+ response: AxiosResponse<DetermineResponseDataType<G['RequestMethod'], G['RestTableInterface']>>;
206
204
  }) => void | Promise<void>;
207
205
  };
208
206
  };
209
207
 
210
- export type iRestHooks<
211
- RestShortTableName extends string,
212
- RestTableInterface extends { [key: string]: any },
213
- PrimaryKey extends keyof RestTableInterface & string,
214
- CustomAndRequiredFields extends { [key: string]: any } = any,
215
- RequestTableOverrides extends { [key: string]: any } = { [key in keyof RestTableInterface]: any }
216
- > = {
217
- [Method in iRestMethods]: iRestReactiveLifecycle<
218
- Method,
219
- RestShortTableName,
220
- RestTableInterface,
221
- PrimaryKey,
222
- CustomAndRequiredFields,
223
- RequestTableOverrides
224
- >;
208
+ export type iRestHooks<G extends OrmGenerics> = {
209
+ [Method in iRestMethods]: iRestReactiveLifecycle<G>;
225
210
  };
226
211
 
227
212
  export interface iDynamicApiImport<RestData extends { [key: string]: any } = any> {
@@ -238,10 +223,10 @@ export interface iRestApiFunctions<RestData extends { [key: string]: any } = any
238
223
  Put: (request?: RequestQueryBody<'PUT', RestData>) => apiReturn<iPutC6RestResponse<RestData>>;
239
224
  }
240
225
 
241
- export interface iC6Object<
242
- RestShortTableName extends string = any,
243
- RestTableInterface extends { [key: string]: any } = any,
244
- PrimaryKey extends Extract<keyof RestTableInterface, string> = Extract<keyof RestTableInterface, string>
226
+ export interface iC6Object<
227
+ RestShortTableName extends string = any,
228
+ RestTableInterface extends { [key: string]: any } = any,
229
+ PrimaryKey extends keyof RestTableInterface & string = keyof RestTableInterface & string
245
230
  > {
246
231
  C6VERSION: string;
247
232
  TABLES: {
@@ -257,7 +242,7 @@ export interface iRestApiFunctions<RestData extends { [key: string]: any } = any
257
242
  export interface tC6Tables<
258
243
  RestShortTableName extends string = any,
259
244
  RestTableInterface extends { [key: string]: any } = any,
260
- PrimaryKey extends Extract<keyof RestTableInterface, string> = Extract<keyof RestTableInterface, string>
245
+ PrimaryKey extends keyof RestTableInterface & string = keyof RestTableInterface & string
261
246
  > {
262
247
  [key: string]: iC6RestfulModel<RestShortTableName, RestTableInterface, PrimaryKey> & { [key: string]: any };
263
248
  }
package/src/index.ts CHANGED
@@ -12,14 +12,23 @@ export { default as restRequest } from "./api/restRequest";
12
12
  export * from "./api/restRequest";
13
13
  export { default as timeout } from "./api/timeout";
14
14
  export * from "./api/timeout";
15
- export * from "./api/builders/sqlBuilder";
16
15
  export * from "./api/executors/Executor";
17
16
  export * from "./api/executors/HttpExecutor";
18
17
  export * from "./api/executors/SqlExecutor";
19
18
  export * from "./api/handlers/ExpressHandler";
19
+ export * from "./api/orm/queryHelpers";
20
+ export * from "./api/orm/builders/AggregateBuilder";
21
+ export * from "./api/orm/builders/ConditionBuilder";
22
+ export * from "./api/orm/builders/JoinBuilder";
23
+ export * from "./api/orm/builders/PaginationBuilder";
24
+ export * from "./api/orm/queries/DeleteQueryBuilder";
25
+ export * from "./api/orm/queries/SelectQueryBuilder";
26
+ export * from "./api/orm/queries/UpdateQueryBuilder";
27
+ export * from "./api/orm/utils/sqlUtils";
20
28
  export * from "./api/types/dynamicFetching";
21
29
  export * from "./api/types/modifyTypes";
22
30
  export * from "./api/types/mysqlTypes";
31
+ export * from "./api/types/ormGenerics";
23
32
  export * from "./api/types/ormInterfaces";
24
33
  export * from "./api/utils/apiHelpers";
25
34
  export * from "./api/utils/cacheManager";