postgres-schema-ts icon indicating copy to clipboard operation
postgres-schema-ts copied to clipboard

Change JSONValue to allow ReadonlyArray

Open garybernhardt opened this issue 3 years ago • 0 comments

Background:

When possible, it's preferable to accept read-only arrays as arguments. That allows upstream callers to pass read-only arrays or regular arrays in. But passing a read-only array in place of a regular array is disallowed. (Because the function is effectively saying "I might mutate that array", which would break the "read-only-ness" of the ReadonlyArray<T>.)

My problem:

  • We have some JSON columns.
  • Some of the app's functions want to write values into those columns, and the values contain some read-only arrays.
  • It's not possible to write these values (or any value containing a ReadonlyArray) into a postgres-schema-ts JSONValue column. The JSONValue type only allows Array<T>.

My proposed solution:

Change JSONArray to:

export type JSONArray = ReadonlyArray<JSONValue>

I tried to do this myself, but failed to run the tests, and this is definitely a change where I want to see the tests run. I installed docker-compose (v2.3.3, which is current according to homebrew). Still, the tests fail like this:

$ npm run test

> [email protected] test /Users/grb/Downloads/src/postgres-schema-ts
> docker-compose up -d && jest --runInBand --forceExit

(root) Additional property musicbrainz is not allowed

So no PR from me. But looking at the code, this feels like a very quick change.

garybernhardt avatar Mar 15 '22 21:03 garybernhardt