Extends: PostgresPersistence
Description
The IdentifiablePostgresPersistence class allows you to create persistence components that store data in PostgreSQL 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 the 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 this._collection and this._model properties.
Configuration parameters
- table: (optional) PostgreSQL table name
- schema: (optional) PostgreSQL schema 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:
- 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 can 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) credential stores to resolve credentials (ICredentialStore)
Constructors
Creates a new instance of the persistence component.
IdentifiablePostgresPersistence(String? tableName, String? schemaName) : super(tableName, schemaName);
- tableName: string - (optional) a table name.
- schemaName: string - (optional) a schema name.
Fields
Instance methods
convertFromPublicPartial_
Converts the given object from the public partial format.
dynamic convertFromPublicPartial_(value)
- value: the object to convert from the public partial format.
- returns: dynamic - the initial object.
create
Creates a data item.
Future<T?> create(IContext? context, T? item) async
- context: IContext - (optional) a context to trace execution through a call chain.
- item: T - item to be created.
- returns: Future<T?> - created item
deleteById
Deletes a data item by it’s unique id.
Future<T?> deleteById(IContext? context, K? id) async
- context: IContext - (optional) a context to trace execution through a call chain.
- id: K - id of the item to be deleted
- returns: Future<T?> - deleted item
deleteByIds
Deletes multiple data items by their unique ids.
Future
deleteByIds(IContext? context, List ids) async
- context: IContext - (optional) a context to trace execution through a call chain.
- ids: List
- ids of data items to be deleted.
getListByIds
Gets a list of data items retrieved by given unique ids.
Future<List
> getListByIds(IContext? context, List ids) async
- context: IContext - (optional) a context to trace execution through a call chain.
- ids: List
- ids of data items to be retrieved - returns: Future<List
> - data list
getOneById
Gets a data item by its unique id.
Future<T?> getOneById(IContext? context, K id) async
- context: IContext - (optional) a context to trace execution through a call chain.
- id: K - id of data item to be retrieved.
- returns: Future<T?> - data item
set
Sets a data item. If the data item exists it updates it. Otherwise, it creates a new data item.
Future<T?> set(IContext? context, T? item) async
- context: IContext - (optional) a context to trace execution through a call chain.
- item: T - item to be set.
- returns: Future<T?> - updated item
update
Updates a data item.
Future<T?> update(IContext? context, T? item) async
- context: IContext - (optional) a context to trace execution through a call chain.
- item: T - item to be updated.
- returns: Future<T?> - updated item
updatePartially
Updates only a few selected fields in a data item.
Future<T?> updatePartially(IContext? context, K? id, AnyValueMap? data) async
- context: IContext - (optional) a context to trace execution through a call chain.
- id: K - id of the data item to be updated.
- data: AnyValueMap - map with fields to be updated.
- returns: Future<T?> - updated item
Examples
class MyPostgresPersistence extends IdentifiablePostgresPersistence<MyData, String> {
MyPostgresPersistence() : super('mydata', null);
@override
void defineSchema_() {
clearSchema();
ensureSchema_('CREATE TABLE ' +
tableName_! +
' (id TEXT PRIMARY KEY, key TEXT, content TEXT)');
ensureIndex_(tableName_! + '_key', {'key': 1}, {'unique': true});
}
Future<DataPage<Dummy>> getPageByFilter(
IContext? context, FilterParams? filter, PagingParams? paging) {
filter = filter ?? FilterParams();
var key = filter.getAsNullableString('key');
var filterCondition = "";
if (key != null) filterCondition += "key='" + key + "'";
return super
.getPageByFilter_(context, filterCondition, paging, null, null);
}
}
var persistence = MyPostgresPersistence();
persistence
.configure(ConfigParams.fromTuples(["host", "localhost", "port", 5432]));
await persistence.open(null);
var item = await persistence.create(null, MyData());
var page = await persistence.getPageByFilter(
null, FilterParams.fromTuples(["key", "ABC"]), null);
print(page.data);
var deleted = await persistence.deleteById(null, '1');