Skip to content
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.1.0
- Breaking: Renamed `Client` to `PostgresClient` to avoid conflict with class from `dart:html`.

## 0.0.4
- Fixed links in docs.
- Updated supported platforms.
Expand Down
7 changes: 4 additions & 3 deletions add_imports.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +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
- Transaction
13 changes: 8 additions & 5 deletions bin/add_imports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ String createNewSource(String sourceString, Config config) {
.map((e) => e.group(1))
.whereNotNull()
.toSet()
.intersection(config.classes)
.whereNot((e) => sourceString.contains('import { $e }'))
.where((alias) => config.classes.any((e) => e.alias == alias))
.whereNot((e) => sourceString.contains('self.$e = '))
.map((alias) => config.classes.firstWhere((e) => e.alias == alias))
.toList();

final imports = classes.map((e) => config.importStringForClass(e.jsName));
final assignments = classes.map((e) => 'self.${e.alias} = ${e.jsName};');

return [
...[config.importStringForClass, (e) => 'self.$e = $e;']
.map(classes.map)
.flattened,
...imports,
...assignments,
sourceString,
].join('\n');
}
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Future<Response> 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(
Expand Down
2 changes: 1 addition & 1 deletion lib/deno_postgres_interop.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/// An interop for [deno-postgres@v​0.17.0](https://deno.land/x/postgres@v0.17.0).
library;

export 'src/client.dart';
export 'src/client_configuration.dart';
export 'src/client_options.dart';
export 'src/column.dart';
Expand All @@ -19,6 +18,7 @@ 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/postgres_client.dart';
export 'src/query.dart';
export 'src/query_array_result.dart';
export 'src/query_client.dart';
Expand Down
11 changes: 11 additions & 0 deletions lib/src/add_imports/better_map.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This is an internal implementation so it is okay here.
// ignore_for_file: public_member_api_docs
typedef Predicate<T> = bool Function(T);

extension BetterMap<K, V> on Map<K, V> {
Map<K, V1> mapValues<V1>(V1 Function(V) f) =>
map((k, v) => MapEntry(k, f(v)));

MapEntry<K, V> firstWhereValue(Predicate<V> predicate) =>
entries.firstWhere((e) => predicate(e.value));
}
24 changes: 24 additions & 0 deletions lib/src/add_imports/class_interop_data.dart
Original file line number Diff line number Diff line change
@@ -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<ClassInteropData> 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();
}
27 changes: 15 additions & 12 deletions lib/src/add_imports/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,41 @@
// 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';

class Config {
final String fileUrlPrefix;
final Map<String, List<String>> classesMap;
final Map<String, List<ClassInteropData>> classesMap;

Set<String> get classes => classesMap.values.flattened.toSet();
Set<ClassInteropData> get classes => classesMap.values.flattened.toSet();

Config({required this.fileUrlPrefix, required this.classesMap});

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(
(key, value) => MapEntry(
key as String,
[...value as YamlList].cast<String>(),
),
);
final classesMap = classesYamlMap
.cast<String, YamlList>()
.mapValues(ClassInteropData.fromYamlList);

return Config(
classesMap: classesMap,
fileUrlPrefix: parsedYaml['file_url_prefix'] as String,
fileUrlPrefix: fileUrlPrefix,
);
} catch (_) {
throw YamlException('', null);
}
}

String _filenameForClass(String classname) =>
classesMap.entries.firstWhere((e) => e.value.contains(classname)).key;
String _filenameForClass(String classname) => classesMap
.firstWhereValue((v) => v.map((e) => e.jsName).contains(classname))
.key;

String importStringForClass(String classname) {
final filename = _filenameForClass(classname);
Expand Down
10 changes: 5 additions & 5 deletions lib/src/client.dart → lib/src/postgres_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ 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 {
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 Client(String dbUrl);
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 Client.config(ClientOptions config) =>
callConstructor('Client', [config]);
factory PostgresClient.config(ClientOptions 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 Client.empty() => callConstructor('Client', null);
factory PostgresClient.empty() => callConstructor('PostgresClient', null);
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.1.0
repository: https://github.com/solid-software/deno_postgres_interop

environment:
Expand Down