Inherits: SqlServerPersistence
Description
The IdentifiableSqlServerPersistence class allows you to create persistence components that store data in SQL Server databases and implement a number of CRUD operations over data items with unique ids.
Important points
- The data items must implement the IIdentifiable interface.
- In basic scenarios, child classes shall only override GetPageByFilterAsync, GetListByFilterAsync or DeleteByFilterAsync 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 this._collection and this._model properties.
Configuration parameters
- collection: (optional) SQL Server collection name
connection(s):
- discovery_key: (optional) 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) key to retrieve the credentials from ICredentialStore
- username: (optional) username
- password: (optional) user’s password
options:
- max_pool_size: (optional) maximum connection pool size (default: 2)
- keep_alive: (optional) enable connection keep alive (default: true)
- connect_timeout: (optional) connection timeout in milliseconds (default: 5 sec)
- auto_reconnect: (optional) enable auto reconnection (default: true)
- max_page_size: (optional) maximum page size (default: 100)
- debug: (optional) enable debug output (default: false).
References
- *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
- *:discovery:*:*:1.0 - (optional) IDiscovery services
- *:credential-store:*:*:1.0 - (optional) Credential stores to resolve credentials (ICredentialStore)
Constructors
Creates a new instance of the persistence component.
public
IdentifiableSqlServerPersistence(string tableName)
- tableName: string - (optional) collection name.
Instance methods
CreateAsync
Creates a data item.
public override
async Task<T> CreateAsync(IContext context, T item)
- context: IContext - (optional) a context to trace execution through a call chain.
- item: T - item to be created.
- returns: Task<T> - created item
DeleteByIdAsync
Deletes a data item by it’s unique id.
public virtual
Task<T> DeleteByIdAsync(IContext context, K id)
- context: IContext - (optional) a context to trace execution through a call chain.
- id: K - id of the item to be deleted
- returns: Task<T> - deleted item
DeleteByIdsAsync
Deletes multiple data items based on their unique ids.
public virtual
Task DeleteByIdsAsync(IContext context, K[] ids)
- context: IContext - (optional) a context to trace execution through a call chain.
- ids: K[] - ids of data items to be deleted.
GetListByIdsAsync
Gets a list of data items retrieved based on given unique ids.
public virtual
Task<List<T>> GetListByIdsAsync(IContext context, K[] ids)
- context: IContext - (optional) a context to trace execution through a call chain.
- ids: K[] - ids of data items to be retrieved
- returns: Promise<\T[]> - data list
GetOneByIdAsync
Gets a data item by its unique id.
public virtual
Task<T> GetOneByIdAsync(IContext context, K id)
- context: IContext - (optional) a context to trace execution through a call chain.
- id: K - id of data item to be retrieved.
- returns: Task<T> - data item
SetAsync
Sets a data item. If the data item exists, it updates it. Otherwise, it creates a new data item.
public virtual
Task<T> SetAsync(IContext context, T item)
- context: IContext - (optional) a context to trace execution through a call chain.
- item: T - item to be set.
- returns: Task<T> - updated item
UpdateAsync
Updates a data item.
public virtual
Task<T> UpdateAsync(IContext context, T item)
- context: IContext - (optional) a context to trace execution through a call chain.
- item: T - item to be updated.
- returns: Task<T> - updated item
UpdatePartially
Updates only a few selected fields in a data item.
public virtual
Task<T> UpdatePartially(IContext context, K id, AnyValueMap data)
- context: IContext - (optional) a context to trace execution through a call chain.
- id: K - id of data item to be updated.
- data: AnyValueMap - map with fields to be updated.
- returns: Task<T> - updated item
Examples
class MySqlServerPersistence: SqlServerPersistence<MyData, string>
{
public MySqlServerPersistence(): base("mydata") { }
private FilterDefinition<MyData> ComposeFilter(FilterParams filter)
{
filterParams = filterParams ?? new FilterParams();
var builder = Builders<BeaconV1>.Filter;
var filter = builder.Empty;
String name = filter.getAsNullableString("name");
if (name != null)
filter &= builder.Eq(b => b.Name, name);
return filter;
}
public GetPageByFilter(IContext context, FilterParams filter, PagingParams paging)
{
base.GetPageByFilter(context, this.ComposeFilter(filter), paging, null, null);
}
}
var persistence = new MySqlServerPersistence();
persistence.Configure(ConfigParams.fromTuples(
"host", "localhost",
"port", 27017 ));
persitence.Open("123");
persistence.Create("123", new MyData("1", "ABC"));
var mydata = persistence.GetPageByFilter(
"123",
FilterParams.FromTuples("name", "ABC"));
Console.Out.WriteLine(mydata.Data); // Result: { id: "1", name: "ABC" }
persistence.DeleteById("123", "1");
...