mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 05:52:51 +00:00
# Which Problems Are Solved As part of going to a relation storage model, we need to implement the repository for handling sessions and their events. # How the Problems Are Solved - Adds a migration step to create the necessary tables and types in the database. - Adds the repository implementation for sessions. - Implements a projection handler for session events to handle the events written by the old command handlers. - Updates the `Session` type and `SessionRepository` interface: - added `CreatorID` - Adjusted `Delete` method to return amount of affected columns - Removed sub-repository clumns from the main interface - Added `CreatorIDCondition`, `ExpirationCondition`, `ExistsFactor` and `ExistsMetadata` - Removed `SetUserAgent` - Methods of `SessionFactor` and `SessionChallenge` have been made public - fixed enumer on `SessionFactorType` - Adds a `JSON` type to the repository package to easy can JSON columns, resp. jsonb object inside a property of a `JSONArray` object. - Adds a `NoChange` interface, which can be implemented to signal that a `Change` interface implementation is not writing any changes to the statement builder, e.g. `CTEChange` # Additional Changes - Fixes the operators enums. `TextOperations` had wrong comments and `NumberOperations` additionally also had from comparisons. - Fixes downgrade of migration step 008. # Additional Context - closes https://github.com/zitadel/zitadel/issues/10212
49 lines
993 B
Go
49 lines
993 B
Go
package repository
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
|
)
|
|
|
|
type JSON[T any] struct {
|
|
Value T
|
|
}
|
|
|
|
func (j *JSON[T]) Scan(src any) (err error) {
|
|
var rawJSON []byte
|
|
switch s := src.(type) {
|
|
case string:
|
|
rawJSON = []byte(s)
|
|
case []byte:
|
|
rawJSON = s
|
|
case nil:
|
|
return nil
|
|
default:
|
|
return ErrScanSource
|
|
}
|
|
if err = j.UnmarshalJSON(rawJSON); err != nil {
|
|
return database.NewScanError(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UnmarshalJSON allows encoding/json to decode nested JSON into JSON[T] fields.
|
|
// This makes [json.Unmarshal] work for fields inside aggregated JSON (e.g. JSON arrays).
|
|
func (j *JSON[T]) UnmarshalJSON(data []byte) error {
|
|
if len(data) == 0 || string(data) == "null" {
|
|
return nil
|
|
}
|
|
var v T
|
|
if err := json.Unmarshal(data, &v); err != nil {
|
|
return err
|
|
}
|
|
j.Value = v
|
|
return nil
|
|
}
|
|
|
|
// MarshalJSON encodes the wrapped value or null if nil.
|
|
func (j JSON[T]) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(j.Value)
|
|
}
|