@aitianyu.cn/tianyu-store
Version:
tianyu storage for nodejs.
75 lines • 2.65 kB
JavaScript
;
/**@format */
Object.defineProperty(exports, "__esModule", { value: true });
exports.InstanceIdImpl = void 0;
const Defs_1 = require("../../types/Defs");
const ObjectUtils_1 = require("../../utils/ObjectUtils");
class InstanceIdImpl {
instanceKey;
constructor(instanceId) {
const instancePair = typeof instanceId === "string"
? ((0, ObjectUtils_1.parseJsonString)(instanceId) || [])
: Array.isArray(instanceId)
? instanceId
: instanceId.structure();
this.instanceKey = JSON.stringify(instancePair);
}
get id() {
return this.instanceKey;
}
get instanceId() {
const instancePair = this.structure();
return instancePair[instancePair.length - 1]?.entityId || "";
}
get storeType() {
const instancePair = this.structure();
return instancePair[instancePair.length - 1]?.storeType || "";
}
get parent() {
const instancePair = this.structure();
// if current instance id is invalid or current instance id is root
// to return a copy from current
if (instancePair.length <= 1) {
return new InstanceIdImpl(this);
}
const parentPair = [];
for (let i = 0; i < instancePair.length - 1; i++) {
parentPair.push(instancePair[i]);
}
return new InstanceIdImpl(parentPair);
}
get ancestor() {
const pair = this.structure();
return pair.length > 0 && pair[0].storeType === Defs_1.TIANYU_STORE_INSTANCE_BASE_ENTITY_STORE_TYPE
? new InstanceIdImpl([pair[0]])
: new InstanceIdImpl([]);
}
get entity() {
const pair = this.structure();
return pair.length > 0 && pair[0].storeType === Defs_1.TIANYU_STORE_INSTANCE_BASE_ENTITY_STORE_TYPE
? pair[0].entityId
: "";
}
isValid() {
const pair = this.structure();
return pair.length > 0 && pair[0].storeType === Defs_1.TIANYU_STORE_INSTANCE_BASE_ENTITY_STORE_TYPE;
}
toString() {
return this.instanceKey;
}
structure() {
return JSON.parse(this.instanceKey);
}
equals(other) {
return other.id === this.id;
}
compareTo(other) {
const otherStr = other.id;
return otherStr < this.id ? 1 : otherStr > this.id ? -1 : 0;
}
static isAncestor(instanceId) {
return instanceId.id === instanceId.ancestor.id;
}
}
exports.InstanceIdImpl = InstanceIdImpl;
//# sourceMappingURL=InstanceIdImpl.js.map