romeerez/orchid-orm

[TSError] When declaring `vector` string inside a `search()` method

matthewmorek opened this issue · 2 comments

I created a repo method to allow searching for strings inside one of my db tables. When declaring a vector to use with my query, I'm getting the following TS error:

CleanShot 2023-11-22 at 21 24 53@2x

This is how I define my vector in said table:

change(async (db) => {
  await db.createTable(
    "location",
    { createIfNotExists: true, dropIfExists: true },
    (t) => ({
      // ... omitted table columns
      generatedTsVector: t
        .tsvector()
        .generated(["title", "description", "locality", "host_name", "address"])
        .searchIndex(),
    }),
  );
});

This is how I define my search method:

filteredBySearchQuery(q, searchQuery: string = undefined) {
  return searchQuery && searchQuery !== ""
    ? q.search({
        vector: "generated_ts_vector",
        query: searchQuery,
      })
    : q;
}

Which results in the following TS error:

Type 'string' is not assignable to type 'never'.
CleanShot 2023-11-22 at 21 23 02@2x

This is how I call the repo search method:

const locations = await locationRepo
  .allWithRelations()
  .onlyPublished()
  .filteredByEventType(selectedEventType)
  .filteredByFacilities(selectedFacilities)
  .filteredBySearchQuery(searchQuery)
  .offset(skip)
  .limit(take);

It looks to me like a simple type error, but without ignoring it it crashes my Next.js 13 build on Vercel.

Querying works perfectly when I use it in dev environment (where type errors don't cause a critical error), it only fails the build process where types are strictly checked.

I can see this column generated_ts_vector is of type tsvector in your migration, but what is the column type in your table file?

Type never in this error indicates that it cannot find any tsvector columns in the table.

Also, is the casing correct? It's camelCased in the migration, but snake_cased in a query.

The casing is correct, as I explicitly use snake_case for compatibility. Good call on the table structure: the table definition file didn't contain the correct reference to vector. All fixed now, thanks!

The error totally threw me off, as I was expecting to see something like "this property doesn't exist on type X".