Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSQL custom type array returned as native array instead JS array. #2720

Open
PetteriHaro opened this issue Nov 11, 2024 · 0 comments
Open

PSQL custom type array returned as native array instead JS array. #2720

PetteriHaro opened this issue Nov 11, 2024 · 0 comments

Comments

@PetteriHaro
Copy link

Bumped into an interesting case where I have a table with a languages column thats is typed to a custom type named LANGUAGE[]. If a column is custom typed array, objection returns it as psql native array instead of javascript. I'll put steps to repro below.

Not really end of the world since I can just use TEXT[] for my case. I bumped into this problem using version 2.2.14. Tried also upgrading to the latest version of 3.1.5. Used also the jsonAttributes method but it didn't work.

Steps for repro:

CREATE TYPE language AS ENUM ('fi', 'en');

CREATE TABLE lamp(
  id SERIAL PRIMARY KEY,
  languages language[] NOT NULL -- If this his TEXT[] there are no issues.
);

INSERT INTO lamp(id, languages) VALUES (1, ARRAY['en', 'fi']::language[])

Lamp.ts model:

import { Model } from "objection";

class Lamp extends Model {
  readonly id: number;
  languages: ('fi' | 'en')[];

  static tableName = "lamp";

  static jsonAttributes = [];

  static jsonSchema = {
    type: "object",
    required: ["languages"],
    properties: {
      id: { type: "integer" },
      languages: { type: "array" }
    }
  }
}

module.exports = Lamp

LampController.ts

import Lamp from "../models/Lamp";

export const getLamp = async () => {
   const lamp = await Lamp.query().findById(1);

  // lamp.languages with language[] type returns '{en, fi}' 
  // but with TEXT[] it returns ["en", "fi"] correctly
}

As I said not huge problem and can be worked around pretty easily but more of an FYI if someone else happens to bump into the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant