title: TypeORM jsonb array column
body: <p>I'm working on a node micro-service, orm and db are respectively <code>typeorm</code> and <code>postgresql</code></p>
  
  <p>I'm trying to create <code>jsonb</code> array column but I'm probably not doing it the correct way.</p>
  
  <p><strong>Notes</strong></p>
  
  <ol>
  <li>I would normally have accomplished this by simply adding a simple extra entity and relation. In this case I have been asked to use the <code>jsonb</code> type in oder to be able to amend the interface without catering for schema changes.</li>
  <li>Storing a simple list of indexed ids would be equally good enough for now.</li>
  <li>I am not sure I should be using the <code>array: true</code> column option. I made a few attempts to use a plain <code>jsonb</code> object without succeeding ( <code>"{}"::jsonb</code> ).</li>
  </ol>
  
  <p><strong>My aim:</strong></p>
  
  <p>To store an array of objects with an <strong>indexed</strong> <code>id</code> column, and to be able to add and remove ids. In case this is not possible a flat indexed string array would do.</p>
  
  <p>e.g:</p>
  
  <p><code>[ {id: 'some-uuid-000'}, {id: 'some-uuid-001'}, ... ]</code></p>
  
  <p>or:</p>
  
  <p><code>['some-uuid-000', 'some-uuid-001', 'some-uuid-002']</code></p>
  
  <p><strong>Code:</strong></p>
  
  <p>My column definition :</p>
  
  <pre><code>@Column({
          type: 'jsonb',
          array: true,
          default: () =&gt; 'ARRAY[]::jsonb[]',
          nullable: false,
      })
      public users: Array&lt;{ id: string }&gt; = [];
  </code></pre>
  
  <p>I manage to fetch the empty array with</p>
  
  <pre><code>const group = await repo.findOneOrFail({ id: groupId });
  console.log('&gt;&gt;&gt;&gt;&gt;', group.users);
  </code></pre>
  
  <p>which outputs:</p>
  
  <pre><code>&gt;&gt;&gt;&gt;&gt; []
  </code></pre>
  
  <p>when trying to add an item to the array and persist as below</p>
  
  <pre><code>return repo.update(groupId, { users: [...group.users, { id: userId }] });
  </code></pre>
  
  <p>I get the following output:</p>
  
  <pre><code>2019-12-21 14:40:44.088 UTC [556] ERROR:  malformed array literal: "[{"id":"cc135b8a-b6ed-4cd7-99fc-396228e74509"}]"
  2019-12-21 14:40:44.088 UTC [556] DETAIL:  "[" must introduce explicitly-specified array dimensions.
  2019-12-21 14:40:44.088 UTC [556] STATEMENT:  UPDATE "group" SET "users" = $2, "created_at" = CURRENT_TIMESTAMP WHERE "id" IN ($1)
  (node:5050) UnhandledPromiseRejectionWarning: QueryFailedError: malformed array literal: "[{"id":"cc135b8a-b6ed-4cd7-99fc-396228e74509"}]"
  </code></pre>
  
  <p>The output error tells me that the configuration must be wrong as postgres seems to be provided with a plain objects array while expecting a different format/notation. I haven't found much details about this sort of scenarios in the docs.</p>