sst / kysely-data-api

78 stars 21 forks source link

Set strict kysely peer dependency version range #32

Closed omikader closed 1 year ago

omikader commented 1 year ago

kysely version 0.26.0 introduced a breaking API change that removed the orWhere method which is used in the PostgresIntrospector class. See https://github.com/sst/kysely-data-api/issues/28 for more context.

There are two approaches for fixing this issue.

Set a stricter peerDependency on kysely

The pros of this approach are that we don't have to do any type gymnastics. However, this could potentially be disruptive for people who are happily using kysely-data-api on ^0.26.0 without issue because they are not depending on the PostgresIntrospector class.

Introduce a runtime typecheck

As per the linked issue, this could look something like this:

.where(
(qb) => typeof qb.where === 'function' 
  ? qb.where("c.relkind", "=", "r").orWhere("c.relkind", "=", "v") 
  : qb("c.relkind", "=", "r").or("c.relkind", "=", "v")
)

However, in order for this to pass the type system, we'd need to add some utility functions with specific type guards which can get a bit messy.

function qbHasWhere(qb: any): qb is { where: (condition: string, operator: string, value: string) => any } {
  return typeof qb.where === "function";
}

function qbIsCallable(qb: any): qb is { (condition: string, operator: string, value: string): any } {
  return typeof qb.where === "undefined";
}

...

.where(
(qb) => {
  if (qbHasWhere(qb)) qb.where("c.relkind", "=", "r").orWhere("c.relkind", "=", "v") 
  else if (qbIsCallable(qb)) qb("c.relkind", "=", "r").or("c.relkind", "=", "v")
  else // oops
}

We could also sprinkle in some // @ts-ignore but I'd rather avoid that if possible.

changeset-bot[bot] commented 1 year ago

🦋 Changeset detected

Latest commit: c0e13587842e6b2c642275e9b683817cbad0ac98

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | --------------- | ----- | | kysely-data-api | Patch |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR