Skip to content

Commit 1b677e0

Browse files
committed
chore: make datasource url optional
1 parent a2aff81 commit 1b677e0

File tree

2 files changed

+10
-36
lines changed

2 files changed

+10
-36
lines changed

packages/language/src/validators/datasource-validator.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,16 @@ export default class DataSourceValidator implements AstValidator<DataSource> {
4141
}
4242

4343
private validateUrl(ds: DataSource, accept: ValidationAcceptor) {
44-
const url = ds.fields.find((f) => f.name === 'url');
45-
if (!url) {
46-
accept('error', 'datasource must include a "url" field', {
47-
node: ds,
48-
});
44+
const urlField = ds.fields.find((f) => f.name === 'url');
45+
if (!urlField) {
46+
return;
4947
}
5048

51-
for (const fieldName of ['url', 'shadowDatabaseUrl']) {
52-
const field = ds.fields.find((f) => f.name === fieldName);
53-
if (!field) {
54-
continue;
55-
}
56-
const value = getStringLiteral(field.value);
57-
if (!value && !(isInvocationExpr(field.value) && field.value.function.ref?.name === 'env')) {
58-
accept('error', `"${fieldName}" must be set to a string literal or an invocation of "env" function`, {
59-
node: field.value,
60-
});
61-
}
49+
const value = getStringLiteral(urlField.value);
50+
if (!value && !(isInvocationExpr(urlField.value) && urlField.value.function.ref?.name === 'env')) {
51+
accept('error', `"${urlField.name}" must be set to a string literal or an invocation of "env" function`, {
52+
node: urlField.value,
53+
});
6254
}
6355
}
6456

packages/sdk/src/ts-schema-generator.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -559,32 +559,14 @@ export class TsSchemaGenerator {
559559
);
560560
}
561561

562-
private getDataSourceProvider(
563-
model: Model,
564-
): { type: string; env: undefined; url: string } | { type: string; env: string; url: undefined } {
562+
private getDataSourceProvider(model: Model) {
565563
const dataSource = model.declarations.find(isDataSource);
566564
invariant(dataSource, 'No data source found in the model');
567565

568566
const providerExpr = dataSource.fields.find((f) => f.name === 'provider')?.value;
569567
invariant(isLiteralExpr(providerExpr), 'Provider must be a literal');
570568
const type = providerExpr.value as string;
571-
572-
const urlExpr = dataSource.fields.find((f) => f.name === 'url')?.value;
573-
invariant(isLiteralExpr(urlExpr) || isInvocationExpr(urlExpr), 'URL must be a literal or env function');
574-
575-
if (isLiteralExpr(urlExpr)) {
576-
return { type, url: urlExpr.value as string, env: undefined };
577-
} else if (isInvocationExpr(urlExpr)) {
578-
invariant(urlExpr.function.$refText === 'env', 'only "env" function is supported');
579-
invariant(urlExpr.args.length === 1, 'env function must have one argument');
580-
return {
581-
type,
582-
env: (urlExpr.args[0]!.value as LiteralExpr).value as string,
583-
url: undefined,
584-
};
585-
} else {
586-
throw new Error('Unsupported URL type');
587-
}
569+
return { type };
588570
}
589571

590572
private getFieldMappedDefault(

0 commit comments

Comments
 (0)