planetscale/database-js

Customising cast results in unexpected behaviour

OultimoCoder opened this issue · 2 comments

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function decimalsToString(field: Field, value: any) {
  if (field.type === 'DECIMAL' && typeof value === 'string') {
    return value
  }
  return cast(field, value)
}

export const getDBClient = (databaseConfig: Config['database']): Kysely<Database> => {
  dbClient =
    dbClient ||
    new Kysely<Database>({
      dialect: new PlanetScaleDialect({
        username: databaseConfig.username,
        password: databaseConfig.password,
        host: databaseConfig.host,
        fetch: (url, init) => {
          // TODO: REMOVE.
          // Remove cache header
          // https://github.com/cloudflare/workerd/issues/698
          // eslint-disable-next-line @typescript-eslint/no-explicit-any
          delete (init as any)['cache']
          return fetch(url, init)
        },
        cast: decimalsToString
      })
    })
  return dbClient
}

My tests fail because the timestamp typed fields change behaviour. If I revert the cast change then everything passes as expected.

    -   "timestamp": "2021-04-26T10:08:50.000Z",
    +   "timestamp": "2021-04-26 10:08:50",

Can you post your test example? We don't cast timestamps, I'd be surprised if it's something we're doing wrong on our end

Closing for now, feel free to reopen if you are still experiencing this issue and can reproduce the issue