diff --git a/packages/cli/src/types.ts b/packages/cli/src/types.ts index de12c3f3..9f06834c 100644 --- a/packages/cli/src/types.ts +++ b/packages/cli/src/types.ts @@ -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; @@ -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; } } diff --git a/packages/example/config.json b/packages/example/config.json index 3d3ef202..482b3b9f 100644 --- a/packages/example/config.json +++ b/packages/example/config.json @@ -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" diff --git a/packages/example/src/books/books.queries.ts b/packages/example/src/books/books.queries.ts index 790c0117..260f6ae5 100644 --- a/packages/example/src/books/books.queries.ts +++ b/packages/example/src/books/books.queries.ts @@ -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)[]; @@ -42,6 +44,37 @@ const findBookByIdIR: any = {"usedParamSet":{"id":true},"params":[{"name":"id"," export const findBookById = new PreparedQuery(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(findBookByCategoryIR); + + /** 'FindBookNameOrRank' parameters type */ export interface IFindBookNameOrRankParams { name?: string | null | void; diff --git a/packages/example/src/books/books.sql b/packages/example/src/books/books.sql index 11b9a7bb..1b64a43d 100644 --- a/packages/example/src/books/books.sql +++ b/packages/example/src/books/books.sql @@ -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 diff --git a/packages/example/src/customTypes.ts b/packages/example/src/customTypes.ts new file mode 100644 index 00000000..fea304a9 --- /dev/null +++ b/packages/example/src/customTypes.ts @@ -0,0 +1,5 @@ +export enum Category { + Novel = 'novel', + ScienceFiction = 'science-fiction', + Thriller = 'thriller', +} diff --git a/packages/example/src/index.test.ts b/packages/example/src/index.test.ts index 9a382510..0947e7b7 100644 --- a/packages/example/src/index.test.ts +++ b/packages/example/src/index.test.ts @@ -1,28 +1,36 @@ -import {test, expect, afterEach, beforeEach, beforeAll, describe} from '@jest/globals'; +import { afterEach, beforeAll, beforeEach, expect, test } from '@jest/globals'; import pg from 'pg'; -const {Client} = pg; import { - aggregateEmailsAndTest, - findBookUnicode, - findBookById, - getBooksByAuthorName, - insertBooks, - updateBooks, - updateBooksCustom, - updateBooksRankNotNull, - findBookNameOrRank, getBooks, countBooks, + aggregateEmailsAndTest, + countBooks, + findBookById, + findBookNameOrRank, + findBookUnicode, + getBooks, + getBooksByAuthorName, + insertBooks, + updateBooks, + updateBooksCustom, + updateBooksRankNotNull, } from './books/books.queries.js'; -import {getAllComments, insertComment, selectExistsTest} from './comments/comments.queries.js'; +import { + getAllComments, + insertComment, + selectExistsTest, +} from './comments/comments.queries.js'; import { insertNotification, insertNotifications, } from './notifications/notifications.js'; import { - getNotifications, - sendNotifications, - thresholdFrogs, + getNotifications, + sendNotifications, + thresholdFrogs, } from './notifications/notifications.queries.js'; -import {getUsersWithComment} from "./users/sample.js"; +import { getUsersWithComment } from './users/sample.js'; +import { Category } from './customTypes'; + +const { Client } = pg; const dbConfig = { host: process.env.PGHOST ?? '127.0.0.1', @@ -35,179 +43,190 @@ const dbConfig = { // Connect to the database once before all tests let client: pg.Client; beforeAll(async () => { - // Parse dates as strings for demo and testing purposes - pg.types.setTypeParser(pg.types.builtins.DATE, function(val) { - return val; - }) - - pg.types.setTypeParser(pg.types.builtins.INT8, function(val) { - return BigInt(val); - }) - - // Create a new client and connect to the database - client = new Client(dbConfig); - await client.connect(); + // Parse dates as strings for demo and testing purposes + pg.types.setTypeParser(pg.types.builtins.DATE, function (val) { + return val; + }); + + pg.types.setTypeParser(pg.types.builtins.INT8, function (val) { + return BigInt(val); + }); + + // Create a new client and connect to the database + client = new Client(dbConfig); + await client.connect(); }); // Disconnect from the database after all tests afterAll(async () => { - await client.end(); -}) + await client.end(); +}); // Run each test in a transaction that is rolled back at the end -beforeEach( () => client.query('BEGIN')) -afterEach( () => client.query('ROLLBACK')) +beforeEach(() => client.query('BEGIN')); +afterEach(() => client.query('ROLLBACK')); test('select query with unicode characters', () => { - const result = findBookUnicode.run(undefined, client); - expect(result).resolves.toMatchSnapshot(); -}) + const result = findBookUnicode.run(undefined, client); + expect(result).resolves.toMatchSnapshot(); +}); test('select query with parameters', async () => { - const comments = await getAllComments.run({ id: 1 }, client); - expect(comments).toMatchSnapshot(); -}) + const comments = await getAllComments.run({ id: 1 }, client); + expect(comments).toMatchSnapshot(); +}); test('select query with dynamic or', () => { - const result = findBookNameOrRank.run({ - rank: 1, - }, client); - expect(result).resolves.toMatchSnapshot(); -}) + const result = findBookNameOrRank.run( + { + rank: 1, + }, + client, + ); + expect(result).resolves.toMatchSnapshot(); +}); test('select query with date type override (TS)', async () => { - const comments = await getUsersWithComment(0, client); - const dateAsString: string = comments.registration_date; - expect(typeof dateAsString).toBe("string"); -}) + const comments = await getUsersWithComment(0, client); + const dateAsString: string = comments.registration_date; + expect(typeof dateAsString).toBe('string'); +}); test('select query with date type override (SQL)', async () => { - const notifications = await getNotifications.run({userId: 1}, client); - const dateAsString: string = notifications[0].created_at; - expect(typeof dateAsString).toBe("string"); -}) + const notifications = await getNotifications.run( + { userId: 1, date: '2000-01-01' }, + client, + ); + const dateAsString: string = notifications[0].created_at; + expect(typeof dateAsString).toBe('string'); +}); test('insert query with parameter spread', async () => { const [{ book_id: insertedBookId }] = await insertBooks.run( - { - books: [ - { - authorId: 1, - name: 'A Brief History of Time: From the Big Bang to Black Holes', - rank: 1, - categories: ['novel', 'science-fiction'], - }, - ], - }, - client, + { + books: [ + { + authorId: 1, + name: 'A Brief History of Time: From the Big Bang to Black Holes', + rank: 1, + categories: [Category.Novel, Category.ScienceFiction], + }, + ], + }, + client, ); const { 0: insertedBook } = await findBookById.run( - { id: insertedBookId }, - client, + { id: insertedBookId }, + client, ); - expect(insertedBook.categories).toEqual("{novel,science-fiction}"); -}) + expect(insertedBook.categories).toEqual('{novel,science-fiction}'); +}); test('update query with a non-null parameter override', async () => { - await updateBooks.run({ id: 2, rank: 12, name: 'Another title' }, client); -}) + await updateBooks.run({ id: 2, rank: 12, name: 'Another title' }, client); +}); test('insert query with an inline sql comment', async () => { - const [result] = await insertComment.run({ comments: [{ commentBody: "Just a comment", userId: 1}] }, client); - expect(result).toMatchSnapshot({ - id: expect.any(Number), - }); -}) + const [result] = await insertComment.run( + { comments: [{ commentBody: 'Just a comment', userId: 1 }] }, + client, + ); + expect(result).toMatchSnapshot({ + id: expect.any(Number), + }); +}); test('dynamic update query', async () => { - await updateBooksCustom.run({ id: 2, rank: 13 }, client); -}) + await updateBooksCustom.run({ id: 2, rank: 13 }, client); +}); test('update query with a multiple non-null parameter overrides', async () => { - await updateBooksRankNotNull.run({ id: 2, rank: 12, name: 'Another title' }, client); -}) + await updateBooksRankNotNull.run( + { id: 2, rank: 12, name: 'Another title' }, + client, + ); +}); test('select query with join and a parameter override', async () => { - const books = await getBooksByAuthorName.run( - { - authorName: 'Carl Sagan', - }, - client, - ); - expect(books).toMatchSnapshot(); -}) + const books = await getBooksByAuthorName.run( + { + authorName: 'Carl Sagan', + }, + client, + ); + expect(books).toMatchSnapshot(); +}); test('select query with aggregation', async () => { - const [aggregateData] = await aggregateEmailsAndTest.run( - { testAges: [35, 23, 19] }, - client, - ); - expect(aggregateData.agetest).toBe(true); + const [aggregateData] = await aggregateEmailsAndTest.run( + { testAges: [35, 23, 19] }, + client, + ); + expect(aggregateData.agetest).toBe(true); expect(aggregateData.emails).toEqual([ 'alex.doe@example.com', 'jane.holmes@example.com', 'andrewjackson@example.com', ]); -}) +}); test('insert query with an enum field', async () => { - await sendNotifications.run( + await sendNotifications.run( + { + notifications: [ { - notifications: [ - { - user_id: 2, - payload: { num_frogs: 82 }, - type: 'reminder', - }, - ], + user_id: 2, + payload: { num_frogs: 82 }, + type: 'reminder', }, - client, - ); + ], + }, + client, + ); }); test('multiple insert queries with an enum field', async () => { - await insertNotifications.run( + await insertNotifications.run( + { + params: [ { - params: [ - { - user_id: 1, - payload: { num_frogs: 1002 }, - type: 'reminder', - }, - ], - }, - client, - ); - await insertNotification.run( - { - notification: { user_id: 1, payload: { num_frogs: 1002 }, type: 'reminder', }, + ], + }, + client, + ); + await insertNotification.run( + { + notification: { + user_id: 1, + payload: { num_frogs: 1002 }, + type: 'reminder', }, - client, + }, + client, ); -}) - +}); test('select query with json fields and casts', async () => { - const notifications = await thresholdFrogs.run({ numFrogs: 80 }, client); - expect(notifications).toMatchSnapshot(); -}) + const notifications = await thresholdFrogs.run({ numFrogs: 80 }, client); + expect(notifications).toMatchSnapshot(); +}); test('select query nullability override on return field', async () => { - const result = await getBooks.run(undefined, client); - expect(result).toMatchSnapshot(); -}) + const result = await getBooks.run(undefined, client); + expect(result).toMatchSnapshot(); +}); test('select exists query, testing #472', async () => { - const result = await selectExistsTest.run(undefined, client); - expect(result).toMatchSnapshot(); -}) + const result = await selectExistsTest.run(undefined, client); + expect(result).toMatchSnapshot(); +}); test('select query with a bigint field', async () => { - const [row] = await countBooks.run(undefined, client); - expect(typeof row.book_count).toBe("bigint"); - expect(row.book_count).toBe(BigInt(4)); + const [row] = await countBooks.run(undefined, client); + expect(typeof row.book_count).toBe('bigint'); + expect(row.book_count).toBe(BigInt(4)); }); diff --git a/packages/example/src/notifications/notifications.queries.ts b/packages/example/src/notifications/notifications.queries.ts index e85ba10d..c0c8491a 100644 --- a/packages/example/src/notifications/notifications.queries.ts +++ b/packages/example/src/notifications/notifications.queries.ts @@ -39,6 +39,7 @@ export const sendNotifications = new PreparedQuery :date!"}; /** * Query generated from SQL: @@ -65,6 +66,7 @@ const getNotificationsIR: any = {"usedParamSet":{"userId":true},"params":[{"name * SELECT * * FROM notifications * WHERE user_id = :userId + * AND created_at > :date! * ``` */ export const getNotifications = new PreparedQuery(getNotificationsIR); diff --git a/packages/example/src/notifications/notifications.sql b/packages/example/src/notifications/notifications.sql index 7e392fb6..831ae3f0 100644 --- a/packages/example/src/notifications/notifications.sql +++ b/packages/example/src/notifications/notifications.sql @@ -8,7 +8,8 @@ VALUES :notifications RETURNING id as notification_id; /* @name GetNotifications */ SELECT * FROM notifications - WHERE user_id = :userId; + WHERE user_id = :userId + AND created_at > :date!; /*