@hiki9/rich-domain
Version:
Rich Domain is a library that provides a set of tools to help you build complex business logic in NodeJS using Domain Driven Design principles.
59 lines • 1.53 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Id = void 0;
const crypto_1 = require("crypto");
const short_uuid_1 = __importDefault(require("short-uuid"));
/**
* @description Identity to Entity and Aggregates
* @method create
* @param value as string
*/
const short = (0, short_uuid_1.default)();
class Id {
constructor(id, isNew) {
if (typeof id === 'undefined') {
const uuid = (0, crypto_1.randomUUID)();
this._value = short.fromUUID(uuid);
this._isNew = true;
}
else {
const isString = typeof id === 'string';
this._value = isString ? id : String(id);
this._isNew = false;
}
if (typeof isNew === 'boolean') {
this._isNew = isNew;
}
}
static generate() {
return new Id();
}
setAsNew() {
this._isNew = true;
}
get value() {
return this._value;
}
get longValue() {
return short.toUUID(this._value);
}
isNew() {
return this._isNew;
}
isEqual(id) {
return this.value === id.value;
}
cloneAsNew() {
const newUUID = new Id(this._value);
newUUID.setAsNew();
return newUUID;
}
clone() {
return new Id(this._value);
}
}
exports.Id = Id;
//# sourceMappingURL=ids.js.map