stabilize-orm 1.1.6 → 1.1.7

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 (2) hide show
  1. package/client.ts +42 -33
  2. package/package.json +1 -1
package/client.ts CHANGED
@@ -103,41 +103,50 @@ export class DBClient {
103
103
  * const users = await dbClient.query('SELECT * FROM users WHERE status = ?', ['active']);
104
104
  * ```
105
105
  */
106
- async query<T>(query: string, params: any[] = []): Promise<T[]> {
107
- const start = Date.now();
108
- this.logger.logQuery(query, params);
109
-
110
- for (let attempt = 1; attempt <= this.retryAttempts; attempt++) {
111
- try {
112
- let result: any;
113
-
114
- if (this.client instanceof Database) { // SQLite
115
- let stmt = this.preparedStatements.get(query);
116
- if (!stmt) {
117
- stmt = this.client.prepare(query);
118
- this.preparedStatements.set(query, stmt);
119
- }
120
- result = stmt.all(...params);
121
- } else if (isMySQLPool(this.client) || ('query' in this.client && 'release' in this.client)) { // mysql2 Pool or Connection
122
- const [rows] = await (this.client as mysql.Pool).query(query, params);
123
- result = rows;
124
- } else { // Postgres Pool or Client
125
- const pgQuery = query.replace(/\?/g, (_, i) => `$${i + 1}`);
126
- const pgResult = await (this.client as Pool).query(pgQuery, params);
127
- result = pgResult.rows;
106
+
107
+ async query<T>(query: string, params: any[] = []): Promise<T[]> {
108
+ const start = Date.now();
109
+
110
+ for (let attempt = 1; attempt <= this.retryAttempts; attempt++) {
111
+ try {
112
+ let result: any;
113
+
114
+ // Log the query before execution
115
+ this.logger.logQuery(query, params);
116
+
117
+ if (this.client instanceof Database) { // SQLite
118
+ let stmt = this.preparedStatements.get(query);
119
+ if (!stmt) {
120
+ stmt = this.client.prepare(query);
121
+ this.preparedStatements.set(query, stmt);
122
+ }
123
+ result = stmt.all(...params);
124
+ } else if (isMySQLPool(this.client) || ('query' in this.client && 'release' in this.client && !(this.client instanceof Pool))) { // mysql2 Pool or Connection
125
+ const [rows] = await (this.client as mysql.Pool).query(query, params);
126
+ result = rows;
127
+ } else { // Postgres Pool or Client
128
+
129
+ // Correctly replace '?' with '$1', '$2', etc.
130
+ let paramIndex = 0;
131
+ const pgQuery = query.replace(/\?/g, () => `$${++paramIndex}`);
132
+ const pgResult = await (this.client as Pool).query(pgQuery, params);
133
+ result = pgResult.rows;
134
+ }
135
+
136
+ const executionTime = Date.now() - start;
137
+ this.logger.logQuery(query, params, executionTime);
138
+ return result as T[];
139
+ } catch (error) {
140
+ this.logger.logError(error as Error);
141
+ if (attempt === this.retryAttempts) {
142
+ throw new StabilizeError(`Query failed after ${this.retryAttempts} attempts: ${(error as Error).message}`, "QUERY_ERROR");
143
+ }
144
+ await new Promise(res => setTimeout(res, this.retryDelay * Math.pow(2, attempt - 1) + this.getJitter()));
145
+ }
128
146
  }
129
-
130
- const executionTime = Date.now() - start;
131
- this.logger.logQuery(query, params, executionTime);
132
- return result as T[];
133
- } catch (error) {
134
- this.logger.logError(error as Error);
135
- if (attempt === this.retryAttempts) throw new StabilizeError(`Query failed: ${(error as Error).message}`, "QUERY_ERROR");
136
- await new Promise(res => setTimeout(res, this.retryDelay * Math.pow(2, attempt - 1) + this.getJitter()));
137
- }
147
+ // This line should theoretically be unreachable if retryAttempts >= 1
148
+ throw new StabilizeError("Query failed: maximum retries reached without success", "QUERY_ERROR");
138
149
  }
139
- throw new StabilizeError("Query failed: no attempts made", "QUERY_ERROR");
140
- }
141
150
 
142
151
  /**
143
152
  * Executes a series of database operations within a single atomic transaction.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stabilize-orm",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "description": "A lightweight, type-safe ORM for Bun.js with support for SQLite, MySQL, PostgreSQL, and Redis caching",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",