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
| Constructor | Modifiers | Description |
|---|---|---|
| (constructor)(domain) | Constructs a new instance of the Model class |
Methods
Constructors
Model.(constructor)
Constructs a new instance of the Model class
Signature:
constructor(domain: string);
Parameters
| Parameter | Type | Description |
|---|---|---|
| domain | string |
Methods
add
Signature:
add(doc: Doc): void;
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
| 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
| 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
| Parameter | Type | Description |
|---|---|---|
| layout | AnyLayout | |
| _class | Ref<Class<Obj>> | |
| values | AnyLayout |
Returns:
attributeClass
Signature:
attributeClass(type: Type): Ref<Class<Doc>> | undefined;
Parameters
| Parameter | Type | Description |
|---|---|---|
| type | Type |
Returns:
attributeKey
Signature:
attributeKey(clazz: Classifier, key: string): string;
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
| Parameter | Type | Description |
|---|---|---|
| doc | Doc | |
| mixin | Ref<Mixin |
Returns:
T
classAttribute
Signature:
classAttribute(cls: Ref<Class<Obj>>, key: string): AttributeMatch;
Parameters
| Parameter | Type | Description |
|---|---|---|
| cls | Ref<Class<Obj>> | |
| key | string |
Returns:
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
| 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
| 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
| 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
| Parameter | Type | Description |
|---|---|---|
| _class | Ref<Class<Obj>> |
Returns:
find
Signature:
find<T extends Doc>(clazz: Ref<Class<T>>, query: DocumentQuery<T>, options?: FindOptions<T>): Promise<T[]>;
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
| 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
| 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
| 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
| Parameter | Type | Description |
|---|---|---|
| curValue | unknown | |
| attrClass | Ref<Class<Doc>> | |
| embedded | AnyLayout |
Returns:
flattenQuery
Convert a layout into ‘dot’ notation form if applicable.
Signature:
flattenQuery(_class: Ref<Class<Obj>>, layout: AnyLayout, useOperators?: boolean): AnyLayout;
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:
- an layout with replacement of embedded documents into ‘dot’ notation.
get
Signature:
get<T extends Doc>(id: Ref<T>): T;
Parameters
| Parameter | Type | Description |
|---|---|---|
| id | Ref |
Returns:
T
getAllAttributes
Signature:
getAllAttributes(_class: Ref<Class<Obj>>): AttributeMatch[];
Parameters
| Parameter | Type | Description |
|---|---|---|
| _class | Ref<Class<Obj>> |
Returns:
getClass
Signature:
getClass(_class: Ref<Class<Obj>>): Ref<Class<Obj>>;
Parameters
| Parameter | Type | Description |
|---|---|---|
| _class | Ref<Class<Obj>> |
Returns:
getClassHierarchy
Signature:
getClassHierarchy(cls: Ref<Class<Obj>>, top?: Ref<Class<Obj>>): Array<Ref<Class<Obj>>>;
Parameters
| Parameter | Type | Description |
|---|---|---|
| cls | Ref<Class<Obj>> | |
| top | Ref<Class<Obj>> |
Returns:
getClassMixins
Signature:
getClassMixins(cls: Ref<Class<Obj>>): Array<Ref<Doc>>;
Parameters
| Parameter | Type | Description |
|---|---|---|
| cls | Ref<Class<Obj>> |
Returns:
getDomain
Signature:
getDomain(id: Ref<Class<Doc>>): string;
Parameters
| Parameter | Type | Description |
|---|---|---|
| id | Ref<Class<Doc>> |
Returns:
string
getLayout
Signature:
getLayout(doc: Obj): AnyLayout;
Parameters
| Parameter | Type | Description |
|---|---|---|
| doc | Obj |
Returns:
getPrimaryKey
Signature:
getPrimaryKey(_class: Ref<Class<Obj>>): string | undefined;
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
| Parameter | Type | Description |
|---|---|---|
| doc | E | |
| clazz | Ref<Mixin |
Returns:
void
is
Signature:
is(_class: Ref<Class<Obj>>, a: Ref<Class<Obj>>): boolean;
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
| 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
| 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
| Parameter | Type | Description |
|---|---|---|
| _attributes | AnyLayout | |
| sort | DocumentSorting |
Returns:
boolean
loadDomain
Signature:
loadDomain(domain: string): Promise<Doc[]>;
Parameters
| Parameter | Type | Description |
|---|---|---|
| domain | string |
Returns:
Promise<Doc[]>
loadModel
Signature:
loadModel(model: Doc[]): void;
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
| 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
| 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
| Parameter | Type | Description |
|---|---|---|
| doc | E | |
| clazz | Ref<Mixin |
|
| values | Partial |
Returns:
void
objectsOfClass
Signature:
protected objectsOfClass(_class: Ref<Class<Doc>>): Doc[];
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
| 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
| 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
| Parameter | Type | Description |
|---|---|---|
| curValue | unknown | |
| attrClass | Ref<Class<Doc>> | |
| embedded | AnyLayout |
Returns:
remove
Signature:
remove(ctx: TxContext, _class: Ref<Class<Doc>>, _id: Ref<Doc>): Promise<void>;
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
| Parameter | Type | Description |
|---|---|---|
| doc | T |
Returns:
T
store
Signature:
store(ctx: TxContext, doc: Doc): Promise<void>;
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
| 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
| 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
| 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
| 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
| Parameter | Type | Description |
|---|---|---|
| doc | T | |
| _attributes | Partial<DocumentValue |
Returns:
T