Skip to content

Commit

Permalink
fix(sqlite): fix mapping of joined results with DateTimeType proper…
Browse files Browse the repository at this point in the history
…ties

Closes #5550
  • Loading branch information
B4nan committed May 14, 2024
1 parent 4783945 commit 4001d2b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/knex/src/AbstractSqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection
relationPojo[prop.name] = pk.every(val => val != null) ? pk as EntityValue<T> : null;
} else if (prop.runtimeType === 'Date') {
const alias = `${relationAlias}__${prop.fieldNames[0]}` as EntityKey<T>;
relationPojo[prop.name] = (typeof root![alias] === 'string' ? new Date(root![alias] as string) : root![alias]) as EntityValue<T>;
const type = typeof root![alias];
relationPojo[prop.name] = (['string', 'number'].includes(type) ? this.platform.parseDate(root![alias] as string) : root![alias]) as EntityValue<T>;
} else {
const alias = `${relationAlias}__${prop.fieldNames[0]}` as EntityKey<T>;
relationPojo[prop.name] = root![alias];
Expand Down
105 changes: 105 additions & 0 deletions tests/features/custom-types/GH5550.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { Collection, Entity, ManyToOne, MikroORM, OneToMany, PrimaryKey, Property, t } from '@mikro-orm/sqlite';

@Entity()
class User {

@PrimaryKey({ type: t.integer })
id!: number;

@Property({ type: t.string })
fullName!: string;

@Property({ type: t.datetime })
tdatetime: Date;

@Property({ type: 'datetime' })
qdatetime: Date;

@Property({ type: 'Date' })
qDate: Date;

@Property({ type: Date })
Date: Date;

@OneToMany(() => Article, (article: Article) => article.author)
articles = new Collection<Article>(this);

constructor(fullName: string, date: Date) {
this.fullName = fullName;
this.tdatetime = date;
this.qdatetime = date;
this.qDate = date;
this.Date = date;
}

}

@Entity()
class Article {

@PrimaryKey({ type: t.integer })
id!: number;

@Property({ type: t.string })
title: string;

@Property({ type: t.datetime })
tdatetime: Date;

@Property({ type: 'datetime' })
qdatetime: Date;

@Property({ type: 'Date' })
qDate: Date;

@Property({ type: Date })
Date: Date;

@ManyToOne(() => User)
author: User;

constructor(author: User, title: string, date: Date) {
this.author = author;
this.title = title;
this.tdatetime = date;
this.qdatetime = date;
this.qDate = date;
this.Date = date;
}

}

let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [User],
dbName: `:memory:`,
});
await orm.schema.refreshDatabase();
});

afterAll(() => orm.close(true));

test('5550', async () => {
const user = new User('Foo Bar', new Date());
user.articles.add(new Article(user, 'hello world', new Date()));
await orm.em.persist(user).flush();
const dbUser = await orm.em.fork().findOne(User, { id: 1 }, { populate: ['articles'] });
expect(dbUser).toMatchObject({
tdatetime: expect.any(Date),
qdatetime: expect.any(Date),
qDate: expect.any(Date),
Date: expect.any(Date),
id: 1,
});
expect(dbUser?.articles[0]).toMatchObject({
title: 'hello world',
tdatetime: expect.any(Date),
qdatetime: expect.any(Date),
qDate: expect.any(Date),
Date: expect.any(Date),
author: expect.any(User),
id: 1,
});
});

0 comments on commit 4001d2b

Please sign in to comment.