-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show repository with current (#2917)
* show embedeed snippet with @WithoutTenant * Show example of @WithoutTenantId in docs
- Loading branch information
Showing
7 changed files
with
89 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 13 additions & 4 deletions
17
data-jdbc/src/test/groovy/io/micronaut/data/jdbc/h2/multitenancy/TenancyBookController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 9 additions & 4 deletions
13
data-jdbc/src/test/groovy/io/micronaut/data/jdbc/h2/multitenancy/TenancyBookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...n/docs/guide/shared/multitenancy/discriminatormode/multitenancyannotations.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters