• This repository has been archived on 10/Nov/2023
  • Stars
    star
    115
  • Rank 305,916 (Top 7 %)
  • Language
    JavaScript
  • License
    ISC License
  • Created almost 10 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A mixin to automatically generate createdAt and updatedAt Date attributes for loopback Models

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 true 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": true,
        "index": true
      }
    }
  }

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

More Repositories

1

jest-localstorage-mock

A module to mock window.localStorage and window.sessionStorage in Jest
JavaScript
320
star
2

jest-webextension-mock

A module to mock WebExtensions in Jest
JavaScript
93
star
3

probot-in-progress

A Probot that marks issues with a specific label when an in progress PR indicates they will be fixed
JavaScript
14
star
4

about-history

A firefox add-on with an awesome history view
JavaScript
13
star
5

searchspot

Firefox add-on for better searching. Multiple search suggestions from a number of different providers. Local search based on GeoLocation. Better search engine preferences management.
JavaScript
13
star
6

circleci-update-yarn-lock

Script to udpate your yarn.lock file from CircleCI, run after greenkeeper changes
Shell
10
star
7

indexed-db-storage

indexed-db-storage is a promise based wrapper around the indexed-db storage layer built for use with the mozilla addon-sdk
JavaScript
10
star
8

addon-pathfinder

The Add-on Pathfinder is the collection of Jetpack modules.
JavaScript
9
star
9

task-list-zero-bot

A probot that blocks merging when there are unchecked checkboxes in your PR description
JavaScript
9
star
10

mdn-reference-search

Search MDN for JS and CSS references via the browser URL bar
JavaScript
6
star
11

npm-package-search

Search for NPM Package via the browser URL bar
JavaScript
6
star
12

google-calendar-tab

Use Thunderbird and Google Calendar? This opens Google Calendar in a tab for you with a handle tab button
JavaScript
6
star
13

extract-zds

Extract ZD ticket references from the current issue
TypeScript
5
star
14

FirefoxAndroidPhoneBuddy

Android application for connecting with your Firefox Browser
Java
4
star
15

thunderbird-bugzilla-link-grabber

Extracts and cleans bugzilla links from Thunderbird Messages
JavaScript
4
star
16

fetch-community-topics

An Action to fetch topics from the GitHub community
TypeScript
4
star
17

browser-search-engine

Mozilla Add-on SDK module for the nsIBrowserSearchService
JavaScript
3
star
18

no-merge-commits

A Probot app that doesn't allow for merge commits
JavaScript
3
star
19

clarkbw

AMA
3
star
20

docker-build

TypeScript
3
star
21

extract-community

TypeScript
3
star
22

loopback-example-email-templates

example application that uses proposed email templates
CSS
3
star
23

clarkbw.github.io

HTML
3
star
24

container-actions-privs

Dockerfile
3
star
25

loopback-mixins-example

DEPRECATED: Example of using mixins within loopback
3
star
26

webext-omnibox-highlight

A WebExtension module for highlighting matches with the omnibox API for Chrome and Firefox
JavaScript
3
star
27

neon-nextjs-drizzle-auth0

TypeScript
2
star
28

committed-mozillians

Photos of Mozillians as they commit code
JavaScript
2
star
29

test-node-package

2
star
30

Ask-Which-Firefox-Window

A Firefox add-on that asks you which window it was supposed to open a tab in
Shell
2
star
31

mozilla-readability-read

JavaScript
2
star
32

devtools-metrics

Metrics Dashboard for Firefox DevTools
JavaScript
2
star
33

mlb-proxy

vcap proxy for mlb data to route around CORS issue
JavaScript
2
star
34

npr-demo

Demo of an HTML5 NPR App designed for all browsers
JavaScript
2
star
35

open-in-firefox

JavaScript
2
star
36

setup-docker

An Action for securely logging a docker client into a Docker registry
TypeScript
2
star
37

zendesk-view

An Action for querying a Zendesk view
TypeScript
2
star
38

about-new-tab

JavaScript
2
star
39

about-debugging

Test layout page for the Firefox about:debugging page
2
star
40

searchspot-server

The server component to the searchspot add-on, this isn't required to run but collects statistics on searchspot usage
JavaScript
2
star
41

github-employees-only-action

Outputs all non-empoyees of GitHub to an array
JavaScript
2
star
42

mlb

An unofficial MLB web app
JavaScript
2
star
43

apps-demo

2
star
44

drizzle

Python
2
star
45

fx-sdk-geocode

Mozilla Add-on SDK module for the geocoding geolocation results
JavaScript
2
star
46

devtools-inspector

Demo of a devtools inspector
2
star
47

moz-apps

A suite of omnigraffle stencils for Mozilla Apps Designers
2
star
48

Thunderbird-Mega-Preferences

A new look at Thunderbird preferences orgranized and cleanly designed
2
star
49

test-probot-repo

This is a test of the emergency probot system, this is only a test
2
star
50

loopback-failing-upsert

JavaScript
2
star
51

jekyll-test

Generate files, but then hide those generated files from code review
HTML
2
star
52

cards

JavaScript
2
star
53

reload-error

Firefox add-on to reload the network error pages when the network returns
JavaScript
2
star
54

rails-translate-demo

2
star
55

autocompleteoff

Turns off the autocomplete attribute in any form that accepts a password
JavaScript
2
star
56

about-what

Used for creating about: URIs in Firefox
JavaScript
2
star
57

browserid-design

Designs for testing the latest browserid work
JavaScript
2
star
58

fantastic-bar

A future url bar prototype for Firefox.
JavaScript
2
star
59

pocket-addon

Firefox add-on for Pocket
JavaScript
2
star
60

telemetry-promises

A Promise based JS API for Mozilla Telemetry
1
star
61

netflixtab

a new take on the new tab page for firefox
JavaScript
1
star
62

jsonviewer.html

JavaScript
1
star
63

moar-private

JavaScript
1
star
64

loopback-user-email

Email templates for handling user management within the Strongloop Loopback framework
1
star
65

npr-temp

initial npr demo to get our real demo working
JavaScript
1
star
66

replaybrowseractions

1
star
67

yelp-search

Yelp search WebExtension
JavaScript
1
star
68

vizzini

gives you a button that will take your browser back to it's original homepage state
JavaScript
1
star
69

ignoreplus

ignore google plus by scrolling it out of view as google properties load
JavaScript
1
star
70

victoria

1
star
71

fx-sdk-geolocation

Mozilla Add-on SDK module for the nsIDOMGeoGeolocation
JavaScript
1
star
72

Thunderbird-Captive-Portal-Detector

Looks for those wifi login pages when Thunderbird starts and opens a tab to login into the network so you can check your mail
JavaScript
1
star