Skip to content

Commit

Permalink
IsolationLevel (#3)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
danylo-safonov-solid authored Sep 14, 2023
1 parent 4637546 commit 9ac090e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
6 changes: 5 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ Future<Response> fetch(Request _) async {

final client = Client(dbUrl);
await client.connect();
final result = await client.transaction('transaction', transaction);
final result = await client.transaction(
'transaction',
transaction,
TransactionOptions(isolationLevel: IsolationLevel.serializable),
);
await client.end();

return Response(result.rows.map(rowToPrettyString).join('\n\n'));
Expand Down
2 changes: 2 additions & 0 deletions lib/deno_postgres_interop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
library;

export 'src/client.dart';
export 'src/isolation_level.dart';
export 'src/query_client.dart';
export 'src/query_object_result.dart';
export 'src/transaction.dart';
export 'src/transaction_options.dart';
15 changes: 15 additions & 0 deletions lib/src/isolation_level.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// https://www.postgresql.org/docs/current/transaction-iso.html
enum IsolationLevel {
/// [postgresql/Transaction Isolation/Read Committed Isolation Level](https://www.postgresql.org/docs/current/transaction-iso.html#XACT-READ-COMMITTED)
readCommitted,

/// [postgresql/Transaction Isolation/Repeatable Read Isolation Level](https://www.postgresql.org/docs/current/transaction-iso.html#XACT-REPEATABLE-READ)
repeatableRead,

/// [postgresql/Transaction Isolation/Serializable Isolation Level](https://www.postgresql.org/docs/current/transaction-iso.html#XACT-SERIALIZABLE)
serializable;

/// Parses a string containing an [IsolationLevel] literal into its instance.
static IsolationLevel parse(String string) =>
values.firstWhere((e) => e.name == string);
}
19 changes: 14 additions & 5 deletions lib/src/query_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:js_util';

import 'package:deno_postgres_interop/src/query_object_result.dart';
import 'package:deno_postgres_interop/src/transaction.dart';
import 'package:deno_postgres_interop/src/transaction_options.dart';
import 'package:deno_postgres_interop/src/util.dart';

/// [postgres@v0.17.0/QueryClient](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient).
Expand All @@ -18,17 +19,25 @@ extension QueryClientProps on QueryClient {
Future<void> end() => callFutureMethod(this, 'end');

/// [postgres@v0.17.0/QueryClient/createTransaction](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient#method_createTransaction_0).
Transaction createTransaction(String name) =>
callMethod(this, 'createTransaction', [name]);
Transaction createTransaction(String name, [TransactionOptions? options]) =>
callMethod(
this,
'createTransaction',
[
name,
if (options != null) options,
],
);

/// Convinience wrapper for [createTransaction],
/// [TransactionProps.begin],
/// and [TransactionProps.commit].
Future<T> transaction<T>(
String name,
Future<T> Function(Transaction) f,
) async {
final transaction = createTransaction(name);
Future<T> Function(Transaction) f, [
TransactionOptions? options,
]) async {
final transaction = createTransaction(name, options);
await transaction.begin();
final result = await f(transaction);
await transaction.commit();
Expand Down
36 changes: 36 additions & 0 deletions lib/src/transaction_options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'dart:js_interop';
import 'dart:js_util';

import 'package:deno_postgres_interop/src/isolation_level.dart';

/// [postgres@v0.17.0/TransactionOptions](https://deno.land/x/postgres@v0.17.0/mod.ts?s=TransactionOptions)
@JS()
class TransactionOptions {
/// [postgres@v0.17.0/TransactionOptions/snapshot](https://deno.land/x/postgres@v0.17.0/mod.ts?s=TransactionOptions)
external String? get snapshot;

/// [postgres@v0.17.0/TransactionOptions/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=TransactionOptions)
factory TransactionOptions({
IsolationLevel? isolationLevel,
bool? isReadOnly,
String? snapshot,
}) =>
jsify({
if (isolationLevel != null) 'isolation_level': isolationLevel.name,
if (isReadOnly != null) 'read_only': isReadOnly,
if (snapshot != null) 'snapshot': snapshot,
}) as TransactionOptions;
}

/// [postgres@v0.17.0/TransactionOptions](https://deno.land/x/postgres@v0.17.0/mod.ts?s=TransactionOptions)
extension TransactionOptionsProps on TransactionOptions {
/// [postgres@v0.17.0/TransactionOptions/isolation_level](https://deno.land/x/postgres@v0.17.0/mod.ts?s=TransactionOptions)
IsolationLevel? get isolationLevel {
final jsProperty = getProperty<String?>(this, 'isolation_level');

return jsProperty == null ? null : IsolationLevel.parse(jsProperty);
}

/// [postgres@v0.17.0/TransactionOptions/read_only](https://deno.land/x/postgres@v0.17.0/mod.ts?s=TransactionOptions)
bool? get isReadOnly => getProperty(this, 'read_only');
}

0 comments on commit 9ac090e

Please sign in to comment.