Skip to content

Commit

Permalink
feat: Update rxjs (#42)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
- Removed support for rxjs v5, minimum version is rxjs v7
- Removed support for Node.js v6, minimum version is v16
- Removed support for proc-that v1, minimum version is v2
- Removed support for old elasticsearch version, minimum version is v7.x
  • Loading branch information
thilohaas authored Dec 20, 2022
1 parent 1ef105f commit 4ff984e
Show file tree
Hide file tree
Showing 18 changed files with 425 additions and 433 deletions.
19 changes: 0 additions & 19 deletions .appveyor.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@smartive/eslint-config"]
}
23 changes: 23 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Release npm package

on:
push:
branches:
- master

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- uses: actions/setup-node@v3
with:
node-version: '18.x'
- run: npm install
- run: npm run build
- name: semantic release
uses: cycjimmy/semantic-release-action@v3
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Unit Tests
on:
push:
branches:
- master
pull_request:
branches: [master, develop]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x, 18.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Run Tests
run: |
npm install
npm test
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ node_modules
.node_repl_history

# Typescript stuff
build
dist
coverage

package-lock.json
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@smartive/prettier-config"
41 changes: 0 additions & 41 deletions .travis.yml

This file was deleted.

3 changes: 2 additions & 1 deletion config/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"module": "commonjs",
"moduleResolution": "node",
"removeComments": false,
"outDir": "../build",
"outDir": "../dist",
"rootDir": "../src",
"declaration": true,
"sourceMap": false,
"importHelpers": true,
"skipLibCheck": true,
"strictNullChecks": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
Expand Down
3 changes: 0 additions & 3 deletions config/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"outDir": "../"
}
}
2 changes: 1 addition & 1 deletion jest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"collectCoverage": true,
"mapCoverage": true,
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "ts-jest"
},
"testMatch": [
"**/test/**/*.spec.ts"
Expand Down
49 changes: 27 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
"name": "proc-that-elastic-loader",
"version": "0.0.0-development",
"description": "Loader for proc-that. Loads processed items into an elasticsearch index.",
"main": "index.js",
"main": "dist/index.js",
"typings": "dist/index.d.js",
"scripts": {
"clean": "del-cli ./build ./coverage",
"clean": "del-cli ./dist ./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",
"lint": "npm run lint:ts && npm run prettier",
"lint:fix": "npm run lint:ts:fix && npm run prettier:fix",
"lint:ts": "eslint --max-warnings=-1",
"lint:ts:fix": "eslint --max-warnings=-1 --fix",
"prettier": "prettier --config .prettierrc.json --list-different \"./**/*.{ts,tsx}\"",
"prettier:fix": "prettier --config .prettierrc.json --list-different \"./**/*.{ts,tsx}\" --write",
"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 && typedoc --ignoreCompilerErrors --out ./docs --mode file --tsconfig ./config/tsconfig.build.json ./src/",
"semantic-release": "semantic-release"
"test:watch": "npm run clean && jest -c ./jest.json --watch"
},
"keywords": [
"etl",
Expand All @@ -22,7 +26,7 @@
"elasticsearch"
],
"engines": {
"node": ">=6"
"node": ">=16"
},
"repository": {
"type": "git",
Expand All @@ -32,22 +36,23 @@
"author": "Christoph Bühler <[email protected]>",
"license": "MIT",
"devDependencies": {
"@smartive/tslint-config": "^2.0.0",
"@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",
"tsutils": "^2.18.0",
"typedoc": "^0.9.0",
"typescript": "^2.6.2"
"@smartive/eslint-config": "^3.1.1",
"@smartive/prettier-config": "^3.0.0",
"@types/jest": "^29.2.4",
"del-cli": "^5.0.0",
"jest": "^29.3.1",
"semantic-release": "^19.0.5",
"ts-jest": "^29.0.3",
"tslint": "^5.20.1",
"tsutils": "^3.21.0",
"typedoc": "^0.23.23",
"typescript": "^4.9.4"
},
"dependencies": {
"elasticsearch": "^14.0.0",
"proc-that": "^1.0.2",
"@types/node": "^9.3.0",
"rxjs": "^5.5.6",
"tslib": "^1.8.1"
"@elastic/elasticsearch": "^8.5.0",
"@types/node": "^18.11.17",
"proc-that": "^2.0.0",
"rxjs": "^7.8.0",
"tslib": "^2.4.1"
}
}
89 changes: 42 additions & 47 deletions src/ElasticLoader.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,57 @@
import { Client, ClientOptions } from '@elastic/elasticsearch';
import { Loader } from 'proc-that';
import { Observable } from 'rxjs';

import { EMPTY, from, Observable, throwError } from 'rxjs';
import { Buffer } from './helpers/Buffer';

const elasticsearch = require('elasticsearch');

class NoIdProvidedError extends Error {
constructor(public object: any) {
super('No id provided by object');
}
constructor(public object: any) {
super('No id provided by object');
}
}

export class ElasticLoader implements Loader {
private esClient: any;
private buffer: Buffer<any> = new Buffer();

constructor(
config: any,
private index: string,
private type: string,
private predicate: (obj: any) => boolean = () => true,
private idSelector: (obj: any) => any = o => o.id,
) {
const esConfig = JSON.parse(JSON.stringify(config));
if (!esConfig.requestTimeout) {
// set requestTimeout to 5min.
// reason: when you shoot many index requests to the esClient, elasticsearch buffers your requests.
// after the default timeout of 30s is exceeded, you receive a TimeoutError and the whole index process fails.
esConfig.requestTimeout = 1000 * 60 * 5;
}
this.esClient = new elasticsearch.Client(esConfig);
if (esConfig.maxSockets) {
this.buffer = new Buffer(esConfig.maxSockets);
}
private esClient: Client;
private buffer: Buffer<any>;

constructor(
esConfig: ClientOptions,
private index: string,
private predicate: (obj: any) => boolean = () => true,
private idSelector: (obj: any) => any = (o) => o.id,
bufferSize = 10
) {
if (!esConfig.requestTimeout) {
// set requestTimeout to 5min.
// reason: when you shoot many index requests to the esClient, elasticsearch buffers your requests.
// after the default timeout of 30s is exceeded, you receive a TimeoutError and the whole index process fails.
esConfig.requestTimeout = 1000 * 60 * 5;
}

public write(object: any): Observable<any> {
if (!this.predicate(object)) {
return Observable.empty();
}
this.buffer = new Buffer(bufferSize);
this.esClient = new Client(esConfig);
}

const id = this.idSelector(object);
public write(object: any): Observable<any> {
if (!this.predicate(object)) {
return EMPTY;
}

if (id === null || id === undefined) {
return Observable.throw(new NoIdProvidedError(object));
}
const id = this.idSelector(object);

const promise = this.buffer
.read()
.then(() => this.esClient.index({
id,
index: this.index,
type: this.type,
body: object,
}));
if (id === null || id === undefined) {
return throwError(() => new NoIdProvidedError(object));
}

this.buffer.write(object);
const promise = this.buffer.read().then(() =>
this.esClient.index({
id,
index: this.index,
body: object,
})
);

return Observable.fromPromise(promise);
}
this.buffer.write(object);

return from(promise);
}
}
Loading

0 comments on commit 4ff984e

Please sign in to comment.