A migration tool for Elasticsearch/OpenSearch. This package helps you manage your Elasticsearch indices and mappings with ease.
npm install @direct-democracy-solutions/elasticsearch-migrationnpx es-migrate create <migration-name>This command creates a new migration file. For example:
npx es-migrate create create_users_indexnpx es-migrate upApplies all pending migrations in order.
es-migrate runs and expects ESM. If you have a CommonJS project and
wish to run es-migrate, pass an ESM loader to Node explicitly:
node --loader ts-node/esm node_modules/@ahmetkasap/elasticsearch-migration/dist/cli.jstsx might work but hasn't been tested.
Your migrations should have the .mts or .mjs file extensions, not .ts.
npx es-migrate statusDisplays the status of all migrations.
npx es-migrate list-indicesLists all indices in your Elasticsearch cluster.
npx es-migrate alias-infoDisplays detailed information about index aliases.
npx es-migrate downReverts the last applied migration (rollback).
You can configure the tool using environment variables:
# Elasticsearch connection details
ELASTIC_SEARCH_NODE=http://127.0.0.1:9200
ELASTIC_SEARCH_USERNAME=elastic
ELASTIC_SEARCH_PASSWORD=password
# Directory for migration files
MIGRATIONS_PATH=./migrationsimport { Client } from "@opensearch-project/opensearch";
import type { IMigration } from "@direct-democracy-solutions/elasticsearch-migration";
const migration: IMigration = {
name: "create_users_index",
timestamp: 1703123456789,
async up(client: Client): Promise<void> {
await client.indices.create({
index: "users",
body: {
mappings: {
properties: {
name: { type: "text" },
email: { type: "keyword" },
age: { type: "integer" },
},
},
},
});
console.log("Users index created successfully");
},
async down(client: Client): Promise<void> {
await client.indices.delete({
index: "users",
});
console.log("Users index deleted successfully");
},
};
export default migration;For an effortless runtime experience on recent versions of Node, restrict your migrations to use only erasable syntax.
If you want to enforce this automatically, set the erasableSyntaxOnly
option in tsconfig.json.
TypeScript migrations with no erasable syntax work out of the box on Node v22.18.0 or later.
To run TypeScript migrations on Node versions v22.6-v22.17, Invoke Node with --experimental-strip-types:
npx --node-options='--experimental-strip-types' es-migrate upTo run TypeScript migrations with non-erasable syntax on v22.7.0 or later, invoke Node with --experimental-transform-types:
npx --node-options='--experimental-strip-types' es-migrate upIf neither of the above options is available to you, there is always tsx or ts-node:
npm i -D tsx
tsx es-migrate upOr, you can write your migrations in JavaScript.
import { MigrationService } from "@direct-democracy-solutions/elasticsearch-migration";
import type { IMigrationConfig } from "@direct-democracy-solutions/elasticsearch-migration";
const config: IMigrationConfig = {
node: "http://127.0.0.1:9200",
username: "elastic",
password: "password",
migrationsPath: "./migrations",
};
const migrationService = new MigrationService(config);
// Run all migrations
await migrationService.runMigrations();
// Check pending migrations
const pending = await migrationService.getPendingMigrations();
console.log(`Pending migrations: ${pending.length}`);interface IMigration {
name: string;
timestamp: number;
up(client: Client): Promise<void>;
down?(client: Client): Promise<void>;
}- ✅ TypeScript support
- ✅ Elasticsearch/OpenSearch compatibility
- ✅ Migration status tracking
- ✅ Sequential migration execution
- ✅ Rollback support
- ✅ CLI interface
- ✅ Programmatic API
# Install dependencies
npm install
# Test (requires configuration -- see below)
npm test
# Run in development mode
npm run dev
# Build
npm run build
# Start (run built version)
npm start
# Migration commands (development)
npm run migrate -- create <name> # Create new migration
npm run migrate -- up # Run pending migrations
npm run migrate -- down # Rollback last migration
npm run migrate -- forget # Forget last migration, skip rollback
npm run migrate -- status # Check migration status
npm run migrate -- list-indices # List all indices
npm run migrate -- alias-info # Show alias information
# Build and prepare for publish
npm run prepublishOnlyThe test suite needs a working Elasticsearch node to test against. To configure the test connection:
cp .env.example .envAnd edit the values accordingly.
You can get a basic Elasticsearch instance using the Compose file:
docker-compose up -dThis setup will work with the values in .env.example, no edits needed.
MIT