Model Class

Packages > @anticrm/core > Model

Model is a storage for Class descriptors and useful functions to match class instances to queries and apply values to them based on changes.

Signature:

export declare class Model implements Storage 

Implements: Storage

Constructors

List of constructors for this class
Constructor Modifiers Description
(constructor)(domain) Constructs a new instance of the Model class

Methods

List of methods on this class
Method Modifiers Description
add(doc)
as(doc, mixin) Cast to some mixin, if mixin is not present in class list it will not be added. isMixedIn should be used to ensure if mixin is on place, before modifications.
asMixin(obj, _class, action)
assign(layout, _class, values)
attributeClass(type)
attributeKey(clazz, key)
cast(doc, mixin) Perform as for a list of documents and if mixin is not present it will be added.
classAttribute(cls, key)
createDocument(_class, values, _id) Construct a new proper document with all desired fields.
createQuery(_class, _query, flatten) Creates a query with filled class and mixin information properly set. *
del(id)
dump()
extendsOfClass(_class)
find(clazz, query, options)
findAll(docs, _class, query, options) Find all document matching query
findOne(clazz, query)
findSync(clazz, query, options)
flattenArrayValue(curValue, attrClass, embedded)
flattenQuery(_class, layout, useOperators) Convert a layout into 'dot' notation form if applicable.
get(id)
getAllAttributes(_class)
getClass(_class)
getClassHierarchy(cls, top)
getClassMixins(cls)
getDomain(id)
getLayout(doc)
getPrimaryKey(_class)
includeMixin(doc, clazz) static
is(_class, a)
isMixedIn(obj, _class)
isPartialMatched(_class, _attributes, query) Method will check if passed values of object are matched in query.
isSortHasEffect(_attributes, sort) Check if operation to modify attribute has effect on sorting creteria
loadDomain(domain)
loadModel(model)
matchQuery(_class, doc, query) Matches query with document
mixin(id, clazz, values)
mixinDocument(doc, clazz, values)
objectsOfClass(_class)
pull(ctx, _class, _id, attribute, attributes)
push(ctx, _class, _id, attribute, attributes)
pushArrayValue(curValue, attrClass, embedded)
remove(ctx, _class, _id)
removeDocument(doc)
store(ctx, doc)
update(ctx, _class, _id, operations)
updateDocument(doc, operations) Perform update of document attributes
updateDocumentPull(doc, _attribute, _attributes)
updateDocumentPush(doc, _attribute, _attributes)
updateDocumentSet(doc, _attributes)

Constructors

Model.(constructor)

Constructs a new instance of the Model class

Signature:

constructor(domain: string);

Parameters

List of parameters
Parameter Type Description
domain string

Methods

add

Signature:

add(doc: Doc): void;

Parameters

List of parameters
Parameter Type Description
doc Doc

Returns:

void

as

Cast to some mixin, if mixin is not present in class list it will not be added. isMixedIn should be used to ensure if mixin is on place, before modifications.

Signature:

as<T extends Obj>(doc: Obj, mixin: Ref<Mixin<T>>): T;

Parameters

List of parameters
Parameter Type Description
doc Obj incoming document
mixin Ref<Mixin> a mixin class

Returns:

T

asMixin

Signature:

asMixin<T extends Doc>(obj: Obj, _class: Ref<Mixin<T>>, action: (doc: T) => void): void;

Parameters

List of parameters
Parameter Type Description
obj Obj
_class Ref<Mixin>
action (doc: T) => void

Returns:

void

assign

Signature:

assign(layout: AnyLayout, _class: Ref<Class<Obj>>, values: AnyLayout): AnyLayout;

Parameters

List of parameters
Parameter Type Description
layout AnyLayout
_class Ref<Class<Obj>>
values AnyLayout

Returns:

AnyLayout

attributeClass

Signature:

attributeClass(type: Type): Ref<Class<Doc>> | undefined;

Parameters

List of parameters
Parameter Type Description
type Type

Returns:

Ref<Class<Doc>> | undefined

attributeKey

Signature:

attributeKey(clazz: Classifier, key: string): string;

Parameters

List of parameters
Parameter Type Description
clazz Classifier
key string

Returns:

string

cast

Perform as for a list of documents and if mixin is not present it will be added.

Signature:

cast<T extends Doc>(doc: Doc, mixin: Ref<Mixin<T>>): T;

Parameters

List of parameters
Parameter Type Description
doc Doc
mixin Ref<Mixin>

Returns:

T

classAttribute

Signature:

classAttribute(cls: Ref<Class<Obj>>, key: string): AttributeMatch;

Parameters

List of parameters
Parameter Type Description
cls Ref<Class<Obj>>
key string

Returns:

AttributeMatch

createDocument

Construct a new proper document with all desired fields.

Signature:

createDocument<T extends Doc>(_class: Ref<Class<T>>, values: DocumentValue<T>, _id?: Ref<T>): T;

Parameters

List of parameters
Parameter Type Description
_class Ref<Class>
values DocumentValue
_id Ref optional id, if not sepecified will be automatically generated.

Returns:

T

createQuery

Creates a query with filled class and mixin information properly set. *

Signature:

createQuery<T extends Doc>(_class: Ref<Class<T>>, _query: DocumentQuery<T>, flatten?: boolean): {
        query: DocumentQuery<T>;
        classes: Array<Ref<Class<Obj>>>;
    };

Parameters

List of parameters
Parameter Type Description
_class Ref<Class> a class query is designed for.
_query DocumentQuery a query object to convert to.
flatten boolean use a flat key layout with dot notation.flatten queries are applicable only for mongoDB and not supported by model search operations.

Returns:

{ query: DocumentQuery<T>; classes: Array<Ref<Class<Obj>>>; }

del

Signature:

del(id: Ref<Doc>): void;

Parameters

List of parameters
Parameter Type Description
id Ref<Doc>

Returns:

void

dump

Signature:

dump(): Doc[];

Returns:

Doc[]

extendsOfClass

Signature:

protected extendsOfClass(_class: Ref<Class<Obj>>): Array<Class<Obj>>;

Parameters

List of parameters
Parameter Type Description
_class Ref<Class<Obj>>

Returns:

Array<Class<Obj>>

find

Signature:

find<T extends Doc>(clazz: Ref<Class<T>>, query: DocumentQuery<T>, options?: FindOptions<T>): Promise<T[]>;

Parameters

List of parameters
Parameter Type Description
clazz Ref<Class>
query DocumentQuery
options FindOptions

Returns:

Promise<T[]>

findAll

Find all document matching query

Signature:

protected findAll(docs: Doc[], _class: Ref<Class<Doc>>, query: AnyLayout, options?: FindOptions<any>): Doc[];

Parameters

List of parameters
Parameter Type Description
docs Doc[] document to find in
_class Ref<Class<Doc>> to match against
query AnyLayout to match
options FindOptions

Returns:

Doc[]

findOne

Signature:

findOne<T extends Doc>(clazz: Ref<Class<T>>, query: DocumentQuery<T>): Promise<T | undefined>;

Parameters

List of parameters
Parameter Type Description
clazz Ref<Class>
query DocumentQuery

Returns:

Promise<T | undefined>

findSync

Signature:

findSync<T extends Doc>(clazz: Ref<Class<Doc>>, query: DocumentQuery<T>, options?: FindOptions<T>): T[];

Parameters

List of parameters
Parameter Type Description
clazz Ref<Class<Doc>>
query DocumentQuery
options FindOptions

Returns:

T[]

flattenArrayValue

Signature:

flattenArrayValue(curValue: unknown, attrClass: Ref<Class<Doc>>, embedded: AnyLayout): PropertyType[];

Parameters

List of parameters
Parameter Type Description
curValue unknown
attrClass Ref<Class<Doc>>
embedded AnyLayout

Returns:

PropertyType[]

flattenQuery

Convert a layout into ‘dot’ notation form if applicable.

Signature:

flattenQuery(_class: Ref<Class<Obj>>, layout: AnyLayout, useOperators?: boolean): AnyLayout;

Parameters

List of parameters
Parameter Type Description
_class Ref<Class<Obj>> an query or sort options.
layout AnyLayout input layout.
useOperators boolean will allow to use $elemMatch and $all in case of query.

Returns:

AnyLayout

  • an layout with replacement of embedded documents into ‘dot’ notation.

get

Signature:

get<T extends Doc>(id: Ref<T>): T;

Parameters

List of parameters
Parameter Type Description
id Ref

Returns:

T

getAllAttributes

Signature:

getAllAttributes(_class: Ref<Class<Obj>>): AttributeMatch[];

Parameters

List of parameters
Parameter Type Description
_class Ref<Class<Obj>>

Returns:

AttributeMatch[]

getClass

Signature:

getClass(_class: Ref<Class<Obj>>): Ref<Class<Obj>>;

Parameters

List of parameters
Parameter Type Description
_class Ref<Class<Obj>>

Returns:

Ref<Class<Obj>>

getClassHierarchy

Signature:

getClassHierarchy(cls: Ref<Class<Obj>>, top?: Ref<Class<Obj>>): Array<Ref<Class<Obj>>>;

Parameters

List of parameters
Parameter Type Description
cls Ref<Class<Obj>>
top Ref<Class<Obj>>

Returns:

Array<Ref<Class<Obj>>>

getClassMixins

Signature:

getClassMixins(cls: Ref<Class<Obj>>): Array<Ref<Doc>>;

Parameters

List of parameters
Parameter Type Description
cls Ref<Class<Obj>>

Returns:

Array<Ref<Doc>>

getDomain

Signature:

getDomain(id: Ref<Class<Doc>>): string;

Parameters

List of parameters
Parameter Type Description
id Ref<Class<Doc>>

Returns:

string

getLayout

Signature:

getLayout(doc: Obj): AnyLayout;

Parameters

List of parameters
Parameter Type Description
doc Obj

Returns:

AnyLayout

getPrimaryKey

Signature:

getPrimaryKey(_class: Ref<Class<Obj>>): string | undefined;

Parameters

List of parameters
Parameter Type Description
_class Ref<Class<Obj>>

Returns:

string | undefined

includeMixin

Signature:

static includeMixin<E extends Obj, T extends Obj>(doc: E, clazz: Ref<Mixin<T>>): void;

Parameters

List of parameters
Parameter Type Description
doc E
clazz Ref<Mixin>

Returns:

void

is

Signature:

is(_class: Ref<Class<Obj>>, a: Ref<Class<Obj>>): boolean;

Parameters

List of parameters
Parameter Type Description
_class Ref<Class<Obj>>
a Ref<Class<Obj>>

Returns:

boolean

isMixedIn

Signature:

isMixedIn(obj: Obj, _class: Ref<Mixin<Obj>>): boolean;

Parameters

List of parameters
Parameter Type Description
obj Obj
_class Ref<Mixin<Obj>>

Returns:

boolean

isPartialMatched

Method will check if passed values of object are matched in query.

Signature:

isPartialMatched<T extends Doc>(_class: Ref<Class<Doc>>, _attributes: AnyLayout, query: DocumentQuery<T>): boolean;

Parameters

List of parameters
Parameter Type Description
_class Ref<Class<Doc>>
_attributes AnyLayout partial object values of operation.
query DocumentQuery a query.

Returns:

boolean

isSortHasEffect

Check if operation to modify attribute has effect on sorting creteria

Signature:

isSortHasEffect<T extends Doc>(_attributes: AnyLayout, sort: DocumentSorting<T>): boolean;

Parameters

List of parameters
Parameter Type Description
_attributes AnyLayout
sort DocumentSorting

Returns:

boolean

loadDomain

Signature:

loadDomain(domain: string): Promise<Doc[]>;

Parameters

List of parameters
Parameter Type Description
domain string

Returns:

Promise<Doc[]>

loadModel

Signature:

loadModel(model: Doc[]): void;

Parameters

List of parameters
Parameter Type Description
model Doc[]

Returns:

void

matchQuery

Matches query with document

Signature:

matchQuery<T extends Doc>(_class: Ref<Class<T>>, doc: Doc, query: DocumentQuery<T>): boolean;

Parameters

List of parameters
Parameter Type Description
_class Ref<Class> Accept documents with this only specific _class
doc Doc document to match against.
query DocumentQuery query to check.

Returns:

boolean

mixin

Signature:

mixin<E extends Doc, T extends E>(id: Ref<E>, clazz: Ref<Mixin<T>>, values: Omit<T, keyof E>): void;

Parameters

List of parameters
Parameter Type Description
id Ref
clazz Ref<Mixin>
values Omit

Returns:

void

mixinDocument

Signature:

mixinDocument<E extends Obj, T extends Obj>(doc: E, clazz: Ref<Mixin<T>>, values: Partial<Omit<T, keyof E>>): void;

Parameters

List of parameters
Parameter Type Description
doc E
clazz Ref<Mixin>
values Partial>

Returns:

void

objectsOfClass

Signature:

protected objectsOfClass(_class: Ref<Class<Doc>>): Doc[];

Parameters

List of parameters
Parameter Type Description
_class Ref<Class<Doc>>

Returns:

Doc[]

pull

Signature:

pull<T extends Obj>(ctx: TxContext, _class: Ref<Class<Doc>>, _id: Ref<Doc>, attribute: string, attributes: DocumentValue<T>): void;

Parameters

List of parameters
Parameter Type Description
ctx TxContext
_class Ref<Class<Doc>>
_id Ref<Doc>
attribute string
attributes DocumentValue

Returns:

void

push

Signature:

push<T extends Obj>(ctx: TxContext, _class: Ref<Class<Doc>>, _id: Ref<Doc>, attribute: string, attributes: DocumentValue<T>): void;

Parameters

List of parameters
Parameter Type Description
ctx TxContext
_class Ref<Class<Doc>>
_id Ref<Doc>
attribute string
attributes DocumentValue

Returns:

void

pushArrayValue

Signature:

pushArrayValue(curValue: unknown, attrClass: Ref<Class<Doc>>, embedded: AnyLayout): PropertyType[];

Parameters

List of parameters
Parameter Type Description
curValue unknown
attrClass Ref<Class<Doc>>
embedded AnyLayout

Returns:

PropertyType[]

remove

Signature:

remove(ctx: TxContext, _class: Ref<Class<Doc>>, _id: Ref<Doc>): Promise<void>;

Parameters

List of parameters
Parameter Type Description
ctx TxContext
_class Ref<Class<Doc>>
_id Ref<Doc>

Returns:

Promise<void>

removeDocument

Signature:

removeDocument<T extends Doc>(doc: T): T;

Parameters

List of parameters
Parameter Type Description
doc T

Returns:

T

store

Signature:

store(ctx: TxContext, doc: Doc): Promise<void>;

Parameters

List of parameters
Parameter Type Description
ctx TxContext
doc Doc

Returns:

Promise<void>

update

Signature:

update(ctx: TxContext, _class: Ref<Class<Doc>>, _id: Ref<Doc>, operations: TxOperation[]): Promise<void>;

Parameters

List of parameters
Parameter Type Description
ctx TxContext
_class Ref<Class<Doc>>
_id Ref<Doc>
operations TxOperation[]

Returns:

Promise<void>

updateDocument

Perform update of document attributes

Signature:

updateDocument<T extends Obj>(doc: T, operations: TxOperation[]): T;

Parameters

List of parameters
Parameter Type Description
doc T document to update
operations TxOperation[] define a set of operations to update for document.

Returns:

T

updateDocumentPull

Signature:

updateDocumentPull<P extends Doc, T extends Obj>(doc: P, _attribute: string, _attributes: DocumentQuery<T>): P;

Parameters

List of parameters
Parameter Type Description
doc P
_attribute string
_attributes DocumentQuery

Returns:

P

updateDocumentPush

Signature:

updateDocumentPush<P extends Doc, T extends Obj>(doc: P, _attribute: string, _attributes: DocumentValue<T>): P;

Parameters

List of parameters
Parameter Type Description
doc P
_attribute string
_attributes DocumentValue

Returns:

P

updateDocumentSet

Signature:

updateDocumentSet<T extends Obj>(doc: T, _attributes: Partial<DocumentValue<T>>): T;

Parameters

List of parameters
Parameter Type Description
doc T
_attributes Partial<DocumentValue>

Returns:

T