Skip to content

Commit cd620b8

Browse files
committed
chore: better templates
1 parent 0c155ef commit cd620b8

16 files changed

+154
-210
lines changed
Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
{{> snippets/import}}
22
import type { MultipleBatchRequest } from 'algoliasearch';
33

4-
try {
5-
// You need an API key with `deleteIndex`
6-
{{> snippets/init}}
4+
// You need an API key with `deleteIndex`
5+
{{> snippets/init}}
76

8-
// List all indices
9-
const indices = {{#dynamicSnippet}}listIndicesSimple{{/dynamicSnippet}};
7+
// List all indices
8+
const indices = {{#dynamicSnippet}}listIndicesSimple{{/dynamicSnippet}};
109

11-
// Primary indices don't have a `primary` key
12-
const primaryIndices = indices.items.filter(item => item.primary == null);
13-
const replicaIndices = indices.items.filter(item => item.primary != null);
10+
// Primary indices don't have a `primary` key
11+
const primaryIndices = indices.items.filter(item => item.primary == null);
12+
const replicaIndices = indices.items.filter(item => item.primary != null);
1413

15-
// Delete primary indices first
16-
if (primaryIndices.length > 0) {
17-
const requests: MultipleBatchRequest[] = primaryIndices.map(index => ({
18-
action: 'delete',
19-
indexName: index.name
20-
}));
21-
{{#dynamicSnippet}}deleteMultipleIndicesPrimary{{/dynamicSnippet}};
22-
console.log("Deleted primary indices.");
23-
}
14+
// Delete primary indices first
15+
if (primaryIndices.length > 0) {
16+
const requests: MultipleBatchRequest[] = primaryIndices.map(index => ({
17+
action: 'delete',
18+
indexName: index.name
19+
}));
20+
{{#dynamicSnippet}}deleteMultipleIndicesPrimary{{/dynamicSnippet}};
21+
console.log("Deleted primary indices.");
22+
}
2423

25-
// Now, delete replica indices
26-
if (replicaIndices.length > 0) {
27-
const requests: MultipleBatchRequest[] = replicaIndices.map(index => ({
28-
action: 'delete',
29-
indexName: index.name
30-
}));
31-
{{#dynamicSnippet}}deleteMultipleIndicesReplica{{/dynamicSnippet}};
32-
console.log("Deleted replica indices.");
33-
}
34-
} catch (e: any) {
35-
console.error(e);
24+
// Now, delete replica indices
25+
if (replicaIndices.length > 0) {
26+
const requests: MultipleBatchRequest[] = replicaIndices.map(index => ({
27+
action: 'delete',
28+
indexName: index.name
29+
}));
30+
{{#dynamicSnippet}}deleteMultipleIndicesReplica{{/dynamicSnippet}};
31+
console.log("Deleted replica indices.");
3632
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{{> snippets/import}}
22

3-
try {
4-
{{> snippets/init}}
3+
{{> snippets/init}}
54

6-
// @ts-ignore
7-
const { default: records } = await import('./actors.json');
5+
// @ts-ignore
6+
const { default: records } = await import('./actors.json');
87

9-
const chunkSize = 10000
8+
const chunkSize = 10000
109

11-
for (let beginIndex = 0; beginIndex < records.length; beginIndex += chunkSize) {
10+
for (let beginIndex = 0; beginIndex < records.length; beginIndex += chunkSize) {
11+
try {
1212
const chunk = records.slice(beginIndex, beginIndex + chunkSize);
1313
{{#dynamicSnippet}}saveObjectsChunks{{/dynamicSnippet}}
14+
} catch (e: any) {
15+
console.error(e);
1416
}
15-
} catch (e: any) {
16-
console.error(e);
1717
}
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
{{> snippets/import}}
22

3-
try {
4-
{{> snippets/init}}
3+
{{> snippets/init}}
54

6-
// @ts-ignore
7-
const { default: products } = await import('./products.json');
5+
// @ts-ignore
6+
const { default: products } = await import('./products.json');
87

9-
const records = products.map((product) => {
10-
const reference = product["product_reference"];
11-
const suffixes: string[] = []
8+
const records = products.map((product) => {
9+
const reference = product["product_reference"];
10+
const suffixes: string[] = []
1211
13-
for (let i = reference.length; i > 1; i--) {
14-
suffixes.unshift(reference.slice(i));
15-
}
16-
return { ...product, product_reference_suffixes: suffixes };
17-
});
12+
for (let i = reference.length; i > 1; i--) {
13+
suffixes.unshift(reference.slice(i));
14+
}
15+
return { ...product, product_reference_suffixes: suffixes };
16+
});
1817

19-
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}}
20-
} catch (e: any) {
21-
console.error(e);
22-
}
18+
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}}

templates/javascript/guides/search/saveObjectsPublicUser.mustache

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
const playlists: Record<string, any>[] = [] // Your records
44

5-
try {
6-
{{> snippets/init}}
5+
{{> snippets/init}}
76

8-
{{#dynamicSnippet}}saveObjectsPlaylistsWithUserIDPublic{{/dynamicSnippet}}
9-
} catch (e: any) {
10-
console.error(e);
11-
}
7+
{{#dynamicSnippet}}saveObjectsPlaylistsWithUserIDPublic{{/dynamicSnippet}}
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
{{> snippets/import}}
22

3-
try {
4-
{{> snippets/init}}
3+
{{> snippets/init}}
54

6-
const records: Array<Record<string, any>> = [];
5+
const records: Array<Record<string, any>> = [];
76

8-
await client.browseObjects<Record<string, any>>({ indexName: "<YOUR_INDEX_NAME>", browseParams: undefined, aggregator: (res) =>
9-
records.push(
10-
res.hits.map((record) => ({
11-
...record,
12-
isPopular: record.nbFollowers >= 1_000_000,
13-
}))
14-
)
15-
});
7+
await client.browseObjects<Record<string, any>>({ indexName: "<YOUR_INDEX_NAME>", browseParams: undefined, aggregator: (res) =>
8+
records.push(
9+
res.hits.map((record) => ({
10+
...record,
11+
isPopular: record.nbFollowers >= 1_000_000,
12+
}))
13+
)
14+
});
1615

17-
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}}
18-
} catch (e: any) {
19-
console.error(e);
20-
}
16+
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}}
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
{{> snippets/import}}
22
import type { SearchParams } from 'algoliasearch';
33

4-
try {
5-
{{> snippets/init}}
4+
{{> snippets/init}}
65

7-
const dateTimestamp = Date.now();
8-
const searchParams: SearchParams = {
9-
query: "<YOUR_SEARCH_QUERY>",
10-
filters: `date_timestamp > ${dateTimestamp}`
11-
};
6+
const dateTimestamp = Date.now();
7+
const searchParams: SearchParams = {
8+
query: "<YOUR_SEARCH_QUERY>",
9+
filters: `date_timestamp > ${dateTimestamp}`
10+
};
1211

13-
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
14-
} catch (e: any) {
15-
console.error(e);
16-
}
12+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
{{> snippets/import}}
22

3-
try {
4-
{{> snippets/init}}
3+
{{> snippets/init}}
54

6-
const query = 'query';
5+
const query = 'query';
76

8-
// 1. Change the sort dynamically based on the UI events
9-
const sortByPrice = false;
7+
// 1. Change the sort dynamically based on the UI events
8+
const sortByPrice = false;
109

11-
// 2. Get the index name based on sortByPrice
12-
const indexName = sortByPrice ? 'products_price_desc' : 'products';
10+
// 2. Get the index name based on sortByPrice
11+
const indexName = sortByPrice ? 'products_price_desc' : 'products';
1312

14-
// 3. Search on dynamic index name (primary or replica)
15-
{{#dynamicSnippet}}searchWithIndexNameVar{{/dynamicSnippet}};
16-
} catch (e: any) {
17-
console.error(e.message);
18-
}
13+
// 3. Search on dynamic index name (primary or replica)
14+
{{#dynamicSnippet}}searchWithIndexNameVar{{/dynamicSnippet}};
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
{{> snippets/import}}
22
import type { SearchParams } from 'algoliasearch';
33

4-
try {
5-
{{> snippets/init}}
4+
{{> snippets/init}}
65

7-
const d = new Date();
8-
const searchParams: SearchParams = {
9-
query: "<YOUR_SEARCH_QUERY>",
10-
filters: `date_timestamp > ${Math.floor(d.setDate(d.getDate() - 365) / 1000)}`
11-
};
6+
const d = new Date();
7+
const searchParams: SearchParams = {
8+
query: "<YOUR_SEARCH_QUERY>",
9+
filters: `date_timestamp > ${Math.floor(d.setDate(d.getDate() - 365) / 1000)}`
10+
};
1211

13-
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
14-
} catch (e: any) {
15-
console.error(e);
16-
}
12+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}

templates/javascript/guides/search/searchWithAnalyticsAndHeader.mustache

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@
22
import type { SearchParamsObject } from 'algoliasearch';
33

44

5-
try {
6-
{{> snippets/init}}
5+
{{> snippets/init}}
76

8-
/*
9-
'94.228.178.246' should be replaced with your user's IP address.
10-
Depending on your stack there are multiple ways to get this information.
11-
*/
12-
const ip = '94.228.178.246';
13-
const query = 'query';
7+
/*
8+
'94.228.178.246' should be replaced with your user's IP address.
9+
Depending on your stack there are multiple ways to get this information.
10+
*/
11+
const ip = '94.228.178.246';
12+
const query = 'query';
1413

15-
const searchParams: SearchParamsObject = {
16-
query,
17-
analytics: true,
18-
};
14+
const searchParams: SearchParamsObject = {
15+
query,
16+
analytics: true,
17+
};
1918

20-
{{#dynamicSnippet}}searchWithSearchParamsAndForwardedHeader{{/dynamicSnippet}};
21-
} catch (e: any) {
22-
console.error(e);
23-
}
19+
{{#dynamicSnippet}}searchWithSearchParamsAndForwardedHeader{{/dynamicSnippet}};
2420

templates/javascript/guides/search/searchWithGAToken.mustache

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@ const getGoogleAnalyticsUserIdFromBrowserCookie = (_: string) => {
55
return ""; // Implement your logic here
66
}
77

8-
try {
9-
{{> snippets/init}}
8+
{{> snippets/init}}
109

11-
const userToken = getGoogleAnalyticsUserIdFromBrowserCookie('_ga');
12-
let searchParams: SearchParams = {
13-
query: "<YOUR_SEARCH_QUERY>",
14-
userToken
15-
};
10+
const userToken = getGoogleAnalyticsUserIdFromBrowserCookie('_ga');
11+
let searchParams: SearchParams = {
12+
query: "<YOUR_SEARCH_QUERY>",
13+
userToken
14+
};
1615

17-
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
16+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
1817

19-
const loggedInUser: string | undefined = undefined;
20-
searchParams.userToken = loggedInUser ?? userToken;
18+
const loggedInUser: string | undefined = undefined;
19+
searchParams.userToken = loggedInUser ?? userToken;
2120

22-
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
23-
} catch (e: any) {
24-
console.error(e);
25-
}
21+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}

0 commit comments

Comments
 (0)