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

Commit

Permalink
Merge pull request #3 from egomobile/feature/data-transformations
Browse files Browse the repository at this point in the history
add fields prop to IEntityConfig
  • Loading branch information
Marcel Kloubert authored Mar 21, 2022
2 parents 91c5dbb + 7c84a0b commit 78f0e35
Show file tree
Hide file tree
Showing 12 changed files with 411 additions and 69 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log (@egomobile/orm)

## 0.6.0

- add types which helpds to realize data transformations between databases and entities
- add helper methods to [DataAdapterBase](https://egomobile.github.io/node-orm/classes/DataAdapterBase.html) to simply access entity configuration data

## 0.5.0

- `insert()`, `remove()` and `update()` of [IDataRepository](https://egomobile.github.io/node-orm/interfaces/IDataRepository.html) return the updated entity now
Expand Down
114 changes: 57 additions & 57 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.5.0",
"version": "0.6.0",
"description": "A simple and generic ORM mapper.",
"main": "lib/index.js",
"engines": {
Expand Down
73 changes: 71 additions & 2 deletions src/classes/DataAdapterBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,81 @@ export abstract class DataAdapterBase implements IDataAdapter {
/**
* @inheritdoc
*/
public abstract find<T extends any = any>(type: Constructor<T>, options?: IFindOptions | null): Promise<T[]>;
public abstract find<T extends any = any>(type: Constructor<T>, options?: Nilable<IFindOptions>): Promise<T[]>;

/**
* @inheritdoc
*/
public abstract findOne<T extends any = any>(type: Constructor<T>, options?: IFindOneOptions | null): Promise<T | null>;
public abstract findOne<T extends any = any>(type: Constructor<T>, options?: Nilable<IFindOneOptions>): Promise<T | null>;

private getEntityByType(type: Constructor<any>) {
for (const [name, config] of Object.entries(this.context.entities)) {
if (config.type === type) {
return {
config,
name
};
}
}

return null;
}

private getEntityByTypeOrThrow(type: Constructor<any>) {
const entity = this.getEntityByType(type);
if (entity) {
return entity;
} else {
throw new Error(`Entity type ${type.name} not configured`);
}
}

/**
* Returns the list of entity/table fields, which represent
* the IDs of a row.
*
* @param {Constructor<any>} type The type.
*
* @returns {string[]} The list of field names.
*/
public getEntityIdsByType(type: Constructor<any>): string[] {
const entity = this.getEntityByTypeOrThrow(type);

if (entity.config.ids?.length) {
return entity.config.ids;
} else {
return [];
}
}

/**
* Returns the list of entity fields columns, which represent
* the IDs of a row, or throws an exception if not defined.
*
* @param {Constructor<any>} type The type.
*
* @returns {string[]} The list of ID fields.
*/
public getEntityIdsByTypeOrThrow(type: Constructor<any>): string[] {
const entity = this.getEntityByTypeOrThrow(type);

if (entity.config.ids?.length) {
return entity.config.ids;
} else {
throw new Error(`No IDs defined for type ${type.name}`);
}
}

/**
* Returns the name of the underlying entity/table by type or throws an exception, if not configured.
*
* @param {Constructor<any>} type The type.
*
* @returns {string} The name of the underlying entity/table name.
*/
public getEntityNameByTypeOrThrow(type: Constructor<any>): string {
return this.getEntityByTypeOrThrow(type).name;
}

/**
* @inheritdoc
Expand Down
6 changes: 3 additions & 3 deletions src/classes/DataContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import type { IDataAdapter, IDataContext, EntityConfigurations, IFindOptions, IFindOneOptions } from '../types';
import type { Constructor, List } from '../types/internal';
import type { Constructor, List, Nilable } from '../types/internal';

export interface IDataContextOptions {
/**
Expand All @@ -37,11 +37,11 @@ export class DataContext implements IDataContext {
return this.options.entities;
}

public find<T extends any = any>(type: Constructor<T>, options?: IFindOptions | null) {
public find<T extends any = any>(type: Constructor<T>, options?: Nilable<IFindOptions>) {
return this.options.adapter.find<T>(type, options);
}

public findOne<T extends any = any>(type: Constructor<T>, options?: IFindOneOptions | null) {
public findOne<T extends any = any>(type: Constructor<T>, options?: Nilable<IFindOneOptions>) {
return this.options.adapter.findOne<T>(type, options);
}

Expand Down
Loading

0 comments on commit 78f0e35

Please sign in to comment.