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

Add types overrides #476

Merged
merged 2 commits into from
Jan 21, 2023
Merged
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
3 changes: 3 additions & 0 deletions docs-new/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ These variables will override values provided in `config.json`.
"host": "127.0.0.1", // DB host (optional)
"port": 5432, // DB port (optional)
"ssl": false // Whether or not to connect to DB with SSL (optional)
},
"typesOverrides": {
"date": "string" // Override default Postgres => TypeScript mapping
}
}
```
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as t from 'io-ts';
import { reporter } from 'io-ts-reporters';
import tls from 'tls';
import parseDatabaseUri, { DatabaseConfig } from 'ts-parse-database-url';
import { TypeMapping } from './types';

const transformCodecProps = {
include: t.string,
Expand Down Expand Up @@ -46,6 +47,7 @@ const configParser = t.type({
}),
t.undefined,
]),
typesOverrides: t.union([t.record(t.string, t.string), t.undefined]),
});

export type IConfig = typeof configParser._O;
Expand All @@ -64,6 +66,7 @@ export interface ParsedConfig {
hungarianNotation: boolean;
transforms: IConfig['transforms'];
srcDir: IConfig['srcDir'];
typesOverrides: Partial<TypeMapping>;
}

function merge<T>(base: T, ...overrides: Partial<T>[]): T {
Expand Down Expand Up @@ -131,6 +134,7 @@ export function parseConfig(
failOnError,
camelCaseColumnNames,
hungarianNotation,
typesOverrides,
} = configObject as IConfig;

// CLI connectionUri flag takes precedence over the env and config one
Expand All @@ -149,12 +153,19 @@ export function parseConfig(

const finalDBConfig = merge(defaultDBConfig, db, urlDBConfig, envDBConfig);

const parsedTypesOverrides: Partial<TypeMapping> = {};

for (const [builtIn, mappedTo] of Object.entries(typesOverrides ?? {})) {
parsedTypesOverrides[builtIn] = { name: mappedTo };
}

return {
db: finalDBConfig,
transforms,
srcDir,
failOnError: failOnError ?? false,
camelCaseColumnNames: camelCaseColumnNames ?? false,
hungarianNotation: hungarianNotation ?? true,
typesOverrides: parsedTypesOverrides,
};
}
7 changes: 4 additions & 3 deletions packages/cli/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { pascalCase } from 'pascal-case';
import path from 'path';
import { ParsedConfig } from './config';
import { ProcessingMode } from './index';
import { DefaultTypeMapping, TypeAllocator } from './types';
import { TypeAllocator, TypeMapping } from './types';

export interface IField {
fieldName: string;
Expand Down Expand Up @@ -236,7 +236,7 @@ async function generateTypedecsFromFile(
fileName: string,
connection: any,
mode: 'ts' | 'sql',
types: TypeAllocator = new TypeAllocator(DefaultTypeMapping),
types: TypeAllocator,
config: ParsedConfig,
): Promise<ITypedQuery[]> {
const results: ITypedQuery[] = [];
Expand Down Expand Up @@ -309,9 +309,10 @@ export async function generateDeclarationFile(
fileName: string,
connection: any,
mode: 'ts' | 'sql',
types: TypeAllocator = new TypeAllocator(DefaultTypeMapping),
config: ParsedConfig,
): Promise<{ typeDecs: ITypedQuery[]; declarationFileContents: string }> {
const types = new TypeAllocator(TypeMapping(config.typesOverrides));

if (mode === 'sql') {
types.use({ name: 'PreparedQuery', from: '@pgtyped/query' });
}
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export async function processFile(
fileName,
connection,
transform.mode,
void 0,
config,
);
const relativePath = path.relative(process.cwd(), decsFileName);
Expand Down