Skip to content

Commit

Permalink
fix: add directUrl support to schema datasource decl (zenstackhq#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored May 28, 2023
1 parent e76ee35 commit 8b29eed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/schema/src/plugins/prisma/prisma-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ export class PrismaModel {
name: string,
provider: string,
url: DataSourceUrl,
directUrl?: DataSourceUrl,
shadowDatabaseUrl?: DataSourceUrl,
restFields: SimpleField[] = []
): DataSource {
const ds = new DataSource(name, provider, url, shadowDatabaseUrl, restFields);
const ds = new DataSource(name, provider, url, directUrl, shadowDatabaseUrl, restFields);
this.datasources.push(ds);
return ds;
}
Expand Down Expand Up @@ -57,6 +58,7 @@ export class DataSource {
public name: string,
public provider: string,
public url: DataSourceUrl,
public directUrl?: DataSourceUrl,
public shadowDatabaseUrl?: DataSourceUrl,
public restFields: SimpleField[] = []
) {}
Expand All @@ -70,6 +72,7 @@ export class DataSource {
`datasource ${this.name} {\n` +
indentString(`provider="${this.provider}"\n`) +
indentString(`url=${this.url}\n`) +
(this.directUrl ? indentString(`directUrl=${this.directUrl}\n`) : '') +
(this.shadowDatabaseUrl ? indentString(`shadowDatabaseurl=${this.shadowDatabaseUrl}\n`) : '') +
(restFields ? restFields + '\n' : '') +
`}`
Expand Down
14 changes: 12 additions & 2 deletions packages/schema/src/plugins/prisma/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export default class PrismaSchemaGenerator {
private generateDataSource(prisma: PrismaModel, dataSource: DataSource) {
let provider: string | undefined = undefined;
let url: PrismaDataSourceUrl | undefined = undefined;
let directUrl: PrismaDataSourceUrl | undefined = undefined;
let shadowDatabaseUrl: PrismaDataSourceUrl | undefined = undefined;
const restFields: SimpleField[] = [];

Expand All @@ -143,10 +144,19 @@ export default class PrismaSchemaGenerator {
break;
}

case 'directUrl': {
const r = this.extractDataSourceUrl(f.value);
if (!r) {
throw new PluginError(name, 'Invalid value for directUrl');
}
directUrl = r;
break;
}

case 'shadowDatabaseUrl': {
const r = this.extractDataSourceUrl(f.value);
if (!r) {
throw new PluginError(name, 'Invalid value for datasource url');
throw new PluginError(name, 'Invalid value for shadowDatabaseUrl');
}
shadowDatabaseUrl = r;
break;
Expand Down Expand Up @@ -175,7 +185,7 @@ export default class PrismaSchemaGenerator {
throw new PluginError(name, 'Datasource is missing "url" field');
}

prisma.addDataSource(dataSource.name, provider, url, shadowDatabaseUrl, restFields);
prisma.addDataSource(dataSource.name, provider, url, directUrl, shadowDatabaseUrl, restFields);
}

private extractDataSourceUrl(fieldValue: LiteralExpr | InvocationExpr | ArrayExpr) {
Expand Down

0 comments on commit 8b29eed

Please sign in to comment.