Skip to content

Commit

Permalink
Can't save a ES6 Map or Set variable as a string or blob type in the …
Browse files Browse the repository at this point in the history
…column
  • Loading branch information
fuyifan committed Jun 21, 2018
1 parent d4e9c3e commit b71fafd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
31 changes: 31 additions & 0 deletions test/functional/columns/value-transformer/entity/PhoneBook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {Entity} from "../../../../../src/decorator/entity/Entity";
import {Column} from "../../../../../src/decorator/columns/Column";
import {ValueTransformer} from "../../../../../src/decorator/options/ValueTransformer";
import {PrimaryGeneratedColumn} from "../../../../../src/decorator/columns/PrimaryGeneratedColumn";


class PhonesTransformer implements ValueTransformer {

to (value: Map<string, number>): string {
return JSON.stringify([...value]);
}

from (value: string): Map<string, number> {
return new Map(JSON.parse(value));
}

}

@Entity()
export class PhoneBook {

@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;

@Column({ type: String, transformer: new PhonesTransformer() })
phones: Map<string, number>;

}
21 changes: 19 additions & 2 deletions test/functional/columns/value-transformer/value-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import {expect} from "chai";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils";
import {Connection} from "../../../../src/connection/Connection";
import {Post} from "./entity/Post";
import { PhoneBook } from "./entity/PhoneBook";

describe("columns > value-transformer functionality", () => {

let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [Post],
entities: [Post, PhoneBook],
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should marshal data using the provided value-transformer", () => Promise.all(connections.map(async connection => {
it.only("should marshal data using the provided value-transformer", () => Promise.all(connections.map(async connection => {

const postRepository = connection.getRepository(Post);

Expand All @@ -33,6 +34,22 @@ describe("columns > value-transformer functionality", () => {
expect(loadedPost!.title).to.be.equal("About columns1");
expect(loadedPost!.tags).to.deep.eq(["very", "simple"]);


const phoneBookRepository = connection.getRepository(PhoneBook);
const phoneBook = new PhoneBook();
phoneBook.name = "George";
phoneBook.phones = new Map();
phoneBook.phones.set("work", 123456);
phoneBook.phones.set("mobile", 1234567);
await phoneBookRepository.save(phoneBook);

const loadedPhoneBook = await phoneBookRepository.findOne(1);
expect(loadedPhoneBook!.name).to.be.equal("George");
expect(loadedPhoneBook!.phones).not.to.be.undefined;
expect(loadedPhoneBook!.phones.get("work")).to.equal(123456);
expect(loadedPhoneBook!.phones.get("mobile")).to.equal(1234567);


})));


Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"stripInternal": true,
"pretty": true,
"strictNullChecks": true,
"noUnusedLocals": true
"noUnusedLocals": true,
"downlevelIteration": true
},
"exclude": [
"tmp",
Expand Down

0 comments on commit b71fafd

Please sign in to comment.