Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic type for documents improvement #152

Open
flevi29 opened this issue Feb 22, 2023 · 1 comment
Open

Generic type for documents improvement #152

flevi29 opened this issue Feb 22, 2023 · 1 comment

Comments

@flevi29
Copy link

flevi29 commented Feb 22, 2023

Generic type for Documents can be enhanced. I propose the following change to DocumentSchema:

// schema must include at least an `id`
export type DocumentSchema = { id: string; [name: string]: any }

// if property can be undefined also make it nullable
type CreatableDocumentSchema<TDocument extends DocumentSchema> = {
  [name in keyof TDocument]: undefined extends Extract<TDocument[name], undefined>
    ? TDocument[name] | null
    : TDocument[name]
}

// if property can be undefined also make it nullable, and make everything else possibly undefined as well
// except `id`
type UpdatableDocumentSchema<TDocument extends DocumentSchema> = Pick<TDocument, 'id'> & {
  [name in keyof Omit<TDocument, 'id'>]?: undefined extends Extract<TDocument[name], undefined>
    ? TDocument[name] | null
    : TDocument[name]
}

I propose to extend ImportResponseFail to include the type of the document:

export interface ImportResponseFail<TDocument extends DocumentSchema> {
  success: false
  error: string
  document: TDocument
  code: number
}

With these modifications we can now be more specific for instance in import:

async import<TOptions extends DocumentImportParameters>(
    documents: (TOptions['action'] extends 'update' | 'emplace'
      ? UpdatableDocumentSchema<T>
      : CreatableDocumentSchema<T>)[],
    options?: TOptions
  ): Promise<ImportResponse<T>[]>

I'd also like to propose a type generator based on collection fields schema if this gets approved later.

@flevi29
Copy link
Author

flevi29 commented Feb 22, 2023

Also should these types be recursive for the new object and object[] field types?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant