
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
elasticsearch-migrate
Advanced tools
Tool to run migrations to create/modify Elasticsearch index mapping.
Package to help you run one time migrations into your Elasticsearch DB such as Creating an index, inserting master data etc.
Two indices will be created in your Elasticsearch instance.
One for storing the migration file names that already ran, other for updating a lock if the migrations are running currently, to avoid duplication of migrations in a muliple instances scenario.
Assuming the below project structure
βββ src
β βββdb
β β βββ bootstrap-db.js
β β βββ es-client.js
β β βββ es-migrations
β β β βββ 01-create-index.js
β β β βββ 02-update-schema.js
β β β βββ 03-update-again.js
β β β βββ 04-update-4.js
β βββindex.js
With below file contents
index.js
import { bootstrapDb } from './db/bootstrap-db';
bootstrapDb();
db/es-client.js
Exports the Elasticsearch client instance
import { Client } from '@elastic/elasticsearch';
export const client = new Client({
node: process.env.ELASTICSEARCH_HOST
});
db/bootstrap-db.js
import { migrateLatest, Migration } from 'elasticsearch-migrate';
import { client } from './es-client';
export async function bootstrapDb() {
/** @type {import('elasticsearch-migrate').MigrationConfig} */
const migrationConfig = {
indexName: 'documents_migrations',
client,
directory: __dirname + '/es-migrations'
};
await migrateLatest(migrationConfig);
/*
βββββββ βββββββ
βββββββββββββββββ
βββ βββββββββββ
βββ βββββββββββ
ββββββββββββ βββ
βββββββ βββ βββ
*/
const migration = new Migration(migrationConfig);
await migration.latest();
}
Read more about the input given to migrateLatest
below
interface MigrationConfig {
/**
* Name of the index in which the migrations
* have to be stored/read.
*/
indexName: string;
/**
* (!!OPTIONAL)
* Name of the index in which the migration lock
* has to be stored.
* If not provided, will be created as
* indexName + '_lock'
*/
lockIndexName?: string;
/**
* Relative path to the directory in which migration
* files are present
*/
directory: string;
/**
* Elasticsearch Node.js client instance
*/
client: Client;
/**
* Timeout to wait until the migration
* lock is released.
* Default: 60000
*/
migrationLockTimeout?: number;
/**
* Flag to disable validation if already executed migrations
* are present in the source directory or not
* Default: false
*/
disableMigrationsValidation?: boolean;
}
db/es-migrations/01-create-index.js
Example Migration file
/**
* @param {import('elasticsearch-migrate').MigrateFnInput}
*/
export async function migrate({ client }) {
await client.indices.create({
index: 'new_index',
body: {
mappings: {
properties: {}
}
}
});
}
import { MigrateFnInput } from 'elasticsearch-migrate';
export async function migrate({ client }: MigrateFnInput) {
await client.indices.create({
index: 'new_index',
body: {
mappings: {
properties: {}
}
}
});
}
The exported function must be named migrate
for the migrations to run.
Once the above function is executed succesfully (as-in the Promise is resolved without error), a record with the filename will be inserted in documents_migrations
, and will be skipped execution in future as it is present in the index.
Error | Reason |
---|---|
MigrateFunctionImportFailedError | If importing migrate function from migration file fails. Only CommonJS modules supported as of now. |
MigrationLockTimedOutError | If the migrationLockTimeout value is exceeded. Default: 60000 ms. If you don't have muliple instances and still the issue araises, try releasing the lock manually |
MigrationFileMissingError | If a record with migration file name exists in migrations index, but not in the given migrations directory. Check if you have accidentally deleted a file in the directory |
MigrationRunFailedError | If an error is thrown by a migrate function while running it. Use try/catch blocks to narrow down the issue. |
Inspired by knex-migrations (https://knexjs.org/#Migrations).
FAQs
Tool to run migrations to create/modify Elasticsearch index mapping.
The npm package elasticsearch-migrate receives a total of 110 weekly downloads. As such, elasticsearch-migrate popularity was classified as not popular.
We found that elasticsearch-migrate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.Β It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.