Skip to content

Add JSON[] support to postgres #204

Closed

Description

@dcworldwide

Hi, I'm thinking about adding JSON[] support for Postgres. Please read through my proposal and confirm if you will accept it as a PR.

Problem:

@Column("json")
products: Product[]

I'm trying to store an array of JSON objects in a JSON field. This breaks in typeorm, but is supported by Postgres.

Potential Workaround

As a workaround, I could wrap my Product[] in a json object, but then my Entities would have an extra level of indirection.

I.e.

.persist({id: 1, products: {products: myProductsArray}}

Proposed Solution

It is possible to add the following new type

@Column("json[]")
products: Product[]

This would then be serialised into

INSERT INTO table ("products") 
VALUES (array['{"id":"1","name":"A","price":0}', '{"id":"2","name":"B","price":1}']::json[]);        

@pleerock what are your thoughts?

Activity

  1. dcworldwide commented on Jan 15, 2017

    @dcworldwide
    ContributorAuthor

    Relates to #183

  2. pleerock commented on Jan 15, 2017

    @pleerock
    Member

    Why json[] column type is needed? I don't think we ned this extra type - we can use same json type. To be honest I surprised why array jsons aren't supported right. Json value is going right to the underlying driver and it should be able to handle arrays of json too. Did you research if pg supports array jsons?

  3. dcworldwide commented on Jan 16, 2017

    @dcworldwide
    ContributorAuthor

    Agree we dont need a new type. We can just re-use json or jsonb.

    It's not mentioned explicitly, but from what i've seen people talking about in the pg issues, inserting an array into a JSON field is supported.

    I need to look at how we and how pg are handling JSON serialisation.

  4. pleerock commented on Jan 26, 2017

    @pleerock
    Member

    btw probably @mingyang91 already fixed issue with array jsob? Take a look on this test.

  5. mingyang91 commented on Jan 27, 2017

    @mingyang91
    Contributor

    My workaround. @Reflect.metadata("design:type", Object)

    @Entity()
    export class Record {
    
        @PrimaryGeneratedColumn()
        id: number;
    
        @Column({ type: "jsonb", nullable: true })
        data: any;
    
        @Column({ type: "jsonb", nullable: true })
        @Reflect.metadata("design:type", Object)
        dataset: Data[];
    }
    
    export class Data {
        name: string;
    }
    

    If the metadata is not set, I get the following error.

    events.js:160
          throw er; // Unhandled 'error' event
          ^
    Error: Column type of function Array() { [native code] } cannot be determined.
        at Function.ColumnTypes.determineTypeFromFunction (/Users/famer.me/Repository/typeorm/build/compiled/src/metadata/types/ColumnTypes.js:9:2297)
        at /Users/famer.me/Repository/typeorm/src/decorator/columns/Column.ts:35:4
        at DecorateProperty (/Users/famer.me/Repository/typeorm/node_modules/reflect-metadata/Reflect.ts:1079:31)
        at Object.decorate (/Users/famer.me/Repository/typeorm/node_modules/reflect-metadata/Reflect.ts:276:20)
        at __decorate (/Users/famer.me/Repository/typeorm/build/compiled/test/functional/json/entity/Record.js:4:92)
        at Object.<anonymous> (/Users/famer.me/Repository/typeorm/test/functional/json/entity/Record.ts:21:5)
        at Module._compile (module.js:571:32)
        at Object.Module._extensions..js (module.js:580:10)
        at Object.Module._extensions.(anonymous function) [as .js] (/Users/famer.me/Repository/typeorm/node_modules/istanbul/lib/hook.js:109:37)
        at Module.load (module.js:488:32)
    npm ERR! Test failed.  See above for more details.
    
  6. pleerock commented on Jan 27, 2017

    @pleerock
    Member

    @mingyang91 its a bug. Fixed it in 0.0.8-alpha.3. I think this issue can be closed now.

  7. dcworldwide commented on Jan 27, 2017

    @dcworldwide
    ContributorAuthor

    That's great. Just to confirm..is the reflect workaround needed?

  8. dcworldwide commented on Jan 27, 2017

    @dcworldwide
    ContributorAuthor

    Will this also work with json as well as jsonb?

  9. pleerock commented on Jan 27, 2017

    @pleerock
    Member

    That's great. Just to confirm..is the reflect workaround needed?

    no

    Will this also work with json as well as jsonb?

    yes

  10. dcworldwide commented on Feb 4, 2017

    @dcworldwide
    ContributorAuthor

    Works well, thanks

  11. added a commit that references this issue on Mar 19, 2019
  12. akhil0317 commented on Apr 18, 2022

    @akhil0317

    Hi just a small doubt can I know where I can find detailed docs for Typeorm I am new to TypeOrm and I am facing difficulties in finding the detailed docs where I can find all information and every time I am depending on stack overflow for getting results instead of documentation.

  13. AlexMesser commented on Apr 18, 2022

    @AlexMesser
    Collaborator
  14. akhil0317 commented on Apr 18, 2022

    @akhil0317

    @akhil0317 https://typeorm.io/

    Thanks for the reply in the link https://typeorm.io/
    i did not find all the features documentation I am only finding the basic documentation .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions