Skip to content

Commit 8ba6c29

Browse files
Merge pull request #1 from solid-software/dart-package
init dart package
2 parents 0eb47be + e4cecee commit 8ba6c29

22 files changed

+393
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.dart_tool
2+
pubspec.lock

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.0.1
2+
3+
- Initial version.

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# deno_postgres_interop
2+
[![style: solid](https://img.shields.io/badge/style-solid-orange)](https://pub.dev/packages/solid_lints)
3+
[![$solid_lints](https://nokycucwgzweensacwfy.supabase.co/functions/v1/get_project_badge?projectName=deno_postgres_interop)](https://www.worklog.ai)
4+
5+
The package allows the use
6+
of [Deno Postgres](https://deno.land/x/postgres)
7+
in [Deno Deploy](https://deno.com/deploy)
8+
on [dart_edge](https://docs.dartedge.dev/)
9+
by creating interop bindings for the js library
10+
since other drivers rely on dart:io and don't work there.
11+
12+
The main scenario is Supabase Edge Functions, but it should also work for other cases with dart2js.
13+
14+
## Usage with Supabase edge functions
15+
1. Add dependency in your pubspec.yaml:
16+
```yaml
17+
dependencies:
18+
deno_postgres_interop: <INSERT LATEST VERSION>
19+
```
20+
21+
or run
22+
23+
```bash
24+
dart pub add deno_postgres_interop
25+
```
26+
27+
2. Import the package:
28+
```dart
29+
import 'package:deno_postgres_interop/deno_postgres_interop.dart';
30+
```
31+
32+
3. Compile the code that uses this package with:
33+
```bash
34+
dart run edge build supabase_functions
35+
```
36+
37+
4. Add imports to generated file by calling the script:
38+
```bash
39+
dart run tools/add_imports/bin/add_imports.dart
40+
--filename=example/functions/dart_edge/main.dart.js
41+
```
42+
Note: your filename may differ from the example
43+
44+
5. You can use the function now.

add_imports.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
file_url_prefix: 'https://deno.land/x/[email protected]/'
2+
3+
classes_map:
4+
'query/query.ts':
5+
- QueryObjectResult
6+
'mod.ts':
7+
- QueryClient
8+
- Client
9+
- Transaction

analysis_options.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include: package:solid_lints/analysis_options.yaml
2+
3+
linter:
4+
rules:
5+
package_api_docs: true
6+
prefer_foreach: true
7+
prefer_single_quotes: true

example/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.dart_tool
2+
.packages
3+
build
4+
functions/dart_edge

example/analysis_options.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include: package:solid_lints/analysis_options.yaml
2+
3+
linter:
4+
rules:
5+
public_member_api_docs: false
6+
prefer_foreach: true
7+
prefer_single_quotes: true

example/lib/main.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'package:deno_postgres_interop/deno_postgres_interop.dart';
2+
import 'package:supabase_functions/supabase_functions.dart';
3+
4+
void main() => SupabaseFunctions(fetch: fetch);
5+
6+
Future<Response> fetch(Request _) async {
7+
final dbUrl = Deno.env.get('SUPABASE_DB_URL');
8+
if (dbUrl == null) return Response.error();
9+
10+
final client = Client(dbUrl);
11+
await client.connect();
12+
final result = await client.transaction('transaction', transaction);
13+
await client.end();
14+
15+
return Response(result.rows.map(rowToPrettyString).join('\n\n'));
16+
}
17+
18+
Future<QueryObjectResult<dynamic>> transaction(Transaction transaction) async {
19+
await transaction.queryObject(
20+
'UPDATE public."User" '
21+
"SET username='user${transaction.hashCode}' "
22+
"WHERE last_name='user'",
23+
);
24+
await Future.delayed(const Duration(seconds: 10));
25+
26+
return transaction.queryObject('SELECT * FROM public."User"');
27+
}
28+
29+
String rowToPrettyString(Map<String, dynamic> row) => row
30+
.toString()
31+
.replaceAll(', ', '\n')
32+
.replaceAllMapped(RegExp(r'\{|\}'), (_) => '');

example/pubspec.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: deno_postgres_interop_example
2+
description: An example for deno_postgres_interop package.
3+
version: 1.0.0
4+
publish_to: none
5+
6+
environment:
7+
sdk: ^3.0.0
8+
9+
dependencies:
10+
deno_postgres_interop:
11+
path: ..
12+
edge: ^0.0.5
13+
supabase_functions: ^0.0.1
14+
15+
dev_dependencies:
16+
solid_lints: 0.0.19

lib/deno_postgres_interop.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// An interop for [[email protected]](https://deno.land/x/[email protected]).
2+
library;
3+
4+
export 'src/client.dart';
5+
export 'src/query_client.dart';
6+
export 'src/query_object_result.dart';
7+
export 'src/transaction.dart';

0 commit comments

Comments
 (0)