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

feat!: make nullable scalar parameters optional #482

Merged
merged 1 commit into from
Jan 28, 2023
Merged
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
14 changes: 7 additions & 7 deletions packages/cli/src/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export type Json = null | boolean | number | string | Json[] | { [key: string]:
expect(types.declaration()).toEqual(expectedTypes);
const expected = `/** 'GetNotifications' parameters type */
export interface IGetNotificationsParams {
userId: string | null | void;
userId?: string | null | void;
}

/** 'GetNotifications' return type */
Expand Down Expand Up @@ -236,9 +236,9 @@ export interface IInsertNotificationsQuery {
);
const expected = `/** 'DeleteUsers' parameters type */
export interface IDeleteUsersParams {
userId: string | null | void;
userName: string | null | void;
userNote: string | null | void;
userId?: string | null | void;
userName?: string | null | void;
userNote?: string | null | void;
}

/** 'DeleteUsers' return type */
Expand Down Expand Up @@ -314,7 +314,7 @@ export type Json = null | boolean | number | string | Json[] | { [key: string]:
expect(types.declaration()).toEqual(expectedTypes);
const expected = `/** 'GetNotifications' parameters type */
export interface IGetNotificationsParams {
userId: string | null | void;
userId?: string | null | void;
}

/** 'GetNotifications' return type */
Expand Down Expand Up @@ -462,7 +462,7 @@ export type Json = null | boolean | number | string | Json[] | { [key: string]:
expect(types.declaration()).toEqual(expectedTypes);
const expected = `/** 'GetNotifications' parameters type */
export interface IGetNotificationsParams {
userId: string | null | void;
userId?: string | null | void;
}

/** 'GetNotifications' return type */
Expand Down Expand Up @@ -534,7 +534,7 @@ export type Json = null | boolean | number | string | Json[] | { [key: string]:
expect(types.declaration()).toEqual(expectedTypes);
const expected = `/** 'GetNotifications' parameters type */
export interface IGetNotificationsParams {
userId: string | null | void;
userId?: string | null | void;
}

/** 'GetNotifications' return type */
Expand Down
10 changes: 8 additions & 2 deletions packages/cli/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export enum ProcessingMode {
}

export interface IField {
optional?: boolean;
fieldName: string;
fieldType: string;
comment?: string;
Expand All @@ -47,9 +48,9 @@ export const generateInterface = (interfaceName: string, fields: IField[]) => {
.sort((a, b) => a.fieldName.localeCompare(b.fieldName));
const contents = sortedFields
.map(
({ fieldName, fieldType, comment }) =>
({ fieldName, fieldType, comment, optional }) =>
(comment ? ` /** ${escapeComment(comment)} */\n` : '') +
` ${fieldName}: ${fieldType};`,
` ${fieldName}${optional ? '?' : ''}: ${fieldType};`,
)
.join('\n');
return interfaceGen(interfaceName, contents);
Expand Down Expand Up @@ -153,7 +154,12 @@ export async function queryToTypeDeclarations(
tsTypeName += ' | null | void';
}

// Allow optional scalar parameters to be missing from parameters object
const optional =
param.type === ParameterTransform.Scalar && !param.required;

paramFieldTypes.push({
optional,
fieldName: param.name,
fieldType: isArray ? `readonly (${tsTypeName})[]` : tsTypeName,
});
Expand Down
9 changes: 9 additions & 0 deletions packages/example/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`select query with dynamic or 1`] = `
Array [
Object {
"id": 1,
"name": "Black Swan",
},
]
`;

exports[`select query with join and a parameter override 1`] = `
Array [
Object {
Expand Down
43 changes: 37 additions & 6 deletions packages/example/src/books/books.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type stringArray = (string)[];

/** 'FindBookById' parameters type */
export interface IFindBookByIdParams {
id: number | null | void;
id?: number | null | void;
}

/** 'FindBookById' return type */
Expand Down Expand Up @@ -40,6 +40,37 @@ const findBookByIdIR: any = {"usedParamSet":{"id":true},"params":[{"name":"id","
export const findBookById = new PreparedQuery<IFindBookByIdParams,IFindBookByIdResult>(findBookByIdIR);


/** 'FindBookNameOrRank' parameters type */
export interface IFindBookNameOrRankParams {
name?: string | null | void;
rank?: number | null | void;
}

/** 'FindBookNameOrRank' return type */
export interface IFindBookNameOrRankResult {
id: number;
name: string | null;
}

/** 'FindBookNameOrRank' query type */
export interface IFindBookNameOrRankQuery {
params: IFindBookNameOrRankParams;
result: IFindBookNameOrRankResult;
}

const findBookNameOrRankIR: any = {"usedParamSet":{"name":true,"rank":true},"params":[{"name":"name","required":false,"transform":{"type":"scalar"},"locs":[{"a":41,"b":45}]},{"name":"rank","required":false,"transform":{"type":"scalar"},"locs":[{"a":57,"b":61}]}],"statement":"SELECT id, name\nFROM books\nWHERE (name = :name OR rank = :rank)"};

/**
* Query generated from SQL:
* ```
* SELECT id, name
* FROM books
* WHERE (name = :name OR rank = :rank)
* ```
*/
export const findBookNameOrRank = new PreparedQuery<IFindBookNameOrRankParams,IFindBookNameOrRankResult>(findBookNameOrRankIR);


/** 'FindBookUnicode' parameters type */
export type IFindBookUnicodeParams = void;

Expand Down Expand Up @@ -105,7 +136,7 @@ export const insertBooks = new PreparedQuery<IInsertBooksParams,IInsertBooksResu
/** 'UpdateBooksCustom' parameters type */
export interface IUpdateBooksCustomParams {
id: number;
rank: number | null | void;
rank?: number | null | void;
}

/** 'UpdateBooksCustom' return type */
Expand Down Expand Up @@ -139,8 +170,8 @@ export const updateBooksCustom = new PreparedQuery<IUpdateBooksCustomParams,IUpd
/** 'UpdateBooks' parameters type */
export interface IUpdateBooksParams {
id: number;
name: string | null | void;
rank: number | null | void;
name?: string | null | void;
rank?: number | null | void;
}

/** 'UpdateBooks' return type */
Expand Down Expand Up @@ -171,7 +202,7 @@ export const updateBooks = new PreparedQuery<IUpdateBooksParams,IUpdateBooksResu
/** 'UpdateBooksRankNotNull' parameters type */
export interface IUpdateBooksRankNotNullParams {
id: number;
name: string | null | void;
name?: string | null | void;
rank: number;
}

Expand Down Expand Up @@ -234,7 +265,7 @@ export const getBooksByAuthorName = new PreparedQuery<IGetBooksByAuthorNameParam

/** 'AggregateEmailsAndTest' parameters type */
export interface IAggregateEmailsAndTestParams {
testAges: numberArray | null | void;
testAges?: numberArray | null | void;
}

/** 'AggregateEmailsAndTest' return type */
Expand Down
5 changes: 5 additions & 0 deletions packages/example/src/books/books.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/* @name FindBookById */
SELECT * FROM books WHERE id = :id;

/* @name FindBookNameOrRank */
SELECT id, name
FROM books
WHERE (name = :name OR rank = :rank);

/* @name FindBookUnicode */
SELECT * FROM books WHERE name = 'שקל';

Expand Down
24 changes: 16 additions & 8 deletions packages/example/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import {test, expect, afterEach, beforeEach, beforeAll, describe} from '@jest/gl
import pg from 'pg';
const {Client} = pg;
import {
aggregateEmailsAndTest,
findBookUnicode,
findBookById,
getBooksByAuthorName,
insertBooks,
updateBooks,
updateBooksCustom,
updateBooksRankNotNull,
aggregateEmailsAndTest,
findBookUnicode,
findBookById,
getBooksByAuthorName,
insertBooks,
updateBooks,
updateBooksCustom,
updateBooksRankNotNull,
findBookNameOrRank,
} from './books/books.queries.js';
import { getAllComments } from './comments/comments.queries.js';
import {
Expand Down Expand Up @@ -63,6 +64,13 @@ test('select query with parameters', async () => {
expect(comments).toMatchSnapshot();
})

test('select query with dynamic or', () => {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const sendNotifications = new PreparedQuery<ISendNotificationsParams,ISen

/** 'GetNotifications' parameters type */
export interface IGetNotificationsParams {
userId: number | null | void;
userId?: number | null | void;
}

/** 'GetNotifications' return type */
Expand Down