Skip to content

Commit d2cf8f9

Browse files
add Pgmq class
1 parent 77fe846 commit d2cf8f9

File tree

8 files changed

+309
-7
lines changed

8 files changed

+309
-7
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"cSpell.words": ["Pgmq"]
3+
}

index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

package-lock.json

Lines changed: 249 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"start": "ts-node index.ts",
8-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"start": "ts-node src/index.ts"
98
},
109
"keywords": [],
1110
"author": "",
1211
"license": "ISC",
1312
"devDependencies": {
13+
"@types/pg-pool": "^2.0.6",
1414
"typescript": "^5.3.3"
1515
},
1616
"dependencies": {
17+
"pg": "^8.11.3",
18+
"pg-pool": "^3.6.1",
1719
"ts-node": "^10.9.2"
1820
}
1921
}

src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const DEFAULT_MAX_POOL_SIZE = 20;
2+
export const DEFAULT_IDLE_TIMEOUT_MILLIS = 1000;
3+
export const DEFAULT_CONNECTION_TIMEOUT_MILLIS = 1000;
4+
export const DEFAULT_MAX_USES = 7500;
5+
export const DEFAULT_SSL = true;

src/index.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Client } from "pg";
2+
import Pool from "pg-pool";
3+
import { PgPoolConfig } from "./types";
4+
import {
5+
DEFAULT_CONNECTION_TIMEOUT_MILLIS,
6+
DEFAULT_IDLE_TIMEOUT_MILLIS,
7+
DEFAULT_MAX_POOL_SIZE,
8+
DEFAULT_MAX_USES,
9+
DEFAULT_SSL,
10+
} from "./constants";
11+
12+
export class Pgmq {
13+
private constructor(private readonly pool: Pool<Client>) {
14+
}
15+
16+
public static async new(c: PgPoolConfig) {
17+
if (c.max === undefined) c.max = DEFAULT_MAX_POOL_SIZE;
18+
if (c.idleTimeoutMillis === undefined) c.max = DEFAULT_IDLE_TIMEOUT_MILLIS;
19+
if (c.connectionTimeoutMillis === undefined)
20+
c.connectionTimeoutMillis = DEFAULT_CONNECTION_TIMEOUT_MILLIS;
21+
if (c.maxUses === undefined) c.maxUses = DEFAULT_MAX_USES;
22+
if (c.ssl === undefined) c.ssl = DEFAULT_SSL;
23+
24+
const pool = new Pool(c);
25+
26+
await pool.connect();
27+
const pgmq = new Pgmq(pool);
28+
await pgmq.prepare();
29+
return pgmq;
30+
}
31+
32+
private async prepare() {
33+
await this.pool.query("CREATE EXTENSION IF NOT EXISTS pgmq CASCADE");
34+
}
35+
}

src/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export type PgPoolConfig = {
2+
host: string;
3+
database: string;
4+
user: string;
5+
password: string;
6+
port: number;
7+
ssl?: boolean;
8+
max?: number;
9+
idleTimeoutMillis?: number;
10+
connectionTimeoutMillis?: number;
11+
maxUses?: number;
12+
};

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
/* Modules */
2828
"module": "commonjs" /* Specify what module code is generated. */,
29-
// "rootDir": "./", /* Specify the root folder within your source files. */
29+
"rootDir": "./src" /* Specify the root folder within your source files. */,
3030
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
3131
"baseUrl": "./" /* Specify the base directory to resolve non-relative module names. */,
3232
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */

0 commit comments

Comments
 (0)