From 126b281bb6893f82bcb6b9efbf42137735ff88ce Mon Sep 17 00:00:00 2001 From: danylo-safonov-solid Date: Wed, 15 Nov 2023 17:59:03 +0200 Subject: [PATCH 01/11] rename --- example/lib/main.dart | 2 +- lib/src/{client.dart => postgres_client.dart} | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) rename lib/src/{client.dart => postgres_client.dart} (73%) diff --git a/example/lib/main.dart b/example/lib/main.dart index 14e4c30..9cfb810 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -7,7 +7,7 @@ Future fetch(Request _) async { final dbUrl = Deno.env.get('SUPABASE_DB_URL'); if (dbUrl == null) return Response.error(); - final client = Client(dbUrl); + final client = PostgresClient(dbUrl); await client.connect(); try { final result = await client.transaction( diff --git a/lib/src/client.dart b/lib/src/postgres_client.dart similarity index 73% rename from lib/src/client.dart rename to lib/src/postgres_client.dart index dd66f08..7f0e0a4 100644 --- a/lib/src/client.dart +++ b/lib/src/postgres_client.dart @@ -5,15 +5,15 @@ 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 { +@JS('Client') +class PostgresClient 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); + external factory PostgresClient(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) => + factory PostgresClient.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); + factory PostgresClient.empty() => callConstructor('Client', null); } From 71518bc0b1f7b1c326907cb40e2ccc0ee622ed6b Mon Sep 17 00:00:00 2001 From: danylo-safonov-solid Date: Wed, 15 Nov 2023 18:02:13 +0200 Subject: [PATCH 02/11] revert --- lib/deno_postgres_interop.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/deno_postgres_interop.dart b/lib/deno_postgres_interop.dart index 115dd7b..cea5a36 100644 --- a/lib/deno_postgres_interop.dart +++ b/lib/deno_postgres_interop.dart @@ -1,4 +1,4 @@ -/// An interop for [deno-postgres@v0.17.0](https://deno.land/x/postgres@v0.17.0). +/// An interop for [deno-postgres@v​0.17.0](https://deno.land/x/postgres@v0.17.0). library; export 'src/client_configuration.dart'; From 0ad9f1fe63d06babb246d89594aaaea9a2617b42 Mon Sep 17 00:00:00 2001 From: danylo-safonov-solid Date: Wed, 15 Nov 2023 18:03:29 +0200 Subject: [PATCH 03/11] revert --- lib/src/postgres_client.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/src/postgres_client.dart b/lib/src/postgres_client.dart index 7f0e0a4..27883ad 100644 --- a/lib/src/postgres_client.dart +++ b/lib/src/postgres_client.dart @@ -4,16 +4,16 @@ 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). +/// [deno-postgres@v​0.17.0/Client](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client). @JS('Client') class PostgresClient extends QueryClient { - /// [deno-postgres@v0.17.0/Client/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client#ctor_0). + /// [deno-postgres@v​0.17.0/Client/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client#ctor_0). external factory PostgresClient(String dbUrl); - /// [deno-postgres@v0.17.0/Client/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client#ctor_0). + /// [deno-postgres@v​0.17.0/Client/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client#ctor_0). factory PostgresClient.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). + /// [deno-postgres@v​0.17.0/Client/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client#ctor_0). factory PostgresClient.empty() => callConstructor('Client', null); } From 9ad47b6518c442034486c144e5be8ae13e3859c2 Mon Sep 17 00:00:00 2001 From: danylo-safonov-solid Date: Wed, 15 Nov 2023 18:05:43 +0200 Subject: [PATCH 04/11] version --- CHANGELOG.md | 3 +++ pubspec.yaml | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e006bb5..71a96a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.0.5 +- Renamed `Client` to `PostgresClient` to avoid conflict with class from `dart:html`. + ## 0.0.4 - Fixed links in docs. - Updated supported platforms. diff --git a/pubspec.yaml b/pubspec.yaml index 18d3298..a21d17d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: deno_postgres_interop description: An interop for js package deno-postgres - PostgreSQL driver that can be used in deno-deploy (supabase edge functions). -version: 0.0.4 +version: 0.0.5 repository: https://github.com/solid-software/deno_postgres_interop environment: From 0fcacde6d4908c3271ba2c83543c5648179b3b91 Mon Sep 17 00:00:00 2001 From: danylo-safonov-solid Date: Wed, 15 Nov 2023 21:42:28 +0200 Subject: [PATCH 05/11] major version for breaking change --- CHANGELOG.md | 4 ++-- pubspec.yaml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71a96a0..f1c4139 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ -## 0.0.5 -- Renamed `Client` to `PostgresClient` to avoid conflict with class from `dart:html`. +## 0.1.0 +- Breaking: Renamed `Client` to `PostgresClient` to avoid conflict with class from `dart:html`. ## 0.0.4 - Fixed links in docs. diff --git a/pubspec.yaml b/pubspec.yaml index a21d17d..90b8e7b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: deno_postgres_interop description: An interop for js package deno-postgres - PostgreSQL driver that can be used in deno-deploy (supabase edge functions). -version: 0.0.5 +version: 0.1.0 repository: https://github.com/solid-software/deno_postgres_interop environment: From 020f00316270201ecfbd44e4419f81815c8fd6a4 Mon Sep 17 00:00:00 2001 From: danylo-safonov-solid Date: Wed, 15 Nov 2023 22:31:06 +0200 Subject: [PATCH 06/11] another attempt --- add_imports.yaml | 1 + bin/add_imports.dart | 13 ++++++++++--- lib/src/add_imports/config.dart | 21 ++++++++++++++++----- lib/src/postgres_client.dart | 6 +++--- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/add_imports.yaml b/add_imports.yaml index 6f4f168..dd898f4 100644 --- a/add_imports.yaml +++ b/add_imports.yaml @@ -6,4 +6,5 @@ classes_map: 'mod.ts': - QueryClient - Client + - 'Client': PostgresClient - Transaction diff --git a/bin/add_imports.dart b/bin/add_imports.dart index 1f8f8fe..946689a 100644 --- a/bin/add_imports.dart +++ b/bin/add_imports.dart @@ -37,13 +37,20 @@ String createNewSource(String sourceString, Config config) { .map((e) => e.group(1)) .whereNotNull() .toSet() - .intersection(config.classes) + .where((alias) => config.classes.any((e) => e.$2 == alias)) .whereNot((e) => sourceString.contains('import { $e }')) .toList(); return [ - ...[config.importStringForClass, (e) => 'self.$e = $e;'] - .map(classes.map) + ...[ + ((String, String) e) => config.importStringForClass(e.$1), + ((String, String) e) => 'self.${e.$2} = ${e.$1};', + ] + .map( + classes + .map((alias) => config.classes.firstWhere((e) => e.$2 == alias)) + .map, + ) .flattened, sourceString, ].join('\n'); diff --git a/lib/src/add_imports/config.dart b/lib/src/add_imports/config.dart index 1146c91..0c917f7 100644 --- a/lib/src/add_imports/config.dart +++ b/lib/src/add_imports/config.dart @@ -6,9 +6,9 @@ import 'package:yaml/yaml.dart'; class Config { final String fileUrlPrefix; - final Map> classesMap; + final Map> classesMap; - Set get classes => classesMap.values.flattened.toSet(); + Set<(String, String)> get classes => classesMap.values.flattened.toSet(); Config({required this.fileUrlPrefix, required this.classesMap}); @@ -19,7 +19,13 @@ class Config { final classesMap = (parsedYaml['classes_map'] as YamlMap).map( (key, value) => MapEntry( key as String, - [...value as YamlList].cast(), + [...value as YamlList] + .map( + (e) => e is YamlMap + ? (e.keys.first as String, e.values.first as String) + : (e as String, e), + ) + .toList(), ), ); @@ -32,8 +38,13 @@ class Config { } } - String _filenameForClass(String classname) => - classesMap.entries.firstWhere((e) => e.value.contains(classname)).key; + String _filenameForClass(String classname) => classesMap.entries + .firstWhere( + (e) => e.value.any( + (classnamePair) => classnamePair.$1 == classname, + ), + ) + .key; String importStringForClass(String classname) { final filename = _filenameForClass(classname); diff --git a/lib/src/postgres_client.dart b/lib/src/postgres_client.dart index 27883ad..4d63d38 100644 --- a/lib/src/postgres_client.dart +++ b/lib/src/postgres_client.dart @@ -5,15 +5,15 @@ 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('Client') +@JS() class PostgresClient extends QueryClient { /// [deno-postgres@v​0.17.0/Client/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client#ctor_0). external factory PostgresClient(String dbUrl); /// [deno-postgres@v​0.17.0/Client/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client#ctor_0). factory PostgresClient.config(ClientOptions config) => - callConstructor('Client', [config]); + callConstructor('PostgresClient', [config]); /// [deno-postgres@v​0.17.0/Client/constructor](https://deno.land/x/postgres@v0.17.0/mod.ts?s=Client#ctor_0). - factory PostgresClient.empty() => callConstructor('Client', null); + factory PostgresClient.empty() => callConstructor('PostgresClient', null); } From 63550fe029cabf61169677ceaa39e5bb5ddc6c9b Mon Sep 17 00:00:00 2001 From: danylo-safonov-solid Date: Thu, 16 Nov 2023 13:02:36 +0200 Subject: [PATCH 07/11] prefix --- add_imports.yaml | 8 ++++---- bin/add_imports.dart | 18 +++++++----------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/add_imports.yaml b/add_imports.yaml index dd898f4..0f160b0 100644 --- a/add_imports.yaml +++ b/add_imports.yaml @@ -1,10 +1,10 @@ -file_url_prefix: 'https://deno.land/x/postgres@v0.17.0/' +file_url_prefix: https://deno.land/x/postgres@v0.17.0/ classes_map: - 'query/query.ts': + query/query.ts: - QueryObjectResult - 'mod.ts': + mod.ts: - QueryClient - Client - - 'Client': PostgresClient + - Client: PostgresClient - Transaction diff --git a/bin/add_imports.dart b/bin/add_imports.dart index 946689a..4885b00 100644 --- a/bin/add_imports.dart +++ b/bin/add_imports.dart @@ -38,20 +38,16 @@ String createNewSource(String sourceString, Config config) { .whereNotNull() .toSet() .where((alias) => config.classes.any((e) => e.$2 == alias)) - .whereNot((e) => sourceString.contains('import { $e }')) + .whereNot((e) => sourceString.contains('self.$e = ')) + .map((alias) => config.classes.firstWhere((e) => e.$2 == alias)) .toList(); + final imports = classes.map((e) => config.importStringForClass(e.$1)); + final assignments = classes.map((e) => 'self.${e.$2} = ${e.$1};'); + return [ - ...[ - ((String, String) e) => config.importStringForClass(e.$1), - ((String, String) e) => 'self.${e.$2} = ${e.$1};', - ] - .map( - classes - .map((alias) => config.classes.firstWhere((e) => e.$2 == alias)) - .map, - ) - .flattened, + ...imports, + ...assignments, sourceString, ].join('\n'); } From 0226a1b690c01c2a92707b1ccc9661a30a9af9b3 Mon Sep 17 00:00:00 2001 From: danylo-safonov-solid Date: Thu, 16 Nov 2023 13:28:25 +0200 Subject: [PATCH 08/11] ClassInteropData --- bin/add_imports.dart | 8 +++---- lib/src/add_imports/class_interop_data.dart | 24 +++++++++++++++++++++ lib/src/add_imports/config.dart | 15 +++++-------- 3 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 lib/src/add_imports/class_interop_data.dart diff --git a/bin/add_imports.dart b/bin/add_imports.dart index 4885b00..1c0c06d 100644 --- a/bin/add_imports.dart +++ b/bin/add_imports.dart @@ -37,13 +37,13 @@ String createNewSource(String sourceString, Config config) { .map((e) => e.group(1)) .whereNotNull() .toSet() - .where((alias) => config.classes.any((e) => e.$2 == alias)) + .where((alias) => config.classes.any((e) => e.alias == alias)) .whereNot((e) => sourceString.contains('self.$e = ')) - .map((alias) => config.classes.firstWhere((e) => e.$2 == alias)) + .map((alias) => config.classes.firstWhere((e) => e.alias == alias)) .toList(); - final imports = classes.map((e) => config.importStringForClass(e.$1)); - final assignments = classes.map((e) => 'self.${e.$2} = ${e.$1};'); + final imports = classes.map((e) => config.importStringForClass(e.jsName)); + final assignments = classes.map((e) => 'self.${e.alias} = ${e.jsName};'); return [ ...imports, diff --git a/lib/src/add_imports/class_interop_data.dart b/lib/src/add_imports/class_interop_data.dart new file mode 100644 index 0000000..6816460 --- /dev/null +++ b/lib/src/add_imports/class_interop_data.dart @@ -0,0 +1,24 @@ +// This is an internal implementation so it is okay here. +// ignore_for_file: public_member_api_docs + +import 'package:yaml/yaml.dart'; + +class ClassInteropData { + final String jsName; + final String alias; + + ClassInteropData({required this.jsName, required this.alias}); + + ClassInteropData.noAlias(this.jsName) : alias = jsName; + + static List fromYamlList(YamlList list) => list + .map( + (e) => e is YamlMap + ? ClassInteropData( + jsName: e.keys.first as String, + alias: e.values.first as String, + ) + : ClassInteropData.noAlias(e as String), + ) + .toList(); +} diff --git a/lib/src/add_imports/config.dart b/lib/src/add_imports/config.dart index 0c917f7..986163e 100644 --- a/lib/src/add_imports/config.dart +++ b/lib/src/add_imports/config.dart @@ -2,13 +2,14 @@ // ignore_for_file: public_member_api_docs import 'package:collection/collection.dart'; +import 'package:deno_postgres_interop/src/add_imports/class_interop_data.dart'; import 'package:yaml/yaml.dart'; class Config { final String fileUrlPrefix; - final Map> classesMap; + final Map> classesMap; - Set<(String, String)> get classes => classesMap.values.flattened.toSet(); + Set get classes => classesMap.values.flattened.toSet(); Config({required this.fileUrlPrefix, required this.classesMap}); @@ -19,13 +20,7 @@ class Config { final classesMap = (parsedYaml['classes_map'] as YamlMap).map( (key, value) => MapEntry( key as String, - [...value as YamlList] - .map( - (e) => e is YamlMap - ? (e.keys.first as String, e.values.first as String) - : (e as String, e), - ) - .toList(), + ClassInteropData.fromYamlList(value as YamlList), ), ); @@ -41,7 +36,7 @@ class Config { String _filenameForClass(String classname) => classesMap.entries .firstWhere( (e) => e.value.any( - (classnamePair) => classnamePair.$1 == classname, + (classnamePair) => classnamePair.jsName == classname, ), ) .key; From ffe7f59e491e06ef70576f959daf3f22b10a7985 Mon Sep 17 00:00:00 2001 From: danylo-safonov-solid Date: Thu, 16 Nov 2023 13:33:07 +0200 Subject: [PATCH 09/11] more refactor --- lib/src/add_imports/config.dart | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/src/add_imports/config.dart b/lib/src/add_imports/config.dart index 986163e..d4ee743 100644 --- a/lib/src/add_imports/config.dart +++ b/lib/src/add_imports/config.dart @@ -15,9 +15,12 @@ class Config { factory Config.fromYaml(String yamlString) { try { - final parsedYaml = loadYaml(yamlString) as YamlMap; + final { + 'classes_map': YamlMap classesYamlMap, + 'file_url_prefix': String fileUrlPrefix, + } = loadYaml(yamlString) as YamlMap; - final classesMap = (parsedYaml['classes_map'] as YamlMap).map( + final classesMap = classesYamlMap.map( (key, value) => MapEntry( key as String, ClassInteropData.fromYamlList(value as YamlList), @@ -26,7 +29,7 @@ class Config { return Config( classesMap: classesMap, - fileUrlPrefix: parsedYaml['file_url_prefix'] as String, + fileUrlPrefix: fileUrlPrefix, ); } catch (_) { throw YamlException('', null); @@ -35,9 +38,7 @@ class Config { String _filenameForClass(String classname) => classesMap.entries .firstWhere( - (e) => e.value.any( - (classnamePair) => classnamePair.jsName == classname, - ), + (e) => e.value.any((interop) => interop.jsName == classname), ) .key; From 9e2678b777892c453c38816d8776d5d6fd33e82b Mon Sep 17 00:00:00 2001 From: danylo-safonov-solid Date: Thu, 16 Nov 2023 13:40:51 +0200 Subject: [PATCH 10/11] refactor --- lib/src/add_imports/config.dart | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/src/add_imports/config.dart b/lib/src/add_imports/config.dart index d4ee743..a88ca2d 100644 --- a/lib/src/add_imports/config.dart +++ b/lib/src/add_imports/config.dart @@ -20,12 +20,9 @@ class Config { 'file_url_prefix': String fileUrlPrefix, } = loadYaml(yamlString) as YamlMap; - final classesMap = classesYamlMap.map( - (key, value) => MapEntry( - key as String, - ClassInteropData.fromYamlList(value as YamlList), - ), - ); + final classesMap = classesYamlMap + .cast() + .mapValues(ClassInteropData.fromYamlList); return Config( classesMap: classesMap, @@ -49,3 +46,8 @@ class Config { return 'import { $classname } from "$url";'; } } + +extension MapMapValues on Map { + Map mapValues(V1 Function(V) f) => + map((k, v) => MapEntry(k, f(v))); +} From 7dbe272786f2b5f358802db40d20dd32082ddad2 Mon Sep 17 00:00:00 2001 From: danylo-safonov-solid Date: Thu, 16 Nov 2023 13:46:58 +0200 Subject: [PATCH 11/11] extract --- lib/src/add_imports/better_map.dart | 11 +++++++++++ lib/src/add_imports/config.dart | 12 +++--------- 2 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 lib/src/add_imports/better_map.dart diff --git a/lib/src/add_imports/better_map.dart b/lib/src/add_imports/better_map.dart new file mode 100644 index 0000000..dfaddae --- /dev/null +++ b/lib/src/add_imports/better_map.dart @@ -0,0 +1,11 @@ +// This is an internal implementation so it is okay here. +// ignore_for_file: public_member_api_docs +typedef Predicate = bool Function(T); + +extension BetterMap on Map { + Map mapValues(V1 Function(V) f) => + map((k, v) => MapEntry(k, f(v))); + + MapEntry firstWhereValue(Predicate predicate) => + entries.firstWhere((e) => predicate(e.value)); +} diff --git a/lib/src/add_imports/config.dart b/lib/src/add_imports/config.dart index a88ca2d..f820565 100644 --- a/lib/src/add_imports/config.dart +++ b/lib/src/add_imports/config.dart @@ -2,6 +2,7 @@ // ignore_for_file: public_member_api_docs import 'package:collection/collection.dart'; +import 'package:deno_postgres_interop/src/add_imports/better_map.dart'; import 'package:deno_postgres_interop/src/add_imports/class_interop_data.dart'; import 'package:yaml/yaml.dart'; @@ -33,10 +34,8 @@ class Config { } } - String _filenameForClass(String classname) => classesMap.entries - .firstWhere( - (e) => e.value.any((interop) => interop.jsName == classname), - ) + String _filenameForClass(String classname) => classesMap + .firstWhereValue((v) => v.map((e) => e.jsName).contains(classname)) .key; String importStringForClass(String classname) { @@ -46,8 +45,3 @@ class Config { return 'import { $classname } from "$url";'; } } - -extension MapMapValues on Map { - Map mapValues(V1 Function(V) f) => - map((k, v) => MapEntry(k, f(v))); -}