Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 27 additions & 35 deletions dist/js/entity/in_memory.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AnyObject } from "@mat3ra/esse/dist/js/esse/types";
import { JSONSchema } from "@mat3ra/esse/dist/js/esse/utils";
import { BaseInMemoryEntitySchema, EntityReferenceSchema } from "@mat3ra/esse/dist/js/types";
export declare enum ValidationErrorCode {
Expand All @@ -8,7 +7,7 @@ export declare enum ValidationErrorCode {
}
interface ErrorDetails {
error?: object | null;
json: AnyObject;
json: object;
schema: JSONSchema;
}
export declare class EntityError extends Error {
Expand All @@ -19,48 +18,49 @@ export declare class EntityError extends Error {
details?: ErrorDetails;
});
}
export declare class InMemoryEntity implements BaseInMemoryEntitySchema {
static create(config: object): InMemoryEntity;
type Schema = BaseInMemoryEntitySchema;
export declare class InMemoryEntity<S extends Schema = Schema> implements Schema {
static create<T extends InMemoryEntity<Schema>>(config: Schema): T;
static _isDeepCloneRequired: boolean;
static allowJsonSchemaTypesCoercing: boolean;
static readonly jsonSchema?: JSONSchema;
_json: AnyObject;
constructor(config?: object | InMemoryEntity);
prop<T = undefined>(name: string, defaultValue: T): T;
prop<T = undefined>(name: string): T | undefined;
_json: S;
constructor(config: S);
prop<K extends keyof S>(name: K, defaultValue: S[K]): S[K];
prop<K extends keyof S>(name: K): S[K] | undefined;
/**
* @summary Return a required prop, throwing an error if it doesn't exist or is undefined/null
*/
requiredProp<T>(name: string): T;
requiredProp<K extends keyof S>(name: K): S[K];
/**
* @summary Set a prop
*/
setProp(name: string, value: unknown): void;
setProp(name: keyof S, value: S[typeof name]): void;
/**
* @summary Remove a prop
*/
unsetProp(name: string): void;
unsetProp(name: keyof S): void;
/**
* Updates internal JSON. Works the same as Mongo's $set operator
* @see https://www.mongodb.com/docs/manual/reference/operator/update/set/#-set
*/
setProps(json?: AnyObject): this;
setProps(json?: Partial<S>): this;
/**
* @summary Array of fields to exclude from resulted JSON
*/
toJSON(exclude?: string[]): AnyObject;
toJSONSafe(exclude?: string[]): AnyObject;
toJSONQuick(exclude?: string[]): AnyObject;
toJSON(exclude?: (keyof S)[]): S;
toJSONSafe(exclude?: (keyof S)[]): S;
toJSONQuick(exclude?: (keyof S)[]): S;
/**
* @summary Clone this entity
*/
clone(extraContext?: object): this;
static validateData(data: AnyObject, clean?: boolean, jsonSchema?: import("json-schema").JSONSchema7 | undefined): AnyObject;
static validateData(data: object, clean?: boolean, jsonSchema?: import("json-schema").JSONSchema7 | undefined): object;
/**
* @summary Validate entity contents against schema
*/
validate(): void;
clean(config: AnyObject): AnyObject;
clean(config: S): S;
isValid(): boolean;
static get cls(): string;
get cls(): string;
Expand All @@ -71,26 +71,18 @@ export declare class InMemoryEntity implements BaseInMemoryEntitySchema {
* @returns identifying data
*/
getAsEntityReference(byIdOnly: true): {
_id: string;
_id: NonNullable<S["_id"]>;
};
getAsEntityReference(byIdOnly?: false): Required<EntityReferenceSchema>;
/**
* @summary Pluck an entity from a collection by name.
* If no name is provided and no entity has prop isDefault, return the first entity
* @param entities the entities
* @param entity the kind of entities
* @param name the name of the entity to choose
*/
getEntityByName(entities: InMemoryEntity[], entity: string, name: string): InMemoryEntity;
get id(): string;
set id(id: string);
get _id(): string;
set _id(id: string);
get schemaVersion(): string;
set schemaVersion(schemaVersion: string);
get systemName(): string;
set systemName(systemName: string);
get slug(): string;
get id(): S["_id"];
set id(id: S["_id"]);
get _id(): S["_id"];
set _id(id: S["_id"]);
get schemaVersion(): S["schemaVersion"];
set schemaVersion(schemaVersion: S["schemaVersion"]);
get systemName(): S["systemName"];
set systemName(systemName: S["systemName"]);
get slug(): S["slug"] | undefined;
get isSystemEntity(): boolean;
}
export type InMemoryEntityConstructor<T extends InMemoryEntity = InMemoryEntity> = new (...args: any[]) => T;
Expand Down
60 changes: 17 additions & 43 deletions dist/js/entity/in_memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,17 @@ class InMemoryEntity {
static create(config) {
return new this.prototype.constructor(config);
}
constructor(config = {}) {
this._json = {};
if (config instanceof InMemoryEntity) {
this._json = config.toJSON();
}
else {
this._json = this.constructor._isDeepCloneRequired
? (0, clone_1.deepClone)(config)
: (0, clone_1.clone)(config);
}
constructor(config) {
this._json = this.constructor._isDeepCloneRequired
? (0, clone_1.deepClone)(config)
: (0, clone_1.clone)(config);
}
/**
* @summary Return a prop or the default
*/
prop(name, defaultValue) {
var _a;
// `lodash.get` gets `null` when the value is `null`, but we still want a default value in this case, hence `||`
// `lodash.get` gets `null` when the value is `null`, but we still want a default value in this case, hence `??`
return (_a = (0, get_1.default)(this._json, name, defaultValue)) !== null && _a !== void 0 ? _a : defaultValue;
}
/**
Expand Down Expand Up @@ -114,7 +108,10 @@ class InMemoryEntity {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/set/#-set
*/
setProps(json = {}) {
Object.entries(json).forEach(([key, value]) => this.setProp(key, value));
Object.entries(json).forEach(([key, value]) => {
const keyType = key;
this.setProp(keyType, value);
});
return this;
}
/**
Expand Down Expand Up @@ -206,7 +203,7 @@ class InMemoryEntity {
return this.constructor.name;
}
getAsEntityReference(byIdOnly = false) {
if (!this.id) {
if (!this._id || !this.slug) {
throw new EntityError({
code: ValidationErrorCode.ENTITY_REFERENCE_ERROR,
details: {
Expand All @@ -216,64 +213,41 @@ class InMemoryEntity {
});
}
if (byIdOnly) {
return { _id: this.id };
return { _id: this._id };
}
return {
_id: this.id,
_id: this._id,
slug: this.slug,
cls: this.getClsName(),
};
}
/**
* @summary Pluck an entity from a collection by name.
* If no name is provided and no entity has prop isDefault, return the first entity
* @param entities the entities
* @param entity the kind of entities
* @param name the name of the entity to choose
*/
// eslint-disable-next-line class-methods-use-this
getEntityByName(entities, entity, name) {
let filtered;
if (!name) {
filtered = entities.filter((ent) => ent.prop("isDefault") === true);
if (!filtered.length)
filtered = [entities[0]];
}
else {
filtered = entities.filter((ent) => ent.prop("name") === name);
}
if (filtered.length !== 1) {
console.log(`found ${filtered.length} entity ${entity} with name ${name} expected 1`);
}
return filtered[0];
}
// Properties from BaseInMemoryEntitySchema
get id() {
return this.prop("_id", "");
return this.prop("_id");
}
set id(id) {
this.setProp("_id", id);
}
get _id() {
return this.prop("_id", "");
return this.prop("_id");
}
set _id(id) {
this.setProp("_id", id);
}
get schemaVersion() {
return this.prop("schemaVersion", "");
return this.prop("schemaVersion");
}
set schemaVersion(schemaVersion) {
this.setProp("schemaVersion", schemaVersion);
}
get systemName() {
return this.prop("systemName", "");
return this.prop("systemName");
}
set systemName(systemName) {
this.setProp("systemName", systemName);
}
get slug() {
return this.prop("slug", "");
return this.prop("slug");
}
get isSystemEntity() {
return Boolean(this.systemName);
Expand Down
3 changes: 1 addition & 2 deletions dist/js/entity/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { InMemoryEntity } from "./in_memory";
import { DefaultableInMemoryEntity, HasConsistencyChecksHasMetadataNamedDefaultableInMemoryEntity, HasMetadataNamedDefaultableInMemoryEntity, NamedDefaultableInMemoryEntity, NamedInMemoryEntity } from "./other";
import { ENTITY_SET_TYPES } from "./set/enums";
import { constructEntitySetFactoryByConfig } from "./set/factory";
import { InMemoryEntitySetMixin } from "./set/mixins";
import { orderedEntityInSetMixin, orderedEntitySetMixin } from "./set/ordered/mixins";
import * as selectorsForEntitySet from "./set/selectors";
export { InMemoryEntity, NamedInMemoryEntity, DefaultableInMemoryEntity, NamedDefaultableInMemoryEntity, HasMetadataNamedDefaultableInMemoryEntity, HasConsistencyChecksHasMetadataNamedDefaultableInMemoryEntity, ENTITY_SET_TYPES, constructEntitySetFactoryByConfig, selectorsForEntitySet, InMemoryEntitySetMixin, orderedEntitySetMixin, orderedEntityInSetMixin, };
export { InMemoryEntity, NamedInMemoryEntity, DefaultableInMemoryEntity, NamedDefaultableInMemoryEntity, HasMetadataNamedDefaultableInMemoryEntity, HasConsistencyChecksHasMetadataNamedDefaultableInMemoryEntity, ENTITY_SET_TYPES, selectorsForEntitySet, InMemoryEntitySetMixin, orderedEntitySetMixin, orderedEntityInSetMixin, };
4 changes: 1 addition & 3 deletions dist/js/entity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.orderedEntityInSetMixin = exports.orderedEntitySetMixin = exports.InMemoryEntitySetMixin = exports.selectorsForEntitySet = exports.constructEntitySetFactoryByConfig = exports.ENTITY_SET_TYPES = exports.HasConsistencyChecksHasMetadataNamedDefaultableInMemoryEntity = exports.HasMetadataNamedDefaultableInMemoryEntity = exports.NamedDefaultableInMemoryEntity = exports.DefaultableInMemoryEntity = exports.NamedInMemoryEntity = exports.InMemoryEntity = void 0;
exports.orderedEntityInSetMixin = exports.orderedEntitySetMixin = exports.InMemoryEntitySetMixin = exports.selectorsForEntitySet = exports.ENTITY_SET_TYPES = exports.HasConsistencyChecksHasMetadataNamedDefaultableInMemoryEntity = exports.HasMetadataNamedDefaultableInMemoryEntity = exports.NamedDefaultableInMemoryEntity = exports.DefaultableInMemoryEntity = exports.NamedInMemoryEntity = exports.InMemoryEntity = void 0;
const in_memory_1 = require("./in_memory");
Object.defineProperty(exports, "InMemoryEntity", { enumerable: true, get: function () { return in_memory_1.InMemoryEntity; } });
const other_1 = require("./other");
Expand All @@ -44,8 +44,6 @@ Object.defineProperty(exports, "NamedDefaultableInMemoryEntity", { enumerable: t
Object.defineProperty(exports, "NamedInMemoryEntity", { enumerable: true, get: function () { return other_1.NamedInMemoryEntity; } });
const enums_1 = require("./set/enums");
Object.defineProperty(exports, "ENTITY_SET_TYPES", { enumerable: true, get: function () { return enums_1.ENTITY_SET_TYPES; } });
const factory_1 = require("./set/factory");
Object.defineProperty(exports, "constructEntitySetFactoryByConfig", { enumerable: true, get: function () { return factory_1.constructEntitySetFactoryByConfig; } });
const mixins_1 = require("./set/mixins");
Object.defineProperty(exports, "InMemoryEntitySetMixin", { enumerable: true, get: function () { return mixins_1.InMemoryEntitySetMixin; } });
const mixins_2 = require("./set/ordered/mixins");
Expand Down
7 changes: 7 additions & 0 deletions dist/js/entity/mixins/BankableEntityMixin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { type BankableSchemaMixin } from "../../generated/BankableSchemaMixin";
import type { Constructor } from "../../utils/types";
import { InMemoryEntity } from "../in_memory";
import { type HashedEntity } from "./HashedEntityMixin";
export type BankableEntity = BankableSchemaMixin & HashedEntity;
export type BankableInMemoryEntityConstructor = Constructor<BankableEntity>;
export declare function bankableEntityMixin<T extends InMemoryEntity>(item: T): asserts item is T & BankableEntity;
9 changes: 9 additions & 0 deletions dist/js/entity/mixins/BankableEntityMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bankableEntityMixin = bankableEntityMixin;
const BankableSchemaMixin_1 = require("../../generated/BankableSchemaMixin");
const HashedEntityMixin_1 = require("./HashedEntityMixin");
function bankableEntityMixin(item) {
(0, BankableSchemaMixin_1.bankableSchemaMixin)(item);
(0, HashedEntityMixin_1.hashedEntityMixin)(item);
}
8 changes: 5 additions & 3 deletions dist/js/entity/mixins/HasMetadataMixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import type { MetadataSchema } from "@mat3ra/esse/dist/js/types";
import type { Constructor } from "../../utils/types";
import { InMemoryEntity } from "../in_memory";
type Metadata = MetadataSchema["metadata"];
export type HasMetadata<T extends Metadata = Metadata> = {
metadata?: T;
updateMetadata: (object: Partial<T>) => void;
type HasMetadataSchema<M extends Metadata = Metadata> = {
metadata?: M;
};
export type HasMetadata<M extends Metadata = Metadata> = HasMetadataSchema<M> & {
updateMetadata: (object: M) => void;
};
export type HasMetadataInMemoryEntityConstructor<T extends Metadata = Metadata> = Constructor<HasMetadata<T>>;
export declare function hasMetadataMixin<T extends InMemoryEntity>(item: T): asserts item is T & HasMetadata;
Expand Down
5 changes: 4 additions & 1 deletion dist/js/entity/mixins/HasScopeTrackMixin.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { InMemoryEntity } from "../in_memory";
type ScopeTrackSchema = {
scopeTrack?: unknown[];
};
type ScopeTrackDescriptor = {
get scopeTrack(): unknown[];
set scopeTrack(array: unknown[]);
};
export declare function hasScopeTrackMixin(item: InMemoryEntity): InMemoryEntity & ScopeTrackDescriptor;
export declare function hasScopeTrackMixin(item: InMemoryEntity): InMemoryEntity<ScopeTrackSchema> & ScopeTrackDescriptor;
export type HasScopeTrackInMemoryEntity = ReturnType<typeof hasScopeTrackMixin>;
export {};
3 changes: 2 additions & 1 deletion dist/js/entity/mixins/HasScopeTrackMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ function schemaMixin(item) {
// @ts-expect-error
const properties = {
get scopeTrack() {
return this.prop("scopeTrack", []);
var _a;
return (_a = this.prop("scopeTrack")) !== null && _a !== void 0 ? _a : [];
},
set scopeTrack(array) {
this.setProp("scopeTrack", array);
Expand Down
3 changes: 2 additions & 1 deletion dist/js/entity/mixins/HashedEntityMixin.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type HashedSchemaMixin } from "../../generated/HashedSchemaMixin";
import type { Constructor } from "../../utils/types";
import { InMemoryEntity } from "../in_memory";
export type HashedEntity = {
export type HashedEntity = HashedSchemaMixin & {
calculateHash(): string;
getHashObject?(): object;
};
Expand Down
2 changes: 2 additions & 0 deletions dist/js/entity/mixins/HashedEntityMixin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hashedEntityMixin = hashedEntityMixin;
const HashedSchemaMixin_1 = require("../../generated/HashedSchemaMixin");
const hash_1 = require("../../utils/hash");
function hashedEntityMixin(item) {
(0, HashedSchemaMixin_1.hashedSchemaMixin)(item);
// @ts-expect-error
const properties = {
/**
Expand Down
17 changes: 0 additions & 17 deletions dist/js/entity/mixins/ImportantSettingsProviderMixin.d.ts

This file was deleted.

25 changes: 0 additions & 25 deletions dist/js/entity/mixins/ImportantSettingsProviderMixin.js

This file was deleted.

Loading
Loading