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 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/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 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();