Giter Site home page Giter Site logo

Relational Filter about directus-dart HOT 6 CLOSED

apstanisic avatar apstanisic commented on June 12, 2024
Relational Filter

from directus-dart.

Comments (6)

wwwhttpru avatar wwwhttpru commented on June 12, 2024

My solution to the problem was the following:
add in filter.dart

  const Filter.child(String value, Filter filter)
      : value = filter,
        comparison = value;

and
change toMapEntry method

  MapEntry<String, Object?> toMapEntry(String field) {
    // If value is a list of non filters
    if (value is List<Map<String, Filter>>) {
      if (comparison == '_or' || comparison == '_and') {
        return MapEntry(
          comparison,
          convertFilterList(value! as List<Map<String, Filter>>),
        );
      }
    }

    if (value != null && value is Filter) {
      final filter = value! as Filter;
      final childMap = filter.toMapEntry(comparison);
      return MapEntry(field, {
        childMap.key: childMap.value,
      });
    }

    return MapEntry(field, {comparison: value});
  }

from directus-dart.

wwwhttpru avatar wwwhttpru commented on June 12, 2024

Вот пример реализации в коде

final f = Filters(
      <String, Filter>{
        'author': Filter.child(
          'name',
          Filter.eq('Rijk van Zanten'),
        ),
      },
    );

result: f.toMap();

{author: {name: {_eq: Rijk van Zanten}}}

from directus-dart.

apstanisic avatar apstanisic commented on June 12, 2024

Maybe we can change Map<String, Filter> data; to Map<String, object>

Map<String, Filter> data;

Only problem is that with that way we lose some type safety, but user could then pass simple JSON.
Do you think it's to verbose having to type Filter.xxx for every comparison?

from directus-dart.

wwwhttpru avatar wwwhttpru commented on June 12, 2024

I have used my own class before and I don't think it's very safe.

abstract class ApiFilter {
  ApiFilter._();

  static Map<String, Object?> filterItem(
    String superFiled,
    Object items,
  ) {
    return <String, Object?>{
      superFiled: items,
    };
  }

  static Map<String, Object?> filterItems(List<Map<String, Object?>> items) {
    final map = <String, Object?>{};
    for (final item in items) {
      map.addAll(item);
    }
    return map;
  }

  /// Равно
  static const String eq = '_eq';

  /// Не равно
  static const String neq = '_neq';

  /// Меньше
  static const String lt = '_lt';

  /// Меньше или равно
  static const String lte = '_lte';
  ...
}

I don't see any difficulty in writing Filter.xxx
I am ready to consider different implementations, but I would like to do so that would not spoil the current api

from directus-dart.

apstanisic avatar apstanisic commented on June 12, 2024

Yeah, if you can create PR without breaking change, that would be awesome

from directus-dart.

apstanisic avatar apstanisic commented on June 12, 2024

Closed in #37

from directus-dart.

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.