@pgpmjs/server-utils 2.8.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 ADDED
@@ -0,0 +1,23 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
4
+ Copyright (c) 2025 Constructive <developers@constructive.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,89 @@
1
+ # @launchql/server-utils
2
+
3
+ <p align="center" width="100%">
4
+ <img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" />
5
+ </p>
6
+
7
+ <p align="center" width="100%">
8
+ <a href="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml">
9
+ <img height="20" src="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml/badge.svg" />
10
+ </a>
11
+ <a href="https://github.com/constructive-io/constructive/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/constructive-io/constructive?filename=packages%2Fserver-utils%2Fpackage.json"/></a>
13
+ </p>
14
+
15
+ ---
16
+
17
+ ## Education and Tutorials
18
+
19
+ 1. 🚀 [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
20
+ Get started with modular databases in minutes. Install prerequisites and deploy your first module.
21
+
22
+ 2. 📦 [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
23
+ Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
24
+
25
+ 3. ✏️ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
26
+ Master the workflow for adding, organizing, and managing database changes with pgpm.
27
+
28
+ 4. 🧪 [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
29
+ Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
30
+
31
+ 5. ⚡ [Supabase Testing](https://constructive.io/learn/supabase)
32
+ Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
33
+
34
+ 6. 💧 [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
35
+ Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
36
+
37
+ 7. 🔧 [Troubleshooting](https://constructive.io/learn/troubleshooting)
38
+ Common issues and solutions for pgpm, PostgreSQL, and testing.
39
+
40
+ ## Related Constructive Tooling
41
+
42
+ ### 🧪 Testing
43
+
44
+ * [pgsql-test](https://github.com/constructive-io/constructive/tree/main/packages/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
45
+ * [supabase-test](https://github.com/constructive-io/constructive/tree/main/packages/supabase-test): **🧪 Supabase-native test harness** preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
46
+ * [graphile-test](https://github.com/constructive-io/constructive/tree/main/packages/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
47
+ * [pg-query-context](https://github.com/constructive-io/constructive/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.
48
+
49
+ ### 🧠 Parsing & AST
50
+
51
+ * [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
52
+ * [libpg-query-node](https://www.npmjs.com/package/libpg-query): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
53
+ * [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
54
+ * [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
55
+ * [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
56
+ * [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
57
+ * [pg-ast](https://www.npmjs.com/package/pg-ast): **🔍 Low-level AST tools** and transformations for Postgres query structures.
58
+
59
+ ### 🚀 API & Dev Tools
60
+
61
+ * [launchql/server](https://github.com/constructive-io/constructive/tree/main/packages/server): **⚡ Express-based API server** powered by PostGraphile to expose a secure, scalable GraphQL API over your Postgres database.
62
+ * [launchql/explorer](https://github.com/constructive-io/constructive/tree/main/packages/explorer): **🔎 Visual API explorer** with GraphiQL for browsing across all databases and schemas—useful for debugging, documentation, and API prototyping.
63
+
64
+ ### 🔁 Streaming & Uploads
65
+
66
+ * [launchql/s3-streamer](https://github.com/constructive-io/constructive/tree/main/packages/s3-streamer): **📤 Direct S3 streaming** for large files with support for metadata injection and content validation.
67
+ * [launchql/etag-hash](https://github.com/constructive-io/constructive/tree/main/packages/etag-hash): **🏷️ S3-compatible ETags** created by streaming and hashing file uploads in chunks.
68
+ * [launchql/etag-stream](https://github.com/constructive-io/constructive/tree/main/packages/etag-stream): **🔄 ETag computation** via Node stream transformer during upload or transfer.
69
+ * [launchql/uuid-hash](https://github.com/constructive-io/constructive/tree/main/packages/uuid-hash): **🆔 Deterministic UUIDs** generated from hashed content, great for deduplication and asset referencing.
70
+ * [launchql/uuid-stream](https://github.com/constructive-io/constructive/tree/main/packages/uuid-stream): **🌊 Streaming UUID generation** based on piped file content—ideal for upload pipelines.
71
+ * [launchql/upload-names](https://github.com/constructive-io/constructive/tree/main/packages/upload-names): **📂 Collision-resistant filenames** utility for structured and unique file names for uploads.
72
+
73
+ ### 🧰 CLI & Codegen
74
+
75
+ * [pgpm](https://github.com/constructive-io/constructive/tree/main/packages/pgpm): **🖥️ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
76
+ * [@launchql/cli](https://github.com/constructive-io/constructive/tree/main/packages/cli): **🖥️ Command-line toolkit** for managing LaunchQL projects—supports database scaffolding, migrations, seeding, code generation, and automation.
77
+ * [constructive-io/constructive-gen](https://github.com/constructive-io/constructive/tree/main/packages/launchql-gen): **✨ Auto-generated GraphQL** mutations and queries dynamically built from introspected schema data.
78
+ * [@launchql/query-builder](https://github.com/constructive-io/constructive/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.
79
+ * [@launchql/query](https://github.com/constructive-io/constructive/tree/main/packages/query): **🧩 Fluent GraphQL builder** for PostGraphile schemas. ⚡ Schema-aware via introspection, 🧩 composable and ergonomic for building deeply nested queries.
80
+
81
+ ## Credits
82
+
83
+ **🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
84
+
85
+ ## Disclaimer
86
+
87
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
88
+
89
+ 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.
package/cors.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { Express } from 'express';
2
+ /**
3
+ * App-wide CORS helper (used by Explorer and optionally by API)
4
+ *
5
+ * - If an explicit origin string is provided (and not "*"), enable CORS for that
6
+ * exact origin and set credentials=true.
7
+ * - If origin is omitted or "*", install a small wrapper that reflects the
8
+ * request's Origin header (permissive mode). This is convenient for
9
+ * development or single-tenant setups but should be scoped in production.
10
+ */
11
+ export declare const cors: (app: Express, origin?: string) => void;
package/cors.js ADDED
@@ -0,0 +1,40 @@
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.cors = void 0;
7
+ const cors_1 = __importDefault(require("cors"));
8
+ /**
9
+ * App-wide CORS helper (used by Explorer and optionally by API)
10
+ *
11
+ * - If an explicit origin string is provided (and not "*"), enable CORS for that
12
+ * exact origin and set credentials=true.
13
+ * - If origin is omitted or "*", install a small wrapper that reflects the
14
+ * request's Origin header (permissive mode). This is convenient for
15
+ * development or single-tenant setups but should be scoped in production.
16
+ */
17
+ const cors = (app, origin) => {
18
+ const corsOptions = origin && origin.trim() !== '*'
19
+ ? {
20
+ origin,
21
+ credentials: true,
22
+ optionsSuccessStatus: 200,
23
+ }
24
+ : undefined;
25
+ if (corsOptions) {
26
+ app.use((0, cors_1.default)(corsOptions));
27
+ }
28
+ else {
29
+ // Wildcard/permissive mode: reflect the caller's Origin header
30
+ app.use((req, res, next) => {
31
+ const opts = {
32
+ origin: req.get('origin'),
33
+ credentials: true,
34
+ optionsSuccessStatus: 200,
35
+ };
36
+ return (0, cors_1.default)(opts)(req, res, next);
37
+ });
38
+ }
39
+ };
40
+ exports.cors = cors;
package/esm/cors.js ADDED
@@ -0,0 +1,33 @@
1
+ import corsPlugin from 'cors';
2
+ /**
3
+ * App-wide CORS helper (used by Explorer and optionally by API)
4
+ *
5
+ * - If an explicit origin string is provided (and not "*"), enable CORS for that
6
+ * exact origin and set credentials=true.
7
+ * - If origin is omitted or "*", install a small wrapper that reflects the
8
+ * request's Origin header (permissive mode). This is convenient for
9
+ * development or single-tenant setups but should be scoped in production.
10
+ */
11
+ export const cors = (app, origin) => {
12
+ const corsOptions = origin && origin.trim() !== '*'
13
+ ? {
14
+ origin,
15
+ credentials: true,
16
+ optionsSuccessStatus: 200,
17
+ }
18
+ : undefined;
19
+ if (corsOptions) {
20
+ app.use(corsPlugin(corsOptions));
21
+ }
22
+ else {
23
+ // Wildcard/permissive mode: reflect the caller's Origin header
24
+ app.use((req, res, next) => {
25
+ const opts = {
26
+ origin: req.get('origin'),
27
+ credentials: true,
28
+ optionsSuccessStatus: 200,
29
+ };
30
+ return corsPlugin(opts)(req, res, next);
31
+ });
32
+ }
33
+ };
package/esm/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './cors';
2
+ export * from './lru';
3
+ export * from './utils';
package/esm/lru.js ADDED
@@ -0,0 +1,15 @@
1
+ import { Logger } from '@pgpmjs/logger';
2
+ import { LRUCache } from 'lru-cache';
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
+ // --- Service Cache ---
8
+ export const svcCache = new LRUCache({
9
+ max: 25,
10
+ ttl: ONE_YEAR,
11
+ updateAgeOnGet: true,
12
+ dispose: (_, key) => {
13
+ log.debug(`Disposing service[${key}]`);
14
+ }
15
+ });
package/esm/utils.js ADDED
@@ -0,0 +1,21 @@
1
+ export const healthz = (app) => {
2
+ app.get('/healthz', (req, res) => {
3
+ // could be checking db, etc..
4
+ res.send('ok');
5
+ });
6
+ };
7
+ export const poweredBy = (name) => {
8
+ return async (req, res, next) => {
9
+ res.set({
10
+ 'X-Powered-By': name,
11
+ });
12
+ return next();
13
+ };
14
+ };
15
+ export const trustProxy = (app, trustProxy) => {
16
+ if (trustProxy) {
17
+ app.set('trust proxy', (ip) => {
18
+ return true;
19
+ });
20
+ }
21
+ };
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './cors';
2
+ export * from './lru';
3
+ export * from './utils';
package/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./cors"), exports);
18
+ __exportStar(require("./lru"), exports);
19
+ __exportStar(require("./utils"), exports);
package/lru.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { LRUCache } from 'lru-cache';
2
+ export declare const svcCache: LRUCache<string, any, unknown>;
package/lru.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.svcCache = void 0;
4
+ const logger_1 = require("@pgpmjs/logger");
5
+ const lru_cache_1 = require("lru-cache");
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
+ // --- Service Cache ---
11
+ exports.svcCache = new lru_cache_1.LRUCache({
12
+ max: 25,
13
+ ttl: ONE_YEAR,
14
+ updateAgeOnGet: true,
15
+ dispose: (_, key) => {
16
+ log.debug(`Disposing service[${key}]`);
17
+ }
18
+ });
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@pgpmjs/server-utils",
3
+ "version": "2.8.0",
4
+ "author": "Constructive <developers@constructive.io>",
5
+ "description": "PGPM server utils",
6
+ "main": "index.js",
7
+ "module": "esm/index.js",
8
+ "types": "index.d.ts",
9
+ "homepage": "https://github.com/constructive-io/constructive",
10
+ "license": "MIT",
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "directory": "dist"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/constructive-io/constructive"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/constructive-io/constructive/issues"
21
+ },
22
+ "scripts": {
23
+ "clean": "makage clean",
24
+ "prepack": "npm run build",
25
+ "build": "makage build",
26
+ "build:dev": "makage build --dev",
27
+ "lint": "eslint . --fix",
28
+ "test": "jest",
29
+ "test:watch": "jest --watch"
30
+ },
31
+ "dependencies": {
32
+ "@pgpmjs/logger": "^1.3.0",
33
+ "@pgpmjs/types": "^2.11.0",
34
+ "cors": "^2.8.5",
35
+ "express": "^5.1.0",
36
+ "lru-cache": "^11.2.4"
37
+ },
38
+ "devDependencies": {
39
+ "@types/cors": "^2.8.17",
40
+ "@types/express": "^5.0.6",
41
+ "makage": "^0.1.8",
42
+ "ts-node": "^10.9.2"
43
+ },
44
+ "keywords": [
45
+ "server",
46
+ "utilities",
47
+ "express",
48
+ "pgpm",
49
+ "pgpmjs",
50
+ "helpers"
51
+ ],
52
+ "gitHead": "e4d5396a5d9154f4886176bb00c2b460b0f320e5"
53
+ }
package/utils.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { Express, NextFunction, Request, Response } from 'express';
2
+ export declare const healthz: (app: Express) => void;
3
+ export declare const poweredBy: (name: string) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
4
+ export declare const trustProxy: (app: Express, trustProxy?: boolean) => void;
package/utils.js ADDED
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.trustProxy = exports.poweredBy = exports.healthz = void 0;
4
+ const healthz = (app) => {
5
+ app.get('/healthz', (req, res) => {
6
+ // could be checking db, etc..
7
+ res.send('ok');
8
+ });
9
+ };
10
+ exports.healthz = healthz;
11
+ const poweredBy = (name) => {
12
+ return async (req, res, next) => {
13
+ res.set({
14
+ 'X-Powered-By': name,
15
+ });
16
+ return next();
17
+ };
18
+ };
19
+ exports.poweredBy = poweredBy;
20
+ const trustProxy = (app, trustProxy) => {
21
+ if (trustProxy) {
22
+ app.set('trust proxy', (ip) => {
23
+ return true;
24
+ });
25
+ }
26
+ };
27
+ exports.trustProxy = trustProxy;