pgvector / pgvector-node

pgvector support for Node.js, Deno, and Bun (and TypeScript)
MIT License
303 stars 9 forks source link

Will it support typescript? #2

Closed devilyouwei closed 1 year ago

devilyouwei commented 1 year ago

I am using node.js with TypeScript. I got an issue that DataTypes.VECTOR does not exist even if using registerType(Sequelize). Also, I will use an ORM which can cause more issues when DataType is not supported.

ankane commented 1 year ago

Hi @devilyouwei, added code for generating declaration files in the typescript branch, but they don't appear to be needed for new TypeScript projects (npx tsc --init). What error are you seeing with TypeScript?

Also, I'm not sure how to reproduce the DataTypes.VECTOR issue. Can you paste the full error message, backtrace, and relevant code?

devilyouwei commented 1 year ago

Hi @ankane, actually, I am using sequelize-typescript to create MVC models and my issue is about Dtype declaration. There was no runtime error to reproduce because I even didn't succeed to run my code. TS kept showing that there was no 'VECTOR' in Sequelize DataType. I did try to customize a VECTOR DataType by following the official document and your code. And finally, I found I need to use JS to register the type and I gave up.

I find a more simple approach to use VECTOR, directly using the string type, as the following TS code (egg.js Model):

import { Table, Column, AutoIncrement, PrimaryKey, Model, DataType, Sequelize } from 'sequelize-typescript'
// import pgvector from 'pgvector/sequelize'
// pgvector.registerType(Sequelize)

@Table({ modelName: 'page' })
export class Page extends Model {
    @PrimaryKey
    @AutoIncrement
    @Column({
        type: DataType.INTEGER
    })
    id: number

    @Column({
        type: DataType.INTEGER
    })
    page: number

    @Column({
        type: DataType.INTEGER
    })
    resourceId: number

    @Column({
        type: 'VECTOR(1536)'
    })
    embedding: string

    @Column({
        type: DataType.TEXT
    })
    content: string
}

export default () => Page

Thank you @ankane

ankane commented 1 year ago

fwiw, @Column(DataType.VECTOR(1536)) seems to work if you call pgvector.registerType(Sequelize), but glad you found another approach that works.

seyfer commented 1 year ago

@ankane when I do import pgvector from 'pgvector/pg';

to register a type to pg client

(async () => {
  await pgvector.registerType(pgClient);
})();

I have in my WebStorm IDE and error

TS7016: Could not find a declaration file for module 'pgvector/pg'. '.../node_modules/pgvector/pg/index.js' implicitly has an 'any' type.   Try `npm i --save-dev @types/pgvector` if it exists or add a new declaration (.d.ts) file containing `declare module 'pgvector/pg';`

but there is no @types/pgvector exist.

ankane commented 1 year ago

Just pushed 0.1.1, which includes types.

Note: moduleResolution must be set to node16/nodenext in tsconfig.json for TypeScript to find them.

wlaurance commented 1 year ago

I'm trying this now and I don't believe the types are included in the actual package file.

image

I tried to fix it with local linking the package and I made this change to make it work better https://github.com/pgvector/pgvector-node/pull/3

amine-mf commented 1 year ago

Just pushed 0.1.1, which includes types.

Note: moduleResolution must be set to node16/nodenext in tsconfig.json for TypeScript to find them.

It seems like more than one person struggle with the issue, and it is not always an (easy) option to just migrate to nodenext. Out of 8 issues, 3 are related to this (#11 and #7) @ankane Is there any other workarounds (Could not find any myself)? Is there any chance to support node module resolution?