Skip to content

Commit c43acb0

Browse files
authored
Merge pull request #9 from hapinessjs/next
release(version): v1.1.2
2 parents b1f8dee + e08749d commit c43acb0

File tree

8 files changed

+732
-297
lines changed

8 files changed

+732
-297
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class MyService {
6363

6464
## Change History
6565

66+
* v1.1.2 (2018-02-14)
67+
* MingoModel now have an id exposed.
68+
* Fix when calling the toJson method but value returned by mongo is null.
6669
* v1.1.1 (2018-02-13)
6770
* Fix returned object, now doesn't include mongoose metadata
6871
* v1.1.0 (2018-01-30)

package-lock.json

+592-234
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hapiness/mingo",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "Manage files using minio as file storage and mongodb for metadata",
55
"main": "commonjs/index.js",
66
"types": "index.d.ts",

src/module/interfaces/mingo-file.interface.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface MingoFileInterface {
2+
id?: string | any;
23
filename: string;
34
contentType: string;
45
created_at: Date;

src/module/managers/files.manager.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class FilesManager {
4242
.createFile(input, filename, null, content_type)
4343
.switchMap((result): Observable<MingoFileInterface> =>
4444
Observable.of({
45+
id: undefined,
4546
filename: filename,
4647
size: result.size,
4748
contentType: result.contentType,
@@ -55,7 +56,7 @@ export class FilesManager {
5556
this._getDocument()
5657
.findOneAndUpdate({ filename }, _, { new: true, upsert: true })
5758
)
58-
.map<MingoFileDocumentInterface, MingoFileInterface>(file => file.toJSON())
59+
.map<MingoFileDocumentInterface, MingoFileInterface>(file => file ? file.toJSON() : file)
5960
);
6061
}
6162

@@ -116,7 +117,7 @@ export class FilesManager {
116117
const projectionStr = projection && projection instanceof Array ? projection.join(' ') : projection;
117118

118119
return Observable.fromPromise(this._getDocument().find(query, projectionStr, _options))
119-
.map<MingoFileDocumentInterface[], MingoFileInterface[]>(_ => _.map(__ => __.toJSON()));
120+
.map<MingoFileDocumentInterface[], MingoFileInterface[]>(_ => _.map(__ => __ ? __.toJSON() : __));
120121
}
121122

122123
/**
@@ -132,7 +133,7 @@ export class FilesManager {
132133
const projectionStr = projection && projection instanceof Array ? projection.join(' ') : projection;
133134

134135
return Observable.fromPromise(this._getDocument().findOne({ filename }, projectionStr, options))
135-
.map<MingoFileDocumentInterface, MingoFileInterface>(_ => _.toJSON());
136+
.map<MingoFileDocumentInterface, MingoFileInterface>(_ => _ ? _.toJSON() : _);
136137
}
137138

138139
/**
@@ -180,7 +181,7 @@ export class FilesManager {
180181
{ new: true, upsert: false }
181182
))
182183
)
183-
.map(_ => _.toJSON());
184+
.map(_ => _ ? _.toJSON() : _);
184185
}
185186

186187
/**

src/module/models/mingo-file.model.ts

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ export class MingoFileModel extends Model {
5050
}
5151
},
5252
{
53-
id: false,
5453
versionKey: false
5554
}
5655
);

test/functional/mingo.module.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export class MingoModuleFunctionalTest {
2424
@test('Mingo module load successfuly and run several commands')
2525
mingoRunSuccess(done) {
2626
const fileProperties = {
27+
id: null,
2728
filename: 'package.json',
2829
contentType: 'json',
2930
size: fs.lstatSync('./package.json').size,
@@ -55,7 +56,7 @@ export class MingoModuleFunctionalTest {
5556
}
5657

5758
fb().create(fs.createReadStream('./package.json'), 'package.json', 'json', null)
58-
.do(_ => Object.assign(fileProperties, { created_at: _.created_at, updated_at: _.updated_at }))
59+
.do(_ => Object.assign(fileProperties, { id: _.id, created_at: _.created_at, updated_at: _.updated_at }))
5960
.do(_ => unit
6061
.object(_)
6162
.is(fileProperties)

test/unit/managers/file.manager.test.ts

+128-56
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ export class FilesManagerUnitTest {
7171
unit.function(this._filesManager.exists);
7272
unit.stub(this._bucketManager, 'fileStat').returns(Observable.of({}));
7373
unit.stub(this._filesManager, 'findByFilename').returns(Observable.of({ filename }));
74-
const obs = this._filesManager.exists(filename);
75-
obs.subscribe(_ => {
76-
unit.bool(_).isTrue();
77-
done();
78-
}, err => done(err));
74+
this._filesManager.exists(filename)
75+
.subscribe(_ => {
76+
unit.bool(_).isTrue();
77+
done();
78+
}, err => done(err));
7979
}
8080

8181
@test('- exists: no')
@@ -84,34 +84,55 @@ export class FilesManagerUnitTest {
8484
unit.function(this._filesManager.exists);
8585
unit.stub(this._bucketManager, 'fileStat').returns(Observable.of({}));
8686
unit.stub(this._filesManager, 'findByFilename').returns(Observable.of(null));
87-
const obs = this._filesManager.exists(filename);
88-
obs.subscribe(_ => {
89-
unit.bool(_).isFalse();
90-
done();
91-
}, err => done(err));
87+
this._filesManager.exists(filename)
88+
.subscribe(_ => {
89+
unit.bool(_).isFalse();
90+
done();
91+
}, err => done(err));
9292
}
9393

9494
@test('- exists: no 2')
9595
existsNo2(done) {
9696
const filename = 'helloworld.txt';
9797
unit.function(this._filesManager.exists);
9898
unit.stub(this._bucketManager, 'fileStat').returns(Observable.throw({ code: 'NotFound' }));
99-
const obs = this._filesManager.exists(filename);
100-
obs.subscribe(_ => {
101-
unit.bool(_).isFalse();
102-
done();
103-
}, err => done(err));
99+
this._filesManager.exists(filename)
100+
.subscribe(_ => {
101+
unit.bool(_).isFalse();
102+
done();
103+
}, err => done(err));
104104
}
105105

106106
@test('- exists: filestat throw another error than "NotFound"')
107107
existsNo3(done) {
108108
const filename = 'helloworld.txt';
109109
unit.function(this._filesManager.exists);
110110
unit.stub(this._bucketManager, 'fileStat').returns(Observable.throw({ code: 'AnotherError' }));
111-
const obs = this._filesManager.exists(filename);
112-
obs.subscribe(_ => {
113-
done(new Error('Should not be here!'));
114-
}, err => done());
111+
this._filesManager.exists(filename)
112+
.subscribe(_ => {
113+
done(new Error('Should not be here!'));
114+
}, err => done());
115+
}
116+
117+
@test('- when upload returns null')
118+
uploadReturnsNull(done) {
119+
const input = Buffer.from('helloworld');
120+
const filename = 'helloworld.txt';
121+
const contentType = 'text/plain';
122+
const metadata = { lupulus: { stock: 99, empty: 10 } };
123+
unit.stub(this._bucketManager, 'createFile').returns(Observable.of({
124+
size: input.length,
125+
contentType: contentType,
126+
etag: new Date().getTime(),
127+
lastModified: new Date(),
128+
}));
129+
this._modelMock.findOneAndUpdate = unit.stub().returns(Promise.resolve(null));
130+
unit.function(this._filesManager.upload);
131+
this._filesManager.upload(input, filename, contentType, metadata)
132+
.subscribe(_ => {
133+
unit.value(_).is(null);
134+
done();
135+
}, err => done(err));
115136
}
116137

117138
@test('- Upload')
@@ -139,11 +160,11 @@ export class FilesManagerUnitTest {
139160
toJSON: () => file
140161
}));
141162
unit.function(this._filesManager.upload);
142-
const obs = this._filesManager.upload(input, filename, contentType, metadata);
143-
obs.subscribe(_ => {
144-
unit.object(_).is(file);
145-
done();
146-
}, err => done(err));
163+
this._filesManager.upload(input, filename, contentType, metadata)
164+
.subscribe(_ => {
165+
unit.object(_).is(file);
166+
done();
167+
}, err => done(err));
147168
}
148169

149170
@test('- Create')
@@ -162,11 +183,11 @@ export class FilesManagerUnitTest {
162183
};
163184
unit.stub(this._filesManager, 'exists').returns(Observable.of(false));
164185
unit.stub(this._filesManager, 'upload').returns(Observable.of(file));
165-
const obs = this._filesManager.create(input, filename);
166-
obs.subscribe(_ => {
167-
unit.object(_).is(file);
168-
done();
169-
}, err => done(err));
186+
this._filesManager.create(input, filename)
187+
.subscribe(_ => {
188+
unit.object(_).is(file);
189+
done();
190+
}, err => done(err));
170191
}
171192

172193
@test('- Create error: already exists')
@@ -175,14 +196,37 @@ export class FilesManagerUnitTest {
175196
const input = Buffer.from('helloworld');
176197
const filename = 'helloworld.txt';
177198
unit.stub(this._filesManager, 'exists').returns(Observable.of(true));
178-
const obs = this._filesManager.create(input, filename);
179-
obs.subscribe(_ => done(new Error('Cannot be here')),
180-
err => {
181-
unit.object(err)
182-
.isInstanceOf(Error)
183-
.hasProperty('message', 'File helloworld.txt already exists');
199+
this._filesManager.create(input, filename)
200+
.subscribe(_ => done(new Error('Cannot be here')),
201+
err => {
202+
unit.object(err)
203+
.isInstanceOf(Error)
204+
.hasProperty('message', 'File helloworld.txt already exists');
205+
done();
206+
});
207+
}
208+
209+
@test('- find returns null')
210+
findReturnsNull(done) {
211+
const input = Buffer.from('helloworld');
212+
const filename = 'helloworld.txt';
213+
const file = Object.assign(Object.create(this._classMock), {
214+
filename,
215+
contentType: 'application/octet-stream',
216+
metadata: {},
217+
size: input.length,
218+
created_at: new Date(),
219+
updated_at: new Date(),
220+
md5: new Date().getTime()
221+
});
222+
223+
this._modelMock.find.returns(Promise.resolve([null, null, file, null]));
224+
unit.function(this._filesManager.find);
225+
this._filesManager.find()
226+
.subscribe(_ => {
227+
unit.array(_).is([null, null, file, null]);
184228
done();
185-
});
229+
}, err => done(err));
186230
}
187231

188232
@test('- find')
@@ -221,9 +265,21 @@ export class FilesManagerUnitTest {
221265
});
222266
this._modelMock.find.returns(Promise.resolve([file]));
223267
unit.function(this._filesManager.find);
224-
const obs = this._filesManager.find(null, ['filename', 'contentType', 'size', 'created_at', 'updated_at']);
225-
obs.subscribe(_ => {
226-
unit.array(_).is([file]);
268+
this._filesManager.find(null, ['filename', 'contentType', 'size', 'created_at', 'updated_at'])
269+
.subscribe(_ => {
270+
unit.array(_).is([file]);
271+
done();
272+
}, err => done(err));
273+
}
274+
275+
@test('- findByFilename returns null')
276+
findByFilenameReturnsNull(done) {
277+
const filename = 'helloworld.txt';
278+
this._modelMock.findOne.returns(Promise.resolve(null));
279+
unit.function(this._filesManager.findByFilename);
280+
this._filesManager.findByFilename(filename)
281+
.subscribe(_ => {
282+
unit.value(_).is(null);
227283
done();
228284
}, err => done(err));
229285
}
@@ -243,11 +299,11 @@ export class FilesManagerUnitTest {
243299
});
244300
this._modelMock.findOne.returns(Promise.resolve(file));
245301
unit.function(this._filesManager.findByFilename);
246-
const obs = this._filesManager.findByFilename(filename);
247-
obs.subscribe(_ => {
248-
unit.object(_).is(file);
249-
done();
250-
}, err => done(err));
302+
this._filesManager.findByFilename(filename)
303+
.subscribe(_ => {
304+
unit.object(_).is(file);
305+
done();
306+
}, err => done(err));
251307
}
252308

253309
@test('- findByFilename: with projection as an array')
@@ -261,11 +317,11 @@ export class FilesManagerUnitTest {
261317
});
262318
this._modelMock.findOne.returns(Promise.resolve(file));
263319
unit.function(this._filesManager.findByFilename);
264-
const obs = this._filesManager.findByFilename(filename, ['filename', 'contentType', 'size']);
265-
obs.subscribe(_ => {
266-
unit.object(_).is(file);
267-
done();
268-
}, err => done(err));
320+
this._filesManager.findByFilename(filename, ['filename', 'contentType', 'size'])
321+
.subscribe(_ => {
322+
unit.object(_).is(file);
323+
done();
324+
}, err => done(err));
269325
}
270326

271327
@test('- download')
@@ -278,11 +334,11 @@ export class FilesManagerUnitTest {
278334
});
279335
const filename = 'helloworld.txt';
280336
unit.stub(this._filesManager, 'findByFilename').returns(Observable.of({ filename }));
281-
const obs = this._filesManager.download(filename);
282-
obs.subscribe(_ => {
283-
unit.object(_).isInstanceOf(Readable);
284-
done();
285-
}, err => done(err));
337+
this._filesManager.download(filename)
338+
.subscribe(_ => {
339+
unit.object(_).isInstanceOf(Readable);
340+
done();
341+
}, err => done(err));
286342
}
287343

288344
@test('- _prepareUpdateObject')
@@ -330,12 +386,28 @@ export class FilesManagerUnitTest {
330386
}, err => done(err));
331387
}
332388

389+
@test('- updateByFilename returns null')
390+
updateByFilenameReturnsNull(done) {
391+
const filename = 'helloworld.txt';
392+
393+
this._modelMock.findOneAndUpdate.returns(Promise.resolve(null));
394+
395+
unit.stub(this._filesManager, 'exists').returns(Observable.of(true));
396+
397+
this._filesManager.updateByFilename(filename, { meta1: 'meta1' })
398+
.subscribe(_ => {
399+
unit.value(_).is(null);
400+
unit.assert(this._modelMock.findOneAndUpdate.calledOnce);
401+
done();
402+
}, err => done(err));
403+
}
404+
333405
@test('- updateByFilename: given file to update does not exist')
334406
updateByFilenameWithNonexistantFile(done) {
335407
const filename = 'helloworld.txt';
336408
unit.stub(this._filesManager, 'exists').returns(Observable.of(false));
337-
const obs = this._filesManager.updateByFilename(filename, {});
338-
obs.subscribe(_ => done(new Error('Should fail')), err => done());
409+
this._filesManager.updateByFilename(filename, {})
410+
.subscribe(_ => done(new Error('Should fail')), err => done());
339411
}
340412

341413
@test('- removeByFilename')

0 commit comments

Comments
 (0)