Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Simon Binder
    @simolus3
    Ah ok, tests probably won't work. You could try to skip the tests using custom functions on macOS, but I'll try to figure out what causes this in the first place.
    Andrea Antonello
    @moovida
    Thanks @simolus3 , yes right now I moved to the new packages and am just skipping at dart level what does not work.
    Stas
    @stasvo
    I have question about counting, mytable.id.count() returning Expression<int>, is there way how possible to get int from it?
    Simon Binder
    @simolus3
    @stasvo An Expression is something that will evaluate to an int when run on the database. If you add it to a select, you can read the int value from a row. There's an example here.
    Stas
    @stasvo
    @simolus3 But I see that select as argument take table. You mean that need add Expression as column to table? var amount = await myTable.id.count();
    select(mytable).addColumns([amount]); Is there way to use something as temporary table where only 1 column with result?
    Simon Binder
    @simolus3
    I see what you mean. But since your expression needs a table, so does the select. You can use selectOnly(mytable)..addColumns([amount]). The selectOnly will only return the columns you add explicitly.
    Stas
    @stasvo
    @simolus3 Thanks!
    Michael Lee
    @leecommamichael
    Is it possible to have a DataClass constructor with optional named parameters, with a default? (So the underlying column is not nullable, but the DataClass property is optional.)
    Simon Binder
    @simolus3
    No, that's not possible. A data class always represents a full row, so default values for inserts would still result in required parameters.
    PointSource
    @point-source
    I am using a moor file with sql to generate my dart code. This works pretty well but I also want the uuid as default pk functionality described here: https://moor.simonbinder.eu/docs/getting-started/advanced_dart_tables/#default-values Any way to describe this in sql and have moor generate it? Or do I just need to write that table class myself?
    Simon Binder
    @simolus3
    @point-source You can use the regular DEFAULT of course, but unfortunately there's no way to use clientDefault in moor files. So you'd have to use the Dart table DSL for that table.
    Michael Lee
    @leecommamichael
    It appears that using intEnum<T> columns will break the default toJson on the emitted DataClass. This will attempt to invoke toJson on an Enum instance.
    Michael Lee
    @leecommamichael
    I'm using a custom serializer and have implemented a typecheck for my enum in that serializer.
    This solved my issue.
    Simon Binder
    @simolus3
    Yeah, type converters only apply on the conversion between Dart and SQL, not on the conversion between Dart and JSON. So you'd have to use a custom serializer.
    Ruben Vereecken
    @rubenvereecken
    I've already learnt a few things from reading some previous answers on here
    I was just trying to answer this question for myself: I've got this BehaviorSubject. When I get results from an API, they go straight into the BehaviorSubject. If I now decide to persist the results through Moor first and then have db.items().watch() stream to the BehaviorSubject instead, how much slower would that be?
    Trying to figure out whether it's worth optimising and have two streams feeding into the BehaviorSubject: a db.items().watch() and one coming from the API
    Ruben Vereecken
    @rubenvereecken
    Thought I'd share my findings. For 50 objects that I wanted to upsert (delete all + batch insert), it took about 40-50ms, meaning I probably do want to display the API results straight away rather than waiting for Moor
    Michael Lee
    @leecommamichael
    Nitpick: "upsert" refers to insert-or-replace, not delete and insert. So your performance metric may not be what people expect of an upsert.
    Also which device was this measured on? Was it a simulator?
    Jaumard
    @jaumard

    Hello, I’m trying to migrate from Jaguar ORM to Moor, I’ve got a problem with the table definition because the create table query is not working :(
    I’m getting SqliteException(1): near "id": syntax error where the query is:

    CREATE TABLE IF NOT EXISTS kiwi_transactions (id INTEGER NULL, ext_id INTEGER NOT NULL, amount REAL NOT NULL, amount_outstanding REAL NOT NULL, amount_reversed REAL NOT NULL, amount_change REAL NOT NULL, cash_received REAL NULL, currency VARCHAR NOT NULL, type INTEGER NOT NULL, modified INTEGER NOT NULL, added INTEGER NOT NULL, refunded INTEGER NOT NULL CHECK (refunded in (0, 1)), uuid VARCHAR NOT NULL, ext_tx_id VARCHAR NULL, longitude REAL NULL, latitude REAL NULL, is_cancelled INTEGER NOT NULL CHECK (is_cancelled in (0, 1)), is_reversed INTEGER NOT NULL CHECK (is_reversed in (0, 1)), result INTEGER NOT NULL, msi_enabled INTEGER NOT NULL CHECK (msi_enabled in (0, 1)), msi_merchant_id VARCHAR NULL, msi_description VARCHAR NULL, msi_currency VARCHAR NULL, msi_amount INTEGER NULL, msi_duration INTEGER NULL, msi_commission REAL NULL, cart_id INTEGER NULLABLE REFERENCE carts(id), cart_ext_id INTEGER NULLABLE REFERENCE carts(ext_id), events VARCHAR NOT NULL, PRIMARY KEY (ext_id));

    So look like it doesn’t like the carts(id) reference, but what is wrong with it ?

    Jaumard
    @jaumard
    Nervermind it’s REFERENCES not REFERENCE :)
    Plague Fox
    @PlugFox
    thx for great library)
    Simon Binder
    @simolus3
    Glad you like it!
    Justin Wood
    @ankhers
    How would I go about creating a table like create table foo (id INTEGER PRIMARY KEY ON CONFLICT REPLACE, bar TEXT);? I can't seem to figure out the ON CONFLICT REPLACE part.
    Simon Binder
    @simolus3

    You could use

    IntColumn get id => integer().autoIncrement().customConstraint('PRIMARY KEY ON CONFLICT REPLACE');

    The autoIncrement() will not be part of the CREATE TABLE statement at runtime, but it helps the generator to recognize that id isn't a required column for inserts.

    Justin Wood
    @ankhers
    Awesome. Thank you. I will check that out.
    CalvinGonsalves
    @CalvinGonsalves
    Hello @simolus3 , moor 3.4.0 added renameColumn to the Migrator , is there a similar thing for renaming a table ? Thanks
    Simon Binder
    @simolus3
    @CalvinGonsalves Currently there isn't, but you could just create the new table, do a INSERT INTO new_table SELECT * FROM old_table and then drop the old table (might need to adapt foreign keys too)
    Oh wait, sqlite has builtin support for that, so its actually just ALTER TABLE old_table RENAME TO new_table.
    CalvinGonsalves
    @CalvinGonsalves
    Awesome
    Britannio Jarrett
    @britannio
    @simolus3 Does the web implementation of moor support custom functions?
    Simon Binder
    @simolus3
    @britannio At the moment it doesn't. sql.js would have to support it first since moor web just wraps that library.
    Gurkaran Singh
    @TheStarkster
    import 'dart:convert';
    import 'package:moor_flutter/moor_flutter.dart';
    import 'package:json_annotation/json_annotation.dart' as j;
    part 'court_type.g.dart';
    
    @j.JsonSerializable()
    class DataModel {
      int id;
      String name;
      String uniqueUuid;
      String languages;
      String description;
      String timer;
      String importantDates;
      String createdAt;
      String updatedAt;
    
      DataModel({
        this.id,
        this.name,
        this.uniqueUuid,
        this.languages,
        this.description,
        this.timer,
        this.importantDates,
        this.createdAt,
        this.updatedAt});
    
      factory DataModel.fromJson(Map<String, dynamic> json) =>
          _$DataModelFromJson(json);
    
      Map<String, dynamic> toJson() => _$DataModelToJson(this);}
    
    class DataModelConverter extends TypeConverter<DataModel, String> {
      const DataModelConverter();
      @override
      DataModel mapToDart(String fromDb) {
        if (fromDb == null) {
          return null;
        }
        return DataModel.fromJson(json.decode(fromDb) as Map<String, dynamic>);
      }
    
      @override
      String mapToSql(DataModel value) {
        if (value == null) {
          return null;
        }
        return json.encode(value.toJson());
      }
    }
    am i doing anything wrong in this code? my court_type.g.dart file is not generating
    also i am importing this DataModel as text column in my user table in my project
    Jayant Dhingra
    @jayant1441
    I am facing the same issue @TheStarkster
    Simon Binder
    @simolus3
    That code looks alright to me. Moor isn't supposed to generate court_type.g.dart, the json_serializable package should do that. Did you add a dev_dependency on that pacakge?
    Gurkaran Singh
    @TheStarkster

    That code looks alright to me. Moor isn't supposed to generate court_type.g.dart, the json_serializable package should do that. Did you add a dev_dependency on that pacakge?

    thanks man it worked! can you explain something to me? that JsonSerializable was importing fine even when i didn't putted it in dev deps. But now when i putted it after you told me it generated the file? can you explain me what exactly happed?

    why is it important to mention JsonSerializable in dev deps
    Simon Binder
    @simolus3
    The json_annotation package is what you import, you probably had that already. The json_serializable package is not supposed to be imported - the build system recognizes that package and invokes it to generate code.
    Gurkaran Singh
    @TheStarkster

    hi, i am trying to access typeconverter's fields and its getting null
    here's my class

    import 'dart:convert';
    import 'package:moor_flutter/moor_flutter.dart';
    import 'package:json_annotation/json_annotation.dart';
    part 'courtType.typeconverter.g.dart';
    
    @JsonSerializable()
    class CourtTypeColumn {
      CourtTypeColumn();
    
      int _id;
      String _name;
    
      int get id => _id;
      String get name => _name;
    
      factory CourtTypeColumn.fromJson(Map<String, dynamic> json) => _$CourtTypeColumnFromJson(json);
      Map<String, dynamic> toJson() => _$CourtTypeColumnToJson(this);
    }
    
    class CourtTypeConverter extends TypeConverter<CourtTypeColumn, String> {
      const CourtTypeConverter();
      @override
      CourtTypeColumn mapToDart(String fromDb) {
        if (fromDb == null) {
          return null;
        }
        return CourtTypeColumn.fromJson(json.decode(fromDb) as Map<String, dynamic>);
      }
    
      @override
      String mapToSql(CourtTypeColumn value) {
        if (value == null) {
          return null;
        }
        return json.encode(value.toJson());
      }
    }

    and also m pretty sure that data is getting saved because the field is not null in my table and its not even throwing any error while saving data to table

    but when im trying to access it after getting it the data through my dao its getting null

    TextColumn get courtType => text().map(const CourtTypeConverter())();
    above is the field in my table
    &
    that's how im trying to get it
    Future<TbUserData> getUser() {
        return (select(tbUser)..where((tbl) => tbl.status.equals(true))).getSingle();
      }
    and that's how im setting and trying to get the data
    TbUserData user = await _database.userDao.getUser();
    print(user. courtType.name); // which is printing null
    Simon Binder
    @simolus3
    If user.courtType.name prints null (instead of throwing a NoSuchMethodError about name not existing on null), it looks like the type converter was applied? Can you check that CourtTypeColumn.fromJson works as expected?
    Gurkaran Singh
    @TheStarkster

    yea

    If user.courtType.name prints null (instead of throwing a NoSuchMethodError about name not existing on null), it looks like the type converter was applied? Can you check that CourtTypeColumn.fromJson works as expected?

    yea i already checked it it returns an empty map

    Simon Binder
    @simolus3
    Do yo mean that toJson returns an empty map? It look like json_serializable might have problems picking up your class. Maybe it's because you split fields and getters, does it work if you just use final fields?
    Gurkaran Singh
    @TheStarkster
    ALRIGHT! guess what! it was my fault as always, i just made fields public and now its generating files correctly and now working fine, earlier typeconverter.g.dart file was not generating correctly it was returning empty instance because of those private fields
    Simon Binder
    @simolus3
    No worries, glad you were able to solve the problem.