rawsql-ts 0.10.3-beta → 0.10.5-beta

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,146 @@
1
+ import { SimpleSelectQuery } from "../models/SelectQuery";
2
+ import { SelectableColumnCollector } from "./SelectableColumnCollector";
3
+ import { OrderByClause, OrderByItem, SortDirection, NullsSortDirection } from "../models/Clause";
4
+ import { SelectQueryParser } from "../parsers/SelectQueryParser";
5
+ /**
6
+ * SqlSortInjector injects sort conditions into a SelectQuery model,
7
+ * creating ORDER BY clauses based on provided sort conditions.
8
+ */
9
+ export class SqlSortInjector {
10
+ constructor(tableColumnResolver) {
11
+ this.tableColumnResolver = tableColumnResolver;
12
+ }
13
+ /**
14
+ * Removes ORDER BY clause from the given query.
15
+ * @param query The SelectQuery to modify
16
+ * @returns The modified SimpleSelectQuery with ORDER BY clause removed
17
+ */
18
+ static removeOrderBy(query) {
19
+ // Convert string query to SimpleSelectQuery using SelectQueryParser if needed
20
+ if (typeof query === 'string') {
21
+ query = SelectQueryParser.parse(query);
22
+ }
23
+ // Check if query is SimpleSelectQuery
24
+ if (!(query instanceof SimpleSelectQuery)) {
25
+ throw new Error('Complex queries are not supported for ORDER BY removal');
26
+ }
27
+ // Create a new query without ORDER BY clause
28
+ return new SimpleSelectQuery({
29
+ withClause: query.withClause,
30
+ selectClause: query.selectClause,
31
+ fromClause: query.fromClause,
32
+ whereClause: query.whereClause,
33
+ groupByClause: query.groupByClause,
34
+ havingClause: query.havingClause,
35
+ orderByClause: null, // Remove ORDER BY
36
+ windowClause: query.windowClause,
37
+ limitClause: query.limitClause,
38
+ offsetClause: query.offsetClause,
39
+ fetchClause: query.fetchClause,
40
+ forClause: query.forClause,
41
+ });
42
+ }
43
+ /**
44
+ * Injects sort conditions as ORDER BY clauses into the given query model.
45
+ * Appends to existing ORDER BY clause if present.
46
+ * @param query The SelectQuery to modify
47
+ * @param sortConditions A record of column names and sort conditions
48
+ * @returns The modified SimpleSelectQuery
49
+ */
50
+ inject(query, sortConditions) {
51
+ // Convert string query to SimpleSelectQuery using SelectQueryParser if needed
52
+ if (typeof query === 'string') {
53
+ query = SelectQueryParser.parse(query);
54
+ }
55
+ // Check if query is SimpleSelectQuery
56
+ if (!(query instanceof SimpleSelectQuery)) {
57
+ throw new Error('Complex queries are not supported for sorting');
58
+ }
59
+ // Collect available columns from the current query only (no upstream search)
60
+ const collector = new SelectableColumnCollector(this.tableColumnResolver);
61
+ const availableColumns = collector.collect(query);
62
+ // Validate that all specified columns exist
63
+ for (const columnName of Object.keys(sortConditions)) {
64
+ const columnEntry = availableColumns.find(item => item.name === columnName);
65
+ if (!columnEntry) {
66
+ throw new Error(`Column or alias '${columnName}' not found in current query`);
67
+ }
68
+ }
69
+ // Build new ORDER BY items
70
+ const newOrderByItems = [];
71
+ for (const [columnName, condition] of Object.entries(sortConditions)) {
72
+ const columnEntry = availableColumns.find(item => item.name === columnName);
73
+ if (!columnEntry)
74
+ continue; // Should not happen due to validation above
75
+ const columnRef = columnEntry.value;
76
+ // Validate condition
77
+ this.validateSortCondition(columnName, condition);
78
+ // Determine sort direction
79
+ let sortDirection;
80
+ if (condition.desc) {
81
+ sortDirection = SortDirection.Descending;
82
+ }
83
+ else {
84
+ sortDirection = SortDirection.Ascending; // Default to ASC
85
+ }
86
+ // Determine nulls position
87
+ let nullsPosition = null;
88
+ if (condition.nullsFirst) {
89
+ nullsPosition = NullsSortDirection.First;
90
+ }
91
+ else if (condition.nullsLast) {
92
+ nullsPosition = NullsSortDirection.Last;
93
+ }
94
+ // Create OrderByItem
95
+ const orderByItem = new OrderByItem(columnRef, sortDirection, nullsPosition);
96
+ newOrderByItems.push(orderByItem);
97
+ }
98
+ // Combine with existing ORDER BY clause if present
99
+ let finalOrderByItems = [];
100
+ if (query.orderByClause) {
101
+ // Append to existing ORDER BY
102
+ finalOrderByItems = [...query.orderByClause.order, ...newOrderByItems];
103
+ }
104
+ else {
105
+ // Create new ORDER BY
106
+ finalOrderByItems = newOrderByItems;
107
+ }
108
+ // Create new OrderByClause
109
+ const newOrderByClause = finalOrderByItems.length > 0
110
+ ? new OrderByClause(finalOrderByItems)
111
+ : null;
112
+ // Create new query with updated ORDER BY clause
113
+ return new SimpleSelectQuery({
114
+ withClause: query.withClause,
115
+ selectClause: query.selectClause,
116
+ fromClause: query.fromClause,
117
+ whereClause: query.whereClause,
118
+ groupByClause: query.groupByClause,
119
+ havingClause: query.havingClause,
120
+ orderByClause: newOrderByClause,
121
+ windowClause: query.windowClause,
122
+ limitClause: query.limitClause,
123
+ offsetClause: query.offsetClause,
124
+ fetchClause: query.fetchClause,
125
+ forClause: query.forClause,
126
+ });
127
+ }
128
+ /**
129
+ * Validates sort condition for a column
130
+ */
131
+ validateSortCondition(columnName, condition) {
132
+ // Check for conflicting sort directions
133
+ if (condition.asc && condition.desc) {
134
+ throw new Error(`Conflicting sort directions for column '${columnName}': both asc and desc specified`);
135
+ }
136
+ // Check for conflicting nulls positions
137
+ if (condition.nullsFirst && condition.nullsLast) {
138
+ throw new Error(`Conflicting nulls positions for column '${columnName}': both nullsFirst and nullsLast specified`);
139
+ }
140
+ // Check if at least one option is specified
141
+ if (!condition.asc && !condition.desc && !condition.nullsFirst && !condition.nullsLast) {
142
+ throw new Error(`Empty sort condition for column '${columnName}': at least one sort option must be specified`);
143
+ }
144
+ }
145
+ }
146
+ //# sourceMappingURL=SqlSortInjector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SqlSortInjector.js","sourceRoot":"","sources":["../../../src/transformers/SqlSortInjector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AACvE,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AACxE,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAEjG,OAAO,EAAE,iBAAiB,EAAE,MAAM,8B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KAAK,CAAC,WAAW;YAC9B,YAAY,EAAE,KAAK,CAAC,YAAY;YAChC,WAAW,EAAE,KAAK,CAAC,WAAW;YAC9B,SAAS,EAAE,KAAK,CAAC,SAAS;SAC7B,CAAC,CAAC;IACP,CAAC;IAED;;OAEG;IACK,qBAAqB,CAAC,UAAkB,EAAE,SAAwB;QACtE,wCAAwC;QACxC,IAAI,SAAS,CAAC,GAAG,IAAI,SAAS,CAAC,IAAI,EAAE,CAAC;YAClC,MAAM,IAAI,KAAK,CAAC,2CAA2C,UAAU,gCAAgC,CAAC,CAAC;QAC3G,CAAC;QAED,wCAAwC;QACxC,IAAI,SAAS,CAAC,UAAU,IAAI,SAAS,CAAC,SAAS,EAAE,CAAC;YAC9C,MAAM,IAAI,KAAK,CAAC,2CAA2C,UAAU,4CAA4C,CAAC,CAAC;QACvH,CAAC;QAED,4CAA4C;QAC5C,IAAI,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC,SAAS,CAAC,UAAU,IAAI,CAAC,SAAS,CAAC,SAAS,EAAE,CAAC;YACrF,MAAM,IAAI,KAAK,CAAC,oCAAoC,UAAU,+CAA+C,CAAC,CAAC;QACnH,CAAC;IACL,CAAC;CACJ"}
@@ -17,5 +17,7 @@ export * from './transformers/TableSourceCollector';
17
17
  export * from './transformers/UpstreamSelectQueryFinder';
18
18
  export * from './transformers/SchemaCollector';
19
19
  export * from './transformers/SqlParamInjector';
20
+ export * from './transformers/SqlSortInjector';
21
+ export * from './transformers/SqlPaginationInjector';
20
22
  export * from './utils/SqlSchemaValidator';
21
23
  export * from './utils/SchemaManager';
@@ -0,0 +1,35 @@
1
+ import { SimpleSelectQuery } from "../models/SelectQuery";
2
+ /**
3
+ * Options for pagination injection
4
+ */
5
+ export interface PaginationOptions {
6
+ /** Page number (1-based) */
7
+ page: number;
8
+ /** Number of items per page */
9
+ pageSize: number;
10
+ }
11
+ /**
12
+ * SqlPaginationInjector injects pagination (LIMIT/OFFSET) into a SelectQuery model,
13
+ * creating LIMIT and OFFSET clauses based on provided pagination options.
14
+ */
15
+ export declare class SqlPaginationInjector {
16
+ /**
17
+ * Injects pagination as LIMIT/OFFSET clauses into the given query model.
18
+ * @param query The SelectQuery to modify
19
+ * @param pagination Pagination options containing page number and page size
20
+ * @returns The modified SimpleSelectQuery with pagination applied
21
+ */
22
+ inject(query: SimpleSelectQuery | string, pagination: PaginationOptions): SimpleSelectQuery;
23
+ /**
24
+ * Removes LIMIT and OFFSET clauses from the given query.
25
+ * @param query The SelectQuery to modify
26
+ * @returns The modified SimpleSelectQuery with pagination removed
27
+ */
28
+ static removePagination(query: SimpleSelectQuery | string): SimpleSelectQuery;
29
+ /**
30
+ * Validates pagination options
31
+ * @param pagination Pagination options to validate
32
+ * @throws Error if validation fails
33
+ */
34
+ private validatePaginationOptions;
35
+ }
@@ -19,11 +19,12 @@ export declare class SqlParamInjector {
19
19
  */
20
20
  inject(query: SimpleSelectQuery | string, state: Record<string, number | string | boolean | Date | null | undefined | Condition>): SelectQuery;
21
21
  }
22
- type Condition = {
22
+ type BaseCondition = {
23
23
  '='?: number | string | boolean | Date;
24
24
  min?: number | string | Date;
25
25
  max?: number | string | Date;
26
26
  like?: string;
27
+ ilike?: string;
27
28
  in?: (number | string | Date)[];
28
29
  any?: (number | string | Date)[];
29
30
  '<'?: number | string | Date;
@@ -33,4 +34,13 @@ type Condition = {
33
34
  '<='?: number | string | Date;
34
35
  '>='?: number | string | Date;
35
36
  };
37
+ type SingleCondition = BaseCondition & {
38
+ column?: string;
39
+ };
40
+ type LogicalCondition = {
41
+ column?: string;
42
+ and?: SingleCondition[];
43
+ or?: SingleCondition[];
44
+ };
45
+ type Condition = BaseCondition | SingleCondition | LogicalCondition;
36
46
  export {};
@@ -0,0 +1,36 @@
1
+ import { SimpleSelectQuery } from "../models/SelectQuery";
2
+ /**
3
+ * SqlSortInjector injects sort conditions into a SelectQuery model,
4
+ * creating ORDER BY clauses based on provided sort conditions.
5
+ */
6
+ export declare class SqlSortInjector {
7
+ private tableColumnResolver?;
8
+ constructor(tableColumnResolver?: (tableName: string) => string[]);
9
+ /**
10
+ * Removes ORDER BY clause from the given query.
11
+ * @param query The SelectQuery to modify
12
+ * @returns The modified SimpleSelectQuery with ORDER BY clause removed
13
+ */
14
+ static removeOrderBy(query: SimpleSelectQuery | string): SimpleSelectQuery;
15
+ /**
16
+ * Injects sort conditions as ORDER BY clauses into the given query model.
17
+ * Appends to existing ORDER BY clause if present.
18
+ * @param query The SelectQuery to modify
19
+ * @param sortConditions A record of column names and sort conditions
20
+ * @returns The modified SimpleSelectQuery
21
+ */
22
+ inject(query: SimpleSelectQuery | string, sortConditions: SortConditions): SimpleSelectQuery;
23
+ /**
24
+ * Validates sort condition for a column
25
+ */
26
+ private validateSortCondition;
27
+ }
28
+ export type SortCondition = {
29
+ asc?: boolean;
30
+ desc?: boolean;
31
+ nullsFirst?: boolean;
32
+ nullsLast?: boolean;
33
+ };
34
+ export type SortConditions = {
35
+ [columnName: string]: SortCondition;
36
+ };
package/dist/index.d.ts CHANGED
@@ -17,5 +17,7 @@ export * from './transformers/TableSourceCollector';
17
17
  export * from './transformers/UpstreamSelectQueryFinder';
18
18
  export * from './transformers/SchemaCollector';
19
19
  export * from './transformers/SqlParamInjector';
20
+ export * from './transformers/SqlSortInjector';
21
+ export * from './transformers/SqlPaginationInjector';
20
22
  export * from './utils/SqlSchemaValidator';
21
23
  export * from './utils/SchemaManager';
package/dist/index.js CHANGED
@@ -34,6 +34,8 @@ __exportStar(require("./transformers/TableSourceCollector"), exports);
34
34
  __exportStar(require("./transformers/UpstreamSelectQueryFinder"), exports);
35
35
  __exportStar(require("./transformers/SchemaCollector"), exports);
36
36
  __exportStar(require("./transformers/SqlParamInjector"), exports);
37
+ __exportStar(require("./transformers/SqlSortInjector"), exports);
38
+ __exportStar(require("./transformers/SqlPaginationInjector"), exports);
37
39
  __exportStar(require("./utils/SqlSchemaValidator"), exports);
38
40
  __exportStar(require("./utils/SchemaManager"), exports);
39
41
  // Add more exports here if you want to expose additional public API
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oCAAoC;AACpC,8DAA4C;AAC5C,8DAA4C;AAE5C,6DAA2C;AAC3C,uDAAqC;AACrC,0DAAwC;AACxC,uDAAqC;AAErC,8DAA4C;AAC5C,+DAA6C;AAC7C,2DAAyC;AACzC,8DAA4C;AAC5C,0EAAwD;AACxD,8DAA4C,CAAC,0BAA0B;AACvE,sEAAoD;AACpD,2EAAyD;AACzD,qEAAmD;AACnD,sEAAoD;AACpD,2EAAyD;AACzD,iEAA+C;AAC/C,kEAAgD;AAEhD,6DAA2C;AAC3C,wDAAsC;AACtC,oEAAoE"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oCAAoC;AACpC,8DAA4C;AAC5C,8DAA4C;AAE5C,6DAA2C;AAC3C,uDAAqC;AACrC,0DAAwC;AACxC,uDAAqC;AAErC,8DAA4C;AAC5C,+DAA6C;AAC7C,2DAAyC;AACzC,8DAA4C;AAC5C,0EAAwD;AACxD,8DAA4C,CAAC,0BAA0B;AACvE,sEAAoD;AACpD,2EAAyD;AACzD,qEAAmD;AACnD,sEAAoD;AACpD,2EAAyD;AACzD,iEAA+C;AAC/C,kEAAgD;AAChD,iEAA+C;AAC/C,uEAAqD;AAErD,6DAA2C;AAC3C,wDAAsC;AACtC,oEAAoE"}