From b401c54442d84afa4e202a31597b4dc10938c83a Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Thu, 8 Jun 2023 17:58:39 +0800 Subject: [PATCH] chore(database): append inherit inspect attribute with eager load (#2010) --- .../inhertits/collection-inherits.test.ts | 45 +++++++++++++++++++ .../src/eager-loading/eager-loading-tree.ts | 11 ++++- ...d-collection-name-after-repository-find.ts | 14 +++--- packages/core/database/src/options-parser.ts | 6 +++ 4 files changed, 70 insertions(+), 6 deletions(-) diff --git a/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts b/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts index 72c499503..a55d34c9c 100644 --- a/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts +++ b/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts @@ -16,6 +16,51 @@ pgOnly()('collection inherits', () => { await db.close(); }); + it('should append __collection with eager load', async () => { + const Root = db.collection({ + name: 'root', + fields: [ + { name: 'name', type: 'string' }, + { + name: 'bs', + type: 'hasMany', + target: 'b', + foreignKey: 'root_id', + }, + ], + }); + + const Child = db.collection({ + name: 'child', + inherits: ['root'], + }); + + const B = db.collection({ + name: 'b', + fields: [{ name: 'name', type: 'string' }], + }); + + await db.sync(); + + await Child.repository.create({ + values: { + name: 'child1', + bs: [ + { + name: 'b1', + }, + ], + }, + }); + + const data = await Root.repository.findOne({ + fields: ['bs.name'], + }); + + expect(data.get('__collection')).toEqual('child'); + expect(data.get('bs')[0].get('name')).toEqual('b1'); + }); + it('should not remove parent field reference map after child rewrite field', async () => { const through = db.collection({ name: 'through', diff --git a/packages/core/database/src/eager-loading/eager-loading-tree.ts b/packages/core/database/src/eager-loading/eager-loading-tree.ts index 184e63277..72d0e7aeb 100644 --- a/packages/core/database/src/eager-loading/eager-loading-tree.ts +++ b/packages/core/database/src/eager-loading/eager-loading-tree.ts @@ -33,7 +33,11 @@ const EagerLoadingNodeProto = { }; } - OptionsParser.appendInheritInspectAttribute(this.attributes.include, collection); + OptionsParser.appendInheritInspectAttribute( + lodash.isArray(this.attributes) ? this.attributes : this.attributes.include, + collection, + ); + this.inspectInheritAttribute = true; } }, @@ -346,8 +350,13 @@ export class EagerLoadingTree { } const nodeChildrenAs = node.children.map((child) => child.association.as); + const includeAttributes = [...nodeRawAttributes, ...nodeChildrenAs]; + if (node.inspectInheritAttribute) { + includeAttributes.push('__schemaName', '__tableName', '__collection'); + } + for (const instance of node.instances) { const attributes = lodash.pick(instance.dataValues, includeAttributes); instance.dataValues = attributes; diff --git a/packages/core/database/src/listeners/append-child-collection-name-after-repository-find.ts b/packages/core/database/src/listeners/append-child-collection-name-after-repository-find.ts index bbcae3e95..036736b51 100644 --- a/packages/core/database/src/listeners/append-child-collection-name-after-repository-find.ts +++ b/packages/core/database/src/listeners/append-child-collection-name-after-repository-find.ts @@ -2,11 +2,15 @@ export const appendChildCollectionNameAfterRepositoryFind = (db) => { return ({ findOptions, dataCollection, data }) => { if (dataCollection.isParent()) { for (const row of data) { - const rowCollection = db.tableNameCollectionMap.get( - findOptions.raw - ? `${row['__schemaName']}.${row['__tableName']}` - : `${row.get('__schemaName')}.${row.get('__tableName')}`, - ); + if (row.__collection) { + continue; + } + + const fullTableName = findOptions.raw + ? `${row['__schemaName']}.${row['__tableName']}` + : `${row.get('__schemaName')}.${row.get('__tableName')}`; + + const rowCollection = db.tableNameCollectionMap.get(fullTableName); if (!rowCollection) { db.logger.warn( diff --git a/packages/core/database/src/options-parser.ts b/packages/core/database/src/options-parser.ts index c9657b7b8..0d3ec3690 100644 --- a/packages/core/database/src/options-parser.ts +++ b/packages/core/database/src/options-parser.ts @@ -36,6 +36,11 @@ export class OptionsParser { } static appendInheritInspectAttribute(include, collection): any { + // if include already has __tableName, __schemaName, skip + if (include.find((item) => item?.[1] === '__tableName')) { + return; + } + include.push([ Sequelize.literal(`(select relname from pg_class where pg_class.oid = "${collection.name}".tableoid)`), '__tableName', @@ -147,6 +152,7 @@ export class OptionsParser { if (this.options?.fields) { attributes = []; + if (this.collection.isParent()) { OptionsParser.appendInheritInspectAttribute(attributes, this.collection); }