Skip to content
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

Ignore selective tables while doing codegen for types locally #1425

Open
prvnbist opened this issue Mar 30, 2024 · 2 comments
Open

Ignore selective tables while doing codegen for types locally #1425

prvnbist opened this issue Mar 30, 2024 · 2 comments
Labels
bug Something isn't working needs-triage

Comments

@prvnbist
Copy link

prvnbist commented Mar 30, 2024

Describe the bug

Getting a type error in xata.ts

Short Error

Types of property 'type' are incompatible.
Type '"varchar(255)"' is not assignable to type '"string" | "datetime" | "text" | "int" | "link" | "bool" | "email" | "float" | "multiple" | "vector" | "file[]" | "file" | "json"'.
Full Error

Type 'readonly [{ readonly name: "knex_migrations"; readonly columns: readonly [{ readonly name: "batch"; readonly type: "int"; readonly notNull: false; readonly unique: false; readonly defaultValue: null; }, { readonly name: "id"; readonly type: "int"; readonly notNull: true; readonly unique: true; readonly defaultValue:...' does not satisfy the constraint 'readonly BaseSchema[]'.
  Type '{ readonly name: "knex_migrations"; readonly columns: readonly [{ readonly name: "batch"; readonly type: "int"; readonly notNull: false; readonly unique: false; readonly defaultValue: null; }, { readonly name: "id"; readonly type: "int"; readonly notNull: true; readonly unique: true; readonly defaultValue: "nextval(...' is not assignable to type 'BaseSchema'.
    Type '{ readonly name: "knex_migrations"; readonly columns: readonly [{ readonly name: "batch"; readonly type: "int"; readonly notNull: false; readonly unique: false; readonly defaultValue: null; }, { readonly name: "id"; readonly type: "int"; readonly notNull: true; readonly unique: true; readonly defaultValue: "nextval(...' is not assignable to type 'BaseSchema'.
      Types of property 'columns' are incompatible.
        Type 'readonly [{ readonly name: "batch"; readonly type: "int"; readonly notNull: false; readonly unique: false; readonly defaultValue: null; }, { readonly name: "id"; readonly type: "int"; readonly notNull: true; readonly unique: true; readonly defaultValue: "nextval('bb_c59lmqcph168hepgkv9oil4en4_atlnm6.knex_migrations_...' is not assignable to type 'readonly ({ name: string; type: "string" | "datetime" | "text" | "int" | "link" | "bool" | "email" | "float" | "multiple" | "vector" | "file[]" | "file" | "json"; notNull?: boolean | undefined; } | { ...; })[]'.
          Type '{ readonly name: "batch"; readonly type: "int"; readonly notNull: false; readonly unique: false; readonly defaultValue: null; } | { readonly name: "id"; readonly type: "int"; readonly notNull: true; readonly unique: true; readonly defaultValue: "nextval('bb_c59lmqcph168hepgkv9oil4en4_atlnm6.knex_migrations_id_seq'::...' is not assignable to type '{ name: string; type: "string" | "datetime" | "text" | "int" | "link" | "bool" | "email" | "float" | "multiple" | "vector" | "file[]" | "file" | "json"; notNull?: boolean | undefined; } | { ...; }'.
            Type '{ readonly name: "name"; readonly type: "varchar(255)"; readonly notNull: false; readonly unique: false; readonly defaultValue: null; }' is not assignable to type '{ name: string; type: "string" | "datetime" | "text" | "int" | "link" | "bool" | "email" | "float" | "multiple" | "vector" | "file[]" | "file" | "json"; notNull?: boolean | undefined; } | { ...; }'.
              Type '{ readonly name: "name"; readonly type: "varchar(255)"; readonly notNull: false; readonly unique: false; readonly defaultValue: null; }' is not assignable to type '{ name: string; type: "string" | "datetime" | "text" | "int" | "link" | "bool" | "email" | "float" | "multiple" | "vector" | "file[]" | "file" | "json"; notNull?: boolean | undefined; }'.
                Types of property 'type' are incompatible.
                  Type '"varchar(255)"' is not assignable to type '"string" | "datetime" | "text" | "int" | "link" | "bool" | "email" | "float" | "multiple" | "vector" | "file[]" | "file" | "json"'.

The error is in generated types on second line at SchemaTables

export type SchemaTables = typeof tables;
export type InferredTypes = SchemaInference<SchemaTables>;

Here tables is typed as following where one of the columns have a type varchar(255)

const tables = [
...
{
  columns: [
    ...
    type: "varchar(255)",
    ...
  ]
}
...
] as const;

To Reproduce
Having one of the column type as varchar(255) should help reproduce it.

Expected behavior

Software version
"@xata.io/client": "^0.29.3"

Additional context
I'm using knex for building schema and migration, this would be resolved if I switch to .text instead of .string which sets the column type to be varchar since that's what typescript understands.

@prvnbist prvnbist added the bug Something isn't working label Mar 30, 2024
@prvnbist
Copy link
Author

Update: I migrated to use text datatype for my table columns but knex adds two tables knex_migrations and knex_migrations_lock which contain column with data type varchar which I've no control over, so my questions

is there a way to ignore tables while pulling locally and generating types?

@prvnbist prvnbist changed the title Typescript | Type '"varchar(255)"' is not assignable to type inn xata.ts Ignore selective tables while doing codegen for types locally Mar 31, 2024
@prvnbist
Copy link
Author

prvnbist commented Mar 31, 2024

Looked through the codebase for types generation, I think it might be worth updating this switch statement to handle varchar as well

File:

case 'text':
return 'text';

Suggested changes

case 'varchar':
case 'text':
      return 'text';

Update 1:
Same thing for type uuid and date except for date the type Column['type'] should include date

ref:

export type Column = {
name: string;
type:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-triage
Projects
None yet
Development

No branches or pull requests

1 participant