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

fix: AssociationOptions.select? should be supported #343

Merged
merged 1 commit into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/types/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export interface AssociateOptions {
foreignKey?: string;
through?: string;
where?: Record<string, Literal>;
select?: string[] | ((name: string) => boolean);
}

export type command = 'select' | 'insert' | 'bulkInsert' | 'update' | 'delete' | 'upsert';
Expand Down
56 changes: 55 additions & 1 deletion test/types/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]' });
Expand Down Expand Up @@ -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: '[email protected]' });
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');
});
});
});