Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
danylo-safonov-solid committed Sep 13, 2023
1 parent 975efba commit 23562f5
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 8 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
1 change: 1 addition & 0 deletions lib/deno_postgres_interop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export 'src/client.dart';
export 'src/query_client.dart';
export 'src/query_object_result.dart';
export 'src/transaction.dart';
export 'src/transaction_options.dart';
25 changes: 20 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,31 @@ 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,
// jsify({
// if (options.isolationLevel != null)
// 'isolation_level': options.isolationLevel?.name,
// if (options.isReadOnly != null) 'read_only': options.isReadOnly,
// if (options.snapshot != null) 'snapshot': options.snapshot,
// }),
],
);

/// 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
40 changes: 40 additions & 0 deletions lib/src/transaction_options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'dart:js_interop';
import 'dart:js_util';

/// [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](https://deno.land/x/postgres@v0.17.0/mod.ts?s=TransactionOptions)
external String? get snapshot;

/// [postgres@v0.17.0/TransactionOptions](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;
}

extension TransactionOptionsProps on TransactionOptions {
IsolationLevel? get isolationLevel {
final jsProperty = getProperty<String?>(this, 'isolation_level');

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

bool? get isReadOnly => getProperty(this, 'read_only');
}

enum IsolationLevel {
readCommited,
repeatableRead,
serializable;

static IsolationLevel parse(String string) =>
values.firstWhere((e) => e.name == string);
}
4 changes: 3 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ version: 0.0.1
repository: https://github.com/solid-software/deno_postgres_interop

environment:
sdk: ^3.0.0
# https://github.com/invertase/dart_edge/issues/50
# Tested with 3.0.6, but others should work too
sdk: '>=3.0.0 <3.1.0'

dev_dependencies:
solid_lints: 0.0.19
2 changes: 1 addition & 1 deletion tools/add_imports/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: add_imports.
version: 1.0.0

environment:
sdk: ^3.1.0
sdk: ^3.0.0

dependencies:
args: ^2.4.2
Expand Down

0 comments on commit 23562f5

Please sign in to comment.