npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

happy-mongoose-timestamps

v1.4.0

Published

Mongoose plugin for createdAt and updatedAt timestamps.

Downloads

484

Readme

happy-mongoose-timestamps

A simple mongoose plugin to support createdAt and updatedAt fields, with blacklist support.

What?

Add this plugin to any Mongoose schema so that when any document based on the attached schema is updated then that document will have its updatedAt field set to the current date.

Also supports the handling of createdAt and/or updatedAt fields on .save operations.

You can blacklist specific fields or entire sub-objects so that they do not trigger the modification of updatedAt when documents are updated.

Installation

$ npm i --save happy-mongoose-timestamps

or

$ yarn add happy-mongoose-timestamps

Options

  • blacklist (default: []): Any fields which, when updated, should NOT trigger updatedAt to be modified.
  • createdAt (default: createdAt): Name of the schema field to store the created at value.
  • updatedAt (default: updatedAt): Name of the schema field to store the updated at value.
  • shouldUpdateSchema (default: false): Whether or not the plugin should add createdAt and updatedAt fields to the given schema, if they do not already exist.
  • disableSaveHook (default: false): Whether or not to disable the pre-save hook functionality.
  • forceCreateHook (default: false): Whether or not to force the saveHook if the item is new. Disabling saveHook will disable the "createHook" since a create action will call saveHook. So if you set disableSaveHook to true and forceCreateHook to false createdAt will never be created and updatedAt will not be set on the creation.
  • disableUpdateHook (default: false): Whether or not to disable the pre-update hook functionality.
  • disableUpdateOneHook (default: false): Whether or not to disable the pre-updateOne hook functionality.

Usage

import { Schema } from 'mongoose'
import HappyMongooseTimestamps from 'happy-mongoose-timestamps'

const YourSchema = { .. }
const schema = new Schema(YourSchema)

const options = {
  blacklist: ['foo', 'nested.field'], // Any updates containing these fields will not trigger updatedAt to be modified.
  shouldUpdateSchema: true // Will add createdAt and updatedAt fields to the given schema if they do not exist.
}

schema.plugin(HappyMongooseTimestamps, options)

So, given the blacklist specified above, if the current update operation looks like:

$set: {
  'foo': 'bar',
  'bar': 'baz'
}

Then updatedAt will not be modified as the update operation contains the field: foo. To be clear: the fields will still be updated as usual, the only difference is that updatedAt will not be modified.


The same rule applies for the following scenario:

Given the following update operation:

$set: {
  'foo.nested.value': 'bar'
}

Again, will not trigger updatedAt to be modified as the field foo is blacklisted, therefore any fields inside foo will not trigger updatedAt to be modified.


Finally, the following update operation:

$set: {
 'bar': 'baz'
}

WILL trigger updatedAt to be modified, as the field bar does not exist inside the blacklist.


Notes

  • The variable name blacklist is better than blackList.
  • Symbols are cool.

TO DO

  • Save hook needs testing and blacklist support.
  • Tests.