Skip to main content

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

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

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

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

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

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

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

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

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

Returns

{ModelClass}

makeModelFactory

Create a model class factory.

Example

import { makeModelFactory } from 'foscia/core';

const makeModel = makeModelFactory({ /* definition */ }, { /* config */ });

Arguments

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

Returns

{ModelParsedDefinition}