-
Notifications
You must be signed in to change notification settings - Fork 70
/
query-selected-files.service.ts
87 lines (77 loc) · 2.67 KB
/
query-selected-files.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import {
BindingScope,
CoreBindings,
Provider,
inject,
injectable,
} from '@loopback/core';
import {Filter, defineCrudRepositoryClass, juggler} from '@loopback/repository';
import {AuditLog} from '@sourceloop/audit-log';
// eslint-disable-next-line @typescript-eslint/naming-convention
import AWS from 'aws-sdk';
import csvtojson from 'csvtojson';
import {AuditServiceApplication} from '../application';
import {QuerySelectedFilesFn} from '../types';
@injectable({scope: BindingScope.TRANSIENT})
export class QuerySelectedFilesProvider
implements Provider<QuerySelectedFilesFn>
{
constructor(
@inject(CoreBindings.APPLICATION_INSTANCE)
private application: AuditServiceApplication,
) {}
value(): QuerySelectedFilesFn {
return async (fileName: string, filter: Filter<AuditLog>) => {
return this.querySelectedFiles(fileName, filter);
};
}
async querySelectedFiles(fileName: string, filter: Filter<AuditLog>) {
const json = await this.getFileContent(fileName);
/*Creating an in-memory datasource and a dynamic repository is also created
through which data will be pushed in the in-memory db .Now the find operation
will happen on that db only. This is to ensure simultanoeus find operation
across multiple users
*/
const dsName = 'csvds';
const csvDataSource = new juggler.DataSource({
name: dsName,
connector: 'memory',
});
await csvDataSource.connect();
this.application.dataSource(csvDataSource, dsName);
// configure repository
const CSVRepo = defineCrudRepositoryClass<
AuditLog,
typeof AuditLog.prototype.id,
{}
>(AuditLog);
Object.defineProperty(CSVRepo, 'name', {value: 'CsvRepo'});
inject(`datasources.${csvDataSource.name}`)(CSVRepo, undefined, 0);
this.application.repository(CSVRepo);
const csvRepoInstance = await this.application.getRepository(CSVRepo);
// Fill in the json returned from the csv
await csvRepoInstance.createAll(json!);
const allRecords = await csvRepoInstance.find(filter);
// clear up the memory
await csvRepoInstance.deleteAll();
return allRecords;
}
async getFileContent(fileName: string) {
AWS.config = new AWS.Config({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_REGION,
});
const s3 = new AWS.S3();
const params = {
Bucket: process.env.AWS_S3_BUCKET_NAME as string,
Key: fileName,
};
const {Body} = await s3.getObject(params).promise();
let json: AuditLog[] | undefined;
if (Body) {
json = await csvtojson().fromString(Body?.toString());
}
return json;
}
}