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

use dataloader to collect calls to DB fixes #30 #126

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Kkoile
Copy link

@Kkoile Kkoile commented Oct 7, 2024

Using the dataloder module the calls to the DB are minimized without needing to refactor the code too much.

This fixes the performance issues which occur in drafActivate ( see #30 )

I would love to write a test, but it seems jest is not fully supported in this project.

it("should only optimize reads to the DB", async () => {
    const action1 = POST.bind(
      {},
      `/odata/v4/admin/BookStores(ID=64625905-c234-4d0d-9bc1-283ee8946770,IsActiveEntity=false)/books`,
      {
        ID: "9d703c23-54a8-4eff-81c1-cdce6b8376b2",
        title: "test title 1",
        descr: "test descr",
        author_ID: "d4d4a1b3-5b83-4814-8a20-f039af6f0387",
        stock: 1,
        price: 1.0,
        isUsed: true
      }
    );
    const action2 = POST.bind(
      {},
      `/odata/v4/admin/BookStores(ID=64625905-c234-4d0d-9bc1-283ee8946770,IsActiveEntity=false)/books`,
      {
        ID: "3e53bf3b-53f7-4db8-87ce-8bb0818574d8",
        title: "test title 2",
        descr: "test descr",
        author_ID: "d4d4a1b3-5b83-4814-8a20-f039af6f0387",
        stock: 1,
        price: 1.0,
        isUsed: true
      }
    );
    const cdsRunSpy = jest.spyOn(cds.db, "run")
    await utils.apiAction("admin", "BookStores", "64625905-c234-4d0d-9bc1-283ee8946770", "AdminService", [action1, action2]);
    // `toHaveBeenCalledWith` is not working in this project
    expect(cdsRunSpy).toHaveBeenCalledWith([expect.objectContaining({
      SELECT: {
        from: expect.objectContaining({ref: ["AdminService.Books"]}),
        where: [expect.objectContaining({ref: ["ID"]}), "in", {list: [{val: "9d703c23-54a8-4eff-81c1-cdce6b8376b2"}, {val: "3e53bf3b-53f7-4db8-87ce-8bb0818574d8"}]}]
      }
    })])
  });

@nkaputnik
Copy link
Contributor

Hello @Kkoile
thank you very much for this contribution, this looks quite elegant.
I have to check, though, for the use of the dataloader library, and whether this is something we can include here...
I'll ask @Sv7enNowitzki to modify the test so it works in the CAP context

Best,
Nick

@Sv7enNowitzki
Copy link
Collaborator

Hi @Kkoile

I have fixed the related issues so that the unit test you provided can run in the project. Could you also check if the unit test I added is what you wanted?

Also, ping @nkaputnik for reference.

Best Regards,
Wenjun

@Kkoile
Copy link
Author

Kkoile commented Oct 29, 2024

@Sv7enNowitzki the changes look good to me. Thanks!
What are the next steps? Do you think it is okay to use the dataloader dependency?

@Sv7enNowitzki
Copy link
Collaborator

Hi @nkaputnik ,

Could you please share the latest updates?

Best Regards,
Wenjun

@nkaputnik
Copy link
Contributor

Hello, I'm still waiting for approval to use the external dataloader library.
Since most resources in the CAP team are bound with other, more pressing projects right now, this is unfortunately low on then priority list.
I'll be happy to merge this PR as soon as I have clarity on how to use this OSS lib.

Best,
Nick

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

Successfully merging this pull request may close these issues.

3 participants