Skip to content
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
67 changes: 67 additions & 0 deletions packages/cli/src/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,73 @@ export interface IGetNotificationsResult {
export interface IGetNotificationsQuery {
params: IGetNotificationsParams;
result: IGetNotificationsResult;
}\n\n`;
expect(result).toEqual(expected);
});

test(`Unnested ::text[] input parameters (${mode})`, async () => {
const queryStringSQL = `/* @name getNullStringUnnestedValues */
SELECT * FROM UNNEST(:nullStringUnnestedValues!::text[]);`;
const queryStringTS = `const getNullStringUnnestedValues = sql\`SELECT * FROM UNNEST($nullStringUnnestedValues::text[])\``;

const queryString =
mode === ProcessingMode.SQL ? queryStringSQL : queryStringTS;

const mockTypes: IQueryTypes = {
returnTypes: [
{
columnName: 'unnest',
returnName: 'unnest',
type: 'text',
},
],
paramMetadata: {
params: ['_text'],
mapping: [
{
name: 'nullStringUnnestedValues',
type: ParameterTransform.Scalar,
required: true,
assignedIndex: 1,
},
],
},
};

const typeSource = async (_: any) => mockTypes;
const types = new TypeAllocator(TypeMapping());

// Test out imports
types.use(
{ name: 'PreparedQuery', from: '@pgtyped/runtime' },
TypeScope.Return,
);

const result = await queryToTypeDeclarations(
parsedQuery(mode, queryString),
typeSource,
types,
{ hungarianNotation: true } as ParsedConfig,
);

const expectedTypes = `import { PreparedQuery } from '@pgtyped/runtime';

export type NullStringArray = (null | string)[];\n`;
expect(types.declaration('file.ts')).toEqual(expectedTypes);
const expected = `/** 'GetNullStringUnnestedValues' parameters type */
export interface IGetNullStringUnnestedValuesParams {
nullStringUnnestedValues: NullStringArray;
}

/** 'GetNullStringUnnestedValues' return type */
export interface IGetNullStringUnnestedValuesResult {
unnest: string | null;
}

/** 'GetNullStringUnnestedValues' query type */
export interface IGetNullStringUnnestedValuesQuery {
params: IGetNullStringUnnestedValuesParams;
result: IGetNullStringUnnestedValuesResult;
}\n\n`;
expect(result).toEqual(expected);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('TypeAllocator', () => {
// expect both to be included
expect(types.types).toMatchObject({
Json: expect.objectContaining({ name: 'Json' }),
JsonArray: expect.objectContaining({ name: 'JsonArray' }),
NullJsonArray: expect.objectContaining({ name: 'NullJsonArray' }),
});
});
});
7 changes: 5 additions & 2 deletions packages/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ const Json: Type = {
definition:
'null | boolean | number | string | Json[] | { [key: string]: Json }',
};

const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);

const getArray = (baseType: Type): Type => ({
name: `${baseType.name}Array`,
definition: `(${baseType.definition ?? baseType.name})[]`,
name: `Null${capitalize(baseType.name)}Array`,
definition: `(null | ${baseType.definition ?? baseType.name})[]`,
});

export const DefaultTypeMapping = Object.freeze({
Expand Down
Loading