schultek / stormberry

Access your postgres database effortlessly from dart code.
https://pub.dev/packages/stormberry
MIT License
66 stars 16 forks source link

Insert/Update ManyToMany relations #54

Open f3l1x98 opened 1 year ago

f3l1x98 commented 1 year ago

At the moment it is not possible to update ManyToMany , ManyToOne or OneToMany relations, because the insert/update models are missing the required attributes (and query). That is required, because there is no direct access to the JoinTables (in case of ManyToMany).

Example

// game_model.dart:
abstract class Game {
  @PrimaryKey()
  @AutoIncrement()
  int get id;
  List<Genre> get genres;
  @BindTo(#sequel)
  Game? get prequel;
  @BindTo(#prequel)
  Game? get sequel;
}

// genre_model.dart:
abstract class Genre {
  @PrimaryKey()
  @AutoIncrement()
  int get id;
  String get name;
  String? get description;
  List<Game> get games;
}

generates:

// game_model.schema.dart:
// [...]
class GameInsertRequest {
  GameInsertRequest({
    this.prequelId,
  });

  int? prequelId;
}

class GameUpdateRequest {
  GameUpdateRequest({
    required this.id,
    this.prequelId,
  });

  int id;
  int? prequelId;
}

// genre_model.schema.dart:
// [...]
class GenreInsertRequest {
  GenreInsertRequest({
    required this.name,
    this.description,
  });

  String name;
  String? description;
}

class GenreUpdateRequest {
  GenreUpdateRequest({
    required this.id,
    this.name,
    this.description,
  });

  int id;
  String? name;
  String? description;
}
schultek commented 1 year ago

I see the problem with many-to-many and missing access to join tables. But can you elaborate whats missing with many-to-one and one-to-many?

f3l1x98 commented 1 year ago

I see the problem with many-to-many and missing access to join tables. But can you elaborate whats missing with many-to-one and one-to-many?

My bad. ManyToOne and OneToMany works, just forgot that I changed one of my ManyToOne relations to ManyToMany.