soda_db 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • new60

Dart CI

SodaDB is a lightweight and simple to use object-oriented storage for Dart and Flutter applications, with type-safe access on entities.

Written in pure Dart with no third party dependencies.

Usage #

Define entity classes and type adapters #

Define your entity classes using SodaDB's SodaEntity mixin. Provide TypeAdapters for the necessary de-/serializer methods, which might be written by hand or generated by frameworks like json_serializable.

class User with SodaEntity {
  String firstName;
  String lastName;

  User(this.firstName, this.lastName);

  User.fromMap(Map<String, dynamic> map) {
    firstName = map['firstName'];
    lastName = map['name'];
  }

  Map<String, Object> toJson() {
    return {
      'firstName': firstName,
      'lastName': lastName,
    };
  }
}

class UserAdapter extends TypeAdapter<User> {
  // Provide a serialized version of your entity.
  // A convenient way would be to use JSON.
  @override
  String serialize(User entity) {
    return jsonEncode(entity);
  }

  // Deserialize your entity from the String
  // generated by serialize(User entity).
  @override
  User deserialize(String data) {
    return User.fromMap(jsonDecode(data));
  }
}

Use storage and repositories #

Storage is designed as singleton and can be accessed by the global 'storage' variable. Repositories are used to organize entity types.

void main() async {
  // Register adapter for type 'User'.
  storage.register(UserAdapter());

  // Open/create storage in path './example'.
  await storage.open('./example');

  // Create a user instance.
  var john = User('John', 'McClaine');

  // Obtain repository 'users' from storage.
  // Provide type information to the get method or explicitly
  // type the users variable to help the compiler determine the correct type.
  var users = storage.get<User>('users');

  // Save user john to repository.
  await users.put(john);

  // Auto created ID for new entities.
  print(john.id); // prints '0'

  // Update existing entity.
  john.lastName = 'McClane';
  await users.put(john);

  // Read existing entities.
  var user = await users.get(0);

  // Remove user from repository.
  await users.remove(user);

  // Close storage before the application finishes
  // to ensure all operations get finished.
  await storage.close();
}

Features and bugs #

Please file feature requests and bugs at the issue tracker.

1.0.1 #

  • Improved package description

1.0.0 #

  • Initial release

example/example.dart

import 'dart:convert';

import 'package:soda_db/soda_db.dart';
import 'package:soda_db/src/storage/type_adapter.dart';

void main() async {
  // Register adapter for type 'User'.
  storage.register(UserAdapter());

  // Open/create storage in path './example'.
  await storage.open('./example');

  // Create a user instance.
  var john = User('John', 'McClaine');

  // Obtain repository 'users' from storage.
  // Provide type information to the get method or explicitly
  // type the users variable to help the compiler determine the correct type.
  var users = storage.get<User>('users');

  // Save user john to repository.
  await users.put(john);

  // Auto created ID for new entities.
  print(john.id); // prints '0'

  // Update existing entity.
  john.lastName = 'McClane';
  await users.put(john);

  // Read existing entities.
  var user = await users.get(0);

  // Remove user from repository.
  await users.remove(user);

  // Close storage before the application finishes
  // to ensure all operations get finished.
  await storage.close();
}

class User with SodaEntity {
  String firstName;
  String lastName;

  User(this.firstName, this.lastName);

  User.fromMap(Map<String, dynamic> map) {
    firstName = map['firstName'];
    lastName = map['name'];
  }

  Map<String, Object> toJson() {
    return {
      'firstName': firstName,
      'lastName': lastName,
    };
  }
}

class UserAdapter extends TypeAdapter<User> {
  // Provide a serialized version of your entity.
  // A convenient way would be to use JSON.
  @override
  String serialize(User entity) {
    return jsonEncode(entity);
  }

  // Deserialize your entity from the String
  // generated by serialize(User entity).
  @override
  User deserialize(String data) {
    return User.fromMap(jsonDecode(data));
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  soda_db: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:soda_db/soda_db.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
20
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
60
Learn more about scoring.

We analyzed this package on Mar 26, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health suggestions

Fix lib/src/storage/storage.dart. (-0.50 points)

Analysis of lib/src/storage/storage.dart reported 1 hint:

line 13 col 1: Avoid return types on setters.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
meta ^1.1.8 1.1.8
Dev dependencies
effective_dart ^1.1.0
mockito ^4.1.1
pedantic ^1.8.0
test ^1.6.0