Skip to content

Commit 2a8cfcf

Browse files
authored
Tweaks and CHANGELOG (#163)
Ready for 1.0.1, this PR: * Fixes a couple of tiny bugs * Updates all dependencies * Updates the types * Updates the CHANGELOG I also reverted the `node/types` version to the current latest, and increased the timeouts on tests (because they were timing out for me).
1 parent 095e72c commit 2a8cfcf

File tree

13 files changed

+2255
-2199
lines changed

13 files changed

+2255
-2199
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.0.1 (2025-06-06)
2+
3+
The package now prints a security warning to the console when a connection is made in a web browser. This behaviour can be suppressed with a new configuration option: `disableWarningInBrowsers`. There are a few other very minor fixes.
4+
15
## 1.0.0 (2025-03-25)
26

37
Breaking change: the HTTP query template function can now **only** be called as a template function, not as a conventional function. This improves safety from accidental SQL-injection vulnerabilities. For example:

CONFIG.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const rows = await sql.query('SELECT * FROM posts WHERE id = $1', [postId], {
119119
clearTimeout(timeout);
120120
```
121121

122-
### `types: typeof PgTypes`
122+
### `types: CustomTypesConfig`
123123

124124
The `types` option can be passed to `neon(...)` to override the default PostgreSQL type parsers provided by `PgTypes`. This is useful if you want to define custom parsing behavior for specific PostgreSQL data types, allowing you to control how data is converted when retrieved from the database. Learn more in the [PgTypes official documentation](https://github.com/brianc/node-pg-types).
125125

@@ -129,16 +129,23 @@ Example of usage:
129129
import PgTypes from 'pg-types';
130130
import { neon } from '@neondatabase/serverless';
131131

132-
// Define custom parsers for specific PostgreSQL types
133-
// Parse PostgreSQL `DATE` fields as JavaScript `Date` objects
134-
PgTypes.setTypeParser(PgTypes.builtins.DATE, (val) => new Date(val));
135-
136-
// Parse PostgreSQL `NUMERIC` fields as JavaScript `float` values
137-
PgTypes.setTypeParser(PgTypes.builtins.NUMERIC, parseFloat);
138-
139132
// Configure the Neon client with the custom `types` parser
140133
const sql = neon(process.env.DATABASE_URL, {
141-
types: PgTypes, // Pass in the custom PgTypes object here
134+
types: {
135+
getTypeParser: ((oid, format?: any) => {
136+
// Define custom parsers for specific PostgreSQL types
137+
// Parse PostgreSQL `DATE` fields as JavaScript `Date` objects
138+
if (oid === PgTypes.builtins.DATE) {
139+
return (val: any) => new Date(val);
140+
}
141+
// Parse PostgreSQL `NUMERIC` fields as JavaScript `float` values
142+
if (oid === PgTypes.builtins.NUMERIC) {
143+
return parseFloat;
144+
}
145+
// For all other types, use the default parser
146+
return PgTypes.getTypeParser(oid, format);
147+
}) as typeof PgTypes.getTypeParser,
148+
},
142149
});
143150
```
144151

index.d.mts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,14 @@ export declare interface HTTPQueryOptions<ArrayMode extends boolean, FullResults
315315
/**
316316
* Custom type parsers. See https://github.com/brianc/node-pg-types.
317317
*/
318-
types?: typeof types;
318+
types?: CustomTypesConfig;
319+
/**
320+
* When `disableWarningInBrowsers` is set to `true`, it disables the warning about
321+
* running this driver in the browser.
322+
*
323+
* Default: `false`
324+
*/
325+
disableWarningInBrowsers?: boolean;
319326
}
320327

321328
export declare interface HTTPTransactionOptions<ArrayMode extends boolean, FullResults extends boolean> extends HTTPQueryOptions<ArrayMode, FullResults> {
@@ -416,7 +423,7 @@ export { MessageConfig }
416423
* pass as `fetchOptions` an object which will be merged into the options
417424
* passed to `fetch`.
418425
*/
419-
export declare function neon<ArrayMode extends boolean = false, FullResults extends boolean = false>(connectionString: string, { arrayMode: neonOptArrayMode, fullResults: neonOptFullResults, fetchOptions: neonOptFetchOptions, isolationLevel: neonOptIsolationLevel, readOnly: neonOptReadOnly, deferrable: neonOptDeferrable, authToken, }?: HTTPTransactionOptions<ArrayMode, FullResults>): NeonQueryFunction<ArrayMode, FullResults>;
426+
export declare function neon<ArrayMode extends boolean = false, FullResults extends boolean = false>(connectionString: string, { arrayMode: neonOptArrayMode, fullResults: neonOptFullResults, fetchOptions: neonOptFetchOptions, isolationLevel: neonOptIsolationLevel, readOnly: neonOptReadOnly, deferrable: neonOptDeferrable, authToken, disableWarningInBrowsers, }?: HTTPTransactionOptions<ArrayMode, FullResults>): NeonQueryFunction<ArrayMode, FullResults>;
420427

421428
export declare interface NeonConfig {
422429
poolQueryViaFetch: boolean;
@@ -433,6 +440,7 @@ export declare interface NeonConfig {
433440
rootCerts: string;
434441
pipelineTLS: boolean;
435442
disableSNI: boolean;
443+
disableWarningInBrowsers: boolean;
436444
}
437445

438446
export declare class neonConfig extends EventEmitter {
@@ -552,6 +560,16 @@ export declare class neonConfig extends EventEmitter {
552560
static set disableSNI(newValue: NeonConfig['disableSNI']);
553561
get disableSNI(): NeonConfig["disableSNI"];
554562
set disableSNI(newValue: NeonConfig['disableSNI']);
563+
/**
564+
* When `disableWarningInBrowsers` is set to `true`, it disables the warning about
565+
* running this driver in the browser.
566+
*
567+
* Default: `false`.
568+
*/
569+
static get disableWarningInBrowsers(): NeonConfig["disableWarningInBrowsers"];
570+
static set disableWarningInBrowsers(newValue: NeonConfig['disableWarningInBrowsers']);
571+
get disableWarningInBrowsers(): NeonConfig["disableWarningInBrowsers"];
572+
set disableWarningInBrowsers(newValue: NeonConfig['disableWarningInBrowsers']);
555573
/**
556574
* Pipelines the startup message, cleartext password message and first query
557575
* when set to `"password"`. This works only for cleartext password auth.
@@ -782,7 +800,7 @@ export { PoolConfig }
782800
export declare interface ProcessQueryResultOptions {
783801
arrayMode: boolean;
784802
fullResults: boolean;
785-
types?: typeof types;
803+
types?: CustomTypesConfig;
786804
}
787805

788806
export { Query }
@@ -879,6 +897,12 @@ export declare class UnsafeRawSql {
879897
constructor(sql: string);
880898
}
881899

900+
/**
901+
* Detects if the code is running in a browser environment and displays a warning
902+
* about the security implications of running SQL directly from the browser.
903+
*/
904+
export declare function warnIfBrowser(): void;
905+
882906
export declare interface WebSocketConstructor {
883907
new (...args: any[]): WebSocketLike;
884908
}

index.d.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,14 @@ export declare interface HTTPQueryOptions<ArrayMode extends boolean, FullResults
315315
/**
316316
* Custom type parsers. See https://github.com/brianc/node-pg-types.
317317
*/
318-
types?: typeof types;
318+
types?: CustomTypesConfig;
319+
/**
320+
* When `disableWarningInBrowsers` is set to `true`, it disables the warning about
321+
* running this driver in the browser.
322+
*
323+
* Default: `false`
324+
*/
325+
disableWarningInBrowsers?: boolean;
319326
}
320327

321328
export declare interface HTTPTransactionOptions<ArrayMode extends boolean, FullResults extends boolean> extends HTTPQueryOptions<ArrayMode, FullResults> {
@@ -416,7 +423,7 @@ export { MessageConfig }
416423
* pass as `fetchOptions` an object which will be merged into the options
417424
* passed to `fetch`.
418425
*/
419-
export declare function neon<ArrayMode extends boolean = false, FullResults extends boolean = false>(connectionString: string, { arrayMode: neonOptArrayMode, fullResults: neonOptFullResults, fetchOptions: neonOptFetchOptions, isolationLevel: neonOptIsolationLevel, readOnly: neonOptReadOnly, deferrable: neonOptDeferrable, authToken, }?: HTTPTransactionOptions<ArrayMode, FullResults>): NeonQueryFunction<ArrayMode, FullResults>;
426+
export declare function neon<ArrayMode extends boolean = false, FullResults extends boolean = false>(connectionString: string, { arrayMode: neonOptArrayMode, fullResults: neonOptFullResults, fetchOptions: neonOptFetchOptions, isolationLevel: neonOptIsolationLevel, readOnly: neonOptReadOnly, deferrable: neonOptDeferrable, authToken, disableWarningInBrowsers, }?: HTTPTransactionOptions<ArrayMode, FullResults>): NeonQueryFunction<ArrayMode, FullResults>;
420427

421428
export declare interface NeonConfig {
422429
poolQueryViaFetch: boolean;
@@ -433,6 +440,7 @@ export declare interface NeonConfig {
433440
rootCerts: string;
434441
pipelineTLS: boolean;
435442
disableSNI: boolean;
443+
disableWarningInBrowsers: boolean;
436444
}
437445

438446
export declare class neonConfig extends EventEmitter {
@@ -552,6 +560,16 @@ export declare class neonConfig extends EventEmitter {
552560
static set disableSNI(newValue: NeonConfig['disableSNI']);
553561
get disableSNI(): NeonConfig["disableSNI"];
554562
set disableSNI(newValue: NeonConfig['disableSNI']);
563+
/**
564+
* When `disableWarningInBrowsers` is set to `true`, it disables the warning about
565+
* running this driver in the browser.
566+
*
567+
* Default: `false`.
568+
*/
569+
static get disableWarningInBrowsers(): NeonConfig["disableWarningInBrowsers"];
570+
static set disableWarningInBrowsers(newValue: NeonConfig['disableWarningInBrowsers']);
571+
get disableWarningInBrowsers(): NeonConfig["disableWarningInBrowsers"];
572+
set disableWarningInBrowsers(newValue: NeonConfig['disableWarningInBrowsers']);
555573
/**
556574
* Pipelines the startup message, cleartext password message and first query
557575
* when set to `"password"`. This works only for cleartext password auth.
@@ -782,7 +800,7 @@ export { PoolConfig }
782800
export declare interface ProcessQueryResultOptions {
783801
arrayMode: boolean;
784802
fullResults: boolean;
785-
types?: typeof types;
803+
types?: CustomTypesConfig;
786804
}
787805

788806
export { Query }
@@ -879,6 +897,12 @@ export declare class UnsafeRawSql {
879897
constructor(sql: string);
880898
}
881899

900+
/**
901+
* Detects if the code is running in a browser environment and displays a warning
902+
* about the security implications of running SQL directly from the browser.
903+
*/
904+
export declare function warnIfBrowser(): void;
905+
882906
export declare interface WebSocketConstructor {
883907
new (...args: any[]): WebSocketLike;
884908
}

0 commit comments

Comments
 (0)