Skip to content

Commit

Permalink
revert: json/jsonb change introduced in 0.3.1 (#8777)
Browse files Browse the repository at this point in the history
  • Loading branch information
pleerock committed Mar 22, 2022
1 parent 05fc744 commit edf27d9
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 282 deletions.
2 changes: 1 addition & 1 deletion src/driver/postgres/PostgresDriver.ts
Expand Up @@ -622,7 +622,7 @@ export class PostgresDriver implements Driver {
columnMetadata.type,
) >= 0
) {
return value // JSON.stringify()
return JSON.stringify(value)

This comment has been minimized.

Copy link
@olivierlacan

olivierlacan Nov 8, 2023

@pleerock Just a note that this seems to ignore the array: false config, so it'll insert a stringified array which would also lead to a malformed array literal error in a jsonb[] Postgres column.

Or am I missing something?

} else if (columnMetadata.type === "hstore") {
if (typeof value === "string") {
return value
Expand Down
2 changes: 1 addition & 1 deletion test/benchmark/bulk-save-case2/entity/Document.ts
Expand Up @@ -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" })
Expand Down
Expand Up @@ -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)
}
Expand All @@ -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: [],
},
])
Expand All @@ -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: [],
},
])
Expand Down Expand Up @@ -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: [],
},
])
Expand All @@ -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: [],
},
])
Expand Down
Expand Up @@ -24,12 +24,6 @@ export class Post {
})
authors: string[]

@Column({
type: "jsonb",
array: true,
})
categories: PostCategory[]

@Column({
type: "enum",
enum: PostStatus,
Expand Down
Expand Up @@ -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)
}
Expand All @@ -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],
},
])
Expand All @@ -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],
},
])
Expand All @@ -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],
},
])
Expand All @@ -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],
},
])
Expand Down
Expand Up @@ -24,12 +24,6 @@ export class Post {
})
authors: string[]

@Column({
type: "jsonb",
array: true,
})
categories: PostCategory[]

@Column({
type: "enum",
enum: PostStatus,
Expand Down

0 comments on commit edf27d9

Please sign in to comment.