From edf27d97a30d1aaf28e5c7a7aab96d16152e4983 Mon Sep 17 00:00:00 2001 From: Umed Khudoiberdiev Date: Tue, 22 Mar 2022 23:04:41 +0500 Subject: [PATCH] revert: json/jsonb change introduced in 0.3.1 (#8777) --- src/driver/postgres/PostgresDriver.ts | 2 +- .../bulk-save-case2/entity/Document.ts | 2 +- .../array-contained-by-operator.test.ts | 89 ------------------- .../entity/Post.ts | 6 -- .../array-contains-operator.test.ts | 77 ---------------- .../array-contains-operator/entity/Post.ts | 6 -- .../array-overlap-operator.test.ts | 83 ----------------- .../find-options/array-overlap/entity/Post.ts | 6 -- .../basic-jsonb.test.ts} | 12 +-- .../json/{ => basic-jsonb}/entity/Record.ts | 5 +- .../json/jsonb-defaults/entity/Post.ts | 32 +++++++ .../jsonb-defaults/jsonb-defaults.test.ts | 70 +++++++++++++++ test/github-issues/204/entity/Record.ts | 4 +- test/github-issues/3496/entity/Post.ts | 2 +- 14 files changed, 114 insertions(+), 282 deletions(-) rename test/functional/json/{jsonb.ts => basic-jsonb/basic-jsonb.test.ts} (95%) rename test/functional/json/{ => basic-jsonb}/entity/Record.ts (86%) create mode 100644 test/functional/json/jsonb-defaults/entity/Post.ts create mode 100644 test/functional/json/jsonb-defaults/jsonb-defaults.test.ts diff --git a/src/driver/postgres/PostgresDriver.ts b/src/driver/postgres/PostgresDriver.ts index 7c4e9e2ef6..d63170836a 100644 --- a/src/driver/postgres/PostgresDriver.ts +++ b/src/driver/postgres/PostgresDriver.ts @@ -622,7 +622,7 @@ export class PostgresDriver implements Driver { columnMetadata.type, ) >= 0 ) { - return value // JSON.stringify() + return JSON.stringify(value) } else if (columnMetadata.type === "hstore") { if (typeof value === "string") { return value diff --git a/test/benchmark/bulk-save-case2/entity/Document.ts b/test/benchmark/bulk-save-case2/entity/Document.ts index e7dc1b5ac5..08bcc66183 100644 --- a/test/benchmark/bulk-save-case2/entity/Document.ts +++ b/test/benchmark/bulk-save-case2/entity/Document.ts @@ -16,7 +16,7 @@ export class Document { @Column("text") context: string - @Column({ type: "jsonb", array: true }) + @Column({ type: "jsonb" }) distributions: Distribution[] @Column({ type: "timestamp with time zone" }) diff --git a/test/functional/find-options/array-contained-by-operator/array-contained-by-operator.test.ts b/test/functional/find-options/array-contained-by-operator/array-contained-by-operator.test.ts index 2560c4d8d0..5ac197fd83 100644 --- a/test/functional/find-options/array-contained-by-operator/array-contained-by-operator.test.ts +++ b/test/functional/find-options/array-contained-by-operator/array-contained-by-operator.test.ts @@ -25,21 +25,18 @@ describe("find options > find operators > ArrayContainedBy", () => { const post1 = new Post() post1.title = "Post #1" post1.authors = ["dmitry", "olimjon"] - post1.categories = [{ name: "typescript" }, { name: "programming" }] post1.statuses = [PostStatus.draft, PostStatus.published] await manager.save(post1) const post2 = new Post() post2.title = "Post #2" post2.authors = ["olimjon"] - post2.categories = [{ name: "programming" }] post2.statuses = [PostStatus.published] await manager.save(post2) const post3 = new Post() post3.title = "Post #3" post3.authors = [] - post3.categories = [] post3.statuses = [] await manager.save(post3) } @@ -62,24 +59,18 @@ describe("find options > find operators > ArrayContainedBy", () => { id: 1, title: "Post #1", authors: ["dmitry", "olimjon"], - categories: [ - { name: "typescript" }, - { name: "programming" }, - ], statuses: [PostStatus.draft, PostStatus.published], }, { id: 2, title: "Post #2", authors: ["olimjon"], - categories: [{ name: "programming" }], statuses: [PostStatus.published], }, { id: 3, title: "Post #3", authors: [], - categories: [], statuses: [], }, ]) @@ -97,84 +88,12 @@ describe("find options > find operators > ArrayContainedBy", () => { id: 2, title: "Post #2", authors: ["olimjon"], - categories: [{ name: "programming" }], statuses: [PostStatus.published], }, { id: 3, title: "Post #3", authors: [], - categories: [], - statuses: [], - }, - ]) - }), - )) - - it("should find entries in jsonb", () => - Promise.all( - connections.map(async (connection) => { - await prepareData(connection.manager) - - const loadedPost1 = await connection.manager.find(Post, { - where: { - categories: ArrayContainedBy([ - { name: "typescript" }, - { name: "programming" }, - ]), - }, - order: { - id: "asc", - }, - }) - loadedPost1.should.be.eql([ - { - id: 1, - title: "Post #1", - authors: ["dmitry", "olimjon"], - categories: [ - { name: "typescript" }, - { name: "programming" }, - ], - statuses: [PostStatus.draft, PostStatus.published], - }, - { - id: 2, - title: "Post #2", - authors: ["olimjon"], - categories: [{ name: "programming" }], - statuses: [PostStatus.published], - }, - { - id: 3, - title: "Post #3", - authors: [], - categories: [], - statuses: [], - }, - ]) - - const loadedPost2 = await connection.manager.find(Post, { - where: { - categories: ArrayContainedBy([{ name: "programming" }]), - }, - order: { - id: "asc", - }, - }) - loadedPost2.should.be.eql([ - { - id: 2, - title: "Post #2", - authors: ["olimjon"], - categories: [{ name: "programming" }], - statuses: [PostStatus.published], - }, - { - id: 3, - title: "Post #3", - authors: [], - categories: [], statuses: [], }, ]) @@ -202,24 +121,18 @@ describe("find options > find operators > ArrayContainedBy", () => { id: 1, title: "Post #1", authors: ["dmitry", "olimjon"], - categories: [ - { name: "typescript" }, - { name: "programming" }, - ], statuses: [PostStatus.draft, PostStatus.published], }, { id: 2, title: "Post #2", authors: ["olimjon"], - categories: [{ name: "programming" }], statuses: [PostStatus.published], }, { id: 3, title: "Post #3", authors: [], - categories: [], statuses: [], }, ]) @@ -237,14 +150,12 @@ describe("find options > find operators > ArrayContainedBy", () => { id: 2, title: "Post #2", authors: ["olimjon"], - categories: [{ name: "programming" }], statuses: [PostStatus.published], }, { id: 3, title: "Post #3", authors: [], - categories: [], statuses: [], }, ]) diff --git a/test/functional/find-options/array-contained-by-operator/entity/Post.ts b/test/functional/find-options/array-contained-by-operator/entity/Post.ts index 60d8484ca5..16fc96fc63 100644 --- a/test/functional/find-options/array-contained-by-operator/entity/Post.ts +++ b/test/functional/find-options/array-contained-by-operator/entity/Post.ts @@ -24,12 +24,6 @@ export class Post { }) authors: string[] - @Column({ - type: "jsonb", - array: true, - }) - categories: PostCategory[] - @Column({ type: "enum", enum: PostStatus, diff --git a/test/functional/find-options/array-contains-operator/array-contains-operator.test.ts b/test/functional/find-options/array-contains-operator/array-contains-operator.test.ts index 4128fe00e0..c5e66de2e9 100644 --- a/test/functional/find-options/array-contains-operator/array-contains-operator.test.ts +++ b/test/functional/find-options/array-contains-operator/array-contains-operator.test.ts @@ -25,21 +25,18 @@ describe("find options > find operators > ArrayContains", () => { const post1 = new Post() post1.title = "Post #1" post1.authors = ["dmitry", "olimjon"] - post1.categories = [{ name: "typescript" }, { name: "programming" }] post1.statuses = [PostStatus.draft, PostStatus.published] await manager.save(post1) const post2 = new Post() post2.title = "Post #2" post2.authors = ["olimjon"] - post2.categories = [{ name: "programming" }] post2.statuses = [PostStatus.published] await manager.save(post2) const post3 = new Post() post3.title = "Post #3" post3.authors = [] - post3.categories = [] post3.statuses = [] await manager.save(post3) } @@ -62,10 +59,6 @@ describe("find options > find operators > ArrayContains", () => { id: 1, title: "Post #1", authors: ["dmitry", "olimjon"], - categories: [ - { name: "typescript" }, - { name: "programming" }, - ], statuses: [PostStatus.draft, PostStatus.published], }, ]) @@ -83,73 +76,12 @@ describe("find options > find operators > ArrayContains", () => { id: 1, title: "Post #1", authors: ["dmitry", "olimjon"], - categories: [ - { name: "typescript" }, - { name: "programming" }, - ], statuses: [PostStatus.draft, PostStatus.published], }, { id: 2, title: "Post #2", authors: ["olimjon"], - categories: [{ name: "programming" }], - statuses: [PostStatus.published], - }, - ]) - }), - )) - - it("should find entries in jsonb", () => - Promise.all( - connections.map(async (connection) => { - await prepareData(connection.manager) - - const loadedPost1 = await connection.manager.find(Post, { - where: { - categories: ArrayContains([{ name: "typescript" }]), - }, - order: { - id: "asc", - }, - }) - loadedPost1.should.be.eql([ - { - id: 1, - title: "Post #1", - authors: ["dmitry", "olimjon"], - categories: [ - { name: "typescript" }, - { name: "programming" }, - ], - statuses: [PostStatus.draft, PostStatus.published], - }, - ]) - - const loadedPost2 = await connection.manager.find(Post, { - where: { - categories: ArrayContains([{ name: "programming" }]), - }, - order: { - id: "asc", - }, - }) - loadedPost2.should.be.eql([ - { - id: 1, - title: "Post #1", - authors: ["dmitry", "olimjon"], - categories: [ - { name: "typescript" }, - { name: "programming" }, - ], - statuses: [PostStatus.draft, PostStatus.published], - }, - { - id: 2, - title: "Post #2", - authors: ["olimjon"], - categories: [{ name: "programming" }], statuses: [PostStatus.published], }, ]) @@ -174,10 +106,6 @@ describe("find options > find operators > ArrayContains", () => { id: 1, title: "Post #1", authors: ["dmitry", "olimjon"], - categories: [ - { name: "typescript" }, - { name: "programming" }, - ], statuses: [PostStatus.draft, PostStatus.published], }, ]) @@ -195,17 +123,12 @@ describe("find options > find operators > ArrayContains", () => { id: 1, title: "Post #1", authors: ["dmitry", "olimjon"], - categories: [ - { name: "typescript" }, - { name: "programming" }, - ], statuses: [PostStatus.draft, PostStatus.published], }, { id: 2, title: "Post #2", authors: ["olimjon"], - categories: [{ name: "programming" }], statuses: [PostStatus.published], }, ]) diff --git a/test/functional/find-options/array-contains-operator/entity/Post.ts b/test/functional/find-options/array-contains-operator/entity/Post.ts index 60d8484ca5..16fc96fc63 100644 --- a/test/functional/find-options/array-contains-operator/entity/Post.ts +++ b/test/functional/find-options/array-contains-operator/entity/Post.ts @@ -24,12 +24,6 @@ export class Post { }) authors: string[] - @Column({ - type: "jsonb", - array: true, - }) - categories: PostCategory[] - @Column({ type: "enum", enum: PostStatus, diff --git a/test/functional/find-options/array-overlap/array-overlap-operator.test.ts b/test/functional/find-options/array-overlap/array-overlap-operator.test.ts index 4f9d079457..f483289d90 100644 --- a/test/functional/find-options/array-overlap/array-overlap-operator.test.ts +++ b/test/functional/find-options/array-overlap/array-overlap-operator.test.ts @@ -25,21 +25,18 @@ describe("find options > find operators > ArrayOverlap", () => { const post1 = new Post() post1.title = "Post #1" post1.authors = ["dmitry", "olimjon"] - post1.categories = [{ name: "typescript" }, { name: "programming" }] post1.statuses = [PostStatus.draft, PostStatus.published] await manager.save(post1) const post2 = new Post() post2.title = "Post #2" post2.authors = ["olimjon"] - post2.categories = [{ name: "programming" }] post2.statuses = [PostStatus.published] await manager.save(post2) const post3 = new Post() post3.title = "Post #3" post3.authors = [] - post3.categories = [] post3.statuses = [] await manager.save(post3) } @@ -62,10 +59,6 @@ describe("find options > find operators > ArrayOverlap", () => { id: 1, title: "Post #1", authors: ["dmitry", "olimjon"], - categories: [ - { name: "typescript" }, - { name: "programming" }, - ], statuses: [PostStatus.draft, PostStatus.published], }, ]) @@ -83,79 +76,12 @@ describe("find options > find operators > ArrayOverlap", () => { id: 1, title: "Post #1", authors: ["dmitry", "olimjon"], - categories: [ - { name: "typescript" }, - { name: "programming" }, - ], statuses: [PostStatus.draft, PostStatus.published], }, { id: 2, title: "Post #2", authors: ["olimjon"], - categories: [{ name: "programming" }], - statuses: [PostStatus.published], - }, - ]) - }), - )) - - it("should find entries in jsonb", () => - Promise.all( - connections.map(async (connection) => { - await prepareData(connection.manager) - - const loadedPost1 = await connection.manager.find(Post, { - where: { - categories: ArrayOverlap([ - { name: "typescript" }, - { name: "python" }, - ]), - }, - order: { - id: "asc", - }, - }) - loadedPost1.should.be.eql([ - { - id: 1, - title: "Post #1", - authors: ["dmitry", "olimjon"], - categories: [ - { name: "typescript" }, - { name: "programming" }, - ], - statuses: [PostStatus.draft, PostStatus.published], - }, - ]) - - const loadedPost2 = await connection.manager.find(Post, { - where: { - categories: ArrayOverlap([ - { name: "programming" }, - { name: "python" }, - ]), - }, - order: { - id: "asc", - }, - }) - loadedPost2.should.be.eql([ - { - id: 1, - title: "Post #1", - authors: ["dmitry", "olimjon"], - categories: [ - { name: "typescript" }, - { name: "programming" }, - ], - statuses: [PostStatus.draft, PostStatus.published], - }, - { - id: 2, - title: "Post #2", - authors: ["olimjon"], - categories: [{ name: "programming" }], statuses: [PostStatus.published], }, ]) @@ -183,10 +109,6 @@ describe("find options > find operators > ArrayOverlap", () => { id: 1, title: "Post #1", authors: ["dmitry", "olimjon"], - categories: [ - { name: "typescript" }, - { name: "programming" }, - ], statuses: [PostStatus.draft, PostStatus.published], }, ]) @@ -207,17 +129,12 @@ describe("find options > find operators > ArrayOverlap", () => { id: 1, title: "Post #1", authors: ["dmitry", "olimjon"], - categories: [ - { name: "typescript" }, - { name: "programming" }, - ], statuses: [PostStatus.draft, PostStatus.published], }, { id: 2, title: "Post #2", authors: ["olimjon"], - categories: [{ name: "programming" }], statuses: [PostStatus.published], }, ]) diff --git a/test/functional/find-options/array-overlap/entity/Post.ts b/test/functional/find-options/array-overlap/entity/Post.ts index 60d8484ca5..16fc96fc63 100644 --- a/test/functional/find-options/array-overlap/entity/Post.ts +++ b/test/functional/find-options/array-overlap/entity/Post.ts @@ -24,12 +24,6 @@ export class Post { }) authors: string[] - @Column({ - type: "jsonb", - array: true, - }) - categories: PostCategory[] - @Column({ type: "enum", enum: PostStatus, diff --git a/test/functional/json/jsonb.ts b/test/functional/json/basic-jsonb/basic-jsonb.test.ts similarity index 95% rename from test/functional/json/jsonb.ts rename to test/functional/json/basic-jsonb/basic-jsonb.test.ts index 9b88743a36..53a50022bc 100644 --- a/test/functional/json/jsonb.ts +++ b/test/functional/json/basic-jsonb/basic-jsonb.test.ts @@ -1,12 +1,12 @@ -import "../../utils/test-setup" +import "../../../utils/test-setup" import { expect } from "chai" import { Record } from "./entity/Record" -import { DataSource } from "../../../src" +import { DataSource } from "../../../../src" import { closeTestingConnections, createTestingConnections, reloadTestingDatabases, -} from "../../utils/test-utils" +} from "../../../utils/test-utils" describe("jsonb type", () => { let connections: DataSource[] @@ -86,7 +86,7 @@ describe("jsonb type", () => { connections.map(async (connection) => { let recordRepo = connection.getRepository(Record) let record = new Record() - record.data = `"foo"` + record.data = `foo` let persistedRecord = await recordRepo.save(record) let foundRecord = await recordRepo.findOneBy({ id: persistedRecord.id, @@ -102,13 +102,13 @@ describe("jsonb type", () => { connections.map(async (connection) => { let recordRepo = connection.getRepository(Record) let record = new Record() - record.data2 = [1, `"2"`, { a: 3 }] + record.data = [1, `2`, { a: 3 }] let persistedRecord = await recordRepo.save(record) let foundRecord = await recordRepo.findOneBy({ id: persistedRecord.id, }) expect(foundRecord).to.be.not.undefined - expect(foundRecord!.data2).to.deep.include.members([ + expect(foundRecord!.data).to.deep.include.members([ 1, "2", { a: 3 }, diff --git a/test/functional/json/entity/Record.ts b/test/functional/json/basic-jsonb/entity/Record.ts similarity index 86% rename from test/functional/json/entity/Record.ts rename to test/functional/json/basic-jsonb/entity/Record.ts index 040a3f1d47..a64f2f0bdc 100644 --- a/test/functional/json/entity/Record.ts +++ b/test/functional/json/basic-jsonb/entity/Record.ts @@ -1,4 +1,4 @@ -import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src" +import { Column, Entity, PrimaryGeneratedColumn } from "../../../../../src" /** * For testing Postgres jsonb @@ -14,9 +14,6 @@ export class Record { @Column({ type: "jsonb", nullable: true }) data: any - @Column({ type: "jsonb", nullable: true, array: true }) - data2: any - @Column({ type: "jsonb", nullable: true, diff --git a/test/functional/json/jsonb-defaults/entity/Post.ts b/test/functional/json/jsonb-defaults/entity/Post.ts new file mode 100644 index 0000000000..c1edc78c36 --- /dev/null +++ b/test/functional/json/jsonb-defaults/entity/Post.ts @@ -0,0 +1,32 @@ +import { Column, Entity, PrimaryGeneratedColumn } from "../../../../../src" + +export type PostCategory = { + name: string +} + +@Entity() +export class Post { + @PrimaryGeneratedColumn() + id: number + + @Column() + title: string + + @Column({ + type: "jsonb", + default: ["Dmitry", "Olimjon"], + }) + authors: string[] + + @Column({ + type: "jsonb", + default: { name: "TypeScript" }, + }) + category: PostCategory + + @Column({ + type: "jsonb", + default: [{ name: "TypeScript" }], + }) + categories: PostCategory[] +} diff --git a/test/functional/json/jsonb-defaults/jsonb-defaults.test.ts b/test/functional/json/jsonb-defaults/jsonb-defaults.test.ts new file mode 100644 index 0000000000..1024338cb8 --- /dev/null +++ b/test/functional/json/jsonb-defaults/jsonb-defaults.test.ts @@ -0,0 +1,70 @@ +import "../../../utils/test-setup" +import { Post } from "./entity/Post" +import { DataSource } from "../../../../src" +import { + closeTestingConnections, + createTestingConnections, + reloadTestingDatabases, +} from "../../../utils/test-utils" + +describe("json > defaults", () => { + let connections: DataSource[] + before( + async () => + (connections = await createTestingConnections({ + entities: [Post], + enabledDrivers: ["postgres"], // because only postgres supports jsonb type + // logging: true, + })), + ) + beforeEach(() => reloadTestingDatabases(connections)) + after(() => closeTestingConnections(connections)) + + it("should insert default values properly", () => + Promise.all( + connections.map(async (connection) => { + const post1 = new Post() + post1.title = "Post #1" + await connection.manager.save(post1) + + const loadedPost1 = await connection.manager.findBy(Post, { + title: "Post #1", + }) + loadedPost1.should.be.eql([ + { + id: 1, + title: "Post #1", + authors: ["Dmitry", "Olimjon"], + category: { name: "TypeScript" }, + categories: [{ name: "TypeScript" }], + }, + ]) + + const post2 = new Post() + post2.title = "Post #2" + post2.authors = [`Umed`, `Dmitry`] + post2.category = { name: "JavaScript" } + post2.categories = [ + { name: "JavaScript" }, + { name: "ECMAScript" }, + ] + await connection.manager.save(post2) + + const loadedPost2 = await connection.manager.findBy(Post, { + title: "Post #2", + }) + loadedPost2.should.be.eql([ + { + id: 2, + title: "Post #2", + authors: ["Umed", "Dmitry"], + category: { name: "JavaScript" }, + categories: [ + { name: "JavaScript" }, + { name: "ECMAScript" }, + ], + }, + ]) + }), + )) +}) diff --git a/test/github-issues/204/entity/Record.ts b/test/github-issues/204/entity/Record.ts index eac923085c..58f20414a0 100644 --- a/test/github-issues/204/entity/Record.ts +++ b/test/github-issues/204/entity/Record.ts @@ -7,9 +7,9 @@ export class Record { @PrimaryGeneratedColumn() id: number - @Column({ type: "json", array: true }) + @Column({ type: "json" }) configs: RecordConfig[] - @Column({ type: "jsonb", array: true }) + @Column({ type: "jsonb" }) datas: RecordData[] } diff --git a/test/github-issues/3496/entity/Post.ts b/test/github-issues/3496/entity/Post.ts index 85e4180ca4..c05bbedee5 100644 --- a/test/github-issues/3496/entity/Post.ts +++ b/test/github-issues/3496/entity/Post.ts @@ -10,6 +10,6 @@ export class Post { @VersionColumn() version: number - @Column({ type: "jsonb", array: true }) + @Column({ type: "jsonb" }) problems: object }