mythix-orm-postgresql 1.6.3 → 1.7.1

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.
@@ -123,7 +123,7 @@ class PostgreSQLConnection extends SQLConnectionBase {
123
123
  return await this.query(...args);
124
124
  }
125
125
 
126
- async query(_sql, _options) {
126
+ async query(_sql, _options, _client) {
127
127
  let sql = _sql;
128
128
  if (!sql)
129
129
  return;
@@ -135,15 +135,15 @@ class PostgreSQLConnection extends SQLConnectionBase {
135
135
 
136
136
  let options = _options || {};
137
137
  let logger = options.logger || (this.getOptions().logger);
138
- let inTransaction = false;
138
+ let hasOwnClient;
139
139
  let client;
140
140
 
141
141
  try {
142
- client = options.connection || this.inTransaction;
143
- if (!client)
142
+ client = _client || this.inTransaction;
143
+ if (!client) {
144
144
  client = await this.pool.connect();
145
- else
146
- inTransaction = true;
145
+ hasOwnClient = true;
146
+ }
147
147
 
148
148
  if (logger && sql.text)
149
149
  console.log('QUERY: ', sql.text);
@@ -166,25 +166,25 @@ class PostgreSQLConnection extends SQLConnectionBase {
166
166
 
167
167
  throw error;
168
168
  } finally {
169
- if (client && !inTransaction)
169
+ if (client && hasOwnClient)
170
170
  await client.release();
171
171
  }
172
172
  }
173
173
 
174
174
  async transaction(callback, _options) {
175
175
  let options = _options || {};
176
- let inheritedThis = Object.create(options.connection || this);
177
- let lockMode = this.getLockMode(options.lock);
176
+ let inheritedThis = Object.create(options.connection || this.getContextValue('connection', this));
177
+ let lockMode = inheritedThis.getLockMode(options.lock);
178
178
  let savePointName;
179
179
  let client;
180
180
  let lockStatement;
181
181
 
182
182
  if (lockMode && lockMode.lock) {
183
- let Model = this.getModel(lockMode.modelName);
183
+ let Model = inheritedThis.getModel(lockMode.modelName);
184
184
  if (!Model)
185
- throw new Error(`${this.constructor.name}::transaction: Request to lock table defined by model "${lockMode.modelName}", but model with that name can not be found.`);
185
+ throw new Error(`${inheritedThis.constructor.name}::transaction: Request to lock table defined by model "${lockMode.modelName}", but model with that name can not be found.`);
186
186
 
187
- let escapedTableName = this.escapeID(Model.getTableName());
187
+ let escapedTableName = inheritedThis.escapeID(Model.getTableName());
188
188
  let lockModeStr = (lockMode.mode) ? lockMode.mode : 'ACCESS EXCLUSIVE';
189
189
 
190
190
  if (!lockMode.mode) {
@@ -198,46 +198,42 @@ class PostgreSQLConnection extends SQLConnectionBase {
198
198
  }
199
199
 
200
200
  if (!inheritedThis.inTransaction) {
201
- client = options.connection;
202
- if (!client)
203
- client = await this.pool.connect();
204
-
205
- inheritedThis.inTransaction = client;
201
+ client = inheritedThis.inTransaction = await inheritedThis.pool.connect();
206
202
 
207
203
  try {
208
- await inheritedThis.query(`BEGIN${(options.beginArguments) ? ` ${options.beginArguments}` : ''}`, options);
204
+ await inheritedThis.query(`BEGIN${(options.beginArguments) ? ` ${options.beginArguments}` : ''}`, options, client);
209
205
  if (lockStatement)
210
- await inheritedThis.query(lockStatement, options);
206
+ await inheritedThis.query(lockStatement, options, client);
211
207
 
212
208
  // TODO: Need to handle "busy" error
213
209
  } catch (error) {
214
- if (!options.connection)
215
- await client.release();
216
-
210
+ await client.release();
217
211
  throw error;
218
212
  }
219
213
  } else {
220
- savePointName = this.generateSavePointName();
214
+ client = inheritedThis.inTransaction;
215
+
216
+ savePointName = inheritedThis.generateSavePointName();
221
217
  inheritedThis.savePointName = savePointName;
222
218
  inheritedThis.isSavePoint = true;
223
219
 
224
- await inheritedThis.query(`SAVEPOINT ${savePointName}`, options);
220
+ await inheritedThis.query(`SAVEPOINT ${savePointName}`, options, client);
225
221
  }
226
222
 
227
223
  try {
228
- let result = await callback.call(inheritedThis, inheritedThis);
224
+ let result = await inheritedThis.createContext(callback, inheritedThis, inheritedThis);
229
225
 
230
226
  if (savePointName)
231
- await inheritedThis.query(`RELEASE SAVEPOINT ${savePointName}`, options);
227
+ await inheritedThis.query(`RELEASE SAVEPOINT ${savePointName}`, options, client);
232
228
  else
233
- await inheritedThis.query('COMMIT', options);
229
+ await inheritedThis.query('COMMIT', options, client);
234
230
 
235
231
  return result;
236
232
  } catch (error) {
237
233
  if (savePointName)
238
- await inheritedThis.query(`ROLLBACK TO SAVEPOINT ${savePointName}`, options);
239
- else if (inheritedThis.inTransaction)
240
- await inheritedThis.query('ROLLBACK', options);
234
+ await inheritedThis.query(`ROLLBACK TO SAVEPOINT ${savePointName}`, options, client);
235
+ else
236
+ await inheritedThis.query('ROLLBACK', options, client);
241
237
 
242
238
  throw error;
243
239
  } finally {
@@ -294,6 +290,7 @@ class PostgreSQLConnection extends SQLConnectionBase {
294
290
  return 'INTEGER';
295
291
  }
296
292
 
293
+ // eslint-disable-next-line no-unused-vars
297
294
  _blobTypeToString(type) {
298
295
  return 'BYTEA';
299
296
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mythix-orm-postgresql",
3
- "version": "1.6.3",
3
+ "version": "1.7.1",
4
4
  "description": "PostgreSQL driver for Mythix ORM",
5
5
  "main": "lib/index.js",
6
6
  "type": "commonjs",
@@ -33,8 +33,8 @@
33
33
  },
34
34
  "homepage": "https://github.com/th317erd/mythix-orm-postgresql#readme",
35
35
  "peerDependencies": {
36
- "mythix-orm": "^1.7.2",
37
- "mythix-orm-sql-base": "^1.6.3"
36
+ "mythix-orm": "^1.8.1",
37
+ "mythix-orm-sql-base": "^1.7.1"
38
38
  },
39
39
  "dependencies": {
40
40
  "luxon": "^3.0.4",