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

repository: Add defineModelClass helper and fix DeepPartial<AnyObject> #4266

Merged
merged 2 commits into from
Dec 9, 2019

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented Dec 6, 2019

See #4235 for background context.

Implement a new helper function that allows LB4 users to create
model classes dynamically at runtime. This is similar functionality
as offered by LB3, but with the benefit of compile-time type checks.

Example use:

const definition = new ModelDefinition('Product')
  .addProperty('id', {type: 'number', id: true})
  .addProperty('name', {
    type: 'string',
  });

const Product = defineModelClass<
  typeof Entity,
  {id: number; name: string}
>(
  Entity,
  definition,
);

While testing this code, I found a bug in DeepPartial<T> that I have experienced before. This time I could not ignore it because one of my tests would fail, so I am finally fixing it (see the first commit). Consider the following code:

function create<T extends object>(data?: DeepPartial<T>) {
  // ...
}

Before this change, create<AnyObject>({key: 'value'}) is rejected by the compiler, becase "No overload matches this call."

With this change in place, the compiler no longer complains.

Checklist

👉 Read and sign the CLA (Contributor License Agreement) 👈

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

Consider the following code:

  function create<T extends object>(data?: DeepPartial<T>) {
    // ...
  }

Before this change, `create<AnyObject>({key: 'value'})` is rejected
by the compiler, becase "No overload matches this call."

With this change in place, the compiler no longer complains.

Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
Implement a new helper function that allows LB4 users to create
model classes dynamically at runtime. This is similar functionality
as offered by LB3, but with the benefit of compile-time type checks.

Example use:

    const definition = new ModelDefinition('Product')
      .addProperty('id', {type: 'number', id: true})
      .addProperty('name', {
        type: 'string',
      });

    const Product = defineModelClass<
      typeof Entity,
      {id: number; name: string}
    >(
      Entity,
      definition,
    );

Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
@bajtos bajtos added feature Repository Issues related to @loopback/repository package labels Dec 6, 2019
@bajtos bajtos added this to the Dec 2019 milestone Dec 6, 2019
@bajtos bajtos requested review from raymondfeng and a team December 6, 2019 09:11
@bajtos bajtos self-assigned this Dec 6, 2019
@bajtos bajtos changed the title repository: Add defineModelClass helper and fix DeepPartial<AnyObject> repository: Add defineModelClass helper and fix DeepPartial<AnyObject> Dec 6, 2019
Copy link
Contributor

@emonddr emonddr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool

@bajtos bajtos merged commit 4d844d1 into master Dec 9, 2019
@bajtos bajtos deleted the feat/define-model-class branch December 9, 2019 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Repository Issues related to @loopback/repository package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants