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 1 commit 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

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.

2 participants