chore(database): append inherit inspect attribute with eager load (#2010)

This commit is contained in:
ChengLei Shao 2023-06-08 17:58:39 +08:00 committed by GitHub
parent 24601aa66f
commit b401c54442
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 6 deletions

View File

@ -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',

View File

@ -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;

View File

@ -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(

View File

@ -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);
}