Interface IDataAdapter

A data adapter, like a Mongo or PostgreSQL connection.

interface IDataAdapter {
    count<T>(type, options?): Promise<number>;
    find<T>(type, options?): Promise<T[]>;
    findOne<T>(type, options?): Promise<null | T>;
    insert<T>(entity): Promise<T>;
    insert<T>(entities): Promise<T[]>;
    insert<T>(entityOrEntities): Promise<T | T[]>;
    query<T>(q, ...paramsOrArgs?): Promise<T>;
    queryAndIterate<T>(type, q, ...paramsOrArgs?): AsyncGenerator<T, any, unknown>;
    queryAndMap<T>(type, q, ...paramsOrArgs?): Promise<T[]>;
    remove<T>(entity): Promise<T>;
    remove<T>(entities): Promise<T[]>;
    remove<T>(entityOrEntities): Promise<T | T[]>;
    setContext(context): this;
    update<T>(entity): Promise<T>;
    update<T>(entities): Promise<T[]>;
    update<T>(entityOrEntities): Promise<T | T[]>;
}

Hierarchy (view full)

Implemented by

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)
    }
    })
    }
  • Insert one or more entities.

    Type Parameters

    • T extends unknown = any

    Parameters

    • entity: T

      The entity to insert.

    Returns Promise<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)
    }
  • Type Parameters

    • T extends unknown = any

    Parameters

    • entities: List<T>

    Returns Promise<T[]>

  • Type Parameters

    • T extends unknown = any

    Parameters

    • entityOrEntities: T | List<T>

    Returns Promise<T | T[]>

  • Does a raw query.

    Type Parameters

    • T extends unknown = any

    Parameters

    • q: any

      The object / value, which represents the query.

    • Optional Rest ...paramsOrArgs: any[]

      A list of optional parameters or arguments for the query.

    Returns Promise<T>

    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.

    • Optional 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".

    Type Parameters

    • T extends unknown = any

    Parameters

    • type: Constructor<T>

      The target type.

    • q: any

      The object / value, which represents the query.

    • Optional Rest ...paramsOrArgs: any[]

      A list of optional parameters or arguments for the query.

    Returns Promise<T[]>

    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

    • entity: T

      The entity to remove.

    Returns Promise<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)
    }
  • Type Parameters

    • T extends unknown = any

    Parameters

    • entities: List<T>

    Returns Promise<T[]>

  • Type Parameters

    • T extends unknown = any

    Parameters

    • entityOrEntities: T | List<T>

    Returns Promise<T | T[]>

  • Updates one or more entities.

    Type Parameters

    • T extends unknown = any

    Parameters

    • entity: T

      The entity to update.

    Returns Promise<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)
    }
  • Type Parameters

    • T extends unknown = any

    Parameters

    • entities: List<T>

    Returns Promise<T[]>

  • Type Parameters

    • T extends unknown = any

    Parameters

    • entityOrEntities: T | List<T>

    Returns Promise<T | T[]>

Generated using TypeDoc