Skip to main content
Version: 6.2

Utils

Index

Constructors

constructor

Properties

staticreadonlyPK_SEPARATOR

PK_SEPARATOR: ~~~ = '~~~'

Methods

staticabsolutePath

  • absolutePath(path: string, baseDir?: string): string
  • Computes the absolute path to for the given path relative to the provided base directory. If either path or baseDir are file: URLs, they are converted to local paths.


    Parameters

    • path: string
    • baseDir: string = ...

    Returns string

staticasArray

  • asArray<T>(data?: T | readonly T[] | Iterable<T>, strict?: boolean): T[]
  • Normalize the argument to always be an array.


    Type parameters

    • T

    Parameters

    • optionaldata: T | readonly T[] | Iterable<T>
    • strict: boolean = false

    Returns T[]

staticcallCompiledFunction

  • callCompiledFunction<T, R>(fn: (...args: T) => R, ...args: T): R
  • Type parameters

    • T: unknown[]
    • R

    Parameters

    • fn: (...args: T) => R
    • rest...args: T

    Returns R

staticclassName

  • Gets string name of given class.


    Type parameters

    • T

    Parameters

    Returns string

staticcopy

  • copy<T>(entity: T, respectCustomCloneMethod?: boolean): T
  • Creates deep copy of given object.


    Type parameters

    • T

    Parameters

    • entity: T
    • respectCustomCloneMethod: boolean = true

    Returns T

staticcreateFunction

  • createFunction(context: Map<string, any>, code: string): any
  • Parameters

    • context: Map<string, any>
    • code: string

    Returns any

staticdefaultValue

  • defaultValue<T>(prop: T, option: keyof T, defaultValue: any): void
  • Type parameters

    Parameters

    • prop: T
    • option: keyof T
    • defaultValue: any

    Returns void

staticdetectTsNode

  • detectTsNode(): boolean
  • Tries to detect ts-node runtime.


    Returns boolean

staticdiff

  • Computes difference between two objects, ignoring items missing in b.


    Parameters

    Returns Record<string | number, any>

staticdropUndefinedProperties

  • dropUndefinedProperties<T>(o: any, value?: null, visited?: Set<unknown>): void
  • Removes undefined properties (recursively) so they are not saved as nulls


    Type parameters

    Parameters

    • o: any
    • optionalvalue: null
    • visited: Set<unknown> = ...

    Returns void

staticdynamicImport

  • dynamicImport<T>(id: string): Promise<T>

staticdynamicImportProvider

  • dynamicImportProvider(id: string): Promise<any>
  • Parameters

    • id: string

    Returns Promise<any>

staticentries

  • entries<T>(obj: T): [keyof T, T[keyof T]][]
  • Type parameters

    • T: object

    Parameters

    • obj: T

    Returns [keyof T, T[keyof T]][]

staticequals

  • equals(a: any, b: any): boolean
  • Checks if arguments are deeply (but not strictly) equal.


    Parameters

    • a: any
    • b: any

    Returns boolean

staticextractChildElements

  • extractChildElements(items: string[], prefix: string, allSymbol?: string): string[]
  • Parameters

    • items: string[]
    • prefix: string
    • optionalallSymbol: string

    Returns string[]

staticextractEnumValues

  • extractEnumValues(target: Dictionary): (string | number)[]
  • Extracts all possible values of a TS enum. Works with both string and numeric enums.


    Parameters

    Returns (string | number)[]

staticextractPK

  • extractPK<T>(data: any, meta?: EntityMetadata<T>, strict?: boolean): null | string | (T extends { [PrimaryKeyProp]?: PK } ? PK extends keyof T ? ReadonlyPrimary<UnwrapPrimary<T[PK]>> : PK extends keyof T[] ? ReadonlyPrimary<PrimaryPropToType<T, PK>> : PK : T extends { _id?: PK } ? string | ReadonlyPrimary<PK> : T extends { uuid?: PK } ? ReadonlyPrimary<PK> : T extends { id?: PK } ? ReadonlyPrimary<PK> : T)
  • Extracts primary key from data. Accepts objects or primary keys directly.


    Type parameters

    • T: object

    Parameters

    Returns null | string | (T extends { [PrimaryKeyProp]?: PK } ? PK extends keyof T ? ReadonlyPrimary<UnwrapPrimary<T[PK]>> : PK extends keyof T[] ? ReadonlyPrimary<PrimaryPropToType<T, PK>> : PK : T extends { _id?: PK } ? string | ReadonlyPrimary<PK> : T extends { uuid?: PK } ? ReadonlyPrimary<PK> : T extends { id?: PK } ? ReadonlyPrimary<PK> : T)

staticfileURLToPath

  • fileURLToPath(url: string | URL): string
  • Parameters

    • url: string | URL

    Returns string

staticfindDuplicates

  • findDuplicates<T>(items: T[]): T[]
  • Type parameters

    • T

    Parameters

    • items: T[]

    Returns T[]

staticflatten

  • flatten<T>(arrays: T[][]): T[]
  • Type parameters

    • T

    Parameters

    • arrays: T[][]

    Returns T[]

staticgetCompositeKeyHash

  • Type parameters

    • T

    Parameters

    Returns string

staticgetCompositeKeyValue

staticgetGlobalStorage

staticgetORMVersion

  • getORMVersion(): string
  • Returns string

staticgetObjectKeysSize

staticgetObjectType

  • getObjectType(value: any): string
  • Gets the type of the argument.


    Parameters

    • value: any

    Returns string

staticgetOrderedPrimaryKeys

staticgetParamNames

  • getParamNames(func: string | { toString: any } | { type: string; value: string }[], methodName?: string): string[]
  • Returns array of functions argument names. Uses esprima for source code analysis.


    Parameters

    • func: string | { toString: any } | { type: string; value: string }[]
    • optionalmethodName: string

    Returns string[]

staticgetPrimaryKeyCond

  • getPrimaryKeyCond<T>(entity: T, primaryKeys: EntityKey<T>[]): null | Record<string, Primary<T>>
  • Type parameters

    • T

    Parameters

    Returns null | Record<string, Primary<T>>

staticgetPrimaryKeyCondFromArray

  • getPrimaryKeyCondFromArray<T>(pks: (T extends { [PrimaryKeyProp]?: PK } ? PK extends keyof T ? ReadonlyPrimary<UnwrapPrimary<T[PK]>> : PK extends keyof T[] ? ReadonlyPrimary<PrimaryPropToType<T, PK>> : PK : T extends { _id?: PK } ? string | ReadonlyPrimary<PK> : T extends { uuid?: PK } ? ReadonlyPrimary<PK> : T extends { id?: PK } ? ReadonlyPrimary<PK> : T)[], meta: EntityMetadata<T>): Record<string, T extends { [PrimaryKeyProp]?: PK } ? PK extends keyof T ? ReadonlyPrimary<UnwrapPrimary<T[PK]>> : PK extends keyof T[] ? ReadonlyPrimary<PrimaryPropToType<T, PK>> : PK : T extends { _id?: PK } ? string | ReadonlyPrimary<PK> : T extends { uuid?: PK } ? ReadonlyPrimary<PK> : T extends { id?: PK } ? ReadonlyPrimary<PK> : T>
  • Type parameters

    • T: object

    Parameters

    • pks: (T extends { [PrimaryKeyProp]?: PK } ? PK extends keyof T ? ReadonlyPrimary<UnwrapPrimary<T[PK]>> : PK extends keyof T[] ? ReadonlyPrimary<PrimaryPropToType<T, PK>> : PK : T extends { _id?: PK } ? string | ReadonlyPrimary<PK> : T extends { uuid?: PK } ? ReadonlyPrimary<PK> : T extends { id?: PK } ? ReadonlyPrimary<PK> : T)[]
    • meta: EntityMetadata<T>

    Returns Record<string, T extends { [PrimaryKeyProp]?: PK } ? PK extends keyof T ? ReadonlyPrimary<UnwrapPrimary<T[PK]>> : PK extends keyof T[] ? ReadonlyPrimary<PrimaryPropToType<T, PK>> : PK : T extends { _id?: PK } ? string | ReadonlyPrimary<PK> : T extends { uuid?: PK } ? ReadonlyPrimary<PK> : T extends { id?: PK } ? ReadonlyPrimary<PK> : T>

staticgetPrimaryKeyHash

  • getPrimaryKeyHash(pks: (string | Buffer)[]): string
  • Parameters

    • pks: (string | Buffer)[]

    Returns string

staticgetPrimaryKeyValues

  • getPrimaryKeyValues<T>(entity: T, primaryKeys: string[], allowScalar?: boolean, convertCustomTypes?: boolean): any
  • Type parameters

    • T

    Parameters

    • entity: T
    • primaryKeys: string[]
    • allowScalar: boolean = false
    • convertCustomTypes: boolean = false

    Returns any

staticgetRootEntity

statichasNestedKey

  • hasNestedKey(object: unknown, key: string): boolean
  • Parameters

    • object: unknown
    • key: string

    Returns boolean

statichasObjectKeys

statichash

  • hash(data: string, length?: number): string
  • Parameters

    • data: string
    • optionallength: number

    Returns string

staticisArrayOperator

  • isArrayOperator(key: PropertyKey): boolean
  • Parameters

    • key: PropertyKey

    Returns boolean

staticisCollection

  • isCollection<T, O>(item: any): item is Collection<T, O>
  • Type parameters

    • T: object
    • O: object = object

    Parameters

    • item: any

    Returns item is Collection<T, O>

staticisDefined

  • isDefined<T>(data: any): data is T
  • Checks if the argument is not undefined


    Type parameters

    • T = Record<string, unknown>

    Parameters

    • data: any

    Returns data is T

staticisEmpty

  • isEmpty(data: any): boolean
  • Checks whether the argument is empty (array without items, object without keys or falsy value).


    Parameters

    • data: any

    Returns boolean

staticisEntity

  • isEntity<T>(data: any, allowReference?: boolean): data is T & {}
  • Checks whether given object is an entity instance.


    Type parameters

    • T = unknown

    Parameters

    • data: any
    • allowReference: boolean = false

    Returns data is T & {}

staticisGroupOperator

  • isGroupOperator(key: PropertyKey): boolean
  • Parameters

    • key: PropertyKey

    Returns boolean

staticisIterable

  • isIterable<T>(value: unknown): value is Iterable<T>
  • Checks if the value is iterable, but considers strings and buffers as not iterable.


    Type parameters

    • T

    Parameters

    • value: unknown

    Returns value is Iterable<T>

staticisJsonKeyOperator

  • isJsonKeyOperator(key: PropertyKey): boolean
  • Parameters

    • key: PropertyKey

    Returns boolean

staticisNotObject

  • isNotObject<T>(o: any, not: any[]): o is T
  • Checks if the argument is instance of Object, but not one of the blacklisted types. Returns false for arrays.


    Type parameters

    Parameters

    • o: any
    • not: any[]

    Returns o is T

staticisNumber

  • isNumber<T>(s: any): s is T
  • Checks if the argument is number


    Type parameters

    • T = number

    Parameters

    • s: any

    Returns s is T

staticisObject

  • isObject<T>(o: any): o is T
  • Checks if the argument is instance of Object. Returns false for arrays.


    Type parameters

    Parameters

    • o: any

    Returns o is T

staticisObjectID

  • isObjectID(key: any): boolean
  • Checks whether the argument is ObjectId instance


    Parameters

    • key: any

    Returns boolean

staticisOperator

  • isOperator(key: PropertyKey, includeGroupOperators?: boolean): boolean
  • Parameters

    • key: PropertyKey
    • includeGroupOperators: boolean = true

    Returns boolean

staticisPlainObject

  • isPlainObject<T>(value: any): value is T
  • Checks whether the value is POJO (e.g. { foo: 'bar' }, and not instance of Foo)


    Type parameters

    Parameters

    • value: any

    Returns value is T

staticisPrimaryKey

  • isPrimaryKey<T>(key: any, allowComposite?: boolean): key is Primary<T>
  • Checks whether the argument looks like primary key (string, number or ObjectId).


    Type parameters

    • T

    Parameters

    • key: any
    • allowComposite: boolean = false

    Returns key is Primary<T>

staticisRawSql

  • isRawSql(value: unknown): value is { params: unknown[]; sql: string; use: () => void }
  • Parameters

    • value: unknown

    Returns value is { params: unknown[]; sql: string; use: () => void }

staticisScalarReference

  • isScalarReference<T>(data: any, allowReference?: boolean): data is ScalarReference<any>
  • Checks whether given object is a scalar reference.


    Type parameters

    • T = unknown

    Parameters

    • data: any
    • allowReference: boolean = false

    Returns data is ScalarReference<any>

staticisString

  • isString(s: any): s is string
  • Checks if the argument is string


    Parameters

    • s: any

    Returns s is string

statickeys

  • keys<T>(obj: T): keyof T[]
  • Type parameters

    • T: object

    Parameters

    • obj: T

    Returns keyof T[]

staticlookupPathFromDecorator

  • lookupPathFromDecorator(name: string, stack?: string[]): string
  • Uses some dark magic to get source path to caller where decorator is used. Analyses stack trace of error created inside the function call.


    Parameters

    • name: string
    • optionalstack: string[]

    Returns string

staticmapFlatCompositePrimaryKey

  • mapFlatCompositePrimaryKey(fk: any[], prop: EntityProperty<any, any>, fieldNames?: string[], idx?: number): any
  • Maps nested FKs from [1, 2, 3] to [1, [2, 3]].


    Parameters

    • fk: any[]
    • prop: EntityProperty<any, any>
    • fieldNames: string[] = prop.fieldNames
    • idx: number = 0

    Returns any

staticmerge

  • merge(target: any, ...sources: any[]): any
  • Merges all sources into the target recursively.


    Parameters

    • target: any
    • rest...sources: any[]

    Returns any

staticmergeConfig

  • mergeConfig(target: any, ...sources: any[]): any
  • Merges all sources into the target recursively. Ignores undefined values.


    Parameters

    • target: any
    • rest...sources: any[]

    Returns any

staticnormalizePath

  • normalizePath(...parts: string[]): string
  • Resolves and normalizes a series of path parts relative to each preceeding part. If any part is a file: URL, it is converted to a local path. If any part is an absolute path, it replaces preceeding paths (similar to path.resolve in NodeJS). Trailing directory separators are removed, and all directory separators are converted to POSIX-style separators (/).


    Parameters

    • rest...parts: string[]

    Returns string

staticparseArgs

  • parseArgs<T>(): T
  • simple process.argv parser, supports only properties with long names, prefixed with --


    Type parameters

    Returns T

staticpathExists

  • pathExists(path: string, options?: GlobbyOptions): Promise<boolean>
  • Parameters

    • path: string
    • options: GlobbyOptions = {}

    Returns Promise<boolean>

staticprimaryKeyToObject

  • Type parameters

    • T

    Parameters

    Returns T

staticpropertyDecoratorReturnValue

  • propertyDecoratorReturnValue(): any

staticrandomInt

  • randomInt(min: number, max: number): number
  • Parameters

    • min: number
    • max: number

    Returns number

staticrelativePath

  • relativePath(path: string, relativeTo: string): string
  • Determines the relative path between two paths. If either path is a file: URL, it is converted to a local path.


    Parameters

    • path: string
    • relativeTo: string

    Returns string

staticremoveDuplicates

  • removeDuplicates<T>(items: T[]): T[]
  • Type parameters

    • T

    Parameters

    • items: T[]

    Returns T[]

staticrenameKey

  • renameKey<T>(payload: T, from: string | keyof T, to: string): void
  • Renames object key, keeps order of properties.


    Type parameters

    • T

    Parameters

    • payload: T
    • from: string | keyof T
    • to: string

    Returns void

staticrequireFrom

  • requireFrom<T>(id: string, from?: string): T
  • Require a module from a specific location


    Type parameters

    Parameters

    • id: string

      The module to require

    • optionalfrom: string = ...

      Location to start the node resolution

    Returns T

staticrunIfNotEmpty

  • runIfNotEmpty(clause: () => any, data: any): void
  • Parameters

    • clause: () => any
    • data: any

    Returns void

staticrunSerial

  • runSerial<T, U>(items: Iterable<U>, cb: (item: U) => Promise<T>): Promise<T[]>
  • Executes the cb promise serially on every element of the items array and returns array of resolved values.


    Type parameters

    • T = any
    • U = any

    Parameters

    • items: Iterable<U>
    • cb: (item: U) => Promise<T>

    Returns Promise<T[]>

staticsetDynamicImportProvider

  • setDynamicImportProvider(provider: (id: string) => Promise<unknown>): void
  • Parameters

    • provider: (id: string) => Promise<unknown>

    Returns void

staticsetPayloadProperty

staticsplitPrimaryKeys

  • splitPrimaryKeys<T>(key: string): EntityKey<T>[]
  • Type parameters

    • T: object

    Parameters

    • key: string

    Returns EntityKey<T>[]

staticstripRelativePath

  • stripRelativePath(str: string): string
  • Parameters

    • str: string

    Returns string

statictokenize

  • tokenize(func: string | { toString: any } | { type: string; value: string }[]): { type: string; value: string }[]
  • Returns array of functions argument names. Uses esprima for source code analysis.


    Parameters

    • func: string | { toString: any } | { type: string; value: string }[]

    Returns { type: string; value: string }[]

statictryRequire

  • tryRequire<T>(__namedParameters: { allowError?: string; from?: string; module: string; warning: string }): undefined | T
  • Type parameters

    Parameters

    • __namedParameters: { allowError?: string; from?: string; module: string; warning: string }

    Returns undefined | T

staticunique

  • unique<T>(items: T[]): T[]
  • Gets array without duplicates.


    Type parameters

    • T = string

    Parameters

    • items: T[]

    Returns T[]

staticunwrapProperty

staticvalues

  • values<T>(obj: T): T[keyof T][]
  • Type parameters

    • T: object

    Parameters

    • obj: T

    Returns T[keyof T][]

staticxor

  • xor(a: boolean, b: boolean): boolean
  • Parameters

    • a: boolean
    • b: boolean

    Returns boolean