instanceFieldsAndMethods property

  1. @override
List<String> get instanceFieldsAndMethods

Any instance fields that should be copied to the adapter. Should terminate in ; if required.

Implementation

@override
List<String> get instanceFieldsAndMethods {
  final fieldsToColumns = <String>[];
  final uniqueFields = <String, String>{};

  fieldsToColumns.add('''
    '${InsertTable.PRIMARY_KEY_FIELD}': const RuntimeSqliteColumnDefinition(
      association: false,
      columnName: '${InsertTable.PRIMARY_KEY_COLUMN}',
      iterable: false,
      type: int,
    )''');

  for (final field in unignoredFields) {
    final annotation = fields.annotationForField(field);
    final checker = checkerForType(field.type);
    final columnName = providerNameForField(annotation.name, checker: checker);
    final columnInsertionType = checker.withoutNullResultType;

    // T0D0 support List<Future<Sibling>> for 'association'
    fieldsToColumns.add('''
        '${field.name}': const RuntimeSqliteColumnDefinition(
          association: ${checker.isSibling || (checker.isIterable && checker.isArgTypeASibling)},
          columnName: '$columnName',
          iterable: ${checker.isIterable},
          type: $columnInsertionType,
        )''');
    if (annotation.unique) {
      final value = uniqueValueForField(field.name, checker: checker);
      uniqueFields[value] = columnName;
    }
  }

  final primaryKeyByUniqueColumns = generateUniqueSqliteFunction(uniqueFields);
  final afterSaveCallbacks = unignoredFields.where((f) {
    final checker = checkerForType(f.type);
    return checker.isIterable && checker.isArgTypeASibling;
  }).map(_saveIterableAssociationFieldToJoins);

  return [
    '@override\nfinal Map<String, RuntimeSqliteColumnDefinition> fieldsToSqliteColumns = {${fieldsToColumns.join(',\n')}};',
    primaryKeyByUniqueColumns,
    "@override\nfinal String tableName = '$tableName';",
    if (afterSaveCallbacks.isNotEmpty)
      "@override\nFuture<void> afterSave(instance, {required provider, repository}) async {${afterSaveCallbacks.join('\n')}}",
  ];
}