fauna/fauna-js

Response type for document doesn't include fields added by Fauna

hrishikesh-k opened this issue · 2 comments

For a query like:

const newUser = await faunaClient.query<TUser>(fql`
  Collection('collection_users').createData({
    email: ${emailNormalized},
    name: ${req.body.name},
    password_hash: ${hashPassword}
  })
`)

We get the data like:

Document {
  coll: Module { name: 'collection_users' },
  id: '389333327894544977',
  ts: TimeStub { isoString: '2024-02-10T09:07:25.340Z' },
  email: 'Redacted',
  name: 'Redacted',
  password_hash: '$argon2id$v=19$m=65536,t=3,p=4$7YqlCVX/rHuFoTae2ZwHLA$FkpZnC4FEJphv5ULkuz2TZrYcF8jZf/iv0AhTYXJSQg'
}

but my interface only includes:

export interface TUser {
  email : string
  id : string
  name : string
  password_hash : string
}

So if I try something like:

console.log(newUser.data.coll)

TypeScript complains:

Property coll does not exist on type TUser

This can be solved by adding these properties on my interface, but since these properties are added by Fauna, doesn't it make sense for the driver to handle this? This is a question and I could be wrong, so please consider this as a discussion or asking for advice.

Simply changing my interface to:

import type {Module, TimeStub} from 'fauna'
export interface TUser {
  coll : Module
  email : string
  id : string
  name : string
  password_hash : string
  ts : TimeStub
}

works. But still curios to know if a user is expected to handle this.

Now that I think about it, .query() doesn't know we're going to get a document in the response, so I don't think it's possible to type it accordingly.

I just updated my interface to:

import type {Document} from 'fauna'
export interface TUser extends Document {
  email : string
  name : string
  password_hash : string
}