Comments (11)
Hey, it is not crucial :)
I don't really like moment due to the way it is written, but decided to go with it since it seemed most used/popular. Maybe it is time to reconsider :)
Thanks anyway, I am closing the issue (and maybe it would be worth noting that moment is not supported)
from mapper.
@krukru plainToClass
from class-transformer
initializes a new Moment
object under the hood. That's a bummer. I don't think Moment
is working with this library as of the moment.
from mapper.
Hi, thanks for reporting the issue.
I thought about Moment object but ended up not supporting it since it would mean I would need to support another 3rd party library and there are many different choices regarding Date now. If I could suggest, try: date-fns, dayjs or luxon.
However, if Moment is crucial for your project, I’ll see what I can do.
from mapper.
@krukru Though it is the most popular one, it’s a heavy library.
- Luxon: is written using Moment’s concept but is redesigned to be lighter and less-clunky than Moment, written by one of the old Moment’s maintainer
- date-fns: like Lodash for Date
- dayjs: extremely lightweight with plugin to enable various features.
I’ve used date-fns and dayjs before and they’re great. Working with native JS Date object is awesome with those libs. I would definitely reconsider and will write a small section regarding DateTime on the Readme. Thanks again
from mapper.
@krukru I found a workaround for Moment
object. Read the Date Time section on the README to learn about it. Though, I would still suggest going with vanilla JS Date object if you can.
from mapper.
Hey, thanks for updating the docs, I almost got this to work after updating to 3.1.2 :)
Here is what I am working with
import "reflect-metadata";
import { AutoMap, Mapper } from "@nartc/automapper";
import { Moment } from "moment";
const moment = require("moment");
class Foo {
@AutoMap(() => moment)
public get foo(): Moment {
return this._foo;
}
public set foo(value: Moment) {
this._foo = value;
}
private _foo!: Moment;
}
class FooDTO {
@AutoMap()
public foo!: number; // the unix timestamp
}
Mapper.initialize(cfg => {
cfg.createMap(FooDTO, Foo).forMember(
(dest: Foo) => dest.foo,
(opts: any) => opts.mapFrom((source) => moment(source.foo)),
).reverseMap().forPath(
(dest: FooDTO) => dest.foo,
(opts: any) => opts.mapFrom((source) => source.foo.unix()))
});
const fooDomainObjectMapped = Mapper.map({ foo: 12345 * 1000}, Foo, FooDTO);
expect(fooDomainObjectMapped.foo.unix()).equals(12345); // this passes as true!
const fooDomainObject = new Foo();
fooDomainObject.foo = moment(54321 * 1000);
const fooDtoObjectMapped = Mapper.map(fooDomainObject, FooDTO);
expect(fooDtoObjectMapped.foo).equals(54321); // this fails, fooDtoObjectMapped.foo is equal to the current system timestamp
So mapping from Dto(Vm) -> Domain model works
But mapping Domain model -> Dto fails and always creates a moment object with the current timestamp. Now that's really odd, isn't it! :)
from mapper.
What's maybe more interesting is how this snippet works on 3.1.0
const fooDomainObjectMapped = Mapper.map({ foo: 12345 * 1000}, Foo, FooDTO);
expect(fooDomainObjectMapped.foo.unix()).equals(12345); // this fails, fooDomainObjectMapped.foo.unix() is equal to the current system timestamp
const fooDtoObjectMapped = Mapper.map(fooDomainObject, FooDTO);
expect(fooDtoObjectMapped.foo).equals(54321); // this throws a runtime error: TypeError: Cannot read property '_isAMomentObject' of undefined
I am trying to deduce from the source and commits between versions what has changed, but it is a bit over my head. Hope this piece of info helps!
from mapper.
hmmm I'll give that a quick run on my test bed.
from mapper.
@krukru Another point is I run plainToClass
against the source
(that was passed in Mapper.map(source, DestinationModel)
) every time. I just modified the code to where plainToClass
is only ran if source
isn't a true instance of mapping.source
(aka if source
is a plain object). If source
is a true instance of mapping.source
, then it seems like moment
is working.
Let me know (if you're still interested) if the latest version works out for you.
from mapper.
I don't know what you did, but in 3.1.2 this now works in both directions!
Thanks very much for all the support, you are awesome :)
from mapper.
@krukru I simply changed the way Automapper interacts with class-transformer
a bit. Thanks for reporting.
from mapper.
Related Issues (20)
- @nestjs/swagger HOT 3
- Maximum Call Stack Size error after migrating to v8 HOT 3
- Explicitly pass type to array in AutoMap decorator when a class gets extended using MapperOmitType
- Uncaught TypeError: classes_1.classes is not a function HOT 2
- Nestjs v10 dependency issues HOT 11
- NestJS Error Debug Failure. False expression: Too many nodes written to output HOT 4
- Error: Mapping is not found for class BaseEntity and class BaseVo HOT 1
- The maintenance of this project is dead, needs official fork HOT 2
- Easy Map for properties with the same name. HOT 2
- Attributes that share common prefixes get skipped
- Doesn't work with Typescript 4.9 or newer with moduleResolution:"node16" HOT 3
- No "exports" main defined in node_modules/@automapper/classes/package.json HOT 4
- property is undefined after mapping HOT 2
- Properties with name in camel case renders as undefined HOT 2
- AutoMap doesn't work with union types HOT 1
- Transformer Plugin in swc HOT 1
- Dependency conflict with reflect-metadata when install with NestJS HOT 4
- Cannot use string or symbol as ModelIdentifier
- Unexpected typescript error "Cannot assign an abstract constructor type to a non-abstract constructor type."
- `console.[fn].bind` instead of `console.[fn].call` in logger
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mapper.