1

By following this

I have a CurrentUser entity as follows:

import 'package:admin/core/auth/enums/content_type.dart';
import 'package:admin/core/permission/model/permission.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:objectbox/objectbox.dart';

part 'current_user.freezed.dart';
part 'current_user.g.dart';

@freezed
class CurrentUser with _$CurrentUser {
  @Entity(realClass: CurrentUser)
  factory CurrentUser(
      {@Id(assignable: true) int? localId,
      @Unique() String? id,
      required DateTime createdAt,
      required DateTime updatedAt,
      required String email,
      required String username,
      String? mobile,
      @Default(false) bool? isEmailConfirmed,
      @Default(false) bool? isMobileConfirmed,
      @Default(false) bool? isSuperAdmin,
      @Default([]) List<ContentType>? accessTo,
      DateTime? lastLoginAt,
      DateTime? lastLogoutAt,
      @Default([]) List<Permission>? permissions}) = _CurrentUser;

  factory CurrentUser.fromJson(Map<String, dynamic> json) =>
      _$CurrentUserFromJson(json);
}

But here the field permissions should be a One-to-Many relation with model Permission. How to define the relation in this case?

Thanks in advance.

Michael
  • 13
  • 1
  • 5

1 Answers1

0

You would actually use ToMany (or ToOne) as the parameter type, so

required ToMany<Permission> permissions

for this example. ToMany (or ToOne) has a special constructor for this case that accepts a list of items (or a single item for ToOne).

See also the freezed test entities from objectbox-dart: https://github.com/objectbox/objectbox-dart/blob/main/generator/integration-tests/part-partof/lib/frozen.dart

Uwe - ObjectBox
  • 1,012
  • 1
  • 7
  • 11
  • Thanks. It seems to work. – Michael Nov 30 '21 at 14:33
  • But how can I define an enum converter inside the entity denoted by @freezed? Here the `Permission` entity has an `action` enum representing CRUD, but @freezed seems to only support custom getter. – Michael Nov 30 '21 at 14:39
  • Think I have to use String for such case, and do some type conversions outside of entity class. – Michael Nov 30 '21 at 20:01