npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

es-mapping-ts

v1.0.1

Published

Eleasticsearch mapping manager typescript

Downloads

20,063

Readme

Es Mapping TS

GitHub version GitHub version Build Status Coverage Status

This library is used to generate elasticsearch mapping through typescript decorators

Installation

npm install es-mapping-ts --save

Peer dependencies

This package only work with @elastic/elasticsearch

Version

tested with

  • elasticsearch 7
  • elasticsearch 6
  • elasticsearch 5

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 '@elastic/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);
});

Inheritance

You can also extend EsMapping

export class AbstractEntity {

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

  @EsField({
    type: 'text',
  })
  overridableName: string;
}
@EsEntity({
  index: 'concret',
  type: 'typeConcret'
})
export class ConcretEntity extends AbstractEntity {

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


  @EsField({
    type: 'text',
    null_value : 'undefined'
  })
  overridableName: string;
}

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

| Param | Type | Description | | ------ | ------ | ------ | | index | string | Allow you to define the index name | | type | string | Allow you to define the index type | | readonly | boolean | Define if the mapping must be uploaded when using uploadMappings function | | mixins | Array | Allow you to compose with one or more EsEntities, see "Using mixins" |

@EsField

| Param | Type | Description | | ------ | ------ | ------ | | type | string | Allow you to define the type of the index | | name | string | Allow you to define the name of the property if different from the property name | | dynamic | boolean | Allow you to define if the field can accept additional properties | | analyzer | string | Allow you to define the elasticsearch analyzer | | fields | string | Allow you to define the elasticsearch fields | | format | string | Allow you to define the format (ie for date field) | | enabled | boolean | Allow you to enable ou disable the field | | null_value | string | Allow you to define the null value of the field | | copy_to | string | Allow you to copy the field value into a group field for _search | | relations | string | Define the releation for a join type | | fieldClass | string | Class used to get the properties of the nested or object array type |

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

How to dev

test en build

# launch unit testing
npm run test 

# build
npm run build

This project is also managed by travis for CI

release

npm run build
npm run test

mkdir release
cp -r dist release
cp LICENSE release
cp README.md release
cp package.json release #change version

cd relese
npm publish

License


MIT