orm 6.0.0-beta.7 copy "orm: ^6.0.0-beta.7" to clipboard
orm: ^6.0.0-beta.7 copied to clipboard

Typed relational data access for Dart, from declaration to migration.

example/main.dart

import 'package:orm/orm.dart';
import 'package:orm/sql.dart';
import 'package:orm/migrate.dart';
import 'package:orm/sqlite.dart';

import 'schema.orm.dart';

Future<void> main() async {
  final db = Database.fromSql(await sqlite(const SqliteOptions.memory()));
  try {
    await Migrator(
      db.sql,
    ).apply([Migration.create('0001_initial', appSchema, dialect: db.dialect)]);
    final User user = await db.transaction((tx) async {
      final User user = await tx.user.create(email: 'seven@example.com');
      await tx.post.create(
        authorId: user.id,
        title: 'Hello Dart',
        createdAt: DateTime.now(),
      );
      return user;
    });
    await db.user.byId(user.id).patch(nickname: 'Seven');
    final cards = await db.user
        .select(
          (u) => (
            u.email,
            u.posts
                .orderBy((p) => [p.createdAt.desc(), p.id.desc()])
                .take(3)
                .select((p) => p.title)
                .many(),
          ).map((email, titles) => (email: email, titles: titles)),
        )
        .get();
    print(cards);
  } finally {
    await db.close();
  }
}