1 import { model } from "@medusajs/framework/utils"
2 import { SearchIndexState } from "../utils"
3 import { SearchIndex } from "./search-index"
4 import { SearchIndexSync } from "./search-index-sync"
5
6 /**
7 * One physical index ever built for a logical `SearchIndex`. Auto-incrementing
8 * per index, so `search_index.active_version` can point at whichever one
9 * currently serves reads while older or still-building versions sit beside it.
10 */
11 export const SearchIndexVersion = model
12 .define("SearchIndexVersion", {
13 id: model.id({ prefix: "srhver" }).primaryKey(),
14 version: model.number(),
15 // The identifier of the provider backing this version.
16 provider: model.text(),
17 // The physical index/table/namespace this version was built under.
18 physical_name: model.text(),
19 // Used to detect schema drift against the loaded definition.
20 definition_hash: model.text(),
21 status: model.enum(SearchIndexState).default(SearchIndexState.PENDING),
22 search_index: model.belongsTo(() => SearchIndex, { mappedBy: "versions" }),
23 syncs: model.hasMany(() => SearchIndexSync, {
24 mappedBy: "search_index_version",
25 }),
26 })
27 .indexes([
28 {
29 name: "IDX_search_index_version_search_index_id_version",
30 on: ["search_index_id", "version"],
31 unique: true,
32 where: "deleted_at IS NULL",
33 },
34 ])