{"id":15885796,"url":"https://github.com/toniov/es-fixtures","last_synced_at":"2026-06-15T04:01:19.477Z","repository":{"id":57227023,"uuid":"81793871","full_name":"toniov/es-fixtures","owner":"toniov","description":"esfix - Simple fixture loading for Elasticsearch on Node.js","archived":false,"fork":false,"pushed_at":"2018-07-31T01:27:47.000Z","size":79,"stargazers_count":21,"open_issues_count":2,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-10-06T23:13:25.591Z","etag":null,"topics":["bootstrap","bulk","callback","cli","command-line-tool","elasticsearch","fixture-loading","fixtures","javascript","mock-data","nodejs","promise","promises","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/toniov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-13T06:47:38.000Z","updated_at":"2025-02-20T17:11:24.000Z","dependencies_parsed_at":"2022-09-12T16:52:19.398Z","dependency_job_id":null,"html_url":"https://github.com/toniov/es-fixtures","commit_stats":null,"previous_names":["antonvs2/es-fixtures"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/toniov/es-fixtures","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toniov%2Fes-fixtures","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toniov%2Fes-fixtures/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toniov%2Fes-fixtures/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toniov%2Fes-fixtures/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/toniov","download_url":"https://codeload.github.com/toniov/es-fixtures/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toniov%2Fes-fixtures/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34346870,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-15T02:00:07.085Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bootstrap","bulk","callback","cli","command-line-tool","elasticsearch","fixture-loading","fixtures","javascript","mock-data","nodejs","promise","promises","unit-testing"],"created_at":"2024-10-06T05:07:56.152Z","updated_at":"2026-06-15T04:01:19.457Z","avatar_url":"https://github.com/toniov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Elasticsearch Fixtures\n\n\u003e Simple fixture loading for Elasticsearch on Node.js. \n\n[![Build Status](https://travis-ci.org/toniov/es-fixtures.svg?branch=master)](https://travis-ci.org/toniov/es-fixtures)\n\nClear and load fixtures easily in your Elasticsearch instance, useful for unit Testing or other kind of testing.\n\nTested in Elasticsearch 0.x, 1.x, 2.x and 5.x.\n\n## Contents\n\n- [Command-line Interface](#command-line-interface)\n  - [Examples](#examples)\n  - [Installation](#installation)\n  - [API](#api)\n    - [load](#load)\n    - [clear](#clear)\n    - [clearAndLoad](#clearandload)\n    - [bulk](#bulk)\n    - [createIndex](#createindex)\n    - [addMapping](#addmapping)\n    - [help](#help)\n- [Used as module](#used-as-module)\n  - [Installation](#installation-1)\n  - [API](#api-1)\n    - [bootstrap](#bootstrapindex-type-config)\n    - [Instance methods](#instance-methods)\n      - [load](#loaddata-options-callback)\n      - [clear](#clearcallback)\n      - [clearAndLoad](#clearandloaddata-options-callback)\n      - [bulk](#bulkdata-options-callback)\n      - [createIndex](#createindexdata-callback)\n      - [addMapping](#addmappingdata-callback)\n\n# Command-line Interface\n\nIt can be used out-of-box without any prior configuration, following examples run in `localhost:9200` by default.\n\n## Examples\n\nLoad fixtures using a .json file:\n\n\u003cpre\u003e\n\u003cb\u003efixtures.json\u003c/b\u003e\n\n[{\n  \"name\": \"Dio\",\n  \"standName\": \"The World\",\n  \"age\": 122\n},{\n  \"name\": \"Jotaro\",\n  \"standName\": \"Star Platinum\",\n  \"age\": 17\n}]\n\u003c/pre\u003e\n```\n$ esfix load my_index my_type fixtures.json\n```\n\nClear all the data in `my_index`/`my_type`:\n\n```\n$ esfix clear my_index my_type\n```\n\nBoth `clear` and `load` commands executed at once:\n\n```\n$ esfix clearAndLoad my_index my_type fixtures.json\n```\n\nPerform index/delete operations using the bulk API. The next command will index two documents, update one and delete another one:\n\n\u003cpre\u003e\n\u003cb\u003efixtures-bulk.json\u003c/b\u003e\n\n[\n { \"index\":  { \"_id\": 1 } },\n { \"title\": \"Random title 1\", \"summary\": \"Random summary 1\" },\n { \"index\":  { \"_id\": 2 } },\n { \"title\": \"Random title 2\", \"summary\": \"Random summary 2\" },\n { \"update\": { \"_id\": 3 } },\n { \"doc\": { \"title\": \"New title\" } },\n { \"delete\": { \"_id\": 4 } }\n]\n\u003c/pre\u003e\n\n```\n$ esfix bulk my_index my_type fixtures-bulk.json\n```\n\n## Installation\n\nInstall it as a global module if only the CLI is going to be used:\n\n```\nnpm install esfix -g\n```\n\n## API\n\n**`esfix \u003ccmd\u003e [--host \u003chost\u003e] [--log \u003clog-type\u003e]`**\n\nBy default, `--host` is set `localhost:9200` and `--log` is set to `info`. Log levels are `warn`, `info`, `debug` and `trace`.\n\n\n### load\n\n**`load \u003cindex\u003e \u003ctype\u003e \u003cdata-file\u003e [-i] [-r]`**\n\nIndex documents.\n\nBy default, the documents will be inserted with a random ID set by Elasticsearch.\nIf the document includes an `_id` field, it will be used as the document ID (not compatible with option `-i`).\nThe bulk API is used under the hood.\n\n`-i, --incremental` assign an incremental id starting from 1\n\n`-r, --refresh` force a refresh each time an operation is executed\n\n***Examples***\n\n\nIndex documents with an incremental id:\n\n```\n$ esfix load my_index my_type fixtures.json -i\n```\n\nIndex documents using a `.js` file, this allows to insert some relative values like dates or other kind of logic:\n\n\u003cpre\u003e\n\u003cb\u003efixtures.js\u003c/b\u003e\n\nmodule.exports = [{\n  name: 'Dio',\n  standName: 'The World',\n  age: 122,\n  created: new Date() // current date\n},{\n  name: 'Jotaro',\n  standName: 'Star Platinum',\n  age: 17,\n  created: new Date() // current date\n}];\n\u003c/pre\u003e\n```\n$ esfix load my_index my_type fixtures.json\n```\n\n### clear\n\n**`clear \u003cindex\u003e [type] [-r]`**\n\nDelete all the documents from the specified index. If `type` is specified, it will only delete all the documents included in that type.\n\n`-r, --refresh` force a refresh each time an operation is executed\n\n**Example**\n\nClear index `my_index`:\n\n```\n$ esfix clear my_index\n```\n\n### clearAndLoad\n\n**`clearAndLoad \u003cindex\u003e \u003ctype\u003e \u003cdata-file\u003e [-i] [-r]`**\n\nFirst executes `clear` command and then `load` command. Same functionality as them.\n\n`-i, --incremental` assign an incremental id starting from 1\n`-r, --refresh` force a refresh each time an operation is executed\n\n**Example**\n\nClear all the data in `my_index`/`my_type` and load the data in `fixtures.json`:\n\n```\n$ esfix clearAndLoad my_index my_type fixtures.json\n```\n\n### bulk\n\n**`bulk [index] [type] \u003cdata-file\u003e [-r]`**\n\nPerform bulk index/delete operations.\n\nThe format of `data-file` is an array of:\n\n```\naction_and_meta_data\noptional_source\n```\n\nThe possible actions are `index`, `create`, `delete` and `update`. More information about bulk API [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html).\n\n`index` and `type` are optional, they set default index and type for items which don’t provide ones.\n\n`-r, --refresh` force a refresh each time an operation is executed\n\n**Examples**\n\nIndex two documents in two different indexes:\n\n\u003cpre\u003e\n\u003cb\u003efixtures.json\u003c/b\u003e\n\n[\n { \"index\":  { \"_index\": \"my_index\", \"_type\": \"my_type\",  \"_id\": 1 } },\n { \"name\": \"Yukko\", \"occupation\": \"student\" },\n { \"index\":  { \"_index\": \"my_index2\", \"_type\": \"my_type\",  \"_id\": 1 } },\n { \"name\": \"Sakamoto\", \"occupation\": \"mascot\" }\n]\n\u003c/pre\u003e\n\n```\n$ esfix bulk fixtures.json\n```\n\nDelete and update documents in index `my_index`, `delete` does not expect a following line:\n\n\u003cpre\u003e\n\u003cb\u003efixtures.json\u003c/b\u003e\n\n[\n { \"delete\": { \"_id\": 1 } }\n { \"delete\": { \"_id\": 2 } }\n { \"update\": { \"_id\": 3 } },\n { \"doc\": { \"occupation\": \"Pole dancer\" } },\n]\n\u003c/pre\u003e\n\n```\n$ esfix bulk fixtures.json\n```\n\nUsing a .js file we can, for example, index 1000 documents with a random `age` field with a value between 18 and 60:\n\n```js\n// generated-fixtures.js\nmodule.exports = (() =\u003e {\n  const bulkData = [];\n  for (let i = 0; i \u003c 1000; i++) {\n    bulkData.push({ index: { } });\n    bulkData.push({ name: `User ${i}`, age: Math.floor(Math.random() * 60) + 18 });\n  }\n  return bulkData;\n})();\n```\n\n```\n$ esfix bulk my_index my_type generated-fixtures.js\n```\n\n### createIndex\n\n**`createIndex \u003cindex\u003e [data-file] [-f]`**\n\nCreate an index.\n\n`data-file` is optional, `settings` and `mappings` can be specified there, the format is detailed [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html#mappings).\n\n`-f, --force` will delete the index if it exists, and it will be re-created\n\n**Example**\n\nDelete if exists, and create an index with the following `settings` and `mappings`:\n\n\u003cpre\u003e\n\u003cb\u003eindex-info.json\u003c/b\u003e\n\n{\n  \"settings\" : {\n    \"number_of_shards\" : 1\n  },\n  \"mappings\" : {\n    \"my_type\" : {\n      \"properties\" : {\n        \"age\" : { \"type\" : \"short\" }\n      }\n    }\n  }\n}\n\u003c/pre\u003e\n\n```\n$ esfix createIndex my_index index-info.json -f\n```\n\n### addMapping\n\n**`addMapping \u003cindex\u003e \u003ctype\u003e \u003cdata-file\u003e`**\n\nAdd mapping to a type. \nIndex must exists in order to work.\n`data-file` format is specified [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html):\n\n**Example**\n\nAdd mapping to index `my_index` and type `my_type`\n\n\u003cpre\u003e\n\u003cb\u003emapping-info.json\u003c/b\u003e\n\n{\n  \"properties\": {\n    \"age\": {\n      \"type\": \"byte\"\n    }\n  }\n}\n\u003c/pre\u003e\n\n```\n$ esfix addMapping my_index my_type mapping-info.json\n```\n\n### help\n\n**`help`**\n\nShow help page.\n\n# Used as module\n\nIt can be required in your code, useful to automate unit tests.\n\nAll the methods from the CLI can be used here, but there are some small differences. In the CLI, by default, after each operation the index is not refreshes, but when used as a module it is refreshed everytime; for unit testing usually we need the data without any lag.\n\nAll methods return a Promise if a callback is not specified.\n\n## Examples\n\nIndex two documents with an incremental id:\n\n```js\nconst esfix = require('esfix');\nconst loader = esfix.bootstrap('my_index', 'my_type');\n\nconst data = [{\n  name: 'Jotaro',\n  standName: 'Star Platinum'\n}, {\n  name: 'Jolyne',\n  standName: 'Stone Free'\n}];\n\nconst options = {\n  incremental: true\n};\n\nloader.load(data, options)\n  .then(() =\u003e {\n    console.log('Documents were indexed');\n  })\n  .catch(err =\u003e {\n    // error handling\n  });\n```\n\nPrepare a fresh index, for example, to execute a Unit Test:\n\n```js\nconst esfix = require('esfix');\nconst loader = esfix.bootstrap('users', 'user');\n\nconst indexInfo = require('./fixtures/users/index-info.json');\nconst userData = require('./fixtures/users/data.json');\n\nloader.createIndex(indexInfo, { force: true })\n  .then(() =\u003e loader.load(userData))\n  .then(() =\u003e {\n    console.log('Documents were indexed');\n  })\n  .catch(err =\u003e {\n    // error handling\n  });\n```\n\n## Installation\n\n```\nnpm install esfix\n```\n\n## API\n\n### bootstrap(index, type, config)\n\nReturns a new Loader instance, configured to interact by default with the specified `index` and `type`.\n\n`config` parameter is optional, by default it will contain `{ host: 'localhost:9200' }`.\nA list of available options is specified in the driver official [documentation](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html).\n\n```js\nconst loader = require('esfix').bootstrap('my_index', 'my_type');\n```\n\n```js\nconst loader = require('esfix').bootstrap('my_index', 'my_type', {\n  host: 'http://foo.bar:0000',\n  log: 'trace'\n});\n```\n\n### Instance methods\n\n#### load(data, [options], [callback])\n\nAdd documents into the specified index and type. \n\n`data` contains an array of objects the documents to add to the index. It assigns a random `_id` (Elasticsearch default behaviour).\n\n`options` is optional. \n`options.incremental: true` insert documents assigning an incremental `_id` from 1 instead of a random one. It will overwrite existent documents with the same `_id`. \n`options.noRefresh: true` avoid refreshing the index after each operation.\n\n`callback` is optional, in case it is not set a `Promise` is returned.\n\n```js\nconst data = [{\n  name: 'Jotaro',\n  standName: 'Star Platinum'\n}, {\n  name: 'Jolyne',\n  standName: 'Stone Free'\n}];\n\nconst options = {\n  incremental: true\n};\n\nloader.load(data, options)\n  .catch(err =\u003e {\n    // error handling\n  });\n```\n\nAlso, it is possible to add the desired `_id` inside each document (can't be used altogether with `incremental: true`).\n\n```js\nconst data = [{\n  _id: 1,\n  name: 'Jotaro',\n  standName: 'Star Platinum'\n}, {\n  _id: 2,\n  name: 'Jolyne',\n  standName: 'Stone Free'\n}];\n\nloader.load(data, options)\n  .catch(err =\u003e {\n    // error handling\n  });\n```\n\n#### clear([callback])\n\nDelete all the documents in the index and type specified when bootstraping. It only deletes the document, the type mapping is kept intact.\n\n`callback` is optional, in case it is not set a `Promise` is returned.\n\n```js\nloader.clear()\n  .catch(err =\u003e {\n    // error handling\n  });\n```\n\n#### clearAndLoad(data, [options], [callback])\n\nDelete all the documents in the index and type specified and load new ones. Basically executes first `.clear()` and then `.load()` (check them to see more details).\n\n`data` contains an array of objects the documents to add to the index. It assigns a random `_id` (Elasticsearch default behaviour).\n\n`options` is optional. \n`options.incremental: true` insert documents assigning an incremental `_id` from 1 instead of a random one. It will overwrite existent documents with the same `_id`. \n`options.noRefresh: true` avoid refreshing the index after each operation.\n\n`callback` is optional, in case it is not set a `Promise` is returned.\n\n```js\nconst data = [{\n  name: 'Josuke',\n  standName: 'Crazy Diamond'\n}, {\n  name: 'Joseph',\n  standName: 'Hermit Purple'\n}];\n\nloader.clearAndLoad(data)\n  .catch(err =\u003e {\n    // error handling\n  });\n```\n\n#### bulk(data, [options], [callback])\n\nPerform many index/delete operations in a single API call using the Elasticsearch bulk API. The possible actions are index, create, delete and update.\n\n`data` is an array of objects, the format is specified [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html). A JSONLines string or Buffer can be used as well.\n\n`options` is optional. \n`options.noRefresh: true` avoid refreshing the index after each operation.\n\n`callback` is optional, in case it is not set a `Promise` is returned.\n\n```js\nconst data = [\n  // action description\n  { index: { _id: 1 } },\n   // the document to index\n  { title: 'foo' },\n  // action description\n  { update: { _id: 2 } },\n  // the document to update\n  { doc: { title: 'foo' } },\n  // action description\n  { delete: { _id: 3 } },\n  // no document needed for this delete\n];\nloader.bulk(data)\n  .catch(err =\u003e {\n    // error handling\n  });\n```\n\n#### createIndex([data], [callback])\n\nDelete index and create it again. \n\n`data` is optional: providing type mappings while recreating the index is possible, as well as other settings, format is specified [here](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/indices-create-index.html).\n\n`callback` is optional, in case it is not set a `Promise` is returned.\n\nFor example, can be useful to get a fresh index with a particular mapping each time a unit test is executed.\n\n```js\nconst data = {\n  mappings: {\n    my_type: {\n      properties: {\n        name: {\n          type: 'string'\n        }\n      }\n    }\n  }\n};\n\nloader.createIndex(data)\n  .catch(err =\u003e {\n    // error handling\n  });\n```\n\n#### addMapping(data, [callback])\n\nProvide a mapping to the specified type when bootstraping. The index must already exist. \n\n`data` format is specified [here](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/indices-put-mapping.html).\n\n`callback` is optional, in case it is not set a `Promise` is returned.\n\n```js\nconst data = {\n  properties: {\n    name: {\n      type: 'string'\n    }\n  }\n};\nloader.addMapping(data)\n  .catch(err =\u003e {\n    // error handling\n  });\n```\n\n## License\n\nMIT © [Antonio V](https://github.com/toniov)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoniov%2Fes-fixtures","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftoniov%2Fes-fixtures","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoniov%2Fes-fixtures/lists"}