Merge branch 'main' into next

This commit is contained in:
GitHub Actions Bot 2024-08-12 10:31:38 +00:00
commit b3ad75be0a
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,
});
}