Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Type for TypeORM Raw Entity: CamelToSnake / ValueOf Exact Matching / Property-Method inference

$
0
0

Summary: Generating Types for Raw Return Types in TypeORM

Hello, I'm working with TypeORM and have a question about handling raw return types. When using getRawOne and getRawMany functions, the return includes strings composed of class names and properties, which are concatenated with an underscore ('_').

For example:

export class TestUser {    id: number;    name: string;    verified: boolean;}const userRepository = getRepository(TestUser);// getRawOne is not support type. just return any type. I want to disclose this return type.const loaedRawTestUser = await userRepository.createQueryBuilder('testUser')    .addSelect('COUNT(*()', 'myAliasForAllUserCount')    .getRawOne();/** * { *   testUser_id: number; *   testUser_name: string; *   testUser_verified: boolean; * } */console.log(loaedRawUser);

The issue comprises three parts:

  • Converting camelCase to snake_case.
  • Inferring the class name for the composition of the return string.
  • Exact value matching for the converted properties (Entity key).

The first part has been resolved.

I've implemented a camelCase to snake_case type inferer as follows:

export type CamelCaseToSnakeCase<    T extends string,    Joiner extends '' | '_' = ''> = T extends `${infer Character}${infer Rest}`    ? Character extends Uppercase<Character>        ? `${Joiner}${Lowercase<Character>}${CamelCaseToSnakeCase<Rest, '_'>}`        : `${Character}${CamelCaseToSnakeCase<Rest, '_'>}`    : '';

Is there something way?


Additional Requirements if it can be.

When TestUser class has method like this:

export class TestUser {    id: number;    name: string;    verified: boolean;    getNameIfVerified(): {        return this.verified ? this.name : 'not verified'    }}const typeormRawColumTestSample: Partial<TypeORMRawColumns<TestUser, 'TestUser'>> = {    testUser_id: 1,    //  when testUser_id: '1' should be error.    testUser__get_name_if_verified // should be error.}

How can I exclude methods from a Type in TypeScript


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>