From bbd437505fe16a15b49f95f69d1a52b19680229a Mon Sep 17 00:00:00 2001 From: Chen Yangjian <252317+cyjake@users.noreply.github.com> Date: Wed, 14 Sep 2022 15:05:28 +0800 Subject: [PATCH] fix: AssociationOptions.select? should be supported also fixed an issue about bone.clone(), which didn't overwrite the original unset attribute set correctly --- src/bone.js | 1 + src/types/common.d.ts | 1 + test/types/decorators.test.ts | 56 ++++++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/bone.js b/src/bone.js index 60aefda3..ac6cf869 100644 --- a/src/bone.js +++ b/src/bone.js @@ -224,6 +224,7 @@ class Bone { this.#raw = Object.assign({}, this.getRaw(), target.getRaw()); this.#rawSaved = Object.assign({}, this.getRawSaved(), target.getRawSaved()); this.#rawPrevious = Object.assign({}, this.getRawPrevious(), target.getRawPrevious()); + this.#rawUnset = target._getRawUnset(); } /** diff --git a/src/types/common.d.ts b/src/types/common.d.ts index aaf9a824..559e75a7 100644 --- a/src/types/common.d.ts +++ b/src/types/common.d.ts @@ -68,6 +68,7 @@ export interface AssociateOptions { foreignKey?: string; through?: string; where?: Record; + select?: string[] | ((name: string) => boolean); } export type command = 'select' | 'insert' | 'bulkInsert' | 'update' | 'delete' | 'upsert'; diff --git a/test/types/decorators.test.ts b/test/types/decorators.test.ts index e236b74d..97f85e7c 100644 --- a/test/types/decorators.test.ts +++ b/test/types/decorators.test.ts @@ -319,7 +319,7 @@ describe('=> Decorators (TypeScript)', function() { Note.truncate(), Member.truncate(), ]); - }) + }); it('should be able to declare 1:n association', async function() { const { id: memberId } = await Member.create({ email: 'hi@example.com' }); @@ -409,4 +409,58 @@ describe('=> Decorators (TypeScript)', function() { assert.equal(result.tags[0].name, '中秋'); }); }); + + describe('HasMany({ select })', function() { + class Note extends Bone { + @Column() + id: bigint; + + @Column({ type: DataTypes.TEXT }) + content: string; + + @Column() + memberId: bigint; + } + + class Member extends Bone { + @Column() + id: bigint; + + @Column() + email: string; + + @HasMany({ + select(name) { + return name !== 'content'; + }, + }) + notes: Note[]; + } + + before(async function() { + Object.assign(Bone.models, { Note, Member }); + await Note.sync({ force: true }); + await Member.sync({ force: true }); + // TODO: merge this method into `static sync()`? + Member.initialize(); + }); + + beforeEach(async function() { + await Promise.all([ + Note.truncate(), + Member.truncate(), + ]); + }); + + it('should be able to filter select fields of association', async function() { + const member = await Member.create({ email: 'hi@example.com' }); + await Note.create({ memberId: member.id, content: 'hello' }); + const result = await Member.findOne().with('notes'); + assert.equal(result.notes.length, 1); + assert.equal(result.notes[0].content, undefined); + const [note] = result.notes; + await note.reload(); + assert.equal(note.content, 'hello'); + }); + }); });