open method
Opens the component.
- correlationId (optional) transaction id to trace execution through call chain. Return Future that receives error or null no errors occured.
Implementation
@override
Future open(String? correlationId) async {
if (_opened) {
return null;
}
if (connection == null) {
connection = _createConnection();
_localConnection = true;
}
if (_localConnection == true) {
await connection!.open(correlationId);
}
if (connection == null) {
throw InvalidStateException(
correlationId, 'NO_CONNECTION', 'MongoDB connection is missing');
}
if (!connection!.isOpen()) {
throw ConnectionException(
correlationId, 'CONNECT_FAILED', 'MongoDB connection is not opened');
}
_opened = false;
client = connection!.getConnection();
databaseName = connection!.getDatabaseName();
mongo.DbCollection coll;
try {
coll = client!.collection(collectionName!);
} catch (err) {
client = null;
throw ConnectionException(
correlationId, 'CONNECT_FAILED', 'Connection to mongodb failed')
.withCause(err);
}
try {
// Recreate indexes
for (var index in _indexes) {
//TODO: Need fix work with indexes!
var keys = {};
// var keys = await client.createIndex(collectionName,
// keys: index.keys,
// unique: index.unique,
// sparse: index.sparse,
// background: index.background,
// dropDups: index.dropDups,
// partialFilterExpression: index.partialFilterExpression,
// name: index.name
// );
var indexName = keys['name'] ?? index.keys.keys.join(',');
logger.debug(correlationId, 'Created index %s for collection %s',
[indexName, collectionName]);
}
} catch (err) {
client = null;
throw ConnectionException(
correlationId, 'CONNECT_FAILED', 'Connection to mongodb failed')
.withCause(err);
}
_opened = true;
collection = coll;
logger.debug(
correlationId,
'Connected to mongodb database %s, collection %s',
[databaseName, collectionName]);
}