fix: nest append (#194)

This commit is contained in:
ChengLei Shao 2022-02-17 12:56:52 +08:00 committed by GitHub
parent 8b4d59ee97
commit 86e0f071e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 3 deletions

View File

@ -71,6 +71,16 @@ describe('repository find', () => {
}); });
}); });
it('append with associations', async () => {
const users = await User.repository.findAndCount({
appends: ['posts', 'posts.comments'],
});
const user = users[0][0];
// @ts-ignore
expect(user.get('posts')[0].get('comments')).toBeDefined();
});
describe('findOne', () => { describe('findOne', () => {
test('find one with attribute', async () => { test('find one with attribute', async () => {
const user = await User.repository.findOne({ const user = await User.repository.findOne({

View File

@ -195,7 +195,18 @@ export class OptionsParser {
// appends: ['posts'] // appends: ['posts']
// appends: ['posts.title'] // appends: ['posts.title']
// All of these can be seen as last level // All of these can be seen as last level
const lastLevel = appendFields.length <= 2; let lastLevel: boolean = false;
if (appendFields.length == 1) {
lastLevel = true;
}
if (appendFields.length == 2) {
const associationModel = associations[appendFields[0]].target;
if (associationModel.rawAttributes[appendFields[1]]) {
lastLevel = true;
}
}
// find association index // find association index
if (queryParams['include'] == undefined) { if (queryParams['include'] == undefined) {
@ -230,8 +241,10 @@ export class OptionsParser {
attributes = []; attributes = [];
} }
const attributeName = appendFields[1];
// push field to it // push field to it
attributes.push(appendFields[1]); attributes.push(attributeName);
} else { } else {
// if attributes is empty array, change it to object // if attributes is empty array, change it to object
if (Array.isArray(attributes) && attributes.length == 0) { if (Array.isArray(attributes) && attributes.length == 0) {

View File

@ -10,7 +10,7 @@ import {
ModelCtor, ModelCtor,
Op, Op,
Transaction, Transaction,
UpdateOptions as SequelizeUpdateOptions UpdateOptions as SequelizeUpdateOptions,
} from 'sequelize'; } from 'sequelize';
import { Collection } from './collection'; import { Collection } from './collection';
import { Database } from './database'; import { Database } from './database';