drift icon indicating copy to clipboard operation
drift copied to clipboard

Table code generation with conditional export library

Open AlexandreAndrade00 opened this issue 1 year ago • 2 comments

This table:

import 'package:persistence/src/database/type_converters/tsid_type_converter.dart';

CREATE TABLE dynamic_tables (
    id INTEGER PRIMARY KEY MAPPED BY `const TsidTypeConverter()`,
    name TEXT(512) NOT NULL,
    permissions TEXT(512) NOT NULL,
    max_entries INTEGER NOT NULL,
    version INTEGER NOT NULL
);

Generates the following imports:

import 'package:drift/drift.dart' as i0;
import 'package:persistence/src/database/models/dynamic_database/dynamic_database.drift.dart'
    as i1;
import 'package:tsid_dart/src/tsid_stub.dart' as i2;
import 'package:persistence/src/database/type_converters/tsid_type_converter.dart'
    as i3;
import 'package:persistence/src/database/models/dynamic_database/dart_sql_type.dart'
    as i4;

Problem: In the third import, the code is accessing a private file instead of using the public library:

import 'package:tsid_dart/tsid_dart.dart';

https://pub.dev/packages/tsid_dart package:tsid_dart/tsid_dart.dart file :

export 'src/tsid_stub.dart'
    if (dart.library.io) 'src/tsid_default.dart'
    if (dart.library.html) 'src/tsid_web.dart';
export 'src/tsid_error.dart';

AlexandreAndrade00 avatar Apr 12 '24 17:04 AlexandreAndrade00

We're using the import that the analyzer gives us - I'll ask around if there's a way to get the import URI you've used that contributed the type instead of having to go directly to the definition.

It's probably not going to work right now, but we could add support for a workaround that let's you do something like

typedef MyId = Tsid;

class TsidTypeConverter extends TypeConverter<id, MyId> {...}

Then when drift sees that type it could reference MyId instead of the underlying type. That would fix the import problem, but it's also kind of hacky.

simolus3 avatar Apr 15 '24 19:04 simolus3

Thank you for the fast reply :grin:

AlexandreAndrade00 avatar Apr 15 '24 20:04 AlexandreAndrade00