@easy-cms/db-postgres 1.4.3 → 1.6.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/README.md CHANGED
@@ -27,6 +27,11 @@ export default defineConfig({
27
27
  |---|---|---|
28
28
  | `url` | — | Connection string |
29
29
  | `max` | `10` | Pool size |
30
+ | `prepare` | `true` | Prepared statements; `false` behind a pooler that can't keep them |
31
+ | `statementTimeoutMs` | — | Cancels longer statements (not behind PgBouncer: set it on the role) |
32
+ | `idleTimeoutMs` | — | Closes idle connections after this long |
33
+ | `connectTimeoutMs` | `30000` | Gives up opening a connection after this long |
34
+ | `applicationName` | `easy-cms` | Shown in `pg_stat_activity` |
30
35
  | `pglite` | — | Data directory (relative to the project), `memory://`, or a `PGlite` instance. Needs `@electric-sql/pglite`. |
31
36
  | `tablePrefix` | `ecms_` | Prefix of every table Easy CMS creates |
32
37
  | `migrationDir` | `easy-cms/migrations` | Where migration files are written |
@@ -36,6 +41,6 @@ Text sorting follows the database collation (server defaults like `en_US.UTF-8`
36
41
 
37
42
  ## Links
38
43
 
39
- [Databases](https://easy-cms-website.vercel.app/docs/databases) · [Documentation](https://easy-cms-website.vercel.app/docs) ([ภาษาไทย](https://easy-cms-website.vercel.app/th/docs)) · [GitHub](https://github.com/maritonx/easy-cms)
44
+ [Databases](https://easy-cms-website.vercel.app/docs/databases) (connections, poolers, serverless) · [Documentation](https://easy-cms-website.vercel.app/docs) ([ภาษาไทย](https://easy-cms-website.vercel.app/th/docs)) · [GitHub](https://github.com/maritonx/easy-cms)
40
45
 
41
46
  MIT License
package/dist/index.d.ts CHANGED
@@ -8,6 +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
+ * Use prepared statements. Default true. Turn off behind a pooler in transaction mode that
13
+ * can't keep them (PgBouncer before 1.24, or with `max_prepared_statements = 0`, and some
14
+ * hosted poolers): it would run the next query on a server connection without the statement.
15
+ */
16
+ readonly prepare?: boolean;
17
+ /**
18
+ * Cancels a statement running longer than this (ms), sent as `statement_timeout` when a
19
+ * connection opens. No default: migrations and backups may take a while. PgBouncer refuses
20
+ * the parameter; set it on the role there (`ALTER ROLE … SET statement_timeout = …`).
21
+ */
22
+ readonly statementTimeoutMs?: number;
23
+ /** Closes a connection left idle this long (ms). Default: never. */
24
+ readonly idleTimeoutMs?: number;
25
+ /** Gives up opening a connection after this long (ms). Default 30000. */
26
+ readonly connectTimeoutMs?: number;
27
+ /**
28
+ * `application_name` of the connections, shown in `pg_stat_activity`. Default `PGAPPNAME`,
29
+ * then `'easy-cms'`.
30
+ */
31
+ readonly applicationName?: string;
11
32
  /**
12
33
  * Use PGlite (Postgres in WebAssembly) instead of a server: a data directory
13
34
  * (relative to the project root), `memory://`, or a PGlite instance you manage.
package/dist/index.js CHANGED
@@ -87,14 +87,27 @@ function postgres(options) {
87
87
  name: "postgres",
88
88
  apiVersion: 1,
89
89
  init: async (args) => {
90
- const connection = options.pglite ? await pgliteConnection(options.pglite, args.cwd) : postgresJsConnection(options.url, options.max ?? 10);
90
+ const connection = options.pglite ? await pgliteConnection(options.pglite, args.cwd) : postgresJsConnection(options.url, options);
91
91
  return connectDatabase(postgresDialect, connection, args, options);
92
92
  }
93
93
  };
94
94
  }
95
- function postgresJsConnection(url, max) {
96
- const client = postgresJs(url, { max, onnotice: () => {
97
- } });
95
+ function postgresJsConnection(url, options) {
96
+ const set = (key, value) => value === void 0 ? {} : { [key]: value };
97
+ const seconds = (ms) => ms === void 0 ? void 0 : ms / 1e3;
98
+ const client = postgresJs(url, {
99
+ max: options.max ?? 10,
100
+ ...set("prepare", options.prepare),
101
+ ...set("idle_timeout", seconds(options.idleTimeoutMs)),
102
+ ...set("connect_timeout", seconds(options.connectTimeoutMs)),
103
+ connection: {
104
+ application_name: options.applicationName ?? (process.env.PGAPPNAME || "easy-cms"),
105
+ ...set("statement_timeout", options.statementTimeoutMs)
106
+ },
107
+ // Keep Easy CMS quiet: Postgres NOTICEs (e.g. "relation already exists, skipping") are expected.
108
+ onnotice: () => {
109
+ }
110
+ });
98
111
  const runner = {
99
112
  async query(text2, params = []) {
100
113
  return [...await client.unsafe(text2, [...params])];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/db-postgres",
3
- "version": "1.4.3",
3
+ "version": "1.6.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.3",
24
24
  "postgres": "^3.4.9",
25
- "@easy-cms/drizzle": "^1.4.3"
25
+ "@easy-cms/drizzle": "^1.6.0"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "@electric-sql/pglite": ">=0.3.0",
29
- "@easy-cms/core": "^1.4.3"
29
+ "@easy-cms/core": "^1.6.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.4.3"
38
+ "@easy-cms/core": "^1.6.0"
39
39
  },
40
40
  "publishConfig": {
41
41
  "access": "public"