Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoolClient #17

Merged
merged 27 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions lib/src/client_configuration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import 'dart:js_interop';
import 'dart:js_util';

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

/// [deno-postgres@v0.17.0/ClientConfiguration](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration).
@JS()
class ClientConfiguration {
/// [deno-postgres@v0.17.0/ClientConfiguration/applicationName](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration#prop_applicationName).
external String get applicationName;

/// [deno-postgres@v0.17.0/ClientConfiguration/connection](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration#prop_connection).
external ConnectionOptions get connection;

/// [deno-postgres@v0.17.0/ClientConfiguration/database](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration#prop_database).
external String get database;

/// [deno-postgres@v0.17.0/ClientConfiguration/hostname](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration#prop_hostname).
external String get hostname;

/// [deno-postgres@v0.17.0/ClientConfiguration/options](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration#prop_options).
external Map<String, String> get options;

/// [deno-postgres@v0.17.0/ClientConfiguration/password](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration#prop_password).
external String? get password;

/// [deno-postgres@v0.17.0/ClientConfiguration/port](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration#prop_port).
external int get port;

/// [deno-postgres@v0.17.0/ClientConfiguration/tls](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration#prop_tls).
external TLSOptions get tls;

/// [deno-postgres@v0.17.0/ClientConfiguration/user](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration#prop_user).
external String get user;

/// [deno-postgres@v0.17.0/ClientConfiguration](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration).
factory ClientConfiguration({
required String applicationName,
required ConnectionOptions connection,
required String database,
required String hostname,
required String options,
required int port,
required TLSOptions tls,
required String user,
required Transport hostType,
String? password,
}) =>
jsify({
'applicationName': applicationName,
'connection': connection,
'database': database,
'hostname': hostname,
'options': options,
if (password != null) 'password': password,
'port': port,
'tls': tls,
'user': user,
'hostType': hostType.name,
}) as ClientConfiguration;
}

/// [deno-postgres@v0.17.0/ClientConfiguration](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration).
extension ClientConfigurationProps on ClientConfiguration {
/// [deno-postgres@v0.17.0/ClientConfiguration/host_type](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration#prop_host_type).
Transport get hostType => Transport.parse(getProperty(this, 'host_type'));
}
139 changes: 139 additions & 0 deletions lib/src/client_options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import 'dart:js_interop';
import 'dart:js_util';

import 'package:deno_postgres_interop/src/partial/partial_connection_options.dart';
import 'package:deno_postgres_interop/src/partial/partial_tls_options.dart';
import 'package:deno_postgres_interop/src/transport.dart';

/// [deno-postgres@v0.17.0/ClientOptions](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ClientOptions).
@JS()
class ClientOptions {
/// [deno-postgres@v0.17.0/ClientOptions/applicationName](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ClientOptions#prop_applicationName).
external String? get applicationName;

/// [deno-postgres@v0.17.0/ClientOptions/database](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ClientOptions#prop_database).
external String? get database;

/// [deno-postgres@v0.17.0/ClientOptions/hostname](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ClientOptions#prop_hostname).
external String? get hostname;

/// [deno-postgres@v0.17.0/ClientOptions/password](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ClientOptions#prop_password).
external String? get password;

/// [deno-postgres@v0.17.0/ClientOptions/user](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ClientOptions#prop_applicationName/user).
external String? get user;

/// [deno-postgres@v0.17.0/ClientOptions](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ClientOptions).
factory ClientOptions({
String? applicationName,
PartialConnectionOptions? connection,
String? database,
String? hostname,
Transport? hostType,
String? optionsString,
Map<String, String>? optionsMap,
String? password,
String? portString,
int? port,
PartialTLSOptions? tls,
String? user,
}) {
assert(optionsString == null || optionsMap == null);
assert(portString == null || port == null);

return jsify(
{
if (applicationName != null) 'applicationName': applicationName,
if (connection != null) 'connection': jsify(connection.asMap()),
if (database != null) 'database': database,
if (hostname != null) 'hostname': hostname,
if (hostType != null) 'host_type': hostType.name,
if (optionsString != null)
'options': optionsString
else if (optionsMap != null)
'options': jsify(optionsMap),
if (password != null) 'password': password,
if (portString != null)
'port': portString
else if (port != null)
'port': port,
if (tls != null) 'tls': jsify(tls.asMap()),
if (user != null) 'user': user,
},
) as ClientOptions;
}
}

/// [deno-postgres@v0.17.0/ClientOptions](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ClientOptions).
extension ClientOptionsProps on ClientOptions {
/// [deno-postgres@v0.17.0/ClientOptions/host_type](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ClientOptions#prop_host_type).
Transport get hostType => Transport.parse(getProperty(this, 'host_type'));

/// [deno-postgres@v0.17.0/ClientOptions/options](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ClientOptions#prop_options).
///
/// Either this or [optionsMap] is null.
String? get optionsString {
final prop = getProperty(this, 'options');

return prop is String ? prop : null;
}

/// [deno-postgres@v0.17.0/ClientOptions/options](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ClientOptions#prop_options).
///
/// Either this or [optionsString] is null.
Map<String, String>? get optionsMap {
final prop = getProperty(this, 'options');

return prop is String ? null : prop as Map<String, String>;
}

/// [deno-postgres@v0.17.0/ClientOptions/port](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ClientOptions#prop_port).
///
/// Either this or [port] is null.
String? get portString {
final prop = getProperty(this, 'port');

return prop is String ? prop : null;
}

/// [deno-postgres@v0.17.0/ClientOptions/port](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ClientOptions#prop_port).
///
/// Either this or [portString] is null.
int? get port {
final prop = getProperty(this, 'port');

return prop is int ? prop : null;
}
illia-romanenko marked this conversation as resolved.
Show resolved Hide resolved

/// [deno-postgres@v0.17.0/ClientOptions/connection](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ClientOptions#prop_connection).
PartialConnectionOptions? get connection {
final map =
dartify(getProperty(this, 'connection')) as Map<dynamic, dynamic>?;

if (map == null) return null;

final intervalProp = map['interval'];

return PartialConnectionOptions(
attempts: map['attempts'] as int?,
interval: intervalProp is int ? intervalProp : null,
nextInterval: intervalProp is! int
? intervalProp as int Function(int previousInterval)?
: null,
);
}

/// [deno-postgres@v0.17.0/ClientOptions/tls](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ClientOptions#prop_tls).
PartialTLSOptions? get tls {
final map =
dartify(getProperty(this, 'connection')) as Map<dynamic, dynamic>?;

if (map == null) return null;

return PartialTLSOptions(
caCertificates: map['caCertificates'] as List<String>?,
isEnabled: map['enabled'] as bool?,
isEnforced: map['enforced'] as bool?,
);
}
}
3 changes: 3 additions & 0 deletions lib/src/connection_options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// TODO:
/// [deno-postgres@v0.17.0/ConnectionOptions](https://deno.land/x/postgres@v0.17.0/mod.ts?s=ConnectionOptions).
class ConnectionOptions {}
8 changes: 4 additions & 4 deletions lib/src/isolation_level.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/// [deno-postgres@v0.17.0/Transaction Isolation](https://www.postgresql.org/docs/current/transaction-iso.html).
/// [Transaction Isolation](https://www.postgresql.org/docs/current/transaction-iso.html).
enum IsolationLevel {
/// [deno-postgres@v0.17.0/Transaction Isolation/Read Committed Isolation Level](https://www.postgresql.org/docs/current/transaction-iso.html#XACT-READ-COMMITTED).
/// [Transaction Isolation/Read Committed Isolation Level](https://www.postgresql.org/docs/current/transaction-iso.html#XACT-READ-COMMITTED).
readCommitted,

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

/// [deno-postgres@v0.17.0/Transaction Isolation/Serializable Isolation Level](https://www.postgresql.org/docs/current/transaction-iso.html#XACT-SERIALIZABLE).
/// [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.
Expand Down
33 changes: 33 additions & 0 deletions lib/src/partial/partial_connection_options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// [deno-postgres@v0.17.0/ConnectionOptions](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ConnectionOptions).
class PartialConnectionOptions {
/// [deno-postgres@v0.17.0/ConnectionOptions](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ConnectionOptions#prop_attempts).
final int? attempts;

/// [deno-postgres@v0.17.0/ConnectionOptions](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ConnectionOptions#prop_interval).
///
/// Either this or [interval] is null.
final int Function(int previousInterval)? nextInterval;

/// [deno-postgres@v0.17.0/ConnectionOptions](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ConnectionOptions#prop_interval).
///
/// Either this or [nextInterval] is null.
final int? interval;

/// constructor.
PartialConnectionOptions({
required this.attempts,
required this.nextInterval,
required this.interval,
}) : assert(interval == null || nextInterval == null);

/// used for jsify.
Map<String, dynamic> asMap() {
return {
if (attempts != null) 'attempts': attempts,
if (nextInterval != null)
'interval': nextInterval
else if (interval != null)
'interval': interval,
};
}
}
27 changes: 27 additions & 0 deletions lib/src/partial/partial_tls_options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// [deno-postgres@v0.17.0/TLSOptions](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=TLSOptions).
class PartialTLSOptions {
/// [deno-postgres@v0.17.0/TLSOptions/enabled](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=TLSOptions#prop_enabled).
final bool? isEnabled;

/// [deno-postgres@v0.17.0/TLSOptions/enfroce](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=TLSOptions#prop_enfroce).
final bool? isEnforced;

/// [deno-postgres@v0.17.0/TLSOptions/caCertificates](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=TLSOptions#prop_caCertificates).
final List<String>? caCertificates;

/// constructor.
PartialTLSOptions({
required this.isEnabled,
required this.isEnforced,
required this.caCertificates,
});

/// used for jsify.
Map<String, dynamic> asMap() {
return {
if (isEnabled != null) 'enabled': isEnabled,
if (isEnforced != null) 'enforce': isEnforced,
if (caCertificates != null) 'caCertificates': caCertificates,
};
}
}
63 changes: 63 additions & 0 deletions lib/src/pool.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'dart:js_util';

import 'package:deno_postgres_interop/src/client_options.dart';
import 'package:deno_postgres_interop/src/pool_client.dart';
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).
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({
required int size,
bool? lazy,
}) =>
callConstructor('Pool', [
undefined,
size,
if (lazy != null) lazy,
]);

/// [deno-postgres@v0.17.0/Pool/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Pool#ctor_0).
factory Pool.withOptions({
required ClientOptions connectionParams,
required int size,
bool? lazy,
}) =>
callConstructor('Pool', [
connectionParams,
size,
if (lazy != null) lazy,
]);

/// [deno-postgres@v0.17.0/Pool/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Pool#ctor_0).
factory Pool.withString({
required String connectionString,
required int size,
bool? lazy,
}) =>
callConstructor('Pool', [
connectionString,
size,
if (lazy != null) lazy,
]);
}

/// [deno-postgres@v0.17.0/Pool](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Pool).
extension PoolProps on Pool {
/// [deno-postgres@v0.17.0/Pool/size](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Pool#prop_size).
int get connectionsCount => getProperty(this, 'size');

/// [deno-postgres@v0.17.0/Pool/available](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Pool#prop_available).
int get openConnectionsCount => getProperty(this, 'available');

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

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

/// [deno-postgres@v0.17.0/Pool/initialized](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Pool#method_initialized_0).
Future<int> initializedConnectionsCount() =>
callFutureMethod(this, 'initialized');
}
17 changes: 17 additions & 0 deletions lib/src/pool_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'dart:js_interop';

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

/// [deno-postgres@v0.17.0/PoolClient](https://deno.land/x/postgres@v0.17.0/mod.ts?s=PoolClient).
@JS()
class PoolClient extends QueryClient {
/// [deno-postgres@v0.17.0/PoolClient/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=PoolClient#ctor_0).
external factory PoolClient(
ClientConfiguration config,
void Function() releaseCallback,
);

/// [deno-postgres@v0.17.0/PoolClient/constructor/release](https://deno.land/x/postgres@v0.17.0/mod.ts?s=PoolClient#method_release_0).
external void release();
}
3 changes: 3 additions & 0 deletions lib/src/tls_options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// TODO:
/// [deno-postgres@v0.17.0/TLSOptions](https://deno.land/x/postgres@v0.17.0/mod.ts?s=TLSOptions).
class TLSOptions {}
12 changes: 12 additions & 0 deletions lib/src/transport.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// TCP | SOCKET.
enum Transport {
/// tcp.
tcp,

/// socket.
socket;

/// Parses a string containing an [Transport] literal into its instance.
static Transport parse(String string) =>
values.firstWhere((e) => e.name == string);
}
5 changes: 5 additions & 0 deletions lib/src/undefined.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'dart:js_interop';

/// The js' undefined.
@JS()
external dynamic get undefined;
Loading