mythix-orm-postgresql 1.2.1 → 1.3.0

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.
@@ -174,8 +174,28 @@ class PostgreSQLConnection extends SQLConnectionBase {
174
174
  async transaction(callback, _options) {
175
175
  let options = _options || {};
176
176
  let inheritedThis = Object.create(options.connection || this);
177
+ let lockMode = this.getLockMode(options.lock);
177
178
  let savePointName;
178
179
  let client;
180
+ let lockStatement;
181
+
182
+ if (lockMode && lockMode.lock) {
183
+ let Model = this.getModel(lockMode.modelName);
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.`);
186
+
187
+ let escapedTableName = this.escapeID(Model.getTableName());
188
+ let lockModeStr = (lockMode.mode) ? lockMode.mode : 'ACCESS EXCLUSIVE';
189
+
190
+ if (!lockMode.mode) {
191
+ if (lockMode.read === true && lockMode.write === false)
192
+ lockModeStr = 'EXCLUSIVE';
193
+ else if (lockMode.read === false || lockMode.write === false)
194
+ lockModeStr = 'ACCESS SHARE';
195
+ }
196
+
197
+ lockStatement = `LOCK ${(lockMode.dependents === false) ? 'ONLY ' : 'TABLE '} ${escapedTableName} IN ${lockModeStr} MODE${(lockMode.noWait === true) ? ' NOWAIT' : ''}`;
198
+ }
179
199
 
180
200
  if (!inheritedThis.inTransaction) {
181
201
  client = options.connection;
@@ -185,7 +205,11 @@ class PostgreSQLConnection extends SQLConnectionBase {
185
205
  inheritedThis.inTransaction = client;
186
206
 
187
207
  try {
188
- await inheritedThis.query(`BEGIN${(options.mode) ? ` ${options.mode}` : ' DEFERRABLE'}`);
208
+ await inheritedThis.query(`BEGIN${(options.beginArguments) ? ` ${options.beginArguments}` : ''}`, options);
209
+ if (lockStatement)
210
+ await inheritedThis.query(lockStatement, options);
211
+
212
+ // TODO: Need to handle "busy" error
189
213
  } catch (error) {
190
214
  if (!options.connection)
191
215
  await client.release();
@@ -197,23 +221,23 @@ class PostgreSQLConnection extends SQLConnectionBase {
197
221
  inheritedThis.savePointName = savePointName;
198
222
  inheritedThis.isSavePoint = true;
199
223
 
200
- await inheritedThis.query(`SAVEPOINT ${savePointName}`);
224
+ await inheritedThis.query(`SAVEPOINT ${savePointName}`, options);
201
225
  }
202
226
 
203
227
  try {
204
228
  let result = await callback.call(inheritedThis, inheritedThis);
205
229
 
206
230
  if (savePointName)
207
- await inheritedThis.query(`RELEASE SAVEPOINT ${savePointName}`);
231
+ await inheritedThis.query(`RELEASE SAVEPOINT ${savePointName}`, options);
208
232
  else
209
- await inheritedThis.query('COMMIT');
233
+ await inheritedThis.query('COMMIT', options);
210
234
 
211
235
  return result;
212
236
  } catch (error) {
213
237
  if (savePointName)
214
- await inheritedThis.query(`ROLLBACK TO SAVEPOINT ${savePointName}`);
238
+ await inheritedThis.query(`ROLLBACK TO SAVEPOINT ${savePointName}`, options);
215
239
  else if (inheritedThis.inTransaction)
216
- await inheritedThis.query('ROLLBACK');
240
+ await inheritedThis.query('ROLLBACK', options);
217
241
 
218
242
  throw error;
219
243
  } finally {
@@ -236,7 +260,7 @@ class PostgreSQLConnection extends SQLConnectionBase {
236
260
  let queryGenerator = this.getQueryGenerator();
237
261
  let sqlStatement = queryGenerator.generateTruncateTableStatement(Model, options);
238
262
 
239
- return await this.query(sqlStatement);
263
+ return await this.query(sqlStatement, options);
240
264
  }
241
265
 
242
266
  _intTypeToSerial(type) {
@@ -145,6 +145,17 @@ class PostgreSQLQueryGenerator extends SQLQueryGeneratorBase {
145
145
 
146
146
  return `TRUNCATE TABLE${(options.onlySpecifiedTable === true) ? ' ONLY' : ''} ${escapedTableName}${(options.continueIdentity === true) ? ' CONTINUE IDENTITY' : ' RESTART IDENTITY'}${(options.cascade === false) ? ' RESTRICT' : ' CASCADE'}`;
147
147
  }
148
+
149
+ generateSelectQueryOperatorFromQueryEngineOperator(queryPart, operator, value, valueIsReference, options) {
150
+ let sqlOperator = super.generateSelectQueryOperatorFromQueryEngineOperator(queryPart, operator, value, valueIsReference, options);
151
+
152
+ if (sqlOperator === 'LIKE' && queryPart.caseSensitive !== true)
153
+ sqlOperator = 'ILIKE';
154
+ else if (sqlOperator === 'NOT LIKE' && queryPart.caseSensitive !== true)
155
+ sqlOperator = 'NOT ILIKE';
156
+
157
+ return sqlOperator;
158
+ }
148
159
  }
149
160
 
150
161
  module.exports = PostgreSQLQueryGenerator;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mythix-orm-postgresql",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
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.3.0",
37
- "mythix-orm-sql-base": "^1.1.1"
36
+ "mythix-orm": "^1.4.4",
37
+ "mythix-orm-sql-base": "^1.3.0"
38
38
  },
39
39
  "dependencies": {
40
40
  "nife": "^1.11.3",