mirror of
https://github.com/VinGarcia/ksql.git
synced 2025-09-04 19:36:56 +00:00
Updated Modifiers (markdown)
parent
3a6fbedac9
commit
5b03bfdd7f
@ -1,3 +1,7 @@
|
||||
## Scanner and Valuer support
|
||||
|
||||
Since all arguments passed to the `Query*` and `Exec` functions are forwarded directly to the underlying database driver (e.g. `database/sql` or `pgx`) if the underlying implementation supports the `sql.Scanner` and `sql.Valuer` interfaces KSQL will also support it.
|
||||
|
||||
## What is a KSQL Modifier?
|
||||
|
||||
A KSQL modifier is a special tag you can add to any of the attributes of your struct to alter the behavior of KSQL when reading or writing that attribute into the database. To use it, it is necessary to add the name of the modifier on the `ksql` tag after a comma, e.g.:
|
||||
@ -6,6 +10,14 @@ A KSQL modifier is a special tag you can add to any of the attributes of your st
|
||||
`ksql:"column_name,json"`
|
||||
```
|
||||
|
||||
### Why Modifiers?
|
||||
|
||||
KSQL Modifiers play a similar role to the `sql.Scanner` and `sql.Valuer` interfaces: It gives you an easier way of having a custom serialization behavior for a specific attribute whenever you read/write it from/to the database.
|
||||
|
||||
The important difference is that it is more reusable: A single modifier can be reused in many different attributes of different types, while in a similar situation, an implementation of the `Scan` and `Value` methods would need to be provided for each type of attribute.
|
||||
|
||||
### Using KSQL Modifiers
|
||||
|
||||
KSQL comes with a few built-in Modifiers and an API for creating your own modifiers if necessary.
|
||||
These will be discussed in more detail in the next sections.
|
||||
|
||||
@ -26,10 +38,10 @@ type User struct {
|
||||
|
||||
> Please note that each attribute can only accept **one and only one** modifier at most.
|
||||
> So where I use `timeNowUTC/skipUpdates` in the example above I am not using
|
||||
> a special syntax, it is just the full name of the modifier.
|
||||
> a special syntax, that's just the full name of the modifier.
|
||||
>
|
||||
> It is a long name, but the idea is to make it as descriptive as possible
|
||||
> for avoiding interpretation errors.
|
||||
> for avoiding interpretation mistakes.
|
||||
|
||||
Applying a Modifier to a struct attribute will cause KSQL to use this modifier when Inserting, Updating, and Querying that field.
|
||||
|
||||
@ -82,4 +94,8 @@ func init() {
|
||||
}
|
||||
```
|
||||
|
||||
This registration should be performed inside your code before making any calls to the KSQL library. I recommend doing it inside a `init()` function since then it will run before `main()` starts.
|
||||
This registration should be performed inside your code before making any calls to the KSQL library. I recommend doing it inside a `init()` function since then it will run before `main()` starts.
|
||||
|
||||
### Modifiers vs `sql.Scanner` and `sql.Valuer` interfaces
|
||||
|
||||
When using a Modifier, if that Modifier specifies a `Scan` function and the type of the attribute implements the `sql.Scanner` interface the method implementation will be ignored in favor of the Modifier implementation. The same is valid for the Modifier Value function when the type implements the `sql.Valuer` interface.
|
Loading…
x
Reference in New Issue
Block a user