Skip to content

Commit

Permalink
feat: upgrade typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Bühler committed Jul 27, 2017
1 parent 3ce7973 commit d003cfa
Show file tree
Hide file tree
Showing 16 changed files with 287 additions and 158 deletions.
15 changes: 11 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
node_modules

# Logs
logs
*.log
npm-debug.log*

# Dependency directory
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# Typescript stuff
typings
build
docs
coverage

package-lock.json
34 changes: 9 additions & 25 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
build
typings

# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Dependency directory
node_modules

Expand All @@ -26,20 +12,18 @@ node_modules
# Optional REPL history
.node_repl_history


# Typescript stuff
typings
build
docs
coverage
build/
coverage/
config/
tsconfig.json
tslint.json

# Files
# Typescript files (but not the definitions)
*.ts
!*.d.ts
*.js.map
*.spec.js
*.spec.ts
*.spec.d.ts
*.spec.js.map

.travis.yml
# Testfiles
jest.json
test/
29 changes: 29 additions & 0 deletions config/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"removeComments": false,
"outDir": "../build",
"rootDir": "../src",
"declaration": true,
"sourceMap": false,
"importHelpers": true,
"strictNullChecks": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"lib": [
"es2015",
"dom"
]
},
"include": [
"../src/**/*"
],
"exclude": [
"../node_modules",
"../build"
]
}
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": "../"
}
}
1 change: 0 additions & 1 deletion index.ts

This file was deleted.

19 changes: 19 additions & 0 deletions jest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"collectCoverage": true,
"mapCoverage": true,
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testMatch": [
"**/test/**/*.spec.ts"
],
"testPathIgnorePatterns": [
"/node_modules/"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
]
}
39 changes: 16 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
"description": "Loader for proc-that. Loads processed items into an elasticsearch index.",
"main": "index.js",
"scripts": {
"clean": "rimraf build",
"bootstrap": "npm install",
"pretest": "npm run bootstrap && npm run clean && tsc",
"test": "mocha --ui bdd --recursive ./build",
"precitest": "npm run bootstrap && npm run clean && tsc",
"citest": "istanbul cover -x \"**/*.spec.*\" _mocha --report lcovonly -- --ui bdd --recursive ./build",
"develop": "npm run clean && tsc -w --sourceMap --pretty"
"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"
},
"keywords": [
"etl",
Expand All @@ -25,32 +24,26 @@
},
"repository": {
"type": "git",
"url": "git@github.com:smartive/proc-that-elastic-loader.git"
"url": "https://github.com/smartive/proc-that-elastic-loader.git"
},
"bugs": "https://github.com/smartive/proc-that-elastic-loader/issues",
"author": "Christoph Bühler <[email protected]>",
"license": "MIT",
"devDependencies": {
"@types/chai": "^4.0.1",
"@types/chai-as-promised": "^0.0.31",
"@types/mocha": "^2.2.41",
"@types/node": "^8.0.16",
"@types/sinon": "^2.3.3",
"@types/sinon-chai": "^2.7.28",
"chai": "^4.1.0",
"chai-as-promised": "^7.1.1",
"@types/jest": "^20.0.5",
"del-cli": "^1.1.0",
"istanbul": "^0.4.4",
"mocha": "^3.4.1",
"mocha-lcov-reporter": "^1.2.0",
"rimraf": "^2.5.3",
"sinon": "^2.4.0",
"sinon-chai": "^2.8.0",
"jest": "^20.0.4",
"ts-jest": "^20.0.7",
"tslint": "^5.5.0",
"tslint-config-airbnb": "^5.2.1",
"tsutils": "^2.8.0",
"typescript": "^2.4.2"
},
"dependencies": {
"elasticsearch": "^13.2.0",
"proc-that": "^0.4.0",
"rxjs": "^5.4.2"
"@types/node": "^8.0.17",
"rxjs": "^5.4.2",
"tslib": "^1.7.1"
}
}
41 changes: 24 additions & 17 deletions ElasticLoader.ts → src/ElasticLoader.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import {Loader} from 'proc-that';
import {Observable} from 'rxjs';
import {Buffer} from './helpers/Buffer';
import { Loader } from 'proc-that';
import { Observable } from 'rxjs';

let elasticsearch = require('elasticsearch');
import { Buffer } from './helpers/Buffer';

const elasticsearch = require('elasticsearch');

class NoIdProvidedError extends Error {
constructor(private object:any) {
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 = o => true, private idSelector:(obj:any) => any = o => o.id) {
let esConfig = JSON.parse(JSON.stringify(config));
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.
Expand All @@ -28,28 +35,28 @@ export class ElasticLoader implements Loader {
}
}

write(object: any): Observable<any> {
public write(object: any): Observable<any> {
if (!this.predicate(object)) {
return Observable.empty();
}

let id = this.idSelector(object);
const id = this.idSelector(object);

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

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

this.buffer.write(object);

return Observable.fromPromise(promise);
}
}
}
12 changes: 6 additions & 6 deletions helpers/Buffer.ts → src/helpers/Buffer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {EventEmitter} from 'events';
import { EventEmitter } from 'events';

export class BufferSealedError extends Error {
constructor() {
Expand Down Expand Up @@ -43,9 +43,9 @@ export class Buffer<T> extends EventEmitter {
}
}

public read(): Promise<T> {
public read(): Promise<T | undefined> {
if (!this.isEmpty) {
let content = this.content.shift();
const content = this.content.shift();
this.emit('release', content);

if (this.isEmpty) {
Expand All @@ -56,7 +56,7 @@ export class Buffer<T> extends EventEmitter {
return Promise.resolve(content);
}

return new Promise(resolve => {
return new Promise((resolve) => {
this.once('write', () => resolve(this.read()));
});
}
Expand All @@ -72,8 +72,8 @@ export class Buffer<T> extends EventEmitter {
return Promise.resolve(object);
}

return new Promise(resolve => {
return new Promise((resolve) => {
this.once('release', () => resolve(this.write(object)));
});
}
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ElasticLoader';
Loading

0 comments on commit d003cfa

Please sign in to comment.