@travetto/model

5.0.13 • Public • Published

Data Modeling Support

Datastore abstraction for core operations.

Install: @travetto/model

npm install @travetto/model

# or

yarn add @travetto/model

This module provides a set of contracts/interfaces to data model persistence, modification and retrieval. This module builds heavily upon the Schema, which is used for data model validation.

Contracts

The module is mainly composed of contracts. The contracts define the expected interface for various model patterns. The primary contracts are Basic, CRUD, Indexed, Expiry, Blob and Bulk.

Basic

All Data Modeling Support implementations, must honor the Basic contract to be able to participate in the model ecosystem. This contract represents the bare minimum for a model service.

Code: Basic Contract

export interface ModelBasicSupport<C = unknown> {
  /**
   * Get underlying client
   */
  get client(): C;

  /**
   * Get by Id
   * @param id The identifier of the document to retrieve
   * @throws {NotFoundError} When an item is not found
   */
  get<T extends ModelType>(cls: Class<T>, id: string): Promise<T>;

  /**
   * Create new item
   * @param item The document to create
   * @throws {ExistsError} When an item with the provided id already exists
   */
  create<T extends ModelType>(cls: Class<T>, item: OptionalId<T>): Promise<T>;

  /**
   * Delete an item
   * @param id The id of the document to delete
   * @throws {NotFoundError} When an item is not found
   */
  delete<T extends ModelType>(cls: Class<T>, id: string): Promise<void>;
}

CRUD

The CRUD contract, builds upon the basic contract, and is built around the idea of simple data retrieval and storage, to create a foundation for other services that need only basic support. The model extension in Authentication, is an example of a module that only needs create, read and delete, and so any implementation of Data Modeling Support that honors this contract, can be used with the Authentication model extension.

Code: Crud Contract

export interface ModelCrudSupport extends ModelBasicSupport {

  /**
   * Id Source
   */
  idSource: ModelIdSource;

  /**
   * Update an item
   * @param item The document to update.
   * @throws {NotFoundError} When an item is not found
   */
  update<T extends ModelType>(cls: Class<T>, item: T): Promise<T>;

  /**
   * Create or update an item
   * @param item The document to upsert
   * @param view The schema view to validate against
   */
  upsert<T extends ModelType>(cls: Class<T>, item: OptionalId<T>): Promise<T>;

  /**
   * Update partial, respecting only top level keys.
   *
   * When invoking this method, any top level keys that are null/undefined are treated as removals/deletes.  Any properties
   * that point to sub objects/arrays are treated as wholesale replacements.
   *
   * @param id The document identifier to update
   * @param item The document to partially update.
   * @param view The schema view to validate against
   * @throws {NotFoundError} When an item is not found
   */
  updatePartial<T extends ModelType>(cls: Class<T>, item: Partial<T> & { id: string }, view?: string): Promise<T>;

  /**
   * List all items
   */
  list<T extends ModelType>(cls: Class<T>): AsyncIterable<T>;
}

Indexed

Additionally, an implementation may support the ability for basic Indexed queries. This is not the full featured query support of Data Model Querying, but allowing for indexed lookups. This does not support listing by index, but may be added at a later date.

Code: Indexed Contract

export interface ModelIndexedSupport extends ModelBasicSupport {
  /**
   * Get entity by index as defined by fields of idx and the body fields
   * @param cls The type to search by
   * @param idx The index name to search against
   * @param body The payload of fields needed to search
   */
  getByIndex<T extends ModelType>(cls: Class<T>, idx: string, body: DeepPartial<T>): Promise<T>;

  /**
   * Delete entity by index as defined by fields of idx and the body fields
   * @param cls The type to search by
   * @param idx The index name to search against
   * @param body The payload of fields needed to search
   */
  deleteByIndex<T extends ModelType>(cls: Class<T>, idx: string, body: DeepPartial<T>): Promise<void>;

  /**
   * List entity by ranged index as defined by fields of idx and the body fields
   * @param cls The type to search by
   * @param idx The index name to search against
   * @param body The payload of fields needed to search
   */
  listByIndex<T extends ModelType>(cls: Class<T>, idx: string, body?: DeepPartial<T>): AsyncIterable<T>;

  /**
   * Upsert by index, allowing the index to act as a primary key
   * @param cls The type to create for
   * @param idx The index name to use
   * @param body The document to potentially store
   */
  upsertByIndex<T extends ModelType>(cls: Class<T>, idx: string, body: OptionalId<T>): Promise<T>;
}

Expiry

Certain implementations will also provide support for automatic Expiry of data at runtime. This is extremely useful for temporary data as, and is used in the Caching module for expiring data accordingly.

Code: Expiry Contract

export interface ModelExpirySupport extends ModelCrudSupport {
  /**
   * Delete all expired by class
   *
   * @returns Returns the number of documents expired
   */
  deleteExpired<T extends ModelType>(cls: Class<T>): Promise<number>;
}

Blob

Some implementations also allow for the ability to read/write binary data as Blob. Given that all implementations can store Base64 encoded data, the key differentiator here, is native support for streaming data, as well as being able to store binary data of significant sizes.

Code: Blob Contract

export interface ModelBlobSupport {

  /**
   * Upsert blob to storage
   * @param location The location of the blob
   * @param input The actual blob to write
   * @param meta Additional metadata to store with the blob
   * @param overwrite Should we replace content if already found, defaults to true
   */
  upsertBlob(location: string, input: BinaryInput, meta?: BlobMeta, overwrite?: boolean): Promise<void>;

  /**
   * Get blob from storage
   * @param location The location of the blob
   */
  getBlob(location: string, range?: ByteRange): Promise<Blob>;

  /**
   * Get metadata for blob
   * @param location The location of the blob
   */
  getBlobMeta(location: string): Promise<BlobMeta>;

  /**
   * Delete blob by location
   * @param location The location of the blob
   */
  deleteBlob(location: string): Promise<void>;

  /**
   * Update blob metadata
   * @param location The location of the blob
   */
  updateBlobMeta(location: string, meta: BlobMeta): Promise<void>;

  /**
   * Produces an externally usable URL for sharing limited read access to a specific resource
   *
   * @param location The asset location to read from
   * @param exp Expiry
   */
  getBlobReadUrl?(location: string, exp?: TimeSpan): Promise<string>;

  /**
   * Produces an externally usable URL for sharing allowing direct write access
   *
   * @param location The asset location to write to
   * @param meta The metadata to associate with the final asset
   * @param exp Expiry
   */
  getBlobWriteUrl?(location: string, meta: BlobMeta, exp?: TimeSpan): Promise<string>;
}

Bulk

Finally, there is support for Bulk operations. This is not to simply imply issuing many commands at in parallel, but implementation support for an atomic/bulk operation. This should allow for higher throughput on data ingest, and potentially for atomic support on transactions.

Code: Bulk Contract

export interface ModelBulkSupport extends ModelCrudSupport {
  processBulk<T extends ModelType>(cls: Class<T>, operations: BulkOp<T>[]): Promise<BulkResponse>;
}

Declaration

Models are declared via the @Model decorator, which allows the system to know that this is a class that is compatible with the module. The only requirement for a model is the ModelType

Code: ModelType

export interface ModelType {
  /**
   * Unique identifier.
   *
   * If not provided, will be computed on create
   */
  id: string;
}

The id is the only required field for a model, as this is a hard requirement on naming and type. This may make using existing data models impossible if types other than strings are required. Additionally, the type field, is intended to record the base model type, but can be remapped. This is important to support polymorphism, not only in Data Modeling Support, but also in Schema.

Implementations

Service Basic CRUD Indexed Expiry Blob Bulk
DynamoDB Model Support X X X X
Elasticsearch Model Source X X X X X
Firestore Model Support X X X
MongoDB Model Support X X X X X X
Redis Model Support X X X X
S3 Model Support X X X X
SQL Model Service X X X X X
Memory Model Support X X X X X X
File Model Support X X X X X

Custom Model Service

In addition to the provided contracts, the module also provides common utilities and shared test suites. The common utilities are useful for repetitive functionality, that is unable to be shared due to not relying upon inheritance (this was an intentional design decision). This allows for all the Data Modeling Support implementations to completely own the functionality and also to be able to provide additional/unique functionality that goes beyond the interface. Memory Model Support serves as a great example of what a full featured implementation can look like.

To enforce that these contracts are honored, the module provides shared test suites to allow for custom implementations to ensure they are adhering to the contract's expected behavior.

Code: Memory Service Test Configuration

import { DependencyRegistry } from '@travetto/di';
import { AppError, castTo, Class, classConstruct } from '@travetto/runtime';

import { isBulkSupported, isCrudSupported } from '../../src/internal/service/common';
import { ModelType } from '../../src/types/model';
import { ModelSuite } from './suite';

type ServiceClass = { serviceClass: { new(): unknown } };

@ModelSuite()
export abstract class BaseModelSuite<T> {

  static ifNot(pred: (svc: unknown) => boolean): (x: unknown) => Promise<boolean> {
    return async (x: unknown) => !pred(classConstruct(castTo<ServiceClass>(x).serviceClass));
  }

  serviceClass: Class<T>;
  configClass: Class;

  async getSize<U extends ModelType>(cls: Class<U>): Promise<number> {
    const svc = (await this.service);
    if (isCrudSupported(svc)) {
      let i = 0;
      for await (const __el of svc.list(cls)) {
        i += 1;
      }
      return i;
    } else {
      throw new AppError(`Size is not supported for this service: ${this.serviceClass.name}`);
    }
  }

  async saveAll<M extends ModelType>(cls: Class<M>, items: M[]): Promise<number> {
    const svc = await this.service;
    if (isBulkSupported(svc)) {
      const res = await svc.processBulk(cls, items.map(x => ({ insert: x })));
      return res.counts.insert;
    } else if (isCrudSupported(svc)) {
      const out: Promise<M>[] = [];
      for (const el of items) {
        out.push(svc.create(cls, el));
      }
      await Promise.all(out);
      return out.length;
    } else {
      throw new Error('Service does not support crud operations');
    }
  }

  get service(): Promise<T> {
    return DependencyRegistry.getInstance(this.serviceClass);
  }

  async toArray<U>(src: AsyncIterable<U> | AsyncGenerator<U>): Promise<U[]> {
    const out: U[] = [];
    for await (const el of src) {
      out.push(el);
    }
    return out;
  }
}

CLI - model:export

The module provides the ability to generate an export of the model structure from all the various @Models within the application. This is useful for being able to generate the appropriate files to manually create the data schemas in production.

Terminal: Running model export

$ trv model:export --help

Usage: model:export [options] <provider:string> <models...:string>

Options:
  -e, --env <string>     Application environment
  -m, --module <module>  Module to run for
  -h, --help             display help for command

Providers
--------------------
  * SQL

Models
--------------------
  * samplemodel

CLI - model:install

The module provides the ability to install all the various @Models within the application given the current configuration being targeted. This is useful for being able to prepare the datastore manually.

Terminal: Running model install

$ trv model:install --help

Usage: model:install [options] <provider:string> <models...:string>

Options:
  -e, --env <string>     Application environment
  -m, --module <module>  Module to run for
  -h, --help             display help for command

Providers
--------------------
  * SQL

Models
--------------------
  * samplemodel

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
5.0.0-rc.150rc
2.0.0-alpha.120alpha
1.1.0-rc.00next
1.0.0-beta.80beta
5.0.13207latest

Version History

VersionDownloads (Last 7 Days)Published
5.0.13207
5.0.12234
5.0.117
5.0.101
5.0.910
5.0.81
5.0.71
5.0.61
5.0.53
5.0.41
5.0.31
5.0.21
5.0.11
5.0.00
5.0.0-rc.150
5.0.0-rc.140
5.0.0-rc.130
5.0.0-rc.120
5.0.0-rc.110
5.0.0-rc.100
5.0.0-rc.90
5.0.0-rc.80
5.0.0-rc.70
5.0.0-rc.60
5.0.0-rc.50
5.0.0-rc.40
5.0.0-rc.30
5.0.0-rc.20
5.0.0-rc.10
5.0.0-rc.00
4.1.30
4.1.20
4.1.00
4.0.70
4.0.60
4.0.50
4.0.40
4.0.30
4.0.20
4.0.10
4.0.00
4.0.0-rc.80
4.0.0-rc.70
3.4.60
4.0.0-rc.60
4.0.0-rc.50
4.0.0-rc.40
4.0.0-rc.30
4.0.0-rc.20
4.0.0-rc.10
4.0.0-rc.00
3.4.50
3.4.40
3.4.30
3.4.20
3.4.10
3.4.00
3.4.0-rc.80
3.4.0-rc.70
3.4.0-rc.60
3.4.0-rc.50
3.4.0-rc.40
3.4.0-rc.30
3.4.0-rc.20
3.4.0-rc.10
3.4.0-rc.00
3.3.80
3.3.70
3.3.60
3.3.50
3.3.40
3.3.30
3.3.20
3.3.10
3.3.00
3.2.30
3.2.20
3.2.10
3.2.00
3.2.0-rc.00
3.1.150
3.1.141
3.1.130
3.1.120
3.1.110
3.1.100
3.1.90
3.1.80
3.1.70
3.1.60
3.1.50
3.1.41
3.1.30
3.1.20
3.1.10
3.1.00
3.1.0-rc.100
3.1.0-rc.90
3.1.0-rc.80
3.1.0-rc.70
3.1.0-rc.60
3.1.0-rc.50
3.1.0-rc.40
3.1.0-rc.31
3.1.0-rc.20
3.1.0-rc.10
3.1.0-rc.00
3.0.30
3.0.20
3.0.2-rc.10
3.0.2-rc.00
3.0.10
3.0.1-rc.11
3.0.00
3.0.0-rc.260
3.0.0-rc.250
3.0.0-rc.240
3.0.0-rc.230
3.0.0-rc.220
3.0.0-rc.210
3.0.0-rc.200
3.0.0-rc.190
3.0.0-rc.180
3.0.0-rc.170
3.0.0-rc.160
3.0.0-rc.150
3.0.0-rc.140
3.0.0-rc.130
3.0.0-rc.120
3.0.0-rc.110
3.0.0-rc.100
3.0.0-rc.90
3.0.0-rc.80
3.0.0-rc.70
3.0.0-rc.60
3.0.0-rc.40
3.0.0-rc.30
3.0.0-rc.20
3.0.0-rc.10
3.0.0-rc.00
2.2.40
2.2.30
2.2.20
2.2.10
2.2.00
2.1.50
2.1.40
2.1.30
2.1.20
2.1.10
2.1.00
2.0.30
2.0.20
2.0.10
2.0.00
2.0.0-rc.80
2.0.0-rc.70
2.0.0-rc.60
2.0.0-rc.50
2.0.0-rc.40
2.0.0-rc.30
2.0.0-rc.20
2.0.0-rc.10
2.0.0-rc.00
2.0.0-alpha.120
2.0.0-alpha.110
2.0.0-alpha.100
2.0.0-alpha.90
2.0.0-alpha.80
2.0.0-alpha.70
2.0.0-alpha.60
2.0.0-alpha.50
2.0.0-alpha.40
2.0.0-alpha.30
2.0.0-alpha.20
2.0.0-alpha.10
1.1.10
1.1.00
1.1.0-rc.00
1.1.0-alpha.60
1.1.0-alpha.50
1.1.0-alpha.40
1.1.0-alpha.30
1.1.0-alpha.20
1.1.0-alpha.10
1.1.0-alpha.00
1.0.70
1.0.60
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10
1.0.00
1.0.0-rc.90
1.0.0-rc.80
1.0.0-rc.70
1.0.0-rc.60
1.0.0-rc.50
1.0.0-rc.40
1.0.0-rc.30
1.0.0-rc.20
1.0.0-rc.10
1.0.0-rc.00
1.0.0-beta.80
1.0.0-beta.70
1.0.0-beta.60
1.0.0-beta.50
1.0.0-beta.40
1.0.0-beta.30
1.0.0-beta.20
1.0.0-beta.10
0.7.70
0.7.60
0.7.50
0.7.40
0.7.30
0.7.20
0.7.10
0.7.1-alpha.90
0.7.1-alpha.80
0.7.1-alpha.70
0.7.1-alpha.60
0.7.1-alpha.50
0.7.1-alpha.40
0.7.1-alpha.30
0.7.1-alpha.20
0.7.1-alpha.10
0.7.1-alpha.00
0.7.0-alpha.00
0.6.80
0.6.70
0.6.60
0.6.50
0.6.40
0.6.30
0.6.10
0.6.00
0.6.0-rc.110
0.6.0-rc.100
0.6.0-rc.90
0.6.0-rc.80
0.6.0-rc.70
0.6.0-rc.60
0.6.0-rc.40
0.6.0-rc.30
0.6.0-rc.20
0.6.0-rc.10
0.6.0-rc.00
0.5.150
0.5.140
0.5.130
0.5.120
0.5.110
0.5.100
0.5.90
0.5.80
0.5.70
0.5.60
0.5.50
0.5.40
0.5.30
0.5.20
0.5.10
0.5.00
0.4.150
0.4.140
0.4.130
0.4.120
0.4.110
0.4.100
0.4.90
0.4.80
0.4.70
0.4.60
0.4.50
0.4.40
0.4.30
0.4.20
0.4.10
0.4.00
0.3.280
0.3.270
0.3.260
0.3.250
0.3.240
0.3.230
0.3.220
0.3.210
0.3.200
0.3.190
0.3.180
0.3.170
0.3.160
0.3.150
0.3.140
0.3.130
0.3.120
0.3.110
0.3.100
0.3.90
0.3.80
0.3.70
0.3.60
0.3.50
0.3.40
0.3.20
0.3.10
0.2.150
0.2.140
0.2.130
0.2.120
0.2.110
0.2.100
0.2.90
0.2.80
0.2.70
0.2.50
0.2.40
0.2.20
0.2.00
0.1.80
0.1.70
0.1.60
0.1.50
0.1.40
0.1.30
0.1.20
0.1.10
0.0.410
0.0.400
0.0.390
0.0.380
0.0.370
0.0.360
0.0.350
0.0.340
0.0.330
0.0.320
0.0.310
0.0.300
0.0.290
0.0.280
0.0.270
0.0.260
0.0.250
0.0.240
0.0.230
0.0.220
0.0.210
0.0.200
0.0.190
0.0.180
0.0.170
0.0.160
0.0.150
0.0.140
0.0.130
0.0.120
0.0.110
0.0.100
0.0.90
0.0.80
0.0.70
0.0.60
0.0.50
0.0.40
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i @travetto/model

Homepage

travetto.io

Weekly Downloads

473

Version

5.0.13

License

MIT

Unpacked Size

160 kB

Total Files

53

Last publish

Collaborators

  • arcsine