snail 1.0.3
snail: ^1.0.3 copied to clipboard
A library inspired by Spring Boot's JPA that facilitates SQLite database management in Flutter/Dart applications.
๐ Snail: A Simple ORM-like Library for Flutter/Dart ๐ฆ #
Snail is a library inspired by Spring Boot's JPA, designed to simplify SQLite database management in Flutter/Dart applications. Easy to use like a snail ๐ (but as functional as a rocket ๐)!
๐ Features #
- ๐ฆ Automatic table creation in SQLite based on repositories.
- ๐ Ready-to-use CRUD methods: create, read, update, delete.
- ๐ ๏ธ Batch operations like
createManyandfindMany. - ๐ก Abstraction with
SnailRepositoryand straightforward entity mapping.
๐ Installation #
Add Snail to your project by including it in pubspec.yaml:
dependencies:
snail: ^1.0.3
Install the dependency:
flutter pub get
๐ ๏ธ Initial Setup #
1 - Define Your Model #
Create a class representing your database entity:
class UserModel {
final int id;
final String name;
final String email;
UserModel({required this.id, required this.name, required this.email});
Map<String, dynamic> toMap() => {
'id': id,
'name': name,
'email': email,
};
factory UserModel.fromMap(Map<String, dynamic> map) => UserModel(
id: map['id'],
name: map['name'],
email: map['email'],
);
}
2 - Create the Repository #
Extend the SnailRepository to define your data repository:
import 'package:snail/snail.dart';
import '../models/user_model.dart';
class UserRepository extends SnailRepository<UserModel, int> {
UserRepository()
: super(
tableName: 'users',
primaryKeyColumn: 'id',
defineFields: {
'id': int,
'name': String,
'email': String,
},
);
@override
Map<String, dynamic> toMap(UserModel entity) => entity.toMap();
@override
UserModel fromMap(Map<String, dynamic> map) => UserModel.fromMap(map);
}
3 - Initialize the Database #
Configure repositories in the main() function:
import 'package:flutter/material.dart';
import 'package:snail/snail.dart';
import './repositories/user_repository.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Snail.initialize(
repositories: [
UserRepository(),
],
);
runApp(AppWidget());
}
๐ Usage Examples #
๐ Create a New Record #
final userRepo = UserRepository();
await userRepo.create(UserModel(id: 1, name: 'John Doe', email: '[email protected]'));
๐ Fetch a Record by ID #
final user = await userRepo.findOne(1);
if (user != null) {
print('User found: ${user.name}');
}
๐ Fetch All Records #
final users = await userRepo.findMany();
users.forEach((user) => print(user.name));
๐ ๏ธ Update a Record #
final updatedUser = UserModel(id: 1, name: 'Jane Doe', email: '[email protected]');
await userRepo.update(updatedUser);
๐๏ธ Delete a Record #
await userRepo.delete(1);
๐ Create Multiple Records #
await userRepo.createMany([
UserModel(id: 2, name: 'Alice', email: '[email protected]'),
UserModel(id: 3, name: 'Bob', email: '[email protected]'),
]);
๐งฌ Full API #
CRUD Methods: #
| Method | Description |
|---|---|
create(T entity) |
Creates a new record. |
createMany(List<T>) |
Creates multiple records. |
findOne(ID id) |
Fetches a record by ID. |
findMany() |
Fetches all records. |
update(T entity) |
Updates a record. |
updateMany(List<T>) |
Updates multiple records. |
delete(ID id) |
Deletes a record by ID. |
deleteMany(List<ID>) |
Deletes multiple records. |
๐งช Testing #
A basic unit test example:
void main() async {
final repo = UserRepository();
await repo.create(UserModel(id: 1, name: 'Test User', email: '[email protected]'));
final user = await repo.findOne(1);
assert(user?.name == 'Test User');
print('Test passed ๐');
}
๐ Contributing #
Contributions are always welcome! Open an issue or submit a pull request on the GitHub repository.
๐ License #
This project is licensed under the MIT License.
๐ Ready to Simplify Your SQLite? #
Boost your productivity with Snail and make your SQLite operations simple and efficient! ๐๐จ
Made with โค๏ธ for Flutter developers! ๐ฏ