/
index.js
37 lines (31 loc) 路 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// @flow
import makeDecorator from '../../utils/common/makeDecorator'
import { type ColumnName } from '../../Schema'
import { ensureDecoratorUsedProperly } from '../common'
// Defines a model property representing a date
//
// Serializes dates to milisecond-precision Unix timestamps, and deserializes them to Date objects
// (but passes null values as-is)
//
// Pass the database column name as an argument
//
// Examples:
// @date('reacted_at') reactedAt: Date
const dateDecorator = makeDecorator(
(columnName: ColumnName) => (target: Object, key: string, descriptor: Object) => {
ensureDecoratorUsedProperly(columnName, target, key, descriptor)
return {
configurable: true,
enumerable: true,
get(): ?Date {
const rawValue = this._getRaw(columnName)
return typeof rawValue === 'number' ? new Date(rawValue) : null
},
set(date: ?Date): void {
const rawValue = date ? +new Date(date) : null
this._setRaw(columnName, rawValue)
},
}
},
)
export default dateDecorator