spring_boot_starter_data

spring_boot_starter_data is the data layer of Spring for Dart, the Dart port of Spring Boot's spring-boot-starter-data-jdbc. You describe an aggregate with @Table, declare a repository interface, and the generator writes its implementation, derived queries included. The same repositories run in memory, on SQLite or on PostgreSQL, chosen by properties.

The model is Spring Data JDBC's, not JPA's: an aggregate is loaded and saved whole, save writes at once, and other aggregates are referenced by id. There is no persistence context, no lazy loading and no proxy of entities.

Why

Most applications store the same kind of data the same way: a table per aggregate, a few finders, paging, a transaction around a business operation, migrations at startup. Written by hand, this is the same SQL and mapping code over and over. The starter gives you:

No reflection is used at run time: the generator describes the entities and writes the repositories.

Setup

# pubspec.yaml
dependencies:
  spring_boot_starter_data: ^0.9.0
  sqlite3: ^3.8.0        # for sqlite: URLs; list postgres: ^3.5.0 for PostgreSQL

dev_dependencies:
  build_runner: ^2.17.0
  spring_core_dart_generator: ^0.9.0   # writes the repositories
  spring_boot_starter_test: ^0.9.0     # @DataTest
  test: ^1.33.0

The starter has no driver inside. You list the one you want (sqlite3, postgres, or both), and the generator writes a JdbcDriver bean for each driver package it finds. The URL's scheme chooses among them. Without any URL, repositories keep their data in memory.

One import is enough: package:spring_boot_starter_data/spring_boot_starter_data.dart also exports spring_boot_starter_logging, and through it spring_boot_dart and spring_core_dart.

Example

// bin/main.dart
import 'package:spring_boot_starter_data/spring_boot_starter_data.dart';

import 'spring_gen/main.spring.dart';

@Table('books')
class Book {
  Book({this.id, required this.title, this.year});

  @Id()
  int? id;          // null: new, the database gives the id
  String title;
  int? year;

  @Version()
  int version = 0;  // optimistic locking; 0 means "not saved yet"
}

// No annotation needed: extending a repository interface is enough.
abstract interface class BookRepository
    implements
        ListCrudRepository<Book, int>,
        ListPagingAndSortingRepository<Book, int> {
  Future<List<Book>> findByTitleContainingIgnoreCase(String text);
  Future<Page<Book>> findByYearGreaterThan(int year, Pageable page);
  Future<int> countByYearBetween(int from, int to);
}

@Service()
class Catalog {
  Catalog(this.books);

  final BookRepository books;

  // One transaction: both saves commit together, or neither does.
  @Transactional()
  Future<void> addSeries(List<String> titles) async {
    for (final title in titles) {
      await books.save(Book(title: title));
    }
  }
}

@SpringBootApplication()
// A SQL database is set up on a connection: start asynchronously.
Future<void> main(List<String> args) => runApplicationAsync(args);
# lib/resources/application.properties
spring.datasource.url=sqlite:file:library.db
-- lib/resources/db/migration/V1__books.sql
create table books (
  id integer primary key autoincrement,
  title text not null,
  year integer,
  version integer not null
);
dart run build_runner build   # writes the repository and the wiring
dart run bin/main.dart

At startup the migration runs, the starter checks that the books table fits the Book entity, and only then can any repository or JdbcTemplate use the database.

Running it

What starts with the starter:

What is inside

The model:

Repositories:

Transactions:

Databases:

With other starters:

Reference:

In Spring (Java)

@Table("books")
class Book {
  @Id Long id;
  String title;
  Integer year;
  @Version long version;
}

interface BookRepository
    extends ListCrudRepository<Book, Long>, ListPagingAndSortingRepository<Book, Long> {
  List<Book> findByTitleContainingIgnoreCase(String text);
  Page<Book> findByYearGreaterThan(int year, Pageable page);
}

Differences that matter:

See also