@superhero/db 0.0.2 → 0.4.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.
package/LICENCE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2018 Erik Landvall
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ this software and associated documentation files (the "Software"), to deal in
6
+ the Software without restriction, including without limitation the rights to
7
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8
+ of the Software, and to permit persons to whom the Software is furnished to do
9
+ so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
package/README.md CHANGED
@@ -23,22 +23,45 @@ A simple DB interface implementation that reads queries from a file, segregating
23
23
  }
24
24
  ```
25
25
 
26
- ## Example
26
+ ## Example | MySql
27
27
 
28
28
  ```js
29
29
  const
30
- connections = 5,
31
- host = 'mysql.example.com',
32
- user = 'root',
33
- pass = 'b4real',
34
- filePath = '/sql',
35
- fileSuffix = '.sql',
36
- mysql = require('mysql'),
37
- Db = require('@superhero/db'),
38
- Adaptor = require('@superhero/db/adaptor/mysql'),
39
- adaptor = Adapter.from(mysql, { connections, host, user, password }),
40
- db = new Db(adaptor, filePath, fileSuffix),
41
- result = await db.query('file', ['context'])
30
+ connections = 5,
31
+ host = 'mysql.example.com',
32
+ user = 'root',
33
+ password = 'b4real',
34
+ filePath = '/sql',
35
+ fileSuffix = '.sql',
36
+ mysql = require('mysql'),
37
+ Db = require('@superhero/db'),
38
+ AdapterFactory = require('@superhero/db/adapter/mysql/factory'),
39
+ adapterFactory = new AdapterFactory(),
40
+ adapter = adapterFactory.create(mysql, { connections, host, user, password }),
41
+ db = new Db(adapter, filePath, fileSuffix),
42
+ result = await db.query('file', ['context'])
42
43
  ```
43
44
 
44
45
  The example above will create a pool with 5 idle connections and query the database with the content from the `/sql/file.sql` file composed with the specified context.
46
+
47
+
48
+ ## Example | Postgres
49
+
50
+ ```js
51
+ const
52
+ host = 'postgres.example.com',
53
+ user = 'root',
54
+ database = 'stuff',
55
+ password = 'b4real',
56
+ filePath = '/sql',
57
+ fileSuffix = '.sql',
58
+ pg = require('pg'),
59
+ Db = require('@superhero/db'),
60
+ AdapterFactory = require('@superhero/db/adapter/postgres/factory'),
61
+ adapterFactory = new AdapterFactory(),
62
+ adapter = adapterFactory.create(pg, { host, user, password, database }),
63
+ db = new Db(adapter, filePath, fileSuffix),
64
+ result = await db.query('file', ['context'])
65
+ ```
66
+
67
+ The example above will create a pool and query the database with the content from the `/sql/file.sql` file composed with the specified context.
@@ -0,0 +1,11 @@
1
+ const AdapterInfluxDb = require('.')
2
+
3
+ class AdapterInfluxDbFactory
4
+ {
5
+ create(influxdb, escape)
6
+ {
7
+ return new AdapterInfluxDb(influxdb, escape)
8
+ }
9
+ }
10
+
11
+ module.exports = AdapterInfluxDbFactory
@@ -1,6 +1,6 @@
1
1
  const TransactionsNotAllowed = require('./error/transactions-not-allowed')
2
2
 
3
- class AdaptorInfluxDb
3
+ class AdapterInfluxDb
4
4
  {
5
5
  constructor(influxdb, escape)
6
6
  {
@@ -29,6 +29,11 @@ class AdaptorInfluxDb
29
29
  {
30
30
  throw new TransactionsNotAllowed('InfluxDb transactions are not available')
31
31
  }
32
+
33
+ close()
34
+ {
35
+ // no need to close an http request, which influxdb is based on...
36
+ }
32
37
  }
33
38
 
34
- module.exports = AdaptorInfluxDb
39
+ module.exports = AdapterInfluxDb
@@ -0,0 +1,12 @@
1
+ const AdapterMySql = require('.')
2
+
3
+ class AdapterMySqlFactory
4
+ {
5
+ create(mysql, config)
6
+ {
7
+ const pool = mysql.createPool(config)
8
+ return new AdapterMySql(pool)
9
+ }
10
+ }
11
+
12
+ module.exports = AdapterMySqlFactory
@@ -0,0 +1,51 @@
1
+ const AdapterMySqlTransaction = require('./transaction')
2
+
3
+ class AdapterMySql
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 AdapterMySqlTransaction(connection)
41
+
42
+ return transaction
43
+ }
44
+
45
+ close()
46
+ {
47
+ this.pool.end()
48
+ }
49
+ }
50
+
51
+ module.exports = AdapterMySql
@@ -1,6 +1,6 @@
1
1
  const NestedTransactionsNotAllowed = require('./error/nested-transactions-not-allowed')
2
2
 
3
- class AdaptorMySqlTransaction
3
+ class AdapterMySqlTransaction
4
4
  {
5
5
  constructor(connection)
6
6
  {
@@ -41,4 +41,4 @@ class AdaptorMySqlTransaction
41
41
  }
42
42
  }
43
43
 
44
- module.exports = AdaptorMySqlTransaction
44
+ module.exports = AdapterMySqlTransaction
@@ -0,0 +1,10 @@
1
+ class NestedTransactionsNotAllowed extends Error
2
+ {
3
+ constructor(...args)
4
+ {
5
+ super(...args)
6
+ this.code = 'E_POSTGRES_NESTED_TRANSACTIONS_UNSUPPORTED'
7
+ }
8
+ }
9
+
10
+ module.exports = NestedTransactionsNotAllowed
@@ -0,0 +1,12 @@
1
+ const AdapterPostgres = require('.')
2
+
3
+ class AdapterPostgresFactory
4
+ {
5
+ create(pg, config)
6
+ {
7
+ const pool = new pg.Pool(config)
8
+ return new AdapterPostgres(pool)
9
+ }
10
+ }
11
+
12
+ module.exports = AdapterPostgresFactory
@@ -0,0 +1,35 @@
1
+ const AdapterPostgresTransaction = require('./transaction')
2
+
3
+ class AdapterPostgres
4
+ {
5
+ constructor(pool)
6
+ {
7
+ this.pool = pool
8
+ }
9
+
10
+ query(...args)
11
+ {
12
+ return this.pool.query(...args)
13
+ }
14
+
15
+ getConnection()
16
+ {
17
+ return this.pool.connect()
18
+ }
19
+
20
+ async createTransaction()
21
+ {
22
+ const connection = await this.getConnection()
23
+ await connection.query('BEGIN')
24
+ const transaction = new AdapterPostgresTransaction(connection)
25
+
26
+ return transaction
27
+ }
28
+
29
+ close()
30
+ {
31
+ this.pool.end()
32
+ }
33
+ }
34
+
35
+ module.exports = AdapterPostgres
@@ -0,0 +1,33 @@
1
+ const NestedTransactionsNotAllowed = require('./error/nested-transactions-not-allowed')
2
+
3
+ class AdapterPostgresTransaction
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.connection.query(...args)
18
+ }
19
+
20
+ async commit()
21
+ {
22
+ await this.connection.query('COMMIT')
23
+ this.connection.release()
24
+ }
25
+
26
+ async rollback()
27
+ {
28
+ await this.connection.query('ROLLBACK')
29
+ this.connection.release()
30
+ }
31
+ }
32
+
33
+ module.exports = AdapterPostgresTransaction
@@ -0,0 +1,12 @@
1
+ const AdapterRedis = require('.')
2
+
3
+ class AdapterRedisFactory
4
+ {
5
+ create(redis, options)
6
+ {
7
+ const client = redis.createClient(options)
8
+ return new AdapterRedis(client)
9
+ }
10
+ }
11
+
12
+ module.exports = AdapterRedisFactory
@@ -1,6 +1,6 @@
1
1
  const TransactionsNotAllowed = require('./error/transactions-not-allowed')
2
2
 
3
- class AdaptorRedis
3
+ class AdapterRedis
4
4
  {
5
5
  constructor(redis)
6
6
  {
@@ -20,6 +20,11 @@ class AdaptorRedis
20
20
  {
21
21
  throw new TransactionsNotAllowed('Redis transactions are not available')
22
22
  }
23
+
24
+ close()
25
+ {
26
+ this.redis.end()
27
+ }
23
28
  }
24
29
 
25
- module.exports = AdaptorRedis
30
+ module.exports = AdapterRedis
package/index.js CHANGED
@@ -1,10 +1,10 @@
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
- module.exports = class
7
+ class Db
8
8
  {
9
9
  constructor(adaptor, queryPath, fileSuffix = '')
10
10
  {
@@ -14,11 +14,26 @@ module.exports = class
14
14
  this.queries = {}
15
15
  }
16
16
 
17
+ async close()
18
+ {
19
+ await this.adaptor.close()
20
+ }
21
+
17
22
  async query(file, ...ctx)
18
23
  {
19
24
  const
20
- query = await this.getQuery(file),
21
- 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)
22
37
 
23
38
  return response
24
39
  }
@@ -53,3 +68,5 @@ module.exports = class
53
68
  return query.toString()
54
69
  }
55
70
  }
71
+
72
+ module.exports = Db
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superhero/db",
3
- "version": "0.0.2",
3
+ "version": "0.4.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",
@@ -11,7 +11,6 @@
11
11
  "url": "http://erik.landvall.se"
12
12
  },
13
13
  "scripts": {
14
- "prepublishOnly": "npm run-script generate-doc-coverage && npm run-script generate-doc-tests",
15
14
  "generate-doc-coverage": "nyc report --reporter=html --report-dir=./docs/coverage",
16
15
  "generate-doc-tests": "mocha 'test.js' --reporter mochawesome --reporter-options reportDir=docs/tests,reportFilename=index,showHooks=always",
17
16
  "syntax-check": "syntax-check",
@@ -1,11 +0,0 @@
1
- const AdaptorInfluxDb = require('.')
2
-
3
- class AdaptorInfluxDbFactory
4
- {
5
- create(influxdb, escape)
6
- {
7
- return new AdaptorInfluxDb(influxdb, escape)
8
- }
9
- }
10
-
11
- module.exports = AdaptorInfluxDbFactory
@@ -1,12 +0,0 @@
1
- const AdaptorMySql = require('.')
2
-
3
- class AdaptorMySqlFactory
4
- {
5
- create(mysql, config)
6
- {
7
- const pool = mysql.createPool(config)
8
- return new AdaptorMySql(pool)
9
- }
10
- }
11
-
12
- module.exports = AdaptorMySqlFactory
@@ -1,38 +0,0 @@
1
- const AdaptorMySqlTransaction = require('./transaction')
2
-
3
- class AdaptorMySql
4
- {
5
- constructor(pool)
6
- {
7
- this.pool = pool
8
- }
9
-
10
- query(query, ...ctx)
11
- {
12
- return new Promise((accept, reject) =>
13
- this.pool.query(query, ...ctx, (error, response) =>
14
- error
15
- ? reject(error)
16
- : accept(response)))
17
- }
18
-
19
- getConnection()
20
- {
21
- return new Promise((accept, reject) =>
22
- this.pool.getConnection((error, connection) =>
23
- error
24
- ? reject(error)
25
- : accept(connection)))
26
- }
27
-
28
- async createTransaction()
29
- {
30
- const connection = await this.getConnection()
31
- await connection.query('START TRANSACTION')
32
- const transaction = new AdaptorMySqlTransaction(connection)
33
-
34
- return transaction
35
- }
36
- }
37
-
38
- module.exports = AdaptorMySql
@@ -1,12 +0,0 @@
1
- const AdaptorRedis = require('.')
2
-
3
- class AdaptorRedisFactory
4
- {
5
- create(redis, options)
6
- {
7
- const client = redis.createClient(options)
8
- return new AdaptorRedis(client)
9
- }
10
- }
11
-
12
- module.exports = AdaptorRedisFactory