1 import { model } from "@medusajs/framework/utils"
2 import { SearchSyncStatus } from "../utils"
3 import { SearchIndexVersion } from "./search-index-version"
4
5 /**
6 * Sync runs against one index version. Resuming means finding that version's
7 * most recent row and picking up from its `last_key` — but only when that row
8 * says its progress can still be picked up (see `resumable`).
9 */
10 export const SearchIndexSync = model
11 .define("SearchIndexSync", {
12 id: model.id({ prefix: "srhsync" }).primaryKey(),
13 // The reindex job this run belongs to. One job may span several indexes.
14 job_id: model.text().nullable(),
15 status: model.enum(SearchSyncStatus).default(SearchSyncStatus.PENDING),
16 // Filters handed to the definition's `seed`. `null` is a full rebuild;
17 // anything else records the slice this run covered.
18 filters: model.json().nullable(),
19 // Last primary key handed to the engine, and the resume point.
20 last_key: model.text().nullable(),
21 // Whether a later run may continue from this row's `last_key`.
22 resumable: model.boolean().default(true),
23 documents_synced: model.number().default(0),
24 started_at: model.dateTime().nullable(),
25 completed_at: model.dateTime().nullable(),
26 error: model.text().nullable(),
27 search_index_version: model.belongsTo(() => SearchIndexVersion, {
28 mappedBy: "syncs",
29 }),
30 })
31 .indexes([
32 {
33 name: "IDX_search_index_sync_search_index_version_id",
34 on: ["search_index_version_id"],
35 unique: false,
36 where: "deleted_at IS NULL",
37 },
38 {
39 name: "IDX_search_index_sync_job_id",
40 on: ["job_id"],
41 unique: false,
42 where: "deleted_at IS NULL",
43 },
44 ])