Skip to content

Commit

Permalink
Add index to products table for FTS
Browse files Browse the repository at this point in the history
  • Loading branch information
armans-code committed Oct 18, 2024
1 parent 6606ba0 commit bc49880
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { integer, numeric, pgTable, serial, text } from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
import {
index,
integer,
numeric,
pgTable,
serial,
text,
} from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";

export const collections = pgTable("collections", {
Expand Down Expand Up @@ -30,15 +38,24 @@ export const subcategories = pgTable("subcategories", {
.references(() => subcollection.id, { onDelete: "cascade" }),
});

export const products = pgTable("products", {
slug: text("slug").notNull().primaryKey(),
name: text("name").notNull(),
description: text("description").notNull(),
price: numeric("price").notNull(),
subcategory_slug: text("subcategory_slug")
.notNull()
.references(() => subcategories.slug, { onDelete: "cascade" }),
});
export const products = pgTable(
"products",
{
slug: text("slug").notNull().primaryKey(),
name: text("name").notNull(),
description: text("description").notNull(),
price: numeric("price").notNull(),
subcategory_slug: text("subcategory_slug")
.notNull()
.references(() => subcategories.slug, { onDelete: "cascade" }),
},
(table) => ({
nameSearchIndex: index("name_search_index").using(
"gin",
sql`to_tsvector('english', ${table.name})`,
),
}),
);

export const collectionsRelations = relations(collections, ({ many }) => ({
categories: many(categories),
Expand Down

0 comments on commit bc49880

Please sign in to comment.