Skip to content
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

Extend overrides: Add Type Overrides #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
187 changes: 180 additions & 7 deletions main.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,139 @@
/// <reference types="node" />
import { Knex } from "knex";
import type { Writable } from "stream";
import { Knex } from 'knex';
import type { Writable } from 'stream';
export declare type Options = {
/**
* Filename or output stream where the type definitions needs to be written.
*/
output: Writable | string;
/**
* Name overrides for enums, classes, and fields.
* The name of the exported enum (Which encapsulates all tables)
* Default: "Table"
*/
tablesEnumName?: string;
/**
* The name of the exported tables type
* Default: "Tables"
*/
tablesTypeName?: string;
/**
* Name overrides for enums, schemas, tables and columns.
* Check tests for more info.
*
* @example
* overrides: {
* "identity_provider.linkedin": "LinkedIn"
* }
*
* @example
* Override a table name with a function
*
* overrides: {
* // Overwrite the 'user' table name
* user: (x, type, defaultValue) => UserTable
* }
*
* @example
* Tag all schemas, tables and columns
*
* overrides: {
* // Append "Table" to all tables and TitleCase the name
* "*": (x, type, defaultValue) => type === "table" ? "Table" + upperFirst(camelCase(x.table)) : defaultValue
* }
*/
overrides?: Record<string, string>;
overrides?: Record<string, OverrideStringFunction>;
/**
* Overrides of column types.
* Overrides have higher priority the more specific they are, from highest to lowest:
*
* 1. A single table's column
* 2. All tables columns
* 3. Database's default types
*
* If any of these types can overwrite the other, the one with the higher specificity will be chosen.
*
* The special `"*"` can be used to match all columns after all types have been set. If it's a function
* call it, otherwise just return the value.
* Whatever this special override returns will overwrite all other processed types. That's why it's
* recommended for this property to be a function, as the second argument provided will be the already
* processed type.
*
* @example
* Override a specific table's column.
* If `overrideTableColumnTypes` is set to true, a table's column type will be overwritten
* with the provided custom type.
* Check tests for more info.
*
* typeOverrides: {
* // Will only modify the `notes` column in the `messages` table.
* "messages.notes": "OnlyForThisColumnInThisTable",
*
* // A function can also be provided.
* "messages.notes": (x: Column) => "OnlyForThisColumnInThisTable"
* }
*
* @example
* Override all columns with the same name.
* if `overrideColumnTypes` is set to true, all columns types from all tables that matches
* the column name will be overwritten with the provided custom type.
* Check tests for more info.
*
* typeOverrides: {
* // Will modify all columns called `notes` in all tables.
* "notes": "SomeTypeForAllNotesColumns",
*
* // A function can also be provided.
* "messages.notes": (x: Column) => "SomeTypeForAllNotesColumns"
* }
*
* @example
* Override a database's default type.
* if `overrideDefaultTypes` is set to true, all database's default types that match
* the type name will be overwritten with the provided custom type.
* Check tests for more info.
*
* prefix: 'type Numeric = `${number}` | Number;',
* typeOverrides: {
* // Will modify all decimal columns (which by default are of type 'numeric').
* "numeric": "Numeric",
*
* // A function can also be provided.
* "numeric": (x: Column) => "Numeric"
* }
*
* @example
* Use the special "*" override to format all types (The second argument provided is
* the type that has been processed so far).
* This value provided will overwrite all types.
* Check tests for more info.
*
* typeOverrides: {
* // All types will be set to `null`
* "*": "null",
*
* // Append 'Nullable' to all types
* "numeric": (x: Column, defaultType: string) => defaultType + ' | Nullable'
* }
*/
typeOverrides?: TypePostProcessor | TypeOverride;
/**
* Enable the overriding of a specific table's column type.
* Default value is true.
* More info at the `typeOverrides` documentation.
*/
overrideTableColumnTypes?: boolean;
/**
* Enable the overriding of all columns with the same name types.
* Default value is true.
* More info at the `typeOverrides` documentation.
*/
overrideColumnTypes?: boolean;
/**
* Enable the overriding of the database's default types.
* Default value is true.
* More info at the `typeOverrides` documentation.
*/
overrideDefaultTypes?: boolean;
prefix?: string;
suffix?: string;
/**
Expand Down Expand Up @@ -47,8 +166,62 @@ export declare type Options = {
* Generates TypeScript definitions (types) from a PostgreSQL database schema.
*/
export declare function updateTypes(db: Knex, options: Options): Promise<void>;
export declare type Enum = {
key: string;
value: string;
};
export declare type Column = {
table: string;
column: string;
schema: string;
nullable: boolean;
default: string | null;
type: string;
udt: string;
};
export declare type NameOverrideCategory = keyof Column &
('table' | 'schema' | 'column');
export declare type OverrideStringFunction =
| string
| ((
x: Column,
category: NameOverrideCategory,
defaultValue: string | null
) => string | null);
export declare type TypeOverride = Record<
string,
string | ((x: Column) => string)
>;
export declare type TypePostProcessor = Record<
'*',
string | ((x: Column, defaultType: string) => string)
>;
export declare function getType(
udt: string,
customTypes: Map<string, string>,
defaultValue: string | null
x: Column,
customTypes: Map<string, string>
): string;
/**
* If enabled override a column belonging to a specific table (highest priority),
* all columns with the same name or overwrite the database's default type (lowest priority).
* A function can be provided for each override.
*/
export declare function overrideType(
x: Column,
options: Options
): string | null;
/**
* If the "*" override has been provided, if it's a function then call it and return
* the value, otherwise just return the value it holds.
* If the "*" override was not provided just return again the same type.
* Whatever this function returns will override the processed type.
*/
export declare function typePostProcessor(
x: Column,
type: string,
options: Options
): string;
export declare function overrideName(
x: Column,
category: NameOverrideCategory,
overrides: Record<string, OverrideStringFunction>
): string | null;
Loading