Class DataAdapterBaseAbstract

A basic data adapter.

Implements

Constructors

Properties

_context: Nilable<IDataContext>

The underlying adapter.

Accessors

Methods

  • Counts a list of items.

    Type Parameters

    • T extends unknown = any

    Parameters

    • type: Constructor<T>

      The class / type.

    • Optional options: Nilable<IFindOptions>

      The custom options.

    Returns Promise<number>

    The promise with the items.

    Example

    import { IDataRepository } from '@egomobile/orm'

    class User {
    id: number = -1;
    }

    async function countActiveUsers(repo: IDataRepository): Promise<number> {
    // in SQL context
    return await repo.count(User, {
    where: 'is_active=$1 AND (is_deleted=$2 OR is_deleted IS NULL)',
    params: [ true, false ], // $1, $2
    })
    }
  • Finds a list of items.

    Type Parameters

    • T extends unknown = any

    Parameters

    • type: Constructor<T>

      The class / type.

    • Optional options: Nilable<IFindOptions>

      The custom options.

    Returns Promise<T[]>

    The promise with the items.

    Example

    import { IDataRepository } from '@egomobile/orm'

    // keep sure to initialize your props
    // with a value, which is not (undefined)
    class User {
    id: number = -1;
    first_name: string = '';
    last_name: string = '';
    is_active: boolean | null = null;
    is_deleted: boolean = false;
    }

    async function load10ActiveUsersAndSkipFirst(repo: IDataRepository): Promise<User[]> {
    // in SQL context
    return await repo.find(User, {
    where: 'is_active=$1 AND (is_deleted=$2 OR is_deleted IS NULL)',
    params: [ true, false ], // $1, $2

    offset: 1,
    limit: 10
    })
    }
  • Tries to find a simple item.

    Type Parameters

    • T extends unknown = any

    Parameters

    • type: Constructor<T>

      The class / type.

    • Optional options: Nilable<IFindOneOptions>

      The custom options.

    Returns Promise<null | T>

    The promise with the item or (null) if not found.

    Example

    import { IDataRepository } from '@egomobile/orm'

    // keep sure to initialize your props
    // with a value, which is not (undefined)
    class User {
    id: number = -1;
    first_name: string = '';
    last_name: string = '';
    is_active: boolean | null = null;
    is_deleted: boolean = false;
    }

    async function loadLastActiveUser(repo: IDataRepository): Promise<User> {
    // in SQL context
    return await repo.findOne(User, {
    where: 'is_active=$1 AND (is_deleted=$2 OR is_deleted IS NULL)',
    params: [ true, false ], // $1, $2

    sort: {
    'created': 'DESC', // first sort by 'created' (descending)
    'id': 'DESC', // then by 'id' (descending)
    'last_name': 'ASC', // then by 'last_name' (ascending)
    'first_name': 'ASC' // then by 'first_name' (ascending)
    }
    })
    }
  • Returns the list of entity/table fields, which represent the IDs of a row.

    Parameters

    • type: Constructor<any>

      The type.

    Returns string[]

    The list of field names.

  • Returns the list of entity fields columns, which represent the IDs of a row, or throws an exception if not defined.

    Parameters

    • type: Constructor<any>

      The type.

    Returns string[]

    The list of ID fields.

  • Returns the name of the underlying entity/table by type or throws an exception, if not configured.

    Parameters

    • type: Constructor<any>

      The type.

    Returns string

    The name of the underlying entity/table name.

  • Insert one or more entities.

    Type Parameters

    • T extends unknown = any

    Parameters

    • entityOrEntities: T | List<T>

      The entity to insert.

    Returns Promise<T | T[]>

    The promise with the inserted entity/entities.

    Example

    import { IDataRepository } from '@egomobile/orm'

    // keep sure to initialize your props
    // with a value, which is not (undefined)
    class User {
    id: number | null = null;
    first_name: string = '';
    last_name: string = '';
    is_active: boolean | null = null;
    is_deleted: boolean = false;
    }

    async function createUser(repo: IDataRepository, firstName: string, lastName: string): Promise<User> {
    const newUser = new User()
    newUser.last_name = lastName
    newUser.first_name = firstName
    newUser.is_active = true

    await repo.insert(newUser)
    }
  • Does a raw query.

    Parameters

    • q: any

      The object / value, which represents the query.

    • Rest ...paramsOrArgs: any[]

      A list of optional parameters or arguments for the query.

    Returns Promise<any>

    The promise with the raw result.

    Example

    import { IDataRepository } from '@egomobile/orm'

    async function deleteInactiveUsers(repo: IDataRepository) {
    // in SQL context
    //
    // result is an object or value
    // from the underlying data adapter itself
    const result: any = await repo.query(
    "UPDATE users SET is_deleted=$1 WHERE is_active=$2;",
    false, false // $1, $2
    )
    }
  • Does a raw query and maps the result(s) to entity objects. The classes do not need to be configured in context, so it is possbile to implement and work with "joins". The difference to queryAndMap() is, that is makes use of cursor pattern instead of returning final data.

    Type Parameters

    • T extends unknown = any

    Parameters

    • type: Constructor<T>

      The target type.

    • q: any

      The object / value, which represents the query.

    • Rest ...paramsOrArgs: any[]

      A list of optional parameters or arguments for the query.

    Returns AsyncGenerator<T, any, unknown>

    The async generator.

    Example

    import { IDataRepository } from '@egomobile/orm'

    class UserAndRole {
    role_id!: string;
    user_id!: string;
    }

    async function loadUserRoles(repo: IDataRepository): AsyncGenerator<UserAndRole> {
    // PostgreSQL example
    // s. https://github.com/egomobile/node-orm-pg

    return repo.queryAndIterate(
    UserAndRole, // type of the target entity
    // does not need to be configured
    // in data context

    // build query
    "SELECT DISTINCT ur.id AS role_id, u.id AS user_id " +
    "FROM user_roles ur " +
    "INNER JOIN users u ON u.id = ur.user_id " +
    "WHERE u.is_active = $1 AND u.is_deleted = $2;",

    // additional parameters
    true, false // $1, $2
    )
    }

    const iterator = loadUserRoles(
    // ... your repo instance ...
    )

    for await (const userAndRole of iterator) {
    // your code ...
    }
  • Does a raw query and maps the result(s) to entity objects. The classes do not need to be configured in context, so it is possbile to implement and work with "joins".

    Parameters

    • type: Constructor<any>

      The target type.

    • q: any

      The object / value, which represents the query.

    • Rest ...paramsOrArgs: any[]

      A list of optional parameters or arguments for the query.

    Returns Promise<any[]>

    The promise with the mapped entities.

    Example

    import { IDataRepository } from '@egomobile/orm'

    class UserAndRole {
    role_id!: string;
    user_id!: string;
    }

    async function loadUserRoles(repo: IDataRepository): Promise<UserAndRole[]> {
    // PostgreSQL example
    // s. https://github.com/egomobile/node-orm-pg

    return await repo.queryAndMap(
    UserAndRole, // type of the target entity
    // does not need to be configured
    // in data context

    // build query
    "SELECT DISTINCT ur.id AS role_id, u.id AS user_id " +
    "FROM user_roles ur " +
    "INNER JOIN users u ON u.id = ur.user_id " +
    "WHERE u.is_active = $1 AND u.is_deleted = $2;",

    // additional parameters
    true, false // $1, $2
    )
    }
  • Removes one or more entities.

    Type Parameters

    • T extends unknown = any

    Parameters

    • entityOrEntities: T | List<T>

      The entity to remove.

    Returns Promise<T | T[]>

    The promise with the removed entity/entities.

    Example

    import { IDataRepository } from '@egomobile/orm'

    // keep sure to initialize your props
    // with a value, which is not (undefined)
    class User {
    id: number = -1;
    is_active: boolean | null = null;
    }

    async function removeInactiveUsers(repo: IDataRepository, users: User[]) {
    const inactiveUsers = users.find(u => u.is_active === false)

    await repo.remove(inactiveUsers)
    }
  • Updates one or more entities.

    Type Parameters

    • T extends unknown = any

    Parameters

    • entityOrEntities: T | List<T>

      The entity to update.

    Returns Promise<T | T[]>

    The promise with the updated entity/entities.

    Example

    import { IDataRepository } from '@egomobile/orm'

    // keep sure to initialize your props
    // with a value, which is not (undefined)
    class User {
    id: number = -1;
    is_deleted: boolean = false;
    }

    async function deleteUsers(repo: IDataRepository, users: User[]) {
    users.forEach((user) => {
    user.is_deleted = true
    })

    await repo.update(inactiveUsers)
    }

Generated using TypeDoc