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.

Middleware for Google NDB

See original GitHub issue

I am trying to create a middleware for Google NDB. It requires the use of an context / the wrapping of the actual code within a “with” statement. It is recommended to create a single context for each HTTP Request. E.g.:

app = FastAPI()
client = ndb.Client()

class Contact(ndb.Model):
    name = ndb.StringProperty()
    phone = ndb.StringProperty()
    email = ndb.StringProperty()

@app.get("/create")
def create():
    with client.context():
        contact1 = Contact(name="John Smith",
                           phone="555 617 8993",
                           email="john.smith@gmail.com")
        contact1.put()

I tried to implement the “with-wrapping” within a middleware:

@app.middleware("http")
async def add_datastore(request: Request, call_next):
    with client.context():
        response = await call_next(request)
        return response

@app.get("/create_no_ctx")
def create_no_ctx():
    contact1 = Contact(name="John Smith",
                       phone="555 617 8993",
                       email="john.smith@gmail.com")
    contact1.put()

This middleware has no effect. Calling the create_no_ctx route yields a NDB-Exception that no context was set. Is is possible to create a middleware that wraps all route functions in a with statement with the context?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
ndavydovdevcommented, Aug 3, 2020

I created a pull request with the fix according to https://www.python.org/dev/peps/pep-0567/#converting-code-that-uses-threading-local It really works fine and I hope it will be merged

1reaction
phy25commented, Feb 14, 2020

Opps this is a won’t fix on their end because of possible alternatives: https://github.com/googleapis/python-ndb/issues/289

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrating to Cloud NDB - App Engine
Instead, you can easily write your own middleware with just a few lines of code. App Engine NDB required apps and the Datastore...
Read more >
Django Middleware — ndb documentation - Google Cloud
Django middleware for ndb . class google.cloud.ndb.django_middleware. NdbDjangoMiddleware (*args, **kwargs)[source]¶. Bases: object ...
Read more >
How to configure google app engine datastore (NDB) with ...
I am trying to use google datastore (NDB) with Django. This is the error I get when I try to put an entity....
Read more >
NDB overview docs show incorrect Django middleware path ...
The Django section in https://developers.google.com/appengine/docs/python/ndb/overview has a reference to the middleware that needs adding to settings.py.
Read more >
ModelForms for ndb model appengine (djangoforms not ...
Apart from that you need to add a middleware: 'google.appengine.ext.ndb.NdbDjangoMiddleware'. Doing the above two will fix things.
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