Skip to content

Commit

Permalink
test: clean gatsby data
Browse files Browse the repository at this point in the history
  • Loading branch information
jayavel-testsigma committed Aug 23, 2024
1 parent de11861 commit 0f2de3d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 43 deletions.
15 changes: 3 additions & 12 deletions .github/workflows/staging-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ jobs:
until $(curl --output /dev/null --silent --head --fail http://localhost:8000); do
sleep 1
done
curl -v http://localhost:8000/___graphql
npm run index
- name: Run indexing script
run: npm run index
env:
TYPESENSE_HOST: ${{ secrets.WEBSITE_STAGING_TYPESENSE_HOST }}
TYPESENSE_PORT: ${{ secrets.WEBSITE_STAGING_TYPESENSE_PORT }}
Expand All @@ -81,15 +82,5 @@ jobs:
TYPESENSE_API_KEY: ${{ secrets.WEBSITE_STAGING_TYPESENSE_API_KEY }}
TYPESENSE_SEARCH_API_KEY: ${{ secrets.WEBSITE_STAGING_TYPESENSE_SEARCH_API_KEY }}

# - name: Run indexing script
# run: npm run index
# env:
# TYPESENSE_HOST: ${{ secrets.WEBSITE_STAGING_TYPESENSE_HOST }}
# TYPESENSE_PORT: ${{ secrets.WEBSITE_STAGING_TYPESENSE_PORT }}
# TYPESENSE_PROTOCOL: ${{ secrets.WEBSITE_STAGING_TYPESENSE_PROTOCOL }}
# TYPESENSE_COLLECTION: ${{ secrets.WEBSITE_STAGING_TYPESENSE_COLLECTION }}
# TYPESENSE_API_KEY: ${{ secrets.WEBSITE_STAGING_TYPESENSE_API_KEY }}
# TYPESENSE_SEARCH_API_KEY: ${{ secrets.WEBSITE_STAGING_TYPESENSE_SEARCH_API_KEY }}

- name: Stop Gatsby server
run: lsof -i :8000 -t | xargs kill
70 changes: 39 additions & 31 deletions scripts/indexr.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require('dotenv').config({
require("dotenv").config({
path: `.env`,
});
const Typesense = require('typesense');
const Typesense = require("typesense");
const { request } = require("graphql-request");

const pageQuery = `
Expand Down Expand Up @@ -39,13 +39,13 @@ const pageQuery = `
function pageToTypesenseRecord({ node }) {
const { id, frontmatter, fields = {}, headings = [], ...rest } = node;

const formattedHeadings = headings.map(h => h.value || '').filter(Boolean);
const formattedHeadings = headings.map((h) => h.value || "").filter(Boolean);
return {
objectID: id,
title: frontmatter.title || '',
search_keyword: String(frontmatter.search_keyword || ''),
slug: fields.slug || '',
excerpt: frontmatter.excerpt || '',
title: frontmatter.title || "",
search_keyword: String(frontmatter.search_keyword || ""),
slug: fields.slug || "",
excerpt: frontmatter.excerpt || "",
headings: formattedHeadings,
...rest,
};
Expand All @@ -64,7 +64,6 @@ async function indexData() {
apiKey: process.env.TYPESENSE_API_KEY,
});


let collectionExists = false;
try {
await client.collections(process.env.TYPESENSE_COLLECTION).retrieve();
Expand All @@ -77,42 +76,51 @@ async function indexData() {

if (collectionExists) {
await client.collections(process.env.TYPESENSE_COLLECTION).delete();
console.log(`Collection ${process.env.TYPESENSE_COLLECTION} deleted successfully.`);
console.log(
`Collection ${process.env.TYPESENSE_COLLECTION} deleted successfully.`
);
}

await client.collections().create({
name: process.env.TYPESENSE_COLLECTION,
fields: [
{ name: 'objectID', type: 'string' },
{ name: 'title', type: 'string' },
{ name: 'search_keyword', type: 'string' },
{ name: 'slug', type: 'string' },
{ name: 'excerpt', type: 'string' },
{ name: 'headings', type: 'string[]', facet: false }
]
{ name: "objectID", type: "string" },
{ name: "title", type: "string" },
{ name: "search_keyword", type: "string" },
{ name: "slug", type: "string" },
{ name: "excerpt", type: "string" },
{ name: "headings", type: "string[]", facet: false },
],
});
console.log(`Collection ${process.env.TYPESENSE_COLLECTION} created successfully.`);
console.log(
`Collection ${process.env.TYPESENSE_COLLECTION} created successfully.`
);

const response = await request('http://localhost:8000/___graphql', pageQuery);
console.log('response', response);
const response = await request(
"http://127.0.0.1:8000/___graphql",
pageQuery
);
console.log("response", response);
const data = await response;

const records = data.docs.edges.map(pageToTypesenseRecord);

await client.collections(process.env.TYPESENSE_COLLECTION).documents().import(records).then((typesenseResponse) => {
// check the output of the response in the console
console.log('typesenseResponse', typesenseResponse);
console.log(
`🎉 Successfully indexed records to Typesense search.`
);
})
.catch((error) => {
console.error(error);
});
await client
.collections(process.env.TYPESENSE_COLLECTION)
.documents()
.import(records)
.then((typesenseResponse) => {
// check the output of the response in the console
console.log("typesenseResponse", typesenseResponse);
console.log(`🎉 Successfully indexed records to Typesense search.`);
})
.catch((error) => {
console.error(error);
});

console.log('Indexing complete!');
console.log("Indexing complete!");
} catch (error) {
console.error('Indexing error:', error);
console.error("Indexing error:", error);
}
}

Expand Down

0 comments on commit 0f2de3d

Please sign in to comment.