adamjkb / bark

Materialized path extension for Prisma
https://prisma-extension-bark.gitbook.io
MIT License
33 stars 5 forks source link

TypeError: siblings is not iterable on move operation #68

Closed RaphaelWski closed 9 months ago

RaphaelWski commented 9 months ago

Hey,

I have the following structure :

flowchart TD
    A[Root] --> B[Child 1]
    A --> C[Child 2]
    B --> D[Child 1.1]
[
  {
    "name": "Root",
    "depth": 1,
    "numchild": 2,
    "path": "0001"
  },
  {
    "name": "Child 1",
    "depth": 2,
    "numchild": 1,
    "path": "00010001"
  },
  {
    "slug": "Child 1.1",
    "depth": 3,
    "numchild": 0,
    "path": "000100010001"
  },
  {
    "slug": "Child 2",
    "depth": 2,
    "numchild": 0,
    "path": "00010002"
  }
]

I'm using the move operation to get the following result :

flowchart TD
    A[Root] --> B[Child 1]
    B --> D[Child 1.1]
    B --> |moved| C[Child 2]

Here is my request :

await this.ps.extended.organizationEntity.move({
          where: { id: organization.id },
          position: 'last-child',
          reference: {
            where: {
              id: futureParentOrganization.id,
            },
          },
        });

Works well but I noticed the numchild was not updated (which may be related to the error I get in my next operation ?)

[
  {
    "name": "Root",
    "depth": 1,
    "numchild": 2, <-- should be 1
    "path": "0001"
  },
  {
    "name": "Child 1",
    "depth": 2,
    "numchild": 1, <-- should be 2
    "path": "00010001"
  },
  {
    "name": "Child 1.1",
    "depth": 3,
    "numchild": 0,
    "path": "000100010001"
  },
  {
    "name": "Child 2",
    "depth": 3,
    "numchild": 0,
    "path": "000100010002"
  }
]

Then I rollback my changes (using the same request) :

flowchart TD
    A[Root] --> B[Child 1]
    B --> D[Child 1.1]
    A --> |moved| C[Child 2]

And move Child 2 again to get the following result :

flowchart TD
    A[Root] --> B[Child 1]
    B --> D[Child 1.1]
    D --> |moved| C[Child 2]

Here I get the following error : Failed to move organization : TypeError: siblings is not iterable

Looks like I can't move an item to a parent that has no existing child ?

adamjkb commented 9 months ago

Hi,

Thanks for the detailed ticket! Good catch!

I managed to reproduce it. There actually is a test for it for kind of operation, it just isn't checking if "Child 1" or "Root" is getting updated.

adamjkb commented 9 months ago

Thanks again. This issue should be fixed starting with v0.2.1. (https://github.com/adamjkb/bark/blob/main/packages/prisma-extension-bark/CHANGELOG.md#021)