rodrigogs / mongoose-timezone

Mongoose plugin to normalize stored dates timezone.
BSD 3-Clause "New" or "Revised" License
15 stars 6 forks source link
date mongodb mongoose nodejs offset timezone utc

mongoose-timezone

Build Status Code Climate Test Coverage dependencies Status devDependency Status npm npm version

A mongoose plugin to normalize stored dates timezone. By default mongo dates are stored in UTC format, this behaviour may cause problems when using advanced aggregation queries or when retrieving those dates from another system with different timezone.

mongoose-timezone basically adds the current timezone offset to the date before store and removes the offset when retrieving data. This way dates are kept proportional in the database and in the app.

Install

npm install mongoose-timezone --save

Usage

const mongoose = require('mongoose');
const timeZone = require('mongoose-timezone');

const Schema = new mongoose.Schema({
    date: Date,
    subDocument: {
        subDate: {
            type: Date,
        },
    },
});

// If no path is given, all date fields will be applied
Schema.plugin(timeZone, { paths: ['date', 'subDocument.subDate'] });
mongoose.model('Schema', Schema);

Notes

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request

License

Licence © Rodrigo Gomes da Silva