Skip to content

Commit 9ac090e

Browse files
IsolationLevel (#3)
* wip * fix * rm commented-out code * extract to file * add comments * docs * fix * upd * test * upd * flutter_version * test * ? * blessrng * blessRNG * experiment * up * ultra bless * . * maybe closer * not bruh * please * <> * 777 * pls * ultra please * pls * hope * fix * fix
1 parent 4637546 commit 9ac090e

File tree

5 files changed

+72
-6
lines changed

5 files changed

+72
-6
lines changed

example/lib/main.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ Future<Response> fetch(Request _) async {
99

1010
final client = Client(dbUrl);
1111
await client.connect();
12-
final result = await client.transaction('transaction', transaction);
12+
final result = await client.transaction(
13+
'transaction',
14+
transaction,
15+
TransactionOptions(isolationLevel: IsolationLevel.serializable),
16+
);
1317
await client.end();
1418

1519
return Response(result.rows.map(rowToPrettyString).join('\n\n'));

lib/deno_postgres_interop.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
library;
33

44
export 'src/client.dart';
5+
export 'src/isolation_level.dart';
56
export 'src/query_client.dart';
67
export 'src/query_object_result.dart';
78
export 'src/transaction.dart';
9+
export 'src/transaction_options.dart';

lib/src/isolation_level.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// https://www.postgresql.org/docs/current/transaction-iso.html
2+
enum IsolationLevel {
3+
/// [postgresql/Transaction Isolation/Read Committed Isolation Level](https://www.postgresql.org/docs/current/transaction-iso.html#XACT-READ-COMMITTED)
4+
readCommitted,
5+
6+
/// [postgresql/Transaction Isolation/Repeatable Read Isolation Level](https://www.postgresql.org/docs/current/transaction-iso.html#XACT-REPEATABLE-READ)
7+
repeatableRead,
8+
9+
/// [postgresql/Transaction Isolation/Serializable Isolation Level](https://www.postgresql.org/docs/current/transaction-iso.html#XACT-SERIALIZABLE)
10+
serializable;
11+
12+
/// Parses a string containing an [IsolationLevel] literal into its instance.
13+
static IsolationLevel parse(String string) =>
14+
values.firstWhere((e) => e.name == string);
15+
}

lib/src/query_client.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:js_util';
33

44
import 'package:deno_postgres_interop/src/query_object_result.dart';
55
import 'package:deno_postgres_interop/src/transaction.dart';
6+
import 'package:deno_postgres_interop/src/transaction_options.dart';
67
import 'package:deno_postgres_interop/src/util.dart';
78

89
/// [[email protected]/QueryClient](https://deno.land/x/[email protected]/mod.ts?s=QueryClient).
@@ -18,17 +19,25 @@ extension QueryClientProps on QueryClient {
1819
Future<void> end() => callFutureMethod(this, 'end');
1920

2021
/// [[email protected]/QueryClient/createTransaction](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_createTransaction_0).
21-
Transaction createTransaction(String name) =>
22-
callMethod(this, 'createTransaction', [name]);
22+
Transaction createTransaction(String name, [TransactionOptions? options]) =>
23+
callMethod(
24+
this,
25+
'createTransaction',
26+
[
27+
name,
28+
if (options != null) options,
29+
],
30+
);
2331

2432
/// Convinience wrapper for [createTransaction],
2533
/// [TransactionProps.begin],
2634
/// and [TransactionProps.commit].
2735
Future<T> transaction<T>(
2836
String name,
29-
Future<T> Function(Transaction) f,
30-
) async {
31-
final transaction = createTransaction(name);
37+
Future<T> Function(Transaction) f, [
38+
TransactionOptions? options,
39+
]) async {
40+
final transaction = createTransaction(name, options);
3241
await transaction.begin();
3342
final result = await f(transaction);
3443
await transaction.commit();

lib/src/transaction_options.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'dart:js_interop';
2+
import 'dart:js_util';
3+
4+
import 'package:deno_postgres_interop/src/isolation_level.dart';
5+
6+
/// [[email protected]/TransactionOptions](https://deno.land/x/[email protected]/mod.ts?s=TransactionOptions)
7+
@JS()
8+
class TransactionOptions {
9+
/// [[email protected]/TransactionOptions/snapshot](https://deno.land/x/[email protected]/mod.ts?s=TransactionOptions)
10+
external String? get snapshot;
11+
12+
/// [[email protected]/TransactionOptions/constructor](https://deno.land/x/[email protected]/mod.ts?s=TransactionOptions)
13+
factory TransactionOptions({
14+
IsolationLevel? isolationLevel,
15+
bool? isReadOnly,
16+
String? snapshot,
17+
}) =>
18+
jsify({
19+
if (isolationLevel != null) 'isolation_level': isolationLevel.name,
20+
if (isReadOnly != null) 'read_only': isReadOnly,
21+
if (snapshot != null) 'snapshot': snapshot,
22+
}) as TransactionOptions;
23+
}
24+
25+
/// [[email protected]/TransactionOptions](https://deno.land/x/[email protected]/mod.ts?s=TransactionOptions)
26+
extension TransactionOptionsProps on TransactionOptions {
27+
/// [[email protected]/TransactionOptions/isolation_level](https://deno.land/x/[email protected]/mod.ts?s=TransactionOptions)
28+
IsolationLevel? get isolationLevel {
29+
final jsProperty = getProperty<String?>(this, 'isolation_level');
30+
31+
return jsProperty == null ? null : IsolationLevel.parse(jsProperty);
32+
}
33+
34+
/// [[email protected]/TransactionOptions/read_only](https://deno.land/x/[email protected]/mod.ts?s=TransactionOptions)
35+
bool? get isReadOnly => getProperty(this, 'read_only');
36+
}

0 commit comments

Comments
 (0)