@geekmidas/db 10.0.0-alpha.98 → 10.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 (146) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/README.md +0 -17
  3. package/dist/kysely/pagination.cjs +9 -8
  4. package/dist/kysely/pagination.cjs.map +1 -1
  5. package/dist/kysely/pagination.d.cts +14 -4
  6. package/dist/kysely/pagination.d.cts.map +1 -1
  7. package/dist/kysely/pagination.d.mts +14 -4
  8. package/dist/kysely/pagination.d.mts.map +1 -1
  9. package/dist/kysely/pagination.mjs +10 -7
  10. package/dist/kysely/pagination.mjs.map +1 -1
  11. package/dist/kysely-8WPSKCZG.cjs +17 -0
  12. package/dist/kysely-8WPSKCZG.cjs.map +1 -0
  13. package/dist/kysely-D3eeuId4.d.cts +13 -0
  14. package/dist/kysely-D3eeuId4.d.cts.map +1 -0
  15. package/dist/kysely-Dm1w5gAY.d.mts +13 -0
  16. package/dist/kysely-Dm1w5gAY.d.mts.map +1 -0
  17. package/dist/kysely-DmfA94RY.mjs +11 -0
  18. package/dist/kysely-DmfA94RY.mjs.map +1 -0
  19. package/dist/kysely.cjs +2 -11
  20. package/dist/kysely.d.cts +2 -9
  21. package/dist/kysely.d.mts +2 -9
  22. package/dist/kysely.mjs +2 -10
  23. package/dist/objection/pagination.cjs +7 -7
  24. package/dist/objection/pagination.cjs.map +1 -1
  25. package/dist/objection/pagination.d.cts +14 -4
  26. package/dist/objection/pagination.d.cts.map +1 -1
  27. package/dist/objection/pagination.d.mts +14 -4
  28. package/dist/objection/pagination.d.mts.map +1 -1
  29. package/dist/objection/pagination.mjs +8 -6
  30. package/dist/objection/pagination.mjs.map +1 -1
  31. package/dist/{pagination-CrU89125.d.cts → pagination-BAFX7C1I.d.cts} +4 -14
  32. package/dist/pagination-BAFX7C1I.d.cts.map +1 -0
  33. package/dist/pagination-BDLa7Yb_.mjs +37 -0
  34. package/dist/pagination-BDLa7Yb_.mjs.map +1 -0
  35. package/dist/pagination-Bdoa4PVj.cjs +55 -0
  36. package/dist/pagination-Bdoa4PVj.cjs.map +1 -0
  37. package/dist/{pagination-CrU89125.d.mts → pagination-BziGl-B8.d.mts} +4 -14
  38. package/dist/pagination-BziGl-B8.d.mts.map +1 -0
  39. package/dist/pagination.cjs +4 -55
  40. package/dist/pagination.d.cts +2 -2
  41. package/dist/pagination.d.mts +2 -2
  42. package/dist/pagination.mjs +2 -51
  43. package/dist/rls.cjs +29 -5
  44. package/dist/rls.cjs.map +1 -1
  45. package/dist/rls.d.cts +10 -6
  46. package/dist/rls.d.cts.map +1 -1
  47. package/dist/rls.d.mts +10 -6
  48. package/dist/rls.d.mts.map +1 -1
  49. package/dist/rls.mjs +5 -3
  50. package/dist/rls.mjs.map +1 -1
  51. package/package.json +9 -53
  52. package/src/__tests__/kysely.integration.spec.ts +741 -0
  53. package/src/__tests__/kysely.spec.ts +394 -0
  54. package/src/__tests__/rls.spec.ts +716 -0
  55. package/src/kysely/__tests__/pagination.integration.spec.ts +341 -0
  56. package/src/kysely/pagination.ts +113 -0
  57. package/src/kysely.ts +33 -0
  58. package/src/objection/__tests__/pagination.integration.spec.ts +291 -0
  59. package/src/objection/pagination.ts +98 -0
  60. package/src/pagination.ts +49 -0
  61. package/src/rls.ts +90 -0
  62. package/tsconfig.json +9 -0
  63. package/dist/errors.cjs +0 -73
  64. package/dist/errors.cjs.map +0 -1
  65. package/dist/errors.d.cts +0 -51
  66. package/dist/errors.d.cts.map +0 -1
  67. package/dist/errors.d.mts +0 -51
  68. package/dist/errors.d.mts.map +0 -1
  69. package/dist/errors.mjs +0 -69
  70. package/dist/errors.mjs.map +0 -1
  71. package/dist/introspect/DataBrowser.cjs +0 -117
  72. package/dist/introspect/DataBrowser.cjs.map +0 -1
  73. package/dist/introspect/DataBrowser.d.cts +0 -51
  74. package/dist/introspect/DataBrowser.d.cts.map +0 -1
  75. package/dist/introspect/DataBrowser.d.mts +0 -51
  76. package/dist/introspect/DataBrowser.d.mts.map +0 -1
  77. package/dist/introspect/DataBrowser.mjs +0 -116
  78. package/dist/introspect/DataBrowser.mjs.map +0 -1
  79. package/dist/introspect/errors.cjs +0 -48
  80. package/dist/introspect/errors.cjs.map +0 -1
  81. package/dist/introspect/errors.d.cts +0 -27
  82. package/dist/introspect/errors.d.cts.map +0 -1
  83. package/dist/introspect/errors.d.mts +0 -27
  84. package/dist/introspect/errors.d.mts.map +0 -1
  85. package/dist/introspect/errors.mjs +0 -45
  86. package/dist/introspect/errors.mjs.map +0 -1
  87. package/dist/introspect/filtering.cjs +0 -101
  88. package/dist/introspect/filtering.cjs.map +0 -1
  89. package/dist/introspect/filtering.d.cts +0 -29
  90. package/dist/introspect/filtering.d.cts.map +0 -1
  91. package/dist/introspect/filtering.d.mts +0 -29
  92. package/dist/introspect/filtering.d.mts.map +0 -1
  93. package/dist/introspect/filtering.mjs +0 -98
  94. package/dist/introspect/filtering.mjs.map +0 -1
  95. package/dist/introspect/handler.cjs +0 -159
  96. package/dist/introspect/handler.cjs.map +0 -1
  97. package/dist/introspect/handler.d.cts +0 -43
  98. package/dist/introspect/handler.d.cts.map +0 -1
  99. package/dist/introspect/handler.d.mts +0 -43
  100. package/dist/introspect/handler.d.mts.map +0 -1
  101. package/dist/introspect/handler.mjs +0 -158
  102. package/dist/introspect/handler.mjs.map +0 -1
  103. package/dist/introspect/index.cjs +0 -22
  104. package/dist/introspect/index.d.cts +0 -8
  105. package/dist/introspect/index.d.mts +0 -8
  106. package/dist/introspect/index.mjs +0 -8
  107. package/dist/introspect/introspection.cjs +0 -149
  108. package/dist/introspect/introspection.cjs.map +0 -1
  109. package/dist/introspect/introspection.d.cts +0 -25
  110. package/dist/introspect/introspection.d.cts.map +0 -1
  111. package/dist/introspect/introspection.d.mts +0 -25
  112. package/dist/introspect/introspection.d.mts.map +0 -1
  113. package/dist/introspect/introspection.mjs +0 -146
  114. package/dist/introspect/introspection.mjs.map +0 -1
  115. package/dist/introspect/types.cjs +0 -24
  116. package/dist/introspect/types.cjs.map +0 -1
  117. package/dist/introspect/types.d.cts +0 -113
  118. package/dist/introspect/types.d.cts.map +0 -1
  119. package/dist/introspect/types.d.mts +0 -113
  120. package/dist/introspect/types.d.mts.map +0 -1
  121. package/dist/introspect/types.mjs +0 -23
  122. package/dist/introspect/types.mjs.map +0 -1
  123. package/dist/kysely.cjs.map +0 -1
  124. package/dist/kysely.d.cts.map +0 -1
  125. package/dist/kysely.d.mts.map +0 -1
  126. package/dist/kysely.mjs.map +0 -1
  127. package/dist/pagination-CrU89125.d.cts.map +0 -1
  128. package/dist/pagination-CrU89125.d.mts.map +0 -1
  129. package/dist/pagination.cjs.map +0 -1
  130. package/dist/pagination.mjs.map +0 -1
  131. package/dist/pg/roles.cjs +0 -177
  132. package/dist/pg/roles.cjs.map +0 -1
  133. package/dist/pg/roles.d.cts +0 -90
  134. package/dist/pg/roles.d.cts.map +0 -1
  135. package/dist/pg/roles.d.mts +0 -90
  136. package/dist/pg/roles.d.mts.map +0 -1
  137. package/dist/pg/roles.mjs +0 -173
  138. package/dist/pg/roles.mjs.map +0 -1
  139. package/dist/pg/url.cjs +0 -48
  140. package/dist/pg/url.cjs.map +0 -1
  141. package/dist/pg/url.d.cts +0 -45
  142. package/dist/pg/url.d.cts.map +0 -1
  143. package/dist/pg/url.d.mts +0 -45
  144. package/dist/pg/url.d.mts.map +0 -1
  145. package/dist/pg/url.mjs +0 -47
  146. package/dist/pg/url.mjs.map +0 -1
package/dist/errors.d.mts DELETED
@@ -1,51 +0,0 @@
1
- //#region src/errors.d.ts
2
- /**
3
- * Database URL errors and redaction — engine-neutral.
4
- *
5
- * Nothing here is Postgres-specific: every engine addresses itself as a URL
6
- * carrying a password, and "must name a host" holds for all of them. The parts
7
- * that differ — libpq's `options=-c search_path=…`, which no other engine
8
- * accepts — live under the driver they belong to.
9
- *
10
- * Messages state the rule, which is constant; the offending value is a field.
11
- * An interpolated message cannot be matched on and carries user input into
12
- * every log line that touches it.
13
- */
14
- /**
15
- * Replace the password in a connection string with `***`.
16
- *
17
- * A connection string is a secret, and the places it is most likely to be
18
- * written down — a log line, an error, a crash report shipped to a third party
19
- * — are the places nobody is reading carefully. This exists so that redacting
20
- * is the easy thing to do at each of them.
21
- *
22
- * Safe on input that is not a URL: it returns a constant rather than throwing,
23
- * because this runs on paths that must not fail. Anything unparseable is
24
- * treated as potentially secret and withheld entirely rather than echoed.
25
- */
26
- export declare function redactDatabaseUrl(url: string): string;
27
- /**
28
- * Base for an address that cannot become a connection string.
29
- *
30
- * Deliberately carries no URL field: these are thrown while *composing* one, so
31
- * there is no URL yet — only the parts, which include the password.
32
- */
33
- export declare abstract class DatabaseAddressError extends Error {
34
- constructor(message: string);
35
- }
36
- /** An address with no host to connect to. */
37
- export declare class MissingDatabaseHost extends DatabaseAddressError {
38
- constructor();
39
- }
40
- /**
41
- * An address with no database name.
42
- *
43
- * Postgres silently falls back to the connecting role's name, so the app
44
- * connects successfully to the wrong database and fails later with confusing
45
- * "relation does not exist" errors. Rejecting it here is much cheaper.
46
- */
47
- export declare class MissingDatabaseName extends DatabaseAddressError {
48
- constructor();
49
- }
50
- //#endregion
51
- //# sourceMappingURL=errors.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.d.mts","names":[],"sources":["../src/errors.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;wBAyBgB,kBAAkB;;;;;;;8BAiBZ,6BAA6B;EAClD,YAAY;;;qBAOA,4BAA4B;EACxC;;;;;;;;;qBAYY,4BAA4B;EACxC"}
package/dist/errors.mjs DELETED
@@ -1,69 +0,0 @@
1
- //#region src/errors.ts
2
- /**
3
- * Database URL errors and redaction — engine-neutral.
4
- *
5
- * Nothing here is Postgres-specific: every engine addresses itself as a URL
6
- * carrying a password, and "must name a host" holds for all of them. The parts
7
- * that differ — libpq's `options=-c search_path=…`, which no other engine
8
- * accepts — live under the driver they belong to.
9
- *
10
- * Messages state the rule, which is constant; the offending value is a field.
11
- * An interpolated message cannot be matched on and carries user input into
12
- * every log line that touches it.
13
- */
14
- /**
15
- * Replace the password in a connection string with `***`.
16
- *
17
- * A connection string is a secret, and the places it is most likely to be
18
- * written down — a log line, an error, a crash report shipped to a third party
19
- * — are the places nobody is reading carefully. This exists so that redacting
20
- * is the easy thing to do at each of them.
21
- *
22
- * Safe on input that is not a URL: it returns a constant rather than throwing,
23
- * because this runs on paths that must not fail. Anything unparseable is
24
- * treated as potentially secret and withheld entirely rather than echoed.
25
- */
26
- function redactDatabaseUrl(url) {
27
- try {
28
- const parsed = new URL(url);
29
- if (!parsed.password) return url;
30
- parsed.password = "***";
31
- return parsed.toString();
32
- } catch {
33
- return "<unparseable url>";
34
- }
35
- }
36
- /**
37
- * Base for an address that cannot become a connection string.
38
- *
39
- * Deliberately carries no URL field: these are thrown while *composing* one, so
40
- * there is no URL yet — only the parts, which include the password.
41
- */
42
- var DatabaseAddressError = class extends Error {
43
- constructor(message) {
44
- super(message);
45
- this.name = new.target.name;
46
- }
47
- };
48
- /** An address with no host to connect to. */
49
- var MissingDatabaseHost = class extends DatabaseAddressError {
50
- constructor() {
51
- super("A database address must name a host");
52
- }
53
- };
54
- /**
55
- * An address with no database name.
56
- *
57
- * Postgres silently falls back to the connecting role's name, so the app
58
- * connects successfully to the wrong database and fails later with confusing
59
- * "relation does not exist" errors. Rejecting it here is much cheaper.
60
- */
61
- var MissingDatabaseName = class extends DatabaseAddressError {
62
- constructor() {
63
- super("A database address must name a database");
64
- }
65
- };
66
- //#endregion
67
- export { DatabaseAddressError, MissingDatabaseHost, MissingDatabaseName, redactDatabaseUrl };
68
-
69
- //# sourceMappingURL=errors.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.mjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["/**\n * Database URL errors and redaction — engine-neutral.\n *\n * Nothing here is Postgres-specific: every engine addresses itself as a URL\n * carrying a password, and \"must name a host\" holds for all of them. The parts\n * that differ — libpq's `options=-c search_path=…`, which no other engine\n * accepts — live under the driver they belong to.\n *\n * Messages state the rule, which is constant; the offending value is a field.\n * An interpolated message cannot be matched on and carries user input into\n * every log line that touches it.\n */\n\n/**\n * Replace the password in a connection string with `***`.\n *\n * A connection string is a secret, and the places it is most likely to be\n * written down — a log line, an error, a crash report shipped to a third party\n * — are the places nobody is reading carefully. This exists so that redacting\n * is the easy thing to do at each of them.\n *\n * Safe on input that is not a URL: it returns a constant rather than throwing,\n * because this runs on paths that must not fail. Anything unparseable is\n * treated as potentially secret and withheld entirely rather than echoed.\n */\nexport function redactDatabaseUrl(url: string): string {\n\ttry {\n\t\tconst parsed = new URL(url);\n\t\tif (!parsed.password) return url;\n\t\tparsed.password = '***';\n\t\treturn parsed.toString();\n\t} catch {\n\t\treturn '<unparseable url>';\n\t}\n}\n\n/**\n * Base for an address that cannot become a connection string.\n *\n * Deliberately carries no URL field: these are thrown while *composing* one, so\n * there is no URL yet — only the parts, which include the password.\n */\nexport abstract class DatabaseAddressError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = new.target.name;\n\t}\n}\n\n/** An address with no host to connect to. */\nexport class MissingDatabaseHost extends DatabaseAddressError {\n\tconstructor() {\n\t\tsuper('A database address must name a host');\n\t}\n}\n\n/**\n * An address with no database name.\n *\n * Postgres silently falls back to the connecting role's name, so the app\n * connects successfully to the wrong database and fails later with confusing\n * \"relation does not exist\" errors. Rejecting it here is much cheaper.\n */\nexport class MissingDatabaseName extends DatabaseAddressError {\n\tconstructor() {\n\t\tsuper('A database address must name a database');\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,kBAAkB,KAAqB;CACtD,IAAI;EACH,MAAM,SAAS,IAAI,IAAI,GAAG;EAC1B,IAAI,CAAC,OAAO,UAAU,OAAO;EAC7B,OAAO,WAAW;EAClB,OAAO,OAAO,SAAS;CACxB,QAAQ;EACP,OAAO;CACR;AACD;;;;;;;AAQA,IAAsB,uBAAtB,cAAmD,MAAM;CACxD,YAAY,SAAiB;EAC5B,MAAM,OAAO;EACb,KAAK,OAAO,WAAW;CACxB;AACD;;AAGA,IAAa,sBAAb,cAAyC,qBAAqB;CAC7D,cAAc;EACb,MAAM,qCAAqC;CAC5C;AACD;;;;;;;;AASA,IAAa,sBAAb,cAAyC,qBAAqB;CAC7D,cAAc;EACb,MAAM,yCAAyC;CAChD;AACD"}
@@ -1,117 +0,0 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_pagination = require("../pagination.cjs");
3
- const require_introspect_errors = require("./errors.cjs");
4
- const require_introspect_filtering = require("./filtering.cjs");
5
- const require_introspect_introspection = require("./introspection.cjs");
6
- //#region src/introspect/DataBrowser.ts
7
- /** How long an introspected schema is reused before it is read again. */
8
- const SCHEMA_TTL_MS = 6e4;
9
- const MAX_PAGE_SIZE = 100;
10
- /**
11
- * Reads a Postgres database's structure and pages through its rows, read-only.
12
- *
13
- * @example
14
- * ```typescript
15
- * const browser = new DataBrowser({ db });
16
- *
17
- * const { tables } = await browser.getSchema();
18
- * const page = await browser.query({ table: 'users', pageSize: 20 });
19
- * const next = await browser.query({ table: 'users', cursor: page.nextCursor });
20
- * ```
21
- */
22
- var DataBrowser = class {
23
- db;
24
- schemas;
25
- cursor;
26
- tableCursors;
27
- excludeTables;
28
- defaultPageSize;
29
- schemaCache;
30
- constructor(options) {
31
- this.db = options.db;
32
- this.schemas = options.schemas ?? ["public"];
33
- this.cursor = options.cursor;
34
- this.tableCursors = options.tableCursors ?? {};
35
- this.excludeTables = options.excludeTables ?? [];
36
- this.defaultPageSize = options.defaultPageSize ?? 50;
37
- }
38
- /** Every schema the database has, browsable or not. */
39
- listSchemas() {
40
- return require_introspect_introspection.listSchemas(this.db);
41
- }
42
- /**
43
- * The browsable tables. Cached for a minute, since a migration is rare and
44
- * introspection is several catalog queries per table.
45
- */
46
- async getSchema(forceRefresh = false) {
47
- const now = Date.now();
48
- if (!forceRefresh && this.schemaCache && now < this.schemaCache.expires) return this.schemaCache.info;
49
- const info = await require_introspect_introspection.introspectSchema(this.db, {
50
- schemas: this.schemas,
51
- excludeTables: this.excludeTables
52
- });
53
- this.schemaCache = {
54
- info,
55
- expires: now + SCHEMA_TTL_MS
56
- };
57
- return info;
58
- }
59
- /** One table, or null when it is not browsable. */
60
- async getTableInfo(tableName, schema = this.schemas[0] ?? "public") {
61
- const { tables } = await this.getSchema();
62
- return tables.find((t) => t.name === tableName && t.schema === schema) ?? null;
63
- }
64
- /**
65
- * A page of a table's rows, filtered and sorted.
66
- *
67
- * @throws {TableNotFound} the table is not browsable
68
- * @throws {ColumnNotFound} a filter or sort names a column it does not have
69
- * @throws {UnsupportedFilterOperator} a filter's operator does not apply
70
- * @throws {InvalidCursor} the cursor did not come from a previous page
71
- */
72
- async query(options) {
73
- const schema = options.schema ?? this.schemas[0] ?? "public";
74
- const table = await this.getTableInfo(options.table, schema);
75
- if (!table) throw new require_introspect_errors.TableNotFound(options.table, schema);
76
- const cursor = this.getCursorConfig(table);
77
- const pageSize = Math.min(options.pageSize ?? this.defaultPageSize, MAX_PAGE_SIZE);
78
- let query = this.db.withSchema(schema).selectFrom(table.name).selectAll();
79
- if (options.filters?.length) query = require_introspect_filtering.applyFilters(query, options.filters, table);
80
- if (options.sort?.length) query = require_introspect_filtering.applySorting(query, options.sort, table);
81
- else if (cursor) query = query.orderBy(cursor.field, cursor.direction);
82
- if (options.cursor && cursor) {
83
- const after = require_pagination.decodeCursor(options.cursor);
84
- const operator = cursor.direction === "asc" ? ">" : "<";
85
- query = query.where(cursor.field, operator, after);
86
- }
87
- const rows = await query.limit(pageSize + 1).execute();
88
- const hasMore = rows.length > pageSize;
89
- const page = hasMore ? rows.slice(0, pageSize) : rows;
90
- const last = page[page.length - 1];
91
- const first = page[0];
92
- return {
93
- rows: page,
94
- hasMore,
95
- nextCursor: hasMore && cursor && last ? require_pagination.encodeCursor(last[cursor.field]) : null,
96
- prevCursor: options.cursor && cursor && first ? require_pagination.encodeCursor(first[cursor.field]) : null
97
- };
98
- }
99
- /**
100
- * The cursor a table pages by: its own, the default, or its primary key
101
- * when that is one column. Undefined means the table has nothing unique to
102
- * page by, so it is read a page at a time with no next cursor.
103
- */
104
- getCursorConfig(table) {
105
- const configured = this.tableCursors[table.name] ?? this.cursor;
106
- if (configured) return configured;
107
- const [key, ...rest] = table.primaryKey;
108
- if (key && rest.length === 0) return {
109
- field: key,
110
- direction: "asc"
111
- };
112
- }
113
- };
114
- //#endregion
115
- exports.DataBrowser = DataBrowser;
116
-
117
- //# sourceMappingURL=DataBrowser.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"DataBrowser.cjs","names":["listSchemas","introspectSchema","TableNotFound","applyFilters","applySorting","decodeCursor","encodeCursor"],"sources":["../../src/introspect/DataBrowser.ts"],"sourcesContent":["import type { Kysely, SelectQueryBuilder } from 'kysely';\nimport { Direction, decodeCursor, encodeCursor } from '../pagination';\nimport { TableNotFound } from './errors';\nimport { applyFilters, applySorting } from './filtering';\nimport { introspectSchema, listSchemas } from './introspection';\nimport type {\n\tCursorConfig,\n\tDataBrowserOptions,\n\tQueryOptions,\n\tQueryResult,\n\tSchemaInfo,\n\tTableInfo,\n} from './types';\n\n/** How long an introspected schema is reused before it is read again. */\nconst SCHEMA_TTL_MS = 60 * 1000;\nconst MAX_PAGE_SIZE = 100;\n\n/**\n * Reads a Postgres database's structure and pages through its rows, read-only.\n *\n * @example\n * ```typescript\n * const browser = new DataBrowser({ db });\n *\n * const { tables } = await browser.getSchema();\n * const page = await browser.query({ table: 'users', pageSize: 20 });\n * const next = await browser.query({ table: 'users', cursor: page.nextCursor });\n * ```\n */\nexport class DataBrowser<DB = unknown> {\n\treadonly db: Kysely<DB>;\n\treadonly schemas: string[];\n\tprivate readonly cursor?: CursorConfig;\n\tprivate readonly tableCursors: Record<string, CursorConfig>;\n\tprivate readonly excludeTables: string[];\n\tprivate readonly defaultPageSize: number;\n\tprivate schemaCache?: { info: SchemaInfo; expires: number };\n\n\tconstructor(options: DataBrowserOptions<DB>) {\n\t\tthis.db = options.db;\n\t\tthis.schemas = options.schemas ?? ['public'];\n\t\tthis.cursor = options.cursor;\n\t\tthis.tableCursors = options.tableCursors ?? {};\n\t\tthis.excludeTables = options.excludeTables ?? [];\n\t\tthis.defaultPageSize = options.defaultPageSize ?? 50;\n\t}\n\n\t/** Every schema the database has, browsable or not. */\n\tlistSchemas(): Promise<string[]> {\n\t\treturn listSchemas(this.db);\n\t}\n\n\t/**\n\t * The browsable tables. Cached for a minute, since a migration is rare and\n\t * introspection is several catalog queries per table.\n\t */\n\tasync getSchema(forceRefresh = false): Promise<SchemaInfo> {\n\t\tconst now = Date.now();\n\t\tif (!forceRefresh && this.schemaCache && now < this.schemaCache.expires) {\n\t\t\treturn this.schemaCache.info;\n\t\t}\n\n\t\tconst info = await introspectSchema(this.db, {\n\t\t\tschemas: this.schemas,\n\t\t\texcludeTables: this.excludeTables,\n\t\t});\n\t\tthis.schemaCache = { info, expires: now + SCHEMA_TTL_MS };\n\t\treturn info;\n\t}\n\n\t/** One table, or null when it is not browsable. */\n\tasync getTableInfo(\n\t\ttableName: string,\n\t\tschema = this.schemas[0] ?? 'public',\n\t): Promise<TableInfo | null> {\n\t\tconst { tables } = await this.getSchema();\n\t\treturn (\n\t\t\ttables.find((t) => t.name === tableName && t.schema === schema) ?? null\n\t\t);\n\t}\n\n\t/**\n\t * A page of a table's rows, filtered and sorted.\n\t *\n\t * @throws {TableNotFound} the table is not browsable\n\t * @throws {ColumnNotFound} a filter or sort names a column it does not have\n\t * @throws {UnsupportedFilterOperator} a filter's operator does not apply\n\t * @throws {InvalidCursor} the cursor did not come from a previous page\n\t */\n\tasync query(options: QueryOptions): Promise<QueryResult> {\n\t\tconst schema = options.schema ?? this.schemas[0] ?? 'public';\n\t\tconst table = await this.getTableInfo(options.table, schema);\n\t\tif (!table) {\n\t\t\tthrow new TableNotFound(options.table, schema);\n\t\t}\n\n\t\tconst cursor = this.getCursorConfig(table);\n\t\tconst pageSize = Math.min(\n\t\t\toptions.pageSize ?? this.defaultPageSize,\n\t\t\tMAX_PAGE_SIZE,\n\t\t);\n\n\t\t// The table was found by introspection; the query's static types only\n\t\t// know the tables the application declared.\n\t\tlet query = (this.db as Kysely<any>)\n\t\t\t.withSchema(schema)\n\t\t\t.selectFrom(table.name)\n\t\t\t.selectAll() as SelectQueryBuilder<any, any, Record<string, unknown>>;\n\n\t\tif (options.filters?.length) {\n\t\t\tquery = applyFilters(query, options.filters, table);\n\t\t}\n\n\t\tif (options.sort?.length) {\n\t\t\tquery = applySorting(query, options.sort, table);\n\t\t} else if (cursor) {\n\t\t\tquery = query.orderBy(cursor.field, cursor.direction);\n\t\t}\n\n\t\tif (options.cursor && cursor) {\n\t\t\tconst after = decodeCursor(options.cursor);\n\t\t\tconst operator = cursor.direction === Direction.Asc ? '>' : '<';\n\t\t\tquery = query.where(cursor.field, operator, after);\n\t\t}\n\n\t\t// One more row than the page, so whether there is a next page is known\n\t\t// without counting.\n\t\tconst rows = await query.limit(pageSize + 1).execute();\n\t\tconst hasMore = rows.length > pageSize;\n\t\tconst page = hasMore ? rows.slice(0, pageSize) : rows;\n\n\t\tconst last = page[page.length - 1];\n\t\tconst first = page[0];\n\n\t\treturn {\n\t\t\trows: page,\n\t\t\thasMore,\n\t\t\tnextCursor:\n\t\t\t\thasMore && cursor && last ? encodeCursor(last[cursor.field]) : null,\n\t\t\t// Only known to exist when this page was reached through a cursor.\n\t\t\tprevCursor:\n\t\t\t\toptions.cursor && cursor && first\n\t\t\t\t\t? encodeCursor(first[cursor.field])\n\t\t\t\t\t: null,\n\t\t};\n\t}\n\n\t/**\n\t * The cursor a table pages by: its own, the default, or its primary key\n\t * when that is one column. Undefined means the table has nothing unique to\n\t * page by, so it is read a page at a time with no next cursor.\n\t */\n\tgetCursorConfig(table: TableInfo): CursorConfig | undefined {\n\t\tconst configured = this.tableCursors[table.name] ?? this.cursor;\n\t\tif (configured) return configured;\n\n\t\tconst [key, ...rest] =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D"}
@@ -1,51 +0,0 @@
1
- import { CursorConfig, DataBrowserOptions, QueryOptions, QueryResult, SchemaInfo, TableInfo } from "./types.cjs";
2
- import { Kysely } from "kysely";
3
- //#region src/introspect/DataBrowser.d.ts
4
- /**
5
- * Reads a Postgres database's structure and pages through its rows, read-only.
6
- *
7
- * @example
8
- * ```typescript
9
- * const browser = new DataBrowser({ db });
10
- *
11
- * const { tables } = await browser.getSchema();
12
- * const page = await browser.query({ table: 'users', pageSize: 20 });
13
- * const next = await browser.query({ table: 'users', cursor: page.nextCursor });
14
- * ```
15
- */
16
- export declare class DataBrowser<DB = unknown> {
17
- readonly db: Kysely<DB>;
18
- readonly schemas: string[];
19
- private readonly cursor?;
20
- private readonly tableCursors;
21
- private readonly excludeTables;
22
- private readonly defaultPageSize;
23
- private schemaCache?;
24
- constructor(options: DataBrowserOptions<DB>);
25
- /** Every schema the database has, browsable or not. */
26
- listSchemas(): Promise<string[]>;
27
- /**
28
- * The browsable tables. Cached for a minute, since a migration is rare and
29
- * introspection is several catalog queries per table.
30
- */
31
- getSchema(forceRefresh?: boolean): Promise<SchemaInfo>;
32
- /** One table, or null when it is not browsable. */
33
- getTableInfo(tableName: string, schema?: string): Promise<TableInfo | null>;
34
- /**
35
- * A page of a table's rows, filtered and sorted.
36
- *
37
- * @throws {TableNotFound} the table is not browsable
38
- * @throws {ColumnNotFound} a filter or sort names a column it does not have
39
- * @throws {UnsupportedFilterOperator} a filter's operator does not apply
40
- * @throws {InvalidCursor} the cursor did not come from a previous page
41
- */
42
- query(options: QueryOptions): Promise<QueryResult>;
43
- /**
44
- * The cursor a table pages by: its own, the default, or its primary key
45
- * when that is one column. Undefined means the table has nothing unique to
46
- * page by, so it is read a page at a time with no next cursor.
47
- */
48
- getCursorConfig(table: TableInfo): CursorConfig | undefined;
49
- }
50
- //#endregion
51
- //# sourceMappingURL=DataBrowser.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"DataBrowser.d.cts","names":[],"sources":["../../src/introspect/DataBrowser.ts"],"mappings":";;;;;;;;;;;;;;;qBA8Ba,YAAY;WACf,IAAI,OAAO;WACX;mBACQ;mBACA;mBACA;mBACA;UACT;EAER,YAAY,SAAS,mBAAmB;;EAUxC,eAAe;;;;;EAQT,UAAU,yBAAuB,QAAQ;;EAezC,aACL,mBACA,kBACE,QAAQ;;;;;;;;;EAeL,MAAM,SAAS,eAAe,QAAQ;;;;;;EA+D5C,gBAAgB,OAAO,YAAY"}
@@ -1,51 +0,0 @@
1
- import { CursorConfig, DataBrowserOptions, QueryOptions, QueryResult, SchemaInfo, TableInfo } from "./types.mjs";
2
- import { Kysely } from "kysely";
3
- //#region src/introspect/DataBrowser.d.ts
4
- /**
5
- * Reads a Postgres database's structure and pages through its rows, read-only.
6
- *
7
- * @example
8
- * ```typescript
9
- * const browser = new DataBrowser({ db });
10
- *
11
- * const { tables } = await browser.getSchema();
12
- * const page = await browser.query({ table: 'users', pageSize: 20 });
13
- * const next = await browser.query({ table: 'users', cursor: page.nextCursor });
14
- * ```
15
- */
16
- export declare class DataBrowser<DB = unknown> {
17
- readonly db: Kysely<DB>;
18
- readonly schemas: string[];
19
- private readonly cursor?;
20
- private readonly tableCursors;
21
- private readonly excludeTables;
22
- private readonly defaultPageSize;
23
- private schemaCache?;
24
- constructor(options: DataBrowserOptions<DB>);
25
- /** Every schema the database has, browsable or not. */
26
- listSchemas(): Promise<string[]>;
27
- /**
28
- * The browsable tables. Cached for a minute, since a migration is rare and
29
- * introspection is several catalog queries per table.
30
- */
31
- getSchema(forceRefresh?: boolean): Promise<SchemaInfo>;
32
- /** One table, or null when it is not browsable. */
33
- getTableInfo(tableName: string, schema?: string): Promise<TableInfo | null>;
34
- /**
35
- * A page of a table's rows, filtered and sorted.
36
- *
37
- * @throws {TableNotFound} the table is not browsable
38
- * @throws {ColumnNotFound} a filter or sort names a column it does not have
39
- * @throws {UnsupportedFilterOperator} a filter's operator does not apply
40
- * @throws {InvalidCursor} the cursor did not come from a previous page
41
- */
42
- query(options: QueryOptions): Promise<QueryResult>;
43
- /**
44
- * The cursor a table pages by: its own, the default, or its primary key
45
- * when that is one column. Undefined means the table has nothing unique to
46
- * page by, so it is read a page at a time with no next cursor.
47
- */
48
- getCursorConfig(table: TableInfo): CursorConfig | undefined;
49
- }
50
- //#endregion
51
- //# sourceMappingURL=DataBrowser.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"DataBrowser.d.mts","names":[],"sources":["../../src/introspect/DataBrowser.ts"],"mappings":";;;;;;;;;;;;;;;qBA8Ba,YAAY;WACf,IAAI,OAAO;WACX;mBACQ;mBACA;mBACA;mBACA;UACT;EAER,YAAY,SAAS,mBAAmB;;EAUxC,eAAe;;;;;EAQT,UAAU,yBAAuB,QAAQ;;EAezC,aACL,mBACA,kBACE,QAAQ;;;;;;;;;EAeL,MAAM,SAAS,eAAe,QAAQ;;;;;;EA+D5C,gBAAgB,OAAO,YAAY"}
@@ -1,116 +0,0 @@
1
- import { decodeCursor, encodeCursor } from "../pagination.mjs";
2
- import { TableNotFound } from "./errors.mjs";
3
- import { applyFilters, applySorting } from "./filtering.mjs";
4
- import { introspectSchema, listSchemas } from "./introspection.mjs";
5
- //#region src/introspect/DataBrowser.ts
6
- /** How long an introspected schema is reused before it is read again. */
7
- const SCHEMA_TTL_MS = 6e4;
8
- const MAX_PAGE_SIZE = 100;
9
- /**
10
- * Reads a Postgres database's structure and pages through its rows, read-only.
11
- *
12
- * @example
13
- * ```typescript
14
- * const browser = new DataBrowser({ db });
15
- *
16
- * const { tables } = await browser.getSchema();
17
- * const page = await browser.query({ table: 'users', pageSize: 20 });
18
- * const next = await browser.query({ table: 'users', cursor: page.nextCursor });
19
- * ```
20
- */
21
- var DataBrowser = class {
22
- db;
23
- schemas;
24
- cursor;
25
- tableCursors;
26
- excludeTables;
27
- defaultPageSize;
28
- schemaCache;
29
- constructor(options) {
30
- this.db = options.db;
31
- this.schemas = options.schemas ?? ["public"];
32
- this.cursor = options.cursor;
33
- this.tableCursors = options.tableCursors ?? {};
34
- this.excludeTables = options.excludeTables ?? [];
35
- this.defaultPageSize = options.defaultPageSize ?? 50;
36
- }
37
- /** Every schema the database has, browsable or not. */
38
- listSchemas() {
39
- return listSchemas(this.db);
40
- }
41
- /**
42
- * The browsable tables. Cached for a minute, since a migration is rare and
43
- * introspection is several catalog queries per table.
44
- */
45
- async getSchema(forceRefresh = false) {
46
- const now = Date.now();
47
- if (!forceRefresh && this.schemaCache && now < this.schemaCache.expires) return this.schemaCache.info;
48
- const info = await introspectSchema(this.db, {
49
- schemas: this.schemas,
50
- excludeTables: this.excludeTables
51
- });
52
- this.schemaCache = {
53
- info,
54
- expires: now + SCHEMA_TTL_MS
55
- };
56
- return info;
57
- }
58
- /** One table, or null when it is not browsable. */
59
- async getTableInfo(tableName, schema = this.schemas[0] ?? "public") {
60
- const { tables } = await this.getSchema();
61
- return tables.find((t) => t.name === tableName && t.schema === schema) ?? null;
62
- }
63
- /**
64
- * A page of a table's rows, filtered and sorted.
65
- *
66
- * @throws {TableNotFound} the table is not browsable
67
- * @throws {ColumnNotFound} a filter or sort names a column it does not have
68
- * @throws {UnsupportedFilterOperator} a filter's operator does not apply
69
- * @throws {InvalidCursor} the cursor did not come from a previous page
70
- */
71
- async query(options) {
72
- const schema = options.schema ?? this.schemas[0] ?? "public";
73
- const table = await this.getTableInfo(options.table, schema);
74
- if (!table) throw new TableNotFound(options.table, schema);
75
- const cursor = this.getCursorConfig(table);
76
- const pageSize = Math.min(options.pageSize ?? this.defaultPageSize, MAX_PAGE_SIZE);
77
- let query = this.db.withSchema(schema).selectFrom(table.name).selectAll();
78
- if (options.filters?.length) query = applyFilters(query, options.filters, table);
79
- if (options.sort?.length) query = applySorting(query, options.sort, table);
80
- else if (cursor) query = query.orderBy(cursor.field, cursor.direction);
81
- if (options.cursor && cursor) {
82
- const after = decodeCursor(options.cursor);
83
- const operator = cursor.direction === "asc" ? ">" : "<";
84
- query = query.where(cursor.field, operator, after);
85
- }
86
- const rows = await query.limit(pageSize + 1).execute();
87
- const hasMore = rows.length > pageSize;
88
- const page = hasMore ? rows.slice(0, pageSize) : rows;
89
- const last = page[page.length - 1];
90
- const first = page[0];
91
- return {
92
- rows: page,
93
- hasMore,
94
- nextCursor: hasMore && cursor && last ? encodeCursor(last[cursor.field]) : null,
95
- prevCursor: options.cursor && cursor && first ? encodeCursor(first[cursor.field]) : null
96
- };
97
- }
98
- /**
99
- * The cursor a table pages by: its own, the default, or its primary key
100
- * when that is one column. Undefined means the table has nothing unique to
101
- * page by, so it is read a page at a time with no next cursor.
102
- */
103
- getCursorConfig(table) {
104
- const configured = this.tableCursors[table.name] ?? this.cursor;
105
- if (configured) return configured;
106
- const [key, ...rest] = table.primaryKey;
107
- if (key && rest.length === 0) return {
108
- field: key,
109
- direction: "asc"
110
- };
111
- }
112
- };
113
- //#endregion
114
- export { DataBrowser };
115
-
116
- //# sourceMappingURL=DataBrowser.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"DataBrowser.mjs","names":[],"sources":["../../src/introspect/DataBrowser.ts"],"sourcesContent":["import type { Kysely, SelectQueryBuilder } from 'kysely';\nimport { Direction, decodeCursor, encodeCursor } from '../pagination';\nimport { TableNotFound } from './errors';\nimport { applyFilters, applySorting } from './filtering';\nimport { introspectSchema, listSchemas } from './introspection';\nimport type {\n\tCursorConfig,\n\tDataBrowserOptions,\n\tQueryOptions,\n\tQueryResult,\n\tSchemaInfo,\n\tTableInfo,\n} from './types';\n\n/** How long an introspected schema is reused before it is read again. */\nconst SCHEMA_TTL_MS = 60 * 1000;\nconst MAX_PAGE_SIZE = 100;\n\n/**\n * Reads a Postgres database's structure and pages through its rows, read-only.\n *\n * @example\n * ```typescript\n * const browser = new DataBrowser({ db });\n *\n * const { tables } = await browser.getSchema();\n * const page = await browser.query({ table: 'users', pageSize: 20 });\n * const next = await browser.query({ table: 'users', cursor: page.nextCursor });\n * ```\n */\nexport class DataBrowser<DB = unknown> {\n\treadonly db: Kysely<DB>;\n\treadonly schemas: string[];\n\tprivate readonly cursor?: CursorConfig;\n\tprivate readonly tableCursors: Record<string, CursorConfig>;\n\tprivate readonly excludeTables: string[];\n\tprivate readonly defaultPageSize: number;\n\tprivate schemaCache?: { info: SchemaInfo; expires: number };\n\n\tconstructor(options: DataBrowserOptions<DB>) {\n\t\tthis.db = options.db;\n\t\tthis.schemas = options.schemas ?? ['public'];\n\t\tthis.cursor = options.cursor;\n\t\tthis.tableCursors = options.tableCursors ?? {};\n\t\tthis.excludeTables = options.excludeTables ?? [];\n\t\tthis.defaultPageSize = options.defaultPageSize ?? 50;\n\t}\n\n\t/** Every schema the database has, browsable or not. */\n\tlistSchemas(): Promise<string[]> {\n\t\treturn listSchemas(this.db);\n\t}\n\n\t/**\n\t * The browsable tables. Cached for a minute, since a migration is rare and\n\t * introspection is several catalog queries per table.\n\t */\n\tasync getSchema(forceRefresh = false): Promise<SchemaInfo> {\n\t\tconst now = Date.now();\n\t\tif (!forceRefresh && this.schemaCache && now < this.schemaCache.expires) {\n\t\t\treturn this.schemaCache.info;\n\t\t}\n\n\t\tconst info = await introspectSchema(this.db, {\n\t\t\tschemas: this.schemas,\n\t\t\texcludeTables: this.excludeTables,\n\t\t});\n\t\tthis.schemaCache = { info, expires: now + SCHEMA_TTL_MS };\n\t\treturn info;\n\t}\n\n\t/** One table, or null when it is not browsable. */\n\tasync getTableInfo(\n\t\ttableName: string,\n\t\tschema = this.schemas[0] ?? 'public',\n\t): Promise<TableInfo | null> {\n\t\tconst { tables } = await this.getSchema();\n\t\treturn (\n\t\t\ttables.find((t) => t.name === tableName && t.schema === schema) ?? null\n\t\t);\n\t}\n\n\t/**\n\t * A page of a table's rows, filtered and sorted.\n\t *\n\t * @throws {TableNotFound} the table is not browsable\n\t * @throws {ColumnNotFound} a filter or sort names a column it does not have\n\t * @throws {UnsupportedFilterOperator} a filter's operator does not apply\n\t * @throws {InvalidCursor} the cursor did not come from a previous page\n\t */\n\tasync query(options: QueryOptions): Promise<QueryResult> {\n\t\tconst schema = options.schema ?? this.schemas[0] ?? 'public';\n\t\tconst table = await this.getTableInfo(options.table, schema);\n\t\tif (!table) {\n\t\t\tthrow new TableNotFound(options.table, schema);\n\t\t}\n\n\t\tconst cursor = this.getCursorConfig(table);\n\t\tconst pageSize = Math.min(\n\t\t\toptions.pageSize ?? this.defaultPageSize,\n\t\t\tMAX_PAGE_SIZE,\n\t\t);\n\n\t\t// The table was found by introspection; the query's static types only\n\t\t// know the tables the application declared.\n\t\tlet query = (this.db as Kysely<any>)\n\t\t\t.withSchema(schema)\n\t\t\t.selectFrom(table.name)\n\t\t\t.selectAll() as SelectQueryBuilder<any, any, Record<string, unknown>>;\n\n\t\tif (options.filters?.length) {\n\t\t\tquery = applyFilters(query, options.filters, table);\n\t\t}\n\n\t\tif (options.sort?.length) {\n\t\t\tquery = applySorting(query, options.sort, table);\n\t\t} else if (cursor) {\n\t\t\tquery = query.orderBy(cursor.field, cursor.direction);\n\t\t}\n\n\t\tif (options.cursor && cursor) {\n\t\t\tconst after = decodeCursor(options.cursor);\n\t\t\tconst operator = cursor.direction === Direction.Asc ? '>' : '<';\n\t\t\tquery = query.where(cursor.field, operator, after);\n\t\t}\n\n\t\t// One more row than the page, so whether there is a next page is known\n\t\t// without counting.\n\t\tconst rows = await query.limit(pageSize + 1).execute();\n\t\tconst hasMore = rows.length > pageSize;\n\t\tconst page = hasMore ? rows.slice(0, pageSize) : rows;\n\n\t\tconst last = page[page.length - 1];\n\t\tconst first = page[0];\n\n\t\treturn {\n\t\t\trows: page,\n\t\t\thasMore,\n\t\t\tnextCursor:\n\t\t\t\thasMore && cursor && last ? encodeCursor(last[cursor.field]) : null,\n\t\t\t// Only known to exist when this page was reached through a cursor.\n\t\t\tprevCursor:\n\t\t\t\toptions.cursor && cursor && first\n\t\t\t\t\t? encodeCursor(first[cursor.field])\n\t\t\t\t\t: null,\n\t\t};\n\t}\n\n\t/**\n\t * The cursor a table pages by: its own, the default, or its primary key\n\t * when that is one column. Undefined means the table has nothing unique to\n\t * page by, so it is read a page at a time with no next cursor.\n\t */\n\tgetCursorConfig(table: TableInfo): CursorConfig | undefined {\n\t\tconst configured = this.tableCursors[table.name] ?? this.cursor;\n\t\tif (configured) return configured;\n\n\t\tconst [key, ...rest] = table.primaryKey;\n\t\tif (key && rest.length === 0) {\n\t\t\treturn { field: key, direction: Direction.Asc };\n\t\t}\n\n\t\treturn undefined;\n\t}\n}\n"],"mappings":";;;;;;AAeA,MAAM,gBAAgB;AACtB,MAAM,gBAAgB;;;;;;;;;;;;;AActB,IAAa,cAAb,MAAuC;CACtC;CACA;CACA;CACA;CACA;CACA;CACA;CAEA,YAAY,SAAiC;EAC5C,KAAK,KAAK,QAAQ;EAClB,KAAK,UAAU,QAAQ,WAAW,CAAC,QAAQ;EAC3C,KAAK,SAAS,QAAQ;EACtB,KAAK,eAAe,QAAQ,gBAAgB,CAAC;EAC7C,KAAK,gBAAgB,QAAQ,iBAAiB,CAAC;EAC/C,KAAK,kBAAkB,QAAQ,mBAAmB;CACnD;;CAGA,cAAiC;EAChC,OAAO,YAAY,KAAK,EAAE;CAC3B;;;;;CAMA,MAAM,UAAU,eAAe,OAA4B;EAC1D,MAAM,MAAM,KAAK,IAAI;EACrB,IAAI,CAAC,gBAAgB,KAAK,eAAe,MAAM,KAAK,YAAY,SAC/D,OAAO,KAAK,YAAY;EAGzB,MAAM,OAAO,MAAM,iBAAiB,KAAK,IAAI;GAC5C,SAAS,KAAK;GACd,eAAe,KAAK;EACrB,CAAC;EACD,KAAK,cAAc;GAAE;GAAM,SAAS,MAAM;EAAc;EACxD,OAAO;CACR;;CAGA,MAAM,aACL,WACA,SAAS,KAAK,QAAQ,MAAM,UACA;EAC5B,MAAM,EAAE,WAAW,MAAM,KAAK,UAAU;EACxC,OACC,OAAO,MAAM,MAAM,EAAE,SAAS,aAAa,EAAE,WAAW,MAAM,KAAK;CAErE;;;;;;;;;CAUA,MAAM,MAAM,SAA6C;EACxD,MAAM,SAAS,QAAQ,UAAU,KA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C7B,IAAI,OAAO,KAAK,WAAW,GAC1B,OAAO;GAAE,OAAO;GAAK,WAAA;EAAyB;CAIhD;AACD"}
@@ -1,48 +0,0 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- //#region src/introspect/errors.ts
3
- /**
4
- * What a row query can get wrong. Each is the caller's mistake rather than the
5
- * database's, so the JSON handler answers them as 4xx with the class name as
6
- * the error code — a client matches on that, not on the message.
7
- */
8
- /** A table that is not in the browsable schemas, or was excluded. */
9
- var TableNotFound = class extends Error {
10
- table;
11
- schema;
12
- constructor(table, schema) {
13
- super(`There is no table '${table}' in schema '${schema}'. List the tables to see what can be browsed; an excluded table is left out on purpose.`);
14
- this.table = table;
15
- this.schema = schema;
16
- this.name = "TableNotFound";
17
- }
18
- };
19
- /** A filter or sort naming a column the table does not have. */
20
- var ColumnNotFound = class extends Error {
21
- column;
22
- table;
23
- constructor(column, table) {
24
- super(`Table '${table}' has no column '${column}'. Use a column name exactly as the table description lists it.`);
25
- this.column = column;
26
- this.table = table;
27
- this.name = "ColumnNotFound";
28
- }
29
- };
30
- /** A filter operator that does not apply to the column's type. */
31
- var UnsupportedFilterOperator = class extends Error {
32
- operator;
33
- column;
34
- columnType;
35
- constructor(operator, column, columnType) {
36
- super(`'${operator}' cannot filter '${column}', a ${columnType} column. Use an operator that applies to ${columnType} values.`);
37
- this.operator = operator;
38
- this.column = column;
39
- this.columnType = columnType;
40
- this.name = "UnsupportedFilterOperator";
41
- }
42
- };
43
- //#endregion
44
- exports.ColumnNotFound = ColumnNotFound;
45
- exports.TableNotFound = TableNotFound;
46
- exports.UnsupportedFilterOperator = UnsupportedFilterOperator;
47
-
48
- //# sourceMappingURL=errors.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.cjs","names":[],"sources":["../../src/introspect/errors.ts"],"sourcesContent":["/**\n * What a row query can get wrong. Each is the caller's mistake rather than the\n * database's, so the JSON handler answers them as 4xx with the class name as\n * the error code — a client matches on that, not on the message.\n */\n\n/** A table that is not in the browsable schemas, or was excluded. */\nexport class TableNotFound extends Error {\n\tconstructor(\n\t\treadonly table: string,\n\t\treadonly schema: string,\n\t) {\n\t\tsuper(\n\t\t\t`There is no table '${table}' in schema '${schema}'. List the tables to see what can be browsed; an excluded table is left out on purpose.`,\n\t\t);\n\t\tthis.name = 'TableNotFound';\n\t}\n}\n\n/** A filter or sort naming a column the table does not have. */\nexport class ColumnNotFound extends Error {\n\tconstructor(\n\t\treadonly column: string,\n\t\treadonly table: string,\n\t) {\n\t\tsuper(\n\t\t\t`Table '${table}' has no column '${column}'. Use a column name exactly as the table description lists it.`,\n\t\t);\n\t\tthis.name = 'ColumnNotFound';\n\t}\n}\n\n/** A filter operator that does not apply to the column's type. */\nexport class UnsupportedFilterOperator extends Error {\n\tconstructor(\n\t\treadonly operator: string,\n\t\treadonly column: string,\n\t\treadonly columnType: string,\n\t) {\n\t\tsuper(\n\t\t\t`'${operator}' cannot filter '${column}', a ${columnType} column. Use an operator that applies to ${columnType} values.`,\n\t\t);\n\t\tthis.name = 'UnsupportedFilterOperator';\n\t}\n}\n"],"mappings":";;;;;;;;AAOA,IAAa,gBAAb,cAAmC,MAAM;CAE9B;CACA;CAFV,YACC,OACA,QACC;EACD,MACC,sBAAsB,MAAM,eAAe,OAAO,yFACnD;EALS,KAAA,QAAA;EACA,KAAA,SAAA;EAKT,KAAK,OAAO;CACb;AACD;;AAGA,IAAa,iBAAb,cAAoC,MAAM;CAE/B;CACA;CAFV,YACC,QACA,OACC;EACD,MACC,UAAU,MAAM,mBAAmB,OAAO,gEAC3C;EALS,KAAA,SAAA;EACA,KAAA,QAAA;EAKT,KAAK,OAAO;CACb;AACD;;AAGA,IAAa,4BAAb,cAA+C,MAAM;CAE1C;CACA;CACA;CAHV,YACC,UACA,QACA,YACC;EACD,MACC,IAAI,SAAS,mBAAmB,OAAO,OAAO,WAAW,2CAA2C,WAAW,SAChH;EANS,KAAA,WAAA;EACA,KAAA,SAAA;EACA,KAAA,aAAA;EAKT,KAAK,OAAO;CACb;AACD"}
@@ -1,27 +0,0 @@
1
- //#region src/introspect/errors.d.ts
2
- /**
3
- * What a row query can get wrong. Each is the caller's mistake rather than the
4
- * database's, so the JSON handler answers them as 4xx with the class name as
5
- * the error code — a client matches on that, not on the message.
6
- */
7
- /** A table that is not in the browsable schemas, or was excluded. */
8
- export declare class TableNotFound extends Error {
9
- readonly table: string;
10
- readonly schema: string;
11
- constructor(table: string, schema: string);
12
- }
13
- /** A filter or sort naming a column the table does not have. */
14
- export declare class ColumnNotFound extends Error {
15
- readonly column: string;
16
- readonly table: string;
17
- constructor(column: string, table: string);
18
- }
19
- /** A filter operator that does not apply to the column's type. */
20
- export declare class UnsupportedFilterOperator extends Error {
21
- readonly operator: string;
22
- readonly column: string;
23
- readonly columnType: string;
24
- constructor(operator: string, column: string, columnType: string);
25
- }
26
- //#endregion
27
- //# sourceMappingURL=errors.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.d.cts","names":[],"sources":["../../src/introspect/errors.ts"],"mappings":";;;;;;;qBAOa,sBAAsB;WAExB;WACA;EAFV,YACU,eACA;;;qBAUE,uBAAuB;WAEzB;WACA;EAFV,YACU,gBACA;;;qBAUE,kCAAkC;WAEpC;WACA;WACA;EAHV,YACU,kBACA,gBACA"}
@@ -1,27 +0,0 @@
1
- //#region src/introspect/errors.d.ts
2
- /**
3
- * What a row query can get wrong. Each is the caller's mistake rather than the
4
- * database's, so the JSON handler answers them as 4xx with the class name as
5
- * the error code — a client matches on that, not on the message.
6
- */
7
- /** A table that is not in the browsable schemas, or was excluded. */
8
- export declare class TableNotFound extends Error {
9
- readonly table: string;
10
- readonly schema: string;
11
- constructor(table: string, schema: string);
12
- }
13
- /** A filter or sort naming a column the table does not have. */
14
- export declare class ColumnNotFound extends Error {
15
- readonly column: string;
16
- readonly table: string;
17
- constructor(column: string, table: string);
18
- }
19
- /** A filter operator that does not apply to the column's type. */
20
- export declare class UnsupportedFilterOperator extends Error {
21
- readonly operator: string;
22
- readonly column: string;
23
- readonly columnType: string;
24
- constructor(operator: string, column: string, columnType: string);
25
- }
26
- //#endregion
27
- //# sourceMappingURL=errors.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.d.mts","names":[],"sources":["../../src/introspect/errors.ts"],"mappings":";;;;;;;qBAOa,sBAAsB;WAExB;WACA;EAFV,YACU,eACA;;;qBAUE,uBAAuB;WAEzB;WACA;EAFV,YACU,gBACA;;;qBAUE,kCAAkC;WAEpC;WACA;WACA;EAHV,YACU,kBACA,gBACA"}