Skip to content

Commit 23fc7da

Browse files
committed
Update transformer types and documentation
1 parent 2915105 commit 23fc7da

File tree

2 files changed

+62
-39
lines changed

2 files changed

+62
-39
lines changed

README.md

+24-24
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,34 @@ You can use either a `postgres://` url connection string or the options to defin
4545

4646
```js
4747
const sql = postgres('postgres://username:password@host:port/database', {
48-
host : '', // Postgres ip address[s] or domain name[s]
49-
port : 5432, // Postgres server port[s]
50-
path : '', // unix socket path (usually '/tmp')
51-
database : '', // Name of database to connect to
52-
username : '', // Username of database user
53-
password : '', // Password of database user
54-
ssl : false, // true, prefer, require, tls.connect options
55-
max : 10, // Max number of connections
56-
idle_timeout : 0, // Idle connection timeout in seconds
57-
connect_timeout : 30, // Connect timeout in seconds
58-
no_prepare : false, // No automatic creation of prepared statements
59-
types : [], // Array of custom types, see more below
60-
onnotice : fn, // Defaults to console.log
61-
onparameter : fn, // (key, value) when server param change
62-
debug : fn, // Is called with (connection, query, params)
48+
host : '', // Postgres ip address[s] or domain name[s]
49+
port : 5432, // Postgres server port[s]
50+
path : '', // unix socket path (usually '/tmp')
51+
database : '', // Name of database to connect to
52+
username : '', // Username of database user
53+
password : '', // Password of database user
54+
ssl : false, // true, prefer, require, tls.connect options
55+
max : 10, // Max number of connections
56+
idle_timeout : 0, // Idle connection timeout in seconds
57+
connect_timeout : 30, // Connect timeout in seconds
58+
no_prepare : false, // No automatic creation of prepared statements
59+
types : [], // Array of custom types, see more below
60+
onnotice : fn, // Defaults to console.log
61+
onparameter : fn, // (key, value) when server param change
62+
debug : fn, // Is called with (connection, query, params)
6363
transform : {
64-
column : fn, // Transforms incoming column names
65-
value : fn, // Transforms incoming row values
66-
row : fn // Transforms entire rows
64+
column : {to?: fn, from?: fn}, // Transforms incoming or outgoing column names
65+
value : {to?: fn, from?: fn}, // Transforms incoming or outgoing row values
66+
row : {to?: fn, from?: fn}, // Transforms entire incoming or outgoing rows
6767
},
6868
connection : {
69-
application_name : 'postgres.js', // Default application_name
70-
... // Other connection parameters
69+
application_name : 'postgres.js', // Default application_name
70+
... // Other connection parameters
7171
},
72-
target_session_attrs : null, // Use 'read-write' with multiple hosts to
73-
// ensure only connecting to primary
74-
fetch_array_types : true, // Disable automatically fetching array types
75-
// on initial connection.
72+
target_session_attrs : null, // Use 'read-write' with multiple hosts to
73+
// ensure only connecting to primary
74+
fetch_array_types : true, // Disable automatically fetching array types
75+
// on initial connection.
7676
})
7777
```
7878

types/index.d.ts

+38-15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ declare function postgres<T extends JSToPostgresTypeMap>(options?: postgres.Opti
1212
*/
1313
declare function postgres<T extends JSToPostgresTypeMap>(url: string, options?: postgres.Options<T>): postgres.Sql<JSToPostgresTypeMap extends T ? {} : T>
1414

15+
declare type ColumnTransformer = (column: string) => string;
16+
declare type ValueTransformer = (value: any) => any;
17+
declare type RowTransformer = (row: postgres.Row) => any;
18+
1519
/**
1620
* Connection options of Postgres.
1721
*/
@@ -52,12 +56,12 @@ interface BaseOptions<T extends JSToPostgresTypeMap> {
5256
debug: boolean | ((connection: number, query: string, parameters: any[]) => void);
5357
/** Transform hooks */
5458
transform: {
55-
/** Transforms incoming column names */
56-
column?: (column: string) => string;
57-
/** Transforms incoming row values */
58-
value?: (value: any) => any;
59-
/** Transforms entire rows */
60-
row?: (row: postgres.Row) => any;
59+
/** Transforms incoming and outgoing column names. */
60+
column?: (column: string) => string | { from?: (column: string) => string, to?: (column: string) => string };
61+
/** Transforms incoming and outgoing row values. */
62+
value?: (value: any) => any | { from?: (value: any) => any, to?: (value: any) => any };
63+
/** Transforms entire incoming and outgoing rows. */
64+
row?: (row: postgres.Row) => any | { from?: (row: postgres.Row) => any, to?: (row: postgres.Row) => any };
6165
};
6266
/** Connection parameters */
6367
connection: Partial<postgres.ConnectionParameters>;
@@ -109,21 +113,40 @@ declare namespace postgres {
109113
export const PostgresError: PostgresErrorType;
110114

111115
/**
112-
* Convert a string to Pascal case.
113-
* @param str THe string to convert
114-
* @returns The new string in Pascal case
116+
* Convert a string from PascalCase to snake_case.
117+
* @param str The PascalCase string to convert.
118+
* @returns The new string in snake_case.
119+
*/
120+
function fromPascal(str: string): string;
121+
/**
122+
* Convert a camelCase string to snake_case.
123+
* @param str The camelCase string to convert.
124+
* @returns The new string in snake_case.
125+
*/
126+
function fromCamel(str: string): string;
127+
/**
128+
* Convert a kebab-case string to snake_case.
129+
* @param str The kebab-case string to convert.
130+
* @returns The new string in snake_case.
131+
*/
132+
function fromKebab(str: string): string;
133+
134+
/**
135+
* Convert a snake_case string to PascalCase.
136+
* @param str The snake_case string to convert.
137+
* @returns The new string in PascalCase.
115138
*/
116139
function toPascal(str: string): string;
117140
/**
118-
* Convert a string to Camel case.
119-
* @param str THe string to convert
120-
* @returns The new string in Camel case
141+
* Convert a snake_case string to camelCase.
142+
* @param str The snake_case string to convert.
143+
* @returns The new string in camelCase.
121144
*/
122145
function toCamel(str: string): string;
123146
/**
124-
* Convert a string to Kebab case.
125-
* @param str THe string to convert
126-
* @returns The new string in Kebab case
147+
* Convert a snake_case string to kebab-case.
148+
* @param str The snake_case string to convert.
149+
* @returns The new string in kebab-case.
127150
*/
128151
function toKebab(str: string): string;
129152

0 commit comments

Comments
 (0)