@pgpmjs/migrate-client 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.
Files changed (51) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +112 -0
  3. package/esm/index.d.ts +1 -0
  4. package/esm/index.js +1 -0
  5. package/esm/migrate/index.d.ts +5 -0
  6. package/esm/migrate/index.js +5 -0
  7. package/esm/migrate/orm/client.d.ts +55 -0
  8. package/esm/migrate/orm/client.js +99 -0
  9. package/esm/migrate/orm/index.d.ts +48 -0
  10. package/esm/migrate/orm/index.js +45 -0
  11. package/esm/migrate/orm/input-types.d.ts +513 -0
  12. package/esm/migrate/orm/input-types.js +2 -0
  13. package/esm/migrate/orm/models/index.d.ts +7 -0
  14. package/esm/migrate/orm/models/index.js +7 -0
  15. package/esm/migrate/orm/models/migrateFile.d.ts +56 -0
  16. package/esm/migrate/orm/models/migrateFile.js +94 -0
  17. package/esm/migrate/orm/models/sqlAction.d.ts +56 -0
  18. package/esm/migrate/orm/models/sqlAction.js +94 -0
  19. package/esm/migrate/orm/mutation/index.d.ts +27 -0
  20. package/esm/migrate/orm/mutation/index.js +30 -0
  21. package/esm/migrate/orm/query-builder.d.ts +91 -0
  22. package/esm/migrate/orm/query-builder.js +572 -0
  23. package/esm/migrate/orm/select-types.d.ts +103 -0
  24. package/esm/migrate/orm/select-types.js +1 -0
  25. package/esm/migrate/orm/types.d.ts +6 -0
  26. package/esm/migrate/orm/types.js +7 -0
  27. package/index.d.ts +1 -0
  28. package/index.js +17 -0
  29. package/migrate/index.d.ts +5 -0
  30. package/migrate/index.js +21 -0
  31. package/migrate/orm/client.d.ts +55 -0
  32. package/migrate/orm/client.js +105 -0
  33. package/migrate/orm/index.d.ts +48 -0
  34. package/migrate/orm/index.js +66 -0
  35. package/migrate/orm/input-types.d.ts +513 -0
  36. package/migrate/orm/input-types.js +5 -0
  37. package/migrate/orm/models/index.d.ts +7 -0
  38. package/migrate/orm/models/index.js +12 -0
  39. package/migrate/orm/models/migrateFile.d.ts +56 -0
  40. package/migrate/orm/models/migrateFile.js +98 -0
  41. package/migrate/orm/models/sqlAction.d.ts +56 -0
  42. package/migrate/orm/models/sqlAction.js +98 -0
  43. package/migrate/orm/mutation/index.d.ts +27 -0
  44. package/migrate/orm/mutation/index.js +33 -0
  45. package/migrate/orm/query-builder.d.ts +91 -0
  46. package/migrate/orm/query-builder.js +619 -0
  47. package/migrate/orm/select-types.d.ts +103 -0
  48. package/migrate/orm/select-types.js +2 -0
  49. package/migrate/orm/types.d.ts +6 -0
  50. package/migrate/orm/types.js +23 -0
  51. 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 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,112 @@
1
+ # @pgpmjs/migrate-client
2
+
3
+ <p align="center" width="100%">
4
+ <img height="120" 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/@pgpmjs/migrate-client"><img height="20" src="https://img.shields.io/github/package-json/v/constructive-io/constructive?filename=sdk%2Fmigrate-client%2Fpackage.json"/></a>
13
+ </p>
14
+
15
+ Typed GraphQL ORM client for the Constructive Migrate API (`db_migrate` schema).
16
+
17
+ Generated from `migrate.graphql` via `@constructive-io/graphql-codegen`.
18
+
19
+ ## Usage
20
+
21
+ ```typescript
22
+ import { createClient } from '@pgpmjs/migrate-client';
23
+
24
+ const db = createClient({
25
+ endpoint: 'https://migrate.example.com/graphql',
26
+ headers: { Authorization: 'Bearer <token>' },
27
+ });
28
+
29
+ // Fetch all sql_actions for a database
30
+ const result = await db.sqlAction.findMany({
31
+ select: { id: true, name: true, deploy: true, revert: true, verify: true, content: true },
32
+ where: { databaseId: { equalTo: '<database-uuid>' } },
33
+ }).unwrap();
34
+
35
+ console.log(result.sqlActions.nodes);
36
+ ```
37
+
38
+ ## Regeneration
39
+
40
+ ```bash
41
+ pnpm generate
42
+ ```
43
+
44
+ This runs codegen against `schemas/migrate.graphql` and outputs to `src/`.
45
+
46
+ ---
47
+
48
+ Built by the [Constructive](https://constructive.io) team.
49
+
50
+ ## Disclaimer
51
+
52
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
53
+
54
+ 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.
55
+
56
+ ---
57
+
58
+ ## Education and Tutorials
59
+
60
+ 1. 🚀 [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
61
+ Get started with modular databases in minutes. Install prerequisites and deploy your first module.
62
+
63
+ 2. 📦 [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
64
+ Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
65
+
66
+ 3. ✏️ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
67
+ Master the workflow for adding, organizing, and managing database changes with pgpm.
68
+
69
+ 4. 🧪 [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
70
+ Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
71
+
72
+ 5. ⚡ [Supabase Testing](https://constructive.io/learn/supabase)
73
+ Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
74
+
75
+ 6. 💧 [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
76
+ Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
77
+
78
+ 7. 🔧 [Troubleshooting](https://constructive.io/learn/troubleshooting)
79
+ Common issues and solutions for pgpm, PostgreSQL, and testing.
80
+
81
+ ## Related Constructive Tooling
82
+
83
+ ### 📦 Package Management
84
+
85
+ * [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.
86
+
87
+ ### 🧪 Testing
88
+
89
+ * [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.
90
+ * [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.
91
+ * [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.
92
+ * [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.
93
+ * [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.
94
+
95
+ ### 🧠 Parsing & AST
96
+
97
+ * [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
98
+ * [libpg-query-node](https://www.npmjs.com/package/libpg-query): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
99
+ * [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.
100
+ * [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
101
+ * [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
102
+ * [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
103
+
104
+ ## Credits
105
+
106
+ **🛠 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).**
107
+
108
+ ## Disclaimer
109
+
110
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
111
+
112
+ 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.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './migrate/orm';
package/esm/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './migrate/orm';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * GraphQL SDK - auto-generated, do not edit
3
+ * @generated by @constructive-io/graphql-codegen
4
+ */
5
+ export * from './orm';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * GraphQL SDK - auto-generated, do not edit
3
+ * @generated by @constructive-io/graphql-codegen
4
+ */
5
+ export * from './orm';
@@ -0,0 +1,55 @@
1
+ /**
2
+ * ORM Client - Runtime GraphQL executor
3
+ * @generated by @constructive-io/graphql-codegen
4
+ * DO NOT EDIT - changes will be overwritten
5
+ */
6
+ import type { GraphQLAdapter, GraphQLError, QueryResult } from '@constructive-io/graphql-types';
7
+ export type { GraphQLAdapter, GraphQLError, QueryResult } from '@constructive-io/graphql-types';
8
+ /**
9
+ * Default adapter that uses fetch for HTTP requests.
10
+ * This is used when no custom adapter is provided.
11
+ */
12
+ export declare class FetchAdapter implements GraphQLAdapter {
13
+ private endpoint;
14
+ private headers;
15
+ constructor(endpoint: string, headers?: Record<string, string>);
16
+ execute<T>(document: string, variables?: Record<string, unknown>): Promise<QueryResult<T>>;
17
+ setHeaders(headers: Record<string, string>): void;
18
+ getEndpoint(): string;
19
+ }
20
+ /**
21
+ * Configuration for creating an ORM client.
22
+ * Either provide endpoint (and optional headers) for HTTP requests,
23
+ * or provide a custom adapter for alternative execution strategies.
24
+ */
25
+ export interface OrmClientConfig {
26
+ /** GraphQL endpoint URL (required if adapter not provided) */
27
+ endpoint?: string;
28
+ /** Default headers for HTTP requests (only used with endpoint) */
29
+ headers?: Record<string, string>;
30
+ /** Custom adapter for GraphQL execution (overrides endpoint/headers) */
31
+ adapter?: GraphQLAdapter;
32
+ }
33
+ /**
34
+ * Error thrown when GraphQL request fails
35
+ */
36
+ export declare class GraphQLRequestError extends Error {
37
+ readonly errors: GraphQLError[];
38
+ readonly data: unknown;
39
+ constructor(errors: GraphQLError[], data?: unknown);
40
+ }
41
+ export declare class OrmClient {
42
+ private adapter;
43
+ constructor(config: OrmClientConfig);
44
+ execute<T>(document: string, variables?: Record<string, unknown>): Promise<QueryResult<T>>;
45
+ /**
46
+ * Set headers for requests.
47
+ * Only works if the adapter supports headers.
48
+ */
49
+ setHeaders(headers: Record<string, string>): void;
50
+ /**
51
+ * Get the endpoint URL.
52
+ * Returns empty string if the adapter doesn't have an endpoint.
53
+ */
54
+ getEndpoint(): string;
55
+ }
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Default adapter that uses fetch for HTTP requests.
3
+ * This is used when no custom adapter is provided.
4
+ */
5
+ export class FetchAdapter {
6
+ endpoint;
7
+ headers;
8
+ constructor(endpoint, headers) {
9
+ this.endpoint = endpoint;
10
+ this.headers = headers ?? {};
11
+ }
12
+ async execute(document, variables) {
13
+ const response = await fetch(this.endpoint, {
14
+ method: 'POST',
15
+ headers: {
16
+ 'Content-Type': 'application/json',
17
+ Accept: 'application/json',
18
+ ...this.headers,
19
+ },
20
+ body: JSON.stringify({
21
+ query: document,
22
+ variables: variables ?? {},
23
+ }),
24
+ });
25
+ if (!response.ok) {
26
+ return {
27
+ ok: false,
28
+ data: null,
29
+ errors: [{ message: `HTTP ${response.status}: ${response.statusText}` }],
30
+ };
31
+ }
32
+ const json = (await response.json());
33
+ if (json.errors && json.errors.length > 0) {
34
+ return {
35
+ ok: false,
36
+ data: null,
37
+ errors: json.errors,
38
+ };
39
+ }
40
+ return {
41
+ ok: true,
42
+ data: json.data,
43
+ errors: undefined,
44
+ };
45
+ }
46
+ setHeaders(headers) {
47
+ this.headers = { ...this.headers, ...headers };
48
+ }
49
+ getEndpoint() {
50
+ return this.endpoint;
51
+ }
52
+ }
53
+ /**
54
+ * Error thrown when GraphQL request fails
55
+ */
56
+ export class GraphQLRequestError extends Error {
57
+ errors;
58
+ data;
59
+ constructor(errors, data = null) {
60
+ const messages = errors.map((e) => e.message).join('; ');
61
+ super(`GraphQL Error: ${messages}`);
62
+ this.errors = errors;
63
+ this.data = data;
64
+ this.name = 'GraphQLRequestError';
65
+ }
66
+ }
67
+ export class OrmClient {
68
+ adapter;
69
+ constructor(config) {
70
+ if (config.adapter) {
71
+ this.adapter = config.adapter;
72
+ }
73
+ else if (config.endpoint) {
74
+ this.adapter = new FetchAdapter(config.endpoint, config.headers);
75
+ }
76
+ else {
77
+ throw new Error('OrmClientConfig requires either an endpoint or a custom adapter');
78
+ }
79
+ }
80
+ async execute(document, variables) {
81
+ return this.adapter.execute(document, variables);
82
+ }
83
+ /**
84
+ * Set headers for requests.
85
+ * Only works if the adapter supports headers.
86
+ */
87
+ setHeaders(headers) {
88
+ if (this.adapter.setHeaders) {
89
+ this.adapter.setHeaders(headers);
90
+ }
91
+ }
92
+ /**
93
+ * Get the endpoint URL.
94
+ * Returns empty string if the adapter doesn't have an endpoint.
95
+ */
96
+ getEndpoint() {
97
+ return this.adapter.getEndpoint?.() ?? '';
98
+ }
99
+ }
@@ -0,0 +1,48 @@
1
+ import type { OrmClientConfig } from './client';
2
+ import { MigrateFileModel } from './models/migrateFile';
3
+ import { SqlActionModel } from './models/sqlAction';
4
+ export type { OrmClientConfig, QueryResult, GraphQLError, GraphQLAdapter } from './client';
5
+ export { GraphQLRequestError } from './client';
6
+ export { QueryBuilder } from './query-builder';
7
+ export * from './select-types';
8
+ export * from './models';
9
+ export { createMutationOperations } from './mutation';
10
+ /**
11
+ * Create an ORM client instance
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const db = createClient({
16
+ * endpoint: 'https://api.example.com/graphql',
17
+ * headers: { Authorization: 'Bearer token' },
18
+ * });
19
+ *
20
+ * // Query users
21
+ * const users = await db.user.findMany({
22
+ * select: { id: true, name: true },
23
+ * first: 10,
24
+ * }).execute();
25
+ *
26
+ * // Create a user
27
+ * const newUser = await db.user.create({
28
+ * data: { name: 'John', email: 'john@example.com' },
29
+ * select: { id: true },
30
+ * }).execute();
31
+ * ```
32
+ */
33
+ export declare function createClient(config: OrmClientConfig): {
34
+ migrateFile: MigrateFileModel;
35
+ sqlAction: SqlActionModel;
36
+ mutation: {
37
+ executeSql: <S extends import("./input-types").ExecuteSqlPayloadSelect>(args: import("./mutation").ExecuteSqlVariables, options: {
38
+ select: S;
39
+ } & import("./select-types").StrictSelect<S, import("./input-types").ExecuteSqlPayloadSelect>) => import("./query-builder").QueryBuilder<{
40
+ executeSql: import("./select-types").InferSelectResult<import("./input-types").ExecuteSqlPayload, S> | null;
41
+ }>;
42
+ runMigration: <S extends import("./input-types").RunMigrationPayloadSelect>(args: import("./mutation").RunMigrationVariables, options: {
43
+ select: S;
44
+ } & import("./select-types").StrictSelect<S, import("./input-types").RunMigrationPayloadSelect>) => import("./query-builder").QueryBuilder<{
45
+ runMigration: import("./select-types").InferSelectResult<import("./input-types").RunMigrationPayload, S> | null;
46
+ }>;
47
+ };
48
+ };
@@ -0,0 +1,45 @@
1
+ /**
2
+ * ORM Client - createClient factory
3
+ * @generated by @constructive-io/graphql-codegen
4
+ * DO NOT EDIT - changes will be overwritten
5
+ */
6
+ import { OrmClient } from './client';
7
+ import { MigrateFileModel } from './models/migrateFile';
8
+ import { SqlActionModel } from './models/sqlAction';
9
+ import { createMutationOperations } from './mutation';
10
+ export { GraphQLRequestError } from './client';
11
+ export { QueryBuilder } from './query-builder';
12
+ export * from './select-types';
13
+ export * from './models';
14
+ export { createMutationOperations } from './mutation';
15
+ /**
16
+ * Create an ORM client instance
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const db = createClient({
21
+ * endpoint: 'https://api.example.com/graphql',
22
+ * headers: { Authorization: 'Bearer token' },
23
+ * });
24
+ *
25
+ * // Query users
26
+ * const users = await db.user.findMany({
27
+ * select: { id: true, name: true },
28
+ * first: 10,
29
+ * }).execute();
30
+ *
31
+ * // Create a user
32
+ * const newUser = await db.user.create({
33
+ * data: { name: 'John', email: 'john@example.com' },
34
+ * select: { id: true },
35
+ * }).execute();
36
+ * ```
37
+ */
38
+ export function createClient(config) {
39
+ const client = new OrmClient(config);
40
+ return {
41
+ migrateFile: new MigrateFileModel(client),
42
+ sqlAction: new SqlActionModel(client),
43
+ mutation: createMutationOperations(client),
44
+ };
45
+ }