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

loopback-ds-timestamp-mixin

v3.4.1

Published

A mixin to automatically generate created and updated Date attributes for loopback Models

Downloads

11,620

Readme

NPM

dependencies devDependencies Build Status Coverage Status

TIMESTAMPS

This module is designed for the Strongloop Loopback framework. It automatically adds createdAt and updatedAt attributes to any Model.

createdAt will be set to the current Date the by using the default property of the attribute.

updatedAt will be set for every update of an object through bulk updateAll or instance model.save methods.

This module is implemented with the before save Operation Hook which requires the loopback-datasource-juggler module greater than v2.23.0.

INSTALL

  npm i loopback-ds-timestamp-mixin --save

UPSERT ISSUES

With version 2.33.2 of this module the upsert validation was turned off. This may create issues for your project if upsert validation is required. If you require upsert validation, set the validateUpsert option to true, however most upserts will fail unless you supply the createdAt and updatedAt fields or set required option to false.

SERVER CONFIG

Add the mixins property to your server/model-config.json:

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "../node_modules/loopback-ds-timestamp-mixin",
      "../common/mixins"
    ]
  }
}

MODEL CONFIG

To use with your Models add the mixins attribute to the definition object of your model config.

  {
    "name": "Widget",
    "properties": {
      "name": {
        "type": "string",
      }
    },
    "mixins": {
      "TimeStamp" : true
    }
  }

MODEL OPTIONS

The attribute names createdAt and updatedAt are configurable. To use different values for the default attribute names add the following parameters to the mixin options.

You can also configure whether createdAt and updatedAt are required or not. This can be useful when applying this mixin to existing data where the required constraint would fail by default.

By setting the validateUpsert option to true you will prevent this mixin from overriding the default Model settings. With validation turned on most upsert operations will fail with validation errors about missing the required fields like createdAt or updatedAt.

This mixin uses console logs to warn you whenever something might need your attention. If you would prefer not to receive these warnings, you can disable them by setting the option silenceWarnings to false on a per model basis.

In this example we change createdAt and updatedAt to createdOn and updatedOn, respectively. We also change the default required to false and set validateUpsert to true. We also disable console warnings with silenceWarnings.

  {
    "name": "Widget",
    "properties": {
      "name": {
        "type": "string",
      }
    },
    "mixins": {
      "TimeStamp" : {
        "createdAt" : "createdOn",
        "updatedAt" : "updatedOn",
        "required" : false,
        "validateUpsert": true,
        "silenceWarnings": false
      }
    }
  }

NOTE for database MySQL and Postgres options

When you use database options for MySQL and the like beware that you may have to use the columnName value configured for the database instead of the loopback configured name.

In the following example for the Widget object your createdAt field should equal the columnName which would be created_at.

{
  "name": "Widget",
  "properties": {
    "createdAt": {
      "type": "Date",
       "required": true,
       "length": null,
       "precision": null,
       "scale": null,
       "mysql": {
         "columnName": "created_at",
         "dataType": "datetime",
         "dataLength": null,
         "dataPrecision": null,
         "dataScale": null,
         "nullable": "N"
       }
  }
  }
}

Thus the configuration looks like this for the above example.

  {
    "name": "Widget",
    "properties": {
      "name": {
        "type": "string",
      }
    },
    "mixins": {
      "TimeStamp" : {
        "createdAt" : "created_at"
      }
    }
  }

Please see issue #19 for more information on database options.

OPERATION OPTIONS

By passing in additional options to an update or save operation you can control when this mixin updates the updatedAt field. The passing true to the option skipUpdatedAt will skip updating the updatedAt field.

In this example we assume a book object with the id of 2 already exists. Normally running this operation would change the updatedAt field to a new value.

Book.updateOrCreate({name: 'New name', id: 2}, {skipUpdatedAt: true}, function(err, book) {
  // book.updatedAt will not have changed
});

DEVELOPMENT

This package is written in ES6 JavaScript, check out @getify/You-Dont-Know-JS if you want to learn more about ES6.

Source files are located in the es6 directory. Edit the source files to make changes while running gulp in the background. Gulp is using babel to transform the es6 JavaScript into node compatible JavaScript.

  gulp

TESTING

For error checking and to help maintain style this package uses eslint as a pretest. All test are run against the transformed versions of files, not the es6 versions.

Run the tests in the test directory.

  npm test

Run with debugging output on:

  DEBUG='loopback:mixins:time-stamp' npm test

LICENSE

ISC