Skip to content

Commit

Permalink
Show repository with current (#2917)
Browse files Browse the repository at this point in the history
* show embedeed snippet with @WithoutTenant

* Show example of @WithoutTenantId in docs
  • Loading branch information
sdelamo authored Apr 25, 2024
1 parent 3c17b5f commit d049656
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
import io.micronaut.data.annotation.TenantId;
import io.micronaut.serde.annotation.Serdeable;

@Serdeable // <1>
@MappedEntity // <2>
public record TenancyBook(@Nullable
@Id // <3>
@GeneratedValue // <4>
Long id,
@Serdeable
@MappedEntity
public record TenancyBook(@Nullable @Id @GeneratedValue Long id,
String title,
@TenantId // <5>
String framework) {
@TenantId String framework) {
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
package io.micronaut.data.jdbc.h2.multitenancy;

import io.micronaut.context.annotation.Requires;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Delete;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Status;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.scheduling.annotation.ExecuteOn;

import java.util.List;

@Requires(property = "spec.name", value = "TenancyBookControllerSpec")
@Controller("/books") // <1>
@Controller("/books")
class TenancyBookController {
private final TenancyBookRepository bookRepository;

TenancyBookController(TenancyBookRepository bookRepository) { // <2>
TenancyBookController(TenancyBookRepository bookRepository) {
this.bookRepository = bookRepository;
}

@ExecuteOn(TaskExecutors.BLOCKING) // <3>
@ExecuteOn(TaskExecutors.BLOCKING)
@Get
// <4>
List<TenancyBook> index() {
return bookRepository.findAll();
}

@ExecuteOn(TaskExecutors.BLOCKING)
@Delete
@Status(HttpStatus.NO_CONTENT)
void delete() {
bookRepository.deleteAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ class TenancyBookControllerSpec extends Specification {

given:
BlockingHttpClient client = httpClient.toBlocking()
save(bookRepository, client, "Quarkus in Action", "quarkus")
save(bookRepository, client, "Building Microservices with Micronaut", "micronaut")
save(bookRepository, client, "Introducing Micronaut", "micronaut")
save(bookRepository, client, "Grails 3 - Step by Step", "grails")
save(bookRepository, client, "Falando de Grail", "grails")
save(bookRepository, client, "Grails Goodness Notebook", "grails")

expect:
bookRepository.count() == 6

when:
List<TenancyBook> books = fetchBooks(client, "micronaut")
then:
Expand All @@ -57,10 +61,29 @@ class TenancyBookControllerSpec extends Specification {
books
books.size() == 3

cleanup:
bookRepository.deleteAll()
when:
deleteBooks(client, "micronaut")
then:
bookRepository.count() == 4

when:
deleteBooks(client, "grails")

then:
bookRepository.count() == 1

when:
bookRepository.removeAll()
then:
bookRepository.count() == 0
}

void deleteBooks(BlockingHttpClient client, String framework) {
HttpRequest<?> request = HttpRequest.DELETE("/books").header("tenantId", framework)
HttpResponse<?> response = client.exchange(request)
assertEquals(HttpStatus.NO_CONTENT, response.getStatus())

}
List<TenancyBook> fetchBooks(BlockingHttpClient client, String framework) {
HttpRequest<?> request = HttpRequest.GET("/books").header("tenantId", framework)
Argument<List<TenancyBook>> responseArgument = Argument.listOf(TenancyBook.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package io.micronaut.data.jdbc.h2.multitenancy;

import io.micronaut.context.annotation.Requires;
import io.micronaut.data.annotation.Query;
import io.micronaut.data.annotation.WithoutTenantId;
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.repository.CrudRepository;

@Requires(property = "spec.name", value = "TenancyBookControllerSpec")
@JdbcRepository(dialect = Dialect.H2) // <1>
public interface TenancyBookRepository extends CrudRepository<TenancyBook, Long> { // <2>
//tag::clazz[]
@JdbcRepository(dialect = Dialect.H2)
public interface TenancyBookRepository extends CrudRepository<TenancyBook, Long> {
Long save(String title);

@WithoutTenantId
@Override
void deleteAll();
long count();

@WithoutTenantId
void removeAll();
}
//end::clazz[]
16 changes: 0 additions & 16 deletions src/main/docs/guide/shared/multitenancy/discriminatormode.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,3 @@ When using discriminator column multitenancy, the entity with multitenancy enabl
include::doc-examples/jdbc-multitenancy-discriminator-example-java/src/main/java/example/Book.java[tags="book"]
----

There are specific annotations to alter the behaviour of repositories with api:data.annotation.TenantId[] property and its methods:

[cols=2*]
|===
|*Annotation*
|*Description*

|api:data.annotation.WithoutTenantId[]
|The method's query will not have implicit predicate to include the tenant id

|api:data.annotation.WithTenantId[]
|Modify the tenant id of the query

|===

NOTE: The tenancy annotations are only supported for the discriminator multitenancy
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
There are specific annotations to alter the behaviour of repositories with api:data.annotation.TenantId[] property and its methods:

[cols=2*]
|===
|*Annotation*
|*Description*

|api:data.annotation.WithoutTenantId[`@WithTenantId`]
|The method's query will not have implicit predicate to include the tenant id

|api:data.annotation.WithTenantId[`@WithTenantId`]
|Modify the tenant id of the query

|===

NOTE: The tenancy annotations are only supported for the discriminator multitenancy

For example, given the following Entity:

[source,java]
----
include::data-jdbc/src/test/groovy/io/micronaut/data/jdbc/h2/multitenancy/TenancyBook.java[]
----

The following repository's `removeAll` method is annotated with the `@WithoutTenantId` annotation.

[source,java]
----
include::data-jdbc/src/test/groovy/io/micronaut/data/jdbc/h2/multitenancy/TenancyBookRepository.java[tags=clazz]
----


The method `deleteAll`, inherited from `CrudRepository`, executes a query such as: `DELETE FROM tenancy_book WHERE framework = `.

However, the method `removeAll` will execute a query such as: `DELETE FROM tenancy_book`
4 changes: 3 additions & 1 deletion src/main/docs/guide/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ shared:
kotlinCriteria: Kotlin Criteria API extensions
multitenancy:
title: Multi-tenancy
discriminatormode: Discriminator Mode
discriminatormode:
title: Discriminator Mode
multitenancyannotations: Multi-tenancy annotations
multitenancymodes: Multitenancy modes
datasourcemode: DataSource Mode
schemamode: Schema Mode
Expand Down

0 comments on commit d049656

Please sign in to comment.