Skip to content

Commit 328df7e

Browse files
feat(orm): updated api structure
1 parent 9a6c652 commit 328df7e

File tree

6 files changed

+95
-61
lines changed

6 files changed

+95
-61
lines changed

packages/dartseid_orm/example/api/basic.dart

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import 'package:dartseid_orm/dartseid_orm.dart';
22

33
class DormMockAdapter implements DormAdapterBase {
4-
final dynamic _connection;
4+
@override
5+
late Dorm dorm;
6+
@override
7+
final dynamic connection;
58

69
DormMockAdapter({
7-
required dynamic connection,
8-
}) : _connection = connection;
10+
required this.connection,
11+
});
912

1013
@override
1114
Future<void> init() {
@@ -39,6 +42,11 @@ class DormMockAdapter implements DormAdapterBase {
3942
// TODO: implement transaction
4043
throw UnimplementedError();
4144
}
45+
46+
@override
47+
void setDormInstance(Dorm dorm) {
48+
this.dorm = dorm;
49+
}
4250
}
4351

4452
Future<dynamic> dorming() async {
@@ -49,8 +57,9 @@ Future<dynamic> dorming() async {
4957
final t = a.table(
5058
"users",
5159
{
52-
"id": ColumnInt(),
60+
"id": ColumnInt()..nullable(),
5361
"name": ColumnString(),
62+
"json": ColumnJson(),
5463
},
5564
converter: (
5665
fromJson: (Map<String, dynamic> j) {},
@@ -68,6 +77,7 @@ Future<dynamic> dorming() async {
6877
final data = await t.transaction().start(
6978
(trx) async {
7079
final r = t.findOne(transaction: trx)
80+
..where({"id": eq(10)})
7181
..where(
7282
and([
7383
{
@@ -104,6 +114,9 @@ Future<dynamic> dorming() async {
104114
]),
105115
joinType: JoinOperation.left,
106116
)
117+
..group("id")
118+
..sort({"name": 1})
119+
..having(and([]))
107120
..limit(10)
108121
..skip(0);
109122

packages/dartseid_orm/lib/src/adapter.dart

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import 'dart:async';
22

33
import 'package:dartseid_orm/src/core.dart';
44
import 'package:dartseid_orm/src/query.dart';
5+
import 'package:meta/meta.dart';
56

67
abstract interface class DormAdapterBase {
7-
final dynamic _connection;
8+
@protected
9+
final dynamic connection;
10+
@protected
11+
late Dorm dorm;
812

913
DormAdapterBase({
10-
required dynamic connection,
11-
}) : _connection = connection;
14+
required this.connection,
15+
});
1216

1317
Future<void> init();
1418
Future<Map<String, dynamic>> operate({
@@ -28,6 +32,9 @@ abstract interface class DormAdapterBase {
2832
int? limit,
2933
int? skip,
3034
});
35+
36+
void setDormInstance(Dorm dorm);
37+
3138
DormTransaction transaction();
3239
}
3340

@@ -37,47 +44,13 @@ abstract class DormTransaction {
3744
bool isRolledBack = false;
3845

3946
Future<void> commit() async {
40-
if (!isStarted) {
41-
throw DormException(
42-
message: "Cannot Commit a transaction that has not been started",
43-
originalError: null,
44-
);
45-
}
46-
if (isCommitted) {
47-
throw DormException(
48-
message: "Cannot Commit a transaction that is already committed",
49-
originalError: null,
50-
);
51-
}
52-
if (isRolledBack) {
53-
throw DormException(
54-
message: "Cannot Commit a transaction that has been rolled back",
55-
originalError: null,
56-
);
57-
}
47+
_checkPrecondition("Commit");
5848
await _commit();
5949
isCommitted = true;
6050
}
6151

6252
Future<void> rollback() async {
63-
if (!isStarted) {
64-
throw DormException(
65-
message: "Cannot Rollback a transaction that has not been started",
66-
originalError: null,
67-
);
68-
}
69-
if (isCommitted) {
70-
throw DormException(
71-
message: "Cannot Rollback a transaction that is already committed",
72-
originalError: null,
73-
);
74-
}
75-
if (isRolledBack) {
76-
throw DormException(
77-
message: "Cannot Rollback a transaction that is already rolled back",
78-
originalError: null,
79-
);
80-
}
53+
_checkPrecondition("Rollback");
8154
await _rollback();
8255
isRolledBack = true;
8356
}
@@ -86,6 +59,7 @@ abstract class DormTransaction {
8659
FutureOr<T> Function(DormTransaction trx)? callback,
8760
]) async {
8861
try {
62+
_checkStartPreCondition();
8963
await _start();
9064
isStarted = true;
9165
if (callback == null) {
@@ -107,4 +81,40 @@ abstract class DormTransaction {
10781
Future<void> _rollback();
10882

10983
Future<void> _start();
84+
85+
void _checkStartPreCondition() {
86+
if (isCommitted) {
87+
throw DormException(
88+
message: "Cannot Start a transaction that is already committed",
89+
originalError: null,
90+
);
91+
}
92+
if (isRolledBack) {
93+
throw DormException(
94+
message: "Cannot Start a transaction that is already rolled back",
95+
originalError: null,
96+
);
97+
}
98+
}
99+
100+
void _checkPrecondition(String op) {
101+
if (!isStarted) {
102+
throw DormException(
103+
message: "Cannot $op a transaction that has not been started",
104+
originalError: null,
105+
);
106+
}
107+
if (isCommitted) {
108+
throw DormException(
109+
message: "Cannot $op a transaction that is already committed",
110+
originalError: null,
111+
);
112+
}
113+
if (isRolledBack) {
114+
throw DormException(
115+
message: "Cannot $op a transaction that has been rolled back",
116+
originalError: null,
117+
);
118+
}
119+
}
110120
}

packages/dartseid_orm/lib/src/core.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class Dorm with DormTable {
3333
String? name,
3434
}) async {
3535
await adapter.init();
36-
return Dorm._(adapter: adapter, name: name);
36+
final dorm = Dorm._(adapter: adapter, name: name);
37+
adapter.setDormInstance(dorm);
38+
return dorm;
3739
}
3840
}
3941

packages/dartseid_orm/lib/src/query.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Map<String, WhereParam> and(List<Map<String, WhereParam>> value) {
99
for (final element in value) {
1010
element.forEach((key, _) {
1111
element[key]?.isAnd = true;
12+
element[key]?.isOr = false;
1213
});
1314
mergedMap.addAll(element);
1415
}
@@ -20,6 +21,7 @@ Map<String, WhereParam> or(List<Map<String, WhereParam>> value) {
2021
for (final element in value) {
2122
element.forEach((key, _) {
2223
element[key]?.isOr = true;
24+
element[key]?.isAnd = false;
2325
});
2426
mergedMap.addAll(element);
2527
}

packages/dartseid_orm/lib/src/table.dart

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,43 +122,49 @@ class DormTableSchema {
122122
}
123123
}
124124

125-
abstract class ColumnMeta {}
125+
class ColumnMeta {
126+
void nullable() {
127+
// do something
128+
}
129+
}
126130

127-
class ColumnInt implements ColumnMeta {}
131+
class ColumnInt extends ColumnMeta {}
128132

129-
class ColumnIntRange implements ColumnMeta {}
133+
class ColumnIntRange extends ColumnMeta {}
130134

131-
class ColumnFloat implements ColumnMeta {}
135+
class ColumnFloat extends ColumnMeta {}
132136

133-
class ColumnBool implements ColumnMeta {}
137+
class ColumnBool extends ColumnMeta {}
134138

135-
class ColumnString implements ColumnMeta {
139+
class ColumnString extends ColumnMeta {
136140
final dynamic search;
137141

138142
ColumnString({this.search});
139143
}
140144

141-
class ColumnStringEnum implements ColumnMeta {}
145+
class ColumnStringEnum extends ColumnMeta {}
146+
147+
class ColumnStringSet extends ColumnMeta {}
142148

143-
class ColumnStringSet implements ColumnMeta {}
149+
class ColumnBinary extends ColumnMeta {}
144150

145-
class ColumnBinary implements ColumnMeta {}
151+
class ColumnDate extends ColumnMeta {}
146152

147-
class ColumnDate implements ColumnMeta {}
153+
class ColumnDateRange extends ColumnMeta {}
148154

149-
class ColumnDateRange implements ColumnMeta {}
155+
class ColumnJson extends ColumnMeta {}
150156

151-
class ColumnJson implements ColumnMeta {}
157+
class ColumnComposite extends ColumnMeta {}
152158

153-
class ColumnComposite implements ColumnMeta {}
159+
class ColumnGeoJson extends ColumnMeta {}
154160

155-
class ColumnGeoJson implements ColumnMeta {}
161+
class ColumnObjectId extends ColumnMeta {}
156162

157-
class ColumnArray implements ColumnMeta {}
163+
class ColumnArray extends ColumnMeta {}
158164

159-
abstract interface class ColumnCustomDomain implements ColumnMeta {}
165+
abstract interface class ColumnCustomDomain extends ColumnMeta {}
160166

161-
class ColumnCustom implements ColumnMeta {
167+
class ColumnCustom extends ColumnMeta {
162168
final String raw;
163169

164170
ColumnCustom({required this.raw});

packages/dartseid_orm/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ environment:
88

99
# Add regular dependencies here.
1010
dependencies:
11+
meta: ^1.9.1
1112
# path: ^1.8.0
1213

1314
dev_dependencies:

0 commit comments

Comments
 (0)