@juit/pgproxy-client-psql 1.0.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 ADDED
@@ -0,0 +1,50 @@
1
+ # PostgreSQL Proxy Client (LibPQ Implementation)
2
+
3
+ This package provides a `PGClient` implementation _directly connecting_ to
4
+ PostgreSQL servers, and not requiring a PGProxy Server to run.
5
+
6
+ This is useful (mostly? only?) when running tests with a PostgreSQL instance
7
+ running on a developer's machine, or in CI environments.
8
+
9
+ * [Usage with PGClient](#usage-with-pgclient)
10
+ * [Direct Usage](#direct-usage)
11
+ * [Environment Variables](#environment-variables)
12
+ * [PGProxy](https://github.com/juitnow/juit-pgproxy/blob/main/README.md)
13
+ * [Copyright Notice](https://github.com/juitnow/juit-pgproxy/blob/main/NOTICE.md)
14
+ * [License](https://github.com/juitnow/juit-pgproxy/blob/main/NOTICE.md)
15
+
16
+ ### Usage with PGClient
17
+
18
+ Simply register the client by importing it, and ensure that the `PGURL`
19
+ environment variable is set to the `psql` url of the PostgreSQL server (or
20
+ specify the URL in the constructor):
21
+
22
+ ```ts
23
+ import '@juit/pgproxy-client-psql'
24
+ import { PGClient } from '@juit/pgproxy-client'
25
+
26
+ const client = new PGClient('psql://username:password@locahost:5432/my-database')
27
+ ```
28
+
29
+ ### Direct usage
30
+
31
+ The PSQL client can be used directly by simply importing the `PSQLClient`
32
+ class:
33
+
34
+ ```ts
35
+ import { PSQLClient } from '@juit/pgproxy-client-psql'
36
+
37
+ const client = new PSQLClient('psql://username:password@locahost:5432/my-database')
38
+ ```
39
+
40
+ ### Environment Variables
41
+
42
+ The `PSQLClient` implementation does not _require_ a connection URL, and all
43
+ connection parameters can be specified via `libpq` environment variables.
44
+
45
+ The special (empty) URL `psql:///` can be used with `PGClient` in order to
46
+ trigger this behaviour or `PSQLClient` instances can be constructed without
47
+ an URL parameter.
48
+
49
+ For a description of the environment variables supported by `libpq` simply look
50
+ at the [official documentation](https://www.postgresql.org/docs/current/libpq-envars.html).
package/dist/index.cjs ADDED
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ PSQLClient: () => PSQLClient,
24
+ PSQLProvider: () => PSQLProvider
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+ var import_pgproxy_client = require("@juit/pgproxy-client");
28
+ var import_pgproxy_pool = require("@juit/pgproxy-pool");
29
+ function setupPoolOption(url, options, option) {
30
+ if (url.searchParams.has(option)) {
31
+ options[option] = Number(url.searchParams.get(option));
32
+ }
33
+ }
34
+ function setupPoolOptions(url, options) {
35
+ setupPoolOption(url, options, "minimumPoolSize");
36
+ setupPoolOption(url, options, "maximumPoolSize");
37
+ setupPoolOption(url, options, "maximumIdleConnections");
38
+ setupPoolOption(url, options, "acquireTimeout");
39
+ setupPoolOption(url, options, "borrowTimeout");
40
+ setupPoolOption(url, options, "retryInterval");
41
+ }
42
+ var PSQLProvider = class extends import_pgproxy_client.AbstractPGProvider {
43
+ _pool;
44
+ constructor(url) {
45
+ super();
46
+ if (!url) {
47
+ this._pool = new import_pgproxy_pool.ConnectionPool(PSQLClient.logger);
48
+ } else {
49
+ if (typeof url === "string")
50
+ url = new URL(url);
51
+ (0, import_pgproxy_client.assert)(url.protocol === "psql:", `Unsupported protocol "${url.protocol}"`);
52
+ const options = {};
53
+ if (url.username)
54
+ options.user = decodeURIComponent(url.username);
55
+ if (url.password)
56
+ options.password = decodeURIComponent(url.password);
57
+ if (url.hostname)
58
+ options.host = url.hostname;
59
+ if (url.port)
60
+ options.port = Number(url.port);
61
+ if (url.pathname !== "/")
62
+ options.database = url.pathname.substring(1);
63
+ setupPoolOptions(url, options);
64
+ this._pool = new import_pgproxy_pool.ConnectionPool(PSQLClient.logger, options);
65
+ }
66
+ }
67
+ async acquire() {
68
+ if (!this._pool.running)
69
+ await this._pool.start();
70
+ return this._pool.acquire();
71
+ }
72
+ async release(connection) {
73
+ await this._pool.release(connection);
74
+ }
75
+ async destroy() {
76
+ await this._pool.stop();
77
+ }
78
+ };
79
+ var PSQLClient = class extends import_pgproxy_client.PGClient {
80
+ constructor(url) {
81
+ super(new PSQLProvider(url));
82
+ }
83
+ /* coverage ignore next */
84
+ static logger = {
85
+ debug: () => void 0,
86
+ info: () => void 0,
87
+ warn: () => void 0,
88
+ error: () => void 0
89
+ };
90
+ };
91
+ (0, import_pgproxy_client.registerProvider)("psql", PSQLProvider);
92
+ // Annotate the CommonJS export names for ESM import in node:
93
+ 0 && (module.exports = {
94
+ PSQLClient,
95
+ PSQLProvider
96
+ });
97
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts"],
4
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAAuE;AACvE,0BAA+B;AAI/B,SAAS,gBACL,KACA,SACA,QAOI;AACN,MAAI,IAAI,aAAa,IAAI,MAAM,GAAG;AAChC,YAAQ,MAAM,IAAI,OAAO,IAAI,aAAa,IAAI,MAAM,CAAC;AAAA,EACvD;AACF;AAEA,SAAS,iBAAiB,KAAU,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AAyB,+BAAS;AAAA,EACvC,YAAY,KAAoB;AAC9B,UAAM,IAAI,aAAa,GAAG,CAAC;AAAA,EAC7B;AAAA;AAAA,EAGA,OAAO,SAAiB;AAAA,IACtB,OAAO,MAAM;AAAA,IACb,MAAM,MAAM;AAAA,IACZ,MAAM,MAAM;AAAA,IACZ,OAAO,MAAM;AAAA,EACf;AACF;AAAA,IAEA,wCAAiB,QAAQ,YAAY;",
5
+ "names": []
6
+ }
@@ -0,0 +1,14 @@
1
+ /// <reference types="node" />
2
+ import { AbstractPGProvider, PGClient } from '@juit/pgproxy-client';
3
+ import type { Connection, Logger } from '@juit/pgproxy-pool';
4
+ export declare class PSQLProvider extends AbstractPGProvider<Connection> {
5
+ private _pool;
6
+ constructor(url?: URL | string);
7
+ acquire(): Promise<Connection>;
8
+ release(connection: Connection): Promise<void>;
9
+ destroy(): Promise<void>;
10
+ }
11
+ export declare class PSQLClient extends PGClient {
12
+ constructor(url?: URL | string);
13
+ static logger: Logger;
14
+ }
package/dist/index.mjs ADDED
@@ -0,0 +1,71 @@
1
+ // index.ts
2
+ import { AbstractPGProvider, PGClient, assert, registerProvider } from "@juit/pgproxy-client";
3
+ import { ConnectionPool } from "@juit/pgproxy-pool";
4
+ function setupPoolOption(url, options, option) {
5
+ if (url.searchParams.has(option)) {
6
+ options[option] = Number(url.searchParams.get(option));
7
+ }
8
+ }
9
+ function setupPoolOptions(url, options) {
10
+ setupPoolOption(url, options, "minimumPoolSize");
11
+ setupPoolOption(url, options, "maximumPoolSize");
12
+ setupPoolOption(url, options, "maximumIdleConnections");
13
+ setupPoolOption(url, options, "acquireTimeout");
14
+ setupPoolOption(url, options, "borrowTimeout");
15
+ setupPoolOption(url, options, "retryInterval");
16
+ }
17
+ var PSQLProvider = class extends AbstractPGProvider {
18
+ _pool;
19
+ constructor(url) {
20
+ super();
21
+ if (!url) {
22
+ this._pool = new ConnectionPool(PSQLClient.logger);
23
+ } else {
24
+ if (typeof url === "string")
25
+ url = new URL(url);
26
+ assert(url.protocol === "psql:", `Unsupported protocol "${url.protocol}"`);
27
+ const options = {};
28
+ if (url.username)
29
+ options.user = decodeURIComponent(url.username);
30
+ if (url.password)
31
+ options.password = decodeURIComponent(url.password);
32
+ if (url.hostname)
33
+ options.host = url.hostname;
34
+ if (url.port)
35
+ options.port = Number(url.port);
36
+ if (url.pathname !== "/")
37
+ options.database = url.pathname.substring(1);
38
+ setupPoolOptions(url, options);
39
+ this._pool = new ConnectionPool(PSQLClient.logger, options);
40
+ }
41
+ }
42
+ async acquire() {
43
+ if (!this._pool.running)
44
+ await this._pool.start();
45
+ return this._pool.acquire();
46
+ }
47
+ async release(connection) {
48
+ await this._pool.release(connection);
49
+ }
50
+ async destroy() {
51
+ await this._pool.stop();
52
+ }
53
+ };
54
+ var PSQLClient = class extends PGClient {
55
+ constructor(url) {
56
+ super(new PSQLProvider(url));
57
+ }
58
+ /* coverage ignore next */
59
+ static logger = {
60
+ debug: () => void 0,
61
+ info: () => void 0,
62
+ warn: () => void 0,
63
+ error: () => void 0
64
+ };
65
+ };
66
+ registerProvider("psql", PSQLProvider);
67
+ export {
68
+ PSQLClient,
69
+ PSQLProvider
70
+ };
71
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts"],
4
+ "mappings": ";AAAA,SAAS,oBAAoB,UAAU,QAAQ,wBAAwB;AACvE,SAAS,sBAAsB;AAI/B,SAAS,gBACL,KACA,SACA,QAOI;AACN,MAAI,IAAI,aAAa,IAAI,MAAM,GAAG;AAChC,YAAQ,MAAM,IAAI,OAAO,IAAI,aAAa,IAAI,MAAM,CAAC;AAAA,EACvD;AACF;AAEA,SAAS,iBAAiB,KAAU,SAAsC;AACxE,kBAAgB,KAAK,SAAS,iBAAiB;AAC/C,kBAAgB,KAAK,SAAS,iBAAiB;AAC/C,kBAAgB,KAAK,SAAS,wBAAwB;AACtD,kBAAgB,KAAK,SAAS,gBAAgB;AAC9C,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MAAM;AAAA,IACZ,MAAM,MAAM;AAAA,IACZ,OAAO,MAAM;AAAA,EACf;AACF;AAEA,iBAAiB,QAAQ,YAAY;",
5
+ "names": []
6
+ }
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@juit/pgproxy-client-psql",
3
+ "version": "1.0.0",
4
+ "main": "./dist/index.cjs",
5
+ "module": "./dist/index.mjs",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "require": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.cjs"
12
+ },
13
+ "import": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.mjs"
16
+ }
17
+ }
18
+ },
19
+ "author": "Juit Developers <developers@juit.com>",
20
+ "license": "Apache-2.0",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+ssh://git@github.com/juitnow/juit-pgproxy.git"
24
+ },
25
+ "keywords": [
26
+ "database",
27
+ "pg",
28
+ "pool",
29
+ "postgres",
30
+ "proxy"
31
+ ],
32
+ "bugs": {
33
+ "url": "https://github.com/juitnow/juit-pgproxy/issues"
34
+ },
35
+ "homepage": "https://github.com/juitnow/juit-pgproxy#readme",
36
+ "dependencies": {
37
+ "@juit/pgproxy-client": "1.0.0",
38
+ "@juit/pgproxy-pool": "1.0.0"
39
+ },
40
+ "directories": {
41
+ "test": "test"
42
+ },
43
+ "files": [
44
+ "*.md",
45
+ "dist/",
46
+ "src/"
47
+ ]
48
+ }
package/src/index.ts ADDED
@@ -0,0 +1,83 @@
1
+ import { AbstractPGProvider, PGClient, assert, registerProvider } from '@juit/pgproxy-client'
2
+ import { ConnectionPool } from '@juit/pgproxy-pool'
3
+
4
+ import type { Connection, ConnectionPoolOptions, Logger } from '@juit/pgproxy-pool'
5
+
6
+ function setupPoolOption(
7
+ url: URL,
8
+ options: ConnectionPoolOptions,
9
+ option:
10
+ | 'minimumPoolSize'
11
+ | 'maximumPoolSize'
12
+ | 'maximumIdleConnections'
13
+ | 'acquireTimeout'
14
+ | 'borrowTimeout'
15
+ | 'retryInterval',
16
+ ): void {
17
+ if (url.searchParams.has(option)) {
18
+ options[option] = Number(url.searchParams.get(option))
19
+ }
20
+ }
21
+
22
+ function setupPoolOptions(url: URL, options: ConnectionPoolOptions): void {
23
+ setupPoolOption(url, options, 'minimumPoolSize')
24
+ setupPoolOption(url, options, 'maximumPoolSize')
25
+ setupPoolOption(url, options, 'maximumIdleConnections')
26
+ setupPoolOption(url, options, 'acquireTimeout')
27
+ setupPoolOption(url, options, 'borrowTimeout')
28
+ setupPoolOption(url, options, 'retryInterval')
29
+ }
30
+
31
+ export class PSQLProvider extends AbstractPGProvider<Connection> {
32
+ private _pool: ConnectionPool
33
+
34
+ constructor(url?: URL | string) {
35
+ super()
36
+
37
+ if (! url) {
38
+ this._pool = new ConnectionPool(PSQLClient.logger)
39
+ } else {
40
+ if (typeof url === 'string') url = new URL(url)
41
+ assert(url.protocol === 'psql:', `Unsupported protocol "${url.protocol}"`)
42
+
43
+ const options: ConnectionPoolOptions = {}
44
+ if (url.username) options.user = decodeURIComponent(url.username)
45
+ if (url.password) options.password = decodeURIComponent(url.password)
46
+ if (url.hostname) options.host = url.hostname
47
+ if (url.port) options.port = Number(url.port)
48
+ if (url.pathname !== '/') options.database = url.pathname.substring(1)
49
+
50
+ setupPoolOptions(url, options)
51
+ this._pool = new ConnectionPool(PSQLClient.logger, options)
52
+ }
53
+ }
54
+
55
+ async acquire(): Promise<Connection> {
56
+ if (! this._pool.running) await this._pool.start()
57
+ return this._pool.acquire()
58
+ }
59
+
60
+ async release(connection: Connection): Promise<void> {
61
+ await this._pool.release(connection)
62
+ }
63
+
64
+ async destroy(): Promise<void> {
65
+ await this._pool.stop()
66
+ }
67
+ }
68
+
69
+ export class PSQLClient extends PGClient {
70
+ constructor(url?: URL | string) {
71
+ super(new PSQLProvider(url))
72
+ }
73
+
74
+ /* coverage ignore next */
75
+ static logger: Logger = {
76
+ debug: () => void 0,
77
+ info: () => void 0,
78
+ warn: () => void 0,
79
+ error: () => void 0,
80
+ }
81
+ }
82
+
83
+ registerProvider('psql', PSQLProvider)