From 1b677e07d04ab6883f0a7b09d10bfc351c3cd5d8 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Mon, 4 Aug 2025 17:14:19 +0800 Subject: [PATCH] chore: make datasource url optional --- .../src/validators/datasource-validator.ts | 24 +++++++------------ packages/sdk/src/ts-schema-generator.ts | 22 ++--------------- 2 files changed, 10 insertions(+), 36 deletions(-) diff --git a/packages/language/src/validators/datasource-validator.ts b/packages/language/src/validators/datasource-validator.ts index c2540426..84302785 100644 --- a/packages/language/src/validators/datasource-validator.ts +++ b/packages/language/src/validators/datasource-validator.ts @@ -41,24 +41,16 @@ export default class DataSourceValidator implements AstValidator { } private validateUrl(ds: DataSource, accept: ValidationAcceptor) { - const url = ds.fields.find((f) => f.name === 'url'); - if (!url) { - accept('error', 'datasource must include a "url" field', { - node: ds, - }); + const urlField = ds.fields.find((f) => f.name === 'url'); + if (!urlField) { + return; } - for (const fieldName of ['url', 'shadowDatabaseUrl']) { - const field = ds.fields.find((f) => f.name === fieldName); - if (!field) { - continue; - } - const value = getStringLiteral(field.value); - if (!value && !(isInvocationExpr(field.value) && field.value.function.ref?.name === 'env')) { - accept('error', `"${fieldName}" must be set to a string literal or an invocation of "env" function`, { - node: field.value, - }); - } + const value = getStringLiteral(urlField.value); + if (!value && !(isInvocationExpr(urlField.value) && urlField.value.function.ref?.name === 'env')) { + accept('error', `"${urlField.name}" must be set to a string literal or an invocation of "env" function`, { + node: urlField.value, + }); } } diff --git a/packages/sdk/src/ts-schema-generator.ts b/packages/sdk/src/ts-schema-generator.ts index 1f55db3e..dd8fb49d 100644 --- a/packages/sdk/src/ts-schema-generator.ts +++ b/packages/sdk/src/ts-schema-generator.ts @@ -559,32 +559,14 @@ export class TsSchemaGenerator { ); } - private getDataSourceProvider( - model: Model, - ): { type: string; env: undefined; url: string } | { type: string; env: string; url: undefined } { + private getDataSourceProvider(model: Model) { const dataSource = model.declarations.find(isDataSource); invariant(dataSource, 'No data source found in the model'); const providerExpr = dataSource.fields.find((f) => f.name === 'provider')?.value; invariant(isLiteralExpr(providerExpr), 'Provider must be a literal'); const type = providerExpr.value as string; - - const urlExpr = dataSource.fields.find((f) => f.name === 'url')?.value; - invariant(isLiteralExpr(urlExpr) || isInvocationExpr(urlExpr), 'URL must be a literal or env function'); - - if (isLiteralExpr(urlExpr)) { - return { type, url: urlExpr.value as string, env: undefined }; - } else if (isInvocationExpr(urlExpr)) { - invariant(urlExpr.function.$refText === 'env', 'only "env" function is supported'); - invariant(urlExpr.args.length === 1, 'env function must have one argument'); - return { - type, - env: (urlExpr.args[0]!.value as LiteralExpr).value as string, - url: undefined, - }; - } else { - throw new Error('Unsupported URL type'); - } + return { type }; } private getFieldMappedDefault(