Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jbdemonte committed Apr 21, 2018
0 parents commit 2606ec7
Show file tree
Hide file tree
Showing 35 changed files with 5,984 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Output directory
dist

# Coverage directory
coverage

# Dependency directories
node_modules

# Logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# System
.DS_Store

# IDE Files
.idea
.vscode

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
39 changes: 39 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Source directory
src

# Coverage directory
coverage

# Dependency directories
node_modules

# Logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# System
.DS_Store

# IDE Files
.idea
.vscode

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# git
.gitignore

# useless files for npm
docker-compose.yml
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parser": "typescript",
"singleQuote": true,
"trailingComma": "all",
"printWidth": 150
}
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js

node_js:
- "9"
- "8"

script:
- yarn coverage

after_success: cat coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

## [Unreleased]

## [1.0.0] 2018-04-21

- Initial version
196 changes: 196 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
# @gojob/ts-elasticsearch

[![travis build](https://img.shields.io/travis/gojob-1337/node-ts-elasticsearch.svg)](https://travis-ci.org/gojob-1337/node-ts-elasticsearch)
[![Coverage Status](https://coveralls.io/repos/github/gojob-1337/node-ts-elasticsearch/badge.svg?branch=master)](https://coveralls.io/github/gojob-1337/node-ts-elasticsearch?branch=master)

## Description

The purpose of this library is to provide a decorated class approch to use the [elasticsearch](https://www.npmjs.com/package/elasticsearch) module.

## Installation

```
yarn add @gojob/ts-elasticsearch
```

## Usage example

```typescript
import { Field, Elasticsearch, Primary } from '@gojob/ts-elasticsearch';

@Index()
class User {
@Primary()
@Field({ enabled: false})
id: string;

@Field('text') name: string;
@Field('integer') age: number;
}

// ...

const elasticsearch = new Elasticsearch({ host: 'http://192.168.99.100:9200' });

await elasticsearch.indices.create(User);
await elasticsearch.indices.putMapping(User);

const user = new User();
user.id = 'agent-1122';
user.name = 'Smith';
user.age = 33;

await elasticsearch.index(user);

await elasticsearch.indices.refresh(User);

const { documents } = elasticsearch.search(User, { body: { query: { match_all: {} } } });


```

## Documentation

### Decorators

#### decorator `@Index`

Keep in mind that index [types are scheduled to be removed in Elasticsearch 8](https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-put-mapping.html#_skipping_types_2).

This class decorator factory declares a new Elasticsearch index.

By default, it uses the class name as index (and type), but it can be overwritten.

Index settings may be added in the `settings` optional parameter.

```typescript
@Index({index: 'twt', settings: { number_of_shards: 3 } })
class Tweeter {}
```


#### decorator `@Primary`

This property decorator factory declares the class primary key which will be used as `_id` in Elasticsearch index.
When not provided, Elasticsearch generates ids by it own.

```typescript
@Index()
class User {
@Primary()
@Field({ enabled: false})
id: string;

@Field('text') name: string;
@Field('integer') age: number;
}
```


#### decorator `@Field`

This property decorator factory declares a new property in the current class.

It takes either a simple type as string or an elasticsearch mapping setting.

```typescript
@Index()
class User {
@Field('keyword') id: string;
@Field({ type: 'text', boost: 2 })name: string;
@Field('integer') age: number;
}
```

##### Special case of object or nested (array of object)

When dealing with object or nested, you have to declare a class with some fields declared.
Notice in this case, the class in not decorated with `@Index`.

Object example:

```typescript
class Country {
@Field('text') name: string;
}

@Index()
class City {
@Field('text') name: string;
@Field({ object: Country })
country: Country;
}
```

Nested example:

```typescript
class Follower {
@Field({ enabled: false}) name: string;
@Field({ enabled: false}) popularity: number;
}

@Index()
class User {
@Primary()
@Field({ enabled: false})
id: string;

@Field('text') name: string;

@Field({ nested: Follower })
followers: Follower[];
}
```

### Elasticsearch class

This class provides main elasticsearch features directly usable with indexed classes.

__The purpose of this library is not to override all official plugin features, but only those that are relevant for managing documents using classes.__

When dealing with documents, you can either uses indexed class instances or literal object associated to their indexed class.

The examples presents in this chapter are based on this setup.

```typescript
import { Field, Elasticsearch, Primary } from '@gojob/ts-elasticsearch';

const elasticsearch = new Elasticsearch({ host: 'http://192.168.99.100:9200' });

@Index()
class User {
@Primary()
@Field({ enabled: false})
id: string;

@Field('text') name: string;
@Field('integer') age: number;
}
```

#### Elasticsearch core

Instanciate this class with the [client configuration option](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html).

The main class provides the main part of the [document API](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/docs.html) functions.

- bulkIndex: Index an arary or a stream of documents ([bulk api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-bulk)
- count: Count documents in the index ([count api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-count))
- create: Add a new document to the index [create api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-create))
- delete: Delete a document from the index [delete api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-delete))
- get: Retrieve a document by its id [get api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-get))
- index: Add or update a document in the index [index api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-index))
- search: Search documents in the index [search api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-search))
- update: Update a document [update api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-update))

#### Elasticsearch.indices

The indices provides the main part of the [document API](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/indices.html) functions.

- create: Create an Index ([indice create api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-indices-create))
- delete: Delete an Index ([indice delete api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-indices-delete))
- exists: Check if an Index exists ([indice exists api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-indices-exists))
- flush: Flush an Index ([indice flush api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-indices-flush))
- putMapping: Put mapping of an Index ([indice putmapping api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-indices-putmapping))
- refresh: Refresh an Index ([indice refresh api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-indices-refresh))
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: "3.2"
services:

kibana:
image: docker.elastic.co/kibana/kibana:6.2.3
environment:
- LOGGING_QUIET=true
- ELASTICSEARCH_URL=http://${ELASTICSEARCH_HOST}:${ELASTICSEARCH_PORT}
ports:
- 5601:5601

elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.2.3
environment:
- discovery.type=single-node
volumes:
- esdata:/usr/share/elasticsearch/data
ports:
- ${ELASTICSEARCH_PORT}:9200

volumes:
esdata:
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('ts-node').register();
require('./src/main');
19 changes: 19 additions & 0 deletions jest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": ".*\\.test\\.ts$",
"moduleFileExtensions": [
"ts",
"js",
"json"
],
"collectCoverageFrom" : [
"src/**/*.ts",
"!**/node_modules/**",
"!*.test.ts"
],
"coverageReporters": ["json", "lcov"],
"coverageDirectory": "coverage",
"verbose": true
}
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@gojob/ts-elasticsearch",
"version": "1.0.0",
"license": "MIT",
"description": "Elasticsearch decorated by TypeScript",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/gojob-1337/node-ts-elasticsearch"
},
"keywords": [
"elasticsearch",
"typescript",
"decorator"
],
"pre-commit": [
"lint"
],
"scripts": {
"build": "tsc",
"lint": "tslint -c ./tslint.json -p ./tsconfig.json -t verbose",
"start": "echo 'Not runable' && exit 1",
"test": "jest --silent=false --config=jest.json",
"coverage": "yarn test --coverage"
},
"devDependencies": {
"@gojob/tslint-config": "^0.0.3",
"@types/elasticsearch": "^5.0.22",
"@types/jest": "^22.2.2",
"@types/node": "^7.0.60",
"coveralls": "^3.0.0",
"jest": "^22.4.3",
"prettier": "^1.11.1",
"ts-jest": "^22.4.2",
"ts-node": "^5.0.1",
"tslint": "^5.9.1",
"tslint-config-prettier": "^1.10.0",
"tslint-plugin-prettier": "^1.3.0",
"typescript": "^2.8.1"
},
"dependencies": {
"elasticsearch": "^14.2.2",
"reflect-metadata": "^0.1.12"
}
}
Loading

0 comments on commit 2606ec7

Please sign in to comment.