@superhero/db 0.3.0 → 0.5.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.
@@ -0,0 +1,10 @@
1
+ class NestedTransactionsNotAllowed extends Error
2
+ {
3
+ constructor(...args)
4
+ {
5
+ super(...args)
6
+ this.code = 'E_MYSQL2_NESTED_TRANSACTIONS_UNSUPPORTED'
7
+ }
8
+ }
9
+
10
+ module.exports = NestedTransactionsNotAllowed
@@ -0,0 +1,12 @@
1
+ const AdapterMySql2 = require('.')
2
+
3
+ class AdapterMySql2Factory
4
+ {
5
+ create(mysql2, config)
6
+ {
7
+ const pool = mysql2.createPool(config)
8
+ return new AdapterMySql2(pool)
9
+ }
10
+ }
11
+
12
+ module.exports = AdapterMySql2Factory
@@ -0,0 +1,51 @@
1
+ const AdapterMySql2Transaction = require('./transaction')
2
+
3
+ class AdapterMySql2
4
+ {
5
+ constructor(pool)
6
+ {
7
+ this.pool = pool
8
+ }
9
+
10
+ query(query, ...ctx)
11
+ {
12
+ const resolve = (accept, reject, i = 0) =>
13
+ this.pool.query(query, ctx, (error, response) =>
14
+ error
15
+ ? error.code === 'ETIMEDOUT' && i < 3
16
+ ? resolve(accept, reject, ++i)
17
+ : reject(error)
18
+ : accept(response))
19
+
20
+ return new Promise(resolve)
21
+ }
22
+
23
+ getConnection()
24
+ {
25
+ const resolve = (accept, reject, i = 0) =>
26
+ this.pool.getConnection((error, connection) =>
27
+ error
28
+ ? error.code === 'ETIMEDOUT' && i < 3
29
+ ? resolve(accept, reject, ++i)
30
+ : reject(error)
31
+ : accept(connection))
32
+
33
+ return new Promise(resolve)
34
+ }
35
+
36
+ async createTransaction()
37
+ {
38
+ const connection = await this.getConnection()
39
+ await connection.query('START TRANSACTION')
40
+ const transaction = new AdapterMySql2Transaction(connection)
41
+
42
+ return transaction
43
+ }
44
+
45
+ close()
46
+ {
47
+ this.pool.end()
48
+ }
49
+ }
50
+
51
+ module.exports = AdapterMySql2
@@ -0,0 +1,44 @@
1
+ const NestedTransactionsNotAllowed = require('./error/nested-transactions-not-allowed')
2
+
3
+ class AdapterMySql2Transaction
4
+ {
5
+ constructor(connection)
6
+ {
7
+ this.connection = connection
8
+ }
9
+
10
+ createTransaction()
11
+ {
12
+ throw new NestedTransactionsNotAllowed('Nested transactions are not allowed')
13
+ }
14
+
15
+ query(...args)
16
+ {
17
+ return this._query(...args)
18
+ }
19
+
20
+ // We are forced to have a different query function to be able to use it from inside
21
+ // the class without the Proxy making interferences
22
+ async _query(query, ...ctx)
23
+ {
24
+ return new Promise((accept, reject) =>
25
+ this.connection.query(query, ...ctx, (error, response) =>
26
+ error
27
+ ? reject(error)
28
+ : accept(response)))
29
+ }
30
+
31
+ async commit()
32
+ {
33
+ await this._query('COMMIT')
34
+ this.connection.release()
35
+ }
36
+
37
+ async rollback()
38
+ {
39
+ await this._query('ROLLBACK')
40
+ this.connection.release()
41
+ }
42
+ }
43
+
44
+ module.exports = AdapterMySql2Transaction
package/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  const
2
- util = require('util'),
3
- fs = require('fs'),
4
- promisify = util.promisify,
5
- readFile = promisify(fs.readFile)
2
+ util = require('util'),
3
+ fs = require('fs'),
4
+ promisify = util.promisify,
5
+ readFile = promisify(fs.readFile)
6
6
 
7
7
  class Db
8
8
  {
@@ -22,8 +22,18 @@ class Db
22
22
  async query(file, ...ctx)
23
23
  {
24
24
  const
25
- query = await this.getQuery(file),
26
- response = await this.adaptor.query(query, ...ctx)
25
+ query = await this.getQuery(file),
26
+ response = await this.adaptor.query(query, ...ctx)
27
+
28
+ return response
29
+ }
30
+
31
+ async formatQuery(file, formatCtx, sqlCtx)
32
+ {
33
+ const
34
+ query = await this.getQuery(file),
35
+ formattedQuery = util.format(query, ...formatCtx),
36
+ response = await this.adaptor.query(formattedQuery, ...sqlCtx)
27
37
 
28
38
  return response
29
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superhero/db",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "Db interaction, designed to separate queries from the source code",
5
5
  "repository": "git@github.com:superhero/js.db.git",
6
6
  "main": "index.js",