forked from clarkbw/loopback-ds-timestamp-mixin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtime-stamp.js
132 lines (111 loc) · 4.24 KB
/
time-stamp.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _debug2 = require('./debug');
var _debug3 = _interopRequireDefault(_debug2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var debug = (0, _debug3.default)();
var warn = (0, _debug3.default)(); // create a namespaced warning
warn.log = console.warn.bind(console); // eslint-disable-line no-console
exports.default = function (Model) {
var bootOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
debug('TimeStamp mixin for Model %s', Model.modelName);
var options = _extends({
createdAt: 'createdAt',
updatedAt: 'updatedAt',
deletedAt: 'deletedAt',
required: true,
validateUpsert: false, // default to turning validation off
silenceWarnings: false
}, bootOptions);
debug('options', options);
// enable our warnings via the options
warn.enabled = !options.silenceWarnings;
if (!options.validateUpsert && Model.settings.validateUpsert) {
Model.settings.validateUpsert = false;
warn(Model.pluralModelName + ' settings.validateUpsert was overriden to false');
}
if (Model.settings.validateUpsert && options.required) {
warn('Upserts for ' + Model.pluralModelName + ' will fail when\n validation is turned on and time stamps are required');
}
Model.defineProperty(options.createdAt, {
type: Date,
required: options.required,
defaultFn: 'now',
mysql: {
columnName: 'created_at',
default: 'CURRENT_TIMESTAMP',
dataType: 'timestamp'
}
});
Model.defineProperty(options.updatedAt, {
type: Date,
required: false,
mysql: {
columnName: 'updated_at',
dataType: 'timestamp',
dataLength: null,
dataPrecision: null,
dataScale: null,
nullable: 'Y'
}
});
Model.defineProperty(options.deletedAt, {
type: Date,
required: false,
mysql: {
columnName: 'deleted_at',
dataType: 'timestamp',
dataLength: null,
dataPrecision: null,
dataScale: null,
nullable: 'Y'
}
});
Model.observe('before save', function (ctx, next) {
debug('ctx.options', ctx.options);
if (ctx.options && ctx.options.skipUpdatedAt) {
return next();
}
if (ctx.instance) {
if (ctx.isNewInstance) {
ctx.instance[options.createdAt] = new Date();
}
debug('%s.%s before save: %s', ctx.Model.modelName, options.updatedAt, ctx.instance.id);
ctx.instance[options.updatedAt] = new Date();
ctx.instance[options.deletedAt] = null;
} else {
debug('%s.%s before update matching %j', ctx.Model.pluralModelName, options.updatedAt, ctx.where);
ctx.data[options.updatedAt] = new Date();
}
return next();
});
/**
* Watches destroyAll(), deleteAll(), destroyById() , deleteById(), prototype.destroy(),
* prototype.delete() methods
* and instead of deleting object, sets properties deletedAt and isDeleted.
*/
Model.observe('before delete', function (ctx, next) {
Model.updateAll(ctx.where, _defineProperty({}, options.deletedAt, new Date())).then(function () {
next(null);
});
});
/**
* When ever model tries to access data, we add by default isDeleted: false to where query
* if there is already in query isDeleted property, then we do not modify query
*/
Model.observe('access', function (ctx, next) {
// If we want the deleted ones
if (ctx.query.isDeleted) return next();
if (ctx.query.where && JSON.stringify(ctx.query.where).indexOf('isDeleted') === 1) return next();
// By defaut set deletedAt to null
if (!ctx.query.where) ctx.query.where = {};
ctx.query.where[options.deletedAt] = null;
next();
});
};
module.exports = exports.default;
//# sourceMappingURL=time-stamp.js.map