-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathschema.dart
106 lines (89 loc) · 2.93 KB
/
schema.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
part of 'models.dart';
abstract class BaseSchema {
/// [fields] used for querying, filtering and faceting.
final Set<Field> fields;
BaseSchema(this.fields);
Map<String, dynamic> toMap() {
final map = <String, dynamic>{};
map['fields'] = fields.map((field) => field.toMap()).toList();
return map;
}
@override
String toString() => toMap().toString();
}
class Schema extends BaseSchema {
/// [name] of the collection.
final String name;
/// Number of documents currently in the collection [name].
final int? documentCount;
/// A field in [fields] which will determine the order in which the search
/// results are ranked when a `sort_by` clause is not provided during
/// searching.
final Field? defaultSortingField;
/// Boolean to enable nested fields on the schema, only available for typesense 0.24 or more
final bool? enableNestedFields;
Schema(
this.name,
super.fields, {
this.defaultSortingField,
this.documentCount,
this.enableNestedFields,
});
factory Schema.fromMap(Map<String, dynamic> map) {
if ((map['name'] as String).isEmpty) {
throw ArgumentError('Ensure Schema.name is not empty');
}
final Set<Field> fields = (map['fields'] != null)
? (map['fields'] as List).map((field) => Field.fromMap(field)).toSet()
: {};
final String? sortingFieldName = map['default_sorting_field'];
final Field? defaultSortingField = (fields.isNotEmpty &&
sortingFieldName != null &&
sortingFieldName.isNotEmpty)
? fields.firstWhere(
(field) => map['default_sorting_field'] == field.name,
orElse: () => throw _defaultSortingFieldNotInSchema(
sortingFieldName,
),
)
: null;
final bool? enableNestedFields = map["enable_nested_fields"];
return Schema(
map['name'],
fields,
documentCount: map['num_documents'] ?? 0,
defaultSortingField: defaultSortingField,
enableNestedFields: enableNestedFields,
);
}
@override
Map<String, dynamic> toMap() {
final map = super.toMap();
map['name'] = name;
if (defaultSortingField != null) {
map['default_sorting_field'] = defaultSortingField!.name;
}
if (documentCount != null) {
map['num_documents'] = documentCount;
}
if (enableNestedFields != null) {
map['enable_nested_fields'] = enableNestedFields;
}
return map;
}
}
class UpdateSchema extends BaseSchema {
UpdateSchema(Set<UpdateField> super.fields);
factory UpdateSchema.fromMap(Map<String, dynamic> map) {
final Set<UpdateField> fields = (map['fields'] != null)
? (map['fields'] as List)
.map((field) => UpdateField.fromMap(field))
.toSet()
: {};
return UpdateSchema(
fields,
);
}
}
ArgumentError _defaultSortingFieldNotInSchema(String name) =>
ArgumentError('Ensure defaultSortingField "$name" is present in fields');