velocious 1.0.56 → 1.0.58

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.
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "velocious": "bin/velocious.js"
4
4
  },
5
5
  "name": "velocious",
6
- "version": "1.0.56",
6
+ "version": "1.0.58",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "test": "VELOCIOUS_TEST_DIR=../ cd spec/dummy && npx velocious test",
@@ -29,6 +29,7 @@
29
29
  "diggerize": "^1.0.5",
30
30
  "ejs": "^3.1.6",
31
31
  "env-sense": "^1.0.0",
32
+ "epic-locks": "^1.0.3",
32
33
  "escape-string-regexp": "^1.0.5",
33
34
  "incorporator": "^1.0.2",
34
35
  "inflection": "^3.0.0",
@@ -1,6 +1,7 @@
1
1
  import {Logger} from "../../logger.js"
2
2
  import Query from "../query/index.js"
3
3
  import Handler from "../handler.js"
4
+ import Mutex from "epic-locks/src/mutex.js"
4
5
  import strftime from "strftime"
5
6
  import UUID from "pure-uuid"
6
7
  import TableData from "../table-data/index.js"
@@ -11,8 +12,10 @@ export default class VelociousDatabaseDriversBase {
11
12
  constructor(config, configuration) {
12
13
  this._args = config
13
14
  this.configuration = configuration
15
+ this.mutex = new Mutex() // Can be used to lock this instance for exclusive use
14
16
  this.logger = new Logger(this)
15
17
  this._transactionsCount = 0
18
+ this._transactionsActionsMutex = new Mutex()
16
19
  }
17
20
 
18
21
  async addForeignKey(tableName, columnName, referencedTableName, referencedColumnName, args) {
@@ -206,18 +209,36 @@ export default class VelociousDatabaseDriversBase {
206
209
  }
207
210
 
208
211
  async startTransaction() {
212
+ await this._transactionsActionsMutex.sync(async () => {
213
+ await this._startTransactionAction()
214
+ this._transactionsCount++
215
+ })
216
+ }
217
+
218
+ async _startTransactionAction() {
209
219
  await this.query("BEGIN TRANSACTION")
210
- this._transactionsCount++
211
220
  }
212
221
 
213
222
  async commitTransaction() {
223
+ await this._transactionsActionsMutex.sync(async () => {
224
+ await this._commitTransactionAction()
225
+ this._transactionsCount--
226
+ })
227
+ }
228
+
229
+ async _commitTransactionAction() {
214
230
  await this.query("COMMIT")
215
- this._transactionsCount--
216
231
  }
217
232
 
218
233
  async rollbackTransaction() {
234
+ await this._transactionsActionsMutex.sync(async () => {
235
+ await this._rollbackTransactionAction()
236
+ this._transactionsCount--
237
+ })
238
+ }
239
+
240
+ async _rollbackTransactionAction() {
219
241
  await this.query("ROLLBACK")
220
- this._transactionsCount--
221
242
  }
222
243
 
223
244
  generateSavePointName() {
@@ -225,6 +246,12 @@ export default class VelociousDatabaseDriversBase {
225
246
  }
226
247
 
227
248
  async startSavePoint(savePointName) {
249
+ await this._transactionsActionsMutex.sync(async () => {
250
+ await this._startSavePointAction(savePointName)
251
+ })
252
+ }
253
+
254
+ async _startSavePointAction(savePointName) {
228
255
  await this.query(`SAVEPOINT ${savePointName}`)
229
256
  }
230
257
 
@@ -245,10 +272,22 @@ export default class VelociousDatabaseDriversBase {
245
272
  }
246
273
 
247
274
  async releaseSavePoint(savePointName) {
275
+ await this._transactionsActionsMutex.sync(async () => {
276
+ await this._releaseSavePointAction(savePointName)
277
+ })
278
+ }
279
+
280
+ async _releaseSavePointAction(savePointName) {
248
281
  await this.query(`RELEASE SAVEPOINT ${savePointName}`)
249
282
  }
250
283
 
251
284
  async rollbackSavePoint(savePointName) {
285
+ await this._transactionsActionsMutex.sync(async () => {
286
+ await this._rollbackSavePointAction(savePointName)
287
+ })
288
+ }
289
+
290
+ async _rollbackSavePointAction(savePointName) {
252
291
  await this.query(`ROLLBACK TO SAVEPOINT ${savePointName}`)
253
292
  }
254
293
 
@@ -203,42 +203,39 @@ export default class VelociousDatabaseDriversMssql extends Base{
203
203
  return this._options
204
204
  }
205
205
 
206
- async startTransaction() {
206
+ async _startTransactionAction() {
207
207
  if (!this.connection) throw new Error("No connection")
208
208
  if (this._currentTransaction) throw new Error("A transaction is already running")
209
209
 
210
210
  this._currentTransaction = new mssql.Transaction(this.connection)
211
211
 
212
212
  await this._currentTransaction.begin()
213
- this._transactionsCount++
214
213
  }
215
214
 
216
- async commitTransaction() {
215
+ async _commitTransactionAction() {
217
216
  if (!this._currentTransaction) throw new Error("A transaction isn't running")
218
217
 
219
218
  await this._currentTransaction.commit()
220
219
  this._currentTransaction = null
221
- this._transactionsCount--
222
220
  }
223
221
 
224
- async rollbackTransaction() {
222
+ async _rollbackTransactionAction() {
225
223
  if (!this._currentTransaction) throw new Error("A transaction isn't running")
226
224
 
227
225
  await this._currentTransaction.rollback()
228
226
 
229
227
  this._currentTransaction = null
230
- this._transactionsCount--
231
228
  }
232
229
 
233
- async startSavePoint(savePointName) {
230
+ async _startSavePointAction(savePointName) {
234
231
  await this.query(`SAVE TRANSACTION [${savePointName}]`)
235
232
  }
236
233
 
237
- async releaseSavePoint(savePointName) {
234
+ async _releaseSavePointAction(savePointName) {
238
235
  // Do nothing in MS-SQL.
239
236
  }
240
237
 
241
- async rollbackSavePoint(savePointName) {
238
+ async _rollbackSavePointAction(savePointName) {
242
239
  await this.query(`ROLLBACK TRANSACTION [${savePointName}]`)
243
240
  }
244
241
 
@@ -161,9 +161,8 @@ export default class VelociousDatabaseDriversMysql extends Base{
161
161
  return this._options
162
162
  }
163
163
 
164
- async startTransaction() {
164
+ async _startTransactionAction() {
165
165
  await this.query("START TRANSACTION")
166
- this._transactionsCount++
167
166
  }
168
167
 
169
168
  updateSql({conditions, data, tableName}) {
@@ -174,9 +174,8 @@ export default class VelociousDatabaseDriversPgsql extends Base{
174
174
  return this._options
175
175
  }
176
176
 
177
- async startTransaction() {
177
+ async _startTransactionAction() {
178
178
  await this.query("START TRANSACTION")
179
- this._transactionsCount++
180
179
  }
181
180
 
182
181
  updateSql({conditions, data, tableName}) {