@michaelray/mongoose-timezone

0.0.7 • Public • Published

mongoose-timezone

一个mongoose插件,用于规范存储的日期时区。默认情况下,mongo日期以UTC格式存储,如果服务器存放在其它时区,则显示会有差别。该插件用于渲染服务器所在时区的时间。

A mongoose plugin used to standardize the stored date and time zone. By default, mongo dates are stored in UTC format, but if the server is stored in another time zone, the display may differ. This plugin is used to render the time zone of the server.

Install

npm install @michaelray/mongoose-timezone --save

Usage

const mongoose = require('mongoose');
const {timeZone}  = require('@michaelray/mongoose-timezone');

// baseSchema.ts is in your path
import { baseSchema } from "@/common/mongoose/baseSchema";

const schemaData = Object.assign({},baseSchema,{
	date: Date,
	subDocument: {
		subDate: {
			type: Date,
		},
	},
})

const Schema = new mongoose.Schema(schemaData)

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

baseSchema.ts

export const baseSchema = {
	createdAt: {
		type: Date,
		get: (v: Date) => new Date(v),
	},
	updatedAt: {
		type: Date,
		get: (v: Date) => new Date(v),
	},
}

Readme

Keywords

none

Package Sidebar

Install

npm i @michaelray/mongoose-timezone

Weekly Downloads

1

Version

0.0.7

License

BSD-3-Clause

Unpacked Size

3.96 kB

Total Files

3

Last publish

Collaborators

  • michaelray1982