Dr. Sauce
Dr. Sauce
KKysely
Created by Dr. Sauce on 7/7/2023 in #help
Type of query result is {}[]
Hi all! Loving using Kysely at the moment, it's super easy to use. I ran into one problem, which I believe is probably a lack of understanding from myself around certain elements of TypeScript or Kysely itself. Here is the DB Schema from kysely-codegen:
import type { ColumnType } from "kysely";

export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
? ColumnType<S, I | undefined, U>
: ColumnType<T, T | undefined, T>;

export type Numeric = ColumnType<string, string | number, string | number>;

export type Timestamp = ColumnType<Date, Date | string, Date | string>;

export interface Disease {
id: Generated<string>;
coding_id: string;
coding_id_string: string;
description: string;
created_at: Generated<Timestamp>;
updated_at: Generated<Timestamp>;
}

export interface DiseaseTermSimilarity {
id: Generated<string>;
source_disease_id: string;
target_disease_id: string;
distance: Numeric;
created_at: Generated<Timestamp>;
updated_at: Generated<Timestamp>;
}
import type { ColumnType } from "kysely";

export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
? ColumnType<S, I | undefined, U>
: ColumnType<T, T | undefined, T>;

export type Numeric = ColumnType<string, string | number, string | number>;

export type Timestamp = ColumnType<Date, Date | string, Date | string>;

export interface Disease {
id: Generated<string>;
coding_id: string;
coding_id_string: string;
description: string;
created_at: Generated<Timestamp>;
updated_at: Generated<Timestamp>;
}

export interface DiseaseTermSimilarity {
id: Generated<string>;
source_disease_id: string;
target_disease_id: string;
distance: Numeric;
created_at: Generated<Timestamp>;
updated_at: Generated<Timestamp>;
}
Here is a simplified version of a query (which is without any type errors). It also returns the results correctly from the DB:
const query = db
// Select all disease mappings
.selectFrom('disease_term_similarity as mapping')
// Append on the source disease's info by it's ID
.leftJoin('disease as src_disease', (join) =>
join.onRef('src_disease.id', '=', 'mapping.source_disease_id')
)
// Append on the target disease's info by it's ID
.leftJoin('disease as tgt_disease', (join) =>
join.onRef('tgt_disease.id', '=', 'mapping.target_disease_id')
);

const results = await query.execute();
const query = db
// Select all disease mappings
.selectFrom('disease_term_similarity as mapping')
// Append on the source disease's info by it's ID
.leftJoin('disease as src_disease', (join) =>
join.onRef('src_disease.id', '=', 'mapping.source_disease_id')
)
// Append on the target disease's info by it's ID
.leftJoin('disease as tgt_disease', (join) =>
join.onRef('tgt_disease.id', '=', 'mapping.target_disease_id')
);

const results = await query.execute();
I noticed that results has the type {}[]. I expected the result to be aware of what fields are contained in the returned objects. Is there something specific I need to do in order to have the results array typed? Or should I be casting it to a type I create myself? Many thanks for your help!!!
14 replies