Skip to content

Update transformer types and documentation #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,34 @@ You can use either a `postgres://` url connection string or the options to defin

```js
const sql = postgres('postgres://username:password@host:port/database', {
host : '', // Postgres ip address[s] or domain name[s]
port : 5432, // Postgres server port[s]
path : '', // unix socket path (usually '/tmp')
database : '', // Name of database to connect to
username : '', // Username of database user
password : '', // Password of database user
ssl : false, // true, prefer, require, tls.connect options
max : 10, // Max number of connections
idle_timeout : 0, // Idle connection timeout in seconds
connect_timeout : 30, // Connect timeout in seconds
no_prepare : false, // No automatic creation of prepared statements
types : [], // Array of custom types, see more below
onnotice : fn, // Defaults to console.log
onparameter : fn, // (key, value) when server param change
debug : fn, // Is called with (connection, query, params)
host : '', // Postgres ip address[s] or domain name[s]
port : 5432, // Postgres server port[s]
path : '', // unix socket path (usually '/tmp')
database : '', // Name of database to connect to
username : '', // Username of database user
password : '', // Password of database user
ssl : false, // true, prefer, require, tls.connect options
max : 10, // Max number of connections
idle_timeout : 0, // Idle connection timeout in seconds
connect_timeout : 30, // Connect timeout in seconds
no_prepare : false, // No automatic creation of prepared statements
types : [], // Array of custom types, see more below
onnotice : fn, // Defaults to console.log
onparameter : fn, // (key, value) when server param change
debug : fn, // Is called with (connection, query, params)
transform : {
column : fn, // Transforms incoming column names
value : fn, // Transforms incoming row values
row : fn // Transforms entire rows
column : {to?: fn, from?: fn}, // Transforms incoming or outgoing column names
value : {to?: fn, from?: fn}, // Transforms incoming or outgoing row values
row : {to?: fn, from?: fn}, // Transforms entire incoming or outgoing rows
Comment on lines +64 to +66
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@porsager wasn't sure of the best way to represent this. Please tweak if have a better idea.

},
connection : {
application_name : 'postgres.js', // Default application_name
... // Other connection parameters
application_name : 'postgres.js', // Default application_name
... // Other connection parameters
},
target_session_attrs : null, // Use 'read-write' with multiple hosts to
// ensure only connecting to primary
fetch_array_types : true, // Disable automatically fetching array types
// on initial connection.
target_session_attrs : null, // Use 'read-write' with multiple hosts to
// ensure only connecting to primary
fetch_array_types : true, // Disable automatically fetching array types
// on initial connection.
})
```

Expand Down
49 changes: 34 additions & 15 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ interface BaseOptions<T extends JSToPostgresTypeMap> {
debug: boolean | ((connection: number, query: string, parameters: any[]) => void);
/** Transform hooks */
transform: {
/** Transforms incoming column names */
column?: (column: string) => string;
/** Transforms incoming row values */
value?: (value: any) => any;
/** Transforms entire rows */
row?: (row: postgres.Row) => any;
/** Transforms incoming and outgoing column names. */
column?: ((column: string) => string) | { from?: (column: string) => string, to?: (column: string) => string };
/** Transforms incoming and outgoing row values. */
value?: ((value: any) => any) | { from?: (value: any) => any, to?: (value: any) => any };
/** Transforms entire incoming and outgoing rows. */
row?: ((row: postgres.Row) => any) | { from?: (row: postgres.Row) => any, to?: (row: any) => any };
};
/** Connection parameters */
connection: Partial<postgres.ConnectionParameters>;
Expand Down Expand Up @@ -109,21 +109,40 @@ declare namespace postgres {
export const PostgresError: PostgresErrorType;

/**
* Convert a string to Pascal case.
* @param str THe string to convert
* @returns The new string in Pascal case
* Convert a string from PascalCase to snake_case.
* @param str The PascalCase string to convert.
* @returns The new string in snake_case.
*/
function fromPascal(str: string): string;
/**
* Convert a camelCase string to snake_case.
* @param str The camelCase string to convert.
* @returns The new string in snake_case.
*/
function fromCamel(str: string): string;
/**
* Convert a kebab-case string to snake_case.
* @param str The kebab-case string to convert.
* @returns The new string in snake_case.
*/
function fromKebab(str: string): string;

/**
* Convert a snake_case string to PascalCase.
* @param str The snake_case string to convert.
* @returns The new string in PascalCase.
*/
function toPascal(str: string): string;
/**
* Convert a string to Camel case.
* @param str THe string to convert
* @returns The new string in Camel case
* Convert a snake_case string to camelCase.
* @param str The snake_case string to convert.
* @returns The new string in camelCase.
*/
function toCamel(str: string): string;
/**
* Convert a string to Kebab case.
* @param str THe string to convert
* @returns The new string in Kebab case
* Convert a snake_case string to kebab-case.
* @param str The snake_case string to convert.
* @returns The new string in kebab-case.
*/
function toKebab(str: string): string;

Expand Down