@prisma-next/target-postgres 0.3.0-dev.3 → 0.3.0-dev.30

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.
@@ -0,0 +1,144 @@
1
+ export interface SqlStatement {
2
+ readonly sql: string;
3
+ readonly params: readonly unknown[];
4
+ }
5
+
6
+ export const ensurePrismaContractSchemaStatement: SqlStatement = {
7
+ sql: 'create schema if not exists prisma_contract',
8
+ params: [],
9
+ };
10
+
11
+ export const ensureMarkerTableStatement: SqlStatement = {
12
+ sql: `create table if not exists prisma_contract.marker (
13
+ id smallint primary key default 1,
14
+ core_hash text not null,
15
+ profile_hash text not null,
16
+ contract_json jsonb,
17
+ canonical_version int,
18
+ updated_at timestamptz not null default now(),
19
+ app_tag text,
20
+ meta jsonb not null default '{}'
21
+ )`,
22
+ params: [],
23
+ };
24
+
25
+ export const ensureLedgerTableStatement: SqlStatement = {
26
+ sql: `create table if not exists prisma_contract.ledger (
27
+ id bigserial primary key,
28
+ created_at timestamptz not null default now(),
29
+ origin_core_hash text,
30
+ origin_profile_hash text,
31
+ destination_core_hash text not null,
32
+ destination_profile_hash text,
33
+ contract_json_before jsonb,
34
+ contract_json_after jsonb,
35
+ operations jsonb not null
36
+ )`,
37
+ params: [],
38
+ };
39
+
40
+ export interface WriteMarkerInput {
41
+ readonly coreHash: string;
42
+ readonly profileHash: string;
43
+ readonly contractJson?: unknown;
44
+ readonly canonicalVersion?: number | null;
45
+ readonly appTag?: string | null;
46
+ readonly meta?: Record<string, unknown>;
47
+ }
48
+
49
+ export function buildWriteMarkerStatements(input: WriteMarkerInput): {
50
+ readonly insert: SqlStatement;
51
+ readonly update: SqlStatement;
52
+ } {
53
+ const params: readonly unknown[] = [
54
+ 1,
55
+ input.coreHash,
56
+ input.profileHash,
57
+ jsonParam(input.contractJson),
58
+ input.canonicalVersion ?? null,
59
+ input.appTag ?? null,
60
+ jsonParam(input.meta ?? {}),
61
+ ];
62
+
63
+ return {
64
+ insert: {
65
+ sql: `insert into prisma_contract.marker (
66
+ id,
67
+ core_hash,
68
+ profile_hash,
69
+ contract_json,
70
+ canonical_version,
71
+ updated_at,
72
+ app_tag,
73
+ meta
74
+ ) values (
75
+ $1,
76
+ $2,
77
+ $3,
78
+ $4::jsonb,
79
+ $5,
80
+ now(),
81
+ $6,
82
+ $7::jsonb
83
+ )`,
84
+ params,
85
+ },
86
+ update: {
87
+ sql: `update prisma_contract.marker set
88
+ core_hash = $2,
89
+ profile_hash = $3,
90
+ contract_json = $4::jsonb,
91
+ canonical_version = $5,
92
+ updated_at = now(),
93
+ app_tag = $6,
94
+ meta = $7::jsonb
95
+ where id = $1`,
96
+ params,
97
+ },
98
+ };
99
+ }
100
+
101
+ export interface LedgerInsertInput {
102
+ readonly originCoreHash?: string | null;
103
+ readonly originProfileHash?: string | null;
104
+ readonly destinationCoreHash: string;
105
+ readonly destinationProfileHash?: string | null;
106
+ readonly contractJsonBefore?: unknown;
107
+ readonly contractJsonAfter?: unknown;
108
+ readonly operations: unknown;
109
+ }
110
+
111
+ export function buildLedgerInsertStatement(input: LedgerInsertInput): SqlStatement {
112
+ return {
113
+ sql: `insert into prisma_contract.ledger (
114
+ origin_core_hash,
115
+ origin_profile_hash,
116
+ destination_core_hash,
117
+ destination_profile_hash,
118
+ contract_json_before,
119
+ contract_json_after,
120
+ operations
121
+ ) values (
122
+ $1,
123
+ $2,
124
+ $3,
125
+ $4,
126
+ $5::jsonb,
127
+ $6::jsonb,
128
+ $7::jsonb
129
+ )`,
130
+ params: [
131
+ input.originCoreHash ?? null,
132
+ input.originProfileHash ?? null,
133
+ input.destinationCoreHash,
134
+ input.destinationProfileHash ?? null,
135
+ jsonParam(input.contractJsonBefore),
136
+ jsonParam(input.contractJsonAfter),
137
+ jsonParam(input.operations),
138
+ ],
139
+ };
140
+ }
141
+
142
+ function jsonParam(value: unknown): string {
143
+ return JSON.stringify(value ?? null);
144
+ }
@@ -0,0 +1,56 @@
1
+ import type {
2
+ ControlTargetInstance,
3
+ MigrationPlanner,
4
+ MigrationRunner,
5
+ } from '@prisma-next/core-control-plane/types';
6
+ import type {
7
+ SqlControlFamilyInstance,
8
+ SqlControlTargetDescriptor,
9
+ } from '@prisma-next/family-sql/control';
10
+ import { postgresTargetDescriptorMeta } from '../core/descriptor-meta';
11
+ import type { PostgresPlanTargetDetails } from '../core/migrations/planner';
12
+ import { createPostgresMigrationPlanner } from '../core/migrations/planner';
13
+ import { createPostgresMigrationRunner } from '../core/migrations/runner';
14
+
15
+ /**
16
+ * Postgres target descriptor for CLI config.
17
+ */
18
+ const postgresTargetDescriptor: SqlControlTargetDescriptor<'postgres', PostgresPlanTargetDetails> =
19
+ {
20
+ ...postgresTargetDescriptorMeta,
21
+ /**
22
+ * Migrations capability for CLI to access planner/runner via core types.
23
+ * The SQL-specific planner/runner types are compatible with the generic
24
+ * MigrationPlanner/MigrationRunner interfaces at runtime.
25
+ */
26
+ migrations: {
27
+ createPlanner(_family: SqlControlFamilyInstance) {
28
+ return createPostgresMigrationPlanner() as MigrationPlanner<'sql', 'postgres'>;
29
+ },
30
+ createRunner(family) {
31
+ return createPostgresMigrationRunner(family) as MigrationRunner<'sql', 'postgres'>;
32
+ },
33
+ },
34
+ create(): ControlTargetInstance<'sql', 'postgres'> {
35
+ return {
36
+ familyId: 'sql',
37
+ targetId: 'postgres',
38
+ };
39
+ },
40
+ /**
41
+ * Direct method for SQL-specific usage.
42
+ * @deprecated Use migrations.createPlanner() for CLI compatibility.
43
+ */
44
+ createPlanner(_family: SqlControlFamilyInstance) {
45
+ return createPostgresMigrationPlanner();
46
+ },
47
+ /**
48
+ * Direct method for SQL-specific usage.
49
+ * @deprecated Use migrations.createRunner() for CLI compatibility.
50
+ */
51
+ createRunner(family) {
52
+ return createPostgresMigrationRunner(family);
53
+ },
54
+ };
55
+
56
+ export default postgresTargetDescriptor;
@@ -0,0 +1,6 @@
1
+ import type { TargetPackRef } from '@prisma-next/contract/framework-components';
2
+ import { postgresTargetDescriptorMeta } from '../core/descriptor-meta';
3
+
4
+ const postgresPack: TargetPackRef<'sql', 'postgres'> = postgresTargetDescriptorMeta;
5
+
6
+ export default postgresPack;
@@ -0,0 +1,29 @@
1
+ import type {
2
+ RuntimeTargetDescriptor,
3
+ RuntimeTargetInstance,
4
+ } from '@prisma-next/core-execution-plane/types';
5
+ import { postgresTargetDescriptorMeta } from '../core/descriptor-meta';
6
+
7
+ /**
8
+ * Postgres runtime target instance interface.
9
+ */
10
+ export interface PostgresRuntimeTargetInstance extends RuntimeTargetInstance<'sql', 'postgres'> {}
11
+
12
+ /**
13
+ * Postgres target descriptor for runtime plane.
14
+ */
15
+ const postgresRuntimeTargetDescriptor: RuntimeTargetDescriptor<
16
+ 'sql',
17
+ 'postgres',
18
+ PostgresRuntimeTargetInstance
19
+ > = {
20
+ ...postgresTargetDescriptorMeta,
21
+ create(): PostgresRuntimeTargetInstance {
22
+ return {
23
+ familyId: 'sql',
24
+ targetId: 'postgres',
25
+ };
26
+ },
27
+ };
28
+
29
+ export default postgresRuntimeTargetDescriptor;
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/core/descriptor-meta.ts"],"sourcesContent":["export const postgresTargetDescriptorMeta = {\n kind: 'target',\n familyId: 'sql',\n targetId: 'postgres',\n id: 'postgres',\n version: '0.0.1',\n capabilities: {},\n} as const;\n"],"mappings":";AAAO,IAAM,+BAA+B;AAAA,EAC1C,MAAM;AAAA,EACN,UAAU;AAAA,EACV,UAAU;AAAA,EACV,IAAI;AAAA,EACJ,SAAS;AAAA,EACT,cAAc,CAAC;AACjB;","names":[]}