Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/on-pr-push-code-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
uses: dart-lang/setup-dart@v1
with:
# https://github.com/invertase/dart_edge/issues/50
sdk: 3.0.0
sdk: 3.9.0

- name: Get main dependencies
run: dart pub get
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.0-dev.1
- Refactored JS Interop layer to modern Dart 3.3+ `extension type` (`dart:js_interop`).
- Updated SDK constraint to `>=3.3.0 <4.0.0` supporting Dart SDK 3.12+ (Flutter 3.44+).
- Removed legacy `@JS()` class annotations and `dart:js_util`.

## 0.0.4
- Fixed links in docs.
- Updated supported platforms.
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,3 @@ The main scenario is Supabase Edge Functions, but it should also work for other

6. You can use the function now.

Note that because of the [bug in dart_edge](https://github.com/invertase/dart_edge/issues/50), SDK versions >= 3.1.0 are not actually supported.
15 changes: 14 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
include: package:solid_lints/analysis_options.yaml

plugins:
solid_lints:
version: ^1.0.0-dev.1
diagnostics:
number_of_parameters: false
cyclomatic_complexity: false
avoid_non_null_assertion: false
avoid_duplicate_code: false

linter:
rules:
package_api_docs: true
prefer_foreach: true
prefer_single_quotes: true

formatter:
trailing_commas: preserve


14 changes: 5 additions & 9 deletions bin/add_imports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,15 @@ void main(List<String> arguments) {
}

String createNewSource(String sourceString, Config config) {
final classes = RegExp(r'new self.([A-Za-z]+)\(')
.allMatches(sourceString)
.map((e) => e.group(1))
.whereNotNull()
.toSet()
.intersection(config.classes)
final classesToImport = config.classes
.whereNot((e) => sourceString.contains('import { $e }'))
.toList();

return [
...[config.importStringForClass, (e) => 'self.$e = $e;']
.map(classes.map)
.flattened,
...[
config.importStringForClass,
(e) => 'globalThis.$e = $e;',
].map(classesToImport.map).flattened,
sourceString,
].join('\n');
}
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: 1.0.0
publish_to: none

environment:
sdk: ^3.0.0
sdk: ">=3.9.0 <4.0.0"

dependencies:
deno_postgres_interop:
Expand Down
24 changes: 17 additions & 7 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import 'dart:js_interop';
import 'dart:js_util';
import 'dart:js_interop_unsafe';

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

/// [deno-postgres@v​0.17.0/Client](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client).
@JS()
class Client extends QueryClient {
@JS('Client')
extension type Client._(JSObject _) implements QueryClient, JSObject {
/// [deno-postgres@v​0.17.0/Client/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client#ctor_0).
external factory Client(String dbUrl);
factory Client(String dbUrl) => _createClient(dbUrl.toJS);

/// [deno-postgres@v​0.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]);
factory Client.config(ClientOptions config) => _createClient(config);

/// [deno-postgres@v​0.17.0/Client/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client#ctor_0).
factory Client.empty() => callConstructor('Client', null);
factory Client.empty() => _createClient();

static Client _createClient([JSAny? configOrUrl]) {
final constructor = globalContext['Client'] as JSFunction?;
if (constructor == null) {
throw StateError('Client constructor not found in global context.');
}

return configOrUrl != null
? constructor.callAsConstructor<Client>(configOrUrl)
: constructor.callAsConstructor<Client>();
}
}
29 changes: 14 additions & 15 deletions lib/src/client_common.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:js_interop';

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';
Expand All @@ -7,24 +9,22 @@ import 'package:deno_postgres_interop/src/util.dart';
typedef QueryArguments = Object;

/// This class hosts common interops for clients.
class ClientCommon {
abstract final class ClientCommon {
/// [deno-postgres@v​0.17.0/Transaction/queryArray](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Transaction#method_queryArray_0).
/// [deno-postgres@v​0.17.0/QueryClient/queryArray](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient#method_queryArray_0).
static Future<QueryArrayResult<T>> queryArray<T extends List<dynamic>>(
Object queryable,
String query, [
QueryArguments? arguments,
]) =>
_query('queryArray', queryable, query, arguments);
]) => _query('queryArray', queryable, query, arguments);

/// [deno-postgres@v​0.17.0/Transaction/queryArray](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Transaction#method_queryArray_1).
/// [deno-postgres@v​0.17.0/QueryClient/queryArray](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient#method_queryArray_1).
static Future<QueryArrayResult<T>>
queryArrayWithOptions<T extends List<dynamic>>(
queryArrayWithOptions<T extends List<dynamic>>(
Object queryable,
QueryObjectOptions config,
) =>
callFutureMethod(queryable, 'queryArray', [config]);
) => callFutureMethod(queryable as JSObject, 'queryArray', [config]);

// This one won't be implemented because it doesn't make much sense for dart,
// the query here is of type TemplateStringsArray which is used in
Expand All @@ -39,29 +39,27 @@ class ClientCommon {
// https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient#method_queryArray_2

/// [deno-postgres@v​0.17.0/Transaction/queryObject](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Transaction#method_queryObject_0).
/// [deno-postgres@v​0.17.0/QueryClient/queryObject](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient#method_queryObject_0).
/// [deno-postgres@v​0.17.0/QueryClient/queryObject](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Transaction#method_queryObject_0).
static Future<QueryObjectResult<T>> queryObject<T>(
Object queryable,
String query, [
QueryArguments? arguments,
]) =>
_query('queryObject', queryable, query, arguments);
]) => _query('queryObject', queryable, query, arguments);

/// [deno-postgres@v​0.17.0/Transaction/queryObject](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Transaction#method_queryObject_1).
/// [deno-postgres@v​0.17.0/QueryClient/queryObject](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient#method_queryObject_1).
/// [deno-postgres@v​0.17.0/QueryClient/queryObject](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Transaction#method_queryObject_1).
static Future<QueryObjectResult<T>> queryObjectWithOptions<T>(
Object queryable,
QueryObjectOptions config,
) =>
callFutureMethod(queryable, 'queryObject', [config]);
) => callFutureMethod(queryable as JSObject, 'queryObject', [config]);

// This one won't be implemented because it doesn't make much sense for dart,
// the query here is of type TemplateStringsArray which is used in
// [tagged templates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates).
//
// [related issue](https://github.com/dart-lang/language/issues/1988).
//
// [deno-postgres@v​0.17.0/QueryClient/queryObject](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient#method_queryObject_2).
// [deno-postgres@v0.17.0/QueryClient/queryObject](https://deno.land/x/postgres@v0.17.0/mod.ts?s=QueryClient#method_queryObject_2).
// Future<QueryObjectResult<T>> queryObjectWithOptions<T>(
// List<String> query,
// List args,
Expand All @@ -77,7 +75,7 @@ class ClientCommon {
_assertQueryArgumentsOrNull(arguments);

return callFutureMethod(
queryable,
queryable as JSObject,
function,
[
query,
Expand All @@ -88,7 +86,8 @@ class ClientCommon {
}

void _assertQueryArgumentsOrNull(QueryArguments? arguments) {
final isCorrectType = arguments is List ||
final isCorrectType =
arguments is List ||
arguments is Map<String, dynamic> ||
arguments == null;

Expand Down
57 changes: 34 additions & 23 deletions lib/src/client_configuration.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
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@v​0.17.0/ClientConfiguration](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration).
@JS()
class ClientConfiguration {
@JS('ClientConfiguration')
extension type ClientConfiguration._(JSObject _) implements JSObject {
/// [deno-postgres@v​0.17.0/ClientConfiguration/applicationName](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration#prop_applicationName).
external String get applicationName;

Expand All @@ -20,8 +19,18 @@ class ClientConfiguration {
/// [deno-postgres@v​0.17.0/ClientConfiguration/hostname](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration#prop_hostname).
external String get hostname;

@JS('options')
external JSAny? get _options;

/// [deno-postgres@v​0.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;
Map<String, String> get options {
final map = _options?.dartify();
if (map is Map) {
return map.cast<String, String>();
}

return const {};
}

/// [deno-postgres@v​0.17.0/ClientConfiguration/password](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration#prop_password).
external String? get password;
Expand All @@ -35,6 +44,12 @@ class ClientConfiguration {
/// [deno-postgres@v​0.17.0/ClientConfiguration/user](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration#prop_user).
external String get user;

@JS('host_type')
external String get _hostType;

/// [deno-postgres@v​0.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(_hostType);

/// [deno-postgres@v​0.17.0/ClientConfiguration](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration).
factory ClientConfiguration({
required String applicationName,
Expand All @@ -47,23 +62,19 @@ class ClientConfiguration {
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@v​0.17.0/ClientConfiguration](https://deno.land/x/postgres@v0.17.0/connection/connection_params.ts?s=ClientConfiguration).
extension ClientConfigurationProps on ClientConfiguration {
/// [deno-postgres@v​0.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'));
}) => ClientConfiguration._(
{
'applicationName': applicationName,
'connection': connection,
'database': database,
'hostname': hostname,
'options': options,
if (password != null) 'password': password,
'port': port,
'tls': tls,
'user': user,
'hostType': hostType.name,
}.jsify()!
as JSObject,
);
}
Loading
Loading