在ReactiveCrudRepository中禁用写操作

huangapple go评论48阅读模式
英文:

Disable Write operation in ReactiveCrudRepository

问题

在我的情况下,由于某种原因,我需要从不同原因拥有的表中读取数据。在我的应用程序中,我希望禁用对表的写入。我认为的一种解决方法是继承ReactiveCrudRepository并创建一个在写入/更新操作上引发异常的接口。

是否有其他干净和可维护的方法可用于这种用例?

英文:

In my case, due to some reason, I need to read data from the tables owned by different reason. In my application, I want to disable write on the table. One solution I think is to inherit ReactiveCrudRepository and create an interface which throws exception on write/update operation.

Is there any other clean and maintainable method available for such use-cases?

答案1

得分: 1

第一选项

创建一个只读用户(具有仅读取权限)在数据库中,并使用此用户从您的应用程序连接到数据库。

第二选项

如果第一选项在您的情况下不合适,则:

创建一个通用存储库,其中只包含用于读取的方法,如下所示:(从Repository扩展,因为它不包含任何方法)

@NoRepositoryBean
public interface ReadOnlyReactiveRepository<T, ID> extends Repository<T, ID> {

    Flux<T> findAll();

    Mono<T> findById(ID id);

    ... 你想要的任何方法
}

注意: 不要忘记用@NoRepositoryBean注释此通用存储库,该注释用于避免为实际上符合存储库接口的标准但不打算成为存储库的接口创建存储库代理。

然后从该存储库扩展您的域存储库,而不是从ReactiveCrudRepository扩展,如下所示:

public interface YourDomainRepository extends ReadOnlyReactiveRepository<YourDomain, String> {

}
英文:

I see two options here:


First option

Create a read-only user (which has permissions only for reading) in DB and connect to DB from your application with this user.


Second option

If the first option is not suitable in your case, then:

Create a generic repository which has only methods for reading like this: (extend it from Repository as it does not contain any method)

@NoRepositoryBean
public interface ReadOnlyReactiveRepository&lt;T, ID&gt; extends Repository&lt;T, ID&gt; {

    Flux&lt;T&gt; findAll();

    Mono&lt;T&gt; findById(ID id);
   
    ... any methods you want
}

Note: Dont forget to annotate this generic repository with @NoRepositoryBean which is used to avoid creating repository proxies for interfaces that actually match the criteria of a repo interface but are not intended to be one.

And extend your domain repository from that repository, not from ReactiveCrudRepository like this:

public interface YourDomainRepository extends ReadOnlyReactiveRepository&lt;YourDomain, String&gt; {
    
}

huangapple
  • 本文由 发表于 2023年2月14日 18:43:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定