IdentifiableMySqlPersistence<T extends IIdentifiable<K> , K> class
Abstract persistence component that stores data in MySQL
and implements a number of CRUD operations over data items with unique ids.
The data items must implement IIdentifiable
interface.
In basic scenarios child classes shall only override getPageByFilter_, getListByFilter_ or deleteByFilter_ operations with 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.connection_ and this.client_ properties.
Configuration parameters
-
table
: (optional) MySQL table name -
schema
: (optional) MySQL schema name -
connection(s)
:discovery_key
: (optional) a key to retrieve the connection fromIDiscovery
host
: host name or IP addressport
: 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 fromICredentialStore
username
: (optional) user namepassword
: (optional) user password
-
options
:connect_timeout
: (optional) number of milliseconds to wait before timing out when connecting a new client (default: 10000)
Note: the options below are currently not supported.
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) Credential stores to resolve credentials
Example
class MyMySqlPersistence extends IdentifiableMySqlPersistence<MyData, String> {
MyMySqlPersistence() : super('mydata', null);
@override
void defineSchema_() {
this.clearSchema();
this.ensureSchema_('CREATE TABLE `' +
this.tableName_! +
'` (id VARCHAR(32) PRIMARY KEY, `key` VARCHAR(50), `content` TEXT)');
this.ensureIndex_(this.tableName_! + '_key', {'key': 1}, {'unique': true});
}
@override
Future<DataPage<MyData>> getPageByFilter(
String? correlationId, FilterParams? filter, PagingParams? paging) async {
filter = filter ?? new FilterParams();
var key = filter.getAsNullableString('key');
var filterCondition = null;
if (key != null) {
filterCondition += "`key`='" + key + "'";
}
return super
.getPageByFilter_(correlationId, filterCondition, paging, null, null);
}
}
var persistence = MyMySqlPersistence();
persistence
.configure(ConfigParams.fromTuples(["host", "localhost", "port", 27017]));
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');
- Inheritance
-
- Object
- MySqlPersistence<
T> - IdentifiableMySqlPersistence
- Implementers
Constructors
- IdentifiableMySqlPersistence(String? tableName, String? schemaName)
- Creates a new instance of the persistence component.
Properties
- autoGenerateId_ ↔ bool
-
getter/setter pair
- client_ ↔ MySqlConnection?
-
getter/setter pairinherited
- connection_ ↔ MySqlConnection?
-
getter/setter pairinherited
- databaseName_ ↔ String?
-
getter/setter pairinherited
- dependencyResolver_ ↔ DependencyResolver
-
getter/setter pairinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- logger_ ↔ CompositeLogger
-
getter/setter pairinherited
- maxPageSize_ ↔ int
-
getter/setter pairinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- schemaName_ ↔ String?
-
getter/setter pairinherited
- tableName_ ↔ String?
-
getter/setter pairinherited
Methods
-
clear(
String? correlationId) → Future -
Clears component state.
inherited
-
clearSchema(
) → void -
Clears all auto-created objects
inherited
-
close(
String? correlationId) → Future -
Closes component and frees used resources.
inherited
-
configure(
ConfigParams config) → void -
Configures component by passing configuration parameters.
inherited
-
convertFromPublic_(
dynamic value) → dynamic -
Convert object value from public to internal format.
inherited
-
convertFromPublicPartial_(
dynamic value) → dynamic - Converts the given object from the public partial format.
-
convertToPublic_(
dynamic value) → dynamic -
Converts object value from internal to public format.
inherited
-
create(
String? correlationId, T? item) → Future< T?> -
Creates a data item.
override
-
createSchema_(
String? correlationId) → Future< void> -
inherited
-
defineSchema_(
) → void -
Defines database schema via auto create objects or convenience methods.
inherited
-
deleteByFilter_(
String? correlationId, String? filter) → Future< void> -
Deletes data items that match to a given filter.
inherited
-
deleteById(
String? correlationId, K? id) → Future< T?> - Deleted a data item by it's unique id.
-
deleteByIds(
String? correlationId, List< K> ids) → Future<void> - Deletes multiple data items by their unique ids.
-
ensureIndex_(
String name, Map keys, Map? options) → void -
Adds index definition to create it on opening
inherited
-
ensureSchema_(
String schemaStatement) → void -
Adds a statement to schema definition
inherited
-
generateColumns_(
dynamic values) → String -
Generates a list of column names to use in SQL statements like: "column1,column2,column3"
inherited
-
generateParameters_(
dynamic values) → String -
Generates a list of value parameters to use in SQL statements like: "$1,$2,$3"
inherited
-
generateSetParameters_(
Map values) → String -
Generates a list of column sets to use in UPDATE statements like: column1=$1,column2=$2
inherited
-
generateValues_(
Map values) → List -
Generates a list of column parameters
inherited
-
getCountByFilter_(
String? correlationId, String? filter) → Future< int> -
Gets a number of data items retrieved by a given filter.
inherited
-
getListByFilter_(
String? correlationId, String? filter, String? sort, String? select) → Future< List< T> > -
Gets a list of data items retrieved by a given filter and sorted according to sort parameters.
inherited
-
getListByIds(
String? correlationId, List< K> ids) → Future<List< T> > - Gets a list of data items retrieved by given unique ids.
-
getOneById(
String? correlationId, K id) → Future< T?> - Gets a data item by its unique id.
-
getOneRandom_(
String? correlationId, String? filter) → Future< T?> -
inherited
-
getPageByFilter_(
String? correlationId, String? filter, PagingParams? paging, String? sort, String? select) → Future< DataPage< T> > -
Gets a page of data items retrieved by a given filter and sorted according to sort parameters.
inherited
-
isOpen(
) → bool -
Checks if the component is opened.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
open(
String? correlationId) → Future -
Opens the component.
inherited
-
quotedTableName_(
) → String -
inherited
-
quoteIdentifier_(
String? value) → String -
inherited
-
set(
String? correlationId, T? item) → Future< T?> - Sets a data item. If the data item exists it updates it, otherwise it create a new data item.
-
setReferences(
IReferences references) → void -
Sets references to dependent components.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
unsetReferences(
) → void -
Unsets (clears) previously set references to dependent components.
inherited
-
update(
String? correlationId, T? item) → Future< T?> - Updates a data item.
-
updatePartially(
String? correlationId, K? id, AnyValueMap? data) → Future< T?> - Updates only few selected fields in a data item.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited