Code Monkey home page Code Monkey logo

loopback-ds-timestamp-mixin's Introduction

NPM

dependencies devDependencies Build Status Coverage Status

TIMESTAMPS

This module is designed for the Strongloop Loopback framework. It 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 is relatively new to the loopback framework so your loopback-datasource-juggler module must greater than version 2.23.0.

INSTALL

  npm install --save loopback-ds-timestamp-mixin

MIXINSOURCES

With [email protected] mixinSources have been implemented in a way which allows for loading this mixin without changes to the server.js file previously required.

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

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

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
    }
  }

BOOT 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.

In this example we change createdAt and updatedAt to createdOn and updatedOn, respectively. We also change the default required to false.

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

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

This package uses jscs and jshint as pretests to help maintain style and for error checking. 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

loopback-ds-timestamp-mixin's People

Contributors

clarkbw avatar jasonaden avatar

Watchers

James Cloos avatar Francesco Soncina avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.