xataio / client-ts

Xata.io SDK for TypeScript and JavaScript
https://xata.io/docs/sdk/typescript/overview
Apache License 2.0
123 stars 8 forks source link

Ignore selective tables while doing codegen for types locally #1425

Open prvnbist opened 7 months ago

prvnbist commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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

File: https://github.com/xataio/client-ts/blob/c927299b9e0c154db468ad52c8286b9a71e6c07c/cli/src/migrations/pgroll.ts#L59-L60

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: https://github.com/xataio/client-ts/blob/c927299b9e0c154db468ad52c8286b9a71e6c07c/packages/client/src/api/dataPlaneSchemas.ts#L176-L178