pg-cache 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/LICENSE +23 -0
- package/README.md +187 -0
- package/dist/README.md +187 -0
- package/dist/package.json +51 -0
- package/esm/index.js +3 -0
- package/esm/lru.js +144 -0
- package/esm/pg.js +17 -0
- package/index.d.ts +3 -0
- package/index.js +11 -0
- package/lru.d.ts +23 -0
- package/lru.js +150 -0
- package/package.json +51 -0
- package/pg.d.ts +3 -0
- package/pg.js +24 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
|
|
4
|
+
Copyright (c) 2025 Hyperweb <developers@hyperweb.io>
|
|
5
|
+
Copyright (c) 2020-present, Interweb, Inc.
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# pg-cache
|
|
2
|
+
|
|
3
|
+
<p align="center" width="100%">
|
|
4
|
+
<img height="250" src="https://raw.githubusercontent.com/launchql/launchql/refs/heads/main/assets/outline-logo.svg" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center" width="100%">
|
|
8
|
+
<a href="https://github.com/launchql/launchql/actions/workflows/run-tests.yaml">
|
|
9
|
+
<img height="20" src="https://github.com/launchql/launchql/actions/workflows/run-tests.yaml/badge.svg" />
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/launchql/launchql/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a>
|
|
12
|
+
<a href="https://www.npmjs.com/package/@launchql/server-utils"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/launchql?filename=packages%2Fserver-utils%2Fpackage.json"/></a>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
PostgreSQL connection pool LRU cache manager with zero PostGraphile dependencies.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install pg-cache
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- LRU cache for PostgreSQL connection pools
|
|
26
|
+
- Automatic pool cleanup and disposal
|
|
27
|
+
- Extensible cleanup callback system
|
|
28
|
+
- Service cache for general use
|
|
29
|
+
- Graceful shutdown handling
|
|
30
|
+
- TypeScript support
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
### Basic Pool Management
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { pgCache, getRootPgPool } from 'pg-cache';
|
|
38
|
+
|
|
39
|
+
// Get or create a cached pool
|
|
40
|
+
const pool = getRootPgPool({
|
|
41
|
+
host: 'localhost',
|
|
42
|
+
port: 5432,
|
|
43
|
+
database: 'mydb',
|
|
44
|
+
user: 'postgres',
|
|
45
|
+
password: 'password'
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// Use the pool
|
|
49
|
+
const result = await pool.query('SELECT NOW()');
|
|
50
|
+
|
|
51
|
+
// Pool is automatically cached and reused
|
|
52
|
+
const samePool = getRootPgPool({ database: 'mydb' }); // Returns cached pool
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Direct Cache Access
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { pgCache } from 'pg-cache';
|
|
59
|
+
import { Pool } from 'pg';
|
|
60
|
+
|
|
61
|
+
// Create and cache a pool manually
|
|
62
|
+
const pool = new Pool({ connectionString: 'postgres://...' });
|
|
63
|
+
pgCache.set('my-pool-key', pool);
|
|
64
|
+
|
|
65
|
+
// Retrieve it later
|
|
66
|
+
const cachedPool = pgCache.get('my-pool-key');
|
|
67
|
+
|
|
68
|
+
// Remove from cache (also disposes the pool)
|
|
69
|
+
pgCache.delete('my-pool-key');
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Cleanup Callbacks
|
|
73
|
+
|
|
74
|
+
Register callbacks to be notified when pools are disposed:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { pgCache } from 'pg-cache';
|
|
78
|
+
|
|
79
|
+
// Register a cleanup callback
|
|
80
|
+
const unregister = pgCache.registerCleanupCallback((poolKey: string) => {
|
|
81
|
+
console.log(`Pool ${poolKey} was disposed`);
|
|
82
|
+
// Clean up any resources associated with this pool
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// Later, unregister if needed
|
|
86
|
+
unregister();
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Service Cache
|
|
90
|
+
|
|
91
|
+
A general-purpose cache is also provided:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { svcCache } from 'pg-cache';
|
|
95
|
+
|
|
96
|
+
// Cache any service or object
|
|
97
|
+
svcCache.set('my-service', myServiceInstance);
|
|
98
|
+
const service = svcCache.get('my-service');
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Graceful Shutdown
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { close, teardownPgPools } from 'pg-cache';
|
|
105
|
+
|
|
106
|
+
// In your shutdown handler
|
|
107
|
+
process.on('SIGTERM', async () => {
|
|
108
|
+
await close(); // or teardownPgPools()
|
|
109
|
+
process.exit(0);
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## API Reference
|
|
114
|
+
|
|
115
|
+
### pgCache
|
|
116
|
+
|
|
117
|
+
The main PostgreSQL pool cache instance.
|
|
118
|
+
|
|
119
|
+
- `get(key: string): Pool | undefined` - Get a cached pool
|
|
120
|
+
- `set(key: string, pool: Pool): void` - Cache a pool
|
|
121
|
+
- `has(key: string): boolean` - Check if a pool is cached
|
|
122
|
+
- `delete(key: string): void` - Remove and dispose a pool
|
|
123
|
+
- `clear(): void` - Remove and dispose all pools
|
|
124
|
+
- `registerCleanupCallback(callback: (key: string) => void): () => void` - Register a cleanup callback
|
|
125
|
+
|
|
126
|
+
### getRootPgPool(config: Partial<PgConfig>): Pool
|
|
127
|
+
|
|
128
|
+
Get or create a cached PostgreSQL pool using the provided configuration.
|
|
129
|
+
|
|
130
|
+
### svcCache
|
|
131
|
+
|
|
132
|
+
A general-purpose LRU cache for services and objects.
|
|
133
|
+
|
|
134
|
+
### close() / teardownPgPools()
|
|
135
|
+
|
|
136
|
+
Gracefully close all cached pools and wait for disposal.
|
|
137
|
+
|
|
138
|
+
## Integration with Other Packages
|
|
139
|
+
|
|
140
|
+
This package is designed to be extended. For example, `@launchql/graphile-cache` uses the cleanup callback system to automatically clean up PostGraphile instances when their associated pools are disposed.
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
## Related LaunchQL Tooling
|
|
144
|
+
|
|
145
|
+
### 🧪 Testing
|
|
146
|
+
|
|
147
|
+
* [launchql/pgsql-test](https://github.com/launchql/launchql/tree/main/packages/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
|
|
148
|
+
* [launchql/graphile-test](https://github.com/launchql/launchql/tree/main/packages/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
|
|
149
|
+
* [launchql/pg-query-context](https://github.com/launchql/launchql/tree/main/packages/pg-query-context): **🔒 Session context injection** to add session-local context (e.g., `SET LOCAL`) into queries—ideal for setting `role`, `jwt.claims`, and other session settings.
|
|
150
|
+
|
|
151
|
+
### 🧠 Parsing & AST
|
|
152
|
+
|
|
153
|
+
* [launchql/pgsql-parser](https://github.com/launchql/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
|
|
154
|
+
* [launchql/libpg-query-node](https://github.com/launchql/libpg-query-node): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
|
|
155
|
+
* [launchql/pg-proto-parser](https://github.com/launchql/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
156
|
+
* [@pgsql/enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
|
|
157
|
+
* [@pgsql/types](https://github.com/launchql/pgsql-parser/tree/main/packages/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
|
|
158
|
+
* [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
|
|
159
|
+
* [launchql/pg-ast](https://github.com/launchql/launchql/tree/main/packages/pg-ast): **🔍 Low-level AST tools** and transformations for Postgres query structures.
|
|
160
|
+
|
|
161
|
+
### 🚀 API & Dev Tools
|
|
162
|
+
|
|
163
|
+
* [launchql/server](https://github.com/launchql/launchql/tree/main/packages/server): **⚡ Express-based API server** powered by PostGraphile to expose a secure, scalable GraphQL API over your Postgres database.
|
|
164
|
+
* [launchql/explorer](https://github.com/launchql/launchql/tree/main/packages/explorer): **🔎 Visual API explorer** with GraphiQL for browsing across all databases and schemas—useful for debugging, documentation, and API prototyping.
|
|
165
|
+
|
|
166
|
+
### 🔁 Streaming & Uploads
|
|
167
|
+
|
|
168
|
+
* [launchql/s3-streamer](https://github.com/launchql/launchql/tree/main/packages/s3-streamer): **📤 Direct S3 streaming** for large files with support for metadata injection and content validation.
|
|
169
|
+
* [launchql/etag-hash](https://github.com/launchql/launchql/tree/main/packages/etag-hash): **🏷️ S3-compatible ETags** created by streaming and hashing file uploads in chunks.
|
|
170
|
+
* [launchql/etag-stream](https://github.com/launchql/launchql/tree/main/packages/etag-stream): **🔄 ETag computation** via Node stream transformer during upload or transfer.
|
|
171
|
+
* [launchql/uuid-hash](https://github.com/launchql/launchql/tree/main/packages/uuid-hash): **🆔 Deterministic UUIDs** generated from hashed content, great for deduplication and asset referencing.
|
|
172
|
+
* [launchql/uuid-stream](https://github.com/launchql/launchql/tree/main/packages/uuid-stream): **🌊 Streaming UUID generation** based on piped file content—ideal for upload pipelines.
|
|
173
|
+
* [launchql/upload-names](https://github.com/launchql/launchql/tree/main/packages/upload-names): **📂 Collision-resistant filenames** utility for structured and unique file names for uploads.
|
|
174
|
+
|
|
175
|
+
### 🧰 CLI & Codegen
|
|
176
|
+
|
|
177
|
+
* [@launchql/cli](https://github.com/launchql/launchql/tree/main/packages/cli): **🖥️ Command-line toolkit** for managing LaunchQL projects—supports database scaffolding, migrations, seeding, code generation, and automation.
|
|
178
|
+
* [launchql/launchql-gen](https://github.com/launchql/launchql/tree/main/packages/launchql-gen): **✨ Auto-generated GraphQL** mutations and queries dynamically built from introspected schema data.
|
|
179
|
+
* [@launchql/query-builder](https://github.com/launchql/launchql/tree/main/packages/query-builder): **🏗️ SQL constructor** providing a robust TypeScript-based query builder for dynamic generation of `SELECT`, `INSERT`, `UPDATE`, `DELETE`, and stored procedure calls—supports advanced SQL features like `JOIN`, `GROUP BY`, and schema-qualified queries.
|
|
180
|
+
* [@launchql/query](https://github.com/launchql/launchql/tree/main/packages/query): **🧩 Fluent GraphQL builder** for PostGraphile schemas. ⚡ Schema-aware via introspection, 🧩 composable and ergonomic for building deeply nested queries.
|
|
181
|
+
|
|
182
|
+
## Disclaimer
|
|
183
|
+
|
|
184
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
185
|
+
|
|
186
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
|
187
|
+
|
package/dist/README.md
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# pg-cache
|
|
2
|
+
|
|
3
|
+
<p align="center" width="100%">
|
|
4
|
+
<img height="250" src="https://raw.githubusercontent.com/launchql/launchql/refs/heads/main/assets/outline-logo.svg" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center" width="100%">
|
|
8
|
+
<a href="https://github.com/launchql/launchql/actions/workflows/run-tests.yaml">
|
|
9
|
+
<img height="20" src="https://github.com/launchql/launchql/actions/workflows/run-tests.yaml/badge.svg" />
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/launchql/launchql/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a>
|
|
12
|
+
<a href="https://www.npmjs.com/package/@launchql/server-utils"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/launchql?filename=packages%2Fserver-utils%2Fpackage.json"/></a>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
PostgreSQL connection pool LRU cache manager with zero PostGraphile dependencies.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install pg-cache
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- LRU cache for PostgreSQL connection pools
|
|
26
|
+
- Automatic pool cleanup and disposal
|
|
27
|
+
- Extensible cleanup callback system
|
|
28
|
+
- Service cache for general use
|
|
29
|
+
- Graceful shutdown handling
|
|
30
|
+
- TypeScript support
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
### Basic Pool Management
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { pgCache, getRootPgPool } from 'pg-cache';
|
|
38
|
+
|
|
39
|
+
// Get or create a cached pool
|
|
40
|
+
const pool = getRootPgPool({
|
|
41
|
+
host: 'localhost',
|
|
42
|
+
port: 5432,
|
|
43
|
+
database: 'mydb',
|
|
44
|
+
user: 'postgres',
|
|
45
|
+
password: 'password'
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// Use the pool
|
|
49
|
+
const result = await pool.query('SELECT NOW()');
|
|
50
|
+
|
|
51
|
+
// Pool is automatically cached and reused
|
|
52
|
+
const samePool = getRootPgPool({ database: 'mydb' }); // Returns cached pool
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Direct Cache Access
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { pgCache } from 'pg-cache';
|
|
59
|
+
import { Pool } from 'pg';
|
|
60
|
+
|
|
61
|
+
// Create and cache a pool manually
|
|
62
|
+
const pool = new Pool({ connectionString: 'postgres://...' });
|
|
63
|
+
pgCache.set('my-pool-key', pool);
|
|
64
|
+
|
|
65
|
+
// Retrieve it later
|
|
66
|
+
const cachedPool = pgCache.get('my-pool-key');
|
|
67
|
+
|
|
68
|
+
// Remove from cache (also disposes the pool)
|
|
69
|
+
pgCache.delete('my-pool-key');
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Cleanup Callbacks
|
|
73
|
+
|
|
74
|
+
Register callbacks to be notified when pools are disposed:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { pgCache } from 'pg-cache';
|
|
78
|
+
|
|
79
|
+
// Register a cleanup callback
|
|
80
|
+
const unregister = pgCache.registerCleanupCallback((poolKey: string) => {
|
|
81
|
+
console.log(`Pool ${poolKey} was disposed`);
|
|
82
|
+
// Clean up any resources associated with this pool
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// Later, unregister if needed
|
|
86
|
+
unregister();
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Service Cache
|
|
90
|
+
|
|
91
|
+
A general-purpose cache is also provided:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { svcCache } from 'pg-cache';
|
|
95
|
+
|
|
96
|
+
// Cache any service or object
|
|
97
|
+
svcCache.set('my-service', myServiceInstance);
|
|
98
|
+
const service = svcCache.get('my-service');
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Graceful Shutdown
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { close, teardownPgPools } from 'pg-cache';
|
|
105
|
+
|
|
106
|
+
// In your shutdown handler
|
|
107
|
+
process.on('SIGTERM', async () => {
|
|
108
|
+
await close(); // or teardownPgPools()
|
|
109
|
+
process.exit(0);
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## API Reference
|
|
114
|
+
|
|
115
|
+
### pgCache
|
|
116
|
+
|
|
117
|
+
The main PostgreSQL pool cache instance.
|
|
118
|
+
|
|
119
|
+
- `get(key: string): Pool | undefined` - Get a cached pool
|
|
120
|
+
- `set(key: string, pool: Pool): void` - Cache a pool
|
|
121
|
+
- `has(key: string): boolean` - Check if a pool is cached
|
|
122
|
+
- `delete(key: string): void` - Remove and dispose a pool
|
|
123
|
+
- `clear(): void` - Remove and dispose all pools
|
|
124
|
+
- `registerCleanupCallback(callback: (key: string) => void): () => void` - Register a cleanup callback
|
|
125
|
+
|
|
126
|
+
### getRootPgPool(config: Partial<PgConfig>): Pool
|
|
127
|
+
|
|
128
|
+
Get or create a cached PostgreSQL pool using the provided configuration.
|
|
129
|
+
|
|
130
|
+
### svcCache
|
|
131
|
+
|
|
132
|
+
A general-purpose LRU cache for services and objects.
|
|
133
|
+
|
|
134
|
+
### close() / teardownPgPools()
|
|
135
|
+
|
|
136
|
+
Gracefully close all cached pools and wait for disposal.
|
|
137
|
+
|
|
138
|
+
## Integration with Other Packages
|
|
139
|
+
|
|
140
|
+
This package is designed to be extended. For example, `@launchql/graphile-cache` uses the cleanup callback system to automatically clean up PostGraphile instances when their associated pools are disposed.
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
## Related LaunchQL Tooling
|
|
144
|
+
|
|
145
|
+
### 🧪 Testing
|
|
146
|
+
|
|
147
|
+
* [launchql/pgsql-test](https://github.com/launchql/launchql/tree/main/packages/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
|
|
148
|
+
* [launchql/graphile-test](https://github.com/launchql/launchql/tree/main/packages/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
|
|
149
|
+
* [launchql/pg-query-context](https://github.com/launchql/launchql/tree/main/packages/pg-query-context): **🔒 Session context injection** to add session-local context (e.g., `SET LOCAL`) into queries—ideal for setting `role`, `jwt.claims`, and other session settings.
|
|
150
|
+
|
|
151
|
+
### 🧠 Parsing & AST
|
|
152
|
+
|
|
153
|
+
* [launchql/pgsql-parser](https://github.com/launchql/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
|
|
154
|
+
* [launchql/libpg-query-node](https://github.com/launchql/libpg-query-node): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
|
|
155
|
+
* [launchql/pg-proto-parser](https://github.com/launchql/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
156
|
+
* [@pgsql/enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
|
|
157
|
+
* [@pgsql/types](https://github.com/launchql/pgsql-parser/tree/main/packages/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
|
|
158
|
+
* [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
|
|
159
|
+
* [launchql/pg-ast](https://github.com/launchql/launchql/tree/main/packages/pg-ast): **🔍 Low-level AST tools** and transformations for Postgres query structures.
|
|
160
|
+
|
|
161
|
+
### 🚀 API & Dev Tools
|
|
162
|
+
|
|
163
|
+
* [launchql/server](https://github.com/launchql/launchql/tree/main/packages/server): **⚡ Express-based API server** powered by PostGraphile to expose a secure, scalable GraphQL API over your Postgres database.
|
|
164
|
+
* [launchql/explorer](https://github.com/launchql/launchql/tree/main/packages/explorer): **🔎 Visual API explorer** with GraphiQL for browsing across all databases and schemas—useful for debugging, documentation, and API prototyping.
|
|
165
|
+
|
|
166
|
+
### 🔁 Streaming & Uploads
|
|
167
|
+
|
|
168
|
+
* [launchql/s3-streamer](https://github.com/launchql/launchql/tree/main/packages/s3-streamer): **📤 Direct S3 streaming** for large files with support for metadata injection and content validation.
|
|
169
|
+
* [launchql/etag-hash](https://github.com/launchql/launchql/tree/main/packages/etag-hash): **🏷️ S3-compatible ETags** created by streaming and hashing file uploads in chunks.
|
|
170
|
+
* [launchql/etag-stream](https://github.com/launchql/launchql/tree/main/packages/etag-stream): **🔄 ETag computation** via Node stream transformer during upload or transfer.
|
|
171
|
+
* [launchql/uuid-hash](https://github.com/launchql/launchql/tree/main/packages/uuid-hash): **🆔 Deterministic UUIDs** generated from hashed content, great for deduplication and asset referencing.
|
|
172
|
+
* [launchql/uuid-stream](https://github.com/launchql/launchql/tree/main/packages/uuid-stream): **🌊 Streaming UUID generation** based on piped file content—ideal for upload pipelines.
|
|
173
|
+
* [launchql/upload-names](https://github.com/launchql/launchql/tree/main/packages/upload-names): **📂 Collision-resistant filenames** utility for structured and unique file names for uploads.
|
|
174
|
+
|
|
175
|
+
### 🧰 CLI & Codegen
|
|
176
|
+
|
|
177
|
+
* [@launchql/cli](https://github.com/launchql/launchql/tree/main/packages/cli): **🖥️ Command-line toolkit** for managing LaunchQL projects—supports database scaffolding, migrations, seeding, code generation, and automation.
|
|
178
|
+
* [launchql/launchql-gen](https://github.com/launchql/launchql/tree/main/packages/launchql-gen): **✨ Auto-generated GraphQL** mutations and queries dynamically built from introspected schema data.
|
|
179
|
+
* [@launchql/query-builder](https://github.com/launchql/launchql/tree/main/packages/query-builder): **🏗️ SQL constructor** providing a robust TypeScript-based query builder for dynamic generation of `SELECT`, `INSERT`, `UPDATE`, `DELETE`, and stored procedure calls—supports advanced SQL features like `JOIN`, `GROUP BY`, and schema-qualified queries.
|
|
180
|
+
* [@launchql/query](https://github.com/launchql/launchql/tree/main/packages/query): **🧩 Fluent GraphQL builder** for PostGraphile schemas. ⚡ Schema-aware via introspection, 🧩 composable and ergonomic for building deeply nested queries.
|
|
181
|
+
|
|
182
|
+
## Disclaimer
|
|
183
|
+
|
|
184
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
185
|
+
|
|
186
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
|
187
|
+
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pg-cache",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
|
+
"description": "PostgreSQL connection pool LRU cache manager",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/launchql/launchql",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/launchql/launchql"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/launchql/launchql/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"copy": "copyfiles -f ../../LICENSE README.md package.json dist",
|
|
24
|
+
"clean": "rimraf dist/**",
|
|
25
|
+
"prepare": "npm run build",
|
|
26
|
+
"build": "npm run clean; tsc; tsc -p tsconfig.esm.json; npm run copy",
|
|
27
|
+
"build:dev": "npm run clean; tsc --declarationMap; tsc -p tsconfig.esm.json; npm run copy",
|
|
28
|
+
"lint": "eslint . --fix",
|
|
29
|
+
"test": "jest",
|
|
30
|
+
"test:watch": "jest --watch"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@launchql/logger": "^1.0.0",
|
|
34
|
+
"@launchql/types": "^2.1.12",
|
|
35
|
+
"lru-cache": "^11.1.0",
|
|
36
|
+
"pg-env": "^1.0.0",
|
|
37
|
+
"pg": "^8.16.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/pg": "^8.15.2"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"postgresql",
|
|
44
|
+
"pg",
|
|
45
|
+
"pool",
|
|
46
|
+
"cache",
|
|
47
|
+
"lru",
|
|
48
|
+
"connection",
|
|
49
|
+
"launchql"
|
|
50
|
+
]
|
|
51
|
+
}
|
package/esm/index.js
ADDED
package/esm/lru.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { LRUCache } from 'lru-cache';
|
|
2
|
+
import { Logger } from '@launchql/logger';
|
|
3
|
+
const log = new Logger('pg-cache');
|
|
4
|
+
const ONE_HOUR_IN_MS = 1000 * 60 * 60;
|
|
5
|
+
const ONE_DAY = ONE_HOUR_IN_MS * 24;
|
|
6
|
+
const ONE_YEAR = ONE_DAY * 366;
|
|
7
|
+
// Kubernetes sends only SIGTERM on pod shutdown
|
|
8
|
+
const SYS_EVENTS = ['SIGTERM'];
|
|
9
|
+
class ManagedPgPool {
|
|
10
|
+
pool;
|
|
11
|
+
key;
|
|
12
|
+
isDisposed = false;
|
|
13
|
+
disposePromise = null;
|
|
14
|
+
constructor(pool, key) {
|
|
15
|
+
this.pool = pool;
|
|
16
|
+
this.key = key;
|
|
17
|
+
}
|
|
18
|
+
async dispose() {
|
|
19
|
+
if (this.isDisposed)
|
|
20
|
+
return this.disposePromise;
|
|
21
|
+
this.isDisposed = true;
|
|
22
|
+
this.disposePromise = (async () => {
|
|
23
|
+
try {
|
|
24
|
+
if (!this.pool.ended) {
|
|
25
|
+
await this.pool.end();
|
|
26
|
+
log.success(`pg.Pool ${this.key} ended.`);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
log.info(`pg.Pool ${this.key} already ended.`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
log.error(`Error ending pg.Pool ${this.key}: ${err.message}`);
|
|
34
|
+
throw err;
|
|
35
|
+
}
|
|
36
|
+
})();
|
|
37
|
+
return this.disposePromise;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export class PgPoolCacheManager {
|
|
41
|
+
cleanupTasks = [];
|
|
42
|
+
closed = false;
|
|
43
|
+
cleanupCallbacks = new Set();
|
|
44
|
+
pgCache = new LRUCache({
|
|
45
|
+
max: 10,
|
|
46
|
+
ttl: ONE_YEAR,
|
|
47
|
+
updateAgeOnGet: true,
|
|
48
|
+
dispose: (managedPool, key, reason) => {
|
|
49
|
+
log.debug(`Disposing pg pool [${key}] (${reason})`);
|
|
50
|
+
this.notifyCleanup(key);
|
|
51
|
+
this.disposePool(managedPool);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
// Register a cleanup callback to be called when pools are disposed
|
|
55
|
+
registerCleanupCallback(callback) {
|
|
56
|
+
this.cleanupCallbacks.add(callback);
|
|
57
|
+
// Return unregister function
|
|
58
|
+
return () => {
|
|
59
|
+
this.cleanupCallbacks.delete(callback);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
get(key) {
|
|
63
|
+
return this.pgCache.get(key)?.pool;
|
|
64
|
+
}
|
|
65
|
+
has(key) {
|
|
66
|
+
return this.pgCache.has(key);
|
|
67
|
+
}
|
|
68
|
+
set(key, pool) {
|
|
69
|
+
if (this.closed)
|
|
70
|
+
throw new Error('Cannot add to cache after it has been closed');
|
|
71
|
+
this.pgCache.set(key, new ManagedPgPool(pool, key));
|
|
72
|
+
}
|
|
73
|
+
delete(key) {
|
|
74
|
+
const managedPool = this.pgCache.get(key);
|
|
75
|
+
const existed = this.pgCache.delete(key);
|
|
76
|
+
if (!existed && managedPool) {
|
|
77
|
+
this.notifyCleanup(key);
|
|
78
|
+
this.disposePool(managedPool);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
clear() {
|
|
82
|
+
const entries = [...this.pgCache.entries()];
|
|
83
|
+
this.pgCache.clear();
|
|
84
|
+
for (const [key, managedPool] of entries) {
|
|
85
|
+
this.notifyCleanup(key);
|
|
86
|
+
this.disposePool(managedPool);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async close() {
|
|
90
|
+
if (this.closed)
|
|
91
|
+
return;
|
|
92
|
+
this.closed = true;
|
|
93
|
+
this.clear();
|
|
94
|
+
await this.waitForDisposals();
|
|
95
|
+
}
|
|
96
|
+
async waitForDisposals() {
|
|
97
|
+
if (this.cleanupTasks.length === 0)
|
|
98
|
+
return;
|
|
99
|
+
const tasks = [...this.cleanupTasks];
|
|
100
|
+
this.cleanupTasks = [];
|
|
101
|
+
await Promise.allSettled(tasks);
|
|
102
|
+
}
|
|
103
|
+
notifyCleanup(pgPoolKey) {
|
|
104
|
+
this.cleanupCallbacks.forEach(callback => {
|
|
105
|
+
try {
|
|
106
|
+
callback(pgPoolKey);
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
log.error(`Error in cleanup callback for pool ${pgPoolKey}: ${err.message}`);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
disposePool(managedPool) {
|
|
114
|
+
if (managedPool.isDisposed)
|
|
115
|
+
return;
|
|
116
|
+
const task = managedPool.dispose();
|
|
117
|
+
this.cleanupTasks.push(task);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// Create the singleton instance
|
|
121
|
+
export const pgCache = new PgPoolCacheManager();
|
|
122
|
+
// --- Graceful Shutdown ---
|
|
123
|
+
const closePromise = { promise: null };
|
|
124
|
+
export const close = async (verbose = false) => {
|
|
125
|
+
if (closePromise.promise)
|
|
126
|
+
return closePromise.promise;
|
|
127
|
+
closePromise.promise = (async () => {
|
|
128
|
+
if (verbose)
|
|
129
|
+
log.info('Closing pg cache...');
|
|
130
|
+
await pgCache.close();
|
|
131
|
+
if (verbose)
|
|
132
|
+
log.success('PG cache disposed.');
|
|
133
|
+
})();
|
|
134
|
+
return closePromise.promise;
|
|
135
|
+
};
|
|
136
|
+
SYS_EVENTS.forEach(event => {
|
|
137
|
+
process.on(event, () => {
|
|
138
|
+
log.info(`Received ${event}`);
|
|
139
|
+
close();
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
export const teardownPgPools = async (verbose = false) => {
|
|
143
|
+
return close(verbose);
|
|
144
|
+
};
|
package/esm/pg.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import pg from 'pg';
|
|
2
|
+
import { pgCache } from './lru';
|
|
3
|
+
import { getPgEnvOptions } from 'pg-env';
|
|
4
|
+
const getDbString = (user, password, host, port, database) => `postgres://${user}:${password}@${host}:${port}/${database}`;
|
|
5
|
+
export const getRootPgPool = (pgConfig) => {
|
|
6
|
+
const config = getPgEnvOptions(pgConfig);
|
|
7
|
+
const { user, password, host, port, database, } = config;
|
|
8
|
+
if (pgCache.has(database)) {
|
|
9
|
+
const cached = pgCache.get(database);
|
|
10
|
+
if (cached)
|
|
11
|
+
return cached;
|
|
12
|
+
}
|
|
13
|
+
const connectionString = getDbString(user, password, host, port, database);
|
|
14
|
+
const pgPool = new pg.Pool({ connectionString });
|
|
15
|
+
pgCache.set(database, pgPool);
|
|
16
|
+
return pgPool;
|
|
17
|
+
};
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRootPgPool = exports.teardownPgPools = exports.PgPoolCacheManager = exports.pgCache = void 0;
|
|
4
|
+
// Main exports from pg-cache package
|
|
5
|
+
var lru_1 = require("./lru");
|
|
6
|
+
Object.defineProperty(exports, "pgCache", { enumerable: true, get: function () { return lru_1.pgCache; } });
|
|
7
|
+
Object.defineProperty(exports, "PgPoolCacheManager", { enumerable: true, get: function () { return lru_1.PgPoolCacheManager; } });
|
|
8
|
+
Object.defineProperty(exports, "close", { enumerable: true, get: function () { return lru_1.close; } });
|
|
9
|
+
Object.defineProperty(exports, "teardownPgPools", { enumerable: true, get: function () { return lru_1.teardownPgPools; } });
|
|
10
|
+
var pg_1 = require("./pg");
|
|
11
|
+
Object.defineProperty(exports, "getRootPgPool", { enumerable: true, get: function () { return pg_1.getRootPgPool; } });
|
package/lru.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import pg from 'pg';
|
|
2
|
+
type PgPoolKey = string;
|
|
3
|
+
export type PoolCleanupCallback = (pgPoolKey: string) => void;
|
|
4
|
+
export declare class PgPoolCacheManager {
|
|
5
|
+
private cleanupTasks;
|
|
6
|
+
private closed;
|
|
7
|
+
private cleanupCallbacks;
|
|
8
|
+
private readonly pgCache;
|
|
9
|
+
registerCleanupCallback(callback: PoolCleanupCallback): () => void;
|
|
10
|
+
get(key: PgPoolKey): pg.Pool | undefined;
|
|
11
|
+
has(key: PgPoolKey): boolean;
|
|
12
|
+
set(key: PgPoolKey, pool: pg.Pool): void;
|
|
13
|
+
delete(key: PgPoolKey): void;
|
|
14
|
+
clear(): void;
|
|
15
|
+
close(): Promise<void>;
|
|
16
|
+
waitForDisposals(): Promise<void>;
|
|
17
|
+
private notifyCleanup;
|
|
18
|
+
private disposePool;
|
|
19
|
+
}
|
|
20
|
+
export declare const pgCache: PgPoolCacheManager;
|
|
21
|
+
export declare const close: (verbose?: boolean) => Promise<void>;
|
|
22
|
+
export declare const teardownPgPools: (verbose?: boolean) => Promise<void>;
|
|
23
|
+
export {};
|
package/lru.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.teardownPgPools = exports.close = exports.pgCache = exports.PgPoolCacheManager = void 0;
|
|
4
|
+
const lru_cache_1 = require("lru-cache");
|
|
5
|
+
const logger_1 = require("@launchql/logger");
|
|
6
|
+
const log = new logger_1.Logger('pg-cache');
|
|
7
|
+
const ONE_HOUR_IN_MS = 1000 * 60 * 60;
|
|
8
|
+
const ONE_DAY = ONE_HOUR_IN_MS * 24;
|
|
9
|
+
const ONE_YEAR = ONE_DAY * 366;
|
|
10
|
+
// Kubernetes sends only SIGTERM on pod shutdown
|
|
11
|
+
const SYS_EVENTS = ['SIGTERM'];
|
|
12
|
+
class ManagedPgPool {
|
|
13
|
+
pool;
|
|
14
|
+
key;
|
|
15
|
+
isDisposed = false;
|
|
16
|
+
disposePromise = null;
|
|
17
|
+
constructor(pool, key) {
|
|
18
|
+
this.pool = pool;
|
|
19
|
+
this.key = key;
|
|
20
|
+
}
|
|
21
|
+
async dispose() {
|
|
22
|
+
if (this.isDisposed)
|
|
23
|
+
return this.disposePromise;
|
|
24
|
+
this.isDisposed = true;
|
|
25
|
+
this.disposePromise = (async () => {
|
|
26
|
+
try {
|
|
27
|
+
if (!this.pool.ended) {
|
|
28
|
+
await this.pool.end();
|
|
29
|
+
log.success(`pg.Pool ${this.key} ended.`);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
log.info(`pg.Pool ${this.key} already ended.`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
log.error(`Error ending pg.Pool ${this.key}: ${err.message}`);
|
|
37
|
+
throw err;
|
|
38
|
+
}
|
|
39
|
+
})();
|
|
40
|
+
return this.disposePromise;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
class PgPoolCacheManager {
|
|
44
|
+
cleanupTasks = [];
|
|
45
|
+
closed = false;
|
|
46
|
+
cleanupCallbacks = new Set();
|
|
47
|
+
pgCache = new lru_cache_1.LRUCache({
|
|
48
|
+
max: 10,
|
|
49
|
+
ttl: ONE_YEAR,
|
|
50
|
+
updateAgeOnGet: true,
|
|
51
|
+
dispose: (managedPool, key, reason) => {
|
|
52
|
+
log.debug(`Disposing pg pool [${key}] (${reason})`);
|
|
53
|
+
this.notifyCleanup(key);
|
|
54
|
+
this.disposePool(managedPool);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
// Register a cleanup callback to be called when pools are disposed
|
|
58
|
+
registerCleanupCallback(callback) {
|
|
59
|
+
this.cleanupCallbacks.add(callback);
|
|
60
|
+
// Return unregister function
|
|
61
|
+
return () => {
|
|
62
|
+
this.cleanupCallbacks.delete(callback);
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
get(key) {
|
|
66
|
+
return this.pgCache.get(key)?.pool;
|
|
67
|
+
}
|
|
68
|
+
has(key) {
|
|
69
|
+
return this.pgCache.has(key);
|
|
70
|
+
}
|
|
71
|
+
set(key, pool) {
|
|
72
|
+
if (this.closed)
|
|
73
|
+
throw new Error('Cannot add to cache after it has been closed');
|
|
74
|
+
this.pgCache.set(key, new ManagedPgPool(pool, key));
|
|
75
|
+
}
|
|
76
|
+
delete(key) {
|
|
77
|
+
const managedPool = this.pgCache.get(key);
|
|
78
|
+
const existed = this.pgCache.delete(key);
|
|
79
|
+
if (!existed && managedPool) {
|
|
80
|
+
this.notifyCleanup(key);
|
|
81
|
+
this.disposePool(managedPool);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
clear() {
|
|
85
|
+
const entries = [...this.pgCache.entries()];
|
|
86
|
+
this.pgCache.clear();
|
|
87
|
+
for (const [key, managedPool] of entries) {
|
|
88
|
+
this.notifyCleanup(key);
|
|
89
|
+
this.disposePool(managedPool);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async close() {
|
|
93
|
+
if (this.closed)
|
|
94
|
+
return;
|
|
95
|
+
this.closed = true;
|
|
96
|
+
this.clear();
|
|
97
|
+
await this.waitForDisposals();
|
|
98
|
+
}
|
|
99
|
+
async waitForDisposals() {
|
|
100
|
+
if (this.cleanupTasks.length === 0)
|
|
101
|
+
return;
|
|
102
|
+
const tasks = [...this.cleanupTasks];
|
|
103
|
+
this.cleanupTasks = [];
|
|
104
|
+
await Promise.allSettled(tasks);
|
|
105
|
+
}
|
|
106
|
+
notifyCleanup(pgPoolKey) {
|
|
107
|
+
this.cleanupCallbacks.forEach(callback => {
|
|
108
|
+
try {
|
|
109
|
+
callback(pgPoolKey);
|
|
110
|
+
}
|
|
111
|
+
catch (err) {
|
|
112
|
+
log.error(`Error in cleanup callback for pool ${pgPoolKey}: ${err.message}`);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
disposePool(managedPool) {
|
|
117
|
+
if (managedPool.isDisposed)
|
|
118
|
+
return;
|
|
119
|
+
const task = managedPool.dispose();
|
|
120
|
+
this.cleanupTasks.push(task);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
exports.PgPoolCacheManager = PgPoolCacheManager;
|
|
124
|
+
// Create the singleton instance
|
|
125
|
+
exports.pgCache = new PgPoolCacheManager();
|
|
126
|
+
// --- Graceful Shutdown ---
|
|
127
|
+
const closePromise = { promise: null };
|
|
128
|
+
const close = async (verbose = false) => {
|
|
129
|
+
if (closePromise.promise)
|
|
130
|
+
return closePromise.promise;
|
|
131
|
+
closePromise.promise = (async () => {
|
|
132
|
+
if (verbose)
|
|
133
|
+
log.info('Closing pg cache...');
|
|
134
|
+
await exports.pgCache.close();
|
|
135
|
+
if (verbose)
|
|
136
|
+
log.success('PG cache disposed.');
|
|
137
|
+
})();
|
|
138
|
+
return closePromise.promise;
|
|
139
|
+
};
|
|
140
|
+
exports.close = close;
|
|
141
|
+
SYS_EVENTS.forEach(event => {
|
|
142
|
+
process.on(event, () => {
|
|
143
|
+
log.info(`Received ${event}`);
|
|
144
|
+
(0, exports.close)();
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
const teardownPgPools = async (verbose = false) => {
|
|
148
|
+
return (0, exports.close)(verbose);
|
|
149
|
+
};
|
|
150
|
+
exports.teardownPgPools = teardownPgPools;
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pg-cache",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
|
+
"description": "PostgreSQL connection pool LRU cache manager",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/launchql/launchql",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/launchql/launchql"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/launchql/launchql/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"copy": "copyfiles -f ../../LICENSE README.md package.json dist",
|
|
24
|
+
"clean": "rimraf dist/**",
|
|
25
|
+
"prepare": "npm run build",
|
|
26
|
+
"build": "npm run clean; tsc; tsc -p tsconfig.esm.json; npm run copy",
|
|
27
|
+
"build:dev": "npm run clean; tsc --declarationMap; tsc -p tsconfig.esm.json; npm run copy",
|
|
28
|
+
"lint": "eslint . --fix",
|
|
29
|
+
"test": "jest",
|
|
30
|
+
"test:watch": "jest --watch"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@launchql/logger": "^1.0.0",
|
|
34
|
+
"@launchql/types": "^2.1.12",
|
|
35
|
+
"lru-cache": "^11.1.0",
|
|
36
|
+
"pg-env": "^1.0.0",
|
|
37
|
+
"pg": "^8.16.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/pg": "^8.15.2"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"postgresql",
|
|
44
|
+
"pg",
|
|
45
|
+
"pool",
|
|
46
|
+
"cache",
|
|
47
|
+
"lru",
|
|
48
|
+
"connection",
|
|
49
|
+
"launchql"
|
|
50
|
+
]
|
|
51
|
+
}
|
package/pg.d.ts
ADDED
package/pg.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getRootPgPool = void 0;
|
|
7
|
+
const pg_1 = __importDefault(require("pg"));
|
|
8
|
+
const lru_1 = require("./lru");
|
|
9
|
+
const pg_env_1 = require("pg-env");
|
|
10
|
+
const getDbString = (user, password, host, port, database) => `postgres://${user}:${password}@${host}:${port}/${database}`;
|
|
11
|
+
const getRootPgPool = (pgConfig) => {
|
|
12
|
+
const config = (0, pg_env_1.getPgEnvOptions)(pgConfig);
|
|
13
|
+
const { user, password, host, port, database, } = config;
|
|
14
|
+
if (lru_1.pgCache.has(database)) {
|
|
15
|
+
const cached = lru_1.pgCache.get(database);
|
|
16
|
+
if (cached)
|
|
17
|
+
return cached;
|
|
18
|
+
}
|
|
19
|
+
const connectionString = getDbString(user, password, host, port, database);
|
|
20
|
+
const pgPool = new pg_1.default.Pool({ connectionString });
|
|
21
|
+
lru_1.pgCache.set(database, pgPool);
|
|
22
|
+
return pgPool;
|
|
23
|
+
};
|
|
24
|
+
exports.getRootPgPool = getRootPgPool;
|