Class Model<M>Abstract

Abstract base class to implement data Models. By extending this abstract base class while providing the Symbol.toStringTag property containing the singular name of the resulting data Model, type safe data handling, i.e., retrieval, mutation and storage, can easily be achieved. Through the use of the static- and instance-scoped polymorphic this, all inherited operations warrant type safety and provide intellisense.

Example

Extend the Model base class:

import { Model, Property } from '@sgrud/data';

export class ExampleModel extends Model<ExampleModel> {

⁠@Property(() => String)
public field: string?;

protected [Symbol.toStringTag]: string = 'ExampleModel';

}

See

Querier

Type Parameters

Constructors

  • Public constructor. The constructor of all classes extending the abstract Model base class, unless explicitly overridden, behaves analogous to the instance-scoped assign method, as it takes all supplied parts and assigns them to the instantiated and returned Model. The constructor furthermore wires some internal functionality, e.g., creates a new changes BehaviorSubject which emits every mutation this Model instance experiences etc.

    Type Parameters

    • M extends Model<any> = any

    Parameters

    Returns Model<M>

Properties

[hasMany]?: Record<keyof M, (() => unknown)>

hasMany symbol property used by the HasMany decorator.

Type declaration

    • (): unknown
    • Returns unknown

[hasOne]?: Record<keyof M, (() => unknown)>

hasOne symbol property used by the HasOne decorator.

Type declaration

    • (): unknown
    • Returns unknown

[property]?: Record<keyof M, (() => unknown)>

property symbol property used by the Property decorator.

Type declaration

    • (): unknown
    • Returns unknown

created?: Date

Transient creation Date of this Model instance.

Decorator

Property

modified?: Date

Transient modification Date of this Model instance.

Decorator

Property

uuid?: string

UUID of this Model instance.

Decorator

Property

[toStringTag]: string

Enforced well-known Symbol.toStringTag property containing the singular name of this Model. The value of this property represents the repository which all instances of this Model are considered to belong to. In detail, the different operations committed through this Model are derived from this singular name (and the corresponding pluralized form).

Example

Provide a valid symbol property:

import { Model } from '@sgrud/data';

export class ExampleModel extends Model<ExampleModel> {

protected [Symbol.toStringTag]: string = 'ExampleModel';

}
changes: BehaviorSubject<M>

BehaviorSubject emitting every time this Model instance experiences changes.

static: Model.Type<M>

Type-asserted alias for the static Model context.

Accessors

Methods

  • Static commit method. Calling this method on a class extending the abstract Model base class, while supplying an operation and all its embedded variables, will dispatch the Querier.Operation to the respective Model repository through the highest priority Querier or, if no Querier is compatible, an error is thrown. This method is the entry point for all Model-related data transferral and is internally called by all other distinct methods of the Model.

    Type Parameters

    Parameters

    Returns Observable<unknown>

    An Observable of the commitmed operation.

    Throws

    An Observable ReferenceError on incompatibility.

    Example

    commit a query-type operation:

    import { ExampleModel } from './example-model';

    ExampleModel.commit(`query queryExample(variable: $variable) {
    result {
    field
    }
    }`, {
    variable: 'value'
    }).subscribe(console.log);
  • Static deleteAll method. Calling this method on a class extending the Model, while supplying an array of uuids, will dispatch the deletion of all Model instances identified by these UUIDs to the respective Model repository by internally calling commit with suitable arguments. Through this method, bulk-deletions from the respective Model repository can be achieved.

    Type Parameters

    Parameters

    • this: Model.Type<T>

      The explicit static polymorphic this parameter.

    • uuids: string[]

      An array of uuids of Models to be deleted.

    Returns Observable<unknown>

    An Observable of the deletion.

    Example

    Drop all model instances by UUIDs:

    import { ExampleModel } from './example-model';

    ExampleModel.deleteAll([
    'b050d63f-cede-46dd-8634-a80d0563ead8',
    'a0164132-cd9b-4859-927e-ba68bc20c0ae',
    'b3fca31e-95cd-453a-93ae-969d3b120712'
    ]).subscribe(console.log);
  • Static deleteOne method. Calling this method on a class extending the Model, while supplying an uuid, will dispatch the deletion of the Model instance identified by this UUID to the respective repository by internally calling the commit operation with suitable arguments. Through this method, the deletion of a single Model instance from the respective Model repository can be achieved.

    Type Parameters

    Parameters

    • this: Model.Type<T>

      The explicit static polymorphic this parameter.

    • uuid: string

      The uuid of the Model instance to be deleted.

    Returns Observable<unknown>

    An Observable of the deletion.

    Example

    Drop one model instance by UUID:

    import { ExampleModel } from './example-model';

    ExampleModel.deleteOne(
    '18f3aa99-afa5-40f4-90c2-71a2ecc25651'
    ).subscribe(console.log);
  • Static findAll method. Calling this method on a class extending the abstract Model base class, while supplying a filter to match Model instances by and a graph containing the fields to be included in the result, will dispatch a lookup operation to the respective repository by internally calling the commit operation with suitable arguments. Through this method, the bulk-lookup of Model instances from the respective Model repository can be achieved.

    Type Parameters

    Parameters

    Returns Observable<Results<T>>

    An Observable of the find operation.

    Example

    Lookup all UUIDs for model instances modified between two dates:

    import { ExampleModel } from './example-model';

    ExampleModel.findAll({
    expression: {
    conjunction: {
    operands: [
    {
    entity: {
    operator: 'GREATER_OR_EQUAL',
    path: 'modified',
    value: new Date('2021-01-01')
    }
    },
    {
    entity: {
    operator: 'LESS_OR_EQUAL',
    path: 'modified',
    value: new Date('2021-12-12')
    }
    }
    ],
    operator: 'AND'
    }
    }
    }, [
    'uuid',
    'field'
    ]).subscribe(console.log);
  • Static findOne method. Calling this method on a class extending the abstract Model base class, while supplying the shape to match the Model instance by and a graph describing the fields to be included in the result, will dispatch the lookup operation to the respective repository by internally calling the commit operation with suitable arguments. Through this method, the retrieval of one specific Model instance from the respective Model repository can be achieved.

    Type Parameters

    Parameters

    Returns Observable<T>

    An Observable of the find operation.

    Example

    Lookup one model instance by UUID:

    import { ExampleModel } from './example-model';

    ExampleModel.findOne({
    id: '2cfe7609-c4d9-4e4f-9a8b-ad72737db48a'
    }, [
    'uuid',
    'modified',
    'field'
    ]).subscribe(console.log);
  • Static saveAll method. Calling this method on a class extending the abstract Model base class, while supplying a list of models which to save and a graph describing the fields to be returned in the result, will dispatch the save operation to the respective Model repository by internally calling the commit operation with suitable arguments. Through this method, bulk-persistance of Model instances from the respective Model repository can be achieved.

    Type Parameters

    Parameters

    • this: Model.Type<T>

      The explicit static polymorphic this parameter.

    • models: T[]

      An array of Models to be saved.

    • graph: Graph<T>

      The Model.Graph of fields to be returned.

    Returns Observable<T[]>

    An Observable of the save operation.

    Example

    Persist multiple Models:

    import { ExampleModel } from './example-model';

    ExampleModel.saveAll([
    new ExampleModel({ field: 'example_1' }),
    new ExampleModel({ field: 'example_2' }),
    new ExampleModel({ field: 'example_3' })
    ], [
    'uuid',
    'modified',
    'field'
    ]).subscribe(console.log);
  • Static saveOne method. Calling this method on a class extending the abstract Model base class, while supplying a model which to save and a graph describing the fields to be returned in the result, will dispatch the save operation to the respective Model repository by internally calling the commit operation with suitable arguments. Through this method, persistance of one specific Model instance from the respective Model repository can be achieved.

    Type Parameters

    Parameters

    Returns Observable<T>

    An Observable of the save operation.

    Example

    Persist a model:

    import { ExampleModel } from './example-model';

    ExampleModel.saveOne(new ExampleModel({ field: 'example' }), [
    'uuid',
    'modified',
    'field'
    ]).subscribe(console.log);
  • Static serialize method. Calling this method on a class extending the Model, while supplying a model which to serialize and optionally enabling shallow serialization, will return the serialized Model.Shape of the Model, i.e., a plain JSON representation of all Model fields, or undefined, if the supplied model does not contain any fields or values. By serializing shallowly, only such properties defined on the supplied model are included (which means, all one-to-one and one-to-many associations are ignored). Through this method, the serialization of one specific Model instance from the respective Model repository can be achieved.

    Type Parameters

    Parameters

    • this: Model.Type<T>

      The explicit static polymorphic this parameter.

    • model: T

      The Model which is to be serialized.

    • shallow: boolean = false

      Whether to serialize the Model shallowly.

    Returns undefined | Shape<T>

    The Model.Shape of the Model or undefined.

    Example

    serialize a model:

    import { ExampleModel } from './example-model';

    const model = new ExampleModel({ field: 'example' });
    const shape = ExampleModel.serialize(model);
    console.log(shape); // { field: 'example' }
  • Static treemap method. Calling this method on a class extending the abstract Model base class, while supplying a model which to treemap and optionally enabling shallow treemapping, will return a Model.Graph describing the fields which are declared and defined on the supplied model, or undefined, if the supplied model does not contain any fields or values. By treemapping shallowly, only properties defined on the supplied model are included (which means, all one-to-one and one-to-many associations are ignored). Through this method, the Model.Graph for one specific Model instance from the respective Model repository can be retrieved.

    Type Parameters

    Parameters

    • this: Model.Type<T>

      The explicit static polymorphic this parameter.

    • model: T

      The Model which is to be treemapped.

    • shallow: boolean = false

      Whether to treemap the Model shallowly.

    Returns undefined | Graph<T>

    The Model.Graph of the Model or undefined.

    Example

    treemap a Model:

    import { ExampleModel } from './example-model';

    const model = new ExampleModel({ field: 'example' });
    const graph = ExampleModel.treemap(model);
    console.log(graph); // ['field']
  • Static unravel method. Calling this method on a class extending the abstract Model base class, while supplying a graph describing the fields which to unravel, will return the Model.Graph as raw string. Through this method, the Model.Graph for one specific Model instance from the respective Model repository can be unraveled into a raw string. This unraveled Model.Graph can then be consumed by, e.g., the commit method.

    Type Parameters

    Parameters

    Returns string

    The unraveled Model.Graph as raw string.

    Example

    unravel a Model.Graph:

    import { ExampleModel } from './example-model';

    const unraveled = ExampleModel.unravel([
    'uuid',
    'modified',
    'field'
    ]);

    console.log(unraveled); // '{id modified field}'
  • Static valuate method. Calling this method on a class extending the abstract Model base class, while supplying a model and a field which to valuate, will return the preprocessed value (e.g., primitive representation of JavaScript Dates) of the supplied field of the supplied model. Through this method, the preprocessed field value of one specific Model instance from the respective Model repository can be retrieved.

    Type Parameters

    Parameters

    Returns unknown

    The valuated field value.

    Example

    valuate a field:

    import { ExampleModel } from './example-model';

    const model = new ExampleModel({ created: new Date(0) });
    const value = ExampleModel.valuate(model, 'created');
    console.log(value); // '1970-01-01T00:00:00.000+00:00'
  • Instance-scoped assign method. Calling this method, while supplying a list of parts, will assign all supplied parts to the Model instance. The assignment is implemented as deep merge assignment. Using this method, an existing Model instance can easily be mutated while still emitting the mutated changes.

    Type Parameters

    Parameters

    • this: T

      The explicit polymorphic this parameter.

    • Rest ...parts: Shape<T>[]

      An array of parts to assign to this Model.

    Returns Observable<T>

    An Observable of the mutated instance.

    Example

    assign parts to a Model instance:

    import { ExampleModel } from './example-model';

    const model = new ExampleModel();
    model.assign({ field: 'example' }).subscribe(console.log);
  • Instance-scoped clear method. Calling this method on an instance of a class extending the abstract Model base class, while optionally supplying a list of keys which are to be cleared, will set the value of the properties described by either the supplied keys or, if no keys were supplied, all enumerable properties of the class extending the abstract Model base class to undefined, effectively clearing them.

    Type Parameters

    Parameters

    • this: T

      The explicit polymorphic this parameter.

    • Optional keys: Field<T>[]

      An optional array of keys to clear.

    Returns Observable<T>

    An Observable of the mutated instance.

    Example

    clear a Model instance selectively:

    import { ExampleModel } from './example-model';

    const model = new ExampleModel({ field: 'example' });
    model.clear(['field']).subscribe(console.log);
  • Instance-scoped commit method. Internally calls the commit method on the static this-context of an instance of a class extending the abstract Model base class and furthermore assigns the returned data to the Model instance the commit method was called upon. When supplying a mapping, the returned data will be mutated through the supplied mapping (otherwise this mapping defaults to identity).

    Type Parameters

    Parameters

    Returns Observable<T>

    An Observable of the mutated instance.

    Example

    commit a query-type operation:

    import { ExampleModel } from './example-model';

    const model = new ExampleModel();

    model.commit(`query queryExample(variable: $variable) {
    result {
    field
    }
    }`, {
    variable: 'value'
    }).subscribe(console.log);
  • Instance-scoped delete method. Internally calls the static deleteOne method while supplying the UUID of this instance of a class extending the abstract Model base class. Calling this method furthermore clears the Model instance and finalizes its deletion by completing the internal changes BehaviorSubject of the Model instance the delete method was called upon.

    Type Parameters

    Parameters

    • this: T

      The explicit polymorphic this parameter.

    Returns Observable<T>

    An Observable of the mutated instance.

    Example

    delete a Model instance by UUID:

    import { ExampleModel } from './example-model';

    const model = new ExampleModel({
    id: '3068b30e-82cd-44c5-8912-db13724816fd'
    });

    model.delete().subscribe(console.log);
  • Instance-scoped find method. Internally calls the findOne method on the static this-context of an instance of a class extending the abstract Model base class and then assigns the returned data to the Model instance the find method was called upon.

    Type Parameters

    Parameters

    Returns Observable<T>

    An Observable of the mutated instance.

    Example

    find a Model instance by UUID:

    import { ExampleModel } from './example-model';

    const model = new ExampleModel({
    id: '3068b30e-82cd-44c5-8912-db13724816fd'
    });

    model.find([
    'uuid',
    'modified',
    'field'
    ]).subscribe(console.log);
  • Instance-scoped save method. Internally calls the saveOne method on the static this-context of an instance of a class extending the abstract Model base class and then assigns the returned data to the Model instance the save method was called upon.

    Type Parameters

    Parameters

    • this: T

      The explicit polymorphic this parameter.

    • graph: Graph<T> = ...

      A Model.Graph of fields to be returned.

    Returns Observable<T>

    An Observable of the mutated instance.

    Example

    save a Model instance:

    import { ExampleModel } from './example-model';

    const model = new ExampleModel({ field: 'example' });

    model.save([
    'uuid',
    'modified',
    'field'
    ]).subscribe(console.log);
  • Instance-scoped serializeer. Internally calls the serialize method on the static this-context of an instance of a class extending the abstract Model base class.

    Type Parameters

    Parameters

    • this: T

      The explicit polymorphic this parameter.

    • shallow: boolean = false

      Whether to serialize shallowly.

    Returns undefined | Shape<T>

    The Model.Shape of this instance or undefined.

    Example

    serialize a Model instance:

    import { ExampleModel } from './example-model';

    const model = new ExampleModel({ field: 'example' });
    console.log(model.serialize()); // { field: 'example' }
  • Instance-scoped treemap method. Internally calls the treemap method on the static this-context of an instance of a class extending the abstract Model base class.

    Type Parameters

    Parameters

    • this: T

      The explicit polymorphic this parameter.

    • shallow: boolean = false

      Whether to treemap shallowly.

    Returns undefined | Graph<T>

    A Model.Graph of this instance or undefined.

    Example

    treemap a Model instance:

    import { ExampleModel } from './example-model';

    const model = new ExampleModel({ field: 'example' });
    console.log(model.treemap()); // ['field']

Generated using TypeDoc