Skip to content

Commit 4362461

Browse files
committed
minor fixes
1 parent c455c61 commit 4362461

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

.prettierrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"bracketSpacing": false,
4+
"printWidth": 100
5+
}

src/index.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export interface IElasticModelConfig {
6161
excludedFields?: string[];
6262
}
6363

64-
export class ElasticModel<T extends Item> {
64+
class ElasticModel<T extends Item> {
6565
client: Client;
6666
protected index: string;
6767
protected idField: string;
@@ -123,9 +123,12 @@ export class ElasticModel<T extends Item> {
123123
body: query
124124
};
125125
const response = await this.client.search<T>(params);
126-
// @ts-ignore
127-
const total =
128-
typeof response.hits.total === 'number' ? response.hits.total : response.hits.total.value;
126+
let total: number;
127+
if (typeof response.hits.total === 'number') {
128+
total = response.hits.total;
129+
} else {
130+
total = (response.hits.total as {value: number}).value;
131+
}
129132
return {
130133
items: response.hits.hits.map(hit => hit._source),
131134
total,
@@ -139,7 +142,7 @@ export class ElasticModel<T extends Item> {
139142
* read more - https://www.npmjs.com/package/bodybuilder
140143
* Call query.exec at the end to execute the query
141144
*/
142-
buildQuery(): IBodyBuilder<T> {
145+
queryBuilder(): IBodyBuilder<T> {
143146
const self = this;
144147
const query = (bodyBuilder() as unknown) as IBodyBuilder<T>;
145148
query.exec = function() {
@@ -211,7 +214,6 @@ export class ElasticModel<T extends Item> {
211214
async createIndexIfMissing() {
212215
// check if index already exists
213216
const indexExists = await this.client.indices.exists({index: this.index});
214-
console.log({indexExists});
215217
if (indexExists) return true;
216218

217219
const params: IndicesCreateParams = {index: this.index, body: {}};
@@ -224,8 +226,8 @@ export class ElasticModel<T extends Item> {
224226
params.body.settings = this.settings;
225227
}
226228

227-
console.log(JSON.stringify(params, null, 2));
228-
229229
return this.client.indices.create(params);
230230
}
231231
}
232+
233+
export default ElasticModel;

test/index.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Client} from 'elasticsearch';
22
import {dynamoDBStreamEvent} from './fixtures';
3-
import {ElasticModel} from 'index';
3+
import ElasticModel from 'index';
44

55
const defaultConfig = {
66
host: 'https://someurl.com',
@@ -107,7 +107,7 @@ describe('ElasticModel', () => {
107107
const model = new ElasticModel(defaultConfig);
108108
const searchSpy = jest.spyOn(model, 'search');
109109
const query = model
110-
.buildQuery()
110+
.queryBuilder()
111111
.sort('createdAt', 'desc')
112112
.from(5)
113113
.size(5);

0 commit comments

Comments
 (0)