Skip to content

Issue deploying with shared database resources #39

@chimon2000

Description

@chimon2000

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions