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 🙏

© 2025 – Pkg Stats / Ryan Hefner

bookshelf-touch

v1.1.4

Published

Automatically update timestamps on your Bookshelf models when saving

Downloads

1,472

Readme

bookshelf-touch

This Bookshelf.js plugin automatically updates your timestamps when saving your Bookshelf models.

npm version Dependency Status Build Status Coverage Status License

Developed at the Media Engineering Institute (HEIG-VD).

Usage

const knex = require('knex')({ ... });
const bookshelf = require('bookshelf')(knex);
const touch = require('bookshelf-touch');

bookshelf.plugin(touch);

const MyModel = bookshelf.Model.extend({
  tableName: 'my_table',
  // Define which timestamps you want to be automatically updated
  // (supports "created_at" and "updated_at" by default, but you
  // can configure more).
  timestamps: [ 'created_at', 'updated_at' ]
});

const model = new MyModel({
  name: 'Awesome'
});

// Both timestamps are automatically set when the model is created.
model.save();

// Only the updated_at timestamp is updated when the model is saved.
model.name = 'Indeed';
model.save();

// You can trigger an update of the timestamps with the touch method
// (this does not save the model):
model.touch();

Configuration

The timestamps property of your model can be:

  • true to automatically update both created_at and updated_at.

  • A string indicating which of created_at or updated_at should be updated.

  • An array containing either created_at, updated_at or both to indicate which should be updated.

  • An object mapping timestamp columns as keys to timestamp configurations as value. See Timestamp configuration below.

  • Do not define it or set it to false to disable the plugin on that model.

Timestamp configuration

You may configure the behavior of timestamps (and add timestamps) yourself. This is what the default configuration looks like:

const MyModel = bookshelf.Model.extend({
  tableName: 'my_table',
  timestamps: {
    created_at: {}
    updated_at: {
      default: record => record.get('created_at'),
      update: true
    }
  }
});

A timestamp configuration object has 2 options:

  • default - A function that takes the record as an argument and should return a default value. This function will be called if the record has no value for that timestamp, or when updating the timestamp. If it returns a falsy value, a new date is created instead. If the default function is not specified (like for created_at), a new date is also created as the default value.
  • update - Whether to update the timestamp when the record is touched. This defaults to false. As expected, it is not set for created_at and set to true for updated_at in the default configuration.

You may define new timestamps by adding them to the configuration. Use true and false to enable/disable default timestamps:

const MyModel = bookshelf.Model.extend({
  tableName: 'my_table',
  timestamps: {
    created_at: true,
    updated_at: false,
    amended_at: {
      default: record => new Date(11029388)
    }
  }
});