Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle URL objects as column field values #5771

Merged
merged 3 commits into from Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/util/OrmUtils.ts
@@ -1,4 +1,5 @@
import { ObjectLiteral } from "../common/ObjectLiteral";
import { URL } from "url";

export class OrmUtils {

Expand Down Expand Up @@ -82,7 +83,8 @@ export class OrmUtils {
&& !(value instanceof Set)
&& !(value instanceof Date)
&& !(value instanceof Buffer)
&& !(value instanceof RegExp)) {
&& !(value instanceof RegExp)
&& !(value instanceof URL)) {
if (!target[key])
Object.assign(target, { [key]: Object.create(Object.getPrototypeOf(value)) });
this.mergeDeep(target[key], value);
Expand Down Expand Up @@ -192,7 +194,8 @@ export class OrmUtils {
(x instanceof Date && y instanceof Date) ||
(x instanceof RegExp && y instanceof RegExp) ||
(x instanceof String && y instanceof String) ||
(x instanceof Number && y instanceof Number))
(x instanceof Number && y instanceof Number) ||
(x instanceof URL && y instanceof URL))
return x.toString() === y.toString();

// At last checking prototypes as good as we can
Expand Down
24 changes: 24 additions & 0 deletions test/github-issues/5762/entity/User.ts
@@ -0,0 +1,24 @@
import {Entity} from "../../../../src/decorator/entity/Entity";
import {PrimaryColumn, Column} from "../../../../src";
import { URL } from "url";

@Entity()
export class User {

@PrimaryColumn()
id: number;

@Column("varchar", {
// marshall
transformer: {
from(value: string): URL {
return new URL(value);
},
to(value: URL): string {
return value.toString();
},
},
})
url: URL;

}
39 changes: 39 additions & 0 deletions test/github-issues/5762/issue-5762.ts
@@ -0,0 +1,39 @@
import "reflect-metadata";
import {expect} from "chai";
import {Connection} from "../../../src";
import {User} from "./entity/User";
import {createTestingConnections, reloadTestingDatabases, closeTestingConnections} from "../../utils/test-utils";
import { URL } from "url";

describe("github issues > #5762 `Using URL as a rich column type breaks", () => {

let connections: Connection[];

before(async () => {
connections = await createTestingConnections({
entities: [User],
schemaCreate: true,
dropSchema: true
});
});
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should allow assigning URL as a field value", () =>
Promise.all(connections.map(async (connection) => {
const userRepository = connection.getRepository(User);

const url = new URL("https://typeorm.io");

const user = new User();
user.id = 1;
user.url = url;

const promise = userRepository.save(user);

return expect(promise).to.eventually.be.deep.equal(user)
.and.to.have.property("url").be.instanceOf(URL)
.and.to.have.nested.property("href").equal(url.href);
})));

});