@prisma-next/target-postgres 0.4.0-dev.6 → 0.4.0-dev.8

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,140 @@
1
+ import type {
2
+ MigrationScaffoldContext,
3
+ OperationDescriptor,
4
+ } from '@prisma-next/framework-components/control';
5
+ import { relative } from 'pathe';
6
+
7
+ /**
8
+ * Postgres's internal plan shape. Happens to be descriptor-shaped today — that
9
+ * is a target-internal choice; the framework has no opinion.
10
+ */
11
+ type PostgresPlan = readonly OperationDescriptor[];
12
+
13
+ function serializeQueryInput(input: unknown): string {
14
+ if (typeof input === 'boolean') return String(input);
15
+ if (typeof input === 'symbol') return 'TODO /* fill in using db.sql.from(...) */';
16
+ if (input === null || input === undefined) return 'null';
17
+ if (Array.isArray(input)) {
18
+ if (input.length === 0) return '[]';
19
+ if (input.every((item) => typeof item === 'symbol'))
20
+ return '[TODO /* fill in using db.sql.from(...) */]';
21
+ return `[${input.map(serializeQueryInput).join(', ')}]`;
22
+ }
23
+ return JSON.stringify(input);
24
+ }
25
+
26
+ function renderDescriptor(desc: OperationDescriptor): string {
27
+ switch (desc.kind) {
28
+ case 'createTable':
29
+ return `createTable(${JSON.stringify(desc['table'])})`;
30
+ case 'dropTable':
31
+ return `dropTable(${JSON.stringify(desc['table'])})`;
32
+ case 'addColumn': {
33
+ const args = [JSON.stringify(desc['table']), JSON.stringify(desc['column'])];
34
+ if (desc['overrides']) {
35
+ args.push(JSON.stringify(desc['overrides']));
36
+ }
37
+ return `addColumn(${args.join(', ')})`;
38
+ }
39
+ case 'dropColumn':
40
+ return `dropColumn(${JSON.stringify(desc['table'])}, ${JSON.stringify(desc['column'])})`;
41
+ case 'alterColumnType': {
42
+ const opts: Record<string, unknown> = {};
43
+ if (desc['using']) opts['using'] = desc['using'];
44
+ if (desc['toType']) opts['toType'] = desc['toType'];
45
+ const hasOpts = Object.keys(opts).length > 0;
46
+ return hasOpts
47
+ ? `alterColumnType(${JSON.stringify(desc['table'])}, ${JSON.stringify(desc['column'])}, ${JSON.stringify(opts)})`
48
+ : `alterColumnType(${JSON.stringify(desc['table'])}, ${JSON.stringify(desc['column'])})`;
49
+ }
50
+ case 'setNotNull':
51
+ return `setNotNull(${JSON.stringify(desc['table'])}, ${JSON.stringify(desc['column'])})`;
52
+ case 'dropNotNull':
53
+ return `dropNotNull(${JSON.stringify(desc['table'])}, ${JSON.stringify(desc['column'])})`;
54
+ case 'setDefault':
55
+ return `setDefault(${JSON.stringify(desc['table'])}, ${JSON.stringify(desc['column'])})`;
56
+ case 'dropDefault':
57
+ return `dropDefault(${JSON.stringify(desc['table'])}, ${JSON.stringify(desc['column'])})`;
58
+ case 'addPrimaryKey':
59
+ return `addPrimaryKey(${JSON.stringify(desc['table'])})`;
60
+ case 'addUnique':
61
+ return `addUnique(${JSON.stringify(desc['table'])}, ${JSON.stringify(desc['columns'])})`;
62
+ case 'addForeignKey':
63
+ return `addForeignKey(${JSON.stringify(desc['table'])}, ${JSON.stringify(desc['columns'])})`;
64
+ case 'dropConstraint':
65
+ return `dropConstraint(${JSON.stringify(desc['table'])}, ${JSON.stringify(desc['constraintName'])})`;
66
+ case 'createIndex':
67
+ return `createIndex(${JSON.stringify(desc['table'])}, ${JSON.stringify(desc['columns'])})`;
68
+ case 'dropIndex':
69
+ return `dropIndex(${JSON.stringify(desc['table'])}, ${JSON.stringify(desc['indexName'])})`;
70
+ case 'createEnumType':
71
+ return desc['values']
72
+ ? `createEnumType(${JSON.stringify(desc['typeName'])}, ${JSON.stringify(desc['values'])})`
73
+ : `createEnumType(${JSON.stringify(desc['typeName'])})`;
74
+ case 'addEnumValues':
75
+ return `addEnumValues(${JSON.stringify(desc['typeName'])}, ${JSON.stringify(desc['values'])})`;
76
+ case 'dropEnumType':
77
+ return `dropEnumType(${JSON.stringify(desc['typeName'])})`;
78
+ case 'renameType':
79
+ return `renameType(${JSON.stringify(desc['fromName'])}, ${JSON.stringify(desc['toName'])})`;
80
+ case 'createDependency':
81
+ return `createDependency(${JSON.stringify(desc['dependencyId'])})`;
82
+ case 'dataTransform':
83
+ return `dataTransform(${JSON.stringify(desc['name'])}, {\n check: ${serializeQueryInput(desc['check'])},\n run: ${serializeQueryInput(desc['run'])},\n })`;
84
+ default:
85
+ throw new Error(`Unknown Postgres descriptor kind: ${desc.kind}`);
86
+ }
87
+ }
88
+
89
+ function renderPreamble(
90
+ plan: PostgresPlan,
91
+ context: MigrationScaffoldContext,
92
+ ): ReadonlyArray<string> {
93
+ const hasDataTransform = plan.some((d) => d.kind === 'dataTransform');
94
+
95
+ if (hasDataTransform && context.contractJsonPath) {
96
+ const relativeContractDts = relative(context.packageDir, context.contractJsonPath).replace(
97
+ /\.json$/,
98
+ '.d',
99
+ );
100
+ const importList = [...new Set(plan.map((d) => d.kind))];
101
+ importList.push('TODO');
102
+ return [
103
+ `import type { Contract } from "${relativeContractDts}"`,
104
+ `import { createBuilders } from "@prisma-next/target-postgres/migration-builders"`,
105
+ '',
106
+ `const { ${importList.join(', ')} } = createBuilders<Contract>()`,
107
+ ];
108
+ }
109
+
110
+ const importList = [...new Set(plan.map((d) => d.kind))];
111
+ if (importList.length === 0) {
112
+ importList.push('createTable');
113
+ }
114
+ if (hasDataTransform) {
115
+ importList.push('TODO');
116
+ }
117
+ return [
118
+ `import { ${importList.join(', ')} } from "@prisma-next/target-postgres/migration-builders"`,
119
+ ];
120
+ }
121
+
122
+ /**
123
+ * Render a Postgres descriptor list to a `migration.ts` source string.
124
+ *
125
+ * Internal to the Postgres target — no longer part of the framework SPI.
126
+ * Invoked from two paths:
127
+ * - the migrations capability's `renderDescriptorTypeScript` hook (called by
128
+ * the CLI after `planWithDescriptors` to seed an editable authoring
129
+ * surface, and by `emptyMigration()` to produce the `migration new` stub);
130
+ * - directly by this package's unit tests.
131
+ */
132
+ export function renderDescriptorTypeScript(
133
+ descriptors: readonly OperationDescriptor[],
134
+ context: MigrationScaffoldContext,
135
+ ): string {
136
+ const preamble = renderPreamble(descriptors, context);
137
+ const calls = descriptors.map((d) => ` ${renderDescriptor(d)},`);
138
+ const body = calls.length > 0 ? `\n${calls.join('\n')}\n` : '';
139
+ return [...preamble, '', `export default () => [${body}]`, ''].join('\n');
140
+ }
@@ -17,7 +17,6 @@ import { verifySqlSchema } from '@prisma-next/family-sql/schema-verify';
17
17
  import type { TargetBoundComponentDescriptor } from '@prisma-next/framework-components/components';
18
18
  import type {
19
19
  ControlTargetInstance,
20
- MigrationPlanner,
21
20
  MigrationRunner,
22
21
  OperationDescriptor,
23
22
  } from '@prisma-next/framework-components/control';
@@ -33,6 +32,7 @@ import { createPostgresMigrationPlanner } from '../core/migrations/planner';
33
32
  import { renderDefaultLiteral } from '../core/migrations/planner-ddl-builders';
34
33
  import type { PostgresPlanTargetDetails } from '../core/migrations/planner-target-details';
35
34
  import { createPostgresMigrationRunner } from '../core/migrations/runner';
35
+ import { renderDescriptorTypeScript } from '../core/migrations/scaffolding';
36
36
 
37
37
  function parseDescriptors(descriptors: readonly OperationDescriptor[]) {
38
38
  const result = MigrationDescriptorArraySchema([...descriptors]);
@@ -127,7 +127,7 @@ const postgresTargetDescriptor: SqlControlTargetDescriptor<'postgres', PostgresP
127
127
  ...postgresTargetDescriptorMeta,
128
128
  migrations: {
129
129
  createPlanner(_family: SqlControlFamilyInstance) {
130
- return createPostgresMigrationPlanner() as MigrationPlanner<'sql', 'postgres'>;
130
+ return createPostgresMigrationPlanner();
131
131
  },
132
132
  createRunner(family) {
133
133
  return createPostgresMigrationRunner(family) as MigrationRunner<'sql', 'postgres'>;
@@ -199,6 +199,9 @@ const postgresTargetDescriptor: SqlControlTargetDescriptor<'postgres', PostgresP
199
199
  db,
200
200
  });
201
201
  },
202
+ renderDescriptorTypeScript(descriptors, context) {
203
+ return renderDescriptorTypeScript(descriptors, context);
204
+ },
202
205
  },
203
206
  create(): ControlTargetInstance<'sql', 'postgres'> {
204
207
  return {
@@ -1,10 +1 @@
1
- import type { CodecTypes } from '@prisma-next/adapter-postgres/codec-types';
2
- import type { TargetPackRef } from '@prisma-next/framework-components/components';
3
- import { postgresTargetDescriptorMeta } from '../core/descriptor-meta';
4
-
5
- const postgresPack = postgresTargetDescriptorMeta;
6
-
7
- export default postgresPack as typeof postgresTargetDescriptorMeta &
8
- TargetPackRef<'sql', 'postgres'> & {
9
- readonly __codecTypes?: CodecTypes;
10
- };
1
+ export { postgresTargetDescriptorMeta as default } from '../core/descriptor-meta';
@@ -1,32 +0,0 @@
1
- //#region src/core/authoring.ts
2
- const postgresAuthoringTypes = { enum: {
3
- kind: "typeConstructor",
4
- args: [{ kind: "string" }, { kind: "stringArray" }],
5
- output: {
6
- codecId: "pg/enum@1",
7
- nativeType: {
8
- kind: "arg",
9
- index: 0
10
- },
11
- typeParams: { values: {
12
- kind: "arg",
13
- index: 1
14
- } }
15
- }
16
- } };
17
-
18
- //#endregion
19
- //#region src/core/descriptor-meta.ts
20
- const postgresTargetDescriptorMeta = {
21
- kind: "target",
22
- familyId: "sql",
23
- targetId: "postgres",
24
- id: "postgres",
25
- version: "0.0.1",
26
- capabilities: {},
27
- authoring: { type: postgresAuthoringTypes }
28
- };
29
-
30
- //#endregion
31
- export { postgresTargetDescriptorMeta as t };
32
- //# sourceMappingURL=descriptor-meta-CAf16lsJ.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"descriptor-meta-CAf16lsJ.mjs","names":[],"sources":["../src/core/authoring.ts","../src/core/descriptor-meta.ts"],"sourcesContent":["import type { AuthoringTypeNamespace } from '@prisma-next/framework-components/authoring';\n\nexport const postgresAuthoringTypes = {\n enum: {\n kind: 'typeConstructor',\n args: [{ kind: 'string' }, { kind: 'stringArray' }],\n output: {\n codecId: 'pg/enum@1',\n nativeType: { kind: 'arg', index: 0 },\n typeParams: {\n values: { kind: 'arg', index: 1 },\n },\n },\n },\n} as const satisfies AuthoringTypeNamespace;\n","import { postgresAuthoringTypes } from './authoring';\n\nexport const postgresTargetDescriptorMeta = {\n kind: 'target',\n familyId: 'sql',\n targetId: 'postgres',\n id: 'postgres',\n version: '0.0.1',\n capabilities: {},\n authoring: {\n type: postgresAuthoringTypes,\n },\n} as const;\n"],"mappings":";AAEA,MAAa,yBAAyB,EACpC,MAAM;CACJ,MAAM;CACN,MAAM,CAAC,EAAE,MAAM,UAAU,EAAE,EAAE,MAAM,eAAe,CAAC;CACnD,QAAQ;EACN,SAAS;EACT,YAAY;GAAE,MAAM;GAAO,OAAO;GAAG;EACrC,YAAY,EACV,QAAQ;GAAE,MAAM;GAAO,OAAO;GAAG,EAClC;EACF;CACF,EACF;;;;ACZD,MAAa,+BAA+B;CAC1C,MAAM;CACN,UAAU;CACV,UAAU;CACV,IAAI;CACJ,SAAS;CACT,cAAc,EAAE;CAChB,WAAW,EACT,MAAM,wBACP;CACF"}
package/dist/pack.mjs.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"pack.mjs","names":[],"sources":["../src/exports/pack.ts"],"sourcesContent":["import type { CodecTypes } from '@prisma-next/adapter-postgres/codec-types';\nimport type { TargetPackRef } from '@prisma-next/framework-components/components';\nimport { postgresTargetDescriptorMeta } from '../core/descriptor-meta';\n\nconst postgresPack = postgresTargetDescriptorMeta;\n\nexport default postgresPack as typeof postgresTargetDescriptorMeta &\n TargetPackRef<'sql', 'postgres'> & {\n readonly __codecTypes?: CodecTypes;\n };\n"],"mappings":";;;AAIA,MAAM,eAAe;AAErB,mBAAe"}