From 3c8b48f597d64b16e46632270e58416102cc290a Mon Sep 17 00:00:00 2001 From: Yasiru Kassapa Date: Mon, 27 Nov 2023 16:08:18 +0100 Subject: [PATCH 1/3] Add example schema with interval types. --- packages/example/sql/schema.sql | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/example/sql/schema.sql b/packages/example/sql/schema.sql index ddfa9ceb..96ba87e7 100644 --- a/packages/example/sql/schema.sql +++ b/packages/example/sql/schema.sql @@ -1,3 +1,4 @@ +BEGIN; CREATE TABLE users ( id SERIAL PRIMARY KEY, email TEXT NOT NULL, @@ -35,6 +36,12 @@ CREATE TABLE books ( categories category[] ); +CREATE TABLE audio_books ( + id SERIAL PRIMARY KEY, + text_book_id INTEGER REFERENCES books, + duration INTERVAL NOT NULL +); + CREATE TABLE book_comments ( id SERIAL PRIMARY KEY, user_id INTEGER REFERENCES users, @@ -42,6 +49,8 @@ CREATE TABLE book_comments ( body TEXT ); + + INSERT INTO users (email, user_name, first_name, last_name, age) VALUES ('alex.doe@example.com', 'alexd', 'Alex', 'Doe', 35), ('jane.holmes@example.com', 'jane67', 'Jane', 'Holmes', 23), @@ -78,6 +87,12 @@ INSERT INTO book_comments (user_id, book_id, body) VALUES (1, 1, 'Fantastic read, recommend it!'), (1, 2, 'Did not like it, expected much more...'); +INSERT INTO audio_books (text_book_id, duration) +VALUES (1, '01:23:45'), + (2, '02:34:56'), + (3, '03:45:57'), + (4, '04:56:48'); + CREATE TYPE "Iso31661Alpha2" AS ENUM ( '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', @@ -101,3 +116,5 @@ CREATE TABLE book_country ( INSERT INTO book_country (country) VALUES ('CZ'), ('DE'); + +COMMIT; \ No newline at end of file From acd1efaa3713e061090ef596744ca30492d9b2ae Mon Sep 17 00:00:00 2001 From: Yasiru Kassapa Date: Mon, 27 Nov 2023 16:08:36 +0100 Subject: [PATCH 2/3] Create example query with interval. --- .../src/audio_books/audio_books.queries.ts | 30 +++++++++++++++++++ .../example/src/audio_books/audio_books.sql | 2 ++ 2 files changed, 32 insertions(+) create mode 100644 packages/example/src/audio_books/audio_books.queries.ts create mode 100644 packages/example/src/audio_books/audio_books.sql diff --git a/packages/example/src/audio_books/audio_books.queries.ts b/packages/example/src/audio_books/audio_books.queries.ts new file mode 100644 index 00000000..2424b5e7 --- /dev/null +++ b/packages/example/src/audio_books/audio_books.queries.ts @@ -0,0 +1,30 @@ +/** Types generated for queries found in "src/audio_books/audio_books.sql" */ +import { PreparedQuery } from '@pgtyped/runtime'; + +/** 'GetAudioBooks' parameters type */ +export type IGetAudioBooksParams = void; + +/** 'GetAudioBooks' return type */ +export interface IGetAudioBooksResult { + duration: string; + id: number; + text_book_id: number | null; +} + +/** 'GetAudioBooks' query type */ +export interface IGetAudioBooksQuery { + params: IGetAudioBooksParams; + result: IGetAudioBooksResult; +} + +const getAudioBooksIR: any = {"usedParamSet":{},"params":[],"statement":"select * from audio_books"}; + +/** + * Query generated from SQL: + * ``` + * select * from audio_books + * ``` + */ +export const getAudioBooks = new PreparedQuery(getAudioBooksIR); + + diff --git a/packages/example/src/audio_books/audio_books.sql b/packages/example/src/audio_books/audio_books.sql new file mode 100644 index 00000000..b98bdb55 --- /dev/null +++ b/packages/example/src/audio_books/audio_books.sql @@ -0,0 +1,2 @@ +/* @name getAudioBooks */ +select * from audio_books; \ No newline at end of file From 99fec1f6aa6dec438a8019fc68b32263222a2587 Mon Sep 17 00:00:00 2001 From: Yasiru Kassapa Date: Mon, 27 Nov 2023 16:08:52 +0100 Subject: [PATCH 3/3] Add test for interval column type. --- .../src/__snapshots__/index.test.ts.snap | 25 +++++++++++++++++++ packages/example/src/index.test.ts | 6 ++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/example/src/__snapshots__/index.test.ts.snap b/packages/example/src/__snapshots__/index.test.ts.snap index f98c6f6d..c5d8a3f4 100644 --- a/packages/example/src/__snapshots__/index.test.ts.snap +++ b/packages/example/src/__snapshots__/index.test.ts.snap @@ -47,6 +47,31 @@ Array [ ] `; +exports[`select query with interval field 1`] = ` +Array [ + Object { + "duration": "0 years 0 mons 0 days 1 hours 23 mins 45.0 secs", + "id": 1, + "text_book_id": 1, + }, + Object { + "duration": "0 years 0 mons 0 days 2 hours 34 mins 56.0 secs", + "id": 2, + "text_book_id": 2, + }, + Object { + "duration": "0 years 0 mons 0 days 3 hours 45 mins 57.0 secs", + "id": 3, + "text_book_id": 3, + }, + Object { + "duration": "0 years 0 mons 0 days 4 hours 56 mins 48.0 secs", + "id": 4, + "text_book_id": 4, + }, +] +`; + exports[`select query with join and a parameter override 1`] = ` Array [ Object { diff --git a/packages/example/src/index.test.ts b/packages/example/src/index.test.ts index dd6629cd..01a5b6de 100644 --- a/packages/example/src/index.test.ts +++ b/packages/example/src/index.test.ts @@ -30,6 +30,7 @@ import { import { getUsersWithComment } from './users/sample.js'; import { Category } from './customTypes.js'; import { sql } from './sql/index.js' +import { getAudioBooks } from "./audio_books/audio_books.queries.js"; const { Client } = pg; @@ -232,7 +233,10 @@ test('select query with a bigint field', async () => { expect(row.book_count).toBe(BigInt(4)); }); - +test('select query with interval field', async () => { + const rows = await getAudioBooks.run(undefined, client); + expect(rows).toMatchSnapshot(); +}) test('ts-implicit mode query', async () => { const books = await sql(`SELECT * FROM books WHERE id = $id`).run({id: 1}, client); expect(books).toMatchSnapshot();