rqlite-js 2.5.6 → 2.6.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.
Files changed (86) hide show
  1. package/README.md +174 -155
  2. package/lib/api/backup/index.js +47 -47
  3. package/lib/api/backup/index.js.map +1 -1
  4. package/lib/api/backup/test.unit.js +45 -0
  5. package/lib/api/backup/test.unit.js.map +1 -0
  6. package/lib/api/backup-formats.js +16 -0
  7. package/lib/api/backup-formats.js.map +1 -0
  8. package/lib/api/client/index.js +63 -42
  9. package/lib/api/client/index.js.map +1 -1
  10. package/lib/api/client/test.unit.js +53 -0
  11. package/lib/api/client/test.unit.js.map +1 -0
  12. package/lib/api/consistency-levels.js +30 -0
  13. package/lib/api/consistency-levels.js.map +1 -0
  14. package/lib/api/data/index.js +53 -69
  15. package/lib/api/data/index.js.map +1 -1
  16. package/lib/api/data/test.unit.js +98 -0
  17. package/lib/api/data/test.unit.js.map +1 -0
  18. package/lib/api/paths.js +33 -0
  19. package/lib/api/paths.js.map +1 -0
  20. package/lib/api/query-params.js +43 -0
  21. package/lib/api/query-params.js.map +1 -0
  22. package/lib/api/results/data-result-error.js +39 -10
  23. package/lib/api/results/data-result-error.js.map +1 -1
  24. package/lib/api/results/data-result.js +57 -51
  25. package/lib/api/results/data-result.js.map +1 -1
  26. package/lib/api/results/data-results.js +50 -30
  27. package/lib/api/results/data-results.js.map +1 -1
  28. package/lib/api/results/index.js +1 -1
  29. package/lib/api/results/index.js.map +1 -1
  30. package/lib/api/results/result-fields.js +47 -0
  31. package/lib/api/results/result-fields.js.map +1 -0
  32. package/lib/api/results/test.unit.js +238 -0
  33. package/lib/api/results/test.unit.js.map +1 -0
  34. package/lib/api/status/index.js +40 -28
  35. package/lib/api/status/index.js.map +1 -1
  36. package/lib/api/status/test.unit.js +57 -0
  37. package/lib/api/status/test.unit.js.map +1 -0
  38. package/lib/http-request/auth-keys.js +15 -0
  39. package/lib/http-request/auth-keys.js.map +1 -0
  40. package/lib/http-request/content-types.js +4 -4
  41. package/lib/http-request/content-types.js.map +1 -1
  42. package/lib/http-request/errors.js.map +1 -1
  43. package/lib/http-request/http-headers.js +20 -0
  44. package/lib/http-request/http-headers.js.map +1 -0
  45. package/lib/http-request/http-methods.js +13 -13
  46. package/lib/http-request/http-methods.js.map +1 -1
  47. package/lib/http-request/index.js +295 -275
  48. package/lib/http-request/index.js.map +1 -1
  49. package/lib/http-request/retryable.js +6 -6
  50. package/lib/http-request/retryable.js.map +1 -1
  51. package/lib/http-request/test.unit.js +560 -0
  52. package/lib/http-request/test.unit.js.map +1 -0
  53. package/lib/index.js +31 -6
  54. package/lib/index.js.map +1 -1
  55. package/lib/test/api-data-execute-nock.js +142 -0
  56. package/lib/test/api-data-execute-nock.js.map +1 -0
  57. package/lib/test/api-data-query-nock.js +210 -0
  58. package/lib/test/api-data-query-nock.js.map +1 -0
  59. package/lib/test/backups/backup-nock.js +29 -0
  60. package/lib/test/backups/backup-nock.js.map +1 -0
  61. package/lib/test/backups/restore-nock.js +30 -0
  62. package/lib/test/backups/restore-nock.js.map +1 -0
  63. package/lib/test/env.js +15 -0
  64. package/lib/test/env.js.map +1 -0
  65. package/lib/test/unit/index.unit.js +27 -0
  66. package/lib/test/unit/index.unit.js.map +1 -0
  67. package/lib/test.integration.js +238 -0
  68. package/lib/test.integration.js.map +1 -0
  69. package/package.json +39 -27
  70. package/.eslintrc +0 -20
  71. package/.travis.yml +0 -24
  72. package/es6/api/backup/index.js +0 -71
  73. package/es6/api/client/index.js +0 -83
  74. package/es6/api/data/index.js +0 -123
  75. package/es6/api/results/data-result-error.js +0 -32
  76. package/es6/api/results/data-result.js +0 -134
  77. package/es6/api/results/data-results.js +0 -121
  78. package/es6/api/results/index.js +0 -7
  79. package/es6/api/status/index.js +0 -54
  80. package/es6/http-request/content-types.js +0 -4
  81. package/es6/http-request/errors.js +0 -16
  82. package/es6/http-request/http-methods.js +0 -39
  83. package/es6/http-request/index.js +0 -743
  84. package/es6/http-request/retryable.js +0 -41
  85. package/es6/index.js +0 -13
  86. package/jsconfig.json +0 -18
@@ -1,83 +0,0 @@
1
- /**
2
- * Base API client for RQLite which abstracts the HTTP calls
3
- * @module api/client
4
- */
5
- import HttpRequest from '../../http-request'
6
- import { HTTP_METHOD_GET, HTTP_METHOD_POST } from '../../http-request/http-methods'
7
-
8
- /**
9
- * @typedef HttpRequestOptions
10
- * @type {import('../../http-request').HttpRequestOptions}
11
- */
12
-
13
- /**
14
- * Create the base HTTP query options from RQLite API options
15
- * @param {Object} [options={}] The RQLite API options
16
- * @param {String} [options.level] The consistency level
17
- * @param {String} [options.pretty] Pretty print the response body
18
- * @param {String} [options.timings] Provide query timings
19
- * @param {String} [options.atomic] Treat all commands in the request as a single transaction
20
- * for RQLite v5 and higher
21
- * @param {String} [options.transaction] Treat all commands in the request as a single transaction
22
- * for RQLite v4 and lower
23
- * @returns {Object} The HTTP query
24
- */
25
- export function createQuery (options = {}) {
26
- const { level, pretty, timings, atomic, transaction } = options
27
-
28
- // Remove all undefined values
29
- const query = { level, pretty, timings, atomic, transaction }
30
- return Object.entries(query).reduce((acc, entry) => {
31
- const [key, val] = entry
32
- // Only take defined values
33
- if (typeof val !== 'undefined') {
34
- acc[key] = val
35
- }
36
- return acc
37
- }, {})
38
- }
39
-
40
- /**
41
- * Base API client for RQLite which abstracts the HTTP calls
42
- * from the user
43
- */
44
- export default class ApiClient extends HttpRequest {
45
- /**
46
- * Perform a RQLite data API get request
47
- * @param {String} path The path for the request i.e. /db/query
48
- * @param {String} sql The SQL query
49
- * @param {HttpRequestOptions} [options={}] RQLite API options
50
- */
51
- async get (path, sql, options = {}) {
52
- const { useLeader } = options
53
- if (!path) {
54
- throw new Error('The path argument is required')
55
- }
56
- return super.get({
57
- useLeader,
58
- uri: path,
59
- httpMethod: HTTP_METHOD_GET,
60
- query: { ...createQuery(options), q: sql },
61
- })
62
- }
63
-
64
- /**
65
- * Perform a RQLite data API post request
66
- * @param {String} path The path for the request i.e. /db/query
67
- * @param {String} sql The SQL query
68
- * @param {HttpRequestOptions} [options={}] RQLite API options
69
- */
70
- async post (path, sql, options = {}) {
71
- const { useLeader } = options
72
- if (!path) {
73
- throw new Error('The path argument is required')
74
- }
75
- return super.post({
76
- useLeader,
77
- uri: path,
78
- httpMethod: HTTP_METHOD_POST,
79
- query: createQuery(options),
80
- body: Array.isArray(sql) ? sql : [sql],
81
- })
82
- }
83
- }
@@ -1,123 +0,0 @@
1
- /**
2
- * Data api client to perform RQLite data operations such
3
- * as query and execute
4
- * @module api/data
5
- */
6
- import ApiClient from '../client'
7
- import { DataResults } from '../results'
8
-
9
- /**
10
- * The RQLite query api path
11
- */
12
- export const PATH_QUERY = '/db/query'
13
-
14
- /**
15
- * The RQLite execute api path
16
- */
17
- export const PATH_EXECUTE = '/db/execute'
18
-
19
- /**
20
- * Read query consistency level none which means
21
- * any node can respond
22
- */
23
- export const CONSISTENCY_LEVEL_NONE = 'none'
24
-
25
- /**
26
- * Read query consistency strong which must come from
27
- * the master node
28
- */
29
- export const CONSISTENCY_LEVEL_STRONG = 'strong'
30
-
31
- /**
32
- * Read query consistency weak which must come from
33
- * the master node
34
- */
35
- export const CONSISTENCY_LEVEL_WEAK = 'weak'
36
-
37
- /**
38
- * @typedef HttpRequestOptions
39
- * @type {import('../../http-request').HttpRequestOptions}
40
- */
41
-
42
- /**
43
- * Data request base options
44
- * @typedef DataRequestBaseOptions
45
- * @type {Object}
46
- * @property {Boolean} [raw] If true return the raw http response from
47
- * RQLite response
48
- */
49
-
50
- /**
51
- * Data query request base options
52
- * @typedef QueryRequestBaseOptions
53
- * @type {Object}
54
- * @property {String} level The api consistency level
55
- */
56
-
57
- /**
58
- * Data query request options
59
- * @typedef QueryRequestOptions
60
- * @type {HttpRequestOptions & DataRequestBaseOptions & QueryRequestBaseOptions}
61
- */
62
-
63
- /**
64
- * Data execute request options
65
- * @typedef ExecuteRequestOptions
66
- * @type {HttpRequestOptions & DataRequestBaseOptions}
67
- */
68
-
69
- /**
70
- * Send an RQLite query API request to the RQLite server
71
- * @param {String} sql The SQL string to excute on the server
72
- * @param {DataRequestBaseOptions} [options={}] RQLite api options
73
- */
74
- function handleResponse (response, options = {}) {
75
- const { raw } = options
76
- const { body } = response
77
- if (raw) {
78
- return response
79
- }
80
- return new DataResults(body)
81
- }
82
-
83
- /**
84
- * Data api client to perform RQLite queries
85
- */
86
- export default class DataApiClient extends ApiClient {
87
- /**
88
- * Send an RQLite query API request to the RQLite server
89
- * @param {String} sql The SQL string to excute on the server
90
- * @param {QueryRequestOptions} [options={}] RQLite api options
91
- */
92
- async query (sql, options = {}) {
93
- const { level } = options
94
- let { useLeader } = options
95
- // Weak and strong consistency will be redirect to the master anyway
96
- // so skip the redirect HTTP response and got right to the master
97
- if (level !== CONSISTENCY_LEVEL_NONE) {
98
- useLeader = true
99
- }
100
- let response
101
- if (Array.isArray(sql)) {
102
- response = await super.post(PATH_QUERY, sql, { ...options, useLeader })
103
- } else {
104
- response = await super.get(PATH_QUERY, sql, { ...options, useLeader })
105
- }
106
- // If round robin is true try and balance selects across hosts when
107
- // the master node is not queried directly
108
- if (!useLeader) {
109
- this.setNextActiveHostIndex()
110
- }
111
- return handleResponse(response, options)
112
- }
113
-
114
- /**
115
- * Send an RQLite execute API request to the RQLite server
116
- * @param {String} sql The SQL string to excute on the server
117
- * @param {ExecuteRequestOptions} [options={}] RQLite execute api options
118
- */
119
- async execute (sql, options = {}) {
120
- const response = await super.post(PATH_EXECUTE, sql, options)
121
- return handleResponse(response, options)
122
- }
123
- }
@@ -1,32 +0,0 @@
1
- /**
2
- * Class for handling RQLite data api result error
3
- * @module api/results/data-results
4
- */
5
-
6
- /**
7
- * A class that represents one data error result from an RQLite query or execute
8
- * API call
9
- */
10
- export default class DataResultError extends Error {
11
- constructor (...args) {
12
- super(...args)
13
- this.name = this.constructor.name
14
- this.code = this.constructor.name
15
- }
16
-
17
- /**
18
- * Get the result data error as plain object
19
- * @returns {Object} The data as an object
20
- */
21
- toObject () {
22
- return { error: this.message }
23
- }
24
-
25
- /**
26
- * Convert the result data error to a JSON string
27
- * @returns {String} The JSON string for the data object
28
- */
29
- toString () {
30
- return JSON.stringify(this.toObject())
31
- }
32
- }
@@ -1,134 +0,0 @@
1
- /**
2
- * Class for handling single RQLite data api result
3
- * @module api/results/data-result
4
- */
5
-
6
- /**
7
- * A class that represents one data result from an RQLite query or execute
8
- * API call
9
- */
10
- export default class DataResult {
11
- /**
12
- * The time the results query took to complete
13
- * @type {Number}
14
- */
15
- time = 0.0
16
-
17
- /**
18
- * The last insert id
19
- * @type {Number}
20
- */
21
- lastInsertId
22
-
23
- /**
24
- * The rows affected
25
- * @type {Number}
26
- */
27
- rowsAffected = 0
28
-
29
- /**
30
- * An array of DataResult and/or DataResultError instances
31
- * @type {Array<DataResult|DataResultError>}
32
- */
33
- results = []
34
-
35
- /**
36
- * An object after the columns and values are mapped from
37
- * an RQLite response
38
- */
39
- data = {}
40
-
41
- /**
42
- * The data result constructor
43
- * @param {Array} result An API response individual result
44
- * @param {Array} [valuesIndex] The index to get the values from the result
45
- */
46
- constructor (result, valuesIndex) {
47
- if (typeof result !== 'object') {
48
- throw new Error('The result argument is required to be an object')
49
- }
50
- if (typeof valuesIndex !== 'undefined' && !Number.isFinite(valuesIndex)) {
51
- throw new Error('The valuesIndex argument is required to be a finite number when provided')
52
- }
53
- this.time = parseFloat(result.time || 0.0)
54
- this.rowsAffected = parseInt(result.rows_affected || 0, 10)
55
- this.lastInsertId = result.last_insert_id
56
- // Map the values array to an object where columns are the properties
57
- if (Number.isFinite(valuesIndex)) {
58
- const { columns } = result
59
- const resultValues = result.values[valuesIndex]
60
- if (resultValues) {
61
- this.data = resultValues.reduce((acc, val, i) => {
62
- const col = columns[i]
63
- acc[col] = val
64
- return acc
65
- }, {})
66
- }
67
- }
68
- }
69
-
70
- /**
71
- * Return the value a property or undefined if it does not exist
72
- * @returns {*} The value of the property or undefined
73
- */
74
- get (property) {
75
- return this.data[property]
76
- }
77
-
78
- /**
79
- * Get the time the result took
80
- * @returns {Number} The time the query took
81
- */
82
- getTime () {
83
- return this.time
84
- }
85
-
86
- /**
87
- * Get the last insert id
88
- * @returns {Number|undefined}
89
- */
90
- getLastInsertId () {
91
- return this.lastInsertId
92
- }
93
-
94
- /**
95
- * Get the row affected
96
- * @returns {Number|undefined}
97
- */
98
- getRowsAffected () {
99
- return this.rowsAffected
100
- }
101
-
102
- /**
103
- * Get the result data as plain object
104
- * @returns {Object} The data as an object
105
- */
106
- toObject () {
107
- // Clone deep
108
- return JSON.parse(JSON.stringify(this.data))
109
- }
110
-
111
- /**
112
- * Map the data values to an array
113
- * @returns {Array}
114
- */
115
- toArray () {
116
- return Object.values(this.data)
117
- }
118
-
119
- /**
120
- * Map the data properites to an array
121
- * @returns {String[]}
122
- */
123
- toColumnsArray () {
124
- return Object.keys(this.data)
125
- }
126
-
127
- /**
128
- * Convert the result data to a JSON string
129
- * @returns {String} The JSON string for the data object
130
- */
131
- toString () {
132
- return JSON.stringify(this.data)
133
- }
134
- }
@@ -1,121 +0,0 @@
1
- /**
2
- * Class for handling many RQLite data api results
3
- * @module api/results/data-results
4
- */
5
- import DataResult from './data-result'
6
- import DataResultError from './data-result-error'
7
-
8
- /**
9
- * A class to manage a list of results from an RQLite query or execute API response
10
- */
11
- export default class DataResults {
12
- /**
13
- * The time the results took to return from the
14
- * RQLite api
15
- * @type {Number}
16
- */
17
- time = 0.0
18
-
19
- /**
20
- * The results which is an empty arry to start
21
- * @type {DataResult[]}
22
- */
23
- results = []
24
-
25
- /**
26
- * The data result list constructor
27
- * @param {Object} data The data object sent from a RQLite API response
28
- * @param {Number} data.time The time the API response took to complete
29
- * @param {Object[{error:String,values:Object}]} data.results The results array
30
- */
31
- constructor (data) {
32
- this.setApiData(data)
33
- }
34
-
35
- /**
36
- * Set the api as results
37
- * @param {Object} data Api data
38
- */
39
- setApiData (data) {
40
- if (typeof data !== 'object') {
41
- throw new Error('The data argument is required to be an object')
42
- }
43
- if (!data.results) {
44
- throw new Error('The data object is required to have a results property')
45
- }
46
- this.time = parseFloat(data.time || 0.0)
47
- const { results = [] } = data
48
- this.results = results.reduce((acc, result) => {
49
- // If there is an error property this is an error
50
- if (typeof result === 'object' && result.error) {
51
- return acc.concat(new DataResultError(result.error))
52
- }
53
- const { values: vals } = result
54
- // We don't have values so this is a single result row
55
- if (!vals) {
56
- return acc.concat(new DataResult(result))
57
- }
58
- // Map the values to DataResult instances
59
- const dataResults = vals.map((_v, valuesIndex) => new DataResult(result, valuesIndex))
60
- return acc.concat(dataResults)
61
- }, [])
62
- }
63
-
64
- /**
65
- * Returns true if an instance of DataResultError exists in the results
66
- * @returns {Boolean} True if a DataResultError instance exists
67
- */
68
- hasError () {
69
- return !!this.getFirstError()
70
- }
71
-
72
- /**
73
- * Get the first error that occured
74
- * @returns {DataResultError|undefined}
75
- */
76
- getFirstError () {
77
- return this.results.find((v) => v instanceof DataResultError)
78
- }
79
-
80
- /**
81
- * Get the time the results took
82
- * @returns {Number} The time the query took
83
- */
84
- getTime () {
85
- return this.time
86
- }
87
-
88
- /**
89
- * Return one result at a specific index or undefined it it does not exist
90
- * @returns {DataResult|DataResultError|undefined}
91
- */
92
- get (index) {
93
- return this.results[index]
94
- }
95
-
96
- /**
97
- * Return the results array
98
- * @returns {Array<DataResult|DataResultError>}
99
- */
100
- getResults () {
101
- return this.results
102
- }
103
-
104
- /**
105
- * Get the result data list as array of plain objects
106
- * @returns {Object[]} The data as an array or objects
107
- */
108
- toArray () {
109
- return this.results.map((result) => result.toObject())
110
- }
111
-
112
- /**
113
- * Convert the result data list to a JSON string that is an
114
- * array of objects
115
- * @returns {String} A JSON string
116
- */
117
- toString () {
118
- const list = this.results.map((result) => result.toObject())
119
- return JSON.stringify(list)
120
- }
121
- }
@@ -1,7 +0,0 @@
1
- /**
2
- * Features for handle RQLite data api responses for success and errors
3
- * @module api/results
4
- */
5
- export { default as DataResult } from './data-result'
6
- export { default as DataResultError } from './data-result-error'
7
- export { default as DataResults } from './data-results'
@@ -1,54 +0,0 @@
1
- /**
2
- * Status api client to perform RQLite status and diagnostics operations
3
- * @module api/status
4
- */
5
- import ApiClient from '../client'
6
-
7
- /**
8
- * @typedef {Object} HttpResponse
9
- * @property {Object} Body The HTTP response body parsed by JSON.parse()
10
- * @property {Number} statusCode The HTTP response code
11
- */
12
-
13
- /**
14
- * @typedef {Object} StatusAllHostsResponse
15
- * @property {HttpResponse} response An HTTP response
16
- * @property {String} host The host name
17
- */
18
-
19
- /**
20
- * The RQLite status api path
21
- */
22
- export const PATH_STATUS = '/status'
23
-
24
- /**
25
- * Data api client to perform RQLite queries
26
- */
27
- export default class StatusApiClient extends ApiClient {
28
- /**
29
- * Get the RQLite server status which defaults to the master host
30
- * @param {Object} [options={}] RQLite api options
31
- * @returns {HttpResponse} An HTTP response object
32
- */
33
- async status (options = {}) {
34
- return super.get(PATH_STATUS, { useLeader: true, ...options })
35
- }
36
-
37
- /**
38
- * Get the RQLite server status for all hosts as an array of object with
39
- * the host and their status
40
- * @param {Object} [options={}] RQLite api options
41
- * @param {Object} [options.raw] If true return the raw http response from
42
- * RQLite response
43
- * @returns {StatusAllHostsResponse[]} An array of http response for the provide hosts
44
- */
45
- async statusAllHosts (options = {}) {
46
- const hosts = this.getHosts()
47
- // Get the status for all of the hosts
48
- const promises = hosts.map(async (_host, activeHostIndex) => {
49
- const response = await this.status({ ...options, activeHostIndex })
50
- return { response, host: hosts[activeHostIndex] }
51
- })
52
- return Promise.all(promises)
53
- }
54
- }
@@ -1,4 +0,0 @@
1
- export const CONTENT_TYPE_TEXT_PLAIN = 'text/plain'
2
- export const CONTENT_TYPE_APPLICATION_JSON = 'application/json'
3
- export const CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED = 'application/x-www-form-urlencoded'
4
- export const CONTENT_TYPE_APPLICATION_OCTET_STREAM = 'application/octet-stream'
@@ -1,16 +0,0 @@
1
- /* eslint-disable import/prefer-default-export */
2
- /**
3
- * Http request errors
4
- * @module http-request/errors
5
- */
6
-
7
- /**
8
- * Error when max rediret attempts have been reached
9
- */
10
- export class ERROR_HTTP_REQUEST_MAX_REDIRECTS extends Error {
11
- constructor (...args) {
12
- super(...args)
13
- this.name = this.constructor.name
14
- this.code = this.constructor.name
15
- }
16
- }
@@ -1,39 +0,0 @@
1
- /**
2
- * Http methods
3
- * @module http-request/http-methods
4
- */
5
-
6
- /**
7
- * Http DELETE method
8
- */
9
- export const HTTP_METHOD_DELETE = 'DELETE'
10
-
11
- /**
12
- * Http GET method
13
- */
14
- export const HTTP_METHOD_GET = 'GET'
15
-
16
- /**
17
- * Http HEAD method
18
- */
19
- export const HTTP_METHOD_HEAD = 'HEAD'
20
-
21
- /**
22
- * Http OPTIONS method
23
- */
24
- export const HTTP_METHOD_OPTIONS = 'OPTIONS'
25
-
26
- /**
27
- * Http PATCH method
28
- */
29
- export const HTTP_METHOD_PATCH = 'PATCH'
30
-
31
- /**
32
- * Http POST method
33
- */
34
- export const HTTP_METHOD_POST = 'POST'
35
-
36
- /**
37
- * Http PUT method
38
- */
39
- export const HTTP_METHOD_PUT = 'PUT'