textileio / js-threaddb

This project has been moved to https://github.com/textileio/js-textile
https://github.com/textileio/js-textile
MIT License
14 stars 1 forks source link

Feature/optional id field on create #70

Closed cyphercider closed 1 year ago

cyphercider commented 3 years ago

Description

When working with the collection.create function, I am using a type that looks like:

export interface EntityHeader {
  _id: string
  namespaceId: string
  name: string
  classId: string
  isDeprecated: boolean
  replacedBy: string
}

When I create a typed collection and try to use the collection.create function like this:

const entity = repo.entHeaders.create({
  namespaceId: '1',
  name: 'name',
  classId: 'classId',
  isDeprecated: false,
  replacedBy: '',
})

I get a type error because in my EntityHeader type, _id is required. However, when I call the create function, I want threaddb to create an _id for me and not throw a type error when I don't want to include it.

I also would prefer to leave _id typed as 'string' rather than 'string | unknown', because everywhere other than the create function will have a string value for this field.

EDIT: I had to alter the signature of create() to create(data?: Omit<T, "_id"> & { _id?: string }). I forgot to add Omit<T, "_id"> which is necessary to remove any existing _id property (potentially non-nullable) before re-adding _id?: string as a nullable field.