-
-
Notifications
You must be signed in to change notification settings - Fork 4k
feat(model): add applyTimestamps() function to apply all schema timestamps, including subdocuments, to a given POJO #14943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8d66fa9
feat(model): add applyTimestamps() function to apply all schema times…
vkarpov15 da75251
Update lib/helpers/document/applyTimestamps.js
vkarpov15 c52041b
fix code review comments
vkarpov15 cce41a5
Merge branch 'vkarpov15/gh-14698-2' of github.com:Automattic/mongoose…
vkarpov15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,105 @@ | ||
| 'use strict'; | ||
|
|
||
| const handleTimestampOption = require('../schema/handleTimestampOption'); | ||
| const mpath = require('mpath'); | ||
|
|
||
| module.exports = applyTimestamps; | ||
|
|
||
| /** | ||
| * Apply a given schema's timestamps to the given POJO | ||
| * | ||
| * @param {Schema} schema | ||
| * @param {Object} obj | ||
| * @param {Object} [options] | ||
| * @param {Boolean} [options.isUpdate=false] if true, treat this as an update: just set updatedAt, skip setting createdAt. If false, set both createdAt and updatedAt | ||
| * @param {Function} [options.currentTime] if set, Mongoose will call this function to get the current time. | ||
| */ | ||
|
|
||
| function applyTimestamps(schema, obj, options) { | ||
| if (obj == null) { | ||
| return obj; | ||
| } | ||
|
|
||
| applyTimestampsToChildren(schema, obj, options); | ||
| return applyTimestampsToDoc(schema, obj, options); | ||
| } | ||
|
|
||
| /** | ||
| * Apply timestamps to any subdocuments | ||
| * | ||
| * @param {Schema} schema subdocument schema | ||
| * @param {Object} res subdocument | ||
| * @param {Object} [options] | ||
| * @param {Boolean} [options.isUpdate=false] if true, treat this as an update: just set updatedAt, skip setting createdAt. If false, set both createdAt and updatedAt | ||
| * @param {Function} [options.currentTime] if set, Mongoose will call this function to get the current time. | ||
| */ | ||
|
|
||
| function applyTimestampsToChildren(schema, res, options) { | ||
| for (const childSchema of schema.childSchemas) { | ||
| const _path = childSchema.model.path; | ||
| const _schema = childSchema.schema; | ||
| if (!_path) { | ||
| continue; | ||
| } | ||
| const _obj = mpath.get(_path, res); | ||
| if (_obj == null || (Array.isArray(_obj) && _obj.flat(Infinity).length === 0)) { | ||
| continue; | ||
| } | ||
|
|
||
| applyTimestamps(_schema, _obj, options); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Apply timestamps to a given document. Does not apply timestamps to subdocuments: use `applyTimestampsToChildren` instead | ||
| * | ||
| * @param {Schema} schema | ||
| * @param {Object} obj | ||
| * @param {Object} [options] | ||
| * @param {Boolean} [options.isUpdate=false] if true, treat this as an update: just set updatedAt, skip setting createdAt. If false, set both createdAt and updatedAt | ||
| * @param {Function} [options.currentTime] if set, Mongoose will call this function to get the current time. | ||
| */ | ||
|
|
||
| function applyTimestampsToDoc(schema, obj, options) { | ||
| if (obj == null || typeof obj !== 'object') { | ||
| return; | ||
| } | ||
| if (Array.isArray(obj)) { | ||
| for (const el of obj) { | ||
| applyTimestampsToDoc(schema, el, options); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (schema.discriminators && Object.keys(schema.discriminators).length > 0) { | ||
| for (const discriminatorKey of Object.keys(schema.discriminators)) { | ||
| const discriminator = schema.discriminators[discriminatorKey]; | ||
| const key = discriminator.discriminatorMapping.key; | ||
| const value = discriminator.discriminatorMapping.value; | ||
| if (obj[key] == value) { | ||
| schema = discriminator; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const createdAt = handleTimestampOption(schema.options.timestamps, 'createdAt'); | ||
| const updatedAt = handleTimestampOption(schema.options.timestamps, 'updatedAt'); | ||
| const currentTime = options?.currentTime; | ||
|
|
||
| let ts = null; | ||
| if (currentTime != null) { | ||
| ts = currentTime(); | ||
| } else if (schema.base?.now) { | ||
| ts = schema.base.now(); | ||
| } else { | ||
| ts = new Date(); | ||
| } | ||
|
|
||
| if (createdAt && obj[createdAt] == null && !options?.isUpdate) { | ||
| obj[createdAt] = ts; | ||
| } | ||
| if (updatedAt) { | ||
| obj[updatedAt] = ts; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.