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

Better types overrides for enum #494

Merged
merged 1 commit into from
Feb 27, 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
23 changes: 13 additions & 10 deletions packages/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,10 @@ export class TypeAllocator {

const arrayValueType = typeNameOrType.slice(1);
// ^ Converts _varchar -> varchar, then wraps the type in an array
// type wrapper
if (this.isMappedType(arrayValueType)) {
typ = getArray(this.mapping[arrayValueType][scope]);
// make sure the element type is used so it appears in the declaration
this.use(this.mapping[arrayValueType][scope], scope);
}
}

if (typ == null) {
const mappedType = this.use(arrayValueType, scope);
typ = getArray({ name: mappedType });
} else {
if (!this.isMappedType(typeNameOrType)) {
if (this.allowUnmappedTypes) {
return typeNameOrType;
Expand All @@ -258,11 +253,19 @@ export class TypeAllocator {
}
} else {
if (isEnumArray(typeNameOrType)) {
typ = getArray(typeNameOrType.elementType);
if (this.mapping[typeNameOrType.elementType.name]?.[scope]) {
typ = getArray({
name: typeNameOrType.elementType.name,
definition:
this.mapping[typeNameOrType.elementType.name][scope].name,
});
} else {
typ = getArray(typeNameOrType.elementType);
}
// make sure the element type is used so it appears in the declaration
this.use(typeNameOrType.elementType, scope);
} else {
typ = typeNameOrType;
typ = this.mapping[typeNameOrType.name]?.[scope] ?? typeNameOrType;
}
}

Expand Down
7 changes: 5 additions & 2 deletions packages/example/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
}
],
"typesOverrides": {
"date": "string",
"int8": "BigInt"
"date": {
"return": "string"
},
"int8": "BigInt",
"category": { "return": "./src/customTypes#Category" }
},
"srcDir": "./src/",
"dbUrl": "postgres://postgres:password@localhost/postgres"
Expand Down
35 changes: 34 additions & 1 deletion packages/example/src/books/books.queries.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/** Types generated for queries found in "src/books/books.sql" */
import { Category } from '../customTypes';

import { PreparedQuery } from '@pgtyped/runtime';

export type Iso31661Alpha2 = 'AD' | 'AE' | 'AF' | 'AG' | 'AI' | 'AL' | 'AM' | 'AO' | 'AQ' | 'AR' | 'AS' | 'AT' | 'AU' | 'AW' | 'AX' | 'AZ' | 'BA' | 'BB' | 'BD' | 'BE' | 'BF' | 'BG' | 'BH' | 'BI' | 'BJ' | 'BL' | 'BM' | 'BN' | 'BO' | 'BQ' | 'BR' | 'BS' | 'BT' | 'BV' | 'BW' | 'BY' | 'BZ' | 'CA' | 'CC' | 'CD' | 'CF' | 'CG' | 'CH' | 'CI' | 'CK' | 'CL' | 'CM' | 'CN' | 'CO' | 'CR' | 'CU' | 'CV' | 'CW' | 'CX' | 'CY' | 'CZ' | 'DE' | 'DJ' | 'DK' | 'DM' | 'DO' | 'DZ' | 'EC' | 'EE' | 'EG' | 'EH' | 'ER' | 'ES' | 'ET' | 'FI' | 'FJ' | 'FK' | 'FM' | 'FO' | 'FR' | 'GA' | 'GB' | 'GD' | 'GE' | 'GF' | 'GG' | 'GH' | 'GI' | 'GL' | 'GM' | 'GN' | 'GP' | 'GQ' | 'GR' | 'GS' | 'GT' | 'GU' | 'GW' | 'GY' | 'HK' | 'HM' | 'HN' | 'HR' | 'HT' | 'HU' | 'ID' | 'IE' | 'IL' | 'IM' | 'IN' | 'IO' | 'IQ' | 'IR' | 'IS' | 'IT' | 'JE' | 'JM' | 'JO' | 'JP' | 'KE' | 'KG' | 'KH' | 'KI' | 'KM' | 'KN' | 'KP' | 'KR' | 'KW' | 'KY' | 'KZ' | 'LA' | 'LB' | 'LC' | 'LI' | 'LK' | 'LR' | 'LS' | 'LT' | 'LU' | 'LV' | 'LY' | 'MA' | 'MC' | 'MD' | 'ME' | 'MF' | 'MG' | 'MH' | 'MK' | 'ML' | 'MM' | 'MN' | 'MO' | 'MP' | 'MQ' | 'MR' | 'MS' | 'MT' | 'MU' | 'MV' | 'MW' | 'MX' | 'MY' | 'MZ' | 'NA' | 'NC' | 'NE' | 'NF' | 'NG' | 'NI' | 'NL' | 'NO' | 'NP' | 'NR' | 'NU' | 'NZ' | 'OM' | 'PA' | 'PE' | 'PF' | 'PG' | 'PH' | 'PK' | 'PL' | 'PM' | 'PN' | 'PR' | 'PS' | 'PT' | 'PW' | 'PY' | 'QA' | 'RE' | 'RO' | 'RS' | 'RU' | 'RW' | 'SA' | 'SB' | 'SC' | 'SD' | 'SE' | 'SG' | 'SH' | 'SI' | 'SJ' | 'SK' | 'SL' | 'SM' | 'SN' | 'SO' | 'SR' | 'SS' | 'ST' | 'SV' | 'SX' | 'SY' | 'SZ' | 'TC' | 'TD' | 'TF' | 'TG' | 'TH' | 'TJ' | 'TK' | 'TL' | 'TM';

export type category = 'novel' | 'science-fiction' | 'thriller';

export type categoryArray = (category)[];
export type categoryArray = (Category)[];

export type numberArray = (number)[];

Expand Down Expand Up @@ -42,6 +44,37 @@ const findBookByIdIR: any = {"usedParamSet":{"id":true},"params":[{"name":"id","
export const findBookById = new PreparedQuery<IFindBookByIdParams,IFindBookByIdResult>(findBookByIdIR);


/** 'FindBookByCategory' parameters type */
export interface IFindBookByCategoryParams {
category?: category | null | void;
}

/** 'FindBookByCategory' return type */
export interface IFindBookByCategoryResult {
author_id: number | null;
categories: categoryArray | null;
id: number;
name: string | null;
rank: number | null;
}

/** 'FindBookByCategory' query type */
export interface IFindBookByCategoryQuery {
params: IFindBookByCategoryParams;
result: IFindBookByCategoryResult;
}

const findBookByCategoryIR: any = {"usedParamSet":{"category":true},"params":[{"name":"category","required":false,"transform":{"type":"scalar"},"locs":[{"a":26,"b":34}]}],"statement":"SELECT * FROM books WHERE :category = ANY(categories)"};

/**
* Query generated from SQL:
* ```
* SELECT * FROM books WHERE :category = ANY(categories)
* ```
*/
export const findBookByCategory = new PreparedQuery<IFindBookByCategoryParams,IFindBookByCategoryResult>(findBookByCategoryIR);


/** 'FindBookNameOrRank' parameters type */
export interface IFindBookNameOrRankParams {
name?: string | null | void;
Expand Down
3 changes: 3 additions & 0 deletions packages/example/src/books/books.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* @name FindBookById */
SELECT * FROM books WHERE id = :id;

/* @name FindBookByCategory */
SELECT * FROM books WHERE :category = ANY(categories);

/* @name FindBookNameOrRank */
SELECT id, name
FROM books
Expand Down
5 changes: 5 additions & 0 deletions packages/example/src/customTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum Category {
Novel = 'novel',
ScienceFiction = 'science-fiction',
Thriller = 'thriller',
}
Loading