Skip to content

Commit

Permalink
Merge pull request #7 from hapinessjs/next
Browse files Browse the repository at this point in the history
release(version): v1.1.1
  • Loading branch information
akanass authored Feb 13, 2018
2 parents a59ea0d + dc68c8c commit b1f8dee
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 49 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class MyService {

## Change History

* v1.1.1 (2018-02-13)
* Fix returned object, now doesn't include mongoose metadata
* v1.1.0 (2018-01-30)
* Update mingo model
* Minor code updates
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hapiness/mingo",
"version": "1.1.0",
"version": "1.1.1",
"description": "Manage files using minio as file storage and mongodb for metadata",
"main": "commonjs/index.js",
"types": "index.d.ts",
Expand Down Expand Up @@ -95,7 +95,7 @@
"rxjs": "^5.5.6",
"ts-node": "^3.3.0",
"tslint": "^5.8.0",
"typescript": "^2.6.2",
"typescript": "^2.7.1",
"unit.js": "^2.0.0"
},
"peerDependencies": {
Expand Down
30 changes: 17 additions & 13 deletions src/module/managers/files.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class FilesManager {
this._getDocument()
.findOneAndUpdate({ filename }, _, { new: true, upsert: true })
)
.map(file => <MingoFileInterface>file.toJSON())
.map<MingoFileDocumentInterface, MingoFileInterface>(file => file.toJSON())
);
}

Expand Down Expand Up @@ -111,11 +111,12 @@ export class FilesManager {
*/
find(
query?: { [key: string]: any }, projection?: string | string[], options?: { [key: string]: any }
): Observable<MingoFileDocumentInterface[]> {
): Observable<MingoFileInterface[]> {
const _options = Object.assign({ limit: 10000 }, options);
const projectionStr = projection && projection instanceof Array ? projection.join(' ') : projection;

return Observable.fromPromise(this._getDocument().find(query, projectionStr, _options));
return Observable.fromPromise(this._getDocument().find(query, projectionStr, _options))
.map<MingoFileDocumentInterface[], MingoFileInterface[]>(_ => _.map(__ => __.toJSON()));
}

/**
Expand All @@ -127,9 +128,11 @@ export class FilesManager {
*/
findByFilename(
filename: string, projection?: string | string[], options?: { [key: string]: any }
): Observable<MingoFileDocumentInterface> {
): Observable<MingoFileInterface> {
const projectionStr = projection && projection instanceof Array ? projection.join(' ') : projection;
return Observable.fromPromise(this._getDocument().findOne({ filename }, projectionStr, options));

return Observable.fromPromise(this._getDocument().findOne({ filename }, projectionStr, options))
.map<MingoFileDocumentInterface, MingoFileInterface>(_ => _.toJSON());
}

/**
Expand All @@ -153,8 +156,8 @@ export class FilesManager {
* @param options Mongo update options
*/
update(
query: { [key: string]: any }, update: { [key: string]: any }, options?: ModelUpdateOptions
): Observable<MingoFileDocumentInterface[]> {
query: { [key: string]: any }, update: { [key: string]: any }, options ?: ModelUpdateOptions
): Observable <MingoFileInterface[]> {
return Observable
.fromPromise(this._getDocument().update(query, { $set: this._prepareUpdateObject(update) }, options))
.flatMap(_ => this.find(query, null, options));
Expand All @@ -167,7 +170,7 @@ export class FilesManager {
* @param update Metadata update object
* @param options Mongo update options
*/
updateByFilename(filename: string, update: { [key: string]: any }): Observable<MingoFileDocumentInterface> {
updateByFilename(filename: string, update: { [key: string]: any }): Observable <MingoFileInterface> {
return this.exists(filename)
.flatMap(_ => !_ ?
Observable.throw(Biim.notFound(`Cannot update ${filename}. File does not exist.`)) :
Expand All @@ -176,7 +179,8 @@ export class FilesManager {
{ $set: this._prepareUpdateObject(update) },
{ new: true, upsert: false }
))
);
)
.map(_ => _.toJSON());
}

/**
Expand All @@ -200,15 +204,15 @@ export class FilesManager {
* @param options Mongo options object
*/
/* istanbul ignore next */
remove(query: { [key: string]: any }, options?: Object): Observable<MingoFileDocumentInterface[]> {
remove(query: { [key: string]: any }, options ?: Object): Observable <MingoFileDocumentInterface[]> {
return this.find(query, ['filename'], options)
.map(files => files.map(file => this.removeByFilename(file.filename)))
.flatMap(files => Observable.from(files))
.concatAll()
.toArray();
.toArray()
}

removeByFilename(filename: string): Observable<MingoFileDocumentInterface> {
removeByFilename(filename: string): Observable <MingoFileDocumentInterface> {
return Observable.of(filename)
.flatMap(_ => _ ? Observable.of(filename) : Observable.throw(Biim.badRequest(`No filename provided`)))
.flatMap(_ => this.exists(filename))
Expand All @@ -219,7 +223,7 @@ export class FilesManager {
.flatMap(file =>
this._bucketService
.removeFile(filename)
.flatMap(_ => _ ? Observable.of(file) : Observable.throw(Biim.badRequest(`Unable to remove file ${filename}`)))
.flatMap(_ => _ ? Observable.of(file.toJSON()) : Observable.throw(Biim.badRequest(`Unable to remove file ${filename}`)))
);
}
}
11 changes: 9 additions & 2 deletions src/module/models/mingo-file.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ export class MingoFileModel extends Model {
}
},
{
id: false
id: false,
versionKey: false
}
);

this.schema.set('toJSON', { virtuals: true });
this.schema.set('toJSON', {
virtuals: true,
transform: function (doc, ret) {
delete ret._id;
return ret;
}
});
}
}
26 changes: 16 additions & 10 deletions test/functional/mingo.module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import { Observable } from 'rxjs/Observable';
export class MingoModuleFunctionalTest {
@test('Mingo module load successfuly and run several commands')
mingoRunSuccess(done) {
const fileProperties = [
{ filename: 'package.json' },
{ contentType: 'json' },
{ size: fs.lstatSync('./package.json').size },
{ md5: crypto.createHash('md5').update(fs.readFileSync('./package.json', { encoding: 'utf8'})).digest('hex') }
];
const fileProperties = {
filename: 'package.json',
contentType: 'json',
size: fs.lstatSync('./package.json').size,
md5: crypto.createHash('md5').update(fs.readFileSync('./package.json', { encoding: 'utf8'})).digest('hex'),
created_at: null,
updated_at: null
};

@HapinessModule({
version: '1.0.0',
Expand All @@ -53,22 +55,26 @@ export class MingoModuleFunctionalTest {
}

fb().create(fs.createReadStream('./package.json'), 'package.json', 'json', null)
.do(_ => Object.assign(fileProperties, { created_at: _.created_at, updated_at: _.updated_at }))
.do(_ => unit
.object(_)
.contains(...fileProperties)
.is(fileProperties)
)
.flatMap(_ => fb().exists('package.json'))
.do(_ => unit.bool(_).isTrue())
.flatMap(_ => fb().findByFilename('package.json'))
.do(_ => unit
.object(_)
.contains(...fileProperties)
.is(fileProperties)
)
.do(_ => unit
.object(_)
.is(fileProperties)
)
.flatMap(_ => fb().updateByFilename('package.json', { meta1: 'metadata' }))
.do(_ => unit
.object(_)
.contains(...fileProperties)
.contains({ metadata: { meta1 : 'metadata' } })
.is(Object.assign({}, fileProperties, { metadata: { meta1 : 'metadata' } }))
)
.flatMap(_ => fb().exists('package.json'))
.do(_ => unit
Expand Down
43 changes: 25 additions & 18 deletions test/unit/managers/file.manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export class FilesManagerUnitTest {
private _mongoMock: any;
private _configMock: MingoConfig = {};

private _classMock = {
toJSON: function () {
return this;
}
}

before() {
this._modelMock = {
findOneAndUpdate: unit.stub().returns(Promise.resolve()),
Expand Down Expand Up @@ -183,35 +189,36 @@ export class FilesManagerUnitTest {
find(done) {
const input = Buffer.from('helloworld');
const filename = 'helloworld.txt';
const file = {
const file = Object.assign(Object.create(this._classMock), {
filename,
contentType: 'application/octet-stream',
metadata: {},
size: input.length,
created_at: new Date(),
updated_at: new Date(),
md5: new Date().getTime(),
};
md5: new Date().getTime()
});

this._modelMock.find.returns(Promise.resolve([file]));
unit.function(this._filesManager.find);
const obs = this._filesManager.find();
obs.subscribe(_ => {
unit.array(_).is([file]);
done();
}, err => done(err));
this._filesManager.find()
.subscribe(_ => {
unit.array(_).is([file]);
done();
}, err => done(err));
}

@test('- find: with projection as an array')
findWithProjection(done) {
const input = Buffer.from('helloworld');
const filename = 'helloworld.txt';
const file = {
const file = Object.assign(Object.create(this._classMock), {
filename,
contentType: 'application/octet-stream',
size: input.length,
created_at: new Date(),
updated_at: new Date()
};
});
this._modelMock.find.returns(Promise.resolve([file]));
unit.function(this._filesManager.find);
const obs = this._filesManager.find(null, ['filename', 'contentType', 'size', 'created_at', 'updated_at']);
Expand All @@ -225,15 +232,15 @@ export class FilesManagerUnitTest {
findByFilename(done) {
const input = Buffer.from('helloworld');
const filename = 'helloworld.txt';
const file = {
const file = Object.assign(Object.create(this._classMock), {
filename,
contentType: 'application/octet-stream',
metadata: {},
size: input.length,
created_at: new Date(),
updated_at: new Date(),
md5: new Date().getTime(),
};
});
this._modelMock.findOne.returns(Promise.resolve(file));
unit.function(this._filesManager.findByFilename);
const obs = this._filesManager.findByFilename(filename);
Expand All @@ -247,11 +254,11 @@ export class FilesManagerUnitTest {
findByFilenameWithProjection(done) {
const input = Buffer.from('helloworld');
const filename = 'helloworld.txt';
const file = {
const file = Object.assign(Object.create(this._classMock), {
filename,
contentType: 'application/octet-stream',
size: input.length
};
});
this._modelMock.findOne.returns(Promise.resolve(file));
unit.function(this._filesManager.findByFilename);
const obs = this._filesManager.findByFilename(filename, ['filename', 'contentType', 'size']);
Expand Down Expand Up @@ -301,15 +308,15 @@ export class FilesManagerUnitTest {
@test('- updateByFilename')
updateByFilename(done) {
const filename = 'helloworld.txt';
const updatedFile = {
const updatedFile = Object.assign(Object.create(this._classMock), {
filename,
contentType: 'application/octet-stream',
metadata: { 'metadata.meta1': 'meta1' },
size: 42,
created_at: new Date(),
updated_at: new Date(),
md5: new Date().getTime(),
};
});

this._modelMock.findOneAndUpdate.returns(Promise.resolve(updatedFile));

Expand All @@ -334,15 +341,15 @@ export class FilesManagerUnitTest {
@test('- removeByFilename')
removeByFilename(done) {
const filename = 'helloworld.txt';
const removedFile = {
const removedFile = Object.assign(Object.create(this._classMock), {
filename,
contentType: 'application/octet-stream',
metadata: { 'metadata.meta1': 'meta1' },
size: 42,
created_at: new Date(),
updated_at: new Date(),
md5: new Date().getTime(),
};
});

const existsStub = unit.stub(this._filesManager, 'exists');
existsStub.returns(Observable.of(true));
Expand Down

0 comments on commit b1f8dee

Please sign in to comment.