-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Felipe Klerk Signorini
committed
Aug 17, 2017
1 parent
bc0df25
commit e38338b
Showing
10 changed files
with
188 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"use strict"; | ||
|
||
const _ = require('lodash'); | ||
const {ObjectId} = require('mongorito'); | ||
|
||
module.exports = (_id) => { | ||
return _.isArray(_id) | ||
? {$in: _.map(_id, e=>ObjectId(e))} | ||
: ObjectId(_id); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
|
||
const DFactoryDBRepository = require('core/repositories/DBRepository'); | ||
|
||
const Access = require('core/entities/accessRole'); | ||
const accessMergeTransform = require('./transforms/accessMergeTransform'); | ||
const regexFilterQuery = require('./transforms/regexFilterQuery'); | ||
|
||
|
||
const Persistence = (Entity, FactoryDBRepository = DFactoryDBRepository) => { | ||
|
||
const DBRepository = FactoryDBRepository(Entity); | ||
|
||
return { | ||
|
||
find (query, owner, access = Access.ROLE_READ) { | ||
return new Promise((resolve, reject) => { | ||
|
||
const prepared = _.assign({}, | ||
query, | ||
accessMergeTransform(owner, Entity.access, query, access), | ||
...regexFilterQuery(_.get(query, 'query')) | ||
); | ||
|
||
return Promise.all([ | ||
DBRepository.find(prepared), | ||
DBRepository.count(prepared) | ||
]) | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
}, | ||
|
||
findOne (_id, owner, access = Access.ROLE_READ) { | ||
|
||
return new Promise((resolve, reject) => { | ||
|
||
const prepared = accessMergeTransform(owner, Entity.access, {_id}, access); | ||
|
||
return DBRepository | ||
.findOne(prepared) | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
}, | ||
|
||
update (_id, post, owner, access = Access.ROLE_WRITER) { | ||
|
||
return new Promise((resolve, reject) => { | ||
|
||
const fill = _.difference(Entity.filled, ['owner', Entity.access, 'password', '_id']); | ||
|
||
const prepared = accessMergeTransform(owner, Entity.access, {_id}, access); | ||
|
||
return DBRepository | ||
.update(prepared, post, fill) | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
}, | ||
|
||
create (post) { | ||
return new Promise((resolve, reject) => { | ||
return DBRepository | ||
.create(post) | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
}, | ||
|
||
remove(_id, owner, access = Access.ROLE_ADMIN) { | ||
|
||
return new Promise((resolve, reject) => { | ||
const prepared = accessMergeTransform(owner, Entity.access, {_id}, access); | ||
|
||
return DBRepository | ||
.remove(prepared) | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
} | ||
|
||
}; | ||
}; | ||
|
||
module.exports = _.curry(Persistence); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
|
||
const DFactoryDBRepository = require('core/repositories/DBRepository'); | ||
|
||
const Access = require('core/entities/accessRole'); | ||
const accessMergeTransform = require('core/services/transforms/accessMergeTransform'); | ||
|
||
const Persistence = (Entity, FactoryDBRepository = DFactoryDBRepository) => { | ||
|
||
const DBRepository = FactoryDBRepository(Entity, {oUpdater: 'Many'}); | ||
|
||
return { | ||
addList (_id, post, owner, access = Access.ROLE_WRITER) { | ||
|
||
return new Promise((resolve, reject) => { | ||
|
||
const prepared = _.assign({}, | ||
accessMergeTransform(owner, Entity.access, {_id}, access) | ||
); | ||
|
||
return DBRepository | ||
.updateByPushUnique(prepared, post) | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
|
||
}, | ||
|
||
|
||
}; | ||
}; | ||
|
||
module.exports = _.curry(Persistence); |