@superhero/db 0.4.0 → 0.5.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.
|
@@ -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
|
@@ -21,11 +21,16 @@ class Db
|
|
|
21
21
|
|
|
22
22
|
async query(file, ...ctx)
|
|
23
23
|
{
|
|
24
|
-
|
|
25
|
-
query = await this.getQuery(file),
|
|
26
|
-
response = await this.adaptor.query(query, ...ctx)
|
|
24
|
+
let query = await this.getQuery(file)
|
|
27
25
|
|
|
28
|
-
|
|
26
|
+
for(let noEscapeReplace = query.indexOf('?%s');
|
|
27
|
+
noEscapeReplace > -1;
|
|
28
|
+
noEscapeReplace = query.indexOf('?%s'))
|
|
29
|
+
{
|
|
30
|
+
query = query.replace('?%s', ctx.shift())
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return await this.adaptor.query(query, ...ctx)
|
|
29
34
|
}
|
|
30
35
|
|
|
31
36
|
async formatQuery(file, formatCtx, sqlCtx)
|