Skip to content

Commit

Permalink
fix (client): avoid duplicate rows when including several relations (#…
Browse files Browse the repository at this point in the history
…1026)

This PR fixes a bug in the DAL that caused rows to be included several
times when including multiple relations.
The issue was reported on Discord:
https://discord.com/channels/933657521581858818/1210387734234005614
  • Loading branch information
kevin-dp committed Mar 5, 2024
1 parent 85daa6e commit c30516e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-suns-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"electric-sql": patch
---

Fix issue with duplicate rows when including several relations.
22 changes: 9 additions & 13 deletions clients/typescript/src/client/model/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ export class Table<
relationType: Arity,
includeArg: true | FindInput<any, any, any, any, any>,
db: DB,
onResult: (joinedRows: Kind<GetPayload, T>[]) => void,
onResult: () => void,
onError: (err: any) => void
) {
const otherTable = this._tables.get(relatedTable)!
Expand All @@ -687,15 +687,16 @@ export class Table<
(relatedRows: object[]) => {
// Now, join the original `rows` with the `relatedRows`
// where `row.fromField == relatedRow.toField`
const join = this.joinObjects(
// (this mutates the original rows)
this.joinObjects(
rows,
relatedRows,
fromField,
toField,
relationField,
relationType
) as Kind<GetPayload, T>[]
onResult(join)
onResult()
},
onError
)
Expand All @@ -706,11 +707,11 @@ export class Table<
relation: Relation,
includeArg: boolean | FindInput<any, any, any, any, any>,
db: DB,
onResult: (rows: Kind<GetPayload, T>[]) => void,
onResult: () => void,
onError: (err: any) => void
) {
if (includeArg === false) {
return onResult([])
return onResult()
} else if (relation.isIncomingRelation()) {
// incoming relation from the `fromField` in the other table
// to the `toField` in this table
Expand Down Expand Up @@ -771,9 +772,6 @@ export class Table<
return onResult(rows)
else {
const relationFields = Object.keys(include)
let includedRows: Kind<GetPayload, T>[] = []
// TODO: everywhere we use forEachCont we probably don't need continuation passing style!
// so try to remove it there and then rename this one to `forEachCont`
forEach(
(relationField: string, cont: () => void) => {
if (
Expand All @@ -796,22 +794,20 @@ export class Table<
relationName
)

// `fetchInclude` mutates the `rows` to include the related objects
this.fetchInclude(
rows,
relation,
include[relationField],
db,
(fetchedRows) => {
includedRows = includedRows.concat(fetchedRows)
cont()
},
cont,
onError
)
},
relationFields,
() => {
// once the loop finished, call `onResult`
onResult(includedRows)
onResult(rows)
}
)
}
Expand Down
8 changes: 5 additions & 3 deletions clients/typescript/test/client/model/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,9 @@ test.serial(
'findMany can fetch related objects based on incoming FK of one-to-many relation',
async (t) => {
const res = await userTable.findMany({
where: {
id: 1,
},
include: {
posts: true,
profile: true,
},
})

Expand All @@ -612,6 +610,10 @@ test.serial(
...author1,
posts: [post1, post2],
},
{
...author2,
posts: [post3],
},
])
}
)
Expand Down

0 comments on commit c30516e

Please sign in to comment.