pg-query-sdk 1.0.0 → 1.0.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 (56) hide show
  1. package/README.md +99 -370
  2. package/dist/cjs/builders/ConditionBuilder.d.ts +34 -0
  3. package/dist/cjs/builders/ConditionBuilder.js +34 -0
  4. package/dist/cjs/builders/QueryBuilder.d.ts +112 -1
  5. package/dist/cjs/builders/QueryBuilder.js +94 -1
  6. package/dist/cjs/core/Database.d.ts +26 -0
  7. package/dist/cjs/core/Database.js +26 -0
  8. package/dist/cjs/core/ParamContext.d.ts +16 -0
  9. package/dist/cjs/core/ParamContext.js +16 -0
  10. package/dist/cjs/core/QueryExecutor.d.ts +26 -0
  11. package/dist/cjs/core/QueryExecutor.js +26 -0
  12. package/dist/cjs/core/TransactionManager.d.ts +13 -0
  13. package/dist/cjs/core/TransactionManager.js +13 -0
  14. package/dist/cjs/core/UnitOfWork.d.ts +19 -0
  15. package/dist/cjs/core/UnitOfWork.js +19 -0
  16. package/dist/cjs/dialects/Dialect.d.ts +13 -0
  17. package/dist/cjs/dialects/MysqlDialect.d.ts +12 -0
  18. package/dist/cjs/dialects/MysqlDialect.js +12 -0
  19. package/dist/cjs/dialects/PostgresDialect.d.ts +13 -0
  20. package/dist/cjs/dialects/PostgresDialect.js +13 -0
  21. package/dist/cjs/index.d.ts +28 -0
  22. package/dist/cjs/index.js +28 -0
  23. package/dist/cjs/orm/Repository.d.ts +34 -0
  24. package/dist/cjs/orm/Repository.js +34 -0
  25. package/dist/cjs/query/ConditionBuilder.d.ts +33 -0
  26. package/dist/cjs/query/ConditionBuilder.js +33 -0
  27. package/dist/cjs/query/QueryBuilder.d.ts +154 -0
  28. package/dist/cjs/query/QueryBuilder.js +136 -0
  29. package/dist/esm/builders/ConditionBuilder.d.ts +34 -0
  30. package/dist/esm/builders/ConditionBuilder.js +34 -0
  31. package/dist/esm/builders/QueryBuilder.d.ts +112 -1
  32. package/dist/esm/builders/QueryBuilder.js +94 -1
  33. package/dist/esm/core/Database.d.ts +26 -0
  34. package/dist/esm/core/Database.js +26 -0
  35. package/dist/esm/core/ParamContext.d.ts +16 -0
  36. package/dist/esm/core/ParamContext.js +16 -0
  37. package/dist/esm/core/QueryExecutor.d.ts +26 -0
  38. package/dist/esm/core/QueryExecutor.js +26 -0
  39. package/dist/esm/core/TransactionManager.d.ts +13 -0
  40. package/dist/esm/core/TransactionManager.js +13 -0
  41. package/dist/esm/core/UnitOfWork.d.ts +19 -0
  42. package/dist/esm/core/UnitOfWork.js +19 -0
  43. package/dist/esm/dialects/Dialect.d.ts +13 -0
  44. package/dist/esm/dialects/MysqlDialect.d.ts +12 -0
  45. package/dist/esm/dialects/MysqlDialect.js +12 -0
  46. package/dist/esm/dialects/PostgresDialect.d.ts +13 -0
  47. package/dist/esm/dialects/PostgresDialect.js +13 -0
  48. package/dist/esm/index.d.ts +28 -0
  49. package/dist/esm/index.js +28 -0
  50. package/dist/esm/orm/Repository.d.ts +34 -0
  51. package/dist/esm/orm/Repository.js +34 -0
  52. package/dist/esm/query/ConditionBuilder.d.ts +33 -0
  53. package/dist/esm/query/ConditionBuilder.js +33 -0
  54. package/dist/esm/query/QueryBuilder.d.ts +154 -0
  55. package/dist/esm/query/QueryBuilder.js +136 -0
  56. package/package.json +1 -1
@@ -1,11 +1,45 @@
1
1
  import ParamContext from "../core/ParamContext";
2
+ /**
3
+ * A builder for constructing SQL WHERE and HAVING clauses.
4
+ * This builder is specifically designed for the `builders` directory and might have a different scope or feature set
5
+ * compared to other ConditionBuilder implementations in the project.
6
+ */
2
7
  export default class ConditionBuilder {
3
8
  private ctx;
4
9
  private conditions;
10
+ /**
11
+ * Creates an instance of ConditionBuilder.
12
+ * @param ctx - The ParamContext to manage query parameters.
13
+ */
5
14
  constructor(ctx: ParamContext);
15
+ /**
16
+ * Adds one or more WHERE conditions based on an object.
17
+ * @param obj - An object where keys are column names and values are the desired values.
18
+ * @returns The current ConditionBuilder instance.
19
+ */
6
20
  where(obj: Record<string, any>): this;
21
+ /**
22
+ * Adds a raw SQL expression to the conditions.
23
+ * @param expression - The raw SQL expression.
24
+ * @returns The current ConditionBuilder instance.
25
+ */
7
26
  raw(expression: string): this;
27
+ /**
28
+ * Adds a group of conditions connected by AND.
29
+ * @param callback - A callback function that receives a nested ConditionBuilder to define conditions within the group.
30
+ * @returns The current ConditionBuilder instance.
31
+ */
8
32
  andGroup(callback: (qb: ConditionBuilder) => void): this;
33
+ /**
34
+ * Adds a group of conditions connected by OR.
35
+ * @param callback - A callback function that receives a nested ConditionBuilder to define conditions within the group.
36
+ * @returns The current ConditionBuilder instance.
37
+ */
9
38
  orGroup(callback: (qb: ConditionBuilder) => void): this;
39
+ /**
40
+ * Builds the SQL condition string.
41
+ * @param prefix - The prefix for the condition (e.g., 'WHERE', 'HAVING'). Defaults to 'WHERE'.
42
+ * @returns The built SQL condition string, or an empty string if no conditions were added.
43
+ */
10
44
  build(prefix?: string): string;
11
45
  }
@@ -1,10 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * A builder for constructing SQL WHERE and HAVING clauses.
5
+ * This builder is specifically designed for the `builders` directory and might have a different scope or feature set
6
+ * compared to other ConditionBuilder implementations in the project.
7
+ */
3
8
  class ConditionBuilder {
9
+ /**
10
+ * Creates an instance of ConditionBuilder.
11
+ * @param ctx - The ParamContext to manage query parameters.
12
+ */
4
13
  constructor(ctx) {
5
14
  this.ctx = ctx;
6
15
  this.conditions = [];
7
16
  }
17
+ /**
18
+ * Adds one or more WHERE conditions based on an object.
19
+ * @param obj - An object where keys are column names and values are the desired values.
20
+ * @returns The current ConditionBuilder instance.
21
+ */
8
22
  where(obj) {
9
23
  Object.entries(obj).forEach(([key, value]) => {
10
24
  const placeholder = this.ctx.add(value);
@@ -12,10 +26,20 @@ class ConditionBuilder {
12
26
  });
13
27
  return this;
14
28
  }
29
+ /**
30
+ * Adds a raw SQL expression to the conditions.
31
+ * @param expression - The raw SQL expression.
32
+ * @returns The current ConditionBuilder instance.
33
+ */
15
34
  raw(expression) {
16
35
  this.conditions.push(expression);
17
36
  return this;
18
37
  }
38
+ /**
39
+ * Adds a group of conditions connected by AND.
40
+ * @param callback - A callback function that receives a nested ConditionBuilder to define conditions within the group.
41
+ * @returns The current ConditionBuilder instance.
42
+ */
19
43
  andGroup(callback) {
20
44
  const nested = new ConditionBuilder(this.ctx);
21
45
  callback(nested);
@@ -25,6 +49,11 @@ class ConditionBuilder {
25
49
  }
26
50
  return this;
27
51
  }
52
+ /**
53
+ * Adds a group of conditions connected by OR.
54
+ * @param callback - A callback function that receives a nested ConditionBuilder to define conditions within the group.
55
+ * @returns The current ConditionBuilder instance.
56
+ */
28
57
  orGroup(callback) {
29
58
  const nested = new ConditionBuilder(this.ctx);
30
59
  callback(nested);
@@ -34,6 +63,11 @@ class ConditionBuilder {
34
63
  }
35
64
  return this;
36
65
  }
66
+ /**
67
+ * Builds the SQL condition string.
68
+ * @param prefix - The prefix for the condition (e.g., 'WHERE', 'HAVING'). Defaults to 'WHERE'.
69
+ * @returns The built SQL condition string, or an empty string if no conditions were added.
70
+ */
37
71
  build(prefix = 'WHERE') {
38
72
  if (!this.conditions.length)
39
73
  return '';
@@ -1,31 +1,142 @@
1
1
  import QueryExecutor from '../core/QueryExecutor';
2
2
  import { Dialect } from "../dialects/Dialect";
3
+ /**
4
+ * A fluent SQL query builder for constructing and executing database queries.
5
+ * This builder is specifically designed for the `builders` directory and might have a different scope or feature set
6
+ * compared to other QueryBuilder implementations in the project.
7
+ * @template T The expected type of the results.
8
+ */
3
9
  export default class QueryBuilder<T = any> {
4
10
  private executor;
11
+ /**
12
+ * The fields to be selected in the query.
13
+ */
5
14
  private fields;
15
+ /**
16
+ * The join clauses for the query.
17
+ */
6
18
  private joins;
19
+ /**
20
+ * The fields to group by.
21
+ */
7
22
  private groupByFields;
23
+ /**
24
+ * The fields to order by.
25
+ */
8
26
  private orderByFields;
27
+ /**
28
+ * The maximum number of rows to return.
29
+ */
9
30
  private limitCount?;
31
+ /**
32
+ * The number of rows to skip.
33
+ */
10
34
  private offsetCount?;
35
+ /**
36
+ * Common Table Expressions (CTEs) defined for the query.
37
+ */
11
38
  private ctes;
39
+ /**
40
+ * The FROM clause of the query.
41
+ */
12
42
  private fromClause;
43
+ /**
44
+ * The condition builder for WHERE clauses.
45
+ */
13
46
  private condition;
47
+ /**
48
+ * The condition builder for HAVING clauses.
49
+ */
14
50
  private havingCondition;
51
+ /**
52
+ * The parameter context for managing query parameters.
53
+ */
15
54
  private ctx;
55
+ /**
56
+ * Creates an instance of QueryBuilder.
57
+ * @param table - The name of the table to query from.
58
+ * @param executor - The query executor to use.
59
+ * @param dialect - The database dialect to use.
60
+ */
16
61
  constructor(table: string, executor: QueryExecutor, dialect: Dialect);
62
+ /**
63
+ * Specifies the fields to select.
64
+ * @param fields - An array of field names.
65
+ * @returns The current QueryBuilder instance.
66
+ */
17
67
  select(fields: string[]): this;
68
+ /**
69
+ * Adds a join clause to the query.
70
+ * @param type - The type of join (INNER, LEFT, RIGHT).
71
+ * @param table - The table to join.
72
+ * @param localKey - The local key for the join condition.
73
+ * @param foreignKey - The foreign key for the join condition.
74
+ * @returns The current QueryBuilder instance.
75
+ */
18
76
  private addJoin;
77
+ /**
78
+ * Adds an INNER JOIN clause.
79
+ * @param table - The table to join.
80
+ * @param localKey - The local key for the join condition.
81
+ * @param foreignKey - The foreign key for the join condition.
82
+ * @returns The current QueryBuilder instance.
83
+ */
19
84
  join(table: string, localKey: string, foreignKey: string): this;
85
+ /**
86
+ * Adds a LEFT JOIN clause.
87
+ * @param table - The table to join.
88
+ * @param localKey - The local key for the join condition.
89
+ * @param foreignKey - The foreign key for the join condition.
90
+ * @returns The current QueryBuilder instance.
91
+ */
20
92
  leftJoin(table: string, localKey: string, foreignKey: string): this;
93
+ /**
94
+ * Adds a RIGHT JOIN clause.
95
+ * @param table - The table to join.
96
+ * @param localKey - The local key for the join condition.
97
+ * @param foreignKey - The foreign key for the join condition.
98
+ * @returns The current QueryBuilder instance.
99
+ */
21
100
  rightJoin(table: string, localKey: string, foreignKey: string): this;
101
+ /**
102
+ * Sets the FROM clause to a subquery.
103
+ * @param sub - The QueryBuilder instance representing the subquery.
104
+ * @param alias - The alias for the subquery.
105
+ * @returns The current QueryBuilder instance.
106
+ */
22
107
  fromSubquery(sub: QueryBuilder, alias: string): this;
108
+ /**
109
+ * Adds WHERE conditions based on an object.
110
+ * @param obj - An object where keys are column names and values are the desired values.
111
+ * @returns The current QueryBuilder instance.
112
+ */
23
113
  where(obj: Record<string, any>): this;
114
+ /**
115
+ * Adds a WHERE condition with a subquery.
116
+ * @param column - The column to apply the condition to.
117
+ * @param operator - The operator ('IN' or 'NOT IN').
118
+ * @param sub - The QueryBuilder instance representing the subquery.
119
+ * @returns The current QueryBuilder instance.
120
+ */
24
121
  whereSub(column: string, operator: 'IN' | 'NOT IN', sub: QueryBuilder): this;
122
+ /**
123
+ * Sets the LIMIT for the query.
124
+ * @param limit - The maximum number of rows to return.
125
+ * @returns The current QueryBuilder instance.
126
+ */
25
127
  limit(limit: number): this;
128
+ /**
129
+ * Builds the SQL query string and its parameters without executing it.
130
+ * @returns An object containing the SQL query string and an array of parameters.
131
+ */
26
132
  build(): {
27
133
  query: string;
28
134
  params: any[];
29
135
  };
30
- execute(): Promise<any[]>;
136
+ /**
137
+ * Executes the built SQL query and returns the results.
138
+ * @returns A Promise that resolves to an array of results of type T.
139
+ * @throws Error if no QueryExecutor is provided.
140
+ */
141
+ execute(): Promise<T[]>;
31
142
  }
@@ -5,56 +5,144 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const ParamContext_1 = __importDefault(require("../core/ParamContext"));
7
7
  const ConditionBuilder_1 = __importDefault(require("./ConditionBuilder"));
8
+ /**
9
+ * A fluent SQL query builder for constructing and executing database queries.
10
+ * This builder is specifically designed for the `builders` directory and might have a different scope or feature set
11
+ * compared to other QueryBuilder implementations in the project.
12
+ * @template T The expected type of the results.
13
+ */
8
14
  class QueryBuilder {
15
+ /**
16
+ * Creates an instance of QueryBuilder.
17
+ * @param table - The name of the table to query from.
18
+ * @param executor - The query executor to use.
19
+ * @param dialect - The database dialect to use.
20
+ */
9
21
  constructor(table, executor, dialect) {
10
22
  this.executor = executor;
23
+ /**
24
+ * The fields to be selected in the query.
25
+ */
11
26
  this.fields = [];
27
+ /**
28
+ * The join clauses for the query.
29
+ */
12
30
  this.joins = [];
31
+ /**
32
+ * The fields to group by.
33
+ */
13
34
  this.groupByFields = [];
35
+ /**
36
+ * The fields to order by.
37
+ */
14
38
  this.orderByFields = [];
15
- this.ctes = [];
39
+ /**
40
+ * Common Table Expressions (CTEs) defined for the query.
41
+ */
42
+ this.ctes = []; // TODO: Define a proper type for CTEs
16
43
  this.fromClause = table;
17
44
  this.ctx = new ParamContext_1.default(dialect);
18
45
  this.condition = new ConditionBuilder_1.default(this.ctx);
19
46
  this.havingCondition = new ConditionBuilder_1.default(this.ctx);
20
47
  }
48
+ /**
49
+ * Specifies the fields to select.
50
+ * @param fields - An array of field names.
51
+ * @returns The current QueryBuilder instance.
52
+ */
21
53
  select(fields) {
22
54
  this.fields = fields;
23
55
  return this;
24
56
  }
57
+ /**
58
+ * Adds a join clause to the query.
59
+ * @param type - The type of join (INNER, LEFT, RIGHT).
60
+ * @param table - The table to join.
61
+ * @param localKey - The local key for the join condition.
62
+ * @param foreignKey - The foreign key for the join condition.
63
+ * @returns The current QueryBuilder instance.
64
+ */
25
65
  addJoin(type, table, localKey, foreignKey) {
26
66
  this.joins.push(`${type} JOIN ${table} ON ${localKey} = ${foreignKey}`);
27
67
  return this;
28
68
  }
69
+ /**
70
+ * Adds an INNER JOIN clause.
71
+ * @param table - The table to join.
72
+ * @param localKey - The local key for the join condition.
73
+ * @param foreignKey - The foreign key for the join condition.
74
+ * @returns The current QueryBuilder instance.
75
+ */
29
76
  join(table, localKey, foreignKey) {
30
77
  return this.addJoin('INNER', table, localKey, foreignKey);
31
78
  }
79
+ /**
80
+ * Adds a LEFT JOIN clause.
81
+ * @param table - The table to join.
82
+ * @param localKey - The local key for the join condition.
83
+ * @param foreignKey - The foreign key for the join condition.
84
+ * @returns The current QueryBuilder instance.
85
+ */
32
86
  leftJoin(table, localKey, foreignKey) {
33
87
  return this.addJoin('LEFT', table, localKey, foreignKey);
34
88
  }
89
+ /**
90
+ * Adds a RIGHT JOIN clause.
91
+ * @param table - The table to join.
92
+ * @param localKey - The local key for the join condition.
93
+ * @param foreignKey - The foreign key for the join condition.
94
+ * @returns The current QueryBuilder instance.
95
+ */
35
96
  rightJoin(table, localKey, foreignKey) {
36
97
  return this.addJoin('RIGHT', table, localKey, foreignKey);
37
98
  }
99
+ /**
100
+ * Sets the FROM clause to a subquery.
101
+ * @param sub - The QueryBuilder instance representing the subquery.
102
+ * @param alias - The alias for the subquery.
103
+ * @returns The current QueryBuilder instance.
104
+ */
38
105
  fromSubquery(sub, alias) {
39
106
  const { query, params } = sub.build();
40
107
  params.forEach(p => this.ctx.add(p));
41
108
  this.fromClause = `(${query}) AS ${alias}`;
42
109
  return this;
43
110
  }
111
+ /**
112
+ * Adds WHERE conditions based on an object.
113
+ * @param obj - An object where keys are column names and values are the desired values.
114
+ * @returns The current QueryBuilder instance.
115
+ */
44
116
  where(obj) {
45
117
  this.condition.where(obj);
46
118
  return this;
47
119
  }
120
+ /**
121
+ * Adds a WHERE condition with a subquery.
122
+ * @param column - The column to apply the condition to.
123
+ * @param operator - The operator ('IN' or 'NOT IN').
124
+ * @param sub - The QueryBuilder instance representing the subquery.
125
+ * @returns The current QueryBuilder instance.
126
+ */
48
127
  whereSub(column, operator, sub) {
49
128
  const { query, params } = sub.build();
50
129
  params.forEach(p => this.ctx.add(p));
51
130
  this.condition.raw(`${column} ${operator} (${query})`);
52
131
  return this;
53
132
  }
133
+ /**
134
+ * Sets the LIMIT for the query.
135
+ * @param limit - The maximum number of rows to return.
136
+ * @returns The current QueryBuilder instance.
137
+ */
54
138
  limit(limit) {
55
139
  this.limitCount = limit;
56
140
  return this;
57
141
  }
142
+ /**
143
+ * Builds the SQL query string and its parameters without executing it.
144
+ * @returns An object containing the SQL query string and an array of parameters.
145
+ */
58
146
  build() {
59
147
  const select = this.fields.length
60
148
  ? this.fields.join(', ')
@@ -75,6 +163,11 @@ class QueryBuilder {
75
163
  params: this.ctx.getParams()
76
164
  };
77
165
  }
166
+ /**
167
+ * Executes the built SQL query and returns the results.
168
+ * @returns A Promise that resolves to an array of results of type T.
169
+ * @throws Error if no QueryExecutor is provided.
170
+ */
78
171
  async execute() {
79
172
  if (!this.executor) {
80
173
  throw new Error('No QueryExecutor provided');
@@ -6,15 +6,41 @@ interface DatabaseOptions {
6
6
  dialect?: Dialect;
7
7
  defaultCacheTTL?: number;
8
8
  }
9
+ /**
10
+ * Represents a database connection and provides methods for querying and managing transactions.
11
+ */
9
12
  export default class Database {
10
13
  private executor;
11
14
  private dialect;
12
15
  private transactionManager;
13
16
  private defaultCacheTTL?;
17
+ /**
18
+ * Creates an instance of the Database.
19
+ * @param options - The options for the database connection.
20
+ */
14
21
  constructor(options: DatabaseOptions);
22
+ /**
23
+ * Creates a QueryBuilder for a specific table.
24
+ * @param name - The name of the table.
25
+ * @returns A QueryBuilder instance.
26
+ */
15
27
  table<T = any>(name: string): QueryBuilder<T>;
28
+ /**
29
+ * Executes a transaction.
30
+ * @param callback - The function to execute within the transaction. It receives a transactional Database instance.
31
+ * @returns The result of the callback function.
32
+ */
16
33
  transaction<T>(callback: (trxDb: Database) => Promise<T>): Promise<T>;
34
+ /**
35
+ * Sets the query executor.
36
+ * @param executor - The QueryExecutor instance to set.
37
+ */
17
38
  setExecutor(executor: QueryExecutor): void;
39
+ /**
40
+ * Creates a repository instance.
41
+ * @param RepoClass - The constructor of the repository class.
42
+ * @returns An instance of the repository.
43
+ */
18
44
  repository<R>(RepoClass: new (executor: QueryExecutor, dialect: Dialect) => R): R;
19
45
  }
20
46
  export {};
@@ -7,16 +7,33 @@ const PostgresDialect_1 = __importDefault(require("../dialects/PostgresDialect")
7
7
  const QueryExecutor_1 = __importDefault(require("./QueryExecutor"));
8
8
  const QueryBuilder_1 = __importDefault(require("../query/QueryBuilder"));
9
9
  const TransactionManager_1 = __importDefault(require("./TransactionManager"));
10
+ /**
11
+ * Represents a database connection and provides methods for querying and managing transactions.
12
+ */
10
13
  class Database {
14
+ /**
15
+ * Creates an instance of the Database.
16
+ * @param options - The options for the database connection.
17
+ */
11
18
  constructor(options) {
12
19
  this.dialect = options.dialect ?? new PostgresDialect_1.default();
13
20
  this.executor = new QueryExecutor_1.default(options);
14
21
  this.transactionManager = new TransactionManager_1.default(this.executor.getPool());
15
22
  this.defaultCacheTTL = options.defaultCacheTTL;
16
23
  }
24
+ /**
25
+ * Creates a QueryBuilder for a specific table.
26
+ * @param name - The name of the table.
27
+ * @returns A QueryBuilder instance.
28
+ */
17
29
  table(name) {
18
30
  return new QueryBuilder_1.default(name, this.executor, this.dialect, this.defaultCacheTTL);
19
31
  }
32
+ /**
33
+ * Executes a transaction.
34
+ * @param callback - The function to execute within the transaction. It receives a transactional Database instance.
35
+ * @returns The result of the callback function.
36
+ */
20
37
  async transaction(callback) {
21
38
  return this.transactionManager.transaction(async (trxClient) => {
22
39
  const trxExecutor = new QueryExecutor_1.default(undefined, trxClient);
@@ -29,9 +46,18 @@ class Database {
29
46
  return callback(trxDb);
30
47
  });
31
48
  }
49
+ /**
50
+ * Sets the query executor.
51
+ * @param executor - The QueryExecutor instance to set.
52
+ */
32
53
  setExecutor(executor) {
33
54
  this.executor = executor;
34
55
  }
56
+ /**
57
+ * Creates a repository instance.
58
+ * @param RepoClass - The constructor of the repository class.
59
+ * @returns An instance of the repository.
60
+ */
35
61
  repository(RepoClass) {
36
62
  return new RepoClass(this.executor, this.dialect);
37
63
  }
@@ -1,8 +1,24 @@
1
1
  import { Dialect } from "../dialects/Dialect";
2
+ /**
3
+ * Manages parameters for SQL queries, ensuring proper dialect-specific placeholders.
4
+ */
2
5
  export default class ParamContext {
3
6
  private dialect;
4
7
  private params;
8
+ /**
9
+ * Creates an instance of ParamContext.
10
+ * @param dialect - The dialect to use for generating parameter placeholders.
11
+ */
5
12
  constructor(dialect: Dialect);
13
+ /**
14
+ * Adds a value to the parameter list and returns its dialect-specific placeholder.
15
+ * @param value - The value to add.
16
+ * @returns The placeholder string for the added parameter.
17
+ */
6
18
  add(value: any): string;
19
+ /**
20
+ * Retrieves the list of accumulated parameters.
21
+ * @returns An array of parameters.
22
+ */
7
23
  getParams(): any[];
8
24
  }
@@ -1,14 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Manages parameters for SQL queries, ensuring proper dialect-specific placeholders.
5
+ */
3
6
  class ParamContext {
7
+ /**
8
+ * Creates an instance of ParamContext.
9
+ * @param dialect - The dialect to use for generating parameter placeholders.
10
+ */
4
11
  constructor(dialect) {
5
12
  this.dialect = dialect;
6
13
  this.params = [];
7
14
  }
15
+ /**
16
+ * Adds a value to the parameter list and returns its dialect-specific placeholder.
17
+ * @param value - The value to add.
18
+ * @returns The placeholder string for the added parameter.
19
+ */
8
20
  add(value) {
9
21
  this.params.push(value);
10
22
  return this.dialect.placeholder(this.params.length);
11
23
  }
24
+ /**
25
+ * Retrieves the list of accumulated parameters.
26
+ * @returns An array of parameters.
27
+ */
12
28
  getParams() {
13
29
  return this.params;
14
30
  }
@@ -2,13 +2,39 @@ import { Pool, PoolClient, QueryResult } from 'pg';
2
2
  interface ExecutorOptions {
3
3
  connectionString: string;
4
4
  }
5
+ /**
6
+ * Executes database queries using a PostgreSQL pool or client.
7
+ */
5
8
  export default class QueryExecutor {
6
9
  private pool?;
7
10
  private client?;
11
+ /**
12
+ * Creates an instance of QueryExecutor.
13
+ * @param options - Options for the executor, including the connection string.
14
+ * @param client - An optional PoolClient to use for queries (for transactions).
15
+ */
8
16
  constructor(options?: ExecutorOptions, client?: PoolClient);
17
+ /**
18
+ * Executes a SQL query.
19
+ * @param query - The SQL query string.
20
+ * @param params - An array of parameters for the query.
21
+ * @param cacheTTL - Time-to-live for caching (not currently used in this implementation).
22
+ * @returns A Promise that resolves to the QueryResult.
23
+ */
9
24
  execute(query: string, params: any[] | undefined, cacheTTL: number | undefined): Promise<QueryResult>;
25
+ /**
26
+ * Returns the underlying PostgreSQL Pool instance.
27
+ * @returns The Pool instance or undefined if not initialized with a connection string.
28
+ */
10
29
  getPool(): Pool | undefined;
30
+ /**
31
+ * Returns the underlying PostgreSQL PoolClient instance.
32
+ * @returns The PoolClient instance or undefined if not initialized with a client.
33
+ */
11
34
  getClient(): PoolClient | undefined;
35
+ /**
36
+ * Closes the database connection pool.
37
+ */
12
38
  close(): Promise<void>;
13
39
  }
14
40
  export {};
@@ -1,7 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const pg_1 = require("pg");
4
+ /**
5
+ * Executes database queries using a PostgreSQL pool or client.
6
+ */
4
7
  class QueryExecutor {
8
+ /**
9
+ * Creates an instance of QueryExecutor.
10
+ * @param options - Options for the executor, including the connection string.
11
+ * @param client - An optional PoolClient to use for queries (for transactions).
12
+ */
5
13
  constructor(options, client) {
6
14
  if (client) {
7
15
  this.client = client;
@@ -15,6 +23,13 @@ class QueryExecutor {
15
23
  }
16
24
  throw new Error('Invalid QueryExecutor initialization');
17
25
  }
26
+ /**
27
+ * Executes a SQL query.
28
+ * @param query - The SQL query string.
29
+ * @param params - An array of parameters for the query.
30
+ * @param cacheTTL - Time-to-live for caching (not currently used in this implementation).
31
+ * @returns A Promise that resolves to the QueryResult.
32
+ */
18
33
  async execute(query, params = [], cacheTTL) {
19
34
  if (this.client) {
20
35
  return this.client.query(query, params);
@@ -24,12 +39,23 @@ class QueryExecutor {
24
39
  }
25
40
  throw new Error('Executor not initialized');
26
41
  }
42
+ /**
43
+ * Returns the underlying PostgreSQL Pool instance.
44
+ * @returns The Pool instance or undefined if not initialized with a connection string.
45
+ */
27
46
  getPool() {
28
47
  return this.pool;
29
48
  }
49
+ /**
50
+ * Returns the underlying PostgreSQL PoolClient instance.
51
+ * @returns The PoolClient instance or undefined if not initialized with a client.
52
+ */
30
53
  getClient() {
31
54
  return this.client;
32
55
  }
56
+ /**
57
+ * Closes the database connection pool.
58
+ */
33
59
  async close() {
34
60
  if (this.pool) {
35
61
  await this.pool.end();
@@ -1,6 +1,19 @@
1
1
  import { Pool, PoolClient } from 'pg';
2
+ /**
3
+ * Manages database transactions, providing a method to execute a callback within a transaction.
4
+ */
2
5
  export default class TransactionManager {
3
6
  private pool;
7
+ /**
8
+ * Creates an instance of TransactionManager.
9
+ * @param pool - The PostgreSQL connection pool to use for transactions.
10
+ */
4
11
  constructor(pool: Pool);
12
+ /**
13
+ * Executes a given callback function within a database transaction.
14
+ * The transaction is committed if the callback succeeds, and rolled back if an error occurs.
15
+ * @param callback - The function to execute within the transaction. It receives a PoolClient instance.
16
+ * @returns A Promise that resolves to the result of the callback function.
17
+ */
5
18
  transaction<T>(callback: (trxClient: PoolClient) => Promise<T>): Promise<T>;
6
19
  }