-
-
Notifications
You must be signed in to change notification settings - Fork 54
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
Need help with generated types #90
Comments
along with...
|
Having the same issue. |
Just using kysely and this project and run into the same issue. I think this stems from a combination of things export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
? ColumnType<S, I | undefined, U>
: ColumnType<T, T | undefined, T>; Looking at the actual So the issue here, is that the columns wrapped by this export type selectableNum = Selectable<number>;
export type updateableNum = Updateable<number>; This leads to a separate issue (which I can open another thread if need be), in that the autogenerated types don't generate the |
Not sure where I've picked this up, but you can do the following to quickly reexport all types as selectable:
|
I encountered the same problem. For anyone interested, here's the script I created for post-processing. I'm running it with # `./relative/path/to/types.d.ts` is relative from the directory where `postprocess.ts` is!
pushd path/to/script && bun postprocess.ts ./relative/path/to/types.d.ts && popd // postprocess.ts
import { readFileSync, writeFileSync } from 'fs';
import * as path from 'path';
function transformFile() {
const relativeFilePath = process.argv[2];
if (!relativeFilePath) {
console.error('No file path provided!');
return;
}
const filePath = path.join(__dirname, relativeFilePath);
const content = readFileSync(filePath, 'utf-8');
const lines = content.split('\n');
let dbInterfaceLineIndex = lines.findIndex((line) => {
return line === 'export interface DB {';
});
if (dbInterfaceLineIndex === -1) {
console.warn("Couldn't find `export interface DB {`! This script is fragile and the result is probably wrong!");
dbInterfaceLineIndex = Number.MAX_SAFE_INTEGER; // So that the check below doesn't return true;
}
const transformedLines = lines.flatMap((line, index) => {
if (line.startsWith('import type')) {
//
// Matches on `import type { ... } from "kysely";`
// and appends `Selectable`, `Insertable` and `Updateable`
// to whatever is inside `{ ... }`.
//
// E.g.
//
// import type { ColumnType } from "kysely";
//
// becomes
//
// import type { ColumnType, Insertable, Selectable, Updateable } from "kysely";
//
const importedTypes = line.match(/^import type \{(.*) \} from "kysely";$/)?.[1];
if (!importedTypes) {
return line;
}
return line.replace(importedTypes, `${importedTypes}, Insertable, Selectable, Updateable`);
}
if (line.startsWith('export interface')) {
//
// Matches on `export interface GeneratedInterface {`
// appends `Table` to it: `GeneratedInterfaceTable`
// and adds the three specific definitions:
// `Selectable`, `Insertable` and `Updateable`
//
const interfaceName = line.match(/export interface (\w+) \{/)?.[1];
if (!interfaceName || interfaceName === 'DB') {
return line;
}
return [
`export type ${interfaceName} = Selectable<${interfaceName}Table>;`,
`export type New${interfaceName} = Insertable<${interfaceName}Table>;`,
`export type ${interfaceName}Update = Updateable<${interfaceName}Table>;`,
'',
line.replace(interfaceName, `${interfaceName}Table`),
];
}
if (index > dbInterfaceLineIndex) {
//
// Matches table definitions in the `DB` interface like this
//
// export interface DB {
// user: User;
// }
//
// and generate this
//
// export interface DB {
// user: UserTable;
// }
//
const dbTableInterface = line.match(/^\s+\w+: (\w+);$/)?.[1];
if (!dbTableInterface) {
return line;
}
return line.replace(dbTableInterface, `${dbTableInterface}Table`);
}
return line;
});
writeFileSync(filePath, transformedLines.join('\n'), 'utf-8');
}
transformFile(); I'm using Postgres and simple tables for now, hopefully it works well in other contexts too :) |
I think it'd be beneficial for kysely-codegen to generate the So instead of export interface User {
id: Generated<string>
...
} It'd be great if the generated types were actually export interface UserTable {
id: Generated<string>
...
}
export type User = Selectable<UserTable>
export type NewUser = Insertable<UserTable>
export type UserUpdate = Updateable<UserTable> This is what we instruct people to do in Kysely docs. |
Agreed, for now I manually define the Selectable/Insertable/Updatable in my own file by having it generate stuff prefixed with the postgres schema name and then exporting stuff like I've accidentally used my table interfaces as row types multiple times already due to this not being the case by default, hahaha. |
I am trying to use Kysely as a query builder and prisma for migration
Prisma Model
The generated types are
and for this query
user is of type
So when I use the USER type form generated one
Here type mismatch is happening the returned user has no generated but the generated one has and I am not able to understand how to solve this
Error message :
``
``
I am new to the codegen world any help would be really appreciated do I need to define types my self in this case?
Upvote & Fund
The text was updated successfully, but these errors were encountered: