@smartsoft001/mongo 1.1.91 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc +13 -0
- package/jest.config.ts +27 -0
- package/package.json +2 -26
- package/project.json +48 -0
- package/src/index.ts +3 -0
- package/src/lib/mongo.config.ts +10 -0
- package/src/lib/mongo.module.ts +28 -0
- package/src/lib/mongo.unitofwork.spec.ts +84 -0
- package/src/lib/mongo.unitofwork.ts +58 -0
- package/src/lib/mongo.utils.spec.ts +54 -0
- package/src/lib/mongo.utils.ts +17 -0
- package/src/lib/repositories/attachment.repository.spec.ts +249 -0
- package/src/lib/repositories/attachment.repository.ts +113 -0
- package/src/lib/repositories/interfaces.ts +28 -0
- package/src/lib/repositories/item.repository.spec.ts +1269 -0
- package/src/lib/repositories/item.repository.ts +582 -0
- package/tsconfig.json +13 -0
- package/tsconfig.lib.json +11 -0
- package/tsconfig.spec.json +20 -0
- package/src/index.d.ts +0 -3
- package/src/index.js +0 -7
- package/src/index.js.map +0 -1
- package/src/lib/mongo.config.d.ts +0 -9
- package/src/lib/mongo.config.js +0 -7
- package/src/lib/mongo.config.js.map +0 -1
- package/src/lib/mongo.module.d.ts +0 -5
- package/src/lib/mongo.module.js +0 -25
- package/src/lib/mongo.module.js.map +0 -1
- package/src/lib/mongo.unitofwork.d.ts +0 -12
- package/src/lib/mongo.unitofwork.js +0 -56
- package/src/lib/mongo.unitofwork.js.map +0 -1
- package/src/lib/mongo.utils.d.ts +0 -2
- package/src/lib/mongo.utils.js +0 -13
- package/src/lib/mongo.utils.js.map +0 -1
- package/src/lib/repositories/attachment.repository.d.ts +0 -28
- package/src/lib/repositories/attachment.repository.js +0 -88
- package/src/lib/repositories/attachment.repository.js.map +0 -1
- package/src/lib/repositories/item.repository.d.ts +0 -51
- package/src/lib/repositories/item.repository.js +0 -411
- package/src/lib/repositories/item.repository.js.map +0 -1
- package/test-setup.js +0 -1
- package/test-setup.js.map +0 -1
- /package/{test-setup.d.ts → test-setup.ts} +0 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
|
+
import { IEntity, IAttachmentRepository } from '@smartsoft001/domain-core';
|
|
3
|
+
import { MongoConfig } from '@smartsoft001/mongo';
|
|
4
|
+
import { MongoClient } from 'mongodb';
|
|
5
|
+
import * as mongo from 'mongodb';
|
|
6
|
+
import { Readable, Stream } from 'stream';
|
|
7
|
+
|
|
8
|
+
import { getMongoUrl } from '../mongo.utils';
|
|
9
|
+
|
|
10
|
+
@Injectable()
|
|
11
|
+
export class MongoAttachmentRepository<
|
|
12
|
+
T extends IEntity<string>,
|
|
13
|
+
> extends IAttachmentRepository<T> {
|
|
14
|
+
constructor(private config: MongoConfig) {
|
|
15
|
+
super();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async upload(
|
|
19
|
+
data: {
|
|
20
|
+
id: string;
|
|
21
|
+
fileName: string;
|
|
22
|
+
stream: Stream;
|
|
23
|
+
mimeType: string;
|
|
24
|
+
encoding: string;
|
|
25
|
+
},
|
|
26
|
+
options?: { streamCallback?: (r) => void },
|
|
27
|
+
): Promise<void> {
|
|
28
|
+
const client = await MongoClient.connect(this.getUrl());
|
|
29
|
+
|
|
30
|
+
return await new Promise<void>((res, rej) => {
|
|
31
|
+
const db = client.db(this.config.database);
|
|
32
|
+
const bucket = new mongo.GridFSBucket(db, {
|
|
33
|
+
bucketName: this.config.collection,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const writeStream = bucket.openUploadStreamWithId(
|
|
37
|
+
data.id as any,
|
|
38
|
+
data.fileName,
|
|
39
|
+
{
|
|
40
|
+
contentType: data.mimeType,
|
|
41
|
+
},
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
if (options?.streamCallback) options.streamCallback(writeStream);
|
|
45
|
+
|
|
46
|
+
data.stream.pipe(writeStream);
|
|
47
|
+
|
|
48
|
+
writeStream.on('error', (error) => {
|
|
49
|
+
rej(error);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
writeStream.on('finish', () => {
|
|
53
|
+
res();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async getInfo(
|
|
59
|
+
id: string,
|
|
60
|
+
): Promise<{ fileName: string; contentType: string; length: number }> {
|
|
61
|
+
const client = await MongoClient.connect(this.getUrl());
|
|
62
|
+
|
|
63
|
+
const db = client.db(this.config.database);
|
|
64
|
+
|
|
65
|
+
const bucket = new mongo.GridFSBucket(db, {
|
|
66
|
+
bucketName: this.config.collection,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const items = await bucket
|
|
70
|
+
.find({
|
|
71
|
+
_id: id as any,
|
|
72
|
+
})
|
|
73
|
+
.toArray();
|
|
74
|
+
|
|
75
|
+
if (!items || items.length === 0) return null;
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
fileName: items[0].filename,
|
|
79
|
+
contentType: items[0].contentType,
|
|
80
|
+
length: items[0].length,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async getStream(
|
|
85
|
+
id: string,
|
|
86
|
+
options: { start: number; end: number } | undefined,
|
|
87
|
+
): Promise<Readable> {
|
|
88
|
+
const client = await MongoClient.connect(this.getUrl());
|
|
89
|
+
|
|
90
|
+
const db = client.db(this.config.database);
|
|
91
|
+
const bucket = new mongo.GridFSBucket(db, {
|
|
92
|
+
bucketName: this.config.collection,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
return bucket.openDownloadStream(id as any, options);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async delete(id: string): Promise<void> {
|
|
99
|
+
const client = await MongoClient.connect(this.getUrl());
|
|
100
|
+
|
|
101
|
+
const db = client.db(this.config.database);
|
|
102
|
+
const bucket = new mongo.GridFSBucket(db, {
|
|
103
|
+
bucketName: this.config.collection,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
await bucket.delete(id as any);
|
|
107
|
+
await client.close();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private getUrl(): string {
|
|
111
|
+
return getMongoUrl(this.config);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface IItemCreateData {
|
|
2
|
+
id: string;
|
|
3
|
+
type: 'create';
|
|
4
|
+
data: any;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface IItemUpdateData {
|
|
8
|
+
id: string;
|
|
9
|
+
type: 'update';
|
|
10
|
+
data: {
|
|
11
|
+
removedFields: Array<string>;
|
|
12
|
+
updatedFields: {
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface IItemDeleteData {
|
|
19
|
+
id: string;
|
|
20
|
+
type: 'delete';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type ItemChangedData =
|
|
24
|
+
| IItemCreateData
|
|
25
|
+
| IItemUpdateData
|
|
26
|
+
| IItemDeleteData;
|
|
27
|
+
|
|
28
|
+
export type ItemChangedDataType = 'create' | 'update' | 'delete';
|