Skip to content

Commit

Permalink
feat: Generate elasticsearch index (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickredmark authored and buehler committed Apr 5, 2018
1 parent 3cdf430 commit c1c8879
Show file tree
Hide file tree
Showing 28 changed files with 697 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "{build} - {branch}"
skip_tags: true
skip_branch_with_pr: true

environment:
matrix:
- nodejs_version: "9"

install:
- ps: Install-Product node $env:nodejs_version
- npm install

test_script:
- npm test

build: off
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Logs
logs
*.log
npm-debug.log*

# Dependency directory
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# Typescript stuff
build
coverage

# Generated files
*.d.ts
*.js
21 changes: 21 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Logs
logs
*.log
npm-debug.log*

# Dependency directory
node_modules

# Typescript stuff
build
coverage

*.ts
!*.d.ts
test

jest.json
.travis.yml
tsconfig.json
tslint.json
config/
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
29 changes: 29 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
language: node_js

stages:
- name: test
if: tag IS blank
- name: deploy
if: branch = master AND type != pull_request

notifications:
email: false

jobs:
include:
- stage: test
node_js: '9'
after_success:
- npm install coveralls@^2.11.9 && cat ./coverage/lcov.info | coveralls
- stage: deploy
node_js: '9'
script: npm run typedoc
deploy:
provider: pages
skip_cleanup: true
github_token: $GH_TOKEN
local_dir: ./docs
- stage: deploy
node_js: '9'
before_script: npm run build
script: npm run semantic-release
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 smartive

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Annotate your model and generate an elasticsearch index template.

[![Build Status](https://travis-ci.org/smartive/es-model.svg)](https://travis-ci.org/smartive/es-model)
[![Build Status](https://ci.appveyor.com/api/projects/status/7m14ruqjaopwbfn4?svg=true)](https://ci.appveyor.com/project/buehler/es-model)
[![npm](https://img.shields.io/npm/v/es-model.svg?maxAge=3600)](https://www.npmjs.com/package/es-model)
[![Coverage status](https://img.shields.io/coveralls/smartive/es-model.svg?maxAge=3600)](https://coveralls.io/github/smartive/es-model)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![Greenkeeper badge](https://badges.greenkeeper.io/smartive/es-model.svg)](https://greenkeeper.io/)

## Installation

To install this package, simply run

[![NPM](https://nodei.co/npm/es-model.png?downloads=true&stars=true)](https://nodei.co/npm/es-model/)

## Changelog

The changelog is generated by [semantic release](https://github.com/semantic-release/semantic-release) and is located under
the [release section](https://github.com/smartive/es-model/releases).

## Licence

This software is licenced under the [MIT](LICENSE) licence.
24 changes: 24 additions & 0 deletions config/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"removeComments": false,
"outDir": "../build",
"rootDir": "../src",
"declaration": true,
"sourceMap": false,
"importHelpers": true,
"strict": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"lib": [
"es2015"
]
},
"include": [
"../src/**/*"
]
}
6 changes: 6 additions & 0 deletions config/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"outDir": "../"
}
}
18 changes: 18 additions & 0 deletions jest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"collectCoverage": true,
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testMatch": [
"**/test/**/*.spec.ts"
],
"testPathIgnorePatterns": [
"/node_modules/"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
]
}
48 changes: 48 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@smartive/es-model",
"version": "0.0.0-development",
"description": "Annotate your datastructure to generate an elasticsearch index template",
"main": "index.js",
"typings": "index.d.ts",
"scripts": {
"clean": "del-cli ./build ./coverage",
"build": "npm run clean && tsc -p ./config/tsconfig.build.json",
"develop": "npm run clean && tsc -p .",
"lint": "tslint -c ./tslint.json -p ./config/tsconfig.build.json",
"test": "npm run lint && npm run clean && jest -c ./jest.json",
"test:watch": "npm run clean && jest -c ./jest.json --watch",
"typedoc": "del-cli ./docs/api && typedoc --ignoreCompilerErrors --out ./docs/api --mode file --tsconfig ./config/tsconfig.build.json --theme ./docs/typedoc-theme --readme none ./src/",
"semantic-release": "semantic-release"
},
"engines": {
"node": ">=9",
"npm": "*"
},
"repository": {
"type": "git",
"url": "https://github.com/smartive/es-model.git"
},
"keywords": [
"elasticsearch"
],
"author": "Nicola Marcacci Rossi <nicola@smartive.ch>",
"license": "MIT",
"bugs": {
"url": "https://github.com/smartive/es-model/issues"
},
"homepage": "https://github.com/smartive/es-model#readme",
"dependencies": {},
"devDependencies": {
"@smartive/tslint-config": "^3.0.1",
"@types/jest": "^22.0.1",
"del-cli": "^1.1.0",
"jest": "^22.1.1",
"semantic-release": "^12.2.2",
"ts-jest": "^22.0.1",
"tslint": "^5.9.1",
"tslint-config-airbnb": "^5.4.2",
"tsutils": "^2.18.0",
"typedoc": "^0.10.0",
"typescript": "^2.8.0"
}
}
5 changes: 5 additions & 0 deletions src/decorators/EsField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Field } from '../models/Mapping';
import { generatePropertyDecorator } from '../utils/decorators';

// tslint:disable-next-line:variable-name - This is a decorator
export const EsField = generatePropertyDecorator<Field>('fields');
5 changes: 5 additions & 0 deletions src/decorators/EsMapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { MappingMeta } from '../models/Mapping';
import { generateObjectDecorator } from '../utils/decorators';

// tslint:disable-next-line:variable-name - This is a decorator
export const EsMapping = generateObjectDecorator<MappingMeta>();
5 changes: 5 additions & 0 deletions src/decorators/EsProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Field } from '../models/Mapping';
import { generatePropertyDecorator } from '../utils/decorators';

// tslint:disable-next-line:variable-name - This is a decorator
export const EsProperty = generatePropertyDecorator<Field>('properties');
5 changes: 5 additions & 0 deletions src/decorators/EsSubMapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { SubMappingMeta } from '../models/Mapping';
import { generateObjectDecorator } from '../utils/decorators';

// tslint:disable-next-line:variable-name - This is a decorator
export const EsSubMapping = generateObjectDecorator<SubMappingMeta>();
4 changes: 4 additions & 0 deletions src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './EsField';
export * from './EsProperty';
export * from './EsMapping';
export * from './EsSubMapping';
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'reflect-metadata';

export * from './decorators';
export * from './utils/template';
27 changes: 27 additions & 0 deletions src/models/Mapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export type SimpleFieldType = 'text' | 'keyword' | 'date' | 'long' | 'double' | 'boolean' | 'ip';
export type ObjectFieldType = 'object' | 'nested';
export type SpecializedFieldType = 'geo_point' | 'geo_shape' | 'completion';

export type Field = {
type?: SimpleFieldType | SpecializedFieldType | Function;
search_analyzer?: 'suggestion_query_analyzer';
analyzer?: 'suggestion_analyzer' | 'default_analyzer';
format?: 'date' | 'hour_minute';
};

export type Fields = {
[name: string]: Field,
};

export type MappingMeta = {
name?: string;
dynamic?: 'strict';
_source?: {
enabled: boolean;
};
};

export type SubMappingMeta = {
type?: SimpleFieldType | ObjectFieldType;
include_in_parent?: boolean;
};
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Mapping';
16 changes: 16 additions & 0 deletions src/utils/decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getMetadata, setMetadata } from './metadata';

export function generateObjectDecorator<T>(): (data?: T) => ClassDecorator {
return (data?: T): ClassDecorator => (target: any) => setMetadata(target.prototype, data);
}

export function generatePropertyDecorator<T>(key: string): (data?: T) => PropertyDecorator {
return (data?: T): PropertyDecorator => (target: Object, name: string | symbol) => {
let mapping = getMetadata<{ [key: string]: T | undefined }>(target, key);
if (!mapping) {
mapping = {};
setMetadata(target, mapping, key);
}
mapping[name] = data;
};
}
30 changes: 30 additions & 0 deletions src/utils/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const KEY = 'es-model';

export function setMetadata(target: any, metadata: any, key?: string): void {
if (key) {
Reflect.defineMetadata(
KEY,
metadata,
target,
key,
);
} else {
Reflect.defineMetadata(
KEY,
metadata,
target,
);
}
}

export function getMetadata<T>(target: any, key?: string): T {
if (key) {
return Reflect.getMetadata(KEY, target, key);
}

return Reflect.getMetadata(KEY, target);
}

export function getPrimitiveType(objectType: Function, key: string): Function | null {
return Reflect.getMetadata('design:type', objectType, key);
}
Loading

0 comments on commit c1c8879

Please sign in to comment.