#provide service and reuse the method inside guards for example

1 messages · Page 1 of 1 (latest)

long frigate
#

Using this :

    service = await anext(
        provide_users_service(
            db_config.provide_session(connection.app.state, connection.scope)
        )
    )

should reuse loading property defined inside provide_service method declared like that :

async def provide_users_service(
    db_session: AsyncSession,
) -> AsyncGenerator[UserService, None]:
    async with UserService.new(session=db_session, load=select(User).options(selectinload(User.roles))) as service:
        yield service
cold fogBOT
#
Notes for provide service and reuse the method inside guards for example
At your assistance

@long frigate

No Response?

If no response in a reasonable time, ping @Member.

Closing

To close, type !solve or byte solve.

MCVE

Please include an MCVE so that we can reproduce your issue locally.

digital skiff
#

that should work though, are you saying it doesnt?

long frigate
long frigate
#

I need to pass load=["*"]

    service = await anext(
        provide_users_service(
            db_config.provide_session(connection.app.state, connection.scope)
        )
    )

    user = await service.get_one_or_none(
        email=token.sub, load=["*"]
    )
```to make it work. Without it, it will not lmoad roles
digital skiff
#

@long onyx (whenever you are free)

long onyx
#

There's an ask to change this behavior

#

Basically, if you provide a load option, it's additive on top of the default lazy loads

#

the ask is for us to wipe out all lazy loads if the user provides any load options

#

It doesn't have to be load='*' fwiw, you can override the lazy load with something lese

long frigate
# long onyx It doesn't have to be `load='*'` fwiw, you can override the lazy load with somet...

😮 I understand well tho

I've designed firstly with SQLA models definition using lazy options in relationship but fall into "noload" using services to load only what i want and understand correclty my calls

So if I understand correctly, even if I specify inside the "provide_user_service" a specific load, I have to call load ="something" everytime i call this service using this method ?

long onyx
#

I'm not sure i'm understanding. You should use load when you want to modify the default lazy load relationships. In your case, it sounds like all of them are set to noload by default. So, in this case, you'd have to specify the specific joins you want (or use load='*'). * may not be exactly what you want because it makes things a joined load.

Let's say you have a model with several lazy load relationships and you only need 1 of them. You could do this to disable all of the lazy configs: load=[noload('*'), selectinload(Model.column)]

long frigate
#

For all my models (subclass of UUIDAuditBase), I use for relationship, the lazy parameter lazy="noload"
It help me a lot to find error of loading ex : sqlalchemy.exc.MissingGreenlet:

So I adjust try to adjust then the comportement inside service declaration load

I will rethink this tho

long onyx
#

well, there's a couple of ways i can resolve this for you at the repo level

#
  1. we implement the load removal i mentioned above
#
  1. we implement a "default_load_options" for the repo
#
  1. implement both of the above