🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

es-mapping-ts

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

es-mapping-ts

ES Mapping TypeScript

0.0.13
Source
npm
Version published
Maintainers
1
Created
Source

Es Mapping TS

GitHub version Build Status Coverage Status

This library is used to generate elasticsearch mapping through typescript decorators

Installation

npm install es-mapping-ts --save

Examples

Create the mapping

import { EsEntity, EsField } from 'es-mapping-ts';
import { ObjectEntity } from './object.entity';
import { NestedEntity } from './nested.entity';

@EsEntity({
  index: 'master',
  type: 'masterType'
})
export class MasterEntity {

  @EsField({
    type : 'text'
  })
  name?: string;

  @EsField({
    type: 'text',
    copy_to : 'name'
  })
  firstname: string;

  @EsField({
    type: 'text',
    copy_to : 'name'
  })
  lastname: string;

  @EsField({
    type: 'join',
    relations: { 'master': 'submaster' }
  })
  master: Array<MasterEntity>;

  @EsField({
    type: 'object',
    fieldClass: ObjectEntity
  })
  objects: Array<MasterEntity>;

  @EsField({
    type: 'nested',
    fieldClass: NestedEntity
  })
  nested: Array<NestedEntity>;
}
import { EsEntity, EsField } from 'es-mapping-ts';

@EsEntity({
  index: 'nested'
})
export class NestedEntity {

  @EsField({
    type: 'text',
  })
  name: string;

  @EsField({
    type: 'integer'
  })
  montant: number;
}
import { EsEntity, EsField } from 'es-mapping-ts';

// This es entity is only here for field mapping,
// it's not supposed to have is own index
@EsEntity()
export class ObjectEntity {

  @EsField({
    type: 'text',
    analyzer : 'whitespace'
  })
  name: string;

  @EsField({
    type: 'integer',
  })
  age: number;
}

Get the generated mappings

Simply call the "uploadMappings" function

import { EsMappingService } from 'es-mapping-ts';
import { Client } from 'elasticsearch';

const esClient = new Client({
  host: 'http://localhost:9200',
  log : 'info'
});

// Upload the mapping
const mappings = EsMappingService.getInstance().uploadMappings(esClient);

only none readonly entity will be uploaded

or do it yourself

import { EsMappingService } from 'es-mapping-ts';

//List of ready to use generated mapping
const mappings = EsMappingService.getInstance().getMappings();

Bluebird.each(mappings, async (mapping) => {
    //create index
    await esclient.indices.create({ index: mapping.index  });

    //create mapping
    await esclient.indices.putMapping(mapping);
});

Using mixins

You can add mixins to your entities by declaring an entity like so:

@EsEntity({ mixins: [BaseMixin] })
export class SomeEntity {
   @EsField({
        type: "text",
    })
    name: string;
}

The mixin class looks like:

@EsEntity()
export class BaseMixin {
    @EsField({
       type: "keyword"
    })
    id: string;

    @EsField({
        name: "start_date",
        type: "date"
    })
    startDate: Date;

    @EsField({
        name: "end_date",
        type: "date"
    })
    endDate: Date;
}

SomeClass will now have a mapping like:

{
    "body": {
        "properties": {
            "name": {
                "type": "text",
            },
            "id": {
                "type": "keyword",
            },
            "start_date": {
                "name": "start_date",
                "type": "date",
            },
            "end_date": {
                "name": "end_date",
                "type": "date",
            },
        }
    }
}

Decorators

@EsEntity

ParamTypeDescription
indexstringAllow you to define the index name
typestringAllow you to define the index type
readonlybooleanDefine if the mapping must be uploaded when using uploadMappings function
mixinsArrayAllow you to compose with one or more EsEntities, see "Using mixins"

@EsField

ParamTypeDescription
typestringAllow you to define the type of the index
namestringAllow you to define the name of the property if different from the property name
dynamicbooleanAllow you to define if the field can accept additional properties
analyzerstringAllow you to define the elasticsearch analyzer
fieldsstringAllow you to define the elasticsearch fields
formatstringAllow you to define the format (ie for date field)
enabledbooleanAllow you to enable ou disable the field
null_valuestringAllow you to define the null value of the field
copy_tostringAllow you to copy the field value into a group field for _search
relationsstringDefine the releation for a join type
fieldClassstringClass used to get the properties of the nested or object array type

Additional properties are allowed, allowing you to manage other elasticsearch properties

License

MIT

Keywords

inversify

FAQs

Package last updated on 16 Aug 2018

Did you know?

Socket

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.

Install

Related posts