Skip to main content

drift_postgres 2.0.0-alpha.0 copy "drift_postgres: ^2.0.0-alpha.0" to clipboard
drift_postgres: ^2.0.0-alpha.0 copied to clipboard

Postgres implementation and APIs for the drift database package.

example/main.dart

import 'package:drift3_preview/drift.dart';
import 'package:drift_postgres/drift_postgres.dart';
import 'package:postgres/postgres.dart' as pg;
import 'package:uuid/uuid.dart';

part 'main.g.dart';

class Users extends Table {
  UuidColumn get id => col(PgTypes.uuid).withDefault(genRandomUuid());
  TextColumn get name => text();

  @override
  Set<Column<Object>>? get primaryKey => {id};
}

@DriftDatabase(tables: [Users])
final class DriftPostgresDatabase extends _$DriftPostgresDatabase {
  DriftPostgresDatabase(super.e);

  @override
  int get schemaVersion => 1;
}

void main() async {
  final connection = DriftConnection(
    dialect: PostgresDialect.new,
    openConnection: (_) async {
      final endpoint = pg.Endpoint(
        host: 'localhost',
        database: 'postgres',
        username: 'postgres',
        password: 'postgres',
      );
      final connection = await PostgresSession.open(
        endpoint,
        settings: pg.ConnectionSettings(sslMode: pg.SslMode.disable),
      );
      return connection;
    },
  );

  final database = DriftPostgresDatabase(connection);

  final user = await database.usersQueries.insertReturning(
    UsersCompanion.insert(name: 'Simon', id: Value(Uuid().v4obj())),
  );
  print(user);

  await database.usersQueries.deleteOne(user);

  await database.close();
}
28
likes
130
points
15.7k
downloads

Documentation

API reference

Publisher

verified publishersimonbinder.eu

Weekly Downloads

Postgres implementation and APIs for the drift database package.

Topics

#drift #database #postgres

License

MIT (license)

Dependencies

collection, drift3_preview, meta, postgres, uuid

More

Packages that depend on drift_postgres