-
Notifications
You must be signed in to change notification settings - Fork 92
/
client.ts
200 lines (186 loc) · 6.04 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { Connection, ResultType } from "./connection/connection.ts";
import {
ConnectionOptions,
ConnectionString,
createParams,
} from "./connection/connection_params.ts";
import {
Query,
QueryArguments,
QueryArrayResult,
QueryConfig,
QueryObjectConfig,
QueryObjectResult,
QueryResult,
templateStringToQuery,
} from "./query/query.ts";
import { isTemplateString } from "./utils.ts";
export class QueryClient {
/**
* This function is meant to be replaced when being extended
*
* It's sole purpose is to be a common interface implementations can use
* regardless of their internal structure
*/
_executeQuery(_query: Query, _result: ResultType): Promise<QueryResult> {
throw new Error(
`"${this._executeQuery.name}" hasn't been implemented for class "${this.constructor.name}"`,
);
}
/**
* This method allows executed queries to be retrieved as array entries.
* It supports a generic interface in order to type the entries retrieved by the query
*
* ```ts
* const {rows} = await my_client.queryArray(
* "SELECT ID, NAME FROM CLIENTS"
* ); // Array<unknown[]>
*
* const {rows} = await my_client.queryArray<[number, string]>(
* "SELECT ID, NAME FROM CLIENTS"
* ); // Array<[number, string]>
* ```
*
* It also allows you to execute prepared stamements with template strings
*
* ```ts
* const id = 12;
* // Array<[number, string]>
* const {rows} = await my_client.queryArray<[number, string]>`SELECT ID, NAME FROM CLIENTS WHERE ID = ${id}`;
* ```
*/
queryArray<T extends Array<unknown>>(
query: string,
...args: QueryArguments
): Promise<QueryArrayResult<T>>;
queryArray<T extends Array<unknown>>(
config: QueryConfig,
): Promise<QueryArrayResult<T>>;
queryArray<T extends Array<unknown>>(
strings: TemplateStringsArray,
...args: QueryArguments
): Promise<QueryArrayResult<T>>;
queryArray<T extends Array<unknown> = Array<unknown>>(
// deno-lint-ignore camelcase
query_template_or_config: TemplateStringsArray | string | QueryConfig,
...args: QueryArguments
): Promise<QueryArrayResult<T>> {
let query;
if (typeof query_template_or_config === "string") {
query = new Query(query_template_or_config, ...args);
} else if (isTemplateString(query_template_or_config)) {
query = templateStringToQuery(query_template_or_config, args);
} else {
query = new Query(query_template_or_config);
}
return this._executeQuery(
query,
ResultType.ARRAY,
) as Promise<QueryArrayResult<T>>;
}
/**
* This method allows executed queries to be retrieved as object entries.
* It supports a generic interface in order to type the entries retrieved by the query
*
* ```ts
* const {rows} = await my_client.queryObject(
* "SELECT ID, NAME FROM CLIENTS"
* ); // Record<string, unknown>
*
* const {rows} = await my_client.queryObject<{id: number, name: string}>(
* "SELECT ID, NAME FROM CLIENTS"
* ); // Array<{id: number, name: string}>
* ```
*
* You can also map the expected results to object fields using the configuration interface.
* This will be assigned in the order they were provided
*
* ```ts
* const {rows} = await my_client.queryObject(
* "SELECT ID, NAME FROM CLIENTS"
* );
*
* console.log(rows); // [{id: 78, name: "Frank"}, {id: 15, name: "Sarah"}]
*
* const {rows} = await my_client.queryObject({
* text: "SELECT ID, NAME FROM CLIENTS",
* fields: ["personal_id", "complete_name"],
* });
*
* console.log(rows); // [{personal_id: 78, complete_name: "Frank"}, {personal_id: 15, complete_name: "Sarah"}]
* ```
*
* It also allows you to execute prepared stamements with template strings
*
* ```ts
* const id = 12;
* // Array<{id: number, name: string}>
* const {rows} = await my_client.queryObject<{id: number, name: string}>`SELECT ID, NAME FROM CLIENTS WHERE ID = ${id}`;
* ```
*/
queryObject<T extends Record<string, unknown>>(
query: string,
...args: QueryArguments
): Promise<QueryObjectResult<T>>;
queryObject<T extends Record<string, unknown>>(
config: QueryObjectConfig,
): Promise<QueryObjectResult<T>>;
queryObject<T extends Record<string, unknown>>(
query: TemplateStringsArray,
...args: QueryArguments
): Promise<QueryObjectResult<T>>;
queryObject<
T extends Record<string, unknown> = Record<string, unknown>,
>(
// deno-lint-ignore camelcase
query_template_or_config:
| string
| QueryObjectConfig
| TemplateStringsArray,
...args: QueryArguments
): Promise<QueryObjectResult<T>> {
let query;
if (typeof query_template_or_config === "string") {
query = new Query(query_template_or_config, ...args);
} else if (isTemplateString(query_template_or_config)) {
query = templateStringToQuery(query_template_or_config, args);
} else {
query = new Query(query_template_or_config as QueryObjectConfig);
}
return this._executeQuery(
query,
ResultType.OBJECT,
) as Promise<QueryObjectResult<T>>;
}
}
export class Client extends QueryClient {
protected _connection: Connection;
constructor(config?: ConnectionOptions | ConnectionString) {
super();
this._connection = new Connection(createParams(config));
}
_executeQuery(query: Query, result: ResultType): Promise<QueryResult> {
return this._connection.query(query, result);
}
async connect(): Promise<void> {
await this._connection.startup();
}
async end(): Promise<void> {
await this._connection.end();
}
}
export class PoolClient extends QueryClient {
protected _connection: Connection;
private _releaseCallback: () => void;
constructor(connection: Connection, releaseCallback: () => void) {
super();
this._connection = connection;
this._releaseCallback = releaseCallback;
}
_executeQuery(query: Query, result: ResultType): Promise<QueryResult> {
return this._connection.query(query, result);
}
async release(): Promise<void> {
await this._releaseCallback();
}
}