@pgpmjs/portability 0.1.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 +125 -0
- package/esm/index.js +2 -0
- package/esm/seed.js +29 -0
- package/esm/shapes.js +102 -0
- package/index.d.ts +4 -0
- package/index.js +11 -0
- package/package.json +54 -0
- package/seed.d.ts +40 -0
- package/seed.js +33 -0
- package/shapes.d.ts +69 -0
- package/shapes.js +107 -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 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,125 @@
|
|
|
1
|
+
# @pgpmjs/portability
|
|
2
|
+
|
|
3
|
+
Vendor portability for pgpm: move packages between vendor-managed PostgreSQL
|
|
4
|
+
environments (Supabase, InsForge, ...) and plain PostgreSQL/pgpm — and test the
|
|
5
|
+
result with `pgsql-test`.
|
|
6
|
+
|
|
7
|
+
## Seed adapter
|
|
8
|
+
|
|
9
|
+
Deploy a workspace target (typically an apply proxy carrying `pgpm.apply.json`)
|
|
10
|
+
into a test database, through the same engine path as `pgpm deploy`:
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { getConnections } from 'pgsql-test';
|
|
14
|
+
import { seed } from '@pgpmjs/portability';
|
|
15
|
+
|
|
16
|
+
const { pg, teardown } = await getConnections({}, [
|
|
17
|
+
seed.apply('vendor-app-ported')
|
|
18
|
+
]);
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The workspace is discovered from the test's cwd; dependencies resolve from the
|
|
22
|
+
workspace module map; proxy modules transpile and materialize through the
|
|
23
|
+
apply path.
|
|
24
|
+
|
|
25
|
+
## Vendor shapes
|
|
26
|
+
|
|
27
|
+
A `VendorShape` describes a vendor's managed subsystems (auth schemas, roles,
|
|
28
|
+
extensions schema, users table, accessor functions). Build routing profiles in
|
|
29
|
+
either direction from a shape plus a provider binding:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { fromVendorProfile, supabase, toVendorProfile } from '@pgpmjs/portability';
|
|
33
|
+
|
|
34
|
+
const provider = {
|
|
35
|
+
schema: 'app_auth',
|
|
36
|
+
users: 'users',
|
|
37
|
+
accessors: { uid: 'current_user_id' },
|
|
38
|
+
roles: { authenticated: 'app_authenticated' }
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// vendor → pgpm: exclude the vendor auth subsystem, rebind onto the provider,
|
|
42
|
+
// de-qualify extension symbols, translate roles
|
|
43
|
+
const off = fromVendorProfile(supabase, provider);
|
|
44
|
+
|
|
45
|
+
// pgpm → vendor: the inverse — rebind onto the native subsystem, qualify
|
|
46
|
+
// extension symbols, translate roles back
|
|
47
|
+
const on = toVendorProfile(supabase, provider);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The resulting `PgpmRoutingProfile` drops into a workspace `portability` block
|
|
51
|
+
or a proxy module's `pgpm.apply.json`.
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## Education and Tutorials
|
|
56
|
+
|
|
57
|
+
1. 🚀 [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
|
|
58
|
+
Get started with modular databases in minutes. Install prerequisites and deploy your first module.
|
|
59
|
+
|
|
60
|
+
2. 📦 [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
|
|
61
|
+
Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
|
|
62
|
+
|
|
63
|
+
3. ✏️ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
|
|
64
|
+
Master the workflow for adding, organizing, and managing database changes with pgpm.
|
|
65
|
+
|
|
66
|
+
4. 🧪 [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
|
|
67
|
+
Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
|
|
68
|
+
|
|
69
|
+
5. ⚡ [Supabase Testing](https://constructive.io/learn/supabase)
|
|
70
|
+
Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
|
|
71
|
+
|
|
72
|
+
6. 💧 [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
|
|
73
|
+
Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
|
|
74
|
+
|
|
75
|
+
7. 🔧 [Troubleshooting](https://constructive.io/learn/troubleshooting)
|
|
76
|
+
Common issues and solutions for pgpm, PostgreSQL, and testing.
|
|
77
|
+
|
|
78
|
+
## Related Constructive Tooling
|
|
79
|
+
|
|
80
|
+
### 📦 Package Management
|
|
81
|
+
|
|
82
|
+
* [pgpm](https://github.com/constructive-io/constructive/tree/main/pgpm/pgpm): **🖥️ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
|
|
83
|
+
|
|
84
|
+
### 🧪 Testing
|
|
85
|
+
|
|
86
|
+
* [pgsql-test](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
|
|
87
|
+
* [pglite-test](https://github.com/constructive-io/constructive/tree/main/postgres/pglite-test): **🪶 Drop-in pgsql-test replacement backed by PGlite** — in-process Postgres, no server required, instance-per-suite isolation.
|
|
88
|
+
* [pgsql-seed](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-seed): **🌱 PostgreSQL seeding utilities** for CSV, JSON, SQL data loading, and pgpm deployment.
|
|
89
|
+
* [supabase-test](https://github.com/constructive-io/constructive/tree/main/postgres/supabase-test): **🧪 Supabase-native test harness** preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
|
|
90
|
+
* [graphile-test](https://github.com/constructive-io/constructive/tree/main/graphile/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
|
|
91
|
+
* [pg-query-context](https://github.com/constructive-io/constructive/tree/main/postgres/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.
|
|
92
|
+
|
|
93
|
+
### 🧠 Parsing & AST
|
|
94
|
+
|
|
95
|
+
* [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
|
|
96
|
+
* [libpg-query-node](https://www.npmjs.com/package/libpg-query): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
|
|
97
|
+
* [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.
|
|
98
|
+
* [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
|
|
99
|
+
* [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
|
|
100
|
+
* [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
|
|
101
|
+
|
|
102
|
+
### 📚 Documentation & Skills
|
|
103
|
+
|
|
104
|
+
* [constructive-skills](https://github.com/constructive-io/constructive-skills): **📖 Platform documentation and AI agent skills** — feature catalog, blueprint reference, SDK guides (i18n, billing, limits, events, uploads, security, entities, search, AI), and deployment guides.
|
|
105
|
+
|
|
106
|
+
Install skills for AI coding agents:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
# All platform skills (security, blueprints, codegen, billing, etc.)
|
|
110
|
+
npx skills add constructive-io/constructive-skills
|
|
111
|
+
|
|
112
|
+
# Individual repo skills (pgpm, testing, CLI, search, etc.)
|
|
113
|
+
npx skills add https://github.com/constructive-io/constructive --skill pgpm
|
|
114
|
+
npx skills add https://github.com/constructive-io/constructive --skill constructive-testing
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Credits
|
|
118
|
+
|
|
119
|
+
**🛠 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).**
|
|
120
|
+
|
|
121
|
+
## Disclaimer
|
|
122
|
+
|
|
123
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
124
|
+
|
|
125
|
+
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/esm/index.js
ADDED
package/esm/seed.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { PgpmPackage } from '@pgpmjs/core';
|
|
2
|
+
import { getEnvOptions } from '@pgpmjs/env';
|
|
3
|
+
/**
|
|
4
|
+
* Deploy a named workspace target — typically an apply proxy (a directory
|
|
5
|
+
* carrying `pgpm.apply.json`) — into the test database, exactly as a
|
|
6
|
+
* production `pgpm deploy <target>` would: the workspace is discovered from
|
|
7
|
+
* the test's cwd, dependencies resolve from the workspace module map, and
|
|
8
|
+
* proxy modules transpile/materialize through the engine's apply path.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* getConnections({}, [seed.apply('vendor-app-ported')]);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export function apply(target, options = {}) {
|
|
15
|
+
return {
|
|
16
|
+
async seed(ctx) {
|
|
17
|
+
const workspace = new PgpmPackage(options.cwd ?? ctx.connect.cwd ?? process.cwd());
|
|
18
|
+
await workspace.deploy(getEnvOptions({
|
|
19
|
+
pg: ctx.config,
|
|
20
|
+
deployment: {
|
|
21
|
+
fast: true,
|
|
22
|
+
usePlan: true,
|
|
23
|
+
cache: options.cache ?? false
|
|
24
|
+
}
|
|
25
|
+
}), target);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export const seed = { apply };
|
package/esm/shapes.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/** Supabase: `auth` subsystem, `extensions` schema, `auth.uid()`/`auth.role()`. */
|
|
2
|
+
export const supabase = {
|
|
3
|
+
vendor: 'supabase',
|
|
4
|
+
authSchemas: ['auth'],
|
|
5
|
+
extensionsSchema: 'extensions',
|
|
6
|
+
roles: ['anon', 'authenticated', 'service_role'],
|
|
7
|
+
users: { schema: 'auth', kind: 'table', name: 'users' },
|
|
8
|
+
accessors: [
|
|
9
|
+
{ schema: 'auth', kind: 'function', name: 'uid' },
|
|
10
|
+
{ schema: 'auth', kind: 'function', name: 'role' }
|
|
11
|
+
]
|
|
12
|
+
};
|
|
13
|
+
/** InsForge: `auth` subsystem with `auth.users`, roles on the default search path. */
|
|
14
|
+
export const insforge = {
|
|
15
|
+
vendor: 'insforge',
|
|
16
|
+
authSchemas: ['auth'],
|
|
17
|
+
extensionsSchema: null,
|
|
18
|
+
roles: ['anon', 'authenticated', 'project_admin'],
|
|
19
|
+
users: { schema: 'auth', kind: 'table', name: 'users' },
|
|
20
|
+
accessors: []
|
|
21
|
+
};
|
|
22
|
+
function routesFor(shape, provider, invert) {
|
|
23
|
+
const routes = [];
|
|
24
|
+
if (invert && provider.schema === null && (provider.users || provider.accessors)) {
|
|
25
|
+
throw new Error(`toVendorProfile(${shape.vendor}): provider objects must live in a named schema ` +
|
|
26
|
+
`to be routed back onto the vendor (provider.schema is null)`);
|
|
27
|
+
}
|
|
28
|
+
if (shape.users && provider.users) {
|
|
29
|
+
routes.push(invert
|
|
30
|
+
? {
|
|
31
|
+
fromSchema: provider.schema,
|
|
32
|
+
kind: shape.users.kind,
|
|
33
|
+
name: provider.users,
|
|
34
|
+
toSchema: shape.users.schema,
|
|
35
|
+
...(provider.users !== shape.users.name ? { toName: shape.users.name } : {})
|
|
36
|
+
}
|
|
37
|
+
: {
|
|
38
|
+
fromSchema: shape.users.schema,
|
|
39
|
+
kind: shape.users.kind,
|
|
40
|
+
name: shape.users.name,
|
|
41
|
+
toSchema: provider.schema,
|
|
42
|
+
...(provider.users !== shape.users.name ? { toName: provider.users } : {})
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
for (const accessor of shape.accessors) {
|
|
46
|
+
const target = provider.accessors?.[accessor.name];
|
|
47
|
+
if (!target)
|
|
48
|
+
continue;
|
|
49
|
+
routes.push(invert
|
|
50
|
+
? {
|
|
51
|
+
fromSchema: provider.schema,
|
|
52
|
+
kind: accessor.kind,
|
|
53
|
+
name: target,
|
|
54
|
+
toSchema: accessor.schema,
|
|
55
|
+
...(target !== accessor.name ? { toName: accessor.name } : {})
|
|
56
|
+
}
|
|
57
|
+
: {
|
|
58
|
+
fromSchema: accessor.schema,
|
|
59
|
+
kind: accessor.kind,
|
|
60
|
+
name: accessor.name,
|
|
61
|
+
toSchema: provider.schema,
|
|
62
|
+
...(target !== accessor.name ? { toName: target } : {})
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return routes;
|
|
66
|
+
}
|
|
67
|
+
function invertRoles(roles) {
|
|
68
|
+
if (!roles)
|
|
69
|
+
return undefined;
|
|
70
|
+
return Object.fromEntries(Object.entries(roles).map(([from, to]) => [to, from]));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Routing profile for moving a package *off* a vendor onto plain
|
|
74
|
+
* PostgreSQL/pgpm: exclude the vendor's auth subsystem, rebind the users FK
|
|
75
|
+
* and accessor calls onto the substitute provider, de-qualify (or re-route)
|
|
76
|
+
* extension symbols, and translate role names.
|
|
77
|
+
*/
|
|
78
|
+
export function fromVendorProfile(shape, provider) {
|
|
79
|
+
return {
|
|
80
|
+
exclude: { schemas: [...shape.authSchemas] },
|
|
81
|
+
route: routesFor(shape, provider, false),
|
|
82
|
+
...(shape.extensionsSchema
|
|
83
|
+
? { extensions: { toSchema: null, from: [shape.extensionsSchema] } }
|
|
84
|
+
: {}),
|
|
85
|
+
...(provider.roles ? { roles: { ...provider.roles } } : {})
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The inverse of {@link fromVendorProfile}: move a pgpm-shaped package *onto*
|
|
90
|
+
* the vendor's native subsystem — rebind the provider's objects back onto the
|
|
91
|
+
* vendor's, qualify extension symbols into the vendor's extensions schema,
|
|
92
|
+
* and translate roles back to the vendor's names.
|
|
93
|
+
*/
|
|
94
|
+
export function toVendorProfile(shape, provider) {
|
|
95
|
+
return {
|
|
96
|
+
route: routesFor(shape, provider, true),
|
|
97
|
+
...(shape.extensionsSchema
|
|
98
|
+
? { extensions: { toSchema: shape.extensionsSchema, from: [null] } }
|
|
99
|
+
: {}),
|
|
100
|
+
...(provider.roles ? { roles: invertRoles(provider.roles) } : {})
|
|
101
|
+
};
|
|
102
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { ApplySeedAdapter, ApplySeedContext, ApplySeedOptions } from './seed';
|
|
2
|
+
export { apply, seed } from './seed';
|
|
3
|
+
export type { ProviderBinding, VendorObjectRef, VendorShape } from './shapes';
|
|
4
|
+
export { fromVendorProfile, insforge, supabase, toVendorProfile } from './shapes';
|
package/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toVendorProfile = exports.supabase = exports.insforge = exports.fromVendorProfile = exports.seed = exports.apply = void 0;
|
|
4
|
+
var seed_1 = require("./seed");
|
|
5
|
+
Object.defineProperty(exports, "apply", { enumerable: true, get: function () { return seed_1.apply; } });
|
|
6
|
+
Object.defineProperty(exports, "seed", { enumerable: true, get: function () { return seed_1.seed; } });
|
|
7
|
+
var shapes_1 = require("./shapes");
|
|
8
|
+
Object.defineProperty(exports, "fromVendorProfile", { enumerable: true, get: function () { return shapes_1.fromVendorProfile; } });
|
|
9
|
+
Object.defineProperty(exports, "insforge", { enumerable: true, get: function () { return shapes_1.insforge; } });
|
|
10
|
+
Object.defineProperty(exports, "supabase", { enumerable: true, get: function () { return shapes_1.supabase; } });
|
|
11
|
+
Object.defineProperty(exports, "toVendorProfile", { enumerable: true, get: function () { return shapes_1.toVendorProfile; } });
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pgpmjs/portability",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"author": "Constructive <developers@constructive.io>",
|
|
5
|
+
"description": "Vendor portability for pgpm — vendor shape profiles (supabase, insforge, ...) and pgsql-test seed adapters for deploying apply/transpile targets",
|
|
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
|
+
"keywords": [
|
|
32
|
+
"sql",
|
|
33
|
+
"postgres",
|
|
34
|
+
"postgresql",
|
|
35
|
+
"pgpm",
|
|
36
|
+
"pgpmjs",
|
|
37
|
+
"portability",
|
|
38
|
+
"supabase",
|
|
39
|
+
"insforge",
|
|
40
|
+
"migration",
|
|
41
|
+
"transpile"
|
|
42
|
+
],
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"makage": "^0.3.0",
|
|
45
|
+
"pgsql-test": "^5.8.7"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@pgpmjs/core": "^7.18.0",
|
|
49
|
+
"@pgpmjs/env": "^2.39.2",
|
|
50
|
+
"@pgpmjs/types": "^2.48.1",
|
|
51
|
+
"pg-env": "^1.26.1"
|
|
52
|
+
},
|
|
53
|
+
"gitHead": "04b34ce4b5efcebfd8e49d64d196e651288d97f3"
|
|
54
|
+
}
|
package/seed.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { PgConfig } from 'pg-env';
|
|
2
|
+
/**
|
|
3
|
+
* The slice of pgsql-test's `SeedContext` the portability adapters need.
|
|
4
|
+
* Structural, so the adapters plug into `getConnections(_, [seed.apply(...)])`
|
|
5
|
+
* without this package depending on pgsql-test.
|
|
6
|
+
*/
|
|
7
|
+
export interface ApplySeedContext {
|
|
8
|
+
config: PgConfig;
|
|
9
|
+
connect: {
|
|
10
|
+
cwd?: string;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/** Structurally compatible with pgsql-test's `SeedAdapter`. */
|
|
14
|
+
export interface ApplySeedAdapter {
|
|
15
|
+
seed(ctx: ApplySeedContext): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
export interface ApplySeedOptions {
|
|
18
|
+
/** Enable pgpm's deployment cache. */
|
|
19
|
+
cache?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Directory to discover the workspace from. Defaults to the test's cwd —
|
|
22
|
+
* the workspace root is found automatically by walking up from it.
|
|
23
|
+
*/
|
|
24
|
+
cwd?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Deploy a named workspace target — typically an apply proxy (a directory
|
|
28
|
+
* carrying `pgpm.apply.json`) — into the test database, exactly as a
|
|
29
|
+
* production `pgpm deploy <target>` would: the workspace is discovered from
|
|
30
|
+
* the test's cwd, dependencies resolve from the workspace module map, and
|
|
31
|
+
* proxy modules transpile/materialize through the engine's apply path.
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* getConnections({}, [seed.apply('vendor-app-ported')]);
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare function apply(target: string, options?: ApplySeedOptions): ApplySeedAdapter;
|
|
38
|
+
export declare const seed: {
|
|
39
|
+
apply: typeof apply;
|
|
40
|
+
};
|
package/seed.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.seed = void 0;
|
|
4
|
+
exports.apply = apply;
|
|
5
|
+
const core_1 = require("@pgpmjs/core");
|
|
6
|
+
const env_1 = require("@pgpmjs/env");
|
|
7
|
+
/**
|
|
8
|
+
* Deploy a named workspace target — typically an apply proxy (a directory
|
|
9
|
+
* carrying `pgpm.apply.json`) — into the test database, exactly as a
|
|
10
|
+
* production `pgpm deploy <target>` would: the workspace is discovered from
|
|
11
|
+
* the test's cwd, dependencies resolve from the workspace module map, and
|
|
12
|
+
* proxy modules transpile/materialize through the engine's apply path.
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* getConnections({}, [seed.apply('vendor-app-ported')]);
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function apply(target, options = {}) {
|
|
19
|
+
return {
|
|
20
|
+
async seed(ctx) {
|
|
21
|
+
const workspace = new core_1.PgpmPackage(options.cwd ?? ctx.connect.cwd ?? process.cwd());
|
|
22
|
+
await workspace.deploy((0, env_1.getEnvOptions)({
|
|
23
|
+
pg: ctx.config,
|
|
24
|
+
deployment: {
|
|
25
|
+
fast: true,
|
|
26
|
+
usePlan: true,
|
|
27
|
+
cache: options.cache ?? false
|
|
28
|
+
}
|
|
29
|
+
}), target);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
exports.seed = { apply };
|
package/shapes.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { PgpmRouteKind, PgpmRoutingProfile } from '@pgpmjs/types';
|
|
2
|
+
/** A vendor-owned object that portable SQL may reference. */
|
|
3
|
+
export interface VendorObjectRef {
|
|
4
|
+
schema: string;
|
|
5
|
+
kind: PgpmRouteKind;
|
|
6
|
+
name: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* A vendor's database shape: the schemas, roles, and objects its managed
|
|
10
|
+
* subsystems own — the "weirdnesses" a package must be translated across when
|
|
11
|
+
* it moves between the vendor and a plain-PostgreSQL/pgpm environment.
|
|
12
|
+
*/
|
|
13
|
+
export interface VendorShape {
|
|
14
|
+
/** Vendor identifier (e.g. `supabase`). */
|
|
15
|
+
vendor: string;
|
|
16
|
+
/**
|
|
17
|
+
* Schemas owned by the vendor's managed auth subsystem. Moving *off* the
|
|
18
|
+
* vendor excludes these and substitutes a provider; moving *onto* the
|
|
19
|
+
* vendor rebinds onto the native subsystem.
|
|
20
|
+
*/
|
|
21
|
+
authSchemas: string[];
|
|
22
|
+
/**
|
|
23
|
+
* Schema the vendor installs extensions into (`null`: extensions live on
|
|
24
|
+
* the default `search_path`, no qualification).
|
|
25
|
+
*/
|
|
26
|
+
extensionsSchema: string | null;
|
|
27
|
+
/** The vendor's application-facing role names. */
|
|
28
|
+
roles: string[];
|
|
29
|
+
/** The vendor's canonical users table, the usual FK target. */
|
|
30
|
+
users?: VendorObjectRef;
|
|
31
|
+
/**
|
|
32
|
+
* Auth accessor functions application SQL calls (e.g. `auth.uid()`),
|
|
33
|
+
* which must be rebound onto the substitute provider's equivalents.
|
|
34
|
+
*/
|
|
35
|
+
accessors: VendorObjectRef[];
|
|
36
|
+
}
|
|
37
|
+
/** Supabase: `auth` subsystem, `extensions` schema, `auth.uid()`/`auth.role()`. */
|
|
38
|
+
export declare const supabase: VendorShape;
|
|
39
|
+
/** InsForge: `auth` subsystem with `auth.users`, roles on the default search path. */
|
|
40
|
+
export declare const insforge: VendorShape;
|
|
41
|
+
/**
|
|
42
|
+
* How a substitute provider binds a vendor's auth surface: where the
|
|
43
|
+
* provider's objects live and which of its objects replace each vendor
|
|
44
|
+
* object. Used in both directions — the inverse binding round-trips.
|
|
45
|
+
*/
|
|
46
|
+
export interface ProviderBinding {
|
|
47
|
+
/** Schema the provider's objects live in (`null`: unqualified). */
|
|
48
|
+
schema: string | null;
|
|
49
|
+
/** Provider's users-table name replacing the vendor users table. */
|
|
50
|
+
users?: string;
|
|
51
|
+
/** Vendor accessor name → provider function name (e.g. `uid` → `current_user_id`). */
|
|
52
|
+
accessors?: Record<string, string>;
|
|
53
|
+
/** Vendor role name → target role name. */
|
|
54
|
+
roles?: Record<string, string>;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Routing profile for moving a package *off* a vendor onto plain
|
|
58
|
+
* PostgreSQL/pgpm: exclude the vendor's auth subsystem, rebind the users FK
|
|
59
|
+
* and accessor calls onto the substitute provider, de-qualify (or re-route)
|
|
60
|
+
* extension symbols, and translate role names.
|
|
61
|
+
*/
|
|
62
|
+
export declare function fromVendorProfile(shape: VendorShape, provider: ProviderBinding): PgpmRoutingProfile;
|
|
63
|
+
/**
|
|
64
|
+
* The inverse of {@link fromVendorProfile}: move a pgpm-shaped package *onto*
|
|
65
|
+
* the vendor's native subsystem — rebind the provider's objects back onto the
|
|
66
|
+
* vendor's, qualify extension symbols into the vendor's extensions schema,
|
|
67
|
+
* and translate roles back to the vendor's names.
|
|
68
|
+
*/
|
|
69
|
+
export declare function toVendorProfile(shape: VendorShape, provider: ProviderBinding): PgpmRoutingProfile;
|
package/shapes.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.insforge = exports.supabase = void 0;
|
|
4
|
+
exports.fromVendorProfile = fromVendorProfile;
|
|
5
|
+
exports.toVendorProfile = toVendorProfile;
|
|
6
|
+
/** Supabase: `auth` subsystem, `extensions` schema, `auth.uid()`/`auth.role()`. */
|
|
7
|
+
exports.supabase = {
|
|
8
|
+
vendor: 'supabase',
|
|
9
|
+
authSchemas: ['auth'],
|
|
10
|
+
extensionsSchema: 'extensions',
|
|
11
|
+
roles: ['anon', 'authenticated', 'service_role'],
|
|
12
|
+
users: { schema: 'auth', kind: 'table', name: 'users' },
|
|
13
|
+
accessors: [
|
|
14
|
+
{ schema: 'auth', kind: 'function', name: 'uid' },
|
|
15
|
+
{ schema: 'auth', kind: 'function', name: 'role' }
|
|
16
|
+
]
|
|
17
|
+
};
|
|
18
|
+
/** InsForge: `auth` subsystem with `auth.users`, roles on the default search path. */
|
|
19
|
+
exports.insforge = {
|
|
20
|
+
vendor: 'insforge',
|
|
21
|
+
authSchemas: ['auth'],
|
|
22
|
+
extensionsSchema: null,
|
|
23
|
+
roles: ['anon', 'authenticated', 'project_admin'],
|
|
24
|
+
users: { schema: 'auth', kind: 'table', name: 'users' },
|
|
25
|
+
accessors: []
|
|
26
|
+
};
|
|
27
|
+
function routesFor(shape, provider, invert) {
|
|
28
|
+
const routes = [];
|
|
29
|
+
if (invert && provider.schema === null && (provider.users || provider.accessors)) {
|
|
30
|
+
throw new Error(`toVendorProfile(${shape.vendor}): provider objects must live in a named schema ` +
|
|
31
|
+
`to be routed back onto the vendor (provider.schema is null)`);
|
|
32
|
+
}
|
|
33
|
+
if (shape.users && provider.users) {
|
|
34
|
+
routes.push(invert
|
|
35
|
+
? {
|
|
36
|
+
fromSchema: provider.schema,
|
|
37
|
+
kind: shape.users.kind,
|
|
38
|
+
name: provider.users,
|
|
39
|
+
toSchema: shape.users.schema,
|
|
40
|
+
...(provider.users !== shape.users.name ? { toName: shape.users.name } : {})
|
|
41
|
+
}
|
|
42
|
+
: {
|
|
43
|
+
fromSchema: shape.users.schema,
|
|
44
|
+
kind: shape.users.kind,
|
|
45
|
+
name: shape.users.name,
|
|
46
|
+
toSchema: provider.schema,
|
|
47
|
+
...(provider.users !== shape.users.name ? { toName: provider.users } : {})
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
for (const accessor of shape.accessors) {
|
|
51
|
+
const target = provider.accessors?.[accessor.name];
|
|
52
|
+
if (!target)
|
|
53
|
+
continue;
|
|
54
|
+
routes.push(invert
|
|
55
|
+
? {
|
|
56
|
+
fromSchema: provider.schema,
|
|
57
|
+
kind: accessor.kind,
|
|
58
|
+
name: target,
|
|
59
|
+
toSchema: accessor.schema,
|
|
60
|
+
...(target !== accessor.name ? { toName: accessor.name } : {})
|
|
61
|
+
}
|
|
62
|
+
: {
|
|
63
|
+
fromSchema: accessor.schema,
|
|
64
|
+
kind: accessor.kind,
|
|
65
|
+
name: accessor.name,
|
|
66
|
+
toSchema: provider.schema,
|
|
67
|
+
...(target !== accessor.name ? { toName: target } : {})
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return routes;
|
|
71
|
+
}
|
|
72
|
+
function invertRoles(roles) {
|
|
73
|
+
if (!roles)
|
|
74
|
+
return undefined;
|
|
75
|
+
return Object.fromEntries(Object.entries(roles).map(([from, to]) => [to, from]));
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Routing profile for moving a package *off* a vendor onto plain
|
|
79
|
+
* PostgreSQL/pgpm: exclude the vendor's auth subsystem, rebind the users FK
|
|
80
|
+
* and accessor calls onto the substitute provider, de-qualify (or re-route)
|
|
81
|
+
* extension symbols, and translate role names.
|
|
82
|
+
*/
|
|
83
|
+
function fromVendorProfile(shape, provider) {
|
|
84
|
+
return {
|
|
85
|
+
exclude: { schemas: [...shape.authSchemas] },
|
|
86
|
+
route: routesFor(shape, provider, false),
|
|
87
|
+
...(shape.extensionsSchema
|
|
88
|
+
? { extensions: { toSchema: null, from: [shape.extensionsSchema] } }
|
|
89
|
+
: {}),
|
|
90
|
+
...(provider.roles ? { roles: { ...provider.roles } } : {})
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* The inverse of {@link fromVendorProfile}: move a pgpm-shaped package *onto*
|
|
95
|
+
* the vendor's native subsystem — rebind the provider's objects back onto the
|
|
96
|
+
* vendor's, qualify extension symbols into the vendor's extensions schema,
|
|
97
|
+
* and translate roles back to the vendor's names.
|
|
98
|
+
*/
|
|
99
|
+
function toVendorProfile(shape, provider) {
|
|
100
|
+
return {
|
|
101
|
+
route: routesFor(shape, provider, true),
|
|
102
|
+
...(shape.extensionsSchema
|
|
103
|
+
? { extensions: { toSchema: shape.extensionsSchema, from: [null] } }
|
|
104
|
+
: {}),
|
|
105
|
+
...(provider.roles ? { roles: invertRoles(provider.roles) } : {})
|
|
106
|
+
};
|
|
107
|
+
}
|