From e12e8fdbc9c81579f9f384ed332a1d8763d0d188 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 20 Oct 2023 12:12:08 +0200 Subject: [PATCH] Use preferred import for drift postgres --- docs/lib/snippets/platforms/postgres.dart | 2 +- extras/drift_postgres/example/main.dart | 2 +- extras/drift_postgres/lib/drift_postgres.dart | 135 ++++++++++++++++++ extras/drift_postgres/lib/postgres.dart | 110 +------------- .../test/drift_postgres_test.dart | 2 +- extras/drift_postgres/test/types_test.dart | 2 +- 6 files changed, 141 insertions(+), 112 deletions(-) create mode 100644 extras/drift_postgres/lib/drift_postgres.dart diff --git a/docs/lib/snippets/platforms/postgres.dart b/docs/lib/snippets/platforms/postgres.dart index 3f8bd16d..c5ebe691 100644 --- a/docs/lib/snippets/platforms/postgres.dart +++ b/docs/lib/snippets/platforms/postgres.dart @@ -1,5 +1,5 @@ import 'package:drift/drift.dart'; -import 'package:drift_postgres/postgres.dart'; +import 'package:drift_postgres/drift_postgres.dart'; import 'package:postgres/postgres_v3_experimental.dart'; part 'postgres.g.dart'; diff --git a/extras/drift_postgres/example/main.dart b/extras/drift_postgres/example/main.dart index 45e7d8a1..5ebda50b 100644 --- a/extras/drift_postgres/example/main.dart +++ b/extras/drift_postgres/example/main.dart @@ -1,5 +1,5 @@ import 'package:drift/drift.dart'; -import 'package:drift_postgres/postgres.dart'; +import 'package:drift_postgres/drift_postgres.dart'; import 'package:postgres/postgres_v3_experimental.dart'; import 'package:uuid/uuid.dart'; diff --git a/extras/drift_postgres/lib/drift_postgres.dart b/extras/drift_postgres/lib/drift_postgres.dart new file mode 100644 index 00000000..27ace103 --- /dev/null +++ b/extras/drift_postgres/lib/drift_postgres.dart @@ -0,0 +1,135 @@ +/// PostgreSQL backend for drift. +/// +/// For more information on how to use this package, see +/// https://drift.simonbinder.eu/docs/platforms/postgres/. +library drift.postgres; + +import 'package:drift/drift.dart'; +import 'package:postgres/postgres_v3_experimental.dart'; +import 'package:uuid/uuid.dart'; + +import 'src/types.dart'; + +export 'src/pg_database.dart'; +export 'package:uuid/uuid_value.dart' show UuidValue; + +/// Type for columns storing [UuidValue]s. +typedef UuidColumn = Column; + +/// Type for columns storing [Duration]s as intervals. +typedef IntervalColumn = Column; + +/// Type for columns storing JSON structures. +typedef JsonColumn = Column; + +/// Type for columns storing [PgPoint]s. +typedef PointColumn = Column; + +/// Type for columns storing dates directly as ([PgDateTime]). +typedef TimestampColumn = Column; + +/// Type for columns storing dates directly as ([PgDate]). +typedef PgDateColumn = Column; + +/// Provides [custom types](https://drift.simonbinder.eu/docs/sql-api/types/) +/// to enable the use of Postgres-specific types in drift databases. +final class PgTypes { + PgTypes._(); + + /// The `UUID` type in Postgres. + static const CustomSqlType uuid = UuidType(); + + /// The `interval` type in Postgres. + static const CustomSqlType interval = IntervalType(); + + /// The `date` type in Postgres. + static const CustomSqlType date = DateType( + PgDataType.date, + 'date', + PgDate.fromDateTime, + ); + + /// The `timestamp without time zone` type in Postgres. + static const CustomSqlType timestampNoTimezone = DateType( + PgDataType.timestampWithoutTimezone, + 'timestamp without time zone', + PgDateTime.new, + ); + + /// The `json` type in Postgres. + static const CustomSqlType json = + PostgresType(type: PgDataType.json, name: 'json'); + + /// The `jsonb` type in Postgres. + static const CustomSqlType jsonb = + PostgresType(type: PgDataType.json, name: 'jsonb'); + + /// The `point` type in Postgres. + static const CustomSqlType point = PointType(); +} + +/// A wrapper for values with the Postgres `timestamp without timezone` and +/// `timestamp with timezone` types. +/// +/// We can't use [DateTime] directly because drift expects to store them as +/// unix timestamp or text. +final class PgDateTime implements PgTimeValue { + final DateTime dateTime; + + PgDateTime(this.dateTime); + + @override + int get hashCode => dateTime.hashCode; + + @override + bool operator ==(Object other) { + return other is PgDateTime && other.dateTime == dateTime; + } + + @override + DateTime toDateTime() => dateTime; + + @override + String toString() => dateTime.toString(); +} + +/// A wrapper for the Postgres `date` type, which stores dates (year, month, +/// days). +final class PgDate implements PgTimeValue { + final int year, month, day; + final DateTime _dateTime; + + PgDate({required this.year, required this.month, required this.day}) + : _dateTime = DateTime(year, month, day); + + PgDate.fromDateTime(DateTime dateTime) + : _dateTime = dateTime, + year = dateTime.year, + month = dateTime.month, + day = dateTime.day; + + @override + int get hashCode => Object.hash(year, month, day); + + @override + bool operator ==(Object other) { + return other is PgDate && + other.year == year && + other.month == month && + other.day == day; + } + + @override + String toString() => + '${year.toString().padLeft(4, '0')}-${month.toString().padLeft(2, '0')}-${day.toString().padLeft(2, '0')}'; + + @override + DateTime toDateTime() { + return _dateTime; + } +} + +/// Calls the `gen_random_uuid` function in postgres. +Expression genRandomUuid() { + return FunctionCallExpression('gen_random_uuid', []); +} diff --git a/extras/drift_postgres/lib/postgres.dart b/extras/drift_postgres/lib/postgres.dart index 71909d50..320d9b5d 100644 --- a/extras/drift_postgres/lib/postgres.dart +++ b/extras/drift_postgres/lib/postgres.dart @@ -1,108 +1,2 @@ -/// PostgreSQL -@experimental -library drift.postgres; - -import 'package:drift/drift.dart'; -import 'package:meta/meta.dart'; -import 'package:postgres/postgres_v3_experimental.dart'; -import 'package:uuid/uuid.dart'; - -import 'src/types.dart'; - -export 'src/pg_database.dart'; -export 'package:uuid/uuid_value.dart' show UuidValue; - -typedef UuidColumn = Column; -typedef IntervalColumn = Column; -typedef JsonColumn = Column; -typedef PointColumn = Column; -typedef TimestampColumn = Column; -typedef PgDateColumn = Column; - -final class PgTypes { - PgTypes._(); - - static const CustomSqlType uuid = UuidType(); - static const CustomSqlType interval = IntervalType(); - static const CustomSqlType date = DateType( - PgDataType.date, - 'date', - PgDate.fromDateTime, - ); - static const CustomSqlType timestampNoTimezone = DateType( - PgDataType.timestampWithoutTimezone, - 'timestamp without time zone', - PgDateTime.new, - ); - static const CustomSqlType json = - PostgresType(type: PgDataType.json, name: 'json'); - static const CustomSqlType jsonb = - PostgresType(type: PgDataType.json, name: 'jsonb'); - static const CustomSqlType point = PointType(); -} - -/// A wrapper for values with the Postgres `timestamp without timezone` and -/// `timestamp with timezone` types. -/// -/// We can't use [DateTime] directly because drift expects to store them as -/// unix timestamp or text. -final class PgDateTime implements PgTimeValue { - final DateTime dateTime; - - PgDateTime(this.dateTime); - - @override - int get hashCode => dateTime.hashCode; - - @override - bool operator ==(Object other) { - return other is PgDateTime && other.dateTime == dateTime; - } - - @override - DateTime toDateTime() => dateTime; - - @override - String toString() => dateTime.toString(); -} - -/// A wrapper for the Postgres `date` type, which stores dates (year, month, -/// days). -final class PgDate implements PgTimeValue { - final int year, month, day; - final DateTime _dateTime; - - PgDate({required this.year, required this.month, required this.day}) - : _dateTime = DateTime(year, month, day); - - PgDate.fromDateTime(DateTime dateTime) - : _dateTime = dateTime, - year = dateTime.year, - month = dateTime.month, - day = dateTime.day; - - @override - int get hashCode => Object.hash(year, month, day); - - @override - bool operator ==(Object other) { - return other is PgDate && - other.year == year && - other.month == month && - other.day == day; - } - - @override - String toString() => - '${year.toString().padLeft(4, '0')}-${month.toString().padLeft(2, '0')}-${day.toString().padLeft(2, '0')}'; - - @override - DateTime toDateTime() { - return _dateTime; - } -} - -/// Calls the `gen_random_uuid` function in postgres. -Expression genRandomUuid() { - return FunctionCallExpression('gen_random_uuid', []); -} +@Deprecated('Import `package:drift_postgres/drift_postgres.dart` instead') +export 'drift_postgres.dart'; diff --git a/extras/drift_postgres/test/drift_postgres_test.dart b/extras/drift_postgres/test/drift_postgres_test.dart index 32ed74e6..50c07ab3 100644 --- a/extras/drift_postgres/test/drift_postgres_test.dart +++ b/extras/drift_postgres/test/drift_postgres_test.dart @@ -1,4 +1,4 @@ -import 'package:drift_postgres/postgres.dart'; +import 'package:drift_postgres/drift_postgres.dart'; import 'package:drift_testcases/tests.dart'; import 'package:postgres/postgres_v3_experimental.dart'; diff --git a/extras/drift_postgres/test/types_test.dart b/extras/drift_postgres/test/types_test.dart index 4dad255c..d8fde73a 100644 --- a/extras/drift_postgres/test/types_test.dart +++ b/extras/drift_postgres/test/types_test.dart @@ -1,5 +1,5 @@ import 'package:drift/drift.dart'; -import 'package:drift_postgres/postgres.dart'; +import 'package:drift_postgres/drift_postgres.dart'; import 'package:postgres/postgres_v3_experimental.dart'; import 'package:test/test.dart'; import 'package:uuid/uuid.dart';