Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
implement queryAndMap()
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Kloubert committed Mar 22, 2022
1 parent 6479a94 commit f203219
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log (@egomobile/orm)

## 0.7.0

- add [queryAndMap()](https://egomobile.github.io/node-orm/interfaces/IDataRepository.html#queryAndMap)

## 0.6.1

- add types which helpds to realize data transformations between databases and entities
Expand Down
25 changes: 11 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@egomobile/orm",
"version": "0.6.1",
"version": "0.7.0",
"description": "A simple and generic ORM mapper.",
"main": "lib/index.js",
"engines": {
Expand Down
5 changes: 5 additions & 0 deletions src/classes/DataAdapterBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ export abstract class DataAdapterBase implements IDataAdapter {
*/
public abstract query(q: any, ...paramsOrArgs: any[]): Promise<any>;

/**
* @inheritdoc
*/
public abstract queryAndMap(type: Constructor<any>, q: any, ...paramsOrArgs: any[]): Promise<any[]>;

/**
* @inheritdoc
*/
Expand Down
4 changes: 4 additions & 0 deletions src/classes/DataContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export class DataContext implements IDataContext {
return this.options.adapter.query(q, ...paramsOrArgs);
}

public queryAndMap<T extends any = any>(type: Constructor<T>, q: any, ...paramsOrArgs: any[]): Promise<T[]> {
return this.options.adapter.queryAndMap(type, q, ...paramsOrArgs);
}

public remove<T extends any = any>(entities: T | List<T>) {
return this.options.adapter.remove<T>(entities);
}
Expand Down
43 changes: 43 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,49 @@ export interface IDataRepository {
*/
query<T extends any = any>(q: any, ...paramsOrArgs: any[]): Promise<T>;

/**
* 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".
*
* @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
* )
* }
* ```
*
* @param {Constructor<T>} type The target type.
* @param {any} q The object / value, which represents the query.
* @param {any[]} [paramsOrArgs] A list of optional parameters or arguments for the query.
*
* @returns {Promise<T[]>} The promise with the mapped entities.
*/
queryAndMap<T extends any = any>(type: Constructor<T>, q: any, ...paramsOrArgs: any[]): Promise<T[]>;

/**
* Removes one or more entities.
*
Expand Down

0 comments on commit f203219

Please sign in to comment.