Skip to content

Commit 3cb0548

Browse files
ClientCommon (#10)
* partially resolve queryObject * upd * upd * big upd * fix * split by files * init * CommandType * notice * ResultType * working type * placeholders * working * more consistent comments * rowdescription * query * split by files * queryObjectOptions * uint8 * init * mini upd * working * remove generic * upd * init * upd * init * rm duplicate * restore * restore order * part * more prope * upd * fix * todos * upd * constructor * parse * todo * upd * upd * fix comments * upd * upd * upd * constructor * upd * upd * export * tls options * finish? * upd * last * upd * upd * upd * revert * revert * upd * upd
1 parent 15b56da commit 3cb0548

10 files changed

+170
-49
lines changed

lib/deno_postgres_interop.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export 'src/pool.dart';
1313
export 'src/pool_client.dart';
1414
export 'src/query.dart';
1515
export 'src/query_client.dart';
16-
export 'src/query_object.dart' show QueryArguments;
1716
export 'src/query_object_result.dart';
1817
export 'src/query_result.dart';
1918
export 'src/tls_options.dart';

lib/src/client_common.dart

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import 'package:deno_postgres_interop/src/query_array_result.dart';
2+
import 'package:deno_postgres_interop/src/query_object_options.dart';
3+
import 'package:deno_postgres_interop/src/query_object_result.dart';
4+
import 'package:deno_postgres_interop/src/util.dart';
5+
6+
/// [[email protected]/QueryArguments](https://deno.land/x/[email protected]/query/query.ts?s=QueryArguments).
7+
typedef QueryArguments = Object;
8+
9+
/// This class hosts common interops for clients.
10+
class ClientCommon {
11+
/// [[email protected]/Transaction/queryArray](https://deno.land/x/[email protected]/mod.ts?s=Transaction#method_queryArray_0).
12+
/// [[email protected]/QueryClient/queryArray](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryArray_0).
13+
static Future<QueryArrayResult<T>> queryArray<T extends List<dynamic>>(
14+
Object queryable,
15+
String query, [
16+
QueryArguments? arguments,
17+
]) =>
18+
_query('queryArray', queryable, query, arguments);
19+
20+
/// [[email protected]/Transaction/queryArray](https://deno.land/x/[email protected]/mod.ts?s=Transaction#method_queryArray_1).
21+
/// [[email protected]/QueryClient/queryArray](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryArray_1).
22+
static Future<QueryArrayResult<T>>
23+
queryArrayWithOptions<T extends List<dynamic>>(
24+
Object queryable,
25+
QueryObjectOptions config,
26+
) =>
27+
callFutureMethod(queryable, 'queryArray', [config]);
28+
29+
// This one won't be implemented because it doesn't make much sense for dart,
30+
// the query here is of type TemplateStringsArray which is used in
31+
// [tagged templates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates).
32+
//
33+
// [related issue](https://github.com/dart-lang/language/issues/1988).
34+
//
35+
// queryArray<T extends Array<unknown>>(
36+
// strings: TemplateStringsArray,
37+
// ...args: unknown[],
38+
// ): Promise<QueryArrayResult<T>>
39+
// https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryArray_2
40+
41+
/// [[email protected]/Transaction/queryObject](https://deno.land/x/[email protected]/mod.ts?s=Transaction#method_queryObject_0).
42+
/// [[email protected]/QueryClient/queryObject](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryObject_0).
43+
static Future<QueryObjectResult<T>> queryObject<T>(
44+
Object queryable,
45+
String query, [
46+
QueryArguments? arguments,
47+
]) =>
48+
_query('queryObject', queryable, query, arguments);
49+
50+
/// [[email protected]/Transaction/queryObject](https://deno.land/x/[email protected]/mod.ts?s=Transaction#method_queryObject_1).
51+
/// [[email protected]/QueryClient/queryObject](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryObject_1).
52+
static Future<QueryObjectResult<T>> queryObjectWithOptions<T>(
53+
Object queryable,
54+
QueryObjectOptions config,
55+
) =>
56+
callFutureMethod(queryable, 'queryObject', [config]);
57+
58+
// This one won't be implemented because it doesn't make much sense for dart,
59+
// the query here is of type TemplateStringsArray which is used in
60+
// [tagged templates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates).
61+
//
62+
// [related issue](https://github.com/dart-lang/language/issues/1988).
63+
//
64+
// [[email protected]/QueryClient/queryObject](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryObject_2).
65+
// Future<QueryObjectResult<T>> queryObjectWithOptions<T>(
66+
// List<String> query,
67+
// List args,
68+
// ) =>
69+
// throw UnimplementedError();
70+
71+
static Future<T> _query<T>(
72+
String function,
73+
Object queryable,
74+
String query, [
75+
QueryArguments? arguments,
76+
]) {
77+
_assertQueryArgumentsOrNull(arguments);
78+
79+
return callFutureMethod(
80+
queryable,
81+
function,
82+
[
83+
query,
84+
if (arguments != null) arguments,
85+
],
86+
);
87+
}
88+
}
89+
90+
void _assertQueryArgumentsOrNull(QueryArguments? arguments) {
91+
final isCorrectType = arguments is List ||
92+
arguments is Map<String, dynamic> ||
93+
arguments == null;
94+
95+
if (!isCorrectType) {
96+
throw ArgumentError.value(
97+
arguments,
98+
'arguments',
99+
'Accepted types are List<any> and Map<String, any>.',
100+
);
101+
}
102+
}

lib/src/query.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'dart:js_interop';
22
import 'dart:js_util';
33

4+
import 'package:deno_postgres_interop/src/client_common.dart';
45
import 'package:deno_postgres_interop/src/encoded_arg.dart';
5-
import 'package:deno_postgres_interop/src/query_object.dart';
66
import 'package:deno_postgres_interop/src/query_object_options.dart';
77
import 'package:deno_postgres_interop/src/result_type.dart';
88

lib/src/query_array_result.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'dart:js_interop';
2+
3+
import 'package:deno_postgres_interop/src/query.dart';
4+
import 'package:deno_postgres_interop/src/query_result.dart';
5+
6+
/// [[email protected]/QueryArrayResult](https://deno.land/x/[email protected]/query/query.ts?s=QueryArrayResult).
7+
@JS()
8+
class QueryArrayResult<T extends List<dynamic>> extends QueryResult {
9+
/// [[email protected]/QueryArrayResult/rows](https://deno.land/x/[email protected]/query/query.ts?s=QueryArrayResult#prop_rows).
10+
external List<T> get rows;
11+
12+
/// [[email protected]/QueryResult/constructor](https://deno.land/x/[email protected]/query/query.ts?s=QueryResult#ctor_0).
13+
external factory QueryArrayResult(Query query);
14+
}

lib/src/query_client.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import 'dart:js_interop';
22
import 'dart:js_util';
33

4-
import 'package:deno_postgres_interop/src/query_object.dart';
4+
import 'package:deno_postgres_interop/src/client_common.dart';
5+
import 'package:deno_postgres_interop/src/query_array_result.dart';
6+
import 'package:deno_postgres_interop/src/query_object_options.dart';
57
import 'package:deno_postgres_interop/src/query_object_result.dart';
68
import 'package:deno_postgres_interop/src/transaction.dart';
79
import 'package:deno_postgres_interop/src/transaction_options.dart';
@@ -46,10 +48,29 @@ extension QueryClientProps on QueryClient {
4648
return result;
4749
}
4850

51+
/// [[email protected]/QueryClient/queryArray](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryArray_0).
52+
Future<QueryArrayResult<T>> queryArray<T extends List<dynamic>>(
53+
String query, [
54+
QueryArguments? args,
55+
]) =>
56+
ClientCommon.queryArray(this, query, args);
57+
58+
/// [[email protected]/QueryClient/queryArray](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryArray_1).
59+
Future<QueryArrayResult<T>> queryArrayWithOptions<T extends List<dynamic>>(
60+
QueryObjectOptions config,
61+
) =>
62+
ClientCommon.queryArrayWithOptions(this, config);
63+
4964
/// [[email protected]/QueryClient/queryObject](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryObject_0).
5065
Future<QueryObjectResult<T>> queryObject<T>(
5166
String query, [
5267
QueryArguments? arguments,
5368
]) =>
54-
queryObjectCommon(this, query, arguments);
69+
ClientCommon.queryObject(this, query, arguments);
70+
71+
/// [[email protected]/QueryClient/queryObject](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryObject_1).
72+
Future<QueryObjectResult<T>> queryObjectWithOptions<T>(
73+
QueryObjectOptions config,
74+
) =>
75+
ClientCommon.queryObjectWithOptions(this, config);
5576
}

lib/src/query_object.dart

Lines changed: 0 additions & 36 deletions
This file was deleted.

lib/src/query_object_options.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import 'dart:js_util';
33

44
import 'package:deno_postgres_interop/src/query_options.dart';
55

6-
/// [[email protected]/QueryObjectOptions](https://deno.land/x/[email protected]/query/query.ts?s=QueryObjectOptions).
6+
/// [[email protected]/QueryObjectOptions](https://deno.land/x/[email protected]/mod.ts?s=QueryObjectOptions).
77
@JS()
88
class QueryObjectOptions extends QueryOptions {
9-
/// [[email protected]/QueryObjectOptions/fields](https://deno.land/x/[email protected]/query/query.ts?s=QueryObjectOptions#prop_fields).
9+
/// [[email protected]/QueryObjectOptions/fields](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#prop_fields).
1010
external List<String>? get fields;
1111

12-
/// [[email protected]/QueryObjectOptions](https://deno.land/x/[email protected]/query/query.ts?s=QueryObjectOptions).
12+
/// [[email protected]/QueryObjectOptions](https://deno.land/x/[email protected]/query/mod.ts?s=QueryObjectOptions).
1313
factory QueryObjectOptions({List<String>? fields, bool? isCamelCase}) =>
1414
jsify(
1515
{
@@ -19,8 +19,8 @@ class QueryObjectOptions extends QueryOptions {
1919
) as QueryObjectOptions;
2020
}
2121

22-
/// [[email protected]/QueryObjectOptions](https://deno.land/x/[email protected]/query/query.ts?s=QueryObjectOptions).
22+
/// [[email protected]/QueryObjectOptions](https://deno.land/x/[email protected]/mod.ts?s=QueryObjectOptions).
2323
extension QueryObjectOptionsProps on QueryObjectOptions {
24-
/// [[email protected]/QueryObjectOptions/camelcase](https://deno.land/x/[email protected]/query/query.ts?s=QueryObjectOptions#prop_camelcase).
24+
/// [[email protected]/QueryObjectOptions/camelcase](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#prop_camelcase).
2525
bool? get isCamelCase => getProperty(this, 'camelcase');
2626
}

lib/src/query_options.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'dart:js_interop';
22
import 'dart:js_util';
33

4-
import 'package:deno_postgres_interop/deno_postgres_interop.dart';
4+
import 'package:deno_postgres_interop/src/client_common.dart';
55
import 'package:deno_postgres_interop/src/encoded_arg.dart';
66

77
/// [[email protected]/QueryOptions](https://deno.land/x/[email protected]/query/query.ts?s=QueryOptions).

lib/src/result_type.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// [[email protected]/ResultType](https://deno.land/x/[email protected]/query/query.ts?s=ResultType).
1+
/// [deno-[email protected]/ResultType](https://deno.land/x/[email protected]/query/query.ts?s=ResultType).
22
enum ResultType {
33
/// array.
44
array,

lib/src/transaction.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'dart:js_interop';
22

3-
import 'package:deno_postgres_interop/src/query_object.dart';
3+
import 'package:deno_postgres_interop/src/client_common.dart';
4+
import 'package:deno_postgres_interop/src/query_array_result.dart';
5+
import 'package:deno_postgres_interop/src/query_object_options.dart';
46
import 'package:deno_postgres_interop/src/query_object_result.dart';
57
import 'package:deno_postgres_interop/src/util.dart';
68

@@ -16,10 +18,29 @@ extension TransactionProps on Transaction {
1618
/// [[email protected]/Transaction/commit](https://deno.land/x/[email protected]/mod.ts?s=Transaction#method_commit_0).
1719
Future<void> commit() => callFutureMethod(this, 'commit');
1820

21+
/// [[email protected]/Transaction/queryArray](https://deno.land/x/[email protected]/mod.ts?s=Transaction#method_queryArray_0).
22+
Future<QueryArrayResult<T>> queryArray<T extends List<dynamic>>(
23+
String query, [
24+
QueryArguments? args,
25+
]) =>
26+
ClientCommon.queryArray(this, query, args);
27+
28+
/// [[email protected]/Transaction/queryArray](https://deno.land/x/[email protected]/mod.ts?s=Transaction#method_queryArray_1).
29+
Future<QueryArrayResult<T>> queryArrayWithOptions<T extends List<dynamic>>(
30+
QueryObjectOptions config,
31+
) =>
32+
ClientCommon.queryArrayWithOptions(this, config);
33+
1934
/// [[email protected]/Transaction/queryObject](https://deno.land/x/[email protected]/mod.ts?s=Transaction#method_queryObject_0).
2035
Future<QueryObjectResult<T>> queryObject<T>(
2136
String query, [
2237
QueryArguments? arguments,
2338
]) =>
24-
queryObjectCommon(this, query, arguments);
39+
ClientCommon.queryObject(this, query, arguments);
40+
41+
/// [[email protected]/Transaction/queryObject](https://deno.land/x/[email protected]/mod.ts?s=Transaction#method_queryObject_1).
42+
Future<QueryObjectResult<T>> queryObjectWithOptions<T>(
43+
QueryObjectOptions config,
44+
) =>
45+
ClientCommon.queryObjectWithOptions(this, config);
2546
}

0 commit comments

Comments
 (0)