true-pg 0.6.0 → 0.7.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.
@@ -1,16 +1,21 @@
1
1
  import Pg from "pg";
2
2
  import { PGlite as Pglite } from "@electric-sql/pglite";
3
+ import { type Canonical } from "./canonicalise.ts";
3
4
  export declare class DbAdapter {
4
5
  private client;
5
6
  private external?;
7
+ rawTypeCache: Map<string, Promise<Canonical>>;
8
+ canonicalCache: Map<string, Promise<Canonical>>;
6
9
  constructor(client: Pg.Client | Pg.Pool | Pglite, external?: boolean | undefined);
10
+ clearCache(): void;
11
+ canonicalise(types: string[]): Promise<Canonical[]>;
7
12
  connect(): Promise<void>;
8
13
  /**
9
14
  * Execute a read query and return just the rows
10
15
  */
11
16
  query<R, I extends any[] = []>(text: string, params?: I): Promise<R[]>;
12
17
  /**
13
- * Close the connection if needed
18
+ * Close the connection and clear the cache
14
19
  */
15
20
  close(): Promise<void>;
16
21
  }
@@ -1,11 +1,23 @@
1
1
  import Pg from "pg";
2
2
  import { PGlite as Pglite } from "@electric-sql/pglite";
3
+ import { canonicalise } from "./canonicalise.js";
3
4
  export class DbAdapter {
4
5
  client;
5
6
  external;
7
+ rawTypeCache;
8
+ canonicalCache;
6
9
  constructor(client, external) {
7
10
  this.client = client;
8
11
  this.external = external;
12
+ this.canonicalCache = new Map();
13
+ this.rawTypeCache = new Map();
14
+ }
15
+ clearCache() {
16
+ this.canonicalCache.clear();
17
+ this.rawTypeCache.clear();
18
+ }
19
+ async canonicalise(types) {
20
+ return await canonicalise(this, types, this.rawTypeCache, this.canonicalCache);
9
21
  }
10
22
  async connect() {
11
23
  if (this.external)
@@ -19,6 +31,7 @@ export class DbAdapter {
19
31
  else if (this.client instanceof Pglite) {
20
32
  // Pglite doesn't have an explicit connect method
21
33
  }
34
+ this.clearCache();
22
35
  }
23
36
  /**
24
37
  * Execute a read query and return just the rows
@@ -47,11 +60,12 @@ export class DbAdapter {
47
60
  }
48
61
  }
49
62
  /**
50
- * Close the connection if needed
63
+ * Close the connection and clear the cache
51
64
  */
52
65
  async close() {
53
66
  if (this.external)
54
67
  return;
68
+ this.clearCache();
55
69
  if (this.client instanceof Pg.Pool) {
56
70
  this.client.end();
57
71
  }
@@ -1,6 +1,49 @@
1
1
  import { DbAdapter } from "./adapter.ts";
2
+ interface ParsedTypeName {
3
+ /** Name after removing modifiers and brackets, e.g. "varchar" in "varchar(50)" */
4
+ base: string;
5
+ /** Modifiers, e.g. "50" in "varchar(50)" */
6
+ modifiers: string | null;
7
+ /** Number of dimensions from explicit brackets, e.g. 1 in "int[]" */
8
+ dimensions: number;
9
+ /** Original type name, e.g. "varchar(50)" */
10
+ original: string;
11
+ }
12
+ /**
13
+ * Parses a PostgreSQL type name string to extract its base name,
14
+ * modifiers, and dimensions from explicit '[]' brackets.
15
+ *
16
+ * Examples:
17
+ *
18
+ * - `parseTypeName("varchar(50)")`
19
+ *
20
+ * `⤷ { baseTypeName: "varchar", modifiers: "50", dimensions: 0, originalTypeName: "varchar(50)" }`
21
+ *
22
+ * - `parseTypeName("int[]")`
23
+ *
24
+ * `⤷ { baseTypeName: "int", modifiers: null, dimensions: 1, originalTypeName: "int[]" }`
25
+ *
26
+ * - `parseTypeName("public.my_table[][]")`
27
+ *
28
+ * `⤷ { baseTypeName: "public.my_table", modifiers: null, dimensions: 2, originalTypeName: "public.my_table[][]" }`
29
+ *
30
+ * - `parseTypeName("numeric(10, 2)[]")`
31
+ *
32
+ * `⤷ { baseTypeName: "numeric", modifiers: "10, 2", dimensions: 1, originalTypeName: "numeric(10, 2)[]" }`
33
+ *
34
+ * - `parseTypeName("geometry(Point, 4326)")`
35
+ *
36
+ * `⤷ { baseTypeName: "geometry", modifiers: "Point, 4326", dimensions: 0, originalTypeName: "geometry(Point, 4326)" }`
37
+ *
38
+ * - `parseTypeName("_text")`
39
+ *
40
+ * `⤷ { baseTypeName: "_text", modifiers: null, dimensions: 0, originalTypeName: "_text" }`
41
+ *
42
+ * Internal arrays aren't handled here
43
+ */
44
+ export declare function parseTypeName(type: string): ParsedTypeName;
2
45
  export declare namespace Canonical {
3
- export enum Kind {
46
+ enum Kind {
4
47
  Base = "base",
5
48
  Composite = "composite",
6
49
  Domain = "domain",
@@ -18,14 +61,14 @@ export declare namespace Canonical {
18
61
  dimensions: number;
19
62
  modifiers?: string | null;
20
63
  }
21
- export interface Base extends Abstract {
64
+ interface Base extends Abstract {
22
65
  kind: Kind.Base;
23
66
  }
24
- export interface Enum extends Abstract {
67
+ interface Enum extends Abstract {
25
68
  kind: Kind.Enum;
26
69
  enum_values: string[];
27
70
  }
28
- export interface CompositeAttribute {
71
+ interface CompositeAttribute {
29
72
  name: string;
30
73
  index: number;
31
74
  type: Canonical;
@@ -43,22 +86,25 @@ export declare namespace Canonical {
43
86
  */
44
87
  generated: "ALWAYS" | "NEVER" | "BY DEFAULT";
45
88
  }
46
- export interface Composite extends Abstract {
89
+ interface Composite extends Abstract {
47
90
  kind: Kind.Composite;
48
91
  attributes: CompositeAttribute[];
49
92
  }
50
- export interface Domain extends Abstract {
93
+ interface Domain extends Abstract {
51
94
  kind: Kind.Domain;
52
95
  domain_base_type: Canonical;
53
96
  }
54
- export interface Range extends Abstract {
97
+ interface Range extends Abstract {
55
98
  kind: Kind.Range;
56
99
  range_subtype: Canonical;
57
100
  }
58
- export interface Pseudo extends Abstract {
101
+ interface Pseudo extends Abstract {
59
102
  kind: Kind.Pseudo;
60
103
  }
61
- export {};
62
104
  }
63
105
  export type Canonical = Canonical.Base | Canonical.Enum | Canonical.Composite | Canonical.Domain | Canonical.Range | Canonical.Pseudo;
64
- export declare const canonicalise: (db: DbAdapter, types: string[]) => Promise<Canonical[]>;
106
+ type ExclusiveCanonProps = Omit<Canonical, keyof Canonical.Abstract>;
107
+ export declare const canonicalise: (db: DbAdapter, types: string[], rawTypeCache: Map<string, Promise<Canonical>>, canonicalCache: Map<string, Promise<ExclusiveCanonProps>>) => Promise<Canonical[]>;
108
+ export declare const oidsToQualifiedNames: (db: DbAdapter, oids: number[]) => Promise<string[]>;
109
+ export declare const canonicaliseFromOids: (db: DbAdapter, oids: number[]) => Promise<Canonical[]>;
110
+ export {};