@antelopejs/rethinkdb 1.5.0 → 1.6.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.
Files changed (108) hide show
  1. package/README.md +36 -16
  2. package/dist/config.d.ts +25 -0
  3. package/dist/config.js +106 -0
  4. package/dist/config.js.map +1 -0
  5. package/dist/connection.d.ts +16 -1
  6. package/dist/connection.js +95 -6
  7. package/dist/connection.js.map +1 -1
  8. package/dist/implementations/catalog/discovery.d.ts +11 -0
  9. package/dist/implementations/catalog/discovery.js +209 -0
  10. package/dist/implementations/catalog/discovery.js.map +1 -0
  11. package/dist/implementations/catalog/index.d.ts +7 -0
  12. package/dist/implementations/catalog/index.js +65 -0
  13. package/dist/implementations/catalog/index.js.map +1 -0
  14. package/dist/implementations/catalog/locate.d.ts +10 -0
  15. package/dist/implementations/catalog/locate.js +36 -0
  16. package/dist/implementations/catalog/locate.js.map +1 -0
  17. package/dist/implementations/catalog/measures.d.ts +21 -0
  18. package/dist/implementations/catalog/measures.js +126 -0
  19. package/dist/implementations/catalog/measures.js.map +1 -0
  20. package/dist/implementations/catalog/profile.d.ts +2 -0
  21. package/dist/implementations/catalog/profile.js +218 -0
  22. package/dist/implementations/catalog/profile.js.map +1 -0
  23. package/dist/implementations/catalog/reference.d.ts +2 -0
  24. package/dist/implementations/catalog/reference.js +63 -0
  25. package/dist/implementations/catalog/reference.js.map +1 -0
  26. package/dist/implementations/catalog/rows.d.ts +2 -0
  27. package/dist/implementations/catalog/rows.js +44 -0
  28. package/dist/implementations/catalog/rows.js.map +1 -0
  29. package/dist/implementations/catalog/text.d.ts +4 -0
  30. package/dist/implementations/catalog/text.js +13 -0
  31. package/dist/implementations/catalog/text.js.map +1 -0
  32. package/dist/implementations/database/atomic.d.ts +1 -1
  33. package/dist/implementations/database/atomic.js +17 -10
  34. package/dist/implementations/database/atomic.js.map +1 -1
  35. package/dist/implementations/database/expression.js +1 -1
  36. package/dist/implementations/database/expression.js.map +1 -1
  37. package/dist/implementations/database/external-descriptor.d.ts +36 -0
  38. package/dist/implementations/database/external-descriptor.js +154 -0
  39. package/dist/implementations/database/external-descriptor.js.map +1 -0
  40. package/dist/implementations/database/external-probe.d.ts +26 -0
  41. package/dist/implementations/database/external-probe.js +89 -0
  42. package/dist/implementations/database/external-probe.js.map +1 -0
  43. package/dist/implementations/database/external-read.d.ts +11 -0
  44. package/dist/implementations/database/external-read.js +123 -0
  45. package/dist/implementations/database/external-read.js.map +1 -0
  46. package/dist/implementations/database/external-registration.d.ts +4 -0
  47. package/dist/implementations/database/external-registration.js +18 -0
  48. package/dist/implementations/database/external-registration.js.map +1 -0
  49. package/dist/implementations/database/external-selection.d.ts +25 -0
  50. package/dist/implementations/database/external-selection.js +230 -0
  51. package/dist/implementations/database/external-selection.js.map +1 -0
  52. package/dist/implementations/database/external-writes.d.ts +14 -0
  53. package/dist/implementations/database/external-writes.js +355 -0
  54. package/dist/implementations/database/external-writes.js.map +1 -0
  55. package/dist/implementations/database/initialize.js.map +1 -1
  56. package/dist/implementations/database/query.js +67 -15
  57. package/dist/implementations/database/query.js.map +1 -1
  58. package/dist/implementations/database/schema.d.ts +2 -0
  59. package/dist/implementations/database/schema.js +21 -0
  60. package/dist/implementations/database/schema.js.map +1 -1
  61. package/dist/implementations/database/selection-stages.d.ts +5 -0
  62. package/dist/implementations/database/selection-stages.js +110 -0
  63. package/dist/implementations/database/selection-stages.js.map +1 -0
  64. package/dist/implementations/database/selection.d.ts +10 -3
  65. package/dist/implementations/database/selection.js +67 -91
  66. package/dist/implementations/database/selection.js.map +1 -1
  67. package/dist/implementations/database/stream.d.ts +4 -1
  68. package/dist/implementations/database/stream.js +30 -16
  69. package/dist/implementations/database/stream.js.map +1 -1
  70. package/dist/implementations/database/utils.d.ts +3 -0
  71. package/dist/implementations/database/utils.js +12 -0
  72. package/dist/implementations/database/utils.js.map +1 -1
  73. package/dist/implementations/rethinkdb/index.d.ts +1 -1
  74. package/dist/implementations/rethinkdb/index.js.map +1 -1
  75. package/dist/index.d.ts +3 -7
  76. package/dist/index.js +11 -7
  77. package/dist/index.js.map +1 -1
  78. package/dist/test/antelope.test.js +28 -12
  79. package/dist/test/antelope.test.js.map +1 -1
  80. package/dist/test/atomic-mutation.test.js.map +1 -1
  81. package/dist/test/instance-indexes.test.js.map +1 -1
  82. package/dist/test/primitive-guards.test.d.ts +1 -0
  83. package/dist/test/primitive-guards.test.js +49 -0
  84. package/dist/test/primitive-guards.test.js.map +1 -0
  85. package/dist/test/write-results.test.js.map +1 -1
  86. package/dist/test-external/catalog.test.d.ts +1 -0
  87. package/dist/test-external/catalog.test.js +136 -0
  88. package/dist/test-external/catalog.test.js.map +1 -0
  89. package/dist/test-external/erp.d.ts +18 -0
  90. package/dist/test-external/erp.js +74 -0
  91. package/dist/test-external/erp.js.map +1 -0
  92. package/dist/test-external/fixture.d.ts +12 -0
  93. package/dist/test-external/fixture.js +182 -0
  94. package/dist/test-external/fixture.js.map +1 -0
  95. package/dist/test-external/reads.test.d.ts +1 -0
  96. package/dist/test-external/reads.test.js +220 -0
  97. package/dist/test-external/reads.test.js.map +1 -0
  98. package/dist/test-external/refusals.test.d.ts +1 -0
  99. package/dist/test-external/refusals.test.js +53 -0
  100. package/dist/test-external/refusals.test.js.map +1 -0
  101. package/dist/test-external/startup.test.d.ts +1 -0
  102. package/dist/test-external/startup.test.js +41 -0
  103. package/dist/test-external/startup.test.js.map +1 -0
  104. package/dist/test-external/writes.test.d.ts +1 -0
  105. package/dist/test-external/writes.test.js +179 -0
  106. package/dist/test-external/writes.test.js.map +1 -0
  107. package/dist/write-result.js.map +1 -1
  108. package/package.json +23 -7
package/README.md CHANGED
@@ -3,18 +3,21 @@
3
3
  <div align="center">
4
4
  <a href="https://www.npmjs.com/package/@antelopejs/rethinkdb"><img alt="npm" src="https://img.shields.io/npm/v/@antelopejs/rethinkdb?style=for-the-badge&labelColor=000000&color=000000"></a>
5
5
  <a href="./LICENSE"><img alt="License" src="https://img.shields.io/npm/l/@antelopejs/rethinkdb?style=for-the-badge&labelColor=000000&color=000000"></a>
6
- <a href="https://discord.gg/sjK28QHrA7"><img src="https://img.shields.io/badge/Discord-18181B?logo=discord&style=for-the-badge&color=000000" alt="Discord"></a>
6
+ <a href="https://discord.gg/g4pFt6eXqu"><img src="https://img.shields.io/badge/Discord-18181B?logo=discord&style=for-the-badge&color=000000" alt="Discord"></a>
7
7
  <a href="https://interfaces.antelopejs.com/docs/database"><img src="https://img.shields.io/badge/Docs-18181B?style=for-the-badge&color=000000" alt="Documentation"></a>
8
8
  </div>
9
9
 
10
10
  The RethinkDB backend of AntelopeJS: it runs the database interface's queries on RethinkDB with [`rethinkdb-ts`](https://github.com/rethinkdb/rethinkdb-ts), and exposes that driver to modules that need it directly.
11
11
 
12
+ Part of [AntelopeJS](https://github.com/AntelopeJS/antelopejs). Used by [AntelopeJS DMS](https://github.com/AntelopeJS/dms), the open-source back office for your PostgreSQL, MongoDB or RethinkDB database.
13
+
12
14
  ## Interfaces
13
15
 
14
- | Interface | Documentation |
15
- | -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
16
- | [`@antelopejs/interface-database`](https://github.com/AntelopeJS/interface-database) | [Database](https://interfaces.antelopejs.com/docs/database) |
17
- | [`@antelopejs/interface-rethinkdb`](https://github.com/AntelopeJS/interface-rethinkdb) | [RethinkDB](https://interfaces.antelopejs.com/docs/rethinkdb/introduction) |
16
+ | Interface | Documentation |
17
+ | ---------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
18
+ | [`@antelopejs/interface-database`](https://github.com/AntelopeJS/interface-database) | [Database](https://interfaces.antelopejs.com/docs/database) |
19
+ | [`@antelopejs/interface-rethinkdb`](https://github.com/AntelopeJS/interface-rethinkdb) | [RethinkDB](https://interfaces.antelopejs.com/docs/rethinkdb/introduction) |
20
+ | [`@antelopejs/interface-database-catalog`](https://github.com/AntelopeJS/interface-database-catalog) | [External mode](docs/external-mode.md#the-catalog) |
18
21
 
19
22
  Schemas, tables, queries, change feeds and atomic mutations are documented on the database interface pages.
20
23
 
@@ -67,12 +70,16 @@ config: {
67
70
  },
68
71
  ```
69
72
 
70
- | Key | Description |
71
- | ------------ | ------------------------------------------------------------------------------------------------------------- |
72
- | `pool` | `rethinkdb-ts` pool options (`RPoolConnectionOptions`), passed to `r.connectPool()`. |
73
- | `connection` | `rethinkdb-ts` connection options (`RConnectionOptions`), passed to `r.connect()`. Used when `pool` is unset. |
73
+ | Key | Description |
74
+ | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
75
+ | `pool` | `rethinkdb-ts` pool options (`RPoolConnectionOptions`), passed to `r.connectPool()`. |
76
+ | `connection` | `rethinkdb-ts` connection options (`RConnectionOptions`), passed to `r.connect()`. Used when `pool` is unset. |
77
+ | `url` | `rethinkdb://user:password@host:port/database`, parsed into pool options. Required in external mode. |
78
+ | `mode` | `owned` (default): the module creates a database per schema and scopes rows to instances. `external`: it serves a database it did not create, see [External mode](docs/external-mode.md). |
79
+ | `statementTimeoutMs` | Time limit of each read of an external instance, enforced by closing that read's connection. |
80
+ | `catalog` | `{ enabled?: boolean }`: whether an external instance answers the catalog outside development. |
74
81
 
75
- The module refuses to start when neither key is set. The option types come from [`rethinkdb-ts`](https://github.com/rethinkdb/rethinkdb-ts).
82
+ The module refuses to start when none of `url`, `pool` and `connection` is set. The option types come from [`rethinkdb-ts`](https://github.com/rethinkdb/rethinkdb-ts).
76
83
 
77
84
  ## Quick start
78
85
 
@@ -82,7 +89,7 @@ In the module that stores data, add the database interface:
82
89
  pnpm add @antelopejs/interface-database
83
90
  ```
84
91
 
85
- Then follow the [database interface guide](https://framework.antelopejs.com/docs/guides/database) to declare a schema and query it. The same code runs on [`@antelopejs/mongodb`](https://github.com/AntelopeJS/mongodb).
92
+ Then follow the [database interface guide](https://framework.antelopejs.com/docs/guides/database) to declare a schema and query it. The same code runs on [`@antelopejs/postgres`](https://github.com/AntelopeJS/postgres) and [`@antelopejs/mongodb`](https://github.com/AntelopeJS/mongodb).
86
93
 
87
94
  To use the driver directly instead, add `@antelopejs/interface-rethinkdb` and read its [documentation](https://interfaces.antelopejs.com/docs/rethinkdb/introduction).
88
95
 
@@ -100,15 +107,28 @@ Version 1.4 added the `<name>__i` indexes. The first boot after an upgrade build
100
107
 
101
108
  - [Database](https://interfaces.antelopejs.com/docs/database): the `@antelopejs/interface-database` contract this module implements
102
109
  - [RethinkDB](https://interfaces.antelopejs.com/docs/rethinkdb/introduction): the `@antelopejs/interface-rethinkdb` contract this module implements
110
+ - [External mode](docs/external-mode.md): serving a database the module did not create, and its catalog
103
111
  - [Configuration](https://framework.antelopejs.com/docs/concepts/configuration): module sources, `config` blocks and config variables in `antelope.config.ts`
104
112
 
105
113
  ## Compatibility
106
114
 
107
- | Package | Supported range |
108
- | --------------------------------- | ---------------- |
109
- | `@antelopejs/interface-database` | `^0.1.8` |
110
- | `@antelopejs/interface-rethinkdb` | `>=0.0.5 <1.0.0` |
111
- | `@antelopejs/interface-core` | `>=0.1.1 <1.0.0` |
115
+ <!-- antelopejs-docs-compat:start -->
116
+
117
+ | Package | Supported | Tested with |
118
+ | ---------------------------------------- | ---------------- | ----------- |
119
+ | `@antelopejs/interface-core` | `>=0.2.4 <1.0.0` | `0.2.4` |
120
+ | `@antelopejs/interface-database` | `>=0.2.0 <0.3.0` | `0.2.0` |
121
+ | `@antelopejs/interface-database-catalog` | `>=0.0.3 <0.1.0` | `0.0.3` |
122
+ | `@antelopejs/interface-rethinkdb` | `>=0.1.0 <0.2.0` | `0.1.0` |
123
+
124
+ <!-- antelopejs-docs-compat:end -->
125
+
126
+ | Package | Supported range |
127
+ | ---------------------------------------- | ---------------- |
128
+ | `@antelopejs/interface-database` | `>=0.2.0 <0.3.0` |
129
+ | `@antelopejs/interface-rethinkdb` | `>=0.1.0 <0.2.0` |
130
+ | `@antelopejs/interface-database-catalog` | `>=0.0.3 <0.1.0` |
131
+ | `@antelopejs/interface-core` | `>=0.2.4 <1.0.0` |
112
132
 
113
133
  The module is developed and tested against `@antelopejs/core` `^1.13.4` on Node.js 24, with `rethinkdb-ts` `^2.7.0` and RethinkDB 2.4.
114
134
 
@@ -0,0 +1,25 @@
1
+ import type { RConnectionOptions, RPoolConnectionOptions } from "rethinkdb-ts";
2
+ export type Mode = "owned" | "external";
3
+ export interface CatalogOptions {
4
+ enabled?: boolean;
5
+ }
6
+ export interface Options {
7
+ mode?: Mode;
8
+ url?: string;
9
+ connection?: RConnectionOptions;
10
+ pool?: RPoolConnectionOptions;
11
+ statementTimeoutMs?: number;
12
+ catalog?: CatalogOptions;
13
+ }
14
+ export interface ResolvedOptions {
15
+ mode: Mode;
16
+ pool?: RPoolConnectionOptions;
17
+ connection?: RConnectionOptions;
18
+ database?: string;
19
+ statementTimeoutMs?: number;
20
+ catalog: CatalogOptions;
21
+ }
22
+ export declare function SetOptions(options?: Options): ResolvedOptions;
23
+ export declare function GetOptions(): ResolvedOptions;
24
+ export declare function IsExternal(): boolean;
25
+ export declare function ExternalDatabaseName(): string;
package/dist/config.js ADDED
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SetOptions = SetOptions;
4
+ exports.GetOptions = GetOptions;
5
+ exports.IsExternal = IsExternal;
6
+ exports.ExternalDatabaseName = ExternalDatabaseName;
7
+ const DEFAULT_MODE = "owned";
8
+ const MODES = ["owned", "external"];
9
+ const URL_PROTOCOL = "rethinkdb:";
10
+ const DEFAULT_PORT = 28015;
11
+ const SMALLEST_POSITIVE_INTEGER = 1;
12
+ const POOL_DEFAULTS = {
13
+ pool: true,
14
+ cursor: false,
15
+ silent: true,
16
+ discovery: false,
17
+ };
18
+ const NOT_CONFIGURED = "RethinkDB adapter is not configured: construct() has not run yet";
19
+ let resolved;
20
+ function ResolveMode(options) {
21
+ const mode = options?.mode ?? DEFAULT_MODE;
22
+ if (!MODES.includes(mode)) {
23
+ throw new Error(`mode must be one of ${MODES.join(", ")}, received ${String(mode)}`);
24
+ }
25
+ return mode;
26
+ }
27
+ function ParseUrl(url) {
28
+ const parsed = new URL(url);
29
+ if (parsed.protocol !== URL_PROTOCOL) {
30
+ throw new Error(`url must start with rethinkdb://, received ${parsed.protocol}//`);
31
+ }
32
+ const database = decodeURIComponent(parsed.pathname.replace(/^\//, ""));
33
+ return {
34
+ connection: {
35
+ host: parsed.hostname,
36
+ port: parsed.port ? Number(parsed.port) : DEFAULT_PORT,
37
+ user: decodeURIComponent(parsed.username) || "admin",
38
+ password: decodeURIComponent(parsed.password),
39
+ },
40
+ database: database === "" ? undefined : database,
41
+ };
42
+ }
43
+ function ExternalDatabase(options, parsed) {
44
+ if (!parsed) {
45
+ throw new Error(`mode "external" needs a non-empty config.url; set it through the project's envOverrides`);
46
+ }
47
+ if (!parsed.database) {
48
+ throw new Error(`mode "external" needs the database as the path of config.url`);
49
+ }
50
+ const poolDatabase = options?.pool?.db;
51
+ if (poolDatabase !== undefined && poolDatabase !== parsed.database) {
52
+ throw new Error(`pool.db (${poolDatabase}) disagrees with the database of config.url (${parsed.database})`);
53
+ }
54
+ return parsed.database;
55
+ }
56
+ function PositiveSafeInteger(option, supplied) {
57
+ if (supplied === undefined) {
58
+ return undefined;
59
+ }
60
+ if (!Number.isSafeInteger(supplied) || supplied < SMALLEST_POSITIVE_INTEGER) {
61
+ throw new Error(`${option} must be a positive safe integer, received ${supplied}`);
62
+ }
63
+ return supplied;
64
+ }
65
+ function UrlOf(options) {
66
+ return options?.url !== undefined && options.url.trim() !== ""
67
+ ? ParseUrl(options.url)
68
+ : undefined;
69
+ }
70
+ function SetOptions(options) {
71
+ const mode = ResolveMode(options);
72
+ const parsed = UrlOf(options);
73
+ const database = mode === "external" ? ExternalDatabase(options, parsed) : undefined;
74
+ const pool = parsed
75
+ ? { ...POOL_DEFAULTS, ...options?.pool, ...parsed.connection }
76
+ : options?.pool;
77
+ if (!pool && !options?.connection) {
78
+ throw new Error("Invalid RethinkDB options: set url, pool or connection");
79
+ }
80
+ resolved = {
81
+ mode,
82
+ pool,
83
+ connection: pool ? undefined : options?.connection,
84
+ database,
85
+ statementTimeoutMs: PositiveSafeInteger("statementTimeoutMs", options?.statementTimeoutMs),
86
+ catalog: options?.catalog ?? {},
87
+ };
88
+ return resolved;
89
+ }
90
+ function GetOptions() {
91
+ if (!resolved) {
92
+ throw new Error(NOT_CONFIGURED);
93
+ }
94
+ return resolved;
95
+ }
96
+ function IsExternal() {
97
+ return GetOptions().mode === "external";
98
+ }
99
+ function ExternalDatabaseName() {
100
+ const database = GetOptions().database;
101
+ if (!database) {
102
+ throw new Error("ExternalDatabaseName() is only defined on an external instance");
103
+ }
104
+ return database;
105
+ }
106
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";;AA0JA,gCAuBC;AAED,gCAKC;AAGD,gCAEC;AAGD,oDAQC;AA7ID,MAAM,YAAY,GAAS,OAAO,CAAC;AACnC,MAAM,KAAK,GAAoB,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;AACrD,MAAM,YAAY,GAAG,YAAY,CAAC;AAClC,MAAM,YAAY,GAAG,KAAK,CAAC;AAC3B,MAAM,yBAAyB,GAAG,CAAC,CAAC;AACpC,MAAM,aAAa,GAA2B;IAC5C,IAAI,EAAE,IAAI;IACV,MAAM,EAAE,KAAK;IACb,MAAM,EAAE,IAAI;IACZ,SAAS,EAAE,KAAK;CACjB,CAAC;AAEF,MAAM,cAAc,GAClB,kEAAkE,CAAC;AAErE,IAAI,QAAqC,CAAC;AAE1C,SAAS,WAAW,CAAC,OAAiB;IACpC,MAAM,IAAI,GAAG,OAAO,EAAE,IAAI,IAAI,YAAY,CAAC;IAC3C,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CACb,uBAAuB,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,cAAc,MAAM,CAAC,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EAAE,CAAC;QAClC,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;IAC5E,CAAC;IACD,QAAQ,GAAG;QACT,IAAI;QACJ,IAAI;QACJ,UAAU,EAAE,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,EAAE,UAAU;QAClD,QAAQ;QACR,kBAAkB,EAAE,mBAAmB,CACrC,oBAAoB,EACpB,OAAO,EAAE,kBAAkB,CAC5B;QACD,OAAO,EAAE,OAAO,EAAE,OAAO,IAAI,EAAE;KAChC,CAAC;IACF,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,SAAgB,UAAU;IACxB,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC;IAClC,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAGD,SAAgB,UAAU;IACxB,OAAO,UAAU,EAAE,CAAC,IAAI,KAAK,UAAU,CAAC;AAC1C,CAAC;AAGD,SAAgB,oBAAoB;IAClC,MAAM,QAAQ,GAAG,UAAU,EAAE,CAAC,QAAQ,CAAC;IACvC,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;IACJ,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC"}
@@ -1,8 +1,23 @@
1
+ import { type Connection, type RConnectionOptions, type RPoolConnectionOptions, type RunOptions } from "rethinkdb-ts";
1
2
  import type { Cursor } from "rethinkdb-ts/lib/response/cursor";
2
3
  import type { TermJson } from "rethinkdb-ts/lib/internal-types";
3
- import { type RConnectionOptions, type RPoolConnectionOptions, type RunOptions } from "rethinkdb-ts";
4
4
  export declare function ConnectDirect(options: RConnectionOptions): Promise<void>;
5
5
  export declare function ConnectPool(options: RPoolConnectionOptions): Promise<void>;
6
6
  export declare function Disconnect(): void;
7
7
  export declare function SendQuery(query: TermJson, opts?: RunOptions): Promise<Cursor | undefined>;
8
8
  export declare function executeTermJson(term: TermJson): Promise<any>;
9
+ export declare class QueryTimeoutError extends Error {
10
+ readonly status = "timeout";
11
+ constructor(timeoutMs: number);
12
+ }
13
+ export declare function SetDirectOptions(options: RConnectionOptions): void;
14
+ export interface BoundedCursor {
15
+ cursor: Cursor;
16
+ arm: () => void;
17
+ disarm: () => void;
18
+ release: () => void;
19
+ expired: () => boolean;
20
+ }
21
+ export declare function OpenBoundedCursor(term: TermJson, timeoutMs: number): Promise<BoundedCursor>;
22
+ export declare function DirectConnection(): Promise<Connection>;
23
+ export declare function executeBounded(term: TermJson, timeoutMs: number): Promise<any>;
@@ -3,23 +3,40 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.QueryTimeoutError = void 0;
6
7
  exports.ConnectDirect = ConnectDirect;
7
8
  exports.ConnectPool = ConnectPool;
8
9
  exports.Disconnect = Disconnect;
9
10
  exports.SendQuery = SendQuery;
10
11
  exports.executeTermJson = executeTermJson;
12
+ exports.SetDirectOptions = SetDirectOptions;
13
+ exports.OpenBoundedCursor = OpenBoundedCursor;
14
+ exports.DirectConnection = DirectConnection;
15
+ exports.executeBounded = executeBounded;
11
16
  const node_assert_1 = __importDefault(require("node:assert"));
12
- const term_backtrace_1 = require("rethinkdb-ts/lib/error/term-backtrace");
13
17
  const rethinkdb_ts_1 = require("rethinkdb-ts");
18
+ const term_backtrace_1 = require("rethinkdb-ts/lib/error/term-backtrace");
19
+ const connection_1 = require("rethinkdb-ts/lib/connection/connection");
20
+ const master_pool_1 = require("rethinkdb-ts/lib/connection/master-pool");
14
21
  const logger_1 = require("./utils/logger");
15
22
  const write_result_1 = require("./write-result");
16
23
  let connection;
24
+ async function OpenConnection(options) {
25
+ const opened = await rethinkdb_ts_1.r.connect(options);
26
+ (0, node_assert_1.default)(opened instanceof connection_1.RethinkDBConnection, "r.connect answered no RethinkDBConnection");
27
+ return opened;
28
+ }
29
+ async function OpenPool(options) {
30
+ const opened = await rethinkdb_ts_1.r.connectPool(options);
31
+ (0, node_assert_1.default)(opened instanceof master_pool_1.MasterConnectionPool, "r.connectPool answered no MasterConnectionPool");
32
+ return opened;
33
+ }
17
34
  async function ConnectDirect(options) {
18
35
  logger_1.Logger.Debug("Connecting directly to RethinkDB", options.host ?? "localhost");
19
36
  Disconnect();
20
37
  connection = {
21
38
  type: "direct",
22
- connection: await rethinkdb_ts_1.r.connect(options),
39
+ connection: await OpenConnection(options),
23
40
  };
24
41
  logger_1.Logger.Debug("Connected directly to RethinkDB");
25
42
  }
@@ -28,7 +45,7 @@ async function ConnectPool(options) {
28
45
  Disconnect();
29
46
  connection = {
30
47
  type: "pool",
31
- connection: await rethinkdb_ts_1.r.connectPool(options),
48
+ connection: await OpenPool(options),
32
49
  };
33
50
  logger_1.Logger.Debug("Connected to RethinkDB pool");
34
51
  }
@@ -55,9 +72,7 @@ function SendQuery(query, opts) {
55
72
  return connection.connection.queue(query, opts);
56
73
  }
57
74
  }
58
- async function executeTermJson(term) {
59
- logger_1.Logger.Debug("Executing query:", (0, term_backtrace_1.backtraceTerm)(term)[0]);
60
- const cursor = await SendQuery(term);
75
+ async function Resolve(term, cursor) {
61
76
  if (!cursor) {
62
77
  return undefined;
63
78
  }
@@ -75,4 +90,78 @@ async function executeTermJson(term) {
75
90
  }
76
91
  return results;
77
92
  }
93
+ async function executeTermJson(term) {
94
+ logger_1.Logger.Debug("Executing query:", (0, term_backtrace_1.backtraceTerm)(term)[0]);
95
+ return Resolve(term, await SendQuery(term));
96
+ }
97
+ class QueryTimeoutError extends Error {
98
+ status = "timeout";
99
+ constructor(timeoutMs) {
100
+ super(`The query did not finish within ${timeoutMs} ms, and was stopped`);
101
+ this.name = "QueryTimeoutError";
102
+ }
103
+ }
104
+ exports.QueryTimeoutError = QueryTimeoutError;
105
+ let directOptions;
106
+ function SetDirectOptions(options) {
107
+ directOptions = options;
108
+ }
109
+ async function OpenBoundedCursor(term, timeoutMs) {
110
+ (0, node_assert_1.default)(directOptions, "No connection established");
111
+ const direct = await OpenConnection(directOptions);
112
+ let expired = false;
113
+ let timer;
114
+ let expire = () => undefined;
115
+ const deadline = new Promise((_, reject) => {
116
+ expire = reject;
117
+ });
118
+ const disarm = () => clearTimeout(timer);
119
+ const arm = () => {
120
+ disarm();
121
+ timer = setTimeout(() => {
122
+ expired = true;
123
+ expire(new QueryTimeoutError(timeoutMs));
124
+ void direct.close().catch(() => undefined);
125
+ }, timeoutMs);
126
+ };
127
+ const release = () => {
128
+ disarm();
129
+ void direct.close().catch(() => undefined);
130
+ };
131
+ arm();
132
+ try {
133
+ const opening = direct.query(term);
134
+ const cursor = await Promise.race([opening, deadline]);
135
+ (0, node_assert_1.default)(cursor, "Query returned no cursor.");
136
+ disarm();
137
+ return { cursor, arm, disarm, release, expired: () => expired };
138
+ }
139
+ catch (error) {
140
+ release();
141
+ throw error;
142
+ }
143
+ }
144
+ function DirectConnection() {
145
+ (0, node_assert_1.default)(directOptions, "No connection established");
146
+ return rethinkdb_ts_1.r.connect(directOptions);
147
+ }
148
+ async function executeBounded(term, timeoutMs) {
149
+ (0, node_assert_1.default)(directOptions, "No connection established");
150
+ const direct = await OpenConnection(directOptions);
151
+ let timer;
152
+ const expired = new Promise((_, reject) => {
153
+ timer = setTimeout(() => {
154
+ reject(new QueryTimeoutError(timeoutMs));
155
+ void direct.close();
156
+ }, timeoutMs);
157
+ });
158
+ try {
159
+ const running = direct.query(term).then((cursor) => Resolve(term, cursor));
160
+ return await Promise.race([running, expired]);
161
+ }
162
+ finally {
163
+ clearTimeout(timer);
164
+ void direct.close().catch(() => undefined);
165
+ }
166
+ }
78
167
  //# sourceMappingURL=connection.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"connection.js","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":";;;;;AA6BA,sCAQC;AAED,kCAQC;AAED,gCAYC;AAED,8BAYC;AAED,0CAmBC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GAAG,MAAM,cAAc,CAAC,aAAa,CAAC,CAAC;IACnD,IAAI,KAAiC,CAAC;IACtC,MAAM,OAAO,GAAG,IAAI,OAAO,CAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE;QAC/C,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE;YACtB,MAAM,CAAC,IAAI,iBAAiB,CAAC,SAAS,CAAC,CAAC,CAAC;YACzC,KAAK,MAAM,CAAC,KAAK,EAAE,CAAC;QACtB,CAAC,EAAE,SAAS,CAAC,CAAC;IAChB,CAAC,CAAC,CAAC;IACH,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC;QAC3E,OAAO,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;IAChD,CAAC;YAAS,CAAC;QACT,YAAY,CAAC,KAAK,CAAC,CAAC;QACpB,KAAK,MAAM,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;IAC7C,CAAC;AACH,CAAC"}
@@ -0,0 +1,11 @@
1
+ import type { CatalogInfo, RelationDescription, RelationSummary } from "@antelopejs/interface-database-catalog";
2
+ export interface ListedTable {
3
+ name: string;
4
+ primaryKey: string;
5
+ estimatedRows: number;
6
+ uuidKeys: boolean;
7
+ }
8
+ export declare function ReadInfo(): Promise<CatalogInfo>;
9
+ export declare function ListTable(name: string): Promise<ListedTable>;
10
+ export declare function Summarize(table: ListedTable): Promise<RelationSummary>;
11
+ export declare function DescribeTable(table: ListedTable): Promise<RelationDescription>;
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReadInfo = ReadInfo;
4
+ exports.ListTable = ListTable;
5
+ exports.Summarize = Summarize;
6
+ exports.DescribeTable = DescribeTable;
7
+ const rethinkdb_ts_1 = require("rethinkdb-ts");
8
+ const config_1 = require("../../config");
9
+ const external_descriptor_1 = require("../database/external-descriptor");
10
+ const external_probe_1 = require("../database/external-probe");
11
+ const measures_1 = require("./measures");
12
+ const DEFAULT_PRIMARY_KEY = "id";
13
+ const KEY_SAMPLE = 1000;
14
+ const FALLBACK_ROWS = 1000;
15
+ const UNREACHABLE_KEY = "__antelopejs_write_probe__ no row has this key";
16
+ const DOMINANT_SHARE = 0.8;
17
+ const PERCENT = 100;
18
+ const NULL_TYPE = "NULL";
19
+ const READ_ONLY = "an embedded value comes back altered by a read, so it is never written";
20
+ const READINGS = new Map([
21
+ ["STRING", "string"],
22
+ ["NUMBER", "number"],
23
+ ["BOOL", "boolean"],
24
+ ["PTYPE<TIME>", "date"],
25
+ ["OBJECT", "object"],
26
+ ["ARRAY", "array"],
27
+ ]);
28
+ async function ReadInfo() {
29
+ const version = await rethinkdb_ts_1.r
30
+ .db("rethinkdb")
31
+ .table("server_status")
32
+ .nth(0)("process")("version")
33
+ .run()
34
+ .catch(() => "unknown");
35
+ return {
36
+ engine: "rethinkdb",
37
+ version,
38
+ database: (0, config_1.ExternalDatabaseName)(),
39
+ role: (0, config_1.GetOptions)().pool?.user ?? "admin",
40
+ };
41
+ }
42
+ async function ListTable(name) {
43
+ const info = await (0, external_probe_1.Table)(name).info().run();
44
+ const primaryKey = info.primary_key ?? DEFAULT_PRIMARY_KEY;
45
+ const keys = await (0, external_probe_1.Table)(name).limit(KEY_SAMPLE)(primaryKey).run();
46
+ return {
47
+ name,
48
+ primaryKey,
49
+ estimatedRows: (info.doc_count_estimates ?? []).reduce((sum, count) => sum + count, 0),
50
+ uuidKeys: keys.length > 0 &&
51
+ keys.every((key) => external_probe_1.UUID_TEXT_PATTERN.test(String(key))),
52
+ };
53
+ }
54
+ async function Writes(name) {
55
+ const allowed = await (0, external_probe_1.Table)(name)
56
+ .get(UNREACHABLE_KEY)
57
+ .delete()
58
+ .run()
59
+ .then(() => true, () => false);
60
+ return { insert: allowed, update: allowed, delete: allowed };
61
+ }
62
+ async function Summarize(table) {
63
+ const finding = (0, external_descriptor_1.KeyOf)({ ends: await (0, external_probe_1.Ends)(table.name, table.primaryKey) });
64
+ return {
65
+ schema: (0, config_1.ExternalDatabaseName)(),
66
+ name: table.name,
67
+ kind: "table",
68
+ estimatedRows: table.estimatedRows,
69
+ estimate: "statistics",
70
+ key: finding.key
71
+ ? { columns: [table.primaryKey], source: "primary" }
72
+ : null,
73
+ writes: await Writes(table.name),
74
+ comment: null,
75
+ };
76
+ }
77
+ async function Indexes(name) {
78
+ const statuses = await (0, external_probe_1.Table)(name)
79
+ .indexStatus()
80
+ .map((status) => ({
81
+ index: status("index"),
82
+ multi: status("multi"),
83
+ query: status("query"),
84
+ }))
85
+ .run();
86
+ return statuses.map((status) => ({
87
+ name: status.index,
88
+ fields: (0, external_probe_1.IndexFields)(status.query),
89
+ multi: status.multi,
90
+ }));
91
+ }
92
+ function FieldTypes(rows) {
93
+ return (0, measures_1.Described)(rows
94
+ .concatMap((row) => row.keys().map((key) => ({ f: key, t: row(key).typeOf() })))
95
+ .group("f", "t")
96
+ .count()
97
+ .ungroup()
98
+ .map((group) => group("group").append(group("reduction"))));
99
+ }
100
+ async function SampledFields(source) {
101
+ const measured = await FieldTypes(source.rows());
102
+ const groups = (0, measures_1.ValueOf)(measured.status === "timeout"
103
+ ? await FieldTypes(source.head(FALLBACK_ROWS))
104
+ : measured) ?? [];
105
+ const fields = new Map();
106
+ for (const [field, type, count] of groups) {
107
+ fields.set(field, [...(fields.get(field) ?? []), { type, count }]);
108
+ }
109
+ return fields;
110
+ }
111
+ function Values(types) {
112
+ return types
113
+ .filter((entry) => entry.type !== NULL_TYPE)
114
+ .sort((left, right) => right.count - left.count);
115
+ }
116
+ function Total(types) {
117
+ return types.reduce((sum, entry) => sum + entry.count, 0);
118
+ }
119
+ function Shares(types) {
120
+ const values = Values(types);
121
+ return values
122
+ .map((entry) => `${entry.type} ${Math.round((entry.count / Total(values)) * PERCENT)}%`)
123
+ .join(" | ");
124
+ }
125
+ function Accepts(types, elements) {
126
+ const values = Values(types);
127
+ const [first] = values;
128
+ const kind = first && first.count >= DOMINANT_SHARE * Total(values)
129
+ ? READINGS.get(first.type)
130
+ : undefined;
131
+ if (!kind) {
132
+ return [];
133
+ }
134
+ const unread = values.filter((entry) => entry !== first);
135
+ const lossy = unread.map((entry) => `${entry.count} of ${Total(values)} values are ${entry.type} and read as null`);
136
+ const declared = kind === "array" ? ArrayDeclaration(elements) : kind;
137
+ if (!declared) {
138
+ return [];
139
+ }
140
+ return [
141
+ lossy.length > 0 ? { declared, lossy: lossy.join("; ") } : { declared },
142
+ ];
143
+ }
144
+ function ArrayDeclaration(elements) {
145
+ const values = Values(elements);
146
+ const kind = values.length === 1 ? READINGS.get(values[0].type) : undefined;
147
+ return kind && kind !== "array" ? `${kind}[]` : undefined;
148
+ }
149
+ async function ElementTypes(source, field) {
150
+ const groups = (0, measures_1.ValueOf)(await (0, measures_1.Described)(source
151
+ .rows()
152
+ .concatMap((row) => row(field).default([]))
153
+ .group((element) => element.typeOf())
154
+ .count()
155
+ .ungroup()
156
+ .map((group) => [group("group"), group("reduction")]))) ?? [];
157
+ return groups.map(([type, count]) => ({ type, count }));
158
+ }
159
+ async function Column(source, table, name, types, rows) {
160
+ const elements = Values(types)[0]?.type === "ARRAY" ? await ElementTypes(source, name) : [];
161
+ const accepts = Accepts(types, elements);
162
+ const kind = READINGS.get(Values(types)[0]?.type ?? "");
163
+ const column = {
164
+ name,
165
+ native: Shares(types) || "NULL",
166
+ nullable: Total(Values(types)) < rows,
167
+ hasDefault: name === table.primaryKey && table.uuidKeys,
168
+ generated: false,
169
+ identity: null,
170
+ accepts,
171
+ comment: null,
172
+ };
173
+ if (accepts.length === 0) {
174
+ column.unsupported =
175
+ Values(types).length === 0
176
+ ? "the sample holds only null for it"
177
+ : `no type covers 80% of the sampled values: ${Shares(types)}`;
178
+ }
179
+ if (kind === "object" || kind === "array") {
180
+ column.readOnly = READ_ONLY;
181
+ }
182
+ return column;
183
+ }
184
+ async function DescribeTable(table) {
185
+ const summary = await Summarize(table);
186
+ const source = (0, measures_1.SourceFor)(table.name, table.estimatedRows, table.uuidKeys);
187
+ const fields = await SampledFields(source);
188
+ const rows = Math.max(0, ...[...fields.values()].map((types) => Total(types)));
189
+ const names = [
190
+ table.primaryKey,
191
+ ...[...fields.keys()].filter((name) => name !== table.primaryKey),
192
+ ];
193
+ const columns = [];
194
+ for (const name of names.filter((field) => fields.has(field))) {
195
+ columns.push(await Column(source, table, name, fields.get(name) ?? [], rows));
196
+ }
197
+ return {
198
+ ...summary,
199
+ columns,
200
+ indexes: (await Indexes(table.name)).map((index) => ({
201
+ name: index.name,
202
+ columns: index.fields,
203
+ unique: false,
204
+ })),
205
+ references: [],
206
+ referencedBy: [],
207
+ };
208
+ }
209
+ //# sourceMappingURL=discovery.js.map