alapa
Version:
A cutting-edge web development framework designed to revolutionize the way developers build modern web applications.
48 lines (47 loc) • 1.84 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShortDateColumn = ShortDateColumn;
/* eslint-disable @typescript-eslint/no-explicit-any */
const typeorm_1 = require("typeorm");
const mics_1 = require("../mics");
function ShortDateColumn(type, options) {
return function (object, propertyName) {
// Apply the @Column decorator
(0, typeorm_1.Column)({
nullable: true,
type: "numeric",
default: 0,
...options,
})(object, propertyName);
// Define the beforeInsert hook
const beforeInsertHook = function () {
const date = new Date();
if (!this[propertyName]) {
if (type === "year") {
this[propertyName] = date.getFullYear();
}
else if (type === "month") {
this[propertyName] = date.getMonth() + 1;
}
else if (type === "day") {
this[propertyName] = date.getDate();
}
else if (type === "hour") {
this[propertyName] = date.getHours();
}
else if (type === "minute") {
this[propertyName] = date.getMinutes();
}
else if (type === "second") {
this[propertyName] = date.getSeconds();
}
}
};
// Generate a unique name for the beforeInsert hook
const uniqueName = `beforeInsert${type}${(0, mics_1.randomNumber)()}`;
// Apply the @BeforeInsert hook
(0, typeorm_1.BeforeInsert)()(object, uniqueName);
// Register the beforeInsert method on the prototype
object.constructor.prototype[uniqueName] = beforeInsertHook;
};
}
;