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

How do you handle nullable/optional fields in Create? #8

Closed
dshook opened this issue Sep 12, 2020 · 3 comments
Closed

How do you handle nullable/optional fields in Create? #8

dshook opened this issue Sep 12, 2020 · 3 comments
Labels
question Further information is requested

Comments

@dshook
Copy link

dshook commented Sep 12, 2020

I have a model that has a number of nullable fields that should be optional to pass when calling create on the repository:

class UserReportModel {
  public readonly id!: number;
  public readonly projectId!: string;
  public readonly createdAt!: Date;
  public readonly description?: string;
  public readonly version?: string;
}

The id is a serial so I'm doing the suggested removing it from params:

type UserReportParams = Omit<UserReportModel, 'id'>;

class UserReportRepository extends CrudRepository<UserReportModel, UserReportParams, number> {
  constructor(database: PgSqlDatabase) {
    super({
      database,
      table: 'UserReport',
      primaryKey: 'id',
      model: UserReportModel,
    });
  }
}

When I go to create I get type errors:

  await userReportRepository.create({
    projectId: report.ProjectIdentifier,
    createdAt: new Date(),
    description: report.Summary
  });
(property) description: string
Type 'string | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.

Looks like because the create attributes are Required here:
https://github.com/Seb-C/kiss-orm/blob/master/src/Repositories/CrudRepository.ts#L97

@Seb-C
Copy link
Owner

Seb-C commented Sep 15, 2020

@dshook Apologies for the delay.

The null values are handled by node-postgres as null values, not undefined, so you would need to change the type:

description: string|null;

In the default create method, everything is indeed made mandatory but can be nullable. undefined values would mean that those fields are not specified in the INSERT instruction (thus relying on the default database values).

@Seb-C Seb-C added the question Further information is requested label Sep 15, 2020
@Seb-C
Copy link
Owner

Seb-C commented Sep 16, 2020

I am closing this issue since I added the missing piece of information in the README file. Let me know if you have other questions or problems, and I will reopen it.

@Seb-C Seb-C closed this as completed Sep 16, 2020
@dshook
Copy link
Author

dshook commented Sep 16, 2020

Thanks for the help and updating the docs :)

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

No branches or pull requests

2 participants