snail 1.0.3 copy "snail: ^1.0.3" to clipboard
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 createMany and findMany.
  • ๐Ÿ’ก Abstraction with SnailRepository and 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! ๐ŸŽฏ

18
likes
0
points
17
downloads

Publisher

verified publisherevandersondev.com.br

Weekly Downloads

A library inspired by Spring Boot's JPA that facilitates SQLite database management in Flutter/Dart applications.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

path, sqflite

More

Packages that depend on snail