Skip to content

Commit

Permalink
[ksqlDb.RestApi.Client]: applied Ignore from model builder during cod…
Browse files Browse the repository at this point in the history
…e generation example and doc
  • Loading branch information
tomasfabian committed Apr 11, 2024
1 parent 9730512 commit 2103f0d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,19 @@ var decimalTypeConvention = new DecimalTypeConvention(14, 14);
builder.AddConvention(decimalTypeConvention);

builder.Entity<Payment>()
.HasKey(c => c.Id)
.Property(b => b.Amount)
.Decimal(precision: 10, scale: 2);

builder.Entity<Payment>()
.Property(b => b.Description)
.Ignore();

builder.Entity<Account>()
.HasKey(c => c.Id);
```

C# entity definitions:

```C#
record Payment
{
Expand All @@ -384,6 +392,31 @@ record Account
}
```

Usage with ksqlDB REST API Client:

```C#
await kSqlDbRestApiClient.CreateTypeAsync<Payment>(cancellationToken);

var entityCreationMetadata = new EntityCreationMetadata(kafkaTopic: nameof(Account), partitions: 3)
{
Replicas = 3
};
responseMessage = await restApiProvider.CreateTableAsync<Account>(entityCreationMetadata, true, cancellationToken);
```

Generated KSQL:

```SQL
CREATE TYPE Payment AS STRUCT<Id VARCHAR, Amount DECIMAL(10,2)>;

CREATE TABLE IF NOT EXISTS Accounts (
Id VARCHAR PRIMARY KEY,
Balance DECIMAL(14,14)
) WITH ( KAFKA_TOPIC='Account', VALUE_FORMAT='Json', PARTITIONS='3', REPLICAS='3' );
```

The `Description` field in the `Payment` entity is ignored during code generation, and the `Id` field in the `Account` entity is marked as the **primary key**.

### Aggregation functions
List of supported ksqldb [aggregation functions](https://github.com/tomasfabian/ksqlDB.RestApi.Client-DotNet/blob/main/docs/aggregations.md):
- [GROUP BY](https://github.com/tomasfabian/ksqlDB.RestApi.Client-DotNet/blob/main/docs/aggregations.md#groupby)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public async Task InitModelAndCreateStreamAsync(CancellationToken cancellationTo

var responseMessage = await restApiProvider.CreateTableAsync<Payment>(entityCreationMetadata, true, cancellationToken);
var content = await responseMessage.Content.ReadAsStringAsync(cancellationToken);

entityCreationMetadata = new EntityCreationMetadata(kafkaTopic: nameof(Account), partitions: 1)
{
Replicas = 1
};
responseMessage = await restApiProvider.CreateTableAsync<Account>(entityCreationMetadata, true, cancellationToken);
content = await responseMessage.Content.ReadAsStringAsync(cancellationToken);
}

private IKSqlDbRestApiClient ConfigureRestApiClientWithServicesCollection(ServiceCollection serviceCollection, ModelBuilder builder)
Expand Down
37 changes: 31 additions & 6 deletions docs/modelbuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ namespace ksqlDB.RestApi.Client.Samples.Model
.HasKey(c => c.Id)
.Property(b => b.Balance);

builder.Entity<Account>()
.Property(b => b.Secret)
.Ignore();

builder.Entity<Payment>()
.HasKey(c => c.Id)
.Property(b => b.Amount)
.Decimal(precision: 10, scale: 2);

builder.Entity<Payment>()
.Property(b => b.Secret)
.Ignore();

var httpClient = new HttpClient
{
Expand All @@ -39,10 +39,16 @@ namespace ksqlDB.RestApi.Client.Samples.Model
var httpClientFactory = new HttpClientFactory(httpClient);
var restApiProvider = new KSqlDbRestApiClient(httpClientFactory, builder);

var entityCreationMetadata = new EntityCreationMetadata(kafkaTopic: nameof(Payment), partitions: 1);
var entityCreationMetadata = new EntityCreationMetadata(kafkaTopic: nameof(Payment), partitions: 3);

var responseMessage = await restApiProvider.CreateTableAsync<Payment>(entityCreationMetadata, true, cancellationToken);
var content = await responseMessage.Content.ReadAsStringAsync(cancellationToken);

entityCreationMetadata = new EntityCreationMetadata(kafkaTopic: nameof(Account), partitions: 1)
{
Replicas = 3
};
responseMessage = await restApiProvider.CreateTableAsync<Account>(entityCreationMetadata, true, cancellationToken);
}
}
}
Expand All @@ -54,13 +60,13 @@ private record Payment
public string Id { get; set; } = null!;
public decimal Amount { get; set; }
public string Description { get; set; } = null!;
public string Secret { get; set; } = null!;
}

private record Account
{
public string Id { get; set; } = null!;
public decimal Balance { get; set; }
public string Secret { get; set; } = null!;
}
```

Expand All @@ -76,6 +82,25 @@ private record Account

`Ignore()`: A method used to specify that a particular property of an entity should be ignored during code generation.

The `Id` column was designated as the **primary key** for the `Payments` table, reflecting its configuration through the model builder's `HasKey` method for the entity:

```SQL
CREATE TABLE IF NOT EXISTS Payments (
Id VARCHAR PRIMARY KEY,
Amount DECIMAL(10,2),
Description VARCHAR
) WITH ( KAFKA_TOPIC='Payment', VALUE_FORMAT='Json', PARTITIONS='3' );
```

The `Secret` column was excluded from the generated DDL statement due to its configuration using the model builder's 'Ignore' method:

```SQL
CREATE TABLE IF NOT EXISTS Accounts (
Id VARCHAR PRIMARY KEY,
Balance DECIMAL
) WITH ( KAFKA_TOPIC='Account', VALUE_FORMAT='Json', PARTITIONS='1', REPLICAS='3' );
```

## IFromItemTypeConfiguration

To apply configurations using the provided `ModelBuilder`, follow these steps:
Expand Down

0 comments on commit 2103f0d

Please sign in to comment.