DocDB is a simple document database for Flutter that stores document metadata in one table and key-value pairs in separate tables for each data type. It is not an ORM for SQLite.
Add docdb as a dependency in your pubspec.yaml file:
dependencies:
docdb:
Then run flutter pub get
to install the package.
Connecting to the database Before you can use DocDB, you need to connect to a database. You can use the connect method to do this:
import 'package:docdb/docdb.dart';
final db = DocDB();
await db.connect(dbName: 'mydb.db');
The connect method creates the database file if it doesn't exist and opens a connection to the database.
To store a document, create a Doc object and add fields to it:
final doc = Doc();
doc['title'] = 'My Document';
doc['content'] = 'Lorem ipsum dolor sit amet...';
await db.insert(doc);
The insert method adds the document to the database and returns the document ID.
To retrieve a document, use the get method:
final docId = 1; // ID of the document to retrieve
final doc = await db.get(docId);
print(doc['title']); // Prints the title of the document
The get method returns a Doc object with the specified ID.
To update a document, retrieve it using the get method, modify its fields, and then call the update method:
final docId = 1; // ID of the document to update
final doc = await db.get(docId);
doc['title'] = 'New Title';
await db.update(doc);
The update method saves the changes to the database.
To delete a document, use the delete method:
final docId = 1; // ID of the document to delete
await db.delete(docId);
The delete method removes the document and its associated key-value pairs from the database.
You can filter documents by a specific field value using the filter method:
final docs = await db.filter('title', 'My Document');
print(docs.length); // Prints the number of documents with the specified title
The filter method returns a list of Doc objects that match the specified criteria.
You can query documents with multiple filters and date constraints using the query method:
List<Doc> docs = await db.query(
{'_collection': 'xyz', 'status': 'active'},
dateField: 'created',
olderThan: DateTime.now().subtract(const Duration(days: 3)),
);
The query method returns a list of Doc objects that match the specified criteria and date constraints.
DocDB is released under the MIT License. See LICENSE for details.