diff --git a/CHANGELOG.md b/CHANGELOG.md index fce6968..b22227e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package-lock.json b/package-lock.json index ed56969..a9a2a95 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@egomobile/orm", - "version": "0.5.0", + "version": "0.6.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -23,24 +23,24 @@ } }, "@babel/compat-data": { - "version": "7.17.0", - "resolved": "https://registry.npmjs.org/@babel/compat-data/-/compat-data-7.17.0.tgz", - "integrity": "sha512-392byTlpGWXMv4FbyWw3sAZ/FrW/DrwqLGXpy0mbyNe9Taqv1mg9yON5/o0cnr8XYCkFTZbC1eV+c+LAROgrng==", + "version": "7.17.7", + "resolved": "https://registry.npmjs.org/@babel/compat-data/-/compat-data-7.17.7.tgz", + "integrity": "sha512-p8pdE6j0a29TNGebNm7NzYZWB3xVZJBZ7XGs42uAKzQo8VQ3F0By/cQCtUEABwIqw5zo6WA4NbmxsfzADzMKnQ==", "dev": true }, "@babel/core": { - "version": "7.17.5", - "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.17.5.tgz", - "integrity": "sha512-/BBMw4EvjmyquN5O+t5eh0+YqB3XXJkYD2cjKpYtWOfFy4lQ4UozNSmxAcWT8r2XtZs0ewG+zrfsqeR15i1ajA==", + "version": "7.17.8", + "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.17.8.tgz", + "integrity": "sha512-OdQDV/7cRBtJHLSOBqqbYNkOcydOgnX59TZx4puf41fzcVtN3e/4yqY8lMQsK+5X2lJtAdmA+6OHqsj1hBJ4IQ==", "dev": true, "requires": { "@ampproject/remapping": "^2.1.0", "@babel/code-frame": "^7.16.7", - "@babel/generator": "^7.17.3", - "@babel/helper-compilation-targets": "^7.16.7", - "@babel/helper-module-transforms": "^7.16.7", - "@babel/helpers": "^7.17.2", - "@babel/parser": "^7.17.3", + "@babel/generator": "^7.17.7", + "@babel/helper-compilation-targets": "^7.17.7", + "@babel/helper-module-transforms": "^7.17.7", + "@babel/helpers": "^7.17.8", + "@babel/parser": "^7.17.8", "@babel/template": "^7.16.7", "@babel/traverse": "^7.17.3", "@babel/types": "^7.17.0", @@ -88,9 +88,9 @@ } }, "@babel/generator": { - "version": "7.17.3", - "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.17.3.tgz", - "integrity": "sha512-+R6Dctil/MgUsZsZAkYgK+ADNSZzJRRy0TvY65T71z/CR854xHQ1EweBYXdfT+HNeN7w0cSJJEzgxZMv40pxsg==", + "version": "7.17.7", + "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.17.7.tgz", + "integrity": "sha512-oLcVCTeIFadUoArDTwpluncplrYBmTCCZZgXCbgNGvOBBiSDDK3eWO4b/+eOTli5tKv1lg+a5/NAXg+nTcei1w==", "dev": true, "requires": { "@babel/types": "^7.17.0", @@ -99,12 +99,12 @@ } }, "@babel/helper-compilation-targets": { - "version": "7.16.7", - "resolved": "https://registry.npmjs.org/@babel/helper-compilation-targets/-/helper-compilation-targets-7.16.7.tgz", - "integrity": "sha512-mGojBwIWcwGD6rfqgRXVlVYmPAv7eOpIemUG3dGnDdCY4Pae70ROij3XmfrH6Fa1h1aiDylpglbZyktfzyo/hA==", + "version": "7.17.7", + "resolved": "https://registry.npmjs.org/@babel/helper-compilation-targets/-/helper-compilation-targets-7.17.7.tgz", + "integrity": "sha512-UFzlz2jjd8kroj0hmCFV5zr+tQPi1dpC2cRsDV/3IEW8bJfCPrPpmcSN6ZS8RqIq4LXcmpipCQFPddyFA5Yc7w==", "dev": true, "requires": { - "@babel/compat-data": "^7.16.4", + "@babel/compat-data": "^7.17.7", "@babel/helper-validator-option": "^7.16.7", "browserslist": "^4.17.5", "semver": "^6.3.0" @@ -166,14 +166,14 @@ } }, "@babel/helper-module-transforms": { - "version": "7.17.6", - "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.17.6.tgz", - "integrity": "sha512-2ULmRdqoOMpdvkbT8jONrZML/XALfzxlb052bldftkicAUy8AxSCkD5trDPQcwHNmolcl7wP6ehNqMlyUw6AaA==", + "version": "7.17.7", + "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.17.7.tgz", + "integrity": "sha512-VmZD99F3gNTYB7fJRDTi+u6l/zxY0BE6OIxPSU7a50s6ZUQkHwSDmV92FfM+oCG0pZRVojGYhkR8I0OGeCVREw==", "dev": true, "requires": { "@babel/helper-environment-visitor": "^7.16.7", "@babel/helper-module-imports": "^7.16.7", - "@babel/helper-simple-access": "^7.16.7", + "@babel/helper-simple-access": "^7.17.7", "@babel/helper-split-export-declaration": "^7.16.7", "@babel/helper-validator-identifier": "^7.16.7", "@babel/template": "^7.16.7", @@ -182,12 +182,12 @@ } }, "@babel/helper-simple-access": { - "version": "7.16.7", - "resolved": "https://registry.npmjs.org/@babel/helper-simple-access/-/helper-simple-access-7.16.7.tgz", - "integrity": "sha512-ZIzHVyoeLMvXMN/vok/a4LWRy8G2v205mNP0XOuf9XRLyX5/u9CnVulUtDgUTama3lT+bf/UqucuZjqiGuTS1g==", + "version": "7.17.7", + "resolved": "https://registry.npmjs.org/@babel/helper-simple-access/-/helper-simple-access-7.17.7.tgz", + "integrity": "sha512-txyMCGroZ96i+Pxr3Je3lzEJjqwaRC9buMUgtomcrLe5Nd0+fk1h0LLA+ixUF5OW7AhHuQ7Es1WcQJZmZsz2XA==", "dev": true, "requires": { - "@babel/types": "^7.16.7" + "@babel/types": "^7.17.0" } }, "@babel/helper-split-export-declaration": { @@ -212,13 +212,13 @@ "dev": true }, "@babel/helpers": { - "version": "7.17.2", - "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.17.2.tgz", - "integrity": "sha512-0Qu7RLR1dILozr/6M0xgj+DFPmi6Bnulgm9M8BVa9ZCWxDqlSnqt3cf8IDPB5m45sVXUZ0kuQAgUrdSFFH79fQ==", + "version": "7.17.8", + "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.17.8.tgz", + "integrity": "sha512-QcL86FGxpfSJwGtAvv4iG93UL6bmqBdmoVY0CMCU2g+oD2ezQse3PT5Pa+jiD6LJndBQi0EDlpzOWNlLuhz5gw==", "dev": true, "requires": { "@babel/template": "^7.16.7", - "@babel/traverse": "^7.17.0", + "@babel/traverse": "^7.17.3", "@babel/types": "^7.17.0" } }, @@ -234,9 +234,9 @@ } }, "@babel/parser": { - "version": "7.17.3", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.17.3.tgz", - "integrity": "sha512-7yJPvPV+ESz2IUTPbOL+YkIGyCqOyNIzdguKQuJGnH7bg1WTIifuM21YqokFt/THWh1AkCRn9IgoykTRCBVpzA==", + "version": "7.17.8", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.17.8.tgz", + "integrity": "sha512-BoHhDJrJXqcg+ZL16Xv39H9n+AqJ4pcDrQBGZN+wHxIysrLZ3/ECwCBUch/1zUNhnsXULcONU3Ei5Hmkfk6kiQ==", "dev": true }, "@babel/template": { @@ -437,9 +437,9 @@ "dev": true }, "@types/json-schema": { - "version": "7.0.9", - "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.9.tgz", - "integrity": "sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==", + "version": "7.0.10", + "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.10.tgz", + "integrity": "sha512-BLO9bBq59vW3fxCpD4o0N4U+DXsvwvIcl+jofw0frQo/GrBFC+/jRZj1E7kgp6dvTyNmA4y6JCV5Id/r3mNP5A==", "dev": true }, "@types/json5": { @@ -828,13 +828,13 @@ } }, "browserslist": { - "version": "4.19.3", - "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.19.3.tgz", - "integrity": "sha512-XK3X4xtKJ+Txj8G5c30B4gsm71s69lqXlkYui4s6EkKxuv49qjYlY6oVd+IFJ73d4YymtM3+djvvt/R/iJwwDg==", + "version": "4.20.2", + "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.20.2.tgz", + "integrity": "sha512-CQOBCqp/9pDvDbx3xfMi+86pr4KXIf2FDkTTdeuYw8OxS9t898LA1Khq57gtufFILXpfgsSx5woNgsBgvGjpsA==", "dev": true, "requires": { - "caniuse-lite": "^1.0.30001312", - "electron-to-chromium": "^1.4.71", + "caniuse-lite": "^1.0.30001317", + "electron-to-chromium": "^1.4.84", "escalade": "^3.1.1", "node-releases": "^2.0.2", "picocolors": "^1.0.0" @@ -919,9 +919,9 @@ } }, "caniuse-lite": { - "version": "1.0.30001312", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001312.tgz", - "integrity": "sha512-Wiz1Psk2MEK0pX3rUzWaunLTZzqS2JYZFzNKqAiJGiuxIjRPLgV6+VDPOg6lQOUxmDwhTlh198JsTTi8Hzw6aQ==", + "version": "1.0.30001319", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001319.tgz", + "integrity": "sha512-xjlIAFHucBRSMUo1kb5D4LYgcN1M45qdKP++lhqowDpwJwGkpIRTt5qQqnhxjj1vHcI7nrJxWhCC1ATrCEBTcw==", "dev": true }, "chalk": { @@ -1067,9 +1067,9 @@ "dev": true }, "debug": { - "version": "4.3.3", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.3.tgz", - "integrity": "sha512-/zxw5+vh1Tfv+4Qn7a5nsbcJKPaSvCDhojn6FEl9vupwK2VCSDtEiEtqr8DFtzYFOdz63LBkxec7DYuc2jon6Q==", + "version": "4.3.4", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", + "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", "dev": true, "requires": { "ms": "2.1.2" @@ -1213,9 +1213,9 @@ "dev": true }, "electron-to-chromium": { - "version": "1.4.75", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.75.tgz", - "integrity": "sha512-LxgUNeu3BVU7sXaKjUDD9xivocQLxFtq6wgERrutdY/yIOps3ODOZExK1jg8DTEg4U8TUCb5MLGeWFOYuxjF3Q==", + "version": "1.4.88", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.88.tgz", + "integrity": "sha512-oA7mzccefkvTNi9u7DXmT0LqvhnOiN2BhSrKerta7HeUC1cLoIwtbf2wL+Ah2ozh5KQd3/1njrGrwDBXx6d14Q==", "dev": true }, "emoji-regex": { @@ -2022,9 +2022,9 @@ } }, "globals": { - "version": "13.12.1", - "resolved": "https://registry.npmjs.org/globals/-/globals-13.12.1.tgz", - "integrity": "sha512-317dFlgY2pdJZ9rspXDks7073GpDmXdfbM3vYYp0HAMKGDh1FfWPleI2ljVNLQX5M5lXcAslTcPTrOrMEFOjyw==", + "version": "13.13.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-13.13.0.tgz", + "integrity": "sha512-EQ7Q18AJlPwp3vUDL4mKA0KXrXyNIQyWon6T6XQiBQF0XHvRsiCSrWmmeATpUzdJN2HhWZU6Pdl0a9zdep5p6A==", "dev": true, "requires": { "type-fest": "^0.20.2" @@ -3667,9 +3667,9 @@ } }, "tsconfig-paths": { - "version": "3.12.0", - "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.12.0.tgz", - "integrity": "sha512-e5adrnOYT6zqVnWqZu7i/BQ3BnhzvGbjEjejFXO20lKIKpwTaupkCPgEfv4GZK1IBciJUEhYs3J3p75FdaTFVg==", + "version": "3.14.0", + "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.14.0.tgz", + "integrity": "sha512-cg/1jAZoL57R39+wiw4u/SCC6Ic9Q5NqjBOb+9xISedOYurfog9ZNmKJSxAnb2m/5Bq4lE9lhUcau33Ml8DM0g==", "dev": true, "requires": { "@types/json5": "^0.0.29", diff --git a/package.json b/package.json index 22242c4..83e18a7 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/classes/DataAdapterBase.ts b/src/classes/DataAdapterBase.ts index d111603..16cc617 100644 --- a/src/classes/DataAdapterBase.ts +++ b/src/classes/DataAdapterBase.ts @@ -45,12 +45,81 @@ export abstract class DataAdapterBase implements IDataAdapter { /** * @inheritdoc */ - public abstract find(type: Constructor, options?: IFindOptions | null): Promise; + public abstract find(type: Constructor, options?: Nilable): Promise; /** * @inheritdoc */ - public abstract findOne(type: Constructor, options?: IFindOneOptions | null): Promise; + public abstract findOne(type: Constructor, options?: Nilable): Promise; + + private getEntityByType(type: Constructor) { + for (const [name, config] of Object.entries(this.context.entities)) { + if (config.type === type) { + return { + config, + name + }; + } + } + + return null; + } + + private getEntityByTypeOrThrow(type: Constructor) { + 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} type The type. + * + * @returns {string[]} The list of field names. + */ + public getEntityIdsByType(type: Constructor): 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} type The type. + * + * @returns {string[]} The list of ID fields. + */ + public getEntityIdsByTypeOrThrow(type: Constructor): 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} type The type. + * + * @returns {string} The name of the underlying entity/table name. + */ + public getEntityNameByTypeOrThrow(type: Constructor): string { + return this.getEntityByTypeOrThrow(type).name; + } /** * @inheritdoc diff --git a/src/classes/DataContext.ts b/src/classes/DataContext.ts index 5ef9f6f..b612721 100644 --- a/src/classes/DataContext.ts +++ b/src/classes/DataContext.ts @@ -16,7 +16,7 @@ // along with this program. If not, see . 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 { /** @@ -37,11 +37,11 @@ export class DataContext implements IDataContext { return this.options.entities; } - public find(type: Constructor, options?: IFindOptions | null) { + public find(type: Constructor, options?: Nilable) { return this.options.adapter.find(type, options); } - public findOne(type: Constructor, options?: IFindOneOptions | null) { + public findOne(type: Constructor, options?: Nilable) { return this.options.adapter.findOne(type, options); } diff --git a/src/transformers/dateTransformer.ts b/src/transformers/dateTransformer.ts new file mode 100644 index 0000000..02a7041 --- /dev/null +++ b/src/transformers/dateTransformer.ts @@ -0,0 +1,52 @@ +// This file is part of the @egomobile/orm distribution. +// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/) +// +// @egomobile/orm is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation, version 3. +// +// @egomobile/orm is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . + +import { NULL } from '../constants'; +import type { IEntityFieldTransformer } from '../types'; +import { isNil } from '../utils/internal'; + +/** + * Converts from and to a Date value. + */ +export const dateTransformer: IEntityFieldTransformer = { + /** + * @inheritdoc + */ + from: (value) => valueToDate(value, NULL), + + /** + * @inheritdoc + */ + to: (value) => valueToDate(value, value) +}; + +function valueToDate(value: unknown, ifNilValue: any): any { + if (value instanceof Date) { + return value; + } else if (isNil(value)) { + return ifNilValue; + } else { + if (typeof value === 'number') { + return new Date(value); + } else { + const str = String(value).trim(); + if (str.length) { + return new Date(parseInt(str, 10)); + } else { + return ifNilValue; + } + } + } +} diff --git a/src/transformers/floatTransformer.ts b/src/transformers/floatTransformer.ts new file mode 100644 index 0000000..939a91f --- /dev/null +++ b/src/transformers/floatTransformer.ts @@ -0,0 +1,48 @@ +// This file is part of the @egomobile/orm distribution. +// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/) +// +// @egomobile/orm is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation, version 3. +// +// @egomobile/orm is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . + +import { NULL } from '../constants'; +import type { IEntityFieldTransformer } from '../types'; +import { isNil } from '../utils/internal'; + +/** + * Converts from and to a float value. + */ +export const floatTransformer: IEntityFieldTransformer = { + /** + * @inheritdoc + */ + from: (value) => valueToFloat(value, NULL), + + /** + * @inheritdoc + */ + to: (value) => valueToFloat(value, value) +}; + +function valueToFloat(value: unknown, ifNilValue: any): any { + if (typeof value === 'number') { + return value; + } else if (isNil(value)) { + return ifNilValue; + } else { + const str = String(value).trim(); + if (str.length) { + return parseFloat(str); + } else { + return ifNilValue; + } + } +} \ No newline at end of file diff --git a/src/transformers/index.ts b/src/transformers/index.ts new file mode 100644 index 0000000..387aaf7 --- /dev/null +++ b/src/transformers/index.ts @@ -0,0 +1,19 @@ +// This file is part of the @egomobile/orm distribution. +// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/) +// +// @egomobile/orm is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation, version 3. +// +// @egomobile/orm is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . + +export * from './dateTransformer'; +export * from './floatTransformer'; +export * from './integerTransformer'; +export * from './stringTransformer'; diff --git a/src/transformers/integerTransformer.ts b/src/transformers/integerTransformer.ts new file mode 100644 index 0000000..2f9ac82 --- /dev/null +++ b/src/transformers/integerTransformer.ts @@ -0,0 +1,48 @@ +// This file is part of the @egomobile/orm distribution. +// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/) +// +// @egomobile/orm is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation, version 3. +// +// @egomobile/orm is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . + +import { NULL } from '../constants'; +import type { IEntityFieldTransformer } from '../types'; +import { isNil } from '../utils/internal'; + +/** + * Converts from and to an integer value. + */ +export const integerTransformer: IEntityFieldTransformer = { + /** + * @inheritdoc + */ + from: (value) => valueToInt(value, NULL), + + /** + * @inheritdoc + */ + to: (value) => valueToInt(value, value) +}; + +function valueToInt(value: unknown, ifNilValue: any): any { + if (Number.isSafeInteger(value)) { + return value; + } else if (isNil(value)) { + return ifNilValue; + } else { + const str = String(value).trim(); + if (str.length) { + return parseInt(str, 10); + } else { + return ifNilValue; + } + } +} diff --git a/src/transformers/stringTransformer.ts b/src/transformers/stringTransformer.ts new file mode 100644 index 0000000..3f9859e --- /dev/null +++ b/src/transformers/stringTransformer.ts @@ -0,0 +1,43 @@ +// This file is part of the @egomobile/orm distribution. +// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/) +// +// @egomobile/orm is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation, version 3. +// +// @egomobile/orm is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . + +import { NULL } from '../constants'; +import type { IEntityFieldTransformer } from '../types'; +import { isNil } from '../utils/internal'; + +/** + * Converts from and to a string value. + */ +export const stringTransformer: IEntityFieldTransformer = { + /** + * @inheritdoc + */ + from: (value) => valueToString(value, NULL), + + /** + * @inheritdoc + */ + to: (value) => valueToString(value, value) +}; + +function valueToString(value: unknown, ifNilValue: any): any { + if (typeof value === 'string') { + return value; + } else if (isNil(value)) { + return ifNilValue; + } else { + return String(value); + } +} diff --git a/src/types/index.ts b/src/types/index.ts index 3b7fff1..d774f0f 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -16,6 +16,15 @@ import type { NULL } from '../constants'; import type { Constructor, List, Nilable } from './internal'; +/** + * An action, which transforms data. + * + * @param {any} value The input value. + * + * @returns {any} The output value. + */ +export type DataTransformer = (value: any) => any; + /** * Object with entity configurations. */ @@ -79,11 +88,11 @@ export interface IDataRepository { * ``` * * @param {Constructor} type The class / type. - * @param {IFindOptions|null} [options] The custom options. + * @param {Nilable} [options] The custom options. * * @returns {Promise} The promise with the items. */ - find(type: Constructor, options?: IFindOptions | null): Promise; + find(type: Constructor, options?: Nilable): Promise; /** * Tries to find a simple item. @@ -119,11 +128,11 @@ export interface IDataRepository { * ``` * * @param {Constructor} type The class / type. - * @param {Nullable} [options] The custom options. + * @param {Nilable} [options] The custom options. * * @returns {Promise} The promise with the item or (null) if not found. */ - findOne(type: Constructor, options?: IFindOneOptions | null): Promise; + findOne(type: Constructor, options?: Nilable): Promise; /** * Insert one or more entities. @@ -245,16 +254,47 @@ export interface IDataRepository { * A configuration for an entity. */ export interface IEntityConfig { + /** + * The custom field configurations. + */ + fields?: Nilable>; /** * List of columns / fields which representthe ID. */ - ids?: string[]; + ids?: Nilable; /** * The class / type to use to create instances for an entity. */ type: Constructor; } +/** + * A configuration for an entity field. + */ +export interface IEntityFieldConfig { + /** + * The custom and optional data transformer. + */ + transformer?: Nilable; +} + +/** + * An object, which transforms the data of a field. + */ +export interface IEntityFieldTransformer { + /** + * The optional action to invoke, when + * data comes from database to entity. + */ + from?: Nilable; + /** + * The optional action to invoke, when + * data of an entity field is written + * to database. + */ + to?: Nilable; +} + /** * Options for finding one single entity. */ @@ -270,7 +310,7 @@ export interface IFindOneOptions { /** * An object that represents the parameters for the 'where' part. */ - params?: any; + params?: Nilable; /** * An object, which is used to sort the result. */ diff --git a/src/utils/internal.ts b/src/utils/internal.ts new file mode 100644 index 0000000..42ba8de --- /dev/null +++ b/src/utils/internal.ts @@ -0,0 +1,18 @@ +// This file is part of the @egomobile/orm distribution. +// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/) +// +// @egomobile/orm is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation, version 3. +// +// @egomobile/orm is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . + +export function isNil(val: unknown): val is (null | undefined) { + return typeof val === 'undefined' || val === null; +}