SKILL.md
Algolia Local Dev Loop
Overview
Set up a fast, reproducible local development workflow for Algolia. Use separate dev indices, mock the client in tests, and iterate without touching production data.
Prerequisites
- Completed
algolia-install-authsetup - Node.js 18+ with npm/pnpm
- Vitest or Jest for testing
Instructions
Step 1: Environment-Based Index Names
// src/algolia/config.ts
import { algoliasearch } from 'algoliasearch';
const ENV = process.env.NODE_ENV || 'development';
// Each environment gets its own index prefix
export function indexName(base: string): string {
if (ENV === 'production') return base;
return `${ENV}_${base}`; // e.g., "development_products"
}
export const client = algoliasearch(
process.env.ALGOLIA_APP_ID!,
process.env.ALGOLIA_ADMIN_KEY!
);
Step 2: Seed Script for Dev Data
// scripts/seed-algolia.ts
import { client, indexName } from '../src/algolia/config';
const SEED_DATA = [
{ objectID: 'prod-1', name: 'Widget A', category: 'tools', price: 29.99 },
{ objectID: 'prod-2', name: 'Widget B', category: 'tools', price: 49.99 },
{ objectID: 'prod-3', name: 'Gadget C', category: 'electronics', price: 199.99 },
];
async function seed() {
const idx = indexName('products');
// replaceAllObjects atomically swaps index content
const { taskID } = await client.replaceAllObjects({
indexName: idx,
objects: SEED_DATA,
});
await client.waitForTask({ indexName: idx, taskID });
// Configure settings for the dev index
await client.setSettings({
indexName: idx,
indexSettings: {
searchableAttributes: ['name', 'category'],
attributesForFaceting: ['category', 'filterOnly(price)'],
customRanking: ['asc(price)'],
},
});
console.log(`Seeded ${SEED_DATA.length} records into ${idx}`);
}
seed().catch(console.error);
{
"scripts": {
"seed:algolia": "npx tsx scripts/seed-algolia.ts",
"dev": "tsx watch src/index.ts",
"test": "vitest",
"test:watch": "vitest --watch"
}
}
