question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Future-proof way to access a global DB object

See original GitHub issue

Hi,

I am trying to pass a db connection objection to all my routes (on demand) and my first guess would be an equivalent of current_app object like we do in flask.

I have seen issues #226 and #374 discussing similar concerns but I am not sure if there is any definite way to do it. And by saying definite I mean to not break our universe in a future version (potentially v1.0.0)

I would say request['app'] is very intriguing to use but I would like to know your thoughts first

Cheers, Periklis

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
tomchristiecommented, Oct 7, 2019

I think what you’re looking for is request.state.

You can include a middleware class that attaches a mongo connection instance (or whatever else) to the request, which will then be available anywhere you’re passing the request around.

If you need that to work lazily, then you’d need the object that you’re attaching to deal with only acquiring the connection at the first point it’s actually used.

1reaction
tomchristiecommented, Oct 7, 2019

Passing a database instance that’s declared once, globally, will do what you need here. Have a look over https://www.starlette.io/database/

This example service that I’m building out also takes the same approach, so might be a good place to look.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kafka, JSON, DevOps: Future Proof Your IBM i With Secure ...
APIs allow outside applications to access data and functions in real time. Many IBM i users are replacing FTP file transfers, EDI and...
Read more >
Does your document database provide these 3 critical ...
Oracle Autonomous JSON Database is part of Oracle Autonomous Database ... Ensure you future-proof your applications today! ... Enter JSON.
Read more >
PHP objects and shared database connection - Stack Overflow
I'd say this makes for a better more future proof approach to database access in php oop scripts. Share.
Read more >
Doing Power BI the Right Way: 1. Futureproofing Power BI ...
Power Query or Power BI Dataflows · Define the technical audience · Establish solution ownership · Continuation plan (if they were to leave?)...
Read more >
Accessing Session-Global Data | Framework | Vaadin 8 Docs
Applications typically need to access some objects from practically all user interface code, such as a user object, a business data model, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found