@prisma-next/adapter-postgres 0.13.0 → 0.14.0-dev.2

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 (39) hide show
  1. package/dist/{adapter-CAlWA4ug.mjs → adapter-CwkcdpM_.mjs} +3 -3
  2. package/dist/adapter-CwkcdpM_.mjs.map +1 -0
  3. package/dist/adapter.d.mts +1 -1
  4. package/dist/adapter.d.mts.map +1 -1
  5. package/dist/adapter.mjs +1 -1
  6. package/dist/column-types.d.mts +1 -6
  7. package/dist/column-types.d.mts.map +1 -1
  8. package/dist/column-types.mjs +2 -17
  9. package/dist/column-types.mjs.map +1 -1
  10. package/dist/{control-adapter-ZWrjGBq7.mjs → control-adapter-Dspz5uKp.mjs} +227 -226
  11. package/dist/control-adapter-Dspz5uKp.mjs.map +1 -0
  12. package/dist/control.d.mts +25 -22
  13. package/dist/control.d.mts.map +1 -1
  14. package/dist/control.mjs +3 -3
  15. package/dist/control.mjs.map +1 -1
  16. package/dist/{descriptor-meta-NBwpqHS7.mjs → descriptor-meta-DOgMfoqm.mjs} +10 -3
  17. package/dist/descriptor-meta-DOgMfoqm.mjs.map +1 -0
  18. package/dist/runtime.d.mts +1 -1
  19. package/dist/runtime.mjs +2 -2
  20. package/dist/{types-Dv7M8jx8.d.mts → types-KXRwRZU8.d.mts} +3 -3
  21. package/dist/types-KXRwRZU8.d.mts.map +1 -0
  22. package/dist/types.d.mts +1 -1
  23. package/package.json +22 -22
  24. package/src/core/adapter.ts +5 -4
  25. package/src/core/codec-lookup.ts +5 -5
  26. package/src/core/control-adapter.ts +261 -86
  27. package/src/core/control-codecs.ts +25 -0
  28. package/src/core/descriptor-meta.ts +3 -0
  29. package/src/core/marker-ledger.ts +2 -18
  30. package/src/core/sql-renderer.ts +146 -8
  31. package/src/core/types.ts +2 -2
  32. package/src/exports/column-types.ts +0 -20
  33. package/src/exports/control.ts +1 -0
  34. package/dist/adapter-CAlWA4ug.mjs.map +0 -1
  35. package/dist/control-adapter-ZWrjGBq7.mjs.map +0 -1
  36. package/dist/descriptor-meta-NBwpqHS7.mjs.map +0 -1
  37. package/dist/types-Dv7M8jx8.d.mts.map +0 -1
  38. package/src/core/ddl-renderer.ts +0 -155
  39. package/src/core/enum-control-hooks.ts +0 -141
@@ -1,141 +0,0 @@
1
- import type { SqlControlDriverInstance } from '@prisma-next/sql-contract/types';
2
- import { PG_ENUM_CODEC_ID } from '@prisma-next/target-postgres/codec-ids';
3
-
4
- /**
5
- * Codec-typed annotation shape that the introspector writes under
6
- * `schema.annotations.pg.storageTypes[<typeName>]`. Distinct from
7
- * `StorageTypeInstance` because the introspector emits a plain literal
8
- * (no class-instance API surface): only the fields downstream consumers
9
- * actually read from the introspected envelope.
10
- */
11
- export interface PostgresEnumStorageTypeAnnotation {
12
- readonly codecId: typeof PG_ENUM_CODEC_ID;
13
- readonly nativeType: string;
14
- readonly typeParams: { readonly values: readonly string[] };
15
- }
16
-
17
- /**
18
- * Postgres enum introspection.
19
- *
20
- * Migration planning and schema verification for enum types live at the
21
- * SQL family layer + the Postgres target's planner-strategies layer (see
22
- * `nativeEnumPlanCallStrategy` and the family-level `verifyEnumType`
23
- * walk). Introspection is the only piece that remains here because the
24
- * control adapter still calls into a codec-keyed dispatch surface
25
- * (`storage.types` is rebuilt from this map in `control-adapter.ts`);
26
- * the introspector returns the codec-typed shape that downstream
27
- * `Contract` consumers expect.
28
- */
29
- type EnumRow = {
30
- schema_name: string;
31
- type_name: string;
32
- values: string[];
33
- };
34
-
35
- const ENUM_INTROSPECT_QUERY = `
36
- SELECT
37
- n.nspname AS schema_name,
38
- t.typname AS type_name,
39
- array_agg(e.enumlabel ORDER BY e.enumsortorder) AS values
40
- FROM pg_type t
41
- JOIN pg_namespace n ON t.typnamespace = n.oid
42
- JOIN pg_enum e ON t.oid = e.enumtypid
43
- WHERE n.nspname = $1
44
- GROUP BY n.nspname, t.typname
45
- ORDER BY n.nspname, t.typname
46
- `;
47
-
48
- function isStringArray(value: unknown): value is string[] {
49
- return Array.isArray(value) && value.every((entry) => typeof entry === 'string');
50
- }
51
-
52
- /**
53
- * Parses a PostgreSQL array value into a JavaScript string array.
54
- *
55
- * The `pg` library returns `array_agg` results either as a JS array
56
- * (when type parsers are configured) or as a string in PostgreSQL array
57
- * literal format (`{value1,value2,...}`). Handles PG's quoting rules:
58
- * - Elements containing commas, quotes, backslashes, or whitespace are
59
- * double-quoted.
60
- * - Inside quoted elements, `\"` represents `"` and `\\` represents `\`.
61
- *
62
- * Returns `null` when the input cannot be parsed as a PG array.
63
- */
64
- export function parsePostgresArray(value: unknown): string[] | null {
65
- if (isStringArray(value)) {
66
- return value;
67
- }
68
- if (typeof value === 'string' && value.startsWith('{') && value.endsWith('}')) {
69
- const inner = value.slice(1, -1);
70
- if (inner === '') {
71
- return [];
72
- }
73
- return parseArrayElements(inner);
74
- }
75
- return null;
76
- }
77
-
78
- function parseArrayElements(input: string): string[] {
79
- const result: string[] = [];
80
- let i = 0;
81
- while (i < input.length) {
82
- if (input[i] === ',') {
83
- i++;
84
- continue;
85
- }
86
- if (input[i] === '"') {
87
- i++;
88
- let element = '';
89
- while (i < input.length && input[i] !== '"') {
90
- if (input[i] === '\\' && i + 1 < input.length) {
91
- i++;
92
- element += input[i];
93
- } else {
94
- element += input[i];
95
- }
96
- i++;
97
- }
98
- i++;
99
- result.push(element);
100
- } else {
101
- const nextComma = input.indexOf(',', i);
102
- if (nextComma === -1) {
103
- result.push(input.slice(i).trim());
104
- i = input.length;
105
- } else {
106
- result.push(input.slice(i, nextComma).trim());
107
- i = nextComma;
108
- }
109
- }
110
- }
111
- return result;
112
- }
113
-
114
- /**
115
- * Reads enum types from the live Postgres schema and returns them in
116
- * the codec-typed annotation shape consumed by `control-adapter.ts`
117
- * (which writes them under `schema.annotations.pg.storageTypes`).
118
- */
119
- export async function introspectPostgresEnumTypes(options: {
120
- readonly driver: SqlControlDriverInstance<'postgres'>;
121
- readonly schemaName?: string;
122
- }): Promise<Record<string, PostgresEnumStorageTypeAnnotation>> {
123
- const namespace = options.schemaName ?? 'public';
124
- const result = await options.driver.query<EnumRow>(ENUM_INTROSPECT_QUERY, [namespace]);
125
- const types: Record<string, PostgresEnumStorageTypeAnnotation> = {};
126
- for (const row of result.rows) {
127
- const values = parsePostgresArray(row.values);
128
- if (!values) {
129
- throw new Error(
130
- `Failed to parse enum values for type "${row.type_name}": ` +
131
- `unexpected format: ${JSON.stringify(row.values)}`,
132
- );
133
- }
134
- types[row.type_name] = {
135
- codecId: PG_ENUM_CODEC_ID,
136
- nativeType: row.type_name,
137
- typeParams: { values },
138
- };
139
- }
140
- return types;
141
- }