planetscale / database-js

A Fetch API-compatible PlanetScale database driver
https://planetscale.com/docs/tutorials/planetscale-serverless-driver
Apache License 2.0
1.17k stars 35 forks source link

Customising cast results in unexpected behaviour #172

Closed OultimoCoder closed 6 months ago

OultimoCoder commented 7 months ago
// 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",
ayrton commented 7 months ago

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

ayrton commented 6 months ago

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