DBCollectionFree class abstract

An abstract class representing a MongoDB collection with utility methods.

The DBCollectionFree class provides an abstraction over a MongoDB collection, offering various methods to interact with the database, such as checking for the existence of a document by its ID, updating fields, form validation, and generating REST API routes. This class is meant to be extended by other classes for more specific collection implementations.

Features include:

  • Document CRUD operations (Create, Read, Update, Delete)
  • Form validation using DBFormFree
  • Index management
  • Event handling for database operations
  • Automatic REST API route generation
  • Search and filter capabilities
  • Pagination support

Constructors

DBCollectionFree.new({required String name, required Db db, required DBFormFree form, Map<String, DBIndex> indexes = const {}})
Constructor to initialize the collection with required parameters.

Properties

collection DbCollection
Provides direct access to the underlying MongoDB collection.
no setter
collectionEvent CollectionEvent
Event handlers for collection operations (insert, update, delete).
getter/setter pair
db Db
The MongoDB database instance.
getter/setter pair
form DBFormFree
The form definition containing field validators and structure.
getter/setter pair
hashCode int
The hash code for this object.
no setterinherited
indexes Map<String, DBIndex>
Map of database indexes to be created for this collection.
getter/setter pair
name String
The name of the MongoDB collection.
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

copy(String id) Future<void>
Creates a copy of a document by its ID and inserts it as a new document.
createIndex({String? key, Map<String, dynamic>? keys, bool? unique, bool? sparse, bool? background, bool? dropDups, Map<String, dynamic>? partialFilterExpression, String? name, bool? modernReply, Map? collation}) Future<Map<String, dynamic>>
Creates a database index on the collection.
delete(String id) Future<bool>
Deletes a document from the collection by its ID.
deleteOid(ObjectId oid) Future<bool>
Deletes a document from the collection by its ObjectId.
existId(String idField) Future<bool>
Checks if a document with the given ID exists in the collection.
existOid(ObjectId? id) Future<bool>
Checks if a document with the given ObjectId exists in the collection.
getAll({SelectorBuilder? selector, Map<String, Object?>? filter, FindOptions? findOptions, String? hint, int? skip, Map<String, Object>? sort, int? limit, Map<String, Object>? hintDocument, Map<String, Object>? projection, Map<String, Object>? rawOptions}) Future<List<Map<String, Object?>>>
Retrieves multiple documents from the collection with flexible options.
getById(String id) Future<Map<String, Object?>?>
Retrieves a single document by its ID.
getByOid(ObjectId? oid) Future<Map<String, Object?>?>
Retrieves a single document by its ObjectId.
getCount({String? field, Object? value, Map<String, Object?>? filter}) Future<int>
Gets the count of documents in the collection.
getSearchableFilter({required Map<String, Object?> inputs, String searchFiled = 'search'}) Map<String, Object?>
Generates search and filter criteria based on form field definitions.
insert(Map<String, Object?> data) Future<FormResultFree>
Inserts a new document into the collection.
mergeOne(String id, Map<String, Object?> data) Future<FormResultFree?>
Merges new data with an existing document by its ID.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
replaceOne(String id, Map<String, Object?> data) Future<FormResultFree?>
Replaces an entire document by its ID.
routeDeleteOne(String path, {required WebRequest rq, List<String> methods = const [RequestMethods.GET], Future<ApiDoc>? apiDoc()?, WaAuthController? auth, List<String> extraPath = const [], List<String> excludePaths = const [], List<String> hosts = const ['*'], Map<String, Object?> params = const {}, List<String> permissions = const [], List<int> ports = const []}) WebRoute
Creates a route for deleting documents from the collection.
routeGetAll(String path, {required WebRequest rq, List<String> methods = const [RequestMethods.GET], Future<ApiDoc>? apiDoc()?, WaAuthController? auth, List<String> extraPath = const [], List<String> excludePaths = const [], List<String> hosts = const ['*'], Map<String, Object?> params = const {}, List<String> permissions = const [], List<int> ports = const [], List<WebRoute> children = const [], bool paging = true, int pageSize = 20, bool orderReverse = true, String orderBy = '_id'}) WebRoute
Creates a route for retrieving all documents in the collection.
routeGetOne(String path, {required WebRequest rq, List<String> methods = const [RequestMethods.GET], Future<ApiDoc>? apiDoc()?, WaAuthController? auth, List<String> extraPath = const [], List<String> excludePaths = const [], List<String> hosts = const ['*'], Map<String, Object?> params = const {}, List<String> permissions = const [], List<int> ports = const []}) WebRoute
Creates a route for retrieving a single document by its ID.
routeInsert(String path, {required WebRequest rq, List<String> methods = const [RequestMethods.POST], Future<ApiDoc>? apiDoc()?, WaAuthController? auth, List<String> extraPath = const [], List<String> excludePaths = const [], List<String> hosts = const ['*'], Map<String, Object?> params = const {}, List<String> permissions = const [], List<int> ports = const []}) WebRoute
Creates a route for inserting new documents into the collection.
routes(String path, {required WebRequest rq, bool useRouteAll = true, bool useRouteDelete = true, bool useRouteInsert = true, bool useRouteUpdate = true, bool useRouteGetOne = true, bool paging = true, int pageSize = 20, bool orderReverse = true, String orderBy = '_id', Future<ApiDoc>? docAll()?, Future<ApiDoc>? docDelete()?, Future<ApiDoc>? docInsert()?, Future<ApiDoc>? docUpdate()?, Future<ApiDoc>? docOne()?, List<WebRoute> children = const []}) List<WebRoute>
Generates a complete set of REST API routes for this collection.
routeUpdate(String path, {required WebRequest rq, List<String> methods = const [RequestMethods.POST], Future<ApiDoc>? apiDoc()?, WaAuthController? auth, List<String> extraPath = const [], List<String> excludePaths = const [], List<String> hosts = const ['*'], Map<String, Object?> params = const {}, List<String> permissions = const [], List<int> ports = const []}) WebRoute
Creates a route for updating existing documents in the collection.
toFormResult(Map<String, Object?> document) Future<FormResultFree>
Converts a raw MongoDB document to a FormResultFree object.
toModel(Map<String, Object?> document, {List<String>? selectedFields}) Future<Map<String, Object?>>
Converts a raw MongoDB document to a validated model.
toString() String
A string representation of this object.
inherited
updateField(String id, String field, Object? value) Future<FormResultFree?>
Updates a specific field of a document by its ID.
validate(Map<String, Object?> data, {List<String> onlyCheckKeys = const []}) Future<FormResultFree>
Validates data against the form definition.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

printDesign() → void
Prints the design and structure of all registered collections.