Skip to main content

Class: ModelUtils

Defined in: service-core/src/models/ModelUtils.ts:28

Utility class for working with data model classes.

Constructors

Constructor

new ModelUtils(): ModelUtils

Returns

ModelUtils

Accessors

orm

Get Signature

get static orm(): any

Defined in: service-core/src/models/ModelUtils.ts:47

Returns the typeorm module, throwing an error if it has not been provided.

Returns

any

Methods

buildIdSearchQuery()

static buildIdSearchQuery<T>(repo, modelClass, id, version?): any

Defined in: service-core/src/models/ModelUtils.ts:134

Builds a query object for use with find functions of the given repository for retrieving objects matching the specified unique identifier.

Type Parameters

T

T extends object

Parameters

repo

Repository<T> | MongoRepository<T> | undefined

The repository to build the query for.

modelClass

any

The class definition of the data model to build a search query for.

id

any

The unique identifier to search for.

version?

number

The version number of the document to search for.

Returns

any

An object that can be passed to a TypeORM find function.


buildIdSearchQueryMongo()

static buildIdSearchQueryMongo(modelClass, id, version?): any

Defined in: service-core/src/models/ModelUtils.ts:182

Builds a MongoDB compatible query object for use in find functions for retrieving objects matching the specified unique identifier.

Parameters

modelClass

any

The class definition of the data model to build a search query for.

id

any

The unique identifier to search for.

version?

number

The version number of the document to search for.

Returns

any

An object that can be passed to a MongoDB find function.


buildIdSearchQuerySQL()

static buildIdSearchQuerySQL(modelClass, id, version?): any

Defined in: service-core/src/models/ModelUtils.ts:156

Builds a TypeORM compatible query object for use in find functions for retrieving objects matching the specified unique identifier.

Parameters

modelClass

any

The class definition of the data model to build a search query for.

id

any

The unique identifier to search for.

version?

number

The version number of the document to search for.

Returns

any

An object that can be passed to a TypeORM find function.


buildSearchQuery()

static buildSearchQuery<T>(modelClass, repo, params?, queryParams?, exactMatch?, user?): any

Defined in: service-core/src/models/ModelUtils.ts:472

Builds a query object for the given criteria and repository. Query params can have a value containing a conditional operator to apply for the search. The operator is encoded with the format op(value). The following operators are supported:

  • eq - Returns matches whose parameter exactly matches of the given value. e.g. param = value
  • gt - Returns matches whose parameter is greater than the given value. e.g. param > value
  • gte - Returns matches whose parameter is greater than or equal to the given value. e.g. param >= value
  • in - Returns matches whose parameter includes one of the given values. e.g. param in ('value1', 'value2', 'value3', ...)
  • like - Returns matches whose parameter is lexographically similar to the given value. param like value
  • lt - Returns matches whose parameter is less than the given value. e.g. param < value
  • lte - Returns matches whose parameter is less than or equal to than the given value. e.g. param < value
  • not - Returns matches whose parameter is not equal to the given value. e.g. param not value
  • range - Returns matches whose parameter is greater than or equal to first given value and less than or equal to the second. e.g. param between(1,100)

When no operator is provided the comparison will always be evaluated as eq.

NOTE: The result of this function is only compatible with the aggregate() function when MongoDB is used.

Type Parameters

T

T extends object

Parameters

modelClass

any

The class definition of the data model to build a search query for.

repo

Repository<T> | MongoRepository<T> | undefined

The repository to build a search query for.

params?

any

The URI parameters for the endpoint that was requested.

queryParams?

any

The URI query parameters that were included in the request.

exactMatch?

boolean = false

Set to true to create a query where parameters are to be matched exactly, otherwise set to false to use a 'contains' search.

user?

any

The user that is performing the request.

Returns

any

The TypeORM compatible query object.


buildSearchQueryMongo()

static buildSearchQueryMongo(modelClass, params?, queryParams?, exactMatch?, user?): any

Defined in: service-core/src/models/ModelUtils.ts:662

Builds a MongoDB compatible query object for the given criteria. Query params can have a value containing a conditional operator to apply for the search. The operator is encoded with the format op(value). The following operators are supported:

  • eq - Returns matches whose parameter exactly matches of the given value. e.g. param = value
  • gt - Returns matches whose parameter is greater than the given value. e.g. param > value
  • gte - Returns matches whose parameter is greater than or equal to the given value. e.g. param >= value
  • in - Returns matches whose parameter includes one of the given values. e.g. param in ('value1', 'value2', 'value3', ...)
  • like - Returns matches whose parameter is lexographically similar to the given value. param like value
  • lt - Returns matches whose parameter is less than the given value. e.g. param < value
  • lte - Returns matches whose parameter is less than or equal to than the given value. e.g. param < value
  • not - Returns matches whose parameter is not equal to the given value. e.g. param not value
  • range - Returns matches whose parameter is greater than or equal to first given value and less than or equal to the second. e.g. param between(1,100)

When no operator is provided the comparison will always be evaluated as eq.

NOTE: The result of this function is only compatible with the aggregate() function.

Parameters

modelClass

any

The class definition of the data model to build a search query for.

params?

any

The URI parameters for the endpoint that was requested.

queryParams?

any

The URI query parameters that were included in the request.

exactMatch?

boolean = false

Set to true to create a query where parameters are to be matched exactly, otherwise set to false to use a 'contains' search.

user?

any

The user that is performing the request.

Returns

any

The TypeORM compatible query object.


buildSearchQuerySQL()

static buildSearchQuerySQL(modelClass, params?, queryParams?, exactMatch?, user?): any

Defined in: service-core/src/models/ModelUtils.ts:518

Builds a TypeORM compatible query object for the given criteria. Query params can have a value containing a conditional operator to apply for the search. The operator is encoded with the format op(value). The following operators are supported:

  • eq - Returns matches whose parameter exactly matches of the given value. e.g. param = value
  • gt - Returns matches whose parameter is greater than the given value. e.g. param > value
  • gte - Returns matches whose parameter is greater than or equal to the given value. e.g. param >= value
  • in - Returns matches whose parameter includes one of the given values. e.g. param in ('value1', 'value2', 'value3', ...)
  • like - Returns matches whose parameter is lexographically similar to the given value. param like value
  • lt - Returns matches whose parameter is less than the given value. e.g. param < value
  • lte - Returns matches whose parameter is less than or equal to than the given value. e.g. param < value
  • not - Returns matches whose parameter is not equal to the given value. e.g. param not value
  • range - Returns matches whose parameter is greater than or equal to first given value and less than or equal to the second. e.g. param between(1,100)

When no operator is provided the comparison will always be evaluated as eq.

Parameters

modelClass

any

The class definition of the data model to build a search query for.

params?

any

The URI parameters for the endpoint that was requested.

queryParams?

any

The URI query parameters that were included in the request.

exactMatch?

boolean = false

Set to true to create a query where parameters are to be matched exactly, otherwise set to false to use a 'contains' search.

user?

any

The user that is performing the request.

Returns

any

The TypeORM compatible query object.


getIdPropertyNames()

static getIdPropertyNames(modelClass): string[]

Defined in: service-core/src/models/ModelUtils.ts:62

Retrieves a list of all of the specified class's properties that have the

Parameters

modelClass

any

The class definition to search for identifiers from.

Returns

string[]

The list of all property names that have the

Identifier

decorator applied.

Identifier

decorator applied.


getReadOnlyPropertyNames()

static getReadOnlyPropertyNames(modelClass): string[]

Defined in: service-core/src/models/ModelUtils.ts:96

Retrieves a list of all of the specified class's properties that have the

Parameters

modelClass

any

The class definition to search for read-only properties from.

Returns

string[]

The list of all property names that have the

Read Only

decorator applied.

Read Only

decorator applied.


loadModels()

static loadModels(src, result?): Promise<Map<string, any>>

Defined in: service-core/src/models/ModelUtils.ts:822

Loads all model schema files from the specified path and returns a map containing all the definitions.

Parameters

src

string

The path to the model files to load.

result?

Map<string, any> = ...

Returns

Promise<Map<string, any>>

A map containing of all loaded model names to their class definitions.


setTypeOrm()

static setTypeOrm(module): void

Defined in: service-core/src/models/ModelUtils.ts:40

Provides the typeorm module to use when building SQL queries. This is called automatically when a SQL datastore connection is established.

Parameters

module

any

The typeorm module.

Returns

void