Skip to content

Commit

Permalink
wip: finish mikro
Browse files Browse the repository at this point in the history
  • Loading branch information
Chau Tran authored and Chau Tran committed Mar 21, 2022
1 parent 2b96f21 commit edba750
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This is a monorepo of `@automapper/core` and official packages.
| **mapped-types** | [`@automapper/classes/mapped-types`](https://npmjs.com/package/@automapper/classes) | - | [![README](https://img.shields.io/badge/README--green.svg)](/packages/classes/mapped-types/src/README.md) |
| **transformer-plugin** | [`@automapper/classes/transformer-plugin`](https://npmjs.com/package/@automapper/classes) | - | [![README](https://img.shields.io/badge/README--green.svg)](/packages/classes/transformer-plugin/src/README.md) |
| **pojos** | [`@automapper/pojos`](https://npmjs.com/package/@automapper/pojos) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/pojos) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/pojos/src/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/pojos) ![NPM](https://img.shields.io/npm/l/@automapper/pojos) |
| **mikro** | [`@automapper/pojos`](https://npmjs.com/package/@automapper/mikro) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/mikro) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/mikro/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/mikro) ![NPM](https://img.shields.io/npm/l/@automapper/mikro) |
| **mikro** | [`@automapper/pojos`](https://npmjs.com/package/@automapper/mikro) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/mikro) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/mikro/src/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/mikro) ![NPM](https://img.shields.io/npm/l/@automapper/mikro) |
| **sequelize** | [`@automapper/sequelize`](https://npmjs.com/package/@automapper/sequelize) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/sequelize) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/sequelize/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/sequelize) ![NPM](https://img.shields.io/npm/l/@automapper/sequelize) |
| **nestjs** | [`@automapper/nestjs`](https://npmjs.com/package/@automapper/nestjs) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/nestjs) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/nestjs/src/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/nestjs) ![NPM](https://img.shields.io/npm/l/@automapper/nestjs) |

Expand Down
48 changes: 48 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
"release": "dotenv release-it --",
"release:beta": "dotenv release-it --major --preRelease=beta",
"build": "nx run-many --target=build --projects=core,classes,classes-mapped-types,classes-transformer-plugin,pojos,nestjs",
"package": "nx run-many --target=package --projects=core,classes,classes-mapped-types,classes-transformer-plugin,pojos,nestjs --parallel=false",
"test": "nx run-many --all --target=test --parallel",
"package": "nx package-all core",
"publish": "nx run-many --target=publish --all --parallel"
},
"private": true,
"dependencies": {
"tslib": "~2.3.1"
},
"devDependencies": {
"@mikro-orm/core": "5.1.1",
"@nestjs/common": "~8.4.1",
"@nestjs/core": "~8.4.1",
"@nestjs/platform-express": "~8.4.1",
Expand Down
29 changes: 20 additions & 9 deletions packages/classes/src/lib/classes.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import type {
ApplyMetadata,
ApplyMetadataFn,
Constructor,
DestinationConstructor,
Mapper,
MappingStrategyInitializer,
MappingStrategyInitializerOptions,
} from '@automapper/core';
import { defaultApplyMetadata } from '@automapper/core';
import 'reflect-metadata';
import { getStandaloneConstructors } from './decorators';
import { getMetadataList } from './get-metadata-list';

export function classes(
destinationConstructor: DestinationConstructor = (
_,
destinationIdentifier
) => new (destinationIdentifier as Constructor)(),
applyMetadata: ApplyMetadata = defaultApplyMetadata
): MappingStrategyInitializer<Constructor> {
export function classes({
destinationConstructor = (_, destinationIdentifier) =>
new (destinationIdentifier as Constructor)(),
applyMetadata = defaultApplyMetadata,
postMap,
preMap,
}: MappingStrategyInitializerOptions = {}): MappingStrategyInitializer<Constructor> {
return (mapper: Mapper) => ({
destinationConstructor,
mapper,
Expand Down Expand Up @@ -49,5 +48,17 @@ export function classes(

return metadataMap;
},
preMap: (...args) => {
if (preMap) {
return preMap(...args);
}
return;
},
postMap: (...args) => {
if (postMap) {
return postMap(...args);
}
return;
},
});
}
4 changes: 1 addition & 3 deletions packages/core/babel.config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
{
"presets": ["minify"]
}
{}
15 changes: 15 additions & 0 deletions packages/core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@
"format": ["cjs", "esm"]
}
},
"package-all": {
"executor": "@nrwl/workspace:run-commands",
"options": {
"commands": [
"nx package core",
"nx package classes",
"nx package classes-mapped-types",
"nx package classes-transformer-plugin",
"nx package pojos",
"nx package nestjs",
"nx package mikro"
],
"parallel": false
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,30 @@ export interface MappingStrategy<TIdentifier extends MetadataIdentifier> {
retrieveMetadata(
...identifiers: TIdentifier[]
): Map<TIdentifier, MetadataList>;
preMap?<TSource extends Dictionary<TSource>>(
source: TSource
): TSource | undefined;
postMap?<
TSource extends Dictionary<TSource>,
TDestination extends Dictionary<TDestination>
>(
source: TSource,
destination: TDestination
): TDestination | undefined;
}

export type MappingStrategyInitializer<TIdentifier extends MetadataIdentifier> =
(mapper: Mapper) => MappingStrategy<TIdentifier>;

export interface MappingStrategyInitializerOptions {
destinationConstructor?: DestinationConstructor;
applyMetadata?: ApplyMetadata;
preMap?<TSource extends Dictionary<TSource>>(source: TSource): TSource;
postMap?<
TSource extends Dictionary<TSource>,
TDestination extends Dictionary<TDestination>
>(
source: TSource,
destination: TDestination
): TDestination;
}
1 change: 1 addition & 0 deletions packages/mikro/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 4 additions & 0 deletions packages/mikro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"import": "./index.esm.js",
"require": "./index.cjs.js"
},
"peerDependencies": {
"@mikro-orm/core": "^5.0.0",
"reflect-metadata": "~0.1.13"
},
"sideEffects": false,
"engines": {
"node": ">=14.0.0"
Expand Down
14 changes: 13 additions & 1 deletion packages/mikro/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
"tsConfig": "packages/mikro/tsconfig.lib.json",
"assets": ["packages/mikro/src/README.md"],
"compiler": "babel",
"external": ["typescript", "tslib", "@automapper/core"],
"external": [
"typescript",
"tslib",
"@automapper/core",
"@automapper/classes"
],
"format": ["cjs", "esm"]
}
},
Expand All @@ -37,6 +42,13 @@
"jestConfig": "packages/mikro/jest.config.js",
"passWithNoTests": true
}
},
"publish": {
"executor": "@nrwl/workspace:run-commands",
"options": {
"command": "npm publish --tag beta",
"cwd": "dist/packages/mikro"
}
}
},
"tags": ["mikro"]
Expand Down
File renamed without changes.
7 changes: 0 additions & 7 deletions packages/mikro/src/lib/mikro.spec.ts

This file was deleted.

21 changes: 19 additions & 2 deletions packages/mikro/src/lib/mikro.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
export function mikro(): string {
return 'mikro';
import { classes } from '@automapper/classes';
import type {
Constructor,
Dictionary,
MappingStrategyInitializer,
MappingStrategyInitializerOptions,
} from '@automapper/core';
import { defaultApplyMetadata } from '@automapper/core';
import { serializeEntity } from './serialize-entity';

export function mikro({
destinationConstructor = (_, destinationIdentifier) =>
new (destinationIdentifier as Constructor)(),
applyMetadata = defaultApplyMetadata,
postMap,
preMap = <TSource extends Dictionary<TSource>>(source: TSource) =>
serializeEntity(source) as TSource,
}: MappingStrategyInitializerOptions = {}): MappingStrategyInitializer<Constructor> {
return classes({ destinationConstructor, applyMetadata, preMap, postMap });
}
35 changes: 35 additions & 0 deletions packages/mikro/src/lib/serialize-entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { AnyEntity } from '@mikro-orm/core';
import { Utils } from '@mikro-orm/core';

const excluded = [
'__gettersDefined',
'__entity',
'__meta',
'__platform',
'__helper',
'__factory',
];

export function serializeEntity(item: AnyEntity) {
if (!Utils.isEntity(item)) return item;

const result = {} as Record<string | symbol, unknown>;
for (const key of Reflect.ownKeys(item)) {
if (typeof key === 'symbol' || excluded.includes(key)) {
continue;
}

const value = item[key as string];
if (Utils.isCollection(value)) {
result[key] = value.getSnapshot() || [];
} else {
result[key] = serializeEntity(value);
}
}

if (result['id'] == null && item['id'] != null) {
result['id'] = item['id'];
}

return result;
}
2 changes: 1 addition & 1 deletion packages/mikro/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"module": "ESNext",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
Expand Down
25 changes: 19 additions & 6 deletions packages/pojos/src/lib/pojos.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import type {
ApplyMetadata,
ApplyMetadataFn,
DestinationConstructor,
MappingStrategyInitializer,
MappingStrategyInitializerOptions,
MetadataList,
} from '@automapper/core';
import { defaultApplyMetadata } from '@automapper/core';
import { PojosMetadataMap } from './metadata-map';

export function pojos(
destinationConstructor: DestinationConstructor = () => ({}),
applyMetadata: ApplyMetadata = defaultApplyMetadata
): MappingStrategyInitializer<symbol> {
export function pojos({
destinationConstructor = () => ({}),
applyMetadata = defaultApplyMetadata,
postMap,
preMap,
}: MappingStrategyInitializerOptions = {}): MappingStrategyInitializer<symbol> {
return (mapper) => ({
destinationConstructor,
mapper,
Expand All @@ -31,5 +32,17 @@ export function pojos(

return metadataMap;
},
preMap: (...args) => {
if (preMap) {
return preMap(...args);
}
return;
},
postMap: (...args) => {
if (postMap) {
return postMap(...args);
}
return;
},
});
}

0 comments on commit edba750

Please sign in to comment.