Implements: IdentifiableSqlitePersistence[T,K]
Description
Important points
- The data items must implement the IIdentifiable interface.
- The JSON table has only two fields: id and data.
- In basic scenarios, child classes shall only override GetPageByFilter, GetListByFilter or DeleteByFilter operations with a specific filter function.
- All other operations can be used out of the box.
- In complex scenarios, child classes can implement additional operations by accessing c._collection and c._model properties.
Configuration parameters
- collection: (optional) SQLite collection name
- connection(s):
- discovery_key: (optional) a key to retrieve the connection from IDiscovery
- host: host name or IP address
- port: port number (default: 27017)
- uri: resource URI or connection string with all parameters in it
- credential(s):
- store_key: (optional) a key to retrieve the credentials from ICredentialStore
- username: (optional) user name
- password: (optional) user password
- options:
- connect_timeout: (optional) number of milliseconds to wait before timing out when connecting a new client (default: 0)
- idle_timeout: (optional) number of milliseconds a client must sit idle in the pool and not be checked out (default: 10000)
- max_pool_size: (optional) maximum number of clients the pool should contain (default: 10)
References
- *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
- *:discovery:*:*:1.0 - (optional) IDiscovery services
- *:credential-store:*:*:1.0 - (optional) ICredentialStore to resolve credentials
Constructors
InheritIdentifiableJsonSqlitePersistence
Creates a new instance of the persistence component.
InheritIdentifiableJsonSqlitePersistence[T any, K any](overrides ISqlitePersistenceOverrides[T], tableName string) [*IdentifiableJsonSqlitePersistence]()
- overrides: ISqlitePersistenceOverrides[T] - references to child class that overrides virtual methods
- tableName: string - (optional) a table name.
Methods
ConvertFromPublic
Converts an object value from public to internal format.
(c *IdentifiableJsonSqlitePersistence[T,K]) ConvertFromPublic(value T) (map[string]any, error)
- value: T - object in public format to convert.
- returns: map[string]any - converted object in internal format.
ConvertToPublic
Converts an object value from internal to public format.
(c *IdentifiableJsonSqlitePersistence[T,K]) ConvertToPublic(item *sql.Rows) (T, error)
- rows: *sql.Rows - object in internal format to convert.
- returns: (T, error) - converted object in public format.
EnsureTable
Adds a DML statement to automatically create a JSON(B) table
(c *IdentifiableJsonSqlitePersistence[T,K]) EnsureTable(idType string, dataType string)
- idType: string - type of the id column (default: VARCHAR(32))
- dataType: string - type of the data column (default: JSON)
UpdatePartially
Updates only a few selected fields in a data item.
(c *IdentifiableJsonSqlitePersistence[T,K]) UpdatePartially(ctx context.Context, correlationId string, id K, data cdata.AnyValueMap) (result T, err error)
- ctx: context.Context - operation context.
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
- id: K - id of the data item to be updated.
- data: AnyValueMap - map with fields to be updated.
- return: (result T, err error) - updated item
Examples
type DummyJsonSqlitePersistence struct {
*persist.IdentifiableJsonSqlitePersistence[fixtures.Dummy, string]
}
func NewDummyJsonSqlitePersistence() *DummyJsonSqlitePersistence {
c := &DummyJsonSqlitePersistence{}
c.IdentifiableJsonSqlitePersistence = persist.InheritIdentifiableJsonSqlitePersistence[fixtures.Dummy, string](c, "dummies_json")
return c
}
func (c *DummyJsonSqlitePersistence) DefineSchema() {
c.ClearSchema()
c.IdentifiableJsonSqlitePersistence.DefineSchema()
c.EnsureTable("", "")
c.EnsureIndex(c.TableName+"_key", map[string]string{"(data->'key')": "1"}, map[string]string{"unique": "true"})
}
func (c *DummyJsonSqlitePersistence) GetPageByFilter(ctx context.Context, correlationId string,
filter cdata.FilterParams, paging cdata.PagingParams) (page cdata.DataPage[fixtures.Dummy], err error) {
key, ok := filter.GetAsNullableString("Key")
filterObj := ""
if ok && key != "" {
filterObj += "JSON_EXTRACT(data, '$.key')='" + key + "'"
}
return c.IdentifiableJsonSqlitePersistence.GetPageByFilter(ctx, correlationId,
filterObj, paging,
"", "",
)
}
func (c *DummyJsonSqlitePersistence) GetCountByFilter(ctx context.Context, correlationId string,
filter cdata.FilterParams) (count int64, err error) {
filterObj := ""
if key, ok := filter.GetAsNullableString("Key"); ok && key != "" {
filterObj += "JSON_EXTRACT(data, '$.key')='" + key + "'"
}
return c.IdentifiableJsonSqlitePersistence.GetCountByFilter(ctx, correlationId, filterObj)
}
func (c *DummyJsonSqlitePersistence) GetOneRandom(ctx context.Context, correlationId string) (item fixtures.Dummy, err error) {
return c.IdentifiableJsonSqlitePersistence.GetOneRandom(ctx, correlationId, "")
}