rqlite-js 2.5.7 → 3.0.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 (71) hide show
  1. package/README.md +354 -157
  2. package/dist/index.cjs +904 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/index.d.cts +329 -0
  5. package/dist/index.d.ts +329 -0
  6. package/dist/index.js +865 -0
  7. package/dist/index.js.map +1 -0
  8. package/docs/MIGRATION-v2-to-v3.md +71 -0
  9. package/package.json +73 -42
  10. package/src/api/backup/index.ts +69 -0
  11. package/src/api/backup-formats.ts +8 -0
  12. package/src/api/client/index.ts +97 -0
  13. package/src/api/consistency-levels.ts +10 -0
  14. package/src/api/data/index.ts +87 -0
  15. package/src/api/paths.ts +13 -0
  16. package/src/api/query-params.ts +15 -0
  17. package/src/api/results/data-result-error.ts +41 -0
  18. package/src/api/results/data-result.ts +91 -0
  19. package/src/api/results/data-results.ts +91 -0
  20. package/src/api/results/index.ts +7 -0
  21. package/src/api/results/result-fields.ts +14 -0
  22. package/src/api/status/index.ts +53 -0
  23. package/src/http-request/auth-keys.ts +14 -0
  24. package/{es6/http-request/errors.js → src/http-request/errors.ts} +4 -3
  25. package/src/http-request/http-headers.ts +19 -0
  26. package/{es6/http-request/http-methods.js → src/http-request/http-methods.ts} +0 -27
  27. package/src/http-request/index.ts +676 -0
  28. package/{es6/http-request/retryable.js → src/http-request/retryable.ts} +1 -1
  29. package/src/index.ts +38 -0
  30. package/.eslintrc +0 -20
  31. package/.travis.yml +0 -24
  32. package/es6/api/backup/index.js +0 -71
  33. package/es6/api/client/index.js +0 -83
  34. package/es6/api/data/index.js +0 -129
  35. package/es6/api/results/data-result-error.js +0 -32
  36. package/es6/api/results/data-result.js +0 -134
  37. package/es6/api/results/data-results.js +0 -121
  38. package/es6/api/results/index.js +0 -7
  39. package/es6/api/status/index.js +0 -54
  40. package/es6/http-request/index.js +0 -743
  41. package/es6/index.js +0 -19
  42. package/jsconfig.json +0 -19
  43. package/lib/api/backup/index.js +0 -72
  44. package/lib/api/backup/index.js.map +0 -1
  45. package/lib/api/client/index.js +0 -84
  46. package/lib/api/client/index.js.map +0 -1
  47. package/lib/api/data/index.js +0 -130
  48. package/lib/api/data/index.js.map +0 -1
  49. package/lib/api/results/data-result-error.js +0 -33
  50. package/lib/api/results/data-result-error.js.map +0 -1
  51. package/lib/api/results/data-result.js +0 -135
  52. package/lib/api/results/data-result.js.map +0 -1
  53. package/lib/api/results/data-results.js +0 -122
  54. package/lib/api/results/data-results.js.map +0 -1
  55. package/lib/api/results/index.js +0 -8
  56. package/lib/api/results/index.js.map +0 -1
  57. package/lib/api/status/index.js +0 -55
  58. package/lib/api/status/index.js.map +0 -1
  59. package/lib/http-request/content-types.js +0 -5
  60. package/lib/http-request/content-types.js.map +0 -1
  61. package/lib/http-request/errors.js +0 -17
  62. package/lib/http-request/errors.js.map +0 -1
  63. package/lib/http-request/http-methods.js +0 -40
  64. package/lib/http-request/http-methods.js.map +0 -1
  65. package/lib/http-request/index.js +0 -744
  66. package/lib/http-request/index.js.map +0 -1
  67. package/lib/http-request/retryable.js +0 -42
  68. package/lib/http-request/retryable.js.map +0 -1
  69. package/lib/index.js +0 -20
  70. package/lib/index.js.map +0 -1
  71. /package/{es6/http-request/content-types.js → src/http-request/content-types.ts} +0 -0
@@ -0,0 +1,15 @@
1
+ /**
2
+ * RQLite HTTP query parameter name constants
3
+ * @module api/query-params
4
+ * @see https://rqlite.io/docs/api/api/
5
+ * @see https://rqlite.io/docs/api/read-consistency/
6
+ * @see https://rqlite.io/docs/guides/backup/
7
+ */
8
+
9
+ export const QUERY_PARAM_LEVEL = 'level'
10
+ export const QUERY_PARAM_PRETTY = 'pretty'
11
+ export const QUERY_PARAM_TIMINGS = 'timings'
12
+ export const QUERY_PARAM_ATOMIC = 'atomic'
13
+ export const QUERY_PARAM_TRANSACTION = 'transaction'
14
+ export const QUERY_PARAM_Q = 'q'
15
+ export const QUERY_PARAM_FMT = 'fmt'
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Class for handling RQLite data api result error
3
+ * @module api/results/data-result-error
4
+ * @see https://rqlite.io/docs/api/api/
5
+ */
6
+ import { RESULT_FIELD_ERROR, RESULT_FIELD_ERROR_V2 } from './result-fields.js'
7
+
8
+ /**
9
+ * A class that represents one data error result from an RQLite query or execute API call
10
+ * @see https://rqlite.io/docs/api/api/
11
+ */
12
+ export default class DataResultError extends Error {
13
+ code: string
14
+
15
+ errorV2?: Record<string, unknown>
16
+
17
+ constructor (message: string, errorV2?: Record<string, unknown>) {
18
+ super(message)
19
+ this.name = this.constructor.name
20
+ this.code = this.constructor.name
21
+ if (errorV2) {
22
+ this.errorV2 = errorV2
23
+ }
24
+ }
25
+
26
+ getErrorV2 (): Record<string, unknown> | undefined {
27
+ return this.errorV2
28
+ }
29
+
30
+ toObject (): Record<string, unknown> {
31
+ const obj: Record<string, unknown> = { [RESULT_FIELD_ERROR]: this.message }
32
+ if (this.errorV2) {
33
+ obj[RESULT_FIELD_ERROR_V2] = this.errorV2
34
+ }
35
+ return obj
36
+ }
37
+
38
+ toString (): string {
39
+ return JSON.stringify(this.toObject())
40
+ }
41
+ }
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Class for handling single RQLite data api result
3
+ * @module api/results/data-result
4
+ * @see https://rqlite.io/docs/api/api/
5
+ * @see module:api/results/result-fields
6
+ */
7
+ import {
8
+ RESULT_FIELD_COLUMNS,
9
+ RESULT_FIELD_LAST_INSERT_ID,
10
+ RESULT_FIELD_ROWS_AFFECTED,
11
+ RESULT_FIELD_TIME,
12
+ RESULT_FIELD_VALUES,
13
+ } from './result-fields.js'
14
+
15
+ export type RqliteSingleResult = {
16
+ [RESULT_FIELD_TIME]?: number | string
17
+ [RESULT_FIELD_ROWS_AFFECTED]?: number | string
18
+ [RESULT_FIELD_LAST_INSERT_ID]?: number
19
+ [RESULT_FIELD_COLUMNS]?: string[]
20
+ [RESULT_FIELD_VALUES]?: unknown[][]
21
+ [key: string]: unknown
22
+ }
23
+
24
+ /**
25
+ * A class that represents one data result from an RQLite query or execute API call
26
+ * @see https://rqlite.io/docs/api/api/
27
+ */
28
+ export default class DataResult {
29
+ time = 0.0
30
+
31
+ lastInsertId?: number
32
+
33
+ rowsAffected = 0
34
+
35
+ data: Record<string, unknown> = {}
36
+
37
+ constructor (result: RqliteSingleResult, valuesIndex?: number) {
38
+ if (typeof result !== 'object' || result === null) {
39
+ throw new Error('The result argument is required to be an object')
40
+ }
41
+ if (typeof valuesIndex !== 'undefined' && !Number.isFinite(valuesIndex)) {
42
+ throw new Error('The valuesIndex argument is required to be a finite number when provided')
43
+ }
44
+ this.time = parseFloat(String(result[RESULT_FIELD_TIME] || 0.0))
45
+ this.rowsAffected = parseInt(String(result[RESULT_FIELD_ROWS_AFFECTED] || 0), 10)
46
+ this.lastInsertId = result[RESULT_FIELD_LAST_INSERT_ID] as number | undefined
47
+ if (Number.isFinite(valuesIndex)) {
48
+ const columns = result[RESULT_FIELD_COLUMNS] as string[]
49
+ const resultValues = (result[RESULT_FIELD_VALUES] as unknown[][])[valuesIndex as number]
50
+ if (resultValues) {
51
+ this.data = resultValues.reduce<Record<string, unknown>>((acc, val, i) => {
52
+ const col = columns[i]
53
+ acc[col] = val
54
+ return acc
55
+ }, {})
56
+ }
57
+ }
58
+ }
59
+
60
+ get (property: string): unknown {
61
+ return this.data[property]
62
+ }
63
+
64
+ getTime (): number {
65
+ return this.time
66
+ }
67
+
68
+ getLastInsertId (): number | undefined {
69
+ return this.lastInsertId
70
+ }
71
+
72
+ getRowsAffected (): number {
73
+ return this.rowsAffected
74
+ }
75
+
76
+ toObject (): Record<string, unknown> {
77
+ return JSON.parse(JSON.stringify(this.data)) as Record<string, unknown>
78
+ }
79
+
80
+ toArray (): unknown[] {
81
+ return Object.values(this.data)
82
+ }
83
+
84
+ toColumnsArray (): string[] {
85
+ return Object.keys(this.data)
86
+ }
87
+
88
+ toString (): string {
89
+ return JSON.stringify(this.data)
90
+ }
91
+ }
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Class for handling many RQLite data api results
3
+ * @module api/results/data-results
4
+ * @see https://rqlite.io/docs/api/api/
5
+ * @see module:api/results/result-fields
6
+ */
7
+ import DataResult from './data-result.js'
8
+ import type { RqliteSingleResult } from './data-result.js'
9
+ import DataResultError from './data-result-error.js'
10
+ import {
11
+ RESULT_FIELD_ERROR,
12
+ RESULT_FIELD_ERROR_V2,
13
+ RESULT_FIELD_RESULTS,
14
+ RESULT_FIELD_TIME,
15
+ RESULT_FIELD_VALUES,
16
+ } from './result-fields.js'
17
+
18
+ export type RqliteApiData = {
19
+ [RESULT_FIELD_TIME]?: number | string
20
+ [RESULT_FIELD_RESULTS]: RqliteSingleResult[]
21
+ }
22
+
23
+ /**
24
+ * A class to manage a list of results from an RQLite query or execute API response
25
+ * @see https://rqlite.io/docs/api/api/
26
+ */
27
+ export default class DataResults {
28
+ time = 0.0
29
+
30
+ results: Array<DataResult | DataResultError> = []
31
+
32
+ constructor (data: RqliteApiData) {
33
+ this.setApiData(data)
34
+ }
35
+
36
+ setApiData (data: RqliteApiData): void {
37
+ if (typeof data !== 'object' || data === null) {
38
+ throw new Error('The data argument is required to be an object')
39
+ }
40
+ if (!data[RESULT_FIELD_RESULTS]) {
41
+ throw new Error('The data object is required to have a results property')
42
+ }
43
+ this.time = parseFloat(String(data[RESULT_FIELD_TIME] || 0.0))
44
+ const results = data[RESULT_FIELD_RESULTS] || []
45
+ this.results = results.reduce<Array<DataResult | DataResultError>>((acc, result) => {
46
+ if (typeof result === 'object' && result !== null && result[RESULT_FIELD_ERROR]) {
47
+ return acc.concat(
48
+ new DataResultError(
49
+ String(result[RESULT_FIELD_ERROR]),
50
+ result[RESULT_FIELD_ERROR_V2] as Record<string, unknown> | undefined,
51
+ ),
52
+ )
53
+ }
54
+ const vals = result[RESULT_FIELD_VALUES]
55
+ if (!vals) {
56
+ return acc.concat(new DataResult(result))
57
+ }
58
+ const dataResults = vals.map((_v, valuesIndex) => new DataResult(result, valuesIndex))
59
+ return acc.concat(dataResults)
60
+ }, [])
61
+ }
62
+
63
+ hasError (): boolean {
64
+ return !!this.getFirstError()
65
+ }
66
+
67
+ getFirstError (): DataResultError | undefined {
68
+ return this.results.find((v) => v instanceof DataResultError) as DataResultError | undefined
69
+ }
70
+
71
+ getTime (): number {
72
+ return this.time
73
+ }
74
+
75
+ get (index: number): DataResult | DataResultError | undefined {
76
+ return this.results[index]
77
+ }
78
+
79
+ getResults (): Array<DataResult | DataResultError> {
80
+ return this.results
81
+ }
82
+
83
+ toArray (): Record<string, unknown>[] {
84
+ return this.results.map((result) => result.toObject())
85
+ }
86
+
87
+ toString (): string {
88
+ const list = this.results.map((result) => result.toObject())
89
+ return JSON.stringify(list)
90
+ }
91
+ }
@@ -0,0 +1,7 @@
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.js'
6
+ export { default as DataResultError } from './data-result-error.js'
7
+ export { default as DataResults } from './data-results.js'
@@ -0,0 +1,14 @@
1
+ /**
2
+ * RQLite JSON response field name constants
3
+ * @module api/results/result-fields
4
+ * @see https://rqlite.io/docs/api/api/
5
+ */
6
+
7
+ export const RESULT_FIELD_TIME = 'time'
8
+ export const RESULT_FIELD_ROWS_AFFECTED = 'rows_affected'
9
+ export const RESULT_FIELD_LAST_INSERT_ID = 'last_insert_id'
10
+ export const RESULT_FIELD_COLUMNS = 'columns'
11
+ export const RESULT_FIELD_VALUES = 'values'
12
+ export const RESULT_FIELD_ERROR = 'error'
13
+ export const RESULT_FIELD_ERROR_V2 = 'error_v2'
14
+ export const RESULT_FIELD_RESULTS = 'results'
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Status api client to perform RQLite status and diagnostics operations
3
+ * @module api/status
4
+ * @see https://rqlite.io/docs/guides/monitoring-rqlite/
5
+ */
6
+ import ApiClient from '../client/index.js'
7
+ import type { HttpResponse } from '../../http-request/index.js'
8
+ import { HTTP_METHOD_GET } from '../../http-request/http-methods.js'
9
+ import { PATH_STATUS } from '../paths.js'
10
+
11
+ export { PATH_STATUS } from '../paths.js'
12
+
13
+ export type StatusRequestOptions = {
14
+ raw?: boolean
15
+ attemptHostIndex?: number
16
+ useLeader?: boolean
17
+ }
18
+
19
+ export type StatusAllHostsResponse = {
20
+ response: HttpResponse
21
+ host: string
22
+ }
23
+
24
+ /**
25
+ * Data api client to perform RQLite status queries
26
+ */
27
+ export default class StatusApiClient extends ApiClient {
28
+ /**
29
+ * Get the RQLite server status which defaults to the leader host
30
+ * @see https://rqlite.io/docs/guides/monitoring-rqlite/
31
+ */
32
+ async status (options: StatusRequestOptions = {}): Promise<HttpResponse> {
33
+ return this.fetch({
34
+ ...options,
35
+ uri: PATH_STATUS,
36
+ httpMethod: HTTP_METHOD_GET,
37
+ useLeader: typeof options.useLeader === 'boolean' ? options.useLeader : true,
38
+ }) as Promise<HttpResponse>
39
+ }
40
+
41
+ /**
42
+ * Get the RQLite server status for all hosts
43
+ * @see https://rqlite.io/docs/guides/monitoring-rqlite/
44
+ */
45
+ async statusAllHosts (options: StatusRequestOptions = {}): Promise<StatusAllHostsResponse[]> {
46
+ const hosts = this.getHosts()
47
+ const promises = hosts.map(async (_host, attemptHostIndex) => {
48
+ const response = await this.status({ ...options, useLeader: false, attemptHostIndex })
49
+ return { response, host: hosts[attemptHostIndex] }
50
+ })
51
+ return Promise.all(promises)
52
+ }
53
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Authentication Map key constants
3
+ * @module http-request/auth-keys
4
+ */
5
+
6
+ /**
7
+ * Username key for authentication
8
+ */
9
+ export const AUTH_USERNAME = 'username'
10
+
11
+ /**
12
+ * Password key for authentication
13
+ */
14
+ export const AUTH_PASSWORD = 'password'
@@ -1,14 +1,15 @@
1
- /* eslint-disable import/prefer-default-export */
2
1
  /**
3
2
  * Http request errors
4
3
  * @module http-request/errors
5
4
  */
6
5
 
7
6
  /**
8
- * Error when max rediret attempts have been reached
7
+ * Error when max redirect attempts have been reached
9
8
  */
10
9
  export class ERROR_HTTP_REQUEST_MAX_REDIRECTS extends Error {
11
- constructor (...args) {
10
+ code: string
11
+
12
+ constructor (...args: ConstructorParameters<typeof Error>) {
12
13
  super(...args)
13
14
  this.name = this.constructor.name
14
15
  this.code = this.constructor.name
@@ -0,0 +1,19 @@
1
+ /**
2
+ * HTTP header name constants used by RQLite clients
3
+ * @module http-request/http-headers
4
+ */
5
+
6
+ /**
7
+ * Accept request header
8
+ */
9
+ export const HTTP_HEADER_ACCEPT = 'Accept'
10
+
11
+ /**
12
+ * Content-Type request header
13
+ */
14
+ export const HTTP_HEADER_CONTENT_TYPE = 'Content-Type'
15
+
16
+ /**
17
+ * Location response header (redirects)
18
+ */
19
+ export const HTTP_HEADER_LOCATION = 'Location'
@@ -3,37 +3,10 @@
3
3
  * @module http-request/http-methods
4
4
  */
5
5
 
6
- /**
7
- * Http DELETE method
8
- */
9
6
  export const HTTP_METHOD_DELETE = 'DELETE'
10
-
11
- /**
12
- * Http GET method
13
- */
14
7
  export const HTTP_METHOD_GET = 'GET'
15
-
16
- /**
17
- * Http HEAD method
18
- */
19
8
  export const HTTP_METHOD_HEAD = 'HEAD'
20
-
21
- /**
22
- * Http OPTIONS method
23
- */
24
9
  export const HTTP_METHOD_OPTIONS = 'OPTIONS'
25
-
26
- /**
27
- * Http PATCH method
28
- */
29
10
  export const HTTP_METHOD_PATCH = 'PATCH'
30
-
31
- /**
32
- * Http POST method
33
- */
34
11
  export const HTTP_METHOD_POST = 'POST'
35
-
36
- /**
37
- * Http PUT method
38
- */
39
12
  export const HTTP_METHOD_PUT = 'PUT'