-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chau Tran
authored and
Chau Tran
committed
Mar 21, 2022
1 parent
2b96f21
commit edba750
Showing
16 changed files
with
202 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
{ | ||
"presets": ["minify"] | ||
} | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters