tarsier_storage 0.0.4
tarsier_storage: ^0.0.4 copied to clipboard
Package that provides a unified API for working with both local SQLite and remote MySQL databases, enabling seamless switching between offline and online storage.
example/main.dart
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:tarsier_storage/tarsier_storage.dart';
Future<void> main() async {
// Default database file name (used mainly for web)
String databaseFile = 'sample.db';
// Get the application's documents directory (mobile / desktop)
var directory = await getApplicationDocumentsDirectory();
// On non-web platforms, store the database inside the app documents folder
if (!kIsWeb) {
databaseFile = _join(directory.path, 'sample.db');
}
// Initialize TarsierStorage with the database tables
// and provide the SQLite database file path
await TarsierStorage.init([
UserTable(),
// ... Other tables that extend BaseTableModel
], sqliteDatabaseFile: databaseFile);
// Create an instance of the User table
final userTable = UserTable();
// Fetch all existing users from the database
var users = await userTable.all();
// Print all users to the console
for (var user in users) {
Console.info(' ${user.toMap()}');
}
// Set the backup directory (documents folder)
var backupDirectory = directory.path;
// Backup the database to the specified directory
// archive: true will compress the backup as a zip file
await TarsierStorage.backup(
path: backupDirectory,
archive: true,
onProgress: (c, t, m) => Console.info('$c/$t $m'),
);
// Insert new users into the database
// Starts after the current number of users
for (int i = users.length + 1; i < 5; i++) {
await userTable.createObject(
User(
id: i,
name: 'Name $i',
email: 'name$i@gmail.com',
createdAt: DateTime.now(),
),
);
}
// Fetch all users again after inserting new records
users = await userTable.all();
// Print updated user list
for (var user in users) {
Console.info(' ${user.toMap()}');
}
// Build the full path to the backup zip file
var backupFile = _join(backupDirectory, 'sample.bk.zip');
// Restore the database from the backup file
await TarsierStorage.restore(
backupFile,
onProgress: (c, t, m) => Console.info('$c/$t $m'),
);
// Fetch all users after restore
users = await userTable.all();
// Print users restored from backup
for (var user in users) {
Console.info(' ${user.toMap()}');
}
}
String _join(String part1, String part2) {
final separator = Platform.pathSeparator;
if (part1.endsWith(separator)) {
return '$part1$part2';
}
return '$part1$separator$part2';
}
class User extends BaseTableModel {
final int? id;
final String name;
final String email;
final DateTime createdAt;
User(
{this.id,
required this.name,
required this.email,
required this.createdAt});
factory User.fromMap(Map<String, dynamic> map) {
return User(
id: map['id'] as int?,
name: map['name'] as String,
email: map['email'] as String,
createdAt: DateTime.parse(map['created_at'] as String),
);
}
@override
Map<String, dynamic> toMap() {
return {
'id': id,
'name': name,
'email': email,
'created_at': createdAt.toIso8601String()
};
}
static const String tableName = 'users';
static Map<String, String> schema(DatabaseUse dbUse) {
//@TODO: Should be use via DbColumn annotations
return {
'id': dbUse == DatabaseUse.mysql
? 'INTEGER AUTO_INCREMENT PRIMARY KEY'
: 'INTEGER PRIMARY KEY AUTOINCREMENT',
'name': 'TEXT',
'email': 'TEXT',
'created_at': 'TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP',
};
}
}
class UserTable extends BaseTable<User> {
UserTable()
: super(
tableName: User.tableName,
schema: User.schema(databaseSelector.value),
fromMap: (map) => User.fromMap(map),
toMap: (user) => user.toMap(),
);
@override
Map<String, String> getSchema(DatabaseUse databaseType) {
return User.schema(databaseType);
}
}