Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue deploying with shared database resources #39

Open
chimon2000 opened this issue Sep 23, 2024 · 1 comment
Open

Issue deploying with shared database resources #39

chimon2000 opened this issue Sep 23, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@chimon2000
Copy link
Contributor

chimon2000 commented Sep 23, 2024

Bug Report

Issue

Upon deploying, I see the following runtime error when attempting to hit an endpoint that pulls data from the SQL Database:

gRPC Error (code: 9, codeName: FAILED_PRECONDITION, message: NITRIC_DATABASE_BASE_URL environment variable not set, details: [], rawResponse: null, trailers: {})

This occurs because I define a shared global resource for the database accessed inside a method handler.

final db = Nitric.sql('db');
final api = Nitric.api('api');

void main(List<String> arguments) {
  api.get("/hello", (ctx) async {
    /// Causes database resource to be created
    await db.connectionString();
    return ctx;
  });
}

The hypothesis posed by @tjholm is that globally defined variables are lazily evaluated. We could verify this by reassigning the database resource inside of main:

final db = Nitric.sql('db');
final api = Nitric.api('api');

void main(List<String> arguments) {
  /// Causes database resource to be created
  final myDb = db;

  api.get("/hello", (ctx) async {
    await myDb.connectionString();
    return ctx;
  });
}

Other resources such as the previously defined api don't have this problem because they are initialized or referenced directly inside of the main function for each service.

Steps

Steps to reproduce the behavior:

Attempt to deploy the following project: https://github.com/chimon2000/db-deploy-issue

Expected

The database is created and seeded before the API is referenced.

Environment and setup information

  • Nitric CLI Version: 1.53.0
  • Operating System: MacOS
  • Nitric dependencies and their versions, such as SDKs or Middleware: Dart SDK (v1.4.0)
  • Cloud providers you are deploying to and their version: AWS

Other info

Conversation thread: https://discord.com/channels/955259353043173427/999455789146181683/1287084311916445727

@chimon2000 chimon2000 added the bug Something isn't working label Sep 23, 2024
@tjholm
Copy link
Member

tjholm commented Nov 5, 2024

@chimon2000 I think the pattern you've used in: https://github.com/chimon2000/notes_nitric, is a good demonstration of database sharing can be achieved for Dart.

Applying recommendations for Option 2 in: nitrictech/cli#812 and updating the Dart guides for resource sharing are probably the best way to address this.

@tjholm tjholm self-assigned this Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants