isoIndex<T extends Model> static method

Future isoIndex<T extends Model>()

Loads all records for model type T from SQLite inside a separate isolate.

This is a work-in-progress helper intended for prototyping heavier database reads away from the main isolate. It opens the SQLite database directly, queries the table for T, and reconstructs model instances using the registered JSON constructor.

Records are returned in descending id order.

Returns an empty list when:

  • no JSON constructor has been registered for T
  • the query fails

Current notes:

  • this implementation is SQLite-specific
  • it executes through QueueJob.inline
  • it is marked WIP and should be treated as experimental

Example:

final users = await SQLiteModel.isoIndex<User>();
print(users.length);

Implementation

static Future<dynamic> isoIndex<T extends Model>() async {
  final constructor = _jsonConstructors[T];
  if (constructor == null) return [];

  return QueueJob.inline(() async {

    final Directory dir = Directory("lib/src/storage");
    final file = File("${dir.absolute.path}/database.sqlite");
    final SQLiteDatabase sqliteDatabase = await databaseFactoryFfi.openDatabase(
      file.absolute.path,
      options: OpenDatabaseOptions(
        version: 1,
        onCreate: (db, version) async {
          // Placeholder for database migrations
        },
      ),
    );

    try {
      final records = await sqliteDatabase.query(
        Model.getTableName<T>(),
        orderBy: 'id DESC',
      );
      return records.map((map) => constructor(map) as T).toList();
    } catch (e) {
      print('SQLite index error: $e');
      return [];
    } finally {
    }
  });
}