From 46f83ff333fd2beb678c6f5894490e9337a46fb2 Mon Sep 17 00:00:00 2001 From: Logan Glasson Date: Wed, 1 Nov 2023 16:25:58 +1300 Subject: [PATCH] feat: include query name in `PreparedQuery` --- packages/cli/src/generator.ts | 2 +- packages/example/src/books/books.queries.ts | 26 +++++++++---------- .../example/src/comments/comments.queries.ts | 8 +++--- .../notifications/notifications.queries.ts | 6 ++--- packages/runtime/src/tag.ts | 4 ++- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/packages/cli/src/generator.ts b/packages/cli/src/generator.ts index 45a3efb5..bb817171 100644 --- a/packages/cli/src/generator.ts +++ b/packages/cli/src/generator.ts @@ -398,7 +398,7 @@ export function generateDeclarations(typeDecs: TypedQuery[]): string { typeDeclarations += `export const ${typeDec.query.name} = ` + `new PreparedQuery<${typeDec.query.paramTypeAlias},${typeDec.query.returnTypeAlias}>` + - `(${typeDec.query.name}IR);\n\n\n`; + `(${JSON.stringify(typeDec.query.name)},${typeDec.query.name}IR);\n\n\n`; } return typeDeclarations; } diff --git a/packages/example/src/books/books.queries.ts b/packages/example/src/books/books.queries.ts index c3a675ea..a6c0d840 100644 --- a/packages/example/src/books/books.queries.ts +++ b/packages/example/src/books/books.queries.ts @@ -41,7 +41,7 @@ const findBookByIdIR: any = {"usedParamSet":{"id":true},"params":[{"name":"id"," * SELECT * FROM books WHERE id = :id * ``` */ -export const findBookById = new PreparedQuery(findBookByIdIR); +export const findBookById = new PreparedQuery("findBookById",findBookByIdIR); /** 'FindBookByCategory' parameters type */ @@ -72,7 +72,7 @@ const findBookByCategoryIR: any = {"usedParamSet":{"category":true},"params":[{" * SELECT * FROM books WHERE :category = ANY(categories) * ``` */ -export const findBookByCategory = new PreparedQuery(findBookByCategoryIR); +export const findBookByCategory = new PreparedQuery("findBookByCategory",findBookByCategoryIR); /** 'FindBookNameOrRank' parameters type */ @@ -103,7 +103,7 @@ const findBookNameOrRankIR: any = {"usedParamSet":{"name":true,"rank":true},"par * WHERE (name = :name OR rank = :rank) * ``` */ -export const findBookNameOrRank = new PreparedQuery(findBookNameOrRankIR); +export const findBookNameOrRank = new PreparedQuery("findBookNameOrRank",findBookNameOrRankIR); /** 'FindBookUnicode' parameters type */ @@ -132,7 +132,7 @@ const findBookUnicodeIR: any = {"usedParamSet":{},"params":[],"statement":"SELEC * SELECT * FROM books WHERE name = 'שקל' * ``` */ -export const findBookUnicode = new PreparedQuery(findBookUnicodeIR); +export const findBookUnicode = new PreparedQuery("findBookUnicode",findBookUnicodeIR); /** 'InsertBooks' parameters type */ @@ -165,7 +165,7 @@ const insertBooksIR: any = {"usedParamSet":{"books":true},"params":[{"name":"boo * VALUES :books RETURNING id as book_id * ``` */ -export const insertBooks = new PreparedQuery(insertBooksIR); +export const insertBooks = new PreparedQuery("insertBooks",insertBooksIR); /** 'UpdateBooksCustom' parameters type */ @@ -199,7 +199,7 @@ const updateBooksCustomIR: any = {"usedParamSet":{"rank":true,"id":true},"params * WHERE id = :id! * ``` */ -export const updateBooksCustom = new PreparedQuery(updateBooksCustomIR); +export const updateBooksCustom = new PreparedQuery("updateBooksCustom",updateBooksCustomIR); /** 'UpdateBooks' parameters type */ @@ -231,7 +231,7 @@ const updateBooksIR: any = {"usedParamSet":{"name":true,"rank":true,"id":true}," * WHERE id = :id! * ``` */ -export const updateBooks = new PreparedQuery(updateBooksIR); +export const updateBooks = new PreparedQuery("updateBooks",updateBooksIR); /** 'UpdateBooksRankNotNull' parameters type */ @@ -262,7 +262,7 @@ const updateBooksRankNotNullIR: any = {"usedParamSet":{"rank":true,"name":true," * WHERE id = :id! * ``` */ -export const updateBooksRankNotNull = new PreparedQuery(updateBooksRankNotNullIR); +export const updateBooksRankNotNull = new PreparedQuery("updateBooksRankNotNull",updateBooksRankNotNullIR); /** 'GetBooksByAuthorName' parameters type */ @@ -295,7 +295,7 @@ const getBooksByAuthorNameIR: any = {"usedParamSet":{"authorName":true},"params" * WHERE a.first_name || ' ' || a.last_name = :authorName! * ``` */ -export const getBooksByAuthorName = new PreparedQuery(getBooksByAuthorNameIR); +export const getBooksByAuthorName = new PreparedQuery("getBooksByAuthorName",getBooksByAuthorNameIR); /** 'AggregateEmailsAndTest' parameters type */ @@ -323,7 +323,7 @@ const aggregateEmailsAndTestIR: any = {"usedParamSet":{"testAges":true},"params" * SELECT array_agg(email) as "emails!", array_agg(age) = :testAges as ageTest FROM users * ``` */ -export const aggregateEmailsAndTest = new PreparedQuery(aggregateEmailsAndTestIR); +export const aggregateEmailsAndTest = new PreparedQuery("aggregateEmailsAndTest",aggregateEmailsAndTestIR); /** 'GetBooks' parameters type */ @@ -349,7 +349,7 @@ const getBooksIR: any = {"usedParamSet":{},"params":[],"statement":"SELECT id, n * SELECT id, name as "name!" FROM books * ``` */ -export const getBooks = new PreparedQuery(getBooksIR); +export const getBooks = new PreparedQuery("getBooks",getBooksIR); /** 'CountBooks' parameters type */ @@ -374,7 +374,7 @@ const countBooksIR: any = {"usedParamSet":{},"params":[],"statement":"SELECT cou * SELECT count(*) as book_count FROM books * ``` */ -export const countBooks = new PreparedQuery(countBooksIR); +export const countBooks = new PreparedQuery("countBooks",countBooksIR); /** 'GetBookCountries' parameters type */ @@ -400,6 +400,6 @@ const getBookCountriesIR: any = {"usedParamSet":{},"params":[],"statement":"SELE * SELECT * FROM book_country * ``` */ -export const getBookCountries = new PreparedQuery(getBookCountriesIR); +export const getBookCountries = new PreparedQuery("getBookCountries",getBookCountriesIR); diff --git a/packages/example/src/comments/comments.queries.ts b/packages/example/src/comments/comments.queries.ts index 3ded9279..97a8bf0c 100644 --- a/packages/example/src/comments/comments.queries.ts +++ b/packages/example/src/comments/comments.queries.ts @@ -28,7 +28,7 @@ const getAllCommentsIR: any = {"usedParamSet":{"id":true},"params":[{"name":"id" * SELECT * FROM book_comments WHERE id = :id! OR user_id = :id * ``` */ -export const getAllComments = new PreparedQuery(getAllCommentsIR); +export const getAllComments = new PreparedQuery("getAllComments",getAllCommentsIR); /** 'GetAllCommentsByIds' parameters type */ @@ -58,7 +58,7 @@ const getAllCommentsByIdsIR: any = {"usedParamSet":{"ids":true},"params":[{"name * SELECT * FROM book_comments WHERE id in :ids AND id in :ids! * ``` */ -export const getAllCommentsByIds = new PreparedQuery(getAllCommentsByIdsIR); +export const getAllCommentsByIds = new PreparedQuery("getAllCommentsByIds",getAllCommentsByIdsIR); /** 'InsertComment' parameters type */ @@ -93,7 +93,7 @@ const insertCommentIR: any = {"usedParamSet":{"comments":true},"params":[{"name" * VALUES :comments RETURNING * * ``` */ -export const insertComment = new PreparedQuery(insertCommentIR); +export const insertComment = new PreparedQuery("insertComment",insertCommentIR); /** 'SelectExistsTest' parameters type */ @@ -118,6 +118,6 @@ const selectExistsTestIR: any = {"usedParamSet":{},"params":[],"statement":"SELE * SELECT EXISTS ( SELECT 1 WHERE true ) AS "isTransactionExists" * ``` */ -export const selectExistsTest = new PreparedQuery(selectExistsTestIR); +export const selectExistsTest = new PreparedQuery("selectExistsTest",selectExistsTestIR); diff --git a/packages/example/src/notifications/notifications.queries.ts b/packages/example/src/notifications/notifications.queries.ts index c7d3246e..3aaab6f4 100644 --- a/packages/example/src/notifications/notifications.queries.ts +++ b/packages/example/src/notifications/notifications.queries.ts @@ -36,7 +36,7 @@ const sendNotificationsIR: any = {"usedParamSet":{"notifications":true},"params" * VALUES :notifications RETURNING id as notification_id * ``` */ -export const sendNotifications = new PreparedQuery(sendNotificationsIR); +export const sendNotifications = new PreparedQuery("sendNotifications",sendNotificationsIR); /** 'GetNotifications' parameters type */ @@ -71,7 +71,7 @@ const getNotificationsIR: any = {"usedParamSet":{"userId":true,"date":true},"par * AND created_at > :date! * ``` */ -export const getNotifications = new PreparedQuery(getNotificationsIR); +export const getNotifications = new PreparedQuery("getNotifications",getNotificationsIR); /** 'ThresholdFrogs' parameters type */ @@ -103,6 +103,6 @@ const thresholdFrogsIR: any = {"usedParamSet":{"numFrogs":true},"params":[{"name * WHERE CAST (n.payload->'num_frogs' AS int) > :numFrogs! * ``` */ -export const thresholdFrogs = new PreparedQuery(thresholdFrogsIR); +export const thresholdFrogs = new PreparedQuery("thresholdFrogs",thresholdFrogsIR); diff --git a/packages/runtime/src/tag.ts b/packages/runtime/src/tag.ts index 7f242bc8..0924ab54 100644 --- a/packages/runtime/src/tag.ts +++ b/packages/runtime/src/tag.ts @@ -65,10 +65,12 @@ export class PreparedQuery { params: TParamType, dbConnection: IDatabaseConnection, ) => Promise>; + public readonly queryName: string; private readonly queryIR: SQLQueryIR; - constructor(queryIR: SQLQueryIR) { + constructor(queryName: string, queryIR: SQLQueryIR) { + this.queryName = queryName; this.queryIR = queryIR; this.run = async (params, connection) => { const { query: processedQuery, bindings } = processSQLQueryIR(