soda_db 1.0.1 copy "soda_db: ^1.0.1" to clipboard
soda_db: ^1.0.1 copied to clipboard

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

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));
  }
}
1
likes
40
pub points
0%
popularity

Publisher

verified publisherradioapptive.de

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

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

meta

More

Packages that depend on soda_db