lib/timestamp.js
function timestamp(schema, options) {
	options = options || {};
	// Set defaults
	options = Object.assign({
		createdName: 'createdAt',
		updatedName: 'updatedAt',
		disableCreated: false,
		disableUpdated: false
	}, options);

	// Add fields if not disabled
	if (!options.disableCreated) {
		addSchemaField(schema, options.createdName, {type: Date});
	}
	if (!options.disableUpdated) {
		addSchemaField(schema, options.updatedName, {type: Date});
	}

	// Add pre-save hook to save dates of creation and modification
	schema.pre('save', preSave);

	/**
	 * Callback for the schema pre save
	 *
	 * @this mongoose.Schema
	 * @param {Function} next The next pre save callback in chain
	 * @returns {undefined}
	 */
	function preSave(next) {
		const _ref = this.get(options.createdName);

		if (!options.disableUpdated) {
			this[options.updatedName] = new Date();
		}
		if (!options.disableCreated && (_ref === undefined || _ref === null)) {
			this[options.createdName] = new Date();
		}

		next();
	}
}
function timestamp(schema, options) {
	options = options || {};
	// Set defaults
	options = Object.assign({
		createdName: 'createdAt',
		updatedName: 'updatedAt',
		disableCreated: false,
		disableUpdated: false
	}, options);

	// Add fields if not disabled
	if (!options.disableCreated) {
		addSchemaField(schema, options.createdName, {type: Date});
	}
	if (!options.disableUpdated) {
		addSchemaField(schema, options.updatedName, {type: Date});
	}

	// Add pre-save hook to save dates of creation and modification
	schema.pre('save', preSave);

	/**
	 * Callback for the schema pre save
	 *
	 * @this mongoose.Schema
	 * @param {Function} next The next pre save callback in chain
	 * @returns {undefined}
	 */
	function preSave(next) {
		const _ref = this.get(options.createdName);

		if (!options.disableUpdated) {
			this[options.updatedName] = new Date();
		}
		if (!options.disableCreated && (_ref === undefined || _ref === null)) {
			this[options.createdName] = new Date();
		}

		next();
	}
}
function timestamp ( schema , options ) { options = options || { } ; // Set defaults options = Object . assign ( { createdName : 'createdAt' , updatedName : 'updatedAt' , disableCreated : false , disableUpdated : false } , options ) ; // Add fields if not disabled if ( ! options . disableCreated ) { addSchemaField ( schema , options . createdName , { type : Date } ) ; } if ( ! options . disableUpdated ) { addSchemaField ( schema , options . updatedName , { type : Date } ) ; } // Add pre-save hook to save dates of creation and modification schema . pre ( 'save' , preSave ) ; /**
	 * Callback for the schema pre save
	 *
	 * @this mongoose.Schema
	 * @param {Function} next The next pre save callback in chain
	 * @returns {undefined}
	 */ function preSave ( next ) { const _ref = this . get ( options . createdName ) ; if ( ! options . disableUpdated ) { this [ options . updatedName ] = new Date ( ) ; } if ( ! options . disableCreated && ( _ref === undefined || _ref === null ) ) { this [ options . createdName ] = new Date ( ) ; } next ( ) ; } }
Mongoose plugin function which adds fields to track creation and modification dates
@param {Schema} schema the mongoose schema
@param {object} [options] the options object
@this mongoose.Schema
@access public
@returns {undefined}
Mongoose plugin function which adds fields to track creation and modification dates