-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgoliaIndexClient.js
66 lines (59 loc) · 1.4 KB
/
algoliaIndexClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const algoliasearch = require("algoliasearch");
const prisma = require("./prismaClient");
require("dotenv").config();
const client = algoliasearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_ADMIN_API_KEY
);
const restaurantsIndex = client.initIndex("restaurant_data_index");
const menuItemsIndex = client.initIndex("menu_items_data_index");
const restaurantRecords = async () => {
const records = await prisma.restaurant.findMany({
select: {
id: true,
email: true,
name: true,
subdomain: true,
cuisineType: true,
location: true,
},
where: {
deletedAt: null,
},
});
return records;
};
const menuItemRecords = async () => {
const records = await prisma.menuItem.findMany({
select: {
id: true,
name: true,
price: true,
nutritionalValues: true,
restaurantId: true,
},
where: {
deletedAt: null,
},
});
return records;
};
async function main() {
try {
const restaurant_records = await restaurantRecords();
const menu_item_records = await menuItemRecords();
await restaurantsIndex
.saveObjects(restaurant_records, {
autoGenerateObjectIDIfNotExist: true,
})
.wait();
await menuItemsIndex
.saveObjects(menu_item_records, {
autoGenerateObjectIDIfNotExist: true,
})
.wait();
} catch (error) {
console.log(error);
}
}
main();