@mikro-orm/postgresql 7.1.0-dev.8 → 7.1.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,8 +1,11 @@
1
1
  import { type PoolConfig } from 'pg';
2
2
  import { PostgresDialect } from 'kysely';
3
+ import { type Routine, type Transaction } from '@mikro-orm/core';
3
4
  import { AbstractSqlConnection } from '@mikro-orm/sql';
4
5
  /** PostgreSQL database connection using the `pg` driver. */
5
6
  export declare class PostgreSqlConnection extends AbstractSqlConnection {
6
7
  createKyselyDialect(overrides: PoolConfig): PostgresDialect;
7
8
  mapOptions(overrides: PoolConfig): PoolConfig;
9
+ callRoutine<T>(routine: Routine, args?: Record<string, unknown>, ctx?: Transaction): Promise<T>;
10
+ private fetchRefcursors;
8
11
  }
@@ -2,13 +2,16 @@ import { Pool, TypeOverrides } from 'pg';
2
2
  import Cursor from 'pg-cursor';
3
3
  import { PostgresDialect } from 'kysely';
4
4
  import array from 'postgres-array';
5
- import { AbstractSqlConnection, Utils } from '@mikro-orm/sql';
5
+ import { AbstractSqlConnection, createPostgreSqlTypeParsers, Utils } from '@mikro-orm/sql';
6
6
  /** PostgreSQL database connection using the `pg` driver. */
7
7
  export class PostgreSqlConnection extends AbstractSqlConnection {
8
8
  createKyselyDialect(overrides) {
9
- const options = this.mapOptions(overrides);
9
+ const { onPoolCreated, ...poolOverrides } = (overrides ?? {});
10
+ const options = this.mapOptions(poolOverrides);
11
+ const pool = new Pool(options);
12
+ void onPoolCreated?.(pool);
10
13
  return new PostgresDialect({
11
- pool: new Pool(options),
14
+ pool,
12
15
  cursor: Cursor,
13
16
  onCreateConnection: this.options.onCreateConnection ?? this.config.get('onCreateConnection'),
14
17
  });
@@ -18,21 +21,45 @@ export class PostgreSqlConnection extends AbstractSqlConnection {
18
21
  const pool = this.config.get('pool');
19
22
  Utils.defaultValue(ret, 'max', pool?.max);
20
23
  Utils.defaultValue(ret, 'idleTimeoutMillis', pool?.idleTimeoutMillis);
21
- // use `select typname, oid, typarray from pg_type order by oid` to get the list of OIDs
22
24
  const types = new TypeOverrides();
23
- [
24
- 1082, // date
25
- 1114, // timestamp
26
- 1184, // timestamptz
27
- 1186, // interval
28
- ].forEach(oid => types.setTypeParser(oid, str => str));
29
- [
30
- 1182, // date[]
31
- 1115, // timestamp[]
32
- 1185, // timestamptz[]
33
- 1187, // interval[]
34
- ].forEach(oid => types.setTypeParser(oid, str => array.parse(str)));
25
+ for (const [oid, parser] of Object.entries(createPostgreSqlTypeParsers(s => array.parse(s)))) {
26
+ types.setTypeParser(Number(oid), parser);
27
+ }
35
28
  ret.types = types;
36
29
  return Utils.mergeConfig(ret, overrides);
37
30
  }
31
+ async callRoutine(routine, args = {}, ctx) {
32
+ if (routine.type === 'function') {
33
+ return this.callRoutineFunction(routine, args, ctx);
34
+ }
35
+ const quoted = (id) => this.platform.quoteIdentifier(id);
36
+ const qualified = (routine.schema ? `${quoted(routine.schema)}.` : '') + quoted(routine.name);
37
+ // Refcursor OUT params come back as server-generated cursor names to FETCH from later.
38
+ const refcursorParams = routine.params.filter(p => p.direction !== 'in' && typeof p.type === 'string' && /^refcursor$/i.test(p.type));
39
+ // Refcursors are transaction-scoped — fail fast instead of opening cursors we can't read.
40
+ if (refcursorParams.length > 0 && !ctx) {
41
+ throw new Error(`Routine ${routine.name} declares refcursor OUT params on PostgreSQL but was not called inside a transaction. Wrap the call in 'em.transactional(...)' so the refcursor OUT params remain valid for FETCH.`);
42
+ }
43
+ const placeholders = routine.params.map(() => '?').join(', ');
44
+ const positional = routine.params.map(p => this.convertRoutineInbound(args[p.name], p));
45
+ const rows = (await this.execute(`call ${qualified}(${placeholders})`, positional, 'all', ctx));
46
+ const row = rows[0] ?? {};
47
+ const scalarOutParams = routine.params.filter(p => p.direction !== 'in' && !refcursorParams.includes(p));
48
+ this.applyRoutineOutParams(row, scalarOutParams, args);
49
+ if (refcursorParams.length > 0) {
50
+ return (await this.fetchRefcursors(row, routine, refcursorParams, ctx));
51
+ }
52
+ return undefined;
53
+ }
54
+ async fetchRefcursors(row, routine, refcursorParams, ctx) {
55
+ const cursorNames = refcursorParams
56
+ .map(p => row[p.name])
57
+ .filter((name) => typeof name === 'string');
58
+ const sets = [];
59
+ for (const cursorName of cursorNames) {
60
+ const fetched = (await this.execute(`fetch all from "${cursorName.replaceAll('"', '""')}"`, [], 'all', ctx));
61
+ sets.push(fetched);
62
+ }
63
+ return sets;
64
+ }
38
65
  }
@@ -1,18 +1,7 @@
1
- import { type EntityName } from '@mikro-orm/core';
2
- import { SqlEntityManager } from '@mikro-orm/sql';
1
+ import { BasePostgreSqlEntityManager } from '@mikro-orm/sql';
3
2
  import type { PostgreSqlDriver } from './PostgreSqlDriver.js';
4
3
  /**
5
4
  * @inheritDoc
6
5
  */
7
- export declare class PostgreSqlEntityManager<Driver extends PostgreSqlDriver = PostgreSqlDriver> extends SqlEntityManager<Driver> {
8
- /**
9
- * Refreshes a materialized view.
10
- *
11
- * @param entityName - The entity name or class of the materialized view
12
- * @param options - Optional settings
13
- * @param options.concurrently - If true, refreshes the view concurrently (requires a unique index on the view)
14
- */
15
- refreshMaterializedView<Entity extends object>(entityName: EntityName<Entity>, options?: {
16
- concurrently?: boolean;
17
- }): Promise<void>;
6
+ export declare class PostgreSqlEntityManager<Driver extends PostgreSqlDriver = PostgreSqlDriver> extends BasePostgreSqlEntityManager<Driver> {
18
7
  }
@@ -1,23 +1,6 @@
1
- import { SqlEntityManager } from '@mikro-orm/sql';
1
+ import { BasePostgreSqlEntityManager } from '@mikro-orm/sql';
2
2
  /**
3
3
  * @inheritDoc
4
4
  */
5
- export class PostgreSqlEntityManager extends SqlEntityManager {
6
- /**
7
- * Refreshes a materialized view.
8
- *
9
- * @param entityName - The entity name or class of the materialized view
10
- * @param options - Optional settings
11
- * @param options.concurrently - If true, refreshes the view concurrently (requires a unique index on the view)
12
- */
13
- async refreshMaterializedView(entityName, options) {
14
- const meta = this.getMetadata(entityName);
15
- if (!meta.view || !meta.materialized) {
16
- throw new Error(`Entity ${meta.className} is not a materialized view`);
17
- }
18
- const helper = this.getDriver().getPlatform().getSchemaHelper();
19
- const schema = meta.schema ?? this.config.get('schema');
20
- const sql = helper.refreshMaterializedView(meta.tableName, schema, options?.concurrently);
21
- await this.execute(sql);
22
- }
5
+ export class PostgreSqlEntityManager extends BasePostgreSqlEntityManager {
23
6
  }
@@ -1,4 +1,5 @@
1
- import { type AnyEntity, type EntityClass, type EntitySchema, MikroORM, type Options, type IDatabaseDriver, type EntityManager, type EntityManagerType } from '@mikro-orm/core';
1
+ import { type AnyEntity, type EntityClass, type EntitySchema, type MikroORM, type Options, type IDatabaseDriver, type EntityManager, type EntityManagerType } from '@mikro-orm/core';
2
+ import { SqlMikroORM } from '@mikro-orm/sql';
2
3
  import { PostgreSqlDriver } from './PostgreSqlDriver.js';
3
4
  import type { PostgreSqlEntityManager } from './PostgreSqlEntityManager.js';
4
5
  /** Configuration options for the PostgreSQL driver. */
@@ -8,7 +9,7 @@ export declare function definePostgreSqlConfig<EM extends PostgreSqlEntityManage
8
9
  /**
9
10
  * @inheritDoc
10
11
  */
11
- export declare class PostgreSqlMikroORM<EM extends PostgreSqlEntityManager = PostgreSqlEntityManager, Entities extends readonly (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]> extends MikroORM<PostgreSqlDriver, EM, Entities> {
12
+ export declare class PostgreSqlMikroORM<EM extends PostgreSqlEntityManager = PostgreSqlEntityManager, Entities extends readonly (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]> extends SqlMikroORM<PostgreSqlDriver, EM, Entities> {
12
13
  /**
13
14
  * @inheritDoc
14
15
  */
@@ -1,4 +1,5 @@
1
- import { defineConfig, MikroORM, } from '@mikro-orm/core';
1
+ import { defineConfig, } from '@mikro-orm/core';
2
+ import { SqlMikroORM } from '@mikro-orm/sql';
2
3
  import { PostgreSqlDriver } from './PostgreSqlDriver.js';
3
4
  /** Creates a type-safe configuration object for the PostgreSQL driver. */
4
5
  export function definePostgreSqlConfig(options) {
@@ -7,7 +8,7 @@ export function definePostgreSqlConfig(options) {
7
8
  /**
8
9
  * @inheritDoc
9
10
  */
10
- export class PostgreSqlMikroORM extends MikroORM {
11
+ export class PostgreSqlMikroORM extends SqlMikroORM {
11
12
  /**
12
13
  * @inheritDoc
13
14
  */
@@ -5,7 +5,6 @@ export declare class PostgreSqlPlatform extends BasePostgreSqlPlatform {
5
5
  convertIntervalToJSValue(value: string): unknown;
6
6
  convertIntervalToDatabaseValue(value: IPostgresInterval): unknown;
7
7
  unmarshallArray(value: string): string[];
8
- escape(value: any): string;
9
8
  /**
10
9
  * @inheritDoc
11
10
  */
@@ -1,4 +1,3 @@
1
- import { Client } from 'pg';
2
1
  import array from 'postgres-array';
3
2
  import parseDate from 'postgres-date';
4
3
  import PostgresInterval from 'postgres-interval';
@@ -17,24 +16,6 @@ export class PostgreSqlPlatform extends BasePostgreSqlPlatform {
17
16
  unmarshallArray(value) {
18
17
  return array.parse(value);
19
18
  }
20
- escape(value) {
21
- if (typeof value === 'bigint') {
22
- value = value.toString();
23
- }
24
- if (typeof value === 'string') {
25
- return Client.prototype.escapeLiteral(value);
26
- }
27
- if (value instanceof Date) {
28
- return `'${this.formatDate(value)}'`;
29
- }
30
- if (ArrayBuffer.isView(value)) {
31
- return `E'\\\\x${value.toString('hex')}'`;
32
- }
33
- if (Array.isArray(value)) {
34
- return value.map(v => this.escape(v)).join(', ');
35
- }
36
- return value;
37
- }
38
19
  /**
39
20
  * @inheritDoc
40
21
  */
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  <a href="https://mikro-orm.io"><img src="https://raw.githubusercontent.com/mikro-orm/mikro-orm/master/docs/static/img/logo-readme.svg?sanitize=true" alt="MikroORM" /></a>
3
3
  </h1>
4
4
 
5
- TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-orm.io/docs/unit-of-work/) and [Identity Map](https://mikro-orm.io/docs/identity-map/) patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL, SQLite (including libSQL), MSSQL and Oracle databases.
5
+ TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-orm.io/docs/unit-of-work/) and [Identity Map](https://mikro-orm.io/docs/identity-map/) patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL (including CockroachDB and PGlite), SQLite (including libSQL), MSSQL and Oracle databases.
6
6
 
7
7
  > Heavily inspired by [Doctrine](https://www.doctrine-project.org/) and [Hibernate](https://hibernate.org/).
8
8
 
@@ -19,6 +19,7 @@ Install a driver package for your database:
19
19
 
20
20
  ```sh
21
21
  npm install @mikro-orm/postgresql # PostgreSQL
22
+ npm install @mikro-orm/pglite # PGlite (embedded PostgreSQL in WASM)
22
23
  npm install @mikro-orm/mysql # MySQL
23
24
  npm install @mikro-orm/mariadb # MariaDB
24
25
  npm install @mikro-orm/sqlite # SQLite
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/postgresql",
3
- "version": "7.1.0-dev.8",
3
+ "version": "7.1.0",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "keywords": [
6
6
  "data-mapper",
@@ -47,19 +47,19 @@
47
47
  "copy": "node ../../scripts/copy.mjs"
48
48
  },
49
49
  "dependencies": {
50
- "@mikro-orm/sql": "7.1.0-dev.8",
51
- "kysely": "0.28.16",
52
- "pg": "8.20.0",
53
- "pg-cursor": "2.19.0",
50
+ "@mikro-orm/sql": "7.1.0",
51
+ "kysely": "0.29.2",
52
+ "pg": "8.21.0",
53
+ "pg-cursor": "2.20.0",
54
54
  "postgres-array": "3.0.4",
55
55
  "postgres-date": "2.1.0",
56
56
  "postgres-interval": "4.0.2"
57
57
  },
58
58
  "devDependencies": {
59
- "@mikro-orm/core": "^7.0.11"
59
+ "@mikro-orm/core": "^7.1.0"
60
60
  },
61
61
  "peerDependencies": {
62
- "@mikro-orm/core": "7.1.0-dev.8"
62
+ "@mikro-orm/core": "7.1.0"
63
63
  },
64
64
  "engines": {
65
65
  "node": ">= 22.17.0"