-
Notifications
You must be signed in to change notification settings - Fork 8
/
model.dart
213 lines (164 loc) · 4.6 KB
/
model.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import 'package:hive/hive.dart';
import 'package:objectbox/objectbox.dart' as obx;
part 'model.g.dart';
abstract class EntityWithSettableId {
int get id;
set id(int value);
}
abstract class TestEntity extends EntityWithSettableId {
String get tString;
int get tInt; // 32-bit
int get tLong; // 64-bit
set tLong(int value);
double get tDouble;
static Map<String, dynamic> toMap(TestEntity object) => <String, dynamic>{
'id': object.id == 0 ? null : object.id,
'tString': object.tString,
'tInt': object.tInt,
'tLong': object.tLong,
'tDouble': object.tDouble
};
}
@obx.Entity()
@HiveType(typeId: 1)
class TestEntityPlain implements TestEntity {
@override
@HiveField(0)
int id;
@override
@HiveField(1)
String tString;
@override
@obx.Property(type: obx.PropertyType.int)
@HiveField(2)
int tInt; // 32-bit
@override
@HiveField(3)
int tLong; // 64-bit
@override
@HiveField(4)
double tDouble;
TestEntityPlain(this.id, this.tString, this.tInt, this.tLong, this.tDouble);
static TestEntityPlain fromMap(Map<String, dynamic> map) => TestEntityPlain(
map['id'] ?? 0,
map['tString'],
map['tInt'],
map['tLong'],
map['tDouble']);
}
// A separate entity for queried data so that indexes don't change CRUD results.
@obx.Entity()
class TestEntityIndexed implements TestEntity {
@override
int id;
@override
@obx.Index()
String tString;
@override
@obx.Index()
@obx.Property(type: obx.PropertyType.int)
int tInt; // 32-bit
@override
int tLong; // 64-bit
@override
double tDouble;
TestEntityIndexed(this.id, this.tString, this.tInt, this.tLong, this.tDouble);
static TestEntityIndexed fromMap(Map<String, dynamic> map) =>
TestEntityIndexed(map['id'] ?? 0, map['tString'], map['tInt'],
map['tLong'], map['tDouble']);
}
abstract class RelSourceEntity extends EntityWithSettableId {
@override
int get id;
@override
set id(int value);
String get tString;
int get tLong;
// for hive & sqflite
int get relTargetId;
static Map<String, dynamic> toMap(RelSourceEntity object) =>
<String, dynamic>{
'id': object.id == 0 ? null : object.id,
'tString': object.tString,
'tLong': object.tLong,
'relTargetId': object.relTargetId,
};
}
@obx.Entity()
@HiveType(typeId: 2)
class RelSourceEntityPlain implements RelSourceEntity {
@override
@HiveField(0)
int id;
@override
@HiveField(1)
final String tString;
@override
@HiveField(2)
final int tLong; // 64-bit
final obxRelTarget = obx.ToOne<RelTargetEntity>();
@override
@obx.Transient()
@HiveField(3)
final int relTargetId;
// Note: constructor arg types must match with fromMap used by sqflite.
RelSourceEntityPlain(this.id, this.tString, this.tLong,
[this.relTargetId = 0]) {
obxRelTarget.targetId = relTargetId;
}
RelSourceEntityPlain.forInsert(
this.tString, this.tLong, RelTargetEntity? relTarget)
: id = 0,
relTargetId = relTarget?.id ?? 0 {
obxRelTarget.targetId = relTargetId;
}
static RelSourceEntityPlain fromMap(Map<String, dynamic> map) =>
RelSourceEntityPlain(
map['id'] ?? 0, map['tString'], map['tLong'], map['relTargetId']);
}
@obx.Entity()
class RelSourceEntityIndexed implements RelSourceEntity {
@override
int id;
@override
@obx.Index()
final String tString;
@override
final int tLong; // 64-bit
final obxRelTarget = obx.ToOne<RelTargetEntity>();
@override
@obx.Transient()
final int relTargetId;
// Note: constructor arg types must match with fromMap used by sqflite.
RelSourceEntityIndexed(this.id, this.tString, this.tLong,
[this.relTargetId = 0]) {
obxRelTarget.targetId = relTargetId;
}
RelSourceEntityIndexed.forInsert(
this.tString, this.tLong, RelTargetEntity? relTarget)
: id = 0,
relTargetId = relTarget?.id ?? 0 {
obxRelTarget.targetId = relTargetId;
}
static RelSourceEntityIndexed fromMap(Map<String, dynamic> map) =>
RelSourceEntityIndexed(
map['id'] ?? 0, map['tString'], map['tLong'], map['relTargetId']);
}
@obx.Entity()
@HiveType(typeId: 4)
class RelTargetEntity extends EntityWithSettableId {
@override
@HiveField(0)
int id;
@obx.Index()
@HiveField(1)
String name;
RelTargetEntity(this.id, this.name);
static Map<String, dynamic> toMap(RelTargetEntity object) =>
<String, dynamic>{
'id': object.id == 0 ? null : object.id,
'name': object.name
};
static RelTargetEntity fromMap(Map<String, dynamic> map) =>
RelTargetEntity(map['id'] ?? 0, map['name']);
}