Abstract
The extending Model InstanceType.
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.
Optional
Readonly
[hasOptional
Readonly
[hasOptional
Readonly
[property]Optional
createdOptional
modifiedOptional
uuidProtected
Readonly
Abstract
[toEnforced 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).
Provide a valid symbol property:
import { Model } from '@sgrud/data';
export class ExampleModel extends Model<ExampleModel> {
protected [Symbol.toStringTag]: string = 'ExampleModel';
}
Protected
Readonly
changesBehaviorSubject emitting every time this Model instance experiences changes.
Protected
Readonly
staticType-asserted alias for the static Model context.
Protected
entityProtected
pluralProtected
typeStatic
commitStatic 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.
The extending Model InstanceType.
The explicit static polymorphic this
parameter.
The Querier.Operation to be committed.
Optional
variables: VariablesAny Querier.Variables within the operation
.
An Observable of the commitmed operation
.
An Observable ReferenceError on incompatibility.
commit a query
-type operation:
import { ExampleModel } from './example-model';
ExampleModel.commit(`query queryExample(variable: $variable) {
result {
field
}
}`, {
variable: 'value'
}).subscribe(console.log);
Static
deleteStatic 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.
The extending Model InstanceType.
An Observable of the deletion.
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
deleteStatic 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.
The extending Model InstanceType.
An Observable of the deletion.
Drop one model instance by UUID:
import { ExampleModel } from './example-model';
ExampleModel.deleteOne(
'18f3aa99-afa5-40f4-90c2-71a2ecc25651'
).subscribe(console.log);
Static
findStatic 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.
The extending Model InstanceType.
The explicit static polymorphic this
parameter.
A Model.Filter to find Model instances by.
A Model.Graph of fields to be returned.
An Observable of the find operation.
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
findStatic 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.
The extending Model InstanceType.
The explicit static polymorphic this
parameter.
The Model.Shape of instance to find.
A Model.Graph of fields to be returned.
An Observable of the find operation.
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
saveStatic 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.
The extending Model InstanceType.
An Observable of the save operation.
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
saveStatic 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.
The extending Model InstanceType.
An Observable of the save operation.
Persist a model:
import { ExampleModel } from './example-model';
ExampleModel.saveOne(new ExampleModel({ field: 'example' }), [
'uuid',
'modified',
'field'
]).subscribe(console.log);
Static
serializeStatic 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 shallow
ly, 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.
The extending Model InstanceType.
The Model.Shape of the Model or undefined
.
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
treemapStatic 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 shallow
ly, 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.
The extending Model InstanceType.
The Model.Graph of the Model or undefined
.
treemap a Model:
import { ExampleModel } from './example-model';
const model = new ExampleModel({ field: 'example' });
const graph = ExampleModel.treemap(model);
console.log(graph); // ['field']
Static
unravelStatic 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.
The extending Model InstanceType.
The unraveled Model.Graph as raw string.
unravel a Model.Graph:
import { ExampleModel } from './example-model';
const unraveled = ExampleModel.unravel([
'uuid',
'modified',
'field'
]);
console.log(unraveled); // '{id modified field}'
Static
valuateStatic 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.
The extending Model InstanceType.
The valuated field
value.
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'
Well-known Symbol.observable
method returning a Subscribable. The
returned Subscribable emits all changes this Model
instance experiences.
A Subscribable emitting all Model changes.
Subscribe to a Model instance:
import { from } from 'rxjs';
import { ExampleModel } from './example-model';
const model = new ExampleModel();
from(model).subscribe(console.log);
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.
The extending Model InstanceType.
An Observable of the mutated instance.
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.
The extending Model InstanceType.
An Observable of the mutated instance.
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).
The extending Model InstanceType.
The explicit polymorphic this
parameter.
The Querier.Operation to be committed.
Optional
variables: VariablesAny Querier.Variables within the operation
.
An optional mutation to apply to the returned data.
An Observable of the mutated instance.
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.
The extending Model InstanceType.
The explicit polymorphic this
parameter.
An Observable of the mutated instance.
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.
The extending Model InstanceType.
The explicit polymorphic this
parameter.
A Model.Graph of fields to be returned.
The Model.Shape of the Model to find.
An Observable of the mutated instance.
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.
The extending Model InstanceType.
The explicit polymorphic this
parameter.
A Model.Graph of fields to be returned.
An Observable of the mutated instance.
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.
The extending Model InstanceType.
The explicit polymorphic this
parameter.
Whether to serialize shallow
ly.
The Model.Shape of this instance or undefined
.
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.
The extending Model InstanceType.
The explicit polymorphic this
parameter.
Whether to treemap shallow
ly.
A Model.Graph of this instance or undefined.
treemap a Model instance:
import { ExampleModel } from './example-model';
const model = new ExampleModel({ field: 'example' });
console.log(model.treemap()); // ['field']
Generated using TypeDoc
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 polymorphicthis
, all inherited operations warrant type safety and provide intellisense.Example
Extend the Model base class:
See
Querier