DBCollectionFree constructor

DBCollectionFree({
  1. required String name,
  2. required Db db,
  3. required DBFormFree form,
  4. Map<String, DBIndex> indexes = const {},
})

Constructor to initialize the collection with required parameters.

Creates a new database collection instance and automatically handles:

  • Collection registration in the global list
  • Collection creation if it doesn't exist in the database
  • Index renewal and management

Parameters:

  • name - The name of the MongoDB collection
  • db - The MongoDB database instance
  • form - The form definition with field validators
  • indexes - Optional map of database indexes to create

Example:

final userCollection = UserCollection(
  name: 'users',
  db: mongoDb,
  form: userForm,
  indexes: {'email': DBIndex(key: 'email', unique: true)},
);

Implementation

DBCollectionFree({
  required this.name,
  required this.db,
  required this.form,
  this.indexes = const {},
}) {
  _allCollectionFree.add(this);

  db.getCollectionNames().then((coll) async {
    if (!coll.contains(name)) {
      await db.createCollection(name);
    }
    _renewIndexes();
  });
}