Skip to content

Commit

Permalink
QueryClient & Transaction (#7)
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

* versions

* fix

* init?

* done?

* fix

* upd

* part

* add placeholders

* exports

* fixes

* fixes

* more fixes

* source

* todo

* clientOptions

* info

* remove things that are implemented

* simplify checklist

* align

* extract checklist

* more implemented

* upd

* upd

* Savepoint

* fix comments

* promise docs

* savepoint comments

* ClientOptions comments

* hosttype

* partialconnectionoptions upd

* partialTLSOptions

* uncomment

* progress

* partially resolve queryObject

* upd

* upd

* big upd

* fix

* split by files

* init

* CommandType

* notice

* ResultType

* working type

* placeholders

* working

* more consistent comments

* rowdescription

* query

* split by files

* queryObjectOptions

* uint8

* init

* mini upd

* working

* remove generic

* upd

* init

* upd

* init

* rm duplicate

* restore

* restore order

* part

* more prope

* upd

* fix

* todos

* upd

* constructor

* parse

* todo

* upd

* upd

* fix comments

* upd

* upd

* upd

* constructor

* upd

* upd

* export

* tls options

* finish?

* upd

* last

* upd

* upd

* upd

* revert

* revert

* upd

* upd

* fix

* restore

* add comments

* comments

* update unimplemented

* upd

* upd

* upd

* upd

* transaction

* fin

* upd

* impl

* seemps good

* upd

* exports
  • Loading branch information
danylo-safonov-solid authored Sep 21, 2023
1 parent 3cb0548 commit 9a75f04
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 22 deletions.
9 changes: 9 additions & 0 deletions lib/deno_postgres_interop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ library;
export 'src/client.dart';
export 'src/client_configuration.dart';
export 'src/client_options.dart';
export 'src/column.dart';
export 'src/command_type.dart';
export 'src/connection.dart';
export 'src/connection_options.dart';
export 'src/encoded_arg.dart';
export 'src/isolation_level.dart';
export 'src/notice.dart';
export 'src/partial/partial_connection_options.dart';
export 'src/partial/partial_tls_options.dart';
export 'src/pool.dart';
export 'src/pool_client.dart';
export 'src/query.dart';
export 'src/query_array_result.dart';
export 'src/query_client.dart';
export 'src/query_object_result.dart';
export 'src/query_result.dart';
export 'src/result_type.dart';
export 'src/row_description.dart';
export 'src/savepoint.dart';
export 'src/session.dart';
export 'src/tls_options.dart';
export 'src/transaction.dart';
export 'src/transaction_options.dart';
Expand Down
9 changes: 9 additions & 0 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import 'dart:js_interop';
import 'dart:js_util';

import 'package:deno_postgres_interop/src/client_options.dart';
import 'package:deno_postgres_interop/src/query_client.dart';

/// [deno-postgres@v0.17.0/Client](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client).
@JS()
class Client extends QueryClient {
/// [deno-postgres@v0.17.0/Client/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client#ctor_0).
external factory Client(String dbUrl);

/// [deno-postgres@v0.17.0/Client/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client#ctor_0).
factory Client.config(ClientOptions config) =>
callConstructor('Client', [config]);

/// [deno-postgres@v0.17.0/Client/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client#ctor_0).
factory Client.empty() => callConstructor('Client', null);
}
50 changes: 50 additions & 0 deletions lib/src/connection.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'dart:js_interop';
import 'dart:js_util';

import 'package:deno_postgres_interop/src/client_configuration.dart';
import 'package:deno_postgres_interop/src/promise.dart';
import 'package:deno_postgres_interop/src/query.dart';
import 'package:deno_postgres_interop/src/query_result.dart';
import 'package:deno_postgres_interop/src/transport.dart';
import 'package:deno_postgres_interop/src/util.dart';

/// [deno-postgres@v0.17.0/Connection](https://deno.land/x/postgres@v0.17.0/connection/connection.ts?s=Connection).
@JS()
class Connection {
/// [deno-postgres@v0.17.0/Connection/connected](https://deno.land/x/postgres@v0.17.0/connection/connection.ts?s=Connection#accessor_pid).
external int get pid;

/// [deno-postgres@v0.17.0/Connection/constructor](https://deno.land/x/postgres@v0.17.0/connection/connection.ts?s=Connection#ctor_0).
factory Connection({
required ClientConfiguration connectionParams,
required Future<void> Function() disconnectionCallback,
}) =>
callConstructor(
'Connection',
[connectionParams, () => futureToPromise(disconnectionCallback())],
);
}

/// [deno-postgres@v0.17.0/Connection](https://deno.land/x/postgres@v0.17.0/connection/connection.ts?s=Connection).
extension ConnectionProps on Connection {
/// [deno-postgres@v0.17.0/Connection/connected](https://deno.land/x/postgres@v0.17.0/connection/connection.ts?s=Connection#prop_connected).
bool get isConnected => getProperty(this, 'connected');

/// [deno-postgres@v0.17.0/Connection/tls](https://deno.land/x/postgres@v0.17.0/connection/connection.ts?s=Connection#accessor_tls).
bool get isCarriedOverTLS => getProperty(this, 'tls');

/// [deno-postgres@v0.17.0/Connection/transport](https://deno.land/x/postgres@v0.17.0/connection/connection.ts?s=Connection#accessor_transport).
Transport get transport => Transport.parse(getProperty(this, 'transport'));

/// [deno-postgres@v0.17.0/Connection/end](https://deno.land/x/postgres@v0.17.0/connection/connection.ts?s=Connection#method_end_0).
Future<void> end() => callFutureMethod(this, 'end');

/// [deno-postgres@v0.17.0/Connection/query](https://deno.land/x/postgres@v0.17.0/connection/connection.ts?s=Connection#method_query_0).
/// [deno-postgres@v0.17.0/Connection/query](https://deno.land/x/postgres@v0.17.0/connection/connection.ts?s=Connection#method_query_1).
Future<T> queryArray<T extends QueryResult>(Query query) =>
callFutureMethod(this, 'query', [query]);

/// [deno-postgres@v0.17.0/Connection/startup](https://deno.land/x/postgres@v0.17.0/connection/connection.ts?s=Connection#method_startup_0).
Future<void> startup({required bool isReconnection}) =>
callFutureMethod(this, 'startup', [isReconnection]);
}
38 changes: 19 additions & 19 deletions lib/src/notice.dart
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
import 'dart:js_interop';
import 'dart:js_util';

/// [postgres@v0.17.0/Notice](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice).
/// [deno-postgres@v0.17.0/Notice](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice).
@JS()
class Notice {
/// [postgres@v0.17.0/Notice/severity](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_severity).
/// [deno-postgres@v0.17.0/Notice/severity](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_severity).
external String get severity;

/// [postgres@v0.17.0/Notice/code](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_code).
/// [deno-postgres@v0.17.0/Notice/code](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_code).
external String get code;

/// [postgres@v0.17.0/Notice/message](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_message).
/// [deno-postgres@v0.17.0/Notice/message](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_message).
external String get message;

/// [postgres@v0.17.0/Notice/detail](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_detail).
/// [deno-postgres@v0.17.0/Notice/detail](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_detail).
external String? get detail;

/// [postgres@v0.17.0/Notice/hint](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_hint).
/// [deno-postgres@v0.17.0/Notice/hint](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_hint).
external String? get hint;

/// [postgres@v0.17.0/Notice/position](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_position).
/// [deno-postgres@v0.17.0/Notice/position](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_position).
external String? get position;

/// [postgres@v0.17.0/Notice/internalPosition](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_internalPosition).
/// [deno-postgres@v0.17.0/Notice/internalPosition](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_internalPosition).
external String? get internalPosition;

/// [postgres@v0.17.0/Notice/internalQuery](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_internalQuery).
/// [deno-postgres@v0.17.0/Notice/internalQuery](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_internalQuery).
external String? get internalQuery;

/// [postgres@v0.17.0/Notice/where](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_where).
/// [deno-postgres@v0.17.0/Notice/where](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_where).
external String? get where;

/// [postgres@v0.17.0/Notice/schema](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_schema).
/// [deno-postgres@v0.17.0/Notice/schema](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_schema).
external String? get schema;

/// [postgres@v0.17.0/Notice/table](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_table).
/// [deno-postgres@v0.17.0/Notice/table](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_table).
external String? get table;

/// [postgres@v0.17.0/Notice/column](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_column).
/// [deno-postgres@v0.17.0/Notice/column](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_column).
external String? get column;

/// [postgres@v0.17.0/Notice/dataType](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_dataType).
/// [deno-postgres@v0.17.0/Notice/dataType](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_dataType).
external String? get dataType;

/// [postgres@v0.17.0/Notice/constraint](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_constraint).
/// [deno-postgres@v0.17.0/Notice/constraint](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_constraint).
external String? get constraint;

/// [postgres@v0.17.0/Notice/file](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_file).
/// [deno-postgres@v0.17.0/Notice/file](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_file).
external String? get file;

/// [postgres@v0.17.0/Notice/line](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_line).
/// [deno-postgres@v0.17.0/Notice/line](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_line).
external String? get line;

/// [postgres@v0.17.0/Notice/routine](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_routine).
/// [deno-postgres@v0.17.0/Notice/routine](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice#prop_routine).
external String? get routine;

/// [postgres@v0.17.0/Notice](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice).
/// [deno-postgres@v0.17.0/Notice](https://deno.land/x/postgres@v0.17.0/connection/message.ts?s=Notice).
factory Notice({
required String severity,
required String code,
Expand Down
2 changes: 2 additions & 0 deletions lib/src/pool.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:js_interop';
import 'dart:js_util';

import 'package:deno_postgres_interop/src/client_options.dart';
Expand All @@ -6,6 +7,7 @@ import 'package:deno_postgres_interop/src/undefined.dart';
import 'package:deno_postgres_interop/src/util.dart';

/// [deno-postgres@v0.17.0/Pool](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Pool).
@JS()
class Pool {
/// [deno-postgres@v0.17.0/Pool/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Pool#ctor_0).
factory Pool({
Expand Down
21 changes: 21 additions & 0 deletions lib/src/promise.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'dart:js_interop';
import 'dart:js_util';

typedef _Resolver<T> = void Function(T result);
typedef _Executor<T> = void Function(_Resolver<T> resolve, Function reject);

/// JS [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) interop.
@JS()
class Promise<T> {
/// [js/Promise/constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise).
external Promise(_Executor<T> executor);
}

/// Convert darts [Future] to js' [Promise].
Promise<T> futureToPromise<T>(Future<T> future) {
return Promise<T>(
allowInterop((resolve, reject) {
future.then(resolve, onError: reject);
}),
);
}
19 changes: 18 additions & 1 deletion lib/src/query_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,36 @@ import 'dart:js_interop';
import 'dart:js_util';

import 'package:deno_postgres_interop/src/client_common.dart';
import 'package:deno_postgres_interop/src/connection.dart';
import 'package:deno_postgres_interop/src/query_array_result.dart';
import 'package:deno_postgres_interop/src/query_object_options.dart';
import 'package:deno_postgres_interop/src/query_object_result.dart';
import 'package:deno_postgres_interop/src/session.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';

/// [deno-postgres@v0.17.0/QueryClient](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient).
@JS()
class QueryClient {}
class QueryClient {
/// [deno-postgres@v0.17.0/QueryClient/session](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient#accessor_session).
external Session get session;

/// [deno-postgres@v0.17.0/QueryClient/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient#ctor_0).
external factory QueryClient(Connection connection);
}

/// [deno-postgres@v0.17.0/QueryClient](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient).
extension QueryClientProps on QueryClient {
/// [deno-postgres@v0.17.0/QueryClient/connected](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient#accessor_connected).
bool get isConnected => getProperty(this, 'connected');

/// [deno-postgres@v0.17.0/QueryClient/closeConnection](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient#method_closeConnection_0).
Future<void> closeConnection() => callFutureMethod(this, 'closeConnection');

/// [deno-postgres@v0.17.0/QueryClient/resetSessionMetadata](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient#method_resetSessionMetadata_0).
void resetSessionMetadata() => callMethod(this, 'resetSessionMetadata', []);

/// [deno-postgres@v0.17.0/QueryClient/connect](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient#method_connect_0).
Future<void> connect() => callFutureMethod(this, 'connect');

Expand Down
33 changes: 33 additions & 0 deletions lib/src/savepoint.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'dart:js_interop';
import 'dart:js_util';

import 'package:deno_postgres_interop/src/promise.dart';
import 'package:deno_postgres_interop/src/util.dart';

/// [deno-postgres@v0.17.0/Savepoint](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Savepoint).
@JS()
class Savepoint {
/// [deno-postgres@v0.17.0/Savepoint/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Savepoint#ctor_0).
factory Savepoint(
String name,
Future<void> Function(String name) updateCallback,
Future<void> Function(String name) releaseCallback,
) =>
callConstructor('Savepoint', [
name,
(String name) => futureToPromise(updateCallback(name)),
(String name) => futureToPromise(releaseCallback(name)),
]);
}

/// [deno-postgres@v0.17.0/Savepoint](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Savepoint).
extension SavepointProps on Savepoint {
/// [deno-postgres@v0.17.0/Savepoint/instances](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Savepoint#accessor_instances).
int get instancesCount => getProperty(this, 'instances');

/// [deno-postgres@v0.17.0/Savepoint/instances](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Savepoint#method_release_0).
Future<void> release() => callFutureMethod(this, 'release');

/// [deno-postgres@v0.17.0/Savepoint/instances](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Savepoint#method_update_0).
Future<void> update() => callFutureMethod(this, 'update');
}
27 changes: 27 additions & 0 deletions lib/src/session.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'dart:js_interop';
import 'dart:js_util';

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

/// [deno-postgres@v0.17.0/Session](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Session).
@JS()
class Session {
/// [deno-postgres@v0.17.0/Session/pid](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Session#prop_pid)
external int? get pid;

/// [deno-postgres@v0.17.0/Session/tls](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Session#prop_tls)
external bool? get tls;
}

/// [deno-postgres@v0.17.0/Session](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Session).
extension SessionProps on Session {
/// [deno-postgres@v0.17.0/Session/current_transaction](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Session#prop_current_transaction)
String? get currentTransacton => getProperty(this, 'current_transaction');

/// [deno-postgres@v0.17.0/Session/transport](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Session#prop_transport)
Transport? get transport {
final string = getProperty<String?>(this, 'transport');

return string == null ? null : Transport.parse(string);
}
}
Loading

0 comments on commit 9a75f04

Please sign in to comment.