@prisma-next/adapter-postgres 0.12.0-dev.16 → 0.12.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.
@@ -1,73 +0,0 @@
1
- import type {
2
- DdlColumn,
3
- DdlColumnDefaultVisitor,
4
- FunctionColumnDefault,
5
- LiteralColumnDefault,
6
- } from '@prisma-next/sql-relational-core/ast';
7
- import type {
8
- PostgresCreateSchema,
9
- PostgresCreateTable,
10
- PostgresDdlNode,
11
- PostgresDdlVisitor,
12
- } from '@prisma-next/target-postgres/ddl';
13
- import { escapeLiteral } from '@prisma-next/target-postgres/sql-utils';
14
- import type { PostgresLoweredStatement } from './types';
15
-
16
- class PostgresDdlVisitorImpl implements PostgresDdlVisitor<string> {
17
- createTable(node: PostgresCreateTable): string {
18
- const ifNotExists = node.ifNotExists ? 'if not exists ' : '';
19
- const tableRef = node.schema ? `${node.schema}.${node.table}` : node.table;
20
- const columnDefs = node.columns.map((column) => renderColumn(column)).join(',\n ');
21
- return `create table ${ifNotExists}${tableRef} (\n ${columnDefs}\n )`;
22
- }
23
-
24
- createSchema(node: PostgresCreateSchema): string {
25
- const ifNotExists = node.ifNotExists ? 'if not exists ' : '';
26
- return `create schema ${ifNotExists}${node.schema}`;
27
- }
28
- }
29
-
30
- const defaultVisitor: DdlColumnDefaultVisitor<string> = {
31
- literal(node: LiteralColumnDefault): string {
32
- const { value } = node;
33
- if (typeof value === 'string') {
34
- return `default '${escapeLiteral(value)}'`;
35
- }
36
- if (typeof value === 'number' || typeof value === 'boolean') {
37
- return `default ${String(value)}`;
38
- }
39
- if (value === null) {
40
- return 'default null';
41
- }
42
- return `default '${JSON.stringify(value)}'`;
43
- },
44
- function(node: FunctionColumnDefault): string {
45
- if (node.expression === 'autoincrement()') {
46
- return '';
47
- }
48
- if (node.expression === 'now()') {
49
- return 'default now()';
50
- }
51
- return `default (${node.expression})`;
52
- },
53
- };
54
-
55
- function renderColumn(column: DdlColumn): string {
56
- const parts = [column.name, column.type];
57
- if (column.notNull) {
58
- parts.push('not null');
59
- }
60
- if (column.primaryKey) {
61
- parts.push('primary key');
62
- }
63
- const defaultClause = column.default ? column.default.accept(defaultVisitor) : '';
64
- if (defaultClause.length > 0) {
65
- parts.push(defaultClause);
66
- }
67
- return parts.join(' ');
68
- }
69
-
70
- export function renderLoweredDdl(ast: PostgresDdlNode): PostgresLoweredStatement {
71
- const sql = ast.accept(new PostgresDdlVisitorImpl());
72
- return Object.freeze({ sql, params: Object.freeze([]) });
73
- }