From c825c2dd5f3df35def32d7b3d5b5d02ffb27ced9 Mon Sep 17 00:00:00 2001 From: 9w Date: Thu, 4 Aug 2022 10:18:47 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20RelationType=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/relation.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/relation.ts b/src/relation.ts index 0b43661..5ea3764 100644 --- a/src/relation.ts +++ b/src/relation.ts @@ -1,14 +1,16 @@ +export type RelationType = 'ONE_TO_MANY'; export interface Relation { table: string; column: string; fk: string; property: string; + type?: RelationType; } const defaultFk = (table: string, fk?: string) => fk || `${table}Id`; -export function relation(table: string, column = 'id', fk?: string, property?: string) { - return { table, column, fk: fk || defaultFk(table, fk), property: property || table }; +export function relation(table: string, column = 'id', fk?: string, property?: string, type?: RelationType) { + return { table, column, fk: fk || defaultFk(table, fk), property: property || table, type }; } // ex. `user` === `user.id` === `userId=user.id` === `userId=user.id@user` From affdcea6693d3950da177c525c113cd96314b89d Mon Sep 17 00:00:00 2001 From: 9w Date: Thu, 4 Aug 2022 10:20:17 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20weave=EC=97=90=20one-to-many?= =?UTF-8?q?=EA=B4=80=EA=B3=84=EB=8F=84=20=EB=8F=99=EC=9E=91=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/weaver.spec.ts | 21 +++++++++++++++++++++ src/weaver.ts | 8 ++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/weaver.spec.ts b/src/weaver.spec.ts index 98f3ef3..71ad881 100644 --- a/src/weaver.spec.ts +++ b/src/weaver.spec.ts @@ -41,6 +41,27 @@ describe('weaver', () => { expect(post.user).toEqual(await knex('user').where({ id: post.userId }).first()); } }); + it('should weave one-to-many relationships', async () => { + const rr = Weaver.create({ knex, cache }); + const posts = await knex('post').select(); + const postsWithRels = await rr.weave(posts, [ + { + table: 'postTag', + column: 'postId', + fk: 'id', + property: 'postTag', + type: 'ONE_TO_MANY', + }, + ]); + for (const post of postsWithRels) { + const relationData = await knex('postTag').where({ postId: post.id }); + if (relationData.length === 0) { + expect(post.postTag).toBeUndefined(); + } else { + expect(post.postTag).toMatchObject(relationData); + } + } + }); it('should weave nothing', async () => { const rr = Weaver.create({ knex, cache }); expect(await rr.weave()).toEqual([]); diff --git a/src/weaver.ts b/src/weaver.ts index c71f037..860b51c 100644 --- a/src/weaver.ts +++ b/src/weaver.ts @@ -54,8 +54,12 @@ export class Weaver { ) { const relRow = relationRows[relationRowIndex]; if (fkValue === relRow[relation.column]) { - row[relation.property] = relRow; - break; + if (relation.type === 'ONE_TO_MANY') { + row[relation.property] = row[relation.property] ? [...row[relation.property], relRow] : [relRow]; + } else { + row[relation.property] = relRow; + break; + } } } }