Use preferred import for drift postgres

This commit is contained in:
Simon Binder 2023-10-20 12:12:08 +02:00
parent 7c91ced9b2
commit e12e8fdbc9
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
6 changed files with 141 additions and 112 deletions

View File

@ -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';

View File

@ -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';

View File

@ -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<UuidValue>;
/// Type for columns storing [Duration]s as intervals.
typedef IntervalColumn = Column<Duration>;
/// Type for columns storing JSON structures.
typedef JsonColumn = Column<Object>;
/// Type for columns storing [PgPoint]s.
typedef PointColumn = Column<PgPoint>;
/// Type for columns storing dates directly as ([PgDateTime]).
typedef TimestampColumn = Column<PgDateTime>;
/// Type for columns storing dates directly as ([PgDate]).
typedef PgDateColumn = Column<PgDate>;
/// 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<UuidValue> uuid = UuidType();
/// The `interval` type in Postgres.
static const CustomSqlType<Duration> interval = IntervalType();
/// The `date` type in Postgres.
static const CustomSqlType<PgDate> date = DateType(
PgDataType.date,
'date',
PgDate.fromDateTime,
);
/// The `timestamp without time zone` type in Postgres.
static const CustomSqlType<PgDateTime> timestampNoTimezone = DateType(
PgDataType.timestampWithoutTimezone,
'timestamp without time zone',
PgDateTime.new,
);
/// The `json` type in Postgres.
static const CustomSqlType<Object> json =
PostgresType(type: PgDataType.json, name: 'json');
/// The `jsonb` type in Postgres.
static const CustomSqlType<Object> jsonb =
PostgresType(type: PgDataType.json, name: 'jsonb');
/// The `point` type in Postgres.
static const CustomSqlType<PgPoint> 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<UuidValue> genRandomUuid() {
return FunctionCallExpression('gen_random_uuid', []);
}

View File

@ -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<UuidValue>;
typedef IntervalColumn = Column<Duration>;
typedef JsonColumn = Column<Object>;
typedef PointColumn = Column<PgPoint>;
typedef TimestampColumn = Column<PgDateTime>;
typedef PgDateColumn = Column<PgDate>;
final class PgTypes {
PgTypes._();
static const CustomSqlType<UuidValue> uuid = UuidType();
static const CustomSqlType<Duration> interval = IntervalType();
static const CustomSqlType<PgDate> date = DateType(
PgDataType.date,
'date',
PgDate.fromDateTime,
);
static const CustomSqlType<PgDateTime> timestampNoTimezone = DateType(
PgDataType.timestampWithoutTimezone,
'timestamp without time zone',
PgDateTime.new,
);
static const CustomSqlType<Object> json =
PostgresType(type: PgDataType.json, name: 'json');
static const CustomSqlType<Object> jsonb =
PostgresType(type: PgDataType.json, name: 'jsonb');
static const CustomSqlType<PgPoint> 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<UuidValue> genRandomUuid() {
return FunctionCallExpression('gen_random_uuid', []);
}
@Deprecated('Import `package:drift_postgres/drift_postgres.dart` instead')
export 'drift_postgres.dart';

View File

@ -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';

View File

@ -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';