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(`Columns with Buffer type`, async () => {
const queryStringSQL = `
/* @name GetFiles */
SELECT file_data FROM uploads WHERE id = :id;
`;
const queryStringTS = `
const GetFiles = sql\`SELECT file_data FROM uploads WHERE id = $id\`;
`;
const queryString =
mode === ProcessingMode.SQL ? queryStringSQL : queryStringTS;
const mockTypes: IQueryTypes = {
returnTypes: [
{
returnName: 'file_data',
columnName: 'file_data',
type: 'bytea',
nullable: false,
},
],
paramMetadata: {
params: ['uuid'],
mapping: [
{
name: 'id',
type: ParameterTransform.Scalar,
required: false,
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,
partialConfig,
);
const expectedTypes = `import { PreparedQuery } from '@pgtyped/runtime';

import type { Buffer } from 'node:buffer';\n`;

expect(types.declaration('file.ts')).toEqual(expectedTypes);
const expected = `/** 'GetFiles' parameters type */
export interface IGetFilesParams {
id?: string | null | void;
}

/** 'GetFiles' return type */
export interface IGetFilesResult {
file_data: Buffer;
}

/** 'GetFiles' query type */
export interface IGetFilesQuery {
params: IGetFilesParams;
result: IGetFilesResult;
}\n\n`;
expect(result).toEqual(expected);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const DateOrString: Type = {
name: 'DateOrString',
definition: 'Date | string',
};
const Bytes: Type = { name: 'Buffer' };
const Bytes: Type = { name: 'Buffer', from: 'node:buffer' };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fortunately, this whole PR is just a single line (everything else is tests)

const Void: Type = { name: 'undefined' };
const Json: Type = {
name: 'Json',
Expand Down