-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #13
- Loading branch information
Showing
10 changed files
with
165 additions
and
83 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,66 @@ | ||
/* eslint-disable no-param-reassign */ | ||
|
||
/** | ||
* A mongoose schema plugin which applies the following in the toJSON transform call: | ||
* - removes __v, createdAt, updatedAt, and any path that has private: true | ||
* - replaces _id with id | ||
*/ | ||
const toJSON = (schema) => { | ||
let transform; | ||
if (schema.options.toJSON && schema.options.toJSON.transform) { | ||
transform = schema.options.toJSON.transform; | ||
} | ||
|
||
schema.options.toJSON = Object.assign(schema.options.toJSON || {}, { | ||
transform(doc, ret, options) { | ||
Object.keys(schema.paths).forEach((path) => { | ||
if (schema.paths[path].options && schema.paths[path].options.private) { | ||
delete ret[path]; | ||
} | ||
}); | ||
|
||
ret.id = ret._id.toString(); | ||
delete ret._id; | ||
delete ret.__v; | ||
delete ret.createdAt; | ||
delete ret.updatedAt; | ||
if (transform) { | ||
return transform(doc, ret, options); | ||
} | ||
}, | ||
}); | ||
}; | ||
|
||
const paginate = (schema) => { | ||
schema.statics.paginate = async function (query, options) { | ||
const sort = {}; | ||
if (options.sortBy) { | ||
const parts = options.sortBy.split(':'); | ||
sort[parts[0]] = parts[1] === 'desc' ? -1 : 1; | ||
} | ||
const limit = options.limit && parseInt(options.limit, 10) > 0 ? parseInt(options.limit, 10) : 10; | ||
const page = options.page && parseInt(options.page, 10) > 0 ? parseInt(options.page, 10) : 1; | ||
const skip = (page - 1) * limit; | ||
|
||
const countPromise = this.countDocuments(query).exec(); | ||
const docsPromise = this.find(query).sort(sort).skip(skip).limit(limit).exec(); | ||
|
||
return Promise.all([countPromise, docsPromise]).then((values) => { | ||
const [totalResults, results] = values; | ||
const totalPages = Math.ceil(totalResults / limit); | ||
const result = { | ||
results, | ||
page, | ||
limit, | ||
totalPages, | ||
totalResults, | ||
}; | ||
return Promise.resolve(result); | ||
}); | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
toJSON, | ||
paginate, | ||
}; |
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
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
2 changes: 1 addition & 1 deletion
2
tests/unit/models/utils.test.js → tests/unit/models/plugins.test.js
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