@easy-cms/db-postgres 1.15.1 → 1.16.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.
package/dist/index.d.ts CHANGED
@@ -8,12 +8,27 @@ interface PostgresAdapterOptions extends DrizzleAdapterOptions {
8
8
  readonly url?: string;
9
9
  /** Maximum connections in the pool. Default 10. */
10
10
  readonly max?: number;
11
+ /**
12
+ * Connections kept for creates, updates and deletes (with every read they make), on top of
13
+ * `max`. Default 0: writes share the pool. With slow reads filling the pool, saves wait for
14
+ * them; a few connections of their own keep saves quick. Not with PGlite, which has one.
15
+ */
16
+ readonly writeConnections?: number;
11
17
  /**
12
18
  * Use prepared statements. Default true. Turn off behind a pooler in transaction mode that
13
19
  * can't keep them (PgBouncer before 1.24, or with `max_prepared_statements = 0`, and some
14
20
  * hosted poolers): it would run the next query on a server connection without the statement.
15
21
  */
16
22
  readonly prepare?: boolean;
23
+ /**
24
+ * Plans each query for its own values (`plan_cache_mode = force_custom_plan`, sent when a
25
+ * connection opens). Default true, unless `prepare` is false (or `?prepare=false`), which plans that way already.
26
+ * Postgres otherwise switches a prepared statement to one plan for any value after a few
27
+ * runs, and for a filter that plan may read every matching row to sort them (at 1M posts,
28
+ * seconds instead of milliseconds). PgBouncer refuses the parameter: set it on the role there
29
+ * (`ALTER ROLE … SET plan_cache_mode = force_custom_plan`) and this to false.
30
+ */
31
+ readonly customPlans?: boolean;
17
32
  /**
18
33
  * Cancels a statement running longer than this (ms), sent as `statement_timeout` when a
19
34
  * connection opens. No default: migrations and backups may take a while. PgBouncer refuses
package/dist/index.js CHANGED
@@ -96,22 +96,33 @@ function postgres(options) {
96
96
  }
97
97
  };
98
98
  }
99
+ function prepares(url, options) {
100
+ if (options.prepare !== void 0) return options.prepare;
101
+ try {
102
+ return new URL(url).searchParams.get("prepare") !== "false";
103
+ } catch {
104
+ return true;
105
+ }
106
+ }
99
107
  function postgresJsConnection(url, options) {
100
108
  const set = (key, value) => value === void 0 ? {} : { [key]: value };
101
109
  const seconds = (ms) => ms === void 0 ? void 0 : ms / 1e3;
102
- const client = postgresJs(url, {
103
- max: options.max ?? 10,
110
+ const open = (max) => postgresJs(url, {
111
+ max,
104
112
  ...set("prepare", options.prepare),
105
113
  ...set("idle_timeout", seconds(options.idleTimeoutMs)),
106
114
  ...set("connect_timeout", seconds(options.connectTimeoutMs)),
107
115
  connection: {
108
116
  application_name: options.applicationName ?? (process.env.PGAPPNAME || "easy-cms"),
109
- ...set("statement_timeout", options.statementTimeoutMs)
117
+ ...set("statement_timeout", options.statementTimeoutMs),
118
+ ...options.customPlans ?? prepares(url, options) ? { plan_cache_mode: "force_custom_plan" } : {}
110
119
  },
111
120
  // Keep Easy CMS quiet: Postgres NOTICEs (e.g. "relation already exists, skipping") are expected.
112
121
  onnotice: () => {
113
122
  }
114
123
  });
124
+ const client = open(options.max ?? 10);
125
+ const writer = options.writeConnections ? open(options.writeConnections) : void 0;
115
126
  const runner = {
116
127
  async query(text2, params = []) {
117
128
  return [...await client.unsafe(text2, [...params])];
@@ -122,7 +133,14 @@ function postgresJsConnection(url, options) {
122
133
  });
123
134
  }
124
135
  };
125
- return { db: drizzlePostgresJs(client), runner, close: () => client.end({ timeout: 5 }) };
136
+ return {
137
+ db: drizzlePostgresJs(client),
138
+ writeDb: writer && drizzlePostgresJs(writer),
139
+ runner,
140
+ close: async () => {
141
+ await Promise.all([client.end({ timeout: 5 }), writer?.end({ timeout: 5 })]);
142
+ }
143
+ };
126
144
  }
127
145
  async function pgliteConnection(source, cwd) {
128
146
  const owned = typeof source === "string";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/db-postgres",
3
- "version": "1.15.1",
3
+ "version": "1.16.0",
4
4
  "description": "PostgreSQL database adapter for Easy CMS",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -22,11 +22,11 @@
22
22
  "drizzle-kit": "0.31.11",
23
23
  "drizzle-orm": "0.45.4",
24
24
  "postgres": "^3.4.9",
25
- "@easy-cms/drizzle": "^1.15.1"
25
+ "@easy-cms/drizzle": "^1.16.0"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "@electric-sql/pglite": ">=0.3.0",
29
- "@easy-cms/core": "^1.15.1"
29
+ "@easy-cms/core": "^1.16.0"
30
30
  },
31
31
  "peerDependenciesMeta": {
32
32
  "@electric-sql/pglite": {
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "devDependencies": {
37
37
  "@electric-sql/pglite": "^0.5.8",
38
- "@easy-cms/core": "^1.15.1"
38
+ "@easy-cms/core": "^1.16.0"
39
39
  },
40
40
  "publishConfig": {
41
41
  "access": "public"