Custom vector type with pgvector

Ffjorn5/21/2023
I'm trying to use the pgvector extension with a custom type but am running into an issue with the migration file. My custom type looks like
export const vector = customType<
  {
    data: number[];
    driverData: string;
    config: { dimension: number };
    default: false
  }
>({
  dataType(config) {
    return `vector(${config?.dimension ?? 3})`;
  },
  toDriver(value: number[]): string {
    return JSON.stringify(value)
  },
  fromDriver(value: string): number[] {
    return JSON.parse(value);
  },
});

export const myTable = pgTable('my_table', {
  embedding: vector('embedding', { dimension: 3 })
})


However when adding a vector column my generated migration file looks like
ALTER TABLE "my_table" ADD COLUMN "embedding" "vector(3)";

Notice the quotes around "vector(3)". Postgres is interpreting this as the literal type "vector(3)" instead of vector from pgvector. Applying the migration gives me a the following error:PostgresError: type "vector(3)" does not exist

I confirmed that removing the quotes fixes the issue. Is there a way to tell the custom type to not wrap quotes here?