simple_storage

Simple Storage

Pub Version Pub Points License: MIT Platform Dart SDK Version

A simple, lightweight NoSQL database written in pure Dart, with support for concurrency control, indexing, pagination, transactions, and TTL.

Features

Getting Started

  1. Add the simple_storage dependency:

    flutter pub add simple_storage
    
  2. Import:

    import 'package:simple_storage/simple_storage.dart';
    
  3. Create a Database Instance:

    final db = Database('./my_database'); // Specify the storage path
    
    • The constructor argument specifies the directory where your database files will be stored.
  4. Create or Access a Collection:

    final users = await db.collection('users', indexes: ['age', 'name']);
    
    • The first argument is the name of the collection. If a collection with that name doesn’t exist it will be created.
    • The optional indexes parameter is a List<String> of properties that should be indexed. Indexing can speed up lookups based on these properties but can also slow down insertions if used too much.
  5. Store Data (with optional TTL):
    • Use the put method to store data using a key and a value.
    • The optional ttl parameter specifies a time-to-live for the data. After that time, the data will be removed when loading from disk, or when accessing using get or getAll.
  6. Retrieve Data:
    • Use the get method to retrieve data using a key.
    • If you use the key parameter as a field that has been indexed you should also provide the value to filter the results.
  7. Retrieve Data with Pagination:
    • Use getAll with limit and offset to get data in pages.
    • The limit specifies the maximum amount of data returned, and the offset specifies which entry is the start of the page.
    • You can use the total number of users to create pagination controls.
  8. Perform Operations Inside a Transaction:
    • Transactions help group operations in one action that can be committed or rolled back if one of the operations fails.
  9. Handle Errors:

    The application throws specific errors:

    • DatabaseCreateException: when the database fails to create the storage directory.
    • CollectionLoadException: when a collection fails to load from the disk.
    • CollectionSaveException: when a collection fails to save to disk.
    • CollectionNotFoundException: when a collection is not found.

    These exceptions should be caught by the application to handle specific error cases.

Core Classes

Error Handling

The database uses custom exception classes for clear and detailed error reporting:

These exceptions provide specific error messages, which you can use to provide feedback to your user, and to help debug.

Contributions

Contributions are welcome! If you have ideas for enhancements or find any bugs, please feel free to:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Submit a pull request with your changes.

Limitations

License

This project is licensed under the MIT License - see the LICENSE file for details.