如何设置 dart objectbox 并预填充本地数据库?

2024-05-09

我想用 Flutter 设置一个 ObjectBox 数据库。 我想用值预先填充数据库文件。安装应用程序时,数据库文件将被复制并由应用程序使用。我希望能够继续提供架构迁移。是否可以?

如何建立这种类型的架构? 你有什么例子吗?

SOLUTION: 通过@vaind 的回复,我实现了一个数据库管理器,例如:

import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';

import '../objectbox.g.dart';

/// Database manager manage database initialisation and configuration.
class DatabaseManager {
  static DatabaseManager? _instance;

  DatabaseManager._internal();

  /// Singleton Factory constructor
  factory DatabaseManager() {
    if (_instance == null) {
      _instance = DatabaseManager._internal();
    }

    return _instance!;
  }

  /// Initialize configuration of database:
  ///  - Init database file by copying.
  ///  - Open instance of database and close it.
  Future<void> init() async {
    await copyDatabaseFileFromAssets();
    await testInstanceAndCloseIt();
  }

  /// Copy packaged database from the assets folder to the app directory.
  Future<void> copyDatabaseFileFromAssets() async {
    // Get a pre-populated DB file.
    ByteData data = await rootBundle.load("assets/databases/data.mdb");
    List<int> bytes =
    data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);

    // Search and create DB file destination folder if not exist
    Directory documentsDirectory = await getApplicationDocumentsDirectory();
    String path = documentsDirectory.path + "/objectbox/data.mdb";

    if (!await File(path).exists()) {
      var objectBoxDirectory = Directory(documentsDirectory.path + "/objectbox/");
      if ((!await objectBoxDirectory.exists())) {
        objectBoxDirectory.create();
      }

      // Copying source data into destination file.
      await new File(path).writeAsBytes(bytes);
    }
  }

  /// Open an instance of the database and close it.
  Future<void> testInstanceAndCloseIt() async {
    await openStore().then((Store store) {
      store.close();
    });
  }
}

是的,即使您想避免在应用程序内填充数据库(例如在第一次运行时),您也可以捆绑现有数据库。只需创建数据库文件(data.mdb)本地,甚至在您的计算机上(它们跨平台兼容),然后将其作为资源添加到应用程序中。

在第一次运行应用程序时,您只需移动data.mdb文件到应用程序文档目录(进入子目录objectbox) - 如果您使用了生成的,则默认情况下,这就是 ObjectBox 存储的位置openStore()方法。所以数据库文件的路径应该是(使用包path_provider): (await getApplicationDocumentsDirectory()).path + '/objectbox/data.mdb'。确保你开店了after当然,您编写该文件。


问题更新后更新。我认为你的copyDatabaseFileFromAssets应该看起来更像下面这样。仅当数据库文件不存在时才创建它。

Future<void> copyDatabaseFileFromAssets() async {
  // Search and create db file destination folder if not exist
  final documentsDirectory = await getApplicationDocumentsDirectory();
  final objectBoxDirectory = Directory(documentsDirectory.path + '/objectbox/');
  
  if (!objectBoxDirectory.existsSync()) {
    await objectBoxDirectory.create(recursive: true);
  }

  final dbFile = File(objectBoxDirectory.path + '/data.mdb');
  if (!dbFile.existsSync()) {
    // Get pre-populated db file.
    ByteData data = await rootBundle.load("assets/databases/data.mdb");

    // Copying source data into destination file.
    await dbFile.writeAsBytes(data.buffer.asUint8List());
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何设置 dart objectbox 并预填充本地数据库? 的相关文章

随机推荐