alapa
Version:
A cutting-edge web development framework designed to revolutionize the way developers build modern web applications.
39 lines (38 loc) • 1.73 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrimaryColumn = PrimaryColumn;
const typeorm_1 = require("typeorm");
const mics_1 = require("../mics");
const uuid_1 = require("uuid");
function PrimaryColumn(type, options) {
return function (object, propertyName) {
if (type === "increment") {
// Use PrimaryGeneratedColumn with increment strategy
(0, typeorm_1.PrimaryGeneratedColumn)("increment", options)(object, propertyName);
return; // No need for a BeforeInsert hook
}
if (type === "uuid") {
// Use PrimaryGeneratedColumn with UUID strategy
(0, typeorm_1.PrimaryGeneratedColumn)("uuid", options)(object, propertyName);
return; // No need for a BeforeInsert hook
}
// Apply the @PrimaryColumn decorator for other types
(0, typeorm_1.PrimaryColumn)()(object, propertyName);
// Add a @BeforeInsert hook to generate the key
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const beforeInsertHook = function () {
if (!this[propertyName]) {
if (type === "md5") {
this[propertyName] = (0, mics_1.md5)(`${(0, mics_1.randomNumber)()}${(0, uuid_1.v4)()}`);
}
else {
this[propertyName] = (0, mics_1.randomNumber)(8); // Fallback if no type matches
}
}
};
// Apply the @BeforeInsert hook
(0, typeorm_1.BeforeInsert)()(object, "beforeInsert");
// Register the beforeInsert method on the prototype
object.constructor.prototype.beforeInsert = beforeInsertHook;
};
}
;