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

Misleading types in the example from the docs when calling insertOne #462

Open
vitaliy-grusha opened this issue May 18, 2023 · 1 comment

Comments

@vitaliy-grusha
Copy link
Contributor

vitaliy-grusha commented May 18, 2023

Code Editor: VSCode latest
Typescript version: 4.9.5

Code:

import Papr, { types, schema } from './index';
const papr = new Papr();

const userSchema = schema({
  age: types.number(),
  firstName: types.string({ required: true }),
  lastName: types.string({ required: true }),
});
const UserModel = papr.model('users', userSchema);

async function insert() {
  const test = await UserModel.insertOne({
    firstName: 'John',
  });
  return test;
}

The schema defines firstName and lastName as required fields, but in the argument doc in insertOne whose fields are optional:

Screenshot 2023-05-18 at 15 11 04

This can be temporarily fixed by providing an empty object for the defaults option in the schema:

const userSchema = schema({
  age: types.number(),
  firstName: types.string({ required: true }),
  lastName: types.string({ required: true }),
}, { defaults: {} });

It's better to somehow fix this in the types, perhaps by removing Partial from DocumentForInsertWithoutDefaults, which would result in this:

export type DocumentForInsertWithoutDefaults<TSchema, TDefaults extends Partial<TSchema>> = Omit<
  OptionalId<TSchema>,
  keyof TDefaults
> &
 Pick<TSchema, keyof TDefaults & keyof TSchema>;

Could this change break any logic? All tests are passing.

@joshuat
Copy link
Collaborator

joshuat commented May 18, 2023

It actually seems to be resolved by adding an empty object to options, not necessarily options.defaults. Very odd, definitely worth investigating further.

Screen Shot 2023-05-18 at 08 15 51 AM@2x

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

2 participants