database-js icon indicating copy to clipboard operation
database-js copied to clipboard

Customising cast results in unexpected behaviour

Open OultimoCoder opened this issue 1 year ago • 1 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",

OultimoCoder avatar Apr 01 '24 19:04 OultimoCoder

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 avatar Apr 02 '24 11:04 ayrton

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

ayrton avatar Apr 26 '24 21:04 ayrton