Skip to content

Commit 63f89cb

Browse files
authored
Merge pull request #88 from malthe/remove-execute-plus-simplify
Remove 'execute' method; simplify 'query' and 'prepare' methods
2 parents d1a66e7 + 90644b9 commit 63f89cb

File tree

3 files changed

+36
-31
lines changed

3 files changed

+36
-31
lines changed

CHANGES.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ In the next release ...
22

33
- The `execute` method has been removed.
44

5-
- The query options now supports an optional `transform` parameter which takes
6-
a column name input, allowing the transformation of column names into for
7-
example camelcase.
5+
- The querying options now include an optional `transform` property which must be
6+
a function which takes a column name input, allowing the transformation of column
7+
names into for example _camel-case_.
88

9-
- The `query` method now accepts a `Query` object as the
10-
first value, in addition to the query string, making it easier to
11-
make a query with additional configuration.
9+
- The `query` method now accepts a `Query` object as the first argument in
10+
addition to a string argument, but no longer accepts any additional arguments
11+
after `values`. The query options must now be provided using the query argument
12+
instead.
13+
14+
The same change has been made to `prepare`.
1215

1316
1.6.0 (2023-12-13)
1417
------------------

src/client.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,13 @@ export class Client {
403403
});
404404
}
405405

406+
/** Connect to the database.
407+
*
408+
* @remarks
409+
* Don't forget to close the connection using {@link end} before exiting.
410+
*
411+
* @returns The connection encryption status.
412+
*/
406413
connect(): Promise<boolean> {
407414
if (this.connecting) {
408415
throw new Error('Already connecting');
@@ -446,6 +453,9 @@ export class Client {
446453
return p;
447454
}
448455

456+
/** End the database connection.
457+
*
458+
*/
449459
end() {
450460
if (this.ending) {
451461
throw new Error('Already ending');
@@ -514,13 +524,13 @@ export class Client {
514524
}
515525
}
516526

517-
prepare<T = ResultRecord>(
518-
text: Query | string,
519-
name?: string,
520-
types?: DataType[]): Promise<PreparedStatement<T>> {
521-
527+
/** Prepare a statement for later execution.
528+
*
529+
* @returns A prepared statement object.
530+
*/
531+
prepare<T = ResultRecord>(text: Query | string): Promise<PreparedStatement<T>> {
522532
const query = typeof text === 'string' ? {text} : text;
523-
const providedNameOrGenerated = name || (
533+
const providedNameOrGenerated = query.name || (
524534
(this.config.preparedStatementPrefix ||
525535
defaults.preparedStatementPrefix) + (
526536
this.nextPreparedStatementId++
@@ -530,7 +540,7 @@ export class Client {
530540
(resolve, reject) => {
531541
const errorHandler: ErrorHandler = (error) => reject(error);
532542
this.errorHandlerQueue.push(errorHandler);
533-
this.writer.parse(providedNameOrGenerated, query.text, types || query.types || []);
543+
this.writer.parse(providedNameOrGenerated, query.text, query.types || []);
534544
this.writer.describe(providedNameOrGenerated, 'S');
535545
this.preFlightQueue.push({
536546
descriptionHandler: (description: RowDescription) => {
@@ -598,28 +608,18 @@ export class Client {
598608
* @param text - The query string, or pass a {@link Query}
599609
* object which provides more control (including streaming values into a socket).
600610
* @param values - The query parameters, corresponding to $1, $2, etc.
601-
* @param types - Allows making the database native type explicit for some or all
602-
* columns.
603-
* @param format - Whether column data should be transferred using text or binary mode.
604-
* @param streams - A mapping from column name to a socket, e.g. an open file.
605611
* @returns A promise for the query results.
606612
*/
607-
query<T = ResultRecord>(
608-
text: Query | string,
609-
values?: any[],
610-
types?: DataType[],
611-
format?: DataFormat | DataFormat[],
612-
streams?: Record<string, Writable>):
613-
ResultIterator<T> {
613+
query<T = ResultRecord>(text: Query | string, values?: any[]): ResultIterator<T> {
614614
const query = typeof text === 'string' ? {text} : text;
615615

616616
if (this.closed && !this.connecting) {
617617
throw new Error('Connection is closed.');
618618
}
619619

620-
format = format || query?.format;
621-
types = types || query?.types;
622-
streams = streams || query?.streams;
620+
const format = query?.format;
621+
const types = query?.types;
622+
const streams =query?.streams;
623623
const portal = query?.portal || '';
624624
const result = makeResult<T>(query?.transform);
625625

test/types.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ function testType<T>(
3535
const query = expected !== null
3636
? getComparisonQueryFor(dataType, expression)
3737
: 'select $1 is null';
38-
await client.query(
39-
(expected !== null) ? query + ' where $1 is not null' : query,
40-
[expected], [dataType], format)
38+
await client.query({
39+
text: expected !== null ? query + ' where $1 is not null' : query,
40+
types: [dataType],
41+
format
42+
}, [expected])
4143
.then(
4244
(result) => {
4345
const rows = result.rows;
@@ -52,7 +54,7 @@ function testType<T>(
5254
testWithClient('Value', async (client) => {
5355
expect.assertions(3);
5456
const query = 'select ' + expression;
55-
await client.query(query, [], [], format).then(
57+
await client.query({text: query, format}, []).then(
5658
(result) => {
5759
const rows = result.rows;
5860
expect(rows.length).toEqual(1);

0 commit comments

Comments
 (0)