|
1
|
import "@medusajs/modules-sdk" |
|
2
|
import { |
|
3
|
defineSearchIndex, |
|
4
|
graphConsume, |
|
5
|
graphSeed, |
|
6
|
ProductEvents, |
|
7
|
search, |
|
8
|
} from "@medusajs/utils" |
|
9
|
|
|
10
|
const BATCH_SIZE = 100 |
|
11
|
|
|
12
|
const PRODUCT_FIELDS = ["id", "title", "handle", "status", "sales_channels.id"] |
|
13
|
const CUSTOMER_FIELDS = ["id", "email", "first_name", "last_name"] |
|
14
|
|
|
15
|
const productFields = search.define({ |
|
16
|
id: search.keyword().filterable(), |
|
17
|
title: search.text().searchable({ weight: 3 }).sortable(), |
|
18
|
handle: search.keyword().filterable(), |
|
19
|
status: search.keyword().filterable().facetable(), |
|
20
|
sales_channel_ids: search.keyword().array().filterable(), |
|
21
|
}) |
|
22
|
|
|
23
|
/** |
|
24
|
* Products come off `query.graph` with their sales channels as rows, and the |
|
25
|
* index wants them as a flat array of ids — which is all a `transform` is for. |
|
26
|
*/ |
|
27
|
const toProductDocuments = (products: any[]) => |
|
28
|
products.map((product) => ({ |
|
29
|
id: product.id, |
|
30
|
title: product.title, |
|
31
|
handle: product.handle, |
|
32
|
status: product.status, |
|
33
|
sales_channel_ids: (product.sales_channels ?? []).map( |
|
34
|
(channel: any) => channel.id |
|
35
|
), |
|
36
|
})) |
|
37
|
|
|
38
|
// Declared like user code: `defineSearchIndex` compiles the DSL schemas and |
|
39
|
// registers the definitions, and medusa-config passes the returned (normalized) |
|
40
|
// definitions to the Search Module as options. Paging, catch-up, read-back and |
|
41
|
// soft deletes all come from `graphSeed` / `graphConsume`. |
|
42
|
const productIndex = defineSearchIndex({ |
|
43
|
name: "product", |
|
44
|
entity: "product", |
|
45
|
fields: productFields, |
|
46
|
|
|
47
|
events: [ |
|
48
|
ProductEvents.PRODUCT_CREATED, |
|
49
|
ProductEvents.PRODUCT_UPDATED, |
|
50
|
ProductEvents.PRODUCT_DELETED, |
|
51
|
], |
|
52
|
|
|
53
|
consume: graphConsume<typeof productFields>({ |
|
54
|
fields: PRODUCT_FIELDS, |
|
55
|
transform: toProductDocuments, |
|
56
|
}), |
|
57
|
|
|
58
|
seed: graphSeed<typeof productFields>({ |
|
59
|
fields: PRODUCT_FIELDS, |
|
60
|
batch_size: BATCH_SIZE, |
|
61
|
transform: toProductDocuments, |
|
62
|
}), |
|
63
|
}) |
|
64
|
|
|
65
|
/** |
|
66
|
* A second index, so grouped results across entities are observable. No `events`: |
|
67
|
* ingestion is covered by the product index, and this one is filled by a reindex. |
|
68
|
*/ |
|
69
|
const customerIndex = defineSearchIndex({ |
|
70
|
name: "customer", |
|
71
|
entity: "customer", |
|
72
|
fields: search.define({ |
|
73
|
id: search.keyword().filterable(), |
|
74
|
email: search.keyword().searchable(), |
|
75
|
first_name: search.text().searchable(), |
|
76
|
last_name: search.text().searchable(), |
|
77
|
}), |
|
78
|
|
|
79
|
async *seed({ container, filters }) { |
|
80
|
let skip = 0 |
|
81
|
|
|
82
|
while (true) { |
|
83
|
const { data } = await container.query.graph({ |
|
84
|
entity: "customer", |
|
85
|
fields: CUSTOMER_FIELDS, |
|
86
|
filters: filters ?? {}, |
|
87
|
pagination: { skip, take: BATCH_SIZE, order: { id: "ASC" } }, |
|
88
|
}) |
|
89
|
|
|
90
|
if (!data.length) { |
|
91
|
return |
|
92
|
} |
|
93
|
|
|
94
|
yield [{ action: "upsert", documents: data }] |
|
95
|
|
|
96
|
if (data.length < BATCH_SIZE) { |
|
97
|
return |
|
98
|
} |
|
99
|
|
|
100
|
skip += BATCH_SIZE |
|
101
|
} |
|
102
|
}, |
|
103
|
}) |
|
104
|
|
|
105
|
export default [productIndex, customerIndex] |