valtyr/prisma-kysely

kyselyType annotation: support types defined in other modules

Closed this issue · 4 comments

I noticed that kyselyType annotation doesn't work with types defined in other ts files.
I can fix this by adding something like import type { Custom } from 'other-module.js' in the generated file, but it would be much better to have this feature built-in.

As an addendum to this, I have a use case where I want to put export type Custom = ColumnType<...>; into the file, and use the custom type as a column type. I think having some form of prelude where we can provide custom code which should be inserted into the generated file would be nice.

valtyr commented

I think an optional prelude is a good idea. I'll try to get to this soon.

Workaround for now. We did this to have Json types be equivalent between Prisma and Kysely

declare global {
  export type JsonObject = { [Key in string]?: JsonValue }

  /**
   * From https://github.com/sindresorhus/type-fest/
   * Matches a JSON array.
   */
  export interface JsonArray extends Array<JsonValue> {}

  /**
   * From https://github.com/sindresorhus/type-fest/
   * Matches any valid JSON value.
   */
  export type JsonValue =
    | string
    | number
    | boolean
    | JsonObject
    | JsonArray
    | null
}

Can then be used in schema annotations, as the type is available globally

  /// @kyselyType(JsonValue)

To keep things clean, also possible to export in a global namespace,

declare global {
  namespace App {
      ....
  }
}

Turns out this was possible all along, and I didn't realize it. See this comment:
#83 (comment)