GithubHelp home page GithubHelp logo

Comments (2)

simolus3 avatar simolus3 commented on June 12, 2024

Is there a way for me to differentiate between the two?

At the moment, no there isn't. The postgres types are supposed to work on postgres only.

As a (horrible) workaround, you can have the type remember whether it's running against postgres or not:

class MultiDialectDate implements CustomSqlType<PgDate> {
  bool isPostgres = false;

  @override
  String mapToSqlLiteral(PgDate dartValue) {
    if (isPostgres) {
      return PgTypes.date.mapToSqlLiteral(dartValue);
    } else {
      return "'${dartValue.toDateTime().toIso8601String()}'";
    }
  }

  @override
  Object mapToSqlParameter(PgDate dartValue) {
    if (isPostgres) {
      return PgTypes.date.mapToSqlParameter(dartValue);
    } else {
      return dartValue.toDateTime().toIso8601String();
    }
  }

  @override
  PgDate read(Object fromSql) {
    if (isPostgres) {
      return PgTypes.date.read(fromSql);
    } else {
      return PgDate.fromDateTime(DateTime.parse(fromSql as String));
    }
  }

  @override
  String sqlTypeName(GenerationContext context) {
    if (isPostgres) {
      return 'date';
    } else {
      return 'text';
    }
  }
}

Then you can configure this after creating the database instance like this

  static final DriftPostgresDatabase _instance =
      DriftPostgresDatabase(LazyDatabase(() async {
    return NativeDatabase(File('rohan.sqlite'), logStatements: true);
  }))
        .._markIsPostgres(false);
  static final DriftPostgresDatabase _remote = DriftPostgresDatabase(PgDatabase(
    endpoint: pg.Endpoint(
      host: 'localhost',
      database: 'postgres',
      username: 'postgres',
      password: 'postgres',
    ),
    settings: pg.ConnectionSettings(
      // If you expect to talk to a Postgres database over a public connection,
      // please use SslMode.verifyFull instead.
      sslMode: pg.SslMode.disable,
    ),
    logStatements: true,
  ))
    .._markIsPostgres(true);

  void _markIsPostgres(bool postgres) {
    for (final table in allTables) {
      for (final column in table.$columns) {
        final type = column.type;
        if (type is MultiDialectDate) {
          type.isPostgres = postgres;
        }
      }
    }
  }

This fixes the problem, but it's horrible - I'll come up with a better API to have dialect-specific types.

from drift.

simolus3 avatar simolus3 commented on June 12, 2024

It will be possible to properly define dialect-specific custom types in drift 2.15 (with f25eaf1).

from drift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.