Models utilities
Common
fill
Fill the model instance's values with the given values.
Example
import { fill } from 'foscia/core';
const post = fill(new Post(), { title: 'Hello', description: 'World' });
Arguments
{I extends ModelInstance}
instance
{Partial<ModelValues<I>>}
values
Returns
{I}
, the affected instance.
isSame
Check if two values are the same model instance. Ensure the equality by checking if the two instances:
- Are Foscia model instances
- Have the same type
- Have the same and non-NIL ID
This function does not deeply compare instances' values.
Example
import { isSame } from 'foscia/core';
const areSameInstances = isSame(foo, bar);
if (areSameInstances) { /* do something */ }
Arguments
{unknown}
value
{unknown}
otherValue
Returns
{boolean}
loaded
Check if the given relations are loaded on the instance or its related instances. Can check for sub relations using dot relation keys: will check each related instances regardless of the number of concerned instances. If no relations are provided, wont perform any check.
Example
import { loaded } from 'foscia/core';
const isFullyLoaded = loaded(post, ['comments', 'comments.author']);
if (!isFullyLoaded) { /* do something */ }
Arguments
{I extends ModelInstance}
instance
{ArrayableVariadic<ModelRelationDotKey<I>>}
...relations
Returns
{boolean}
changed
Check if the given keys have been changed since last instance sync. If no keys are provided, will check whole original snapshot (including IDs).
Example
import { changed } from 'foscia/core';
const wasChanged = changed(post, ['title', 'description']);
if (wasChanged) { /* do something */ }
Arguments
{I extends ModelInstance}
instance
{ArrayableVariadic<ModelKey<I>}
...only
Returns
{boolean}
if keys (or instance) changed since last sync.
markSynced
Mark the model instance's values as synced. If no keys are provided, will replace whole original snapshot (including IDs).
Example
import { markSynced } from 'foscia/core';
markSynced(post, ['title', 'description']);
Arguments
{I extends ModelInstance}
instance
{ArrayableVariadic<ModelKey<I>>}
...only
Returns
{I}
, the affected instance.
restore
Restore the model instance's original values. If no keys are provided, will restore whole original snapshot (including IDs).
Example
import { restore } from 'foscia/core';
restore(post, ['title', 'description']);
Arguments
{I extends ModelInstance}
instance
{ArrayableVariadic<ModelKey<I>>}
...only
Returns
{I}
, the affected instance.
takeSnapshot
Take a snapshot of the model's state (IDs, values, etc.).
Example
import { takeSnapshot } from 'foscia/core';
const postSnapshot = takeSnapshot(post);
Arguments
{I extends ModelInstance}
instance
Returns
{ModelSnapshot<I>}
, the snapshot.
restoreSnapshot
Restore a snapshot of the model's state (IDs, values, etc.). If no keys are provided, will restore whole original snapshot (including IDs).
Example
import { restoreSnapshot } from 'foscia/core';
restoreSnapshot(post, postSnapshot, ['title', 'description']);
Arguments
{I extends ModelInstance}
instance
{ModelSnapshot<I>}
snapshot
{ArrayableVariadic<ModelKey<I>>}
...only
Returns
{I}
, the affected instance.
compareSnapshots
Check if the given keys are different between two snapshots. If no keys are provided, will check whole snapshots (including IDs).
Example
import { compareSnapshots } from 'foscia/core';
compareSnapshots(nextSnapshot, prevSnapshot, ['title', 'description']);
Arguments
{ModelSnapshot<I>}
nextSnapshot
{ModelSnapshot<I>}
prevSnapshot
{ArrayableVariadic<ModelKey<I>>}
...only
Returns
{boolean}
if keys (or all values) are different on given snapshots.
Factories
makeModel
Create a model class.
Example
import { makeModel } from 'foscia/core';
const PostModel = makeModel('posts', { /* definition */ });
Arguments
{ModelConfig | string}
config
{ModelDefinition}
rawDefinition
Returns
makeModelFactory
Create a model class factory.
Example
import { makeModelFactory } from 'foscia/core';
const makeModel = makeModelFactory({ /* definition */ }, { /* config */ });
Arguments
{ModelDefinition | undefined}
baseRawDefinition
{ModelConfig | undefined}
baseConfig
(omittingtype
)
Returns
{makeModel}
, a customized model factory function.
makeComposable
Create a composable definition to integrate in models.
Example
import { makeComposable } from 'foscia/core';
const publishable = makeComposable({ /* definition */ });
Arguments
{ModelDefinition}
rawDefinition