Generates Typescript type definitions from live Postgres server.
npm install ts-pg-modelGenerates Typescript type definitions from live Postgres server.
You can assign connectionURI in configuration or use PG_CONNECTION_URI environment variable.
URI value must follow valid Postgres URI scheme starting with postgresql:// or postgres://.
dotenv is also supported. You can also set .env file
location with dotEnvPath in configuration.
``ts`
{
output: {
root?: string;
includeSchemaPath?: boolean;
keepFiles?: string[];
}
}
output.root
Defaults to ./src/generated.
tsConfig
Defaults to ./tsconfig.json
If there are multiple schemas targeted for file generation, each resources within a schema will be
always grouped within a directory named after the schema. If only single schema is targeted (e.g.
public) directory structure will not be used unless output.includeSchemaPath is set to true.
Files will only be overwritten if there are structural changes (@generated date will not beoutput.root
updated) and will be deleted if no longer needed. If any files should be kept from being removed
inside the root folder, add file paths (relative to ) to output.keepFiles.
`ts
export type ChangeCase =
| 'camelCase'
| 'capitalCase'
| 'constantCase'
| 'dotCase'
| 'kebabCase'
| 'noCase'
| 'pascalCase'
| 'pathCase'
| 'sentenceCase'
| 'snakeCase'
| 'trainCase'
| 'keep';
const config: UserConfig = {
conventions: {
schemas: 'camelCase',
columns: 'keep',
types: 'camelCase',
paths: 'kebabCase',
},
};
`
#### Code convensions
You can choose code convensions of schemas, columns and types. Choices are open to all methods
provided by change-case or keep (no transform).
##### Schema names
Schema code convensions are applied to namespaces. Defaults to camelCase.
##### Column names
Column code conventions are applied to column names. Defaults to keep. Can be useful if you prefercamelCase
automatic column name transformation to , etc.
##### Enum Types
Enum type code conventions are applied to Enum member names. Defaults to camelCase.
#### Paths and filename conventions
Generated file paths and filename conventions. Defaults to kebabCase.
You can target schemas, tables and columns using target selectors.
#### schema
If undefined, default schemas will be chosen by the result of show search_path query, excluding"$user". In most of user cases, the result would be ["public"].
#### tables & columns
Tables and columns can be explicitly included or excluded using targetSelectors configuration.targetSelectors is a series of filters which includes or excludes targets in order.
You can define table / columns names with dot notation, including schema / table names.
If schema / tables names are not defined using dot notation, all matching targets will be
included / excluded.
#### User defined types (UDT)
Most of known Postgres types should be mapped automatically. If there is an unknown data type that
needs casting into a primitive type, use typeMap.udt settings. Array type (e.g. _text, withudt_name
underscore prefix in ) should be automatically handled.
#### JSON(B) definitions
User defined JSON(B) types can be achived via typeMap.json settings & type definitions in thetypeMap.json
settings file. When is defined, this will require JSON type definitions asUserConfig arguments (UserConfig).
#### Example
`ts
export interface JsonTypeMap {
MediaMetadata: {
width: number;
height: number;
};
}
// UserConfig variable has to be either exported via named export (with name userConfig) or default exportusers
export const userConfig: Config
// includes schema only.media.images
schemas: ['users'],
targetSelectors: [
{
include: {
// includes table, despite media schema was not included.sessions
tables: ['media.images'],
},
},
{
exclude: {
// excludes all tables with name , in this case users.sessions table.created_at
tables: ['sessions'],
// excludes , updated_at columns in all tablesusers.sessions
columns: ['created_at', 'updated_at'],
},
},
{
include: {
// include again.created_at
// be noted that all columns including column in this table will be includedcreated_at
// despite all columns with name was removed in previous exclude rule.media.images.updated_at
tables: ['sessions'],
// includes column, despite all updated_at columns were excluded.``
columns: ['media.images.updated_at'],
},
},
],
typeMap: {
json: [
{
schema: 'media',
tableName: 'images',
columnName: 'metadata',
name: 'MediaMetadata',
},
],
},
conventions: {
columns: 'camelCase',
},
};