@prisma-next/adapter-postgres 0.13.0-dev.2 → 0.13.0-dev.21
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/dist/{adapter-CAlWA4ug.mjs → adapter-CoRrSTXZ.mjs} +3 -3
- package/dist/adapter-CoRrSTXZ.mjs.map +1 -0
- package/dist/adapter.d.mts +1 -1
- package/dist/adapter.d.mts.map +1 -1
- package/dist/adapter.mjs +1 -1
- package/dist/{control-adapter-ZWrjGBq7.mjs → control-adapter-BeSqyOGl.mjs} +209 -116
- package/dist/control-adapter-BeSqyOGl.mjs.map +1 -0
- package/dist/control.d.mts +23 -12
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +3 -3
- package/dist/control.mjs.map +1 -1
- package/dist/{descriptor-meta-NBwpqHS7.mjs → descriptor-meta-DOgMfoqm.mjs} +10 -3
- package/dist/descriptor-meta-DOgMfoqm.mjs.map +1 -0
- package/dist/runtime.d.mts +1 -1
- package/dist/runtime.mjs +2 -2
- package/dist/{types-Dv7M8jx8.d.mts → types-KXRwRZU8.d.mts} +3 -3
- package/dist/types-KXRwRZU8.d.mts.map +1 -0
- package/dist/types.d.mts +1 -1
- package/package.json +22 -22
- package/src/core/adapter.ts +5 -4
- package/src/core/codec-lookup.ts +5 -5
- package/src/core/control-adapter.ts +241 -37
- package/src/core/control-codecs.ts +25 -0
- package/src/core/descriptor-meta.ts +3 -0
- package/src/core/marker-ledger.ts +2 -18
- package/src/core/sql-renderer.ts +122 -1
- package/src/core/types.ts +2 -2
- package/src/exports/control.ts +1 -0
- package/dist/adapter-CAlWA4ug.mjs.map +0 -1
- package/dist/control-adapter-ZWrjGBq7.mjs.map +0 -1
- package/dist/descriptor-meta-NBwpqHS7.mjs.map +0 -1
- package/dist/types-Dv7M8jx8.d.mts.map +0 -1
- package/src/core/ddl-renderer.ts +0 -155
package/src/core/ddl-renderer.ts
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import { REFERENTIAL_ACTION_SQL } from '@prisma-next/sql-contract/referential-action-sql';
|
|
2
|
-
import type {
|
|
3
|
-
DdlColumn,
|
|
4
|
-
DdlColumnDefaultVisitor,
|
|
5
|
-
DdlTableConstraint,
|
|
6
|
-
ForeignKeyConstraint,
|
|
7
|
-
FunctionColumnDefault,
|
|
8
|
-
LiteralColumnDefault,
|
|
9
|
-
PrimaryKeyConstraint,
|
|
10
|
-
UniqueConstraint,
|
|
11
|
-
} from '@prisma-next/sql-relational-core/ast';
|
|
12
|
-
import type {
|
|
13
|
-
PostgresCreateSchema,
|
|
14
|
-
PostgresCreateTable,
|
|
15
|
-
PostgresDdlNode,
|
|
16
|
-
PostgresDdlVisitor,
|
|
17
|
-
} from '@prisma-next/target-postgres/ddl';
|
|
18
|
-
import { escapeLiteral, quoteIdentifier } from '@prisma-next/target-postgres/sql-utils';
|
|
19
|
-
import type { PostgresLoweredStatement } from './types';
|
|
20
|
-
|
|
21
|
-
function quoteQualifiedIdentifier(name: string): string {
|
|
22
|
-
return name.split('.').map(quoteIdentifier).join('.');
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function renderPrimaryKeyConstraint(constraint: PrimaryKeyConstraint): string {
|
|
26
|
-
const cols = constraint.columns.map(quoteIdentifier).join(', ');
|
|
27
|
-
if (constraint.name !== undefined) {
|
|
28
|
-
return `CONSTRAINT ${quoteIdentifier(constraint.name)} PRIMARY KEY (${cols})`;
|
|
29
|
-
}
|
|
30
|
-
return `PRIMARY KEY (${cols})`;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function renderForeignKeyConstraint(constraint: ForeignKeyConstraint): string {
|
|
34
|
-
const cols = constraint.columns.map(quoteIdentifier).join(', ');
|
|
35
|
-
const refCols = constraint.refColumns.map(quoteIdentifier).join(', ');
|
|
36
|
-
let sql = `FOREIGN KEY (${cols}) REFERENCES ${quoteQualifiedIdentifier(constraint.refTable)} (${refCols})`;
|
|
37
|
-
if (constraint.onDelete !== undefined) {
|
|
38
|
-
sql += ` ON DELETE ${REFERENTIAL_ACTION_SQL[constraint.onDelete]}`;
|
|
39
|
-
}
|
|
40
|
-
if (constraint.onUpdate !== undefined) {
|
|
41
|
-
sql += ` ON UPDATE ${REFERENTIAL_ACTION_SQL[constraint.onUpdate]}`;
|
|
42
|
-
}
|
|
43
|
-
if (constraint.name !== undefined) {
|
|
44
|
-
sql = `CONSTRAINT ${quoteIdentifier(constraint.name)} ${sql}`;
|
|
45
|
-
}
|
|
46
|
-
return sql;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function renderUniqueConstraint(constraint: UniqueConstraint): string {
|
|
50
|
-
const cols = constraint.columns.map(quoteIdentifier).join(', ');
|
|
51
|
-
if (constraint.name !== undefined) {
|
|
52
|
-
return `CONSTRAINT ${quoteIdentifier(constraint.name)} UNIQUE (${cols})`;
|
|
53
|
-
}
|
|
54
|
-
return `UNIQUE (${cols})`;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function renderTableConstraint(constraint: DdlTableConstraint): string {
|
|
58
|
-
switch (constraint.kind) {
|
|
59
|
-
case 'primary-key':
|
|
60
|
-
return renderPrimaryKeyConstraint(constraint);
|
|
61
|
-
case 'foreign-key':
|
|
62
|
-
return renderForeignKeyConstraint(constraint);
|
|
63
|
-
case 'unique':
|
|
64
|
-
return renderUniqueConstraint(constraint);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
class PostgresDdlVisitorImpl implements PostgresDdlVisitor<string> {
|
|
69
|
-
createTable(node: PostgresCreateTable): string {
|
|
70
|
-
const ifNotExists = node.ifNotExists ? 'IF NOT EXISTS ' : '';
|
|
71
|
-
const tableRef = node.schema
|
|
72
|
-
? `${quoteIdentifier(node.schema)}.${quoteIdentifier(node.table)}`
|
|
73
|
-
: quoteIdentifier(node.table);
|
|
74
|
-
const columnDefs = node.columns.map((column) => renderColumn(column));
|
|
75
|
-
const constraintDefs =
|
|
76
|
-
node.constraints !== undefined ? node.constraints.map(renderTableConstraint) : [];
|
|
77
|
-
const allDefs = [...columnDefs, ...constraintDefs].join(',\n ');
|
|
78
|
-
return `CREATE TABLE ${ifNotExists}${tableRef} (\n ${allDefs}\n)`;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
createSchema(node: PostgresCreateSchema): string {
|
|
82
|
-
const ifNotExists = node.ifNotExists ? 'IF NOT EXISTS ' : '';
|
|
83
|
-
return `CREATE SCHEMA ${ifNotExists}${quoteIdentifier(node.schema)}`;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Postgres infers a quoted-literal default's type as `text` at parse time
|
|
88
|
-
// and applies an implicit text → target coercion at default-evaluation
|
|
89
|
-
// time. That coercion exists for some target types (text, jsonb, json)
|
|
90
|
-
// and not others (uuid, inet, timestamptz, citext, enums, user-defined
|
|
91
|
-
// types, geometry/geography from PostGIS, array types). Quoted-literal
|
|
92
|
-
// defaults on any non-text column therefore need an explicit
|
|
93
|
-
// `::<nativeType>` cast in the emitted DDL — both to state the default's
|
|
94
|
-
// intent (the literal IS that type, not text that happens to coerce) and
|
|
95
|
-
// to keep emitted migrations correct for types where no implicit cast
|
|
96
|
-
// exists. Numeric, boolean, and null literals are typed by Postgres
|
|
97
|
-
// directly (no `text` indirection) so they need no cast.
|
|
98
|
-
function isTextLikeNativeType(nativeType: string): boolean {
|
|
99
|
-
return (
|
|
100
|
-
nativeType === 'text' ||
|
|
101
|
-
nativeType === 'varchar' ||
|
|
102
|
-
nativeType.startsWith('varchar(') ||
|
|
103
|
-
nativeType === 'character varying' ||
|
|
104
|
-
nativeType.startsWith('character varying(') ||
|
|
105
|
-
nativeType === 'char' ||
|
|
106
|
-
nativeType.startsWith('char(') ||
|
|
107
|
-
nativeType === 'character' ||
|
|
108
|
-
nativeType.startsWith('character(')
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const defaultVisitor: DdlColumnDefaultVisitor<string> = {
|
|
113
|
-
literal(node: LiteralColumnDefault, ctx): string {
|
|
114
|
-
const { value } = node;
|
|
115
|
-
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
116
|
-
return `DEFAULT ${String(value)}`;
|
|
117
|
-
}
|
|
118
|
-
if (value === null) {
|
|
119
|
-
return 'DEFAULT NULL';
|
|
120
|
-
}
|
|
121
|
-
const serialized = typeof value === 'string' ? value : JSON.stringify(value);
|
|
122
|
-
const literal = `'${escapeLiteral(serialized)}'`;
|
|
123
|
-
return isTextLikeNativeType(ctx.nativeType)
|
|
124
|
-
? `DEFAULT ${literal}`
|
|
125
|
-
: `DEFAULT ${literal}::${ctx.nativeType}`;
|
|
126
|
-
},
|
|
127
|
-
function(node: FunctionColumnDefault, _ctx): string {
|
|
128
|
-
if (node.expression === 'autoincrement()') {
|
|
129
|
-
return '';
|
|
130
|
-
}
|
|
131
|
-
return `DEFAULT (${node.expression})`;
|
|
132
|
-
},
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
function renderColumn(column: DdlColumn): string {
|
|
136
|
-
const parts = [quoteIdentifier(column.name), column.type];
|
|
137
|
-
if (column.notNull) {
|
|
138
|
-
parts.push('NOT NULL');
|
|
139
|
-
}
|
|
140
|
-
if (column.primaryKey) {
|
|
141
|
-
parts.push('PRIMARY KEY');
|
|
142
|
-
}
|
|
143
|
-
const defaultClause = column.default
|
|
144
|
-
? column.default.accept(defaultVisitor, { nativeType: column.type })
|
|
145
|
-
: '';
|
|
146
|
-
if (defaultClause.length > 0) {
|
|
147
|
-
parts.push(defaultClause);
|
|
148
|
-
}
|
|
149
|
-
return parts.join(' ');
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export function renderLoweredDdl(ast: PostgresDdlNode): PostgresLoweredStatement {
|
|
153
|
-
const sql = ast.accept(new PostgresDdlVisitorImpl());
|
|
154
|
-
return Object.freeze({ sql, params: Object.freeze([]) });
|
|
155
|
-
}
|