mongo_pool 1.4.1 copy "mongo_pool: ^1.4.1" to clipboard
mongo_pool: ^1.4.1 copied to clipboard

A MongoDB connection pool for Dart. Using this package, your application will open as many database connections as you specify as soon as it runs.

example/mongo_pool_example.dart

import 'dart:async';

import 'package:mongo_pool/mongo_pool.dart';

Future<void> main() async {
  /// Create a pool of 5 connections
  final MongoDbPoolService poolService = MongoDbPoolService(
    const MongoPoolConfiguration(
      maxLifetimeMilliseconds: 90000,
      leakDetectionThreshold: 10000,
      uriString: 'mongodb://localhost:27017/my_database',
      poolSize: 4,
    ),
  );

  /// Open the pool
  await openDbPool(poolService);

  /// Get a connection from pool
  final Db connection = await poolService.acquire();

  // Database operations
  final DbCollection collection = connection.collection('my_collection');
  final List<Map<String, dynamic>> result = await collection.find().toList();
  result;
  // Connection release for other operations
  poolService.release(connection);

  // Pool close
  await poolService.close();
}

Future<void> openDbPool(MongoDbPoolService service) async {
  try {
    await service.open();
  } on Exception catch (e) {
    /// handle the exception here
    print(e);
  }
}

class OtherClass {
  OtherClass();

  Future<void> openDbPool() async {
    /// Get the instance of the pool
    final MongoDbPoolService poolService = MongoDbPoolService.getInstance();
    final Db connection = await poolService.acquire();
    // Database operations
    final DbCollection collection = connection.collection('my_collection');
    final List<Map<String, dynamic>> result = await collection.find().toList();
    result;
    // Connection release for other operations
    poolService.release(connection);
    // Pool close
    await poolService.close();
  }
}
3
likes
140
pub points
72%
popularity

Publisher

verified publishermerterkoc.dev

A MongoDB connection pool for Dart. Using this package, your application will open as many database connections as you specify as soon as it runs.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

cached_annotation, meta, mongo_dart, very_good_analysis

More

Packages that depend on mongo_pool