DBCollectionFree
DBCollectionFree is a service library that offers a schema-free, flexible way to manage MongoDB collections in Dart
DBCollectionFree
DBCollectionFree
is a service library that offers a schema-free, flexible way to manage MongoDB collections in Dart. It allows direct interaction with MongoDB collections, providing utilities for common database operations, field validation, and customizable forms.
DBCollectionFree
is an ideal choice for building an API when you need rapid development without the hassle of defining rigid data models. It offers a more flexible approach to manage MongoDB collections directly, enabling you to add or modify fields on the go. This can save you valuable time during development, allowing you to focus on building and deploying your API rather than on data schema management. Perfect for prototyping or for use cases where data structures may evolve frequently, DBCollectionFree
makes it simple to get your API up and running quickly.
This example explains how to set up and use DBCollectionFree
for managing your MongoDB collections without predefined data models. The included example demonstrates a PersonCollectionFree
implementation for handling personal data.
Features
- Flexible Schema Management: No predefined data model required.
- CRUD Operations: Provides
insert
,update
,replace
,delete
, andfind
functions. - Field Validation: Supports required fields, length constraints, and data types (e.g., String, int, ObjectId).
- Customizable Forms: Define field properties, validation rules, and data filters.
- MongoDB-Specific Operations: Includes support for ObjectId checks, MongoDB query builders, and optimized field updates.
Usage
Setting Up a Collection
To define a MongoDB collection using DBCollectionFree
, you create a class that extends DBCollectionFree
and configure the collection's fields and their validation rules. The example below defines a PersonCollectionFree
class with fields such as name
, age
, email
, birthday
, married
, and height
.
Example Code
Define the Collection
Usage Example
We are use this
- check the home_controller.dart in
example
Methods Overview
insert
: Validates and inserts a document.replaceOne
: Replaces a document by its ID with a new data map.getAll
: Retrieves all documents based on specified filters and sort options.existId
: Checks if a document exists based on an ObjectId.delete
: Deletes a document by its ID.updateField
: Updates a single field within a document by its ID.getById
: Retrieves a document by its ID.
Validation and Custom Form Rules
Each field can be customized with validation rules such as required
, min
, max
, and format validations like isEmailField()
.
Error Handling
Errors are tracked and updated based on MongoDB response codes, which can then be used to provide feedback for each field.