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

ts-pg-promise: typed queries for pg-promise without runtime dependencies #559

Open
wants to merge 2 commits into
base: master
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
62 changes: 61 additions & 1 deletion packages/cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,27 @@ const TSTypedSQLTagTransformCodec = t.type({
emitFileName: t.string,
});

const TSPgPromiseTransformCodec = t.type({
mode: t.literal('ts-pg-promise'),
include: t.string,
exclude: t.union([t.string, t.undefined]),
emitFileName: t.string,
tsconfigPath: t.string,
maxMethodParameterUnionTypeLength: t.union([t.number, t.undefined]),
argumentTypeWarning: t.union([t.boolean, t.undefined]),
parameterKindWarning: t.union([t.boolean, t.undefined]),
variableNames: t.array(t.string),
interfaceName: t.string,
});

export type TSTypedSQLTagTransformConfig = t.TypeOf<
typeof TSTypedSQLTagTransformCodec
>;

export type TSPgPromiseTransformConfig = t.TypeOf<
typeof TSPgPromiseTransformCodec
>;

const SQLTransformCodec = t.type({
mode: t.literal('sql'),
...transformCodecProps,
Expand All @@ -45,17 +62,42 @@ const TransformCodec = t.union([
TSTransformCodec,
SQLTransformCodec,
TSTypedSQLTagTransformCodec,
TSPgPromiseTransformCodec,
]);

const EnumsAsEnumsCodec = t.type({
style: t.literal('enum'),
nameCase: t.union([t.literal('keep'), t.literal('pascal')]),
keyCase: t.union([
t.literal('upper'),
t.literal('lower'),
t.literal('sameAsValue'),
]),
dropNameSuffix: t.union([t.string, t.undefined]),
});

const EnumsAsTypesCodec = t.type({
style: t.literal('type'),
});

const EnumCodec = t.union([EnumsAsEnumsCodec, EnumsAsTypesCodec]);

export type EnumsAsEnumsConfig = t.TypeOf<typeof EnumsAsEnumsCodec>;

export type TransformConfig = t.TypeOf<typeof TransformCodec>;

export type EnumConfig = t.TypeOf<typeof EnumCodec>;

const configParser = t.type({
// maximum number of worker threads to use for the codegen worker pool
maxWorkerThreads: t.union([t.number, t.undefined]),
transforms: t.array(TransformCodec),
srcDir: t.string,
failOnError: t.union([t.boolean, t.undefined]),
camelCaseColumnNames: t.union([t.boolean, t.undefined]),
anonymousColumnWarning: t.union([t.boolean, t.undefined]),
enums: t.union([EnumCodec, t.undefined]),
interfaceComments: t.union([t.boolean, t.undefined]),
hungarianNotation: t.union([t.boolean, t.undefined]),
dbUrl: t.union([t.string, t.undefined]),
db: t.union([
Expand All @@ -66,6 +108,7 @@ const configParser = t.type({
user: t.union([t.string, t.undefined]),
dbName: t.union([t.string, t.undefined]),
ssl: t.union([t.UnknownRecord, t.boolean, t.undefined]),
schema: t.union([t.string, t.undefined]),
}),
t.undefined,
]),
Expand Down Expand Up @@ -94,10 +137,14 @@ export interface ParsedConfig {
dbName: string;
port: number;
ssl?: tls.ConnectionOptions | boolean;
schema?: string;
};
maxWorkerThreads: number | undefined;
failOnError: boolean;
camelCaseColumnNames: boolean;
anonymousColumnWarning: boolean;
enums: EnumConfig | undefined;
interfaceComments: boolean;
hungarianNotation: boolean;
transforms: IConfig['transforms'];
srcDir: IConfig['srcDir'];
Expand Down Expand Up @@ -199,6 +246,9 @@ export function parseConfig(
camelCaseColumnNames,
hungarianNotation,
typesOverrides,
enums,
interfaceComments,
anonymousColumnWarning,
} = configObject as IConfig;

// CLI connectionUri flag takes precedence over the env and config one
Expand All @@ -208,7 +258,14 @@ export function parseConfig(
? convertParsedURLToDBConfig(parseDatabaseUri(dbUri))
: {};

if (transforms.some((tr) => tr.mode !== 'ts-implicit' && !!tr.emitFileName)) {
if (
transforms.some(
(tr) =>
tr.mode !== 'ts-implicit' &&
tr.mode !== 'ts-pg-promise' &&
!!tr.emitFileName,
)
) {
// tslint:disable:no-console
console.log(
'Warning: Setting "emitFileName" is deprecated. Consider using "emitTemplate" instead.',
Expand Down Expand Up @@ -241,8 +298,11 @@ export function parseConfig(
srcDir,
failOnError: failOnError ?? false,
camelCaseColumnNames: camelCaseColumnNames ?? false,
anonymousColumnWarning: anonymousColumnWarning ?? true,
hungarianNotation: hungarianNotation ?? true,
typesOverrides: parsedTypesOverrides,
maxWorkerThreads,
enums,
interfaceComments: interfaceComments ?? true,
};
}
Loading