KYSELY - How to use database (complexe) types in front

488 Views Asked by At

I have a NodeJS API using Kysely and Typescript with pgsql database. The database types are generated from kysely-codegen.

  1. I have properties that should be arrays of objects and i'm not sure i set the right type in my migration. I set the type to json. To save my array of object i stringify it and i guess it's okay

  2. The problem is when i want to use those types in my front end. I'm getting them also with kysely-codegen. But because some properties have json type, i can't use array properties on them such as map, length, etc.

Is there a way to explicitly save arrays in the database ? If not, how can i parse the properties of my data that have json type to array ?

Here is an example of what my data looks like when i save it to database :

state = {
    ATTACHEMENTS: [] // -> will be JSON.stringify(state.ATTACHEMENTS),
    CATEGORY: 3,
    REQUESTS: [] // -> same as ATTACHEMENTS,
    TITLE: "",
  }

And the types generated from the database :

state = {
    ATTACHEMENTS: Json | null,
    CATEGORY: number,
    REQUESTS: Json,
    TITLE: string,
  }

Parse properties as an Array

0

There are 0 best solutions below