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

Include query name in PreparedQuery #580

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion packages/cli/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
26 changes: 13 additions & 13 deletions packages/example/src/books/books.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const findBookByIdIR: any = {"usedParamSet":{"id":true},"params":[{"name":"id","
* SELECT * FROM books WHERE id = :id
* ```
*/
export const findBookById = new PreparedQuery<IFindBookByIdParams,IFindBookByIdResult>(findBookByIdIR);
export const findBookById = new PreparedQuery<IFindBookByIdParams,IFindBookByIdResult>("findBookById",findBookByIdIR);


/** 'FindBookByCategory' parameters type */
Expand Down Expand Up @@ -72,7 +72,7 @@ const findBookByCategoryIR: any = {"usedParamSet":{"category":true},"params":[{"
* SELECT * FROM books WHERE :category = ANY(categories)
* ```
*/
export const findBookByCategory = new PreparedQuery<IFindBookByCategoryParams,IFindBookByCategoryResult>(findBookByCategoryIR);
export const findBookByCategory = new PreparedQuery<IFindBookByCategoryParams,IFindBookByCategoryResult>("findBookByCategory",findBookByCategoryIR);


/** 'FindBookNameOrRank' parameters type */
Expand Down Expand Up @@ -103,7 +103,7 @@ const findBookNameOrRankIR: any = {"usedParamSet":{"name":true,"rank":true},"par
* WHERE (name = :name OR rank = :rank)
* ```
*/
export const findBookNameOrRank = new PreparedQuery<IFindBookNameOrRankParams,IFindBookNameOrRankResult>(findBookNameOrRankIR);
export const findBookNameOrRank = new PreparedQuery<IFindBookNameOrRankParams,IFindBookNameOrRankResult>("findBookNameOrRank",findBookNameOrRankIR);


/** 'FindBookUnicode' parameters type */
Expand Down Expand Up @@ -132,7 +132,7 @@ const findBookUnicodeIR: any = {"usedParamSet":{},"params":[],"statement":"SELEC
* SELECT * FROM books WHERE name = 'שקל'
* ```
*/
export const findBookUnicode = new PreparedQuery<IFindBookUnicodeParams,IFindBookUnicodeResult>(findBookUnicodeIR);
export const findBookUnicode = new PreparedQuery<IFindBookUnicodeParams,IFindBookUnicodeResult>("findBookUnicode",findBookUnicodeIR);


/** 'InsertBooks' parameters type */
Expand Down Expand Up @@ -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<IInsertBooksParams,IInsertBooksResult>(insertBooksIR);
export const insertBooks = new PreparedQuery<IInsertBooksParams,IInsertBooksResult>("insertBooks",insertBooksIR);


/** 'UpdateBooksCustom' parameters type */
Expand Down Expand Up @@ -199,7 +199,7 @@ const updateBooksCustomIR: any = {"usedParamSet":{"rank":true,"id":true},"params
* WHERE id = :id!
* ```
*/
export const updateBooksCustom = new PreparedQuery<IUpdateBooksCustomParams,IUpdateBooksCustomResult>(updateBooksCustomIR);
export const updateBooksCustom = new PreparedQuery<IUpdateBooksCustomParams,IUpdateBooksCustomResult>("updateBooksCustom",updateBooksCustomIR);


/** 'UpdateBooks' parameters type */
Expand Down Expand Up @@ -231,7 +231,7 @@ const updateBooksIR: any = {"usedParamSet":{"name":true,"rank":true,"id":true},"
* WHERE id = :id!
* ```
*/
export const updateBooks = new PreparedQuery<IUpdateBooksParams,IUpdateBooksResult>(updateBooksIR);
export const updateBooks = new PreparedQuery<IUpdateBooksParams,IUpdateBooksResult>("updateBooks",updateBooksIR);


/** 'UpdateBooksRankNotNull' parameters type */
Expand Down Expand Up @@ -262,7 +262,7 @@ const updateBooksRankNotNullIR: any = {"usedParamSet":{"rank":true,"name":true,"
* WHERE id = :id!
* ```
*/
export const updateBooksRankNotNull = new PreparedQuery<IUpdateBooksRankNotNullParams,IUpdateBooksRankNotNullResult>(updateBooksRankNotNullIR);
export const updateBooksRankNotNull = new PreparedQuery<IUpdateBooksRankNotNullParams,IUpdateBooksRankNotNullResult>("updateBooksRankNotNull",updateBooksRankNotNullIR);


/** 'GetBooksByAuthorName' parameters type */
Expand Down Expand Up @@ -295,7 +295,7 @@ const getBooksByAuthorNameIR: any = {"usedParamSet":{"authorName":true},"params"
* WHERE a.first_name || ' ' || a.last_name = :authorName!
* ```
*/
export const getBooksByAuthorName = new PreparedQuery<IGetBooksByAuthorNameParams,IGetBooksByAuthorNameResult>(getBooksByAuthorNameIR);
export const getBooksByAuthorName = new PreparedQuery<IGetBooksByAuthorNameParams,IGetBooksByAuthorNameResult>("getBooksByAuthorName",getBooksByAuthorNameIR);


/** 'AggregateEmailsAndTest' parameters type */
Expand Down Expand Up @@ -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<IAggregateEmailsAndTestParams,IAggregateEmailsAndTestResult>(aggregateEmailsAndTestIR);
export const aggregateEmailsAndTest = new PreparedQuery<IAggregateEmailsAndTestParams,IAggregateEmailsAndTestResult>("aggregateEmailsAndTest",aggregateEmailsAndTestIR);


/** 'GetBooks' parameters type */
Expand All @@ -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<IGetBooksParams,IGetBooksResult>(getBooksIR);
export const getBooks = new PreparedQuery<IGetBooksParams,IGetBooksResult>("getBooks",getBooksIR);


/** 'CountBooks' parameters type */
Expand All @@ -374,7 +374,7 @@ const countBooksIR: any = {"usedParamSet":{},"params":[],"statement":"SELECT cou
* SELECT count(*) as book_count FROM books
* ```
*/
export const countBooks = new PreparedQuery<ICountBooksParams,ICountBooksResult>(countBooksIR);
export const countBooks = new PreparedQuery<ICountBooksParams,ICountBooksResult>("countBooks",countBooksIR);


/** 'GetBookCountries' parameters type */
Expand All @@ -400,6 +400,6 @@ const getBookCountriesIR: any = {"usedParamSet":{},"params":[],"statement":"SELE
* SELECT * FROM book_country
* ```
*/
export const getBookCountries = new PreparedQuery<IGetBookCountriesParams,IGetBookCountriesResult>(getBookCountriesIR);
export const getBookCountries = new PreparedQuery<IGetBookCountriesParams,IGetBookCountriesResult>("getBookCountries",getBookCountriesIR);


8 changes: 4 additions & 4 deletions packages/example/src/comments/comments.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IGetAllCommentsParams,IGetAllCommentsResult>(getAllCommentsIR);
export const getAllComments = new PreparedQuery<IGetAllCommentsParams,IGetAllCommentsResult>("getAllComments",getAllCommentsIR);


/** 'GetAllCommentsByIds' parameters type */
Expand Down Expand Up @@ -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<IGetAllCommentsByIdsParams,IGetAllCommentsByIdsResult>(getAllCommentsByIdsIR);
export const getAllCommentsByIds = new PreparedQuery<IGetAllCommentsByIdsParams,IGetAllCommentsByIdsResult>("getAllCommentsByIds",getAllCommentsByIdsIR);


/** 'InsertComment' parameters type */
Expand Down Expand Up @@ -93,7 +93,7 @@ const insertCommentIR: any = {"usedParamSet":{"comments":true},"params":[{"name"
* VALUES :comments RETURNING *
* ```
*/
export const insertComment = new PreparedQuery<IInsertCommentParams,IInsertCommentResult>(insertCommentIR);
export const insertComment = new PreparedQuery<IInsertCommentParams,IInsertCommentResult>("insertComment",insertCommentIR);


/** 'SelectExistsTest' parameters type */
Expand All @@ -118,6 +118,6 @@ const selectExistsTestIR: any = {"usedParamSet":{},"params":[],"statement":"SELE
* SELECT EXISTS ( SELECT 1 WHERE true ) AS "isTransactionExists"
* ```
*/
export const selectExistsTest = new PreparedQuery<ISelectExistsTestParams,ISelectExistsTestResult>(selectExistsTestIR);
export const selectExistsTest = new PreparedQuery<ISelectExistsTestParams,ISelectExistsTestResult>("selectExistsTest",selectExistsTestIR);


6 changes: 3 additions & 3 deletions packages/example/src/notifications/notifications.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const sendNotificationsIR: any = {"usedParamSet":{"notifications":true},"params"
* VALUES :notifications RETURNING id as notification_id
* ```
*/
export const sendNotifications = new PreparedQuery<ISendNotificationsParams,ISendNotificationsResult>(sendNotificationsIR);
export const sendNotifications = new PreparedQuery<ISendNotificationsParams,ISendNotificationsResult>("sendNotifications",sendNotificationsIR);


/** 'GetNotifications' parameters type */
Expand Down Expand Up @@ -71,7 +71,7 @@ const getNotificationsIR: any = {"usedParamSet":{"userId":true,"date":true},"par
* AND created_at > :date!
* ```
*/
export const getNotifications = new PreparedQuery<IGetNotificationsParams,IGetNotificationsResult>(getNotificationsIR);
export const getNotifications = new PreparedQuery<IGetNotificationsParams,IGetNotificationsResult>("getNotifications",getNotificationsIR);


/** 'ThresholdFrogs' parameters type */
Expand Down Expand Up @@ -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<IThresholdFrogsParams,IThresholdFrogsResult>(thresholdFrogsIR);
export const thresholdFrogs = new PreparedQuery<IThresholdFrogsParams,IThresholdFrogsResult>("thresholdFrogs",thresholdFrogsIR);


4 changes: 3 additions & 1 deletion packages/runtime/src/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class PreparedQuery<TParamType, TResultType> {
params: TParamType,
dbConnection: IDatabaseConnection,
) => Promise<Array<TResultType>>;
public readonly queryName: string;

public stream: (
params: TParamType,
Expand All @@ -102,7 +103,8 @@ export class PreparedQuery<TParamType, TResultType> {

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(
Expand Down
Loading