@mikro-orm/pglite 7.1.4-dev.9 → 7.1.4
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/PgliteConnection.d.ts +12 -0
- package/PgliteConnection.js +29 -2
- package/PgliteSchemaHelper.d.ts +11 -5
- package/PgliteSchemaHelper.js +29 -9
- package/package.json +5 -5
package/PgliteConnection.d.ts
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
import { PGliteDialect } from 'kysely';
|
|
2
2
|
import { AbstractSqlConnection, type Dictionary } from '@mikro-orm/sql';
|
|
3
|
+
/**
|
|
4
|
+
* Named-database mode is active when `driverOptions.dataDir` points at a real
|
|
5
|
+
* (persistent) cluster. In that case `dbName` selects a database *within* the
|
|
6
|
+
* cluster (PGlite's `database` option) instead of acting as the cluster
|
|
7
|
+
* location, which unlocks the real `CREATE DATABASE` lifecycle (`database:create`,
|
|
8
|
+
* migrations) handled by `PgliteSchemaHelper`. It stays off for in-memory
|
|
9
|
+
* clusters (we keep a single instance alive across reconnects, so the database
|
|
10
|
+
* can't be switched without losing data) and when a user-owned instance is passed.
|
|
11
|
+
*
|
|
12
|
+
* @internal — shared between the connection and `PgliteSchemaHelper`.
|
|
13
|
+
*/
|
|
14
|
+
export declare const pgliteUsesNamedDatabase: (driverOptions: unknown) => boolean;
|
|
3
15
|
/** PostgreSQL-in-WASM database connection using `@electric-sql/pglite`. */
|
|
4
16
|
export declare class PgliteConnection extends AbstractSqlConnection {
|
|
5
17
|
#private;
|
package/PgliteConnection.js
CHANGED
|
@@ -3,6 +3,24 @@ import { PGliteDialect } from 'kysely';
|
|
|
3
3
|
import array from 'postgres-array';
|
|
4
4
|
import { AbstractSqlConnection, createPostgreSqlTypeParsers } from '@mikro-orm/sql';
|
|
5
5
|
const isInMemoryDataDir = (dataDir) => !dataDir || dataDir === 'memory://' || dataDir.startsWith('memory:');
|
|
6
|
+
/**
|
|
7
|
+
* Named-database mode is active when `driverOptions.dataDir` points at a real
|
|
8
|
+
* (persistent) cluster. In that case `dbName` selects a database *within* the
|
|
9
|
+
* cluster (PGlite's `database` option) instead of acting as the cluster
|
|
10
|
+
* location, which unlocks the real `CREATE DATABASE` lifecycle (`database:create`,
|
|
11
|
+
* migrations) handled by `PgliteSchemaHelper`. It stays off for in-memory
|
|
12
|
+
* clusters (we keep a single instance alive across reconnects, so the database
|
|
13
|
+
* can't be switched without losing data) and when a user-owned instance is passed.
|
|
14
|
+
*
|
|
15
|
+
* @internal — shared between the connection and `PgliteSchemaHelper`.
|
|
16
|
+
*/
|
|
17
|
+
export const pgliteUsesNamedDatabase = (driverOptions) => {
|
|
18
|
+
if (!driverOptions || typeof driverOptions !== 'object') {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
const { pglite, dataDir } = driverOptions;
|
|
22
|
+
return !pglite && !isInMemoryDataDir(dataDir);
|
|
23
|
+
};
|
|
6
24
|
/** PostgreSQL-in-WASM database connection using `@electric-sql/pglite`. */
|
|
7
25
|
export class PgliteConnection extends AbstractSqlConnection {
|
|
8
26
|
// Stored as a Promise so `createKyselyDialect` can stay synchronous (else
|
|
@@ -15,7 +33,11 @@ export class PgliteConnection extends AbstractSqlConnection {
|
|
|
15
33
|
const onCreateConnection = this.options.onCreateConnection ?? this.config.get('onCreateConnection');
|
|
16
34
|
const options = (overrides ?? {});
|
|
17
35
|
const { pglite: userPglite, ...pgliteOptions } = options;
|
|
18
|
-
const
|
|
36
|
+
const namedDatabase = pgliteUsesNamedDatabase(options);
|
|
37
|
+
const dbName = this.config.get('dbName');
|
|
38
|
+
// In named-database mode the cluster lives at `driverOptions.dataDir` and
|
|
39
|
+
// `dbName` selects a database inside it; otherwise `dbName` *is* the cluster.
|
|
40
|
+
const dataDir = options.dataDir ?? dbName;
|
|
19
41
|
const parsers = { ...createPostgreSqlTypeParsers(s => array.parse(s)), ...options.parsers };
|
|
20
42
|
this.#ownsPglite = !userPglite;
|
|
21
43
|
// Skip `pglite.close()` (called by kysely's `PGliteDriver.destroy()`) when:
|
|
@@ -31,7 +53,12 @@ export class PgliteConnection extends AbstractSqlConnection {
|
|
|
31
53
|
this.#pglite = typeof userPglite === 'function' ? Promise.resolve(userPglite()) : Promise.resolve(userPglite);
|
|
32
54
|
}
|
|
33
55
|
else {
|
|
34
|
-
this.#pglite = PGlite.create({
|
|
56
|
+
this.#pglite = PGlite.create({
|
|
57
|
+
...pgliteOptions,
|
|
58
|
+
dataDir,
|
|
59
|
+
parsers,
|
|
60
|
+
...(namedDatabase ? { database: dbName } : {}),
|
|
61
|
+
});
|
|
35
62
|
}
|
|
36
63
|
}
|
|
37
64
|
if (this.#neuterClose) {
|
package/PgliteSchemaHelper.d.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import type { Connection } from '@mikro-orm/core';
|
|
2
2
|
import { PostgreSqlSchemaHelper } from '@mikro-orm/sql';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
* `
|
|
4
|
+
* In the default (single-database) setup a PGlite instance maps 1:1 to a
|
|
5
|
+
* `dataDir`, so there is no management catalog to connect to and the
|
|
6
|
+
* `CREATE DATABASE` lifecycle is a no-op. When `driverOptions.dataDir` is set,
|
|
7
|
+
* `dbName` instead selects a database inside that cluster, and we defer to the
|
|
8
|
+
* PostgreSQL behaviour: connect to the `postgres` management database to run a
|
|
9
|
+
* real `CREATE DATABASE`/`DROP DATABASE`, then reconnect to the target.
|
|
6
10
|
*/
|
|
7
11
|
export declare class PgliteSchemaHelper extends PostgreSqlSchemaHelper {
|
|
12
|
+
#private;
|
|
8
13
|
getManagementDbName(): string;
|
|
9
|
-
getCreateDatabaseSQL(): string;
|
|
10
|
-
getDropDatabaseSQL(): string;
|
|
11
|
-
|
|
14
|
+
getCreateDatabaseSQL(name: string): string;
|
|
15
|
+
getDropDatabaseSQL(name: string): string;
|
|
16
|
+
getDatabaseNotExistsError(dbName: string): string;
|
|
17
|
+
databaseExists(connection: Connection, name: string): Promise<boolean>;
|
|
12
18
|
}
|
package/PgliteSchemaHelper.js
CHANGED
|
@@ -1,19 +1,39 @@
|
|
|
1
1
|
import { PostgreSqlSchemaHelper } from '@mikro-orm/sql';
|
|
2
|
+
import { pgliteUsesNamedDatabase } from './PgliteConnection.js';
|
|
2
3
|
/**
|
|
3
|
-
*
|
|
4
|
-
* `
|
|
4
|
+
* In the default (single-database) setup a PGlite instance maps 1:1 to a
|
|
5
|
+
* `dataDir`, so there is no management catalog to connect to and the
|
|
6
|
+
* `CREATE DATABASE` lifecycle is a no-op. When `driverOptions.dataDir` is set,
|
|
7
|
+
* `dbName` instead selects a database inside that cluster, and we defer to the
|
|
8
|
+
* PostgreSQL behaviour: connect to the `postgres` management database to run a
|
|
9
|
+
* real `CREATE DATABASE`/`DROP DATABASE`, then reconnect to the target.
|
|
5
10
|
*/
|
|
6
11
|
export class PgliteSchemaHelper extends PostgreSqlSchemaHelper {
|
|
12
|
+
#usesNamedDatabase() {
|
|
13
|
+
// `driverOptions` may be a factory (resolved per-connect in `createKysely`),
|
|
14
|
+
// so resolve it the same way to stay in sync with the connection.
|
|
15
|
+
let driverOptions = this.platform.getConfig().get('driverOptions');
|
|
16
|
+
if (typeof driverOptions === 'function') {
|
|
17
|
+
driverOptions = driverOptions();
|
|
18
|
+
}
|
|
19
|
+
return pgliteUsesNamedDatabase(driverOptions);
|
|
20
|
+
}
|
|
7
21
|
getManagementDbName() {
|
|
8
|
-
return '';
|
|
22
|
+
return this.#usesNamedDatabase() ? super.getManagementDbName() : '';
|
|
23
|
+
}
|
|
24
|
+
getCreateDatabaseSQL(name) {
|
|
25
|
+
return this.#usesNamedDatabase() ? super.getCreateDatabaseSQL(name) : '';
|
|
9
26
|
}
|
|
10
|
-
|
|
11
|
-
return '';
|
|
27
|
+
getDropDatabaseSQL(name) {
|
|
28
|
+
return this.#usesNamedDatabase() ? super.getDropDatabaseSQL(name) : '';
|
|
12
29
|
}
|
|
13
|
-
|
|
14
|
-
|
|
30
|
+
getDatabaseNotExistsError(dbName) {
|
|
31
|
+
// PGlite throws a generic init error (rather than Postgres' "database ...
|
|
32
|
+
// does not exist") when the `database` option points at a missing database,
|
|
33
|
+
// which is how `databaseExists()` detects absence before creating it.
|
|
34
|
+
return this.#usesNamedDatabase() ? 'PGlite failed to initialize properly' : super.getDatabaseNotExistsError(dbName);
|
|
15
35
|
}
|
|
16
|
-
async databaseExists(
|
|
17
|
-
return true;
|
|
36
|
+
async databaseExists(connection, name) {
|
|
37
|
+
return this.#usesNamedDatabase() ? super.databaseExists(connection, name) : true;
|
|
18
38
|
}
|
|
19
39
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/pglite",
|
|
3
|
-
"version": "7.1.4
|
|
3
|
+
"version": "7.1.4",
|
|
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",
|
|
@@ -48,18 +48,18 @@
|
|
|
48
48
|
"copy": "node ../../scripts/copy.mjs"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@electric-sql/pglite": "0.
|
|
52
|
-
"@mikro-orm/sql": "7.1.4
|
|
51
|
+
"@electric-sql/pglite": "0.5.1",
|
|
52
|
+
"@mikro-orm/sql": "7.1.4",
|
|
53
53
|
"kysely": "0.29.2",
|
|
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.1.
|
|
59
|
+
"@mikro-orm/core": "^7.1.4"
|
|
60
60
|
},
|
|
61
61
|
"peerDependencies": {
|
|
62
|
-
"@mikro-orm/core": "7.1.4
|
|
62
|
+
"@mikro-orm/core": "7.1.4"
|
|
63
63
|
},
|
|
64
64
|
"engines": {
|
|
65
65
|
"node": ">= 22.17.0"
|