fix: load parent field in inherited collection (#5044)
Some checks failed
auto-merge / push-commit (push) Has been cancelled
Build Docker Image / build-and-push (push) Has been cancelled
Build Pro Image / build-and-push (push) Has been cancelled
E2E / Build (push) Has been cancelled
NocoBase Backend Test / sqlite-test (20, false) (push) Has been cancelled
NocoBase Backend Test / sqlite-test (20, true) (push) Has been cancelled
NocoBase Backend Test / postgres-test (public, 20, nocobase, false) (push) Has been cancelled
NocoBase Backend Test / postgres-test (public, 20, nocobase, true) (push) Has been cancelled
NocoBase Backend Test / postgres-test (public, 20, public, false) (push) Has been cancelled
NocoBase Backend Test / postgres-test (public, 20, public, true) (push) Has been cancelled
NocoBase Backend Test / postgres-test (user_schema, 20, nocobase, false) (push) Has been cancelled
NocoBase Backend Test / postgres-test (user_schema, 20, nocobase, true) (push) Has been cancelled
NocoBase Backend Test / postgres-test (user_schema, 20, public, false) (push) Has been cancelled
NocoBase Backend Test / postgres-test (user_schema, 20, public, true) (push) Has been cancelled
NocoBase Backend Test / mysql-test (20, false) (push) Has been cancelled
NocoBase Backend Test / mysql-test (20, true) (push) Has been cancelled
NocoBase Backend Test / mariadb-test (20, false) (push) Has been cancelled
NocoBase Backend Test / mariadb-test (20, true) (push) Has been cancelled
Test on Windows / build (push) Has been cancelled
E2E / Core and plugins (push) Has been cancelled
E2E / plugin-workflow (push) Has been cancelled
E2E / plugin-workflow-approval (push) Has been cancelled
E2E / plugin-data-source-main (push) Has been cancelled
E2E / Comment on PR (push) Has been cancelled

* fix: load parent field in inherited collection

* chore: test
This commit is contained in:
ChengLei Shao 2024-08-12 18:30:18 +08:00 committed by GitHub
parent b75fdc7f62
commit fb57f12ba2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 77 additions and 4 deletions

View File

@ -25,6 +25,65 @@ describe.runIf(isPg())('collection inherits', () => {
await db.close();
});
it('should load parent collection with association field', async () => {
const User = db.collection({
name: 'users',
autoGenId: false,
timestamps: false,
fields: [
{
name: 'roles',
type: 'belongsToMany',
target: 'roles',
through: 'rolesUsers',
},
],
});
User.setField('roles', {
type: 'belongsToMany',
target: 'roles',
through: 'rolesUsers',
});
User.setField('id', {
type: 'bigInt',
primaryKey: true,
});
const Role = db.collection({
name: 'roles',
autoGenId: false,
fields: [
{
name: 'name',
primaryKey: true,
type: 'string',
},
{
name: 'users',
type: 'belongsToMany',
target: 'users',
through: 'rolesUsers',
},
],
});
await db.sync();
let err;
try {
const child = db.collection({
name: 'child',
inherits: ['users'],
});
} catch (e) {
err = e;
}
expect(err).toBeUndefined();
});
it('should append __collection with eager load', async () => {
const Root = db.collection({
name: 'root',

View File

@ -68,13 +68,13 @@ export class InheritedCollection extends Collection {
for (const parent of this.parents) {
if (parent.isInherited()) {
for (const [name, field] of (<InheritedCollection>parent).parentFields()) {
fields.set(name, field.options);
fields.set(name, field);
}
}
const parentFields = parent.fields;
for (const [name, field] of parentFields) {
fields.set(name, field.options);
fields.set(name, field);
}
}
@ -120,9 +120,23 @@ export class InheritedCollection extends Collection {
}
protected setParentFields() {
for (const [name, fieldOptions] of this.parentFields()) {
const delayFields = new Map<string, Field>();
for (const [name, field] of this.parentFields()) {
if (field.isRelationField()) {
delayFields.set(name, field);
continue;
}
this.setField(name, {
...fieldOptions,
...field.options,
inherit: true,
});
}
for (const [name, field] of delayFields) {
this.setField(name, {
...field.options,
inherit: true,
});
}