nosql_persistence 0.1.1 nosql_persistence: ^0.1.1 copied to clipboard
Using nosql databases with migrations
Using NoSQL databases with migrations
Using NoSQL databases is now easier. We want to simplify a lot of work with NoSQL databases in Flutter using migrations between versions. All the logic for working with data will remain in the class, and you will have to use the prepared methods!
Features #
💻 Good work with models fromJson & toJson.
💐 Migrations between versions.
🌱 Hidden logic inside the class, the connection is SOLID.
🏁 Speed of work and data loading from Hive and Flutter Secure Storage.
Motivation #
Using NoSQL databases is difficult when you need to update data between versions. In our package, we have tried to simplify working with them so that it is easier for you to work. Now migrations can be done like this:
@override
Future<void> migrate(int oldVersion, int currentVersion) async {
if (oldVersion < currentVersion) {
await put(key: "key", value: "value");
}
return super.migrate(oldVersion, currentVersion);
}
Getting Started #
- Add nosql_persistence to dependencies
- Initialize according to the instructions Hive and Flutter Secure Storage.
- Create your final class and inherit it from
SecureDataSource
(if it is protected data) andStorageDataSource
(if there are a lot of them and you need fast work). You will get something like this
final class ExampleDataSource extends SecureDataSource {
ExampleDataSource(super.secureStorage)
: super(
databaseName: 'example',
databaseVersion: 1,
);
}
- Describe the methods you need
Future<String?> getLastEmail() async => get('last_email');
Future<void> putLastEmail(String value) async =>
put(key: 'last_email', value: value);
- Describe the methods with the models that you need
Future<People?> getCurrentPeople() async => getJsonTyped(
'current',
People.fromJson
);
Future<void> putCurrentPeople(People value) async => putJsonTyped(
'current',
value.toJson(),
);
- Use migrations between versions (the current version of the database is specified in the super constructor)
@override
Future<void> migrate(int oldVersion, int currentVersion) async {
if (oldVersion < 2) {
await delete('current');
}
return super.migrate(oldVersion, currentVersion);
}
- Initialize the class and be sure to call initAsync before using it!
Future<void> test() async {
final dataSource = ExampleDataSource(secureStorage);
await dataSource.initAsync();
print(await dataSource.getCurrentPeople());
}
Here's what we'll get:
final class ExampleDataSource extends SecureDataSource {
ExampleDataSource(super.secureStorage)
: super(
databaseName: 'example',
databaseVersion: 1,
);
@override
Future<void> migrate(int oldVersion, int currentVersion) async {
if (oldVersion < 2) {
await delete('current');
}
return super.migrate(oldVersion, currentVersion);
}
Future<String?> getLastEmail() async => read('last_email');
Future<void> putLastEmail(String value) async =>
write(key: 'last_email', value: value);
Future<People?> getCurrentPeople() async =>
getJsonTyped('current', People.fromJson);
Future<void> putCurrentPeople(People value) async => putJsonTyped(
'current',
value.toJson(),
);
}
Very simple and clean! 🌱
Contributing #
Contributions are welcomed! Here is a curated list of how you can help:
- Report bugs and scenarios that are difficult to implement
- Report parts of the documentation that are unclear
- Fix typos/grammar mistakes
- Update the documentation / add examples
- Implement new features by making a pull-request