graphile-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 +226 -0
- package/dist/README.md +226 -0
- package/dist/package.json +53 -0
- package/esm/graphile-cache.js +41 -0
- package/esm/index.js +2 -0
- package/graphile-cache.d.ts +10 -0
- package/graphile-cache.js +45 -0
- package/index.d.ts +1 -0
- package/index.js +7 -0
- package/package.json +53 -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,226 @@
|
|
|
1
|
+
# graphile-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/graphile-cache"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/launchql?filename=packages%2Fgraphile-cache%2Fpackage.json"/></a>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
PostGraphile instance LRU cache with automatic cleanup when PostgreSQL pools are disposed.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install graphile-cache pg-cache
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Note: This package depends on `pg-cache` for the PostgreSQL pool management.
|
|
25
|
+
|
|
26
|
+
## Features
|
|
27
|
+
|
|
28
|
+
- LRU cache for PostGraphile instances
|
|
29
|
+
- Automatic cleanup when associated PostgreSQL pools are disposed
|
|
30
|
+
- Integrates seamlessly with `pg-cache`
|
|
31
|
+
- Service cache re-exported for convenience
|
|
32
|
+
- TypeScript support
|
|
33
|
+
|
|
34
|
+
## How It Works
|
|
35
|
+
|
|
36
|
+
When you import this package, it automatically registers a cleanup callback with `pg-cache`. When a PostgreSQL pool is disposed, any PostGraphile instances using that pool are automatically removed from the cache.
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
### Basic Usage
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { graphileCache, GraphileCache } from 'graphile-cache';
|
|
44
|
+
import { getRootPgPool } from 'pg-cache';
|
|
45
|
+
import { postgraphile } from 'postgraphile';
|
|
46
|
+
|
|
47
|
+
// Create a PostGraphile instance
|
|
48
|
+
const pgPool = getRootPgPool({ database: 'mydb' });
|
|
49
|
+
const handler = postgraphile(pgPool, 'public', {
|
|
50
|
+
// PostGraphile options
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Cache it
|
|
54
|
+
const cacheEntry: GraphileCache = {
|
|
55
|
+
pgPool,
|
|
56
|
+
pgPoolKey: 'mydb',
|
|
57
|
+
handler
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
graphileCache.set('mydb.public', cacheEntry);
|
|
61
|
+
|
|
62
|
+
// Retrieve it later
|
|
63
|
+
const cached = graphileCache.get('mydb.public');
|
|
64
|
+
if (cached) {
|
|
65
|
+
// Use cached.handler
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Automatic Cleanup
|
|
70
|
+
|
|
71
|
+
The cleanup happens automatically:
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import { pgCache } from 'pg-cache';
|
|
75
|
+
import { graphileCache } from 'graphile-cache';
|
|
76
|
+
|
|
77
|
+
// Add entries
|
|
78
|
+
graphileCache.set('mydb.public', { pgPoolKey: 'mydb', ... });
|
|
79
|
+
graphileCache.set('mydb.private', { pgPoolKey: 'mydb', ... });
|
|
80
|
+
|
|
81
|
+
// When the pool is removed...
|
|
82
|
+
pgCache.delete('mydb');
|
|
83
|
+
|
|
84
|
+
// Both graphile entries are automatically cleaned up!
|
|
85
|
+
console.log(graphileCache.has('mydb.public')); // false
|
|
86
|
+
console.log(graphileCache.has('mydb.private')); // false
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Complete Example
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { graphileCache, GraphileCache } from 'graphile-cache';
|
|
93
|
+
import { getRootPgPool } from 'pg-cache';
|
|
94
|
+
import { postgraphile } from 'postgraphile';
|
|
95
|
+
|
|
96
|
+
function getGraphileInstance(database: string, schema: string): GraphileCache {
|
|
97
|
+
const key = `${database}.${schema}`;
|
|
98
|
+
|
|
99
|
+
// Check cache first
|
|
100
|
+
const cached = graphileCache.get(key);
|
|
101
|
+
if (cached) {
|
|
102
|
+
return cached;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Create new instance
|
|
106
|
+
const pgPool = getRootPgPool({ database });
|
|
107
|
+
const handler = postgraphile(pgPool, schema, {
|
|
108
|
+
graphqlRoute: '/graphql',
|
|
109
|
+
graphiqlRoute: '/graphiql',
|
|
110
|
+
// other options...
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const entry: GraphileCache = {
|
|
114
|
+
pgPool,
|
|
115
|
+
pgPoolKey: database,
|
|
116
|
+
handler
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// Cache it
|
|
120
|
+
graphileCache.set(key, entry);
|
|
121
|
+
return entry;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Use in Express
|
|
125
|
+
app.use((req, res, next) => {
|
|
126
|
+
const { handler } = getGraphileInstance('mydb', 'public');
|
|
127
|
+
handler(req, res, next);
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Graceful Shutdown
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
import { closeAllCaches } from 'graphile-cache';
|
|
135
|
+
|
|
136
|
+
// This closes all caches including pg pools
|
|
137
|
+
process.on('SIGTERM', async () => {
|
|
138
|
+
await closeAllCaches();
|
|
139
|
+
process.exit(0);
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## API Reference
|
|
144
|
+
|
|
145
|
+
### graphileCache
|
|
146
|
+
|
|
147
|
+
The main PostGraphile instance cache.
|
|
148
|
+
|
|
149
|
+
- `get(key: string): GraphileCache | undefined` - Get a cached instance
|
|
150
|
+
- `set(key: string, value: GraphileCache): void` - Cache an instance
|
|
151
|
+
- `has(key: string): boolean` - Check if an instance is cached
|
|
152
|
+
- `delete(key: string): void` - Remove an instance
|
|
153
|
+
- `clear(): void` - Remove all instances
|
|
154
|
+
|
|
155
|
+
### GraphileCache Interface
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
interface GraphileCache {
|
|
159
|
+
pgPool: pg.Pool;
|
|
160
|
+
pgPoolKey: string;
|
|
161
|
+
handler: HttpRequestHandler;
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### closeAllCaches()
|
|
166
|
+
|
|
167
|
+
Closes all caches including the service cache, graphile cache, and all PostgreSQL pools.
|
|
168
|
+
|
|
169
|
+
### svcCache
|
|
170
|
+
|
|
171
|
+
Re-exported from `pg-cache` for convenience.
|
|
172
|
+
|
|
173
|
+
## Integration Details
|
|
174
|
+
|
|
175
|
+
The integration with `pg-cache` happens automatically when this module is imported. The cleanup callback is registered immediately, ensuring that PostGraphile instances are cleaned up whenever their associated PostgreSQL pools are disposed.
|
|
176
|
+
|
|
177
|
+
This design ensures:
|
|
178
|
+
- No memory leaks from orphaned PostGraphile instances
|
|
179
|
+
- Automatic cleanup without manual intervention
|
|
180
|
+
- Loose coupling between packages
|
|
181
|
+
|
|
182
|
+
## Related LaunchQL Tooling
|
|
183
|
+
|
|
184
|
+
### 🧪 Testing
|
|
185
|
+
|
|
186
|
+
* [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.
|
|
187
|
+
* [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.
|
|
188
|
+
* [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.
|
|
189
|
+
|
|
190
|
+
### 🧠 Parsing & AST
|
|
191
|
+
|
|
192
|
+
* [launchql/pgsql-parser](https://github.com/launchql/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
|
|
193
|
+
* [launchql/libpg-query-node](https://github.com/launchql/libpg-query-node): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
|
|
194
|
+
* [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.
|
|
195
|
+
* [@pgsql/enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
|
|
196
|
+
* [@pgsql/types](https://github.com/launchql/pgsql-parser/tree/main/packages/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
|
|
197
|
+
* [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
|
|
198
|
+
* [launchql/pg-ast](https://github.com/launchql/launchql/tree/main/packages/pg-ast): **🔍 Low-level AST tools** and transformations for Postgres query structures.
|
|
199
|
+
|
|
200
|
+
### 🚀 API & Dev Tools
|
|
201
|
+
|
|
202
|
+
* [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.
|
|
203
|
+
* [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.
|
|
204
|
+
|
|
205
|
+
### 🔁 Streaming & Uploads
|
|
206
|
+
|
|
207
|
+
* [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.
|
|
208
|
+
* [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.
|
|
209
|
+
* [launchql/etag-stream](https://github.com/launchql/launchql/tree/main/packages/etag-stream): **🔄 ETag computation** via Node stream transformer during upload or transfer.
|
|
210
|
+
* [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.
|
|
211
|
+
* [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.
|
|
212
|
+
* [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.
|
|
213
|
+
|
|
214
|
+
### 🧰 CLI & Codegen
|
|
215
|
+
|
|
216
|
+
* [@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.
|
|
217
|
+
* [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.
|
|
218
|
+
* [@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.
|
|
219
|
+
* [@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.
|
|
220
|
+
|
|
221
|
+
## Disclaimer
|
|
222
|
+
|
|
223
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
224
|
+
|
|
225
|
+
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.
|
|
226
|
+
|
package/dist/README.md
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# graphile-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/graphile-cache"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/launchql?filename=packages%2Fgraphile-cache%2Fpackage.json"/></a>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
PostGraphile instance LRU cache with automatic cleanup when PostgreSQL pools are disposed.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install graphile-cache pg-cache
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Note: This package depends on `pg-cache` for the PostgreSQL pool management.
|
|
25
|
+
|
|
26
|
+
## Features
|
|
27
|
+
|
|
28
|
+
- LRU cache for PostGraphile instances
|
|
29
|
+
- Automatic cleanup when associated PostgreSQL pools are disposed
|
|
30
|
+
- Integrates seamlessly with `pg-cache`
|
|
31
|
+
- Service cache re-exported for convenience
|
|
32
|
+
- TypeScript support
|
|
33
|
+
|
|
34
|
+
## How It Works
|
|
35
|
+
|
|
36
|
+
When you import this package, it automatically registers a cleanup callback with `pg-cache`. When a PostgreSQL pool is disposed, any PostGraphile instances using that pool are automatically removed from the cache.
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
### Basic Usage
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { graphileCache, GraphileCache } from 'graphile-cache';
|
|
44
|
+
import { getRootPgPool } from 'pg-cache';
|
|
45
|
+
import { postgraphile } from 'postgraphile';
|
|
46
|
+
|
|
47
|
+
// Create a PostGraphile instance
|
|
48
|
+
const pgPool = getRootPgPool({ database: 'mydb' });
|
|
49
|
+
const handler = postgraphile(pgPool, 'public', {
|
|
50
|
+
// PostGraphile options
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Cache it
|
|
54
|
+
const cacheEntry: GraphileCache = {
|
|
55
|
+
pgPool,
|
|
56
|
+
pgPoolKey: 'mydb',
|
|
57
|
+
handler
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
graphileCache.set('mydb.public', cacheEntry);
|
|
61
|
+
|
|
62
|
+
// Retrieve it later
|
|
63
|
+
const cached = graphileCache.get('mydb.public');
|
|
64
|
+
if (cached) {
|
|
65
|
+
// Use cached.handler
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Automatic Cleanup
|
|
70
|
+
|
|
71
|
+
The cleanup happens automatically:
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import { pgCache } from 'pg-cache';
|
|
75
|
+
import { graphileCache } from 'graphile-cache';
|
|
76
|
+
|
|
77
|
+
// Add entries
|
|
78
|
+
graphileCache.set('mydb.public', { pgPoolKey: 'mydb', ... });
|
|
79
|
+
graphileCache.set('mydb.private', { pgPoolKey: 'mydb', ... });
|
|
80
|
+
|
|
81
|
+
// When the pool is removed...
|
|
82
|
+
pgCache.delete('mydb');
|
|
83
|
+
|
|
84
|
+
// Both graphile entries are automatically cleaned up!
|
|
85
|
+
console.log(graphileCache.has('mydb.public')); // false
|
|
86
|
+
console.log(graphileCache.has('mydb.private')); // false
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Complete Example
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { graphileCache, GraphileCache } from 'graphile-cache';
|
|
93
|
+
import { getRootPgPool } from 'pg-cache';
|
|
94
|
+
import { postgraphile } from 'postgraphile';
|
|
95
|
+
|
|
96
|
+
function getGraphileInstance(database: string, schema: string): GraphileCache {
|
|
97
|
+
const key = `${database}.${schema}`;
|
|
98
|
+
|
|
99
|
+
// Check cache first
|
|
100
|
+
const cached = graphileCache.get(key);
|
|
101
|
+
if (cached) {
|
|
102
|
+
return cached;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Create new instance
|
|
106
|
+
const pgPool = getRootPgPool({ database });
|
|
107
|
+
const handler = postgraphile(pgPool, schema, {
|
|
108
|
+
graphqlRoute: '/graphql',
|
|
109
|
+
graphiqlRoute: '/graphiql',
|
|
110
|
+
// other options...
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const entry: GraphileCache = {
|
|
114
|
+
pgPool,
|
|
115
|
+
pgPoolKey: database,
|
|
116
|
+
handler
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// Cache it
|
|
120
|
+
graphileCache.set(key, entry);
|
|
121
|
+
return entry;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Use in Express
|
|
125
|
+
app.use((req, res, next) => {
|
|
126
|
+
const { handler } = getGraphileInstance('mydb', 'public');
|
|
127
|
+
handler(req, res, next);
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Graceful Shutdown
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
import { closeAllCaches } from 'graphile-cache';
|
|
135
|
+
|
|
136
|
+
// This closes all caches including pg pools
|
|
137
|
+
process.on('SIGTERM', async () => {
|
|
138
|
+
await closeAllCaches();
|
|
139
|
+
process.exit(0);
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## API Reference
|
|
144
|
+
|
|
145
|
+
### graphileCache
|
|
146
|
+
|
|
147
|
+
The main PostGraphile instance cache.
|
|
148
|
+
|
|
149
|
+
- `get(key: string): GraphileCache | undefined` - Get a cached instance
|
|
150
|
+
- `set(key: string, value: GraphileCache): void` - Cache an instance
|
|
151
|
+
- `has(key: string): boolean` - Check if an instance is cached
|
|
152
|
+
- `delete(key: string): void` - Remove an instance
|
|
153
|
+
- `clear(): void` - Remove all instances
|
|
154
|
+
|
|
155
|
+
### GraphileCache Interface
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
interface GraphileCache {
|
|
159
|
+
pgPool: pg.Pool;
|
|
160
|
+
pgPoolKey: string;
|
|
161
|
+
handler: HttpRequestHandler;
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### closeAllCaches()
|
|
166
|
+
|
|
167
|
+
Closes all caches including the service cache, graphile cache, and all PostgreSQL pools.
|
|
168
|
+
|
|
169
|
+
### svcCache
|
|
170
|
+
|
|
171
|
+
Re-exported from `pg-cache` for convenience.
|
|
172
|
+
|
|
173
|
+
## Integration Details
|
|
174
|
+
|
|
175
|
+
The integration with `pg-cache` happens automatically when this module is imported. The cleanup callback is registered immediately, ensuring that PostGraphile instances are cleaned up whenever their associated PostgreSQL pools are disposed.
|
|
176
|
+
|
|
177
|
+
This design ensures:
|
|
178
|
+
- No memory leaks from orphaned PostGraphile instances
|
|
179
|
+
- Automatic cleanup without manual intervention
|
|
180
|
+
- Loose coupling between packages
|
|
181
|
+
|
|
182
|
+
## Related LaunchQL Tooling
|
|
183
|
+
|
|
184
|
+
### 🧪 Testing
|
|
185
|
+
|
|
186
|
+
* [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.
|
|
187
|
+
* [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.
|
|
188
|
+
* [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.
|
|
189
|
+
|
|
190
|
+
### 🧠 Parsing & AST
|
|
191
|
+
|
|
192
|
+
* [launchql/pgsql-parser](https://github.com/launchql/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
|
|
193
|
+
* [launchql/libpg-query-node](https://github.com/launchql/libpg-query-node): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
|
|
194
|
+
* [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.
|
|
195
|
+
* [@pgsql/enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
|
|
196
|
+
* [@pgsql/types](https://github.com/launchql/pgsql-parser/tree/main/packages/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
|
|
197
|
+
* [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
|
|
198
|
+
* [launchql/pg-ast](https://github.com/launchql/launchql/tree/main/packages/pg-ast): **🔍 Low-level AST tools** and transformations for Postgres query structures.
|
|
199
|
+
|
|
200
|
+
### 🚀 API & Dev Tools
|
|
201
|
+
|
|
202
|
+
* [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.
|
|
203
|
+
* [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.
|
|
204
|
+
|
|
205
|
+
### 🔁 Streaming & Uploads
|
|
206
|
+
|
|
207
|
+
* [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.
|
|
208
|
+
* [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.
|
|
209
|
+
* [launchql/etag-stream](https://github.com/launchql/launchql/tree/main/packages/etag-stream): **🔄 ETag computation** via Node stream transformer during upload or transfer.
|
|
210
|
+
* [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.
|
|
211
|
+
* [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.
|
|
212
|
+
* [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.
|
|
213
|
+
|
|
214
|
+
### 🧰 CLI & Codegen
|
|
215
|
+
|
|
216
|
+
* [@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.
|
|
217
|
+
* [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.
|
|
218
|
+
* [@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.
|
|
219
|
+
* [@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.
|
|
220
|
+
|
|
221
|
+
## Disclaimer
|
|
222
|
+
|
|
223
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
224
|
+
|
|
225
|
+
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.
|
|
226
|
+
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "graphile-cache",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
|
+
"description": "PostGraphile LRU cache with automatic pool cleanup integration",
|
|
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
|
+
"pg-cache": "^1.0.0",
|
|
35
|
+
"lru-cache": "^11.1.0",
|
|
36
|
+
"pg": "^8.16.0",
|
|
37
|
+
"postgraphile": "^4.14.1"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/pg": "^8.15.2",
|
|
41
|
+
"@types/rimraf": "^4.0.5",
|
|
42
|
+
"nodemon": "^3.1.10",
|
|
43
|
+
"ts-node": "^10.9.2"
|
|
44
|
+
},
|
|
45
|
+
"keywords": [
|
|
46
|
+
"postgraphile",
|
|
47
|
+
"graphile",
|
|
48
|
+
"cache",
|
|
49
|
+
"lru",
|
|
50
|
+
"postgresql",
|
|
51
|
+
"launchql"
|
|
52
|
+
]
|
|
53
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { LRUCache } from 'lru-cache';
|
|
2
|
+
import { Logger } from '@launchql/logger';
|
|
3
|
+
import { pgCache } from 'pg-cache';
|
|
4
|
+
const log = new Logger('graphile-cache');
|
|
5
|
+
const ONE_HOUR_IN_MS = 1000 * 60 * 60;
|
|
6
|
+
const ONE_DAY = ONE_HOUR_IN_MS * 24;
|
|
7
|
+
const ONE_YEAR = ONE_DAY * 366;
|
|
8
|
+
// --- Graphile Cache ---
|
|
9
|
+
export const graphileCache = new LRUCache({
|
|
10
|
+
max: 15,
|
|
11
|
+
ttl: ONE_YEAR,
|
|
12
|
+
updateAgeOnGet: true,
|
|
13
|
+
dispose: (_, key) => {
|
|
14
|
+
log.debug(`Disposing PostGraphile[${key}]`);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
// Register cleanup callback with pgCache
|
|
18
|
+
// When a pg pool is disposed, clean up any graphile instances using it
|
|
19
|
+
const unregister = pgCache.registerCleanupCallback((pgPoolKey) => {
|
|
20
|
+
graphileCache.forEach((entry, k) => {
|
|
21
|
+
if (entry.pgPoolKey === pgPoolKey) {
|
|
22
|
+
log.debug(`Removing graphileCache[${k}] due to pgPool[${pgPoolKey}]`);
|
|
23
|
+
graphileCache.delete(k);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
// Enhanced close function that handles all caches
|
|
28
|
+
const closePromise = { promise: null };
|
|
29
|
+
export const closeAllCaches = async (verbose = false) => {
|
|
30
|
+
if (closePromise.promise)
|
|
31
|
+
return closePromise.promise;
|
|
32
|
+
closePromise.promise = (async () => {
|
|
33
|
+
if (verbose)
|
|
34
|
+
log.info('Closing all server caches...');
|
|
35
|
+
graphileCache.clear();
|
|
36
|
+
await pgCache.close();
|
|
37
|
+
if (verbose)
|
|
38
|
+
log.success('All caches disposed.');
|
|
39
|
+
})();
|
|
40
|
+
return closePromise.promise;
|
|
41
|
+
};
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { LRUCache } from 'lru-cache';
|
|
2
|
+
import pg from 'pg';
|
|
3
|
+
import { HttpRequestHandler } from 'postgraphile';
|
|
4
|
+
export interface GraphileCache {
|
|
5
|
+
pgPool: pg.Pool;
|
|
6
|
+
pgPoolKey: string;
|
|
7
|
+
handler: HttpRequestHandler;
|
|
8
|
+
}
|
|
9
|
+
export declare const graphileCache: LRUCache<string, GraphileCache, unknown>;
|
|
10
|
+
export declare const closeAllCaches: (verbose?: boolean) => Promise<void>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.closeAllCaches = exports.graphileCache = void 0;
|
|
4
|
+
const lru_cache_1 = require("lru-cache");
|
|
5
|
+
const logger_1 = require("@launchql/logger");
|
|
6
|
+
const pg_cache_1 = require("pg-cache");
|
|
7
|
+
const log = new logger_1.Logger('graphile-cache');
|
|
8
|
+
const ONE_HOUR_IN_MS = 1000 * 60 * 60;
|
|
9
|
+
const ONE_DAY = ONE_HOUR_IN_MS * 24;
|
|
10
|
+
const ONE_YEAR = ONE_DAY * 366;
|
|
11
|
+
// --- Graphile Cache ---
|
|
12
|
+
exports.graphileCache = new lru_cache_1.LRUCache({
|
|
13
|
+
max: 15,
|
|
14
|
+
ttl: ONE_YEAR,
|
|
15
|
+
updateAgeOnGet: true,
|
|
16
|
+
dispose: (_, key) => {
|
|
17
|
+
log.debug(`Disposing PostGraphile[${key}]`);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
// Register cleanup callback with pgCache
|
|
21
|
+
// When a pg pool is disposed, clean up any graphile instances using it
|
|
22
|
+
const unregister = pg_cache_1.pgCache.registerCleanupCallback((pgPoolKey) => {
|
|
23
|
+
exports.graphileCache.forEach((entry, k) => {
|
|
24
|
+
if (entry.pgPoolKey === pgPoolKey) {
|
|
25
|
+
log.debug(`Removing graphileCache[${k}] due to pgPool[${pgPoolKey}]`);
|
|
26
|
+
exports.graphileCache.delete(k);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
// Enhanced close function that handles all caches
|
|
31
|
+
const closePromise = { promise: null };
|
|
32
|
+
const closeAllCaches = async (verbose = false) => {
|
|
33
|
+
if (closePromise.promise)
|
|
34
|
+
return closePromise.promise;
|
|
35
|
+
closePromise.promise = (async () => {
|
|
36
|
+
if (verbose)
|
|
37
|
+
log.info('Closing all server caches...');
|
|
38
|
+
exports.graphileCache.clear();
|
|
39
|
+
await pg_cache_1.pgCache.close();
|
|
40
|
+
if (verbose)
|
|
41
|
+
log.success('All caches disposed.');
|
|
42
|
+
})();
|
|
43
|
+
return closePromise.promise;
|
|
44
|
+
};
|
|
45
|
+
exports.closeAllCaches = closeAllCaches;
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { graphileCache, GraphileCache, closeAllCaches } from './graphile-cache';
|
package/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.closeAllCaches = exports.graphileCache = void 0;
|
|
4
|
+
// Main exports from graphile-cache package
|
|
5
|
+
var graphile_cache_1 = require("./graphile-cache");
|
|
6
|
+
Object.defineProperty(exports, "graphileCache", { enumerable: true, get: function () { return graphile_cache_1.graphileCache; } });
|
|
7
|
+
Object.defineProperty(exports, "closeAllCaches", { enumerable: true, get: function () { return graphile_cache_1.closeAllCaches; } });
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "graphile-cache",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
|
+
"description": "PostGraphile LRU cache with automatic pool cleanup integration",
|
|
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
|
+
"pg-cache": "^1.0.0",
|
|
35
|
+
"lru-cache": "^11.1.0",
|
|
36
|
+
"pg": "^8.16.0",
|
|
37
|
+
"postgraphile": "^4.14.1"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/pg": "^8.15.2",
|
|
41
|
+
"@types/rimraf": "^4.0.5",
|
|
42
|
+
"nodemon": "^3.1.10",
|
|
43
|
+
"ts-node": "^10.9.2"
|
|
44
|
+
},
|
|
45
|
+
"keywords": [
|
|
46
|
+
"postgraphile",
|
|
47
|
+
"graphile",
|
|
48
|
+
"cache",
|
|
49
|
+
"lru",
|
|
50
|
+
"postgresql",
|
|
51
|
+
"launchql"
|
|
52
|
+
]
|
|
53
|
+
}
|