mirror of
https://gitee.com/nocobase/nocobase.git
synced 2024-12-01 11:47:51 +08:00
feat: add sub fields for the relation field
This commit is contained in:
parent
50a081c861
commit
1006b1a9a5
103
packages/database/src/__tests__/subfields.test.ts
Normal file
103
packages/database/src/__tests__/subfields.test.ts
Normal file
@ -0,0 +1,103 @@
|
||||
import { getDatabase } from '.';
|
||||
import Database from '..';
|
||||
|
||||
let db: Database;
|
||||
|
||||
beforeEach(() => {
|
||||
db = getDatabase();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await db.sync();
|
||||
await db.close();
|
||||
});
|
||||
|
||||
describe('sub fields', () => {
|
||||
it('hasOne', async () => {
|
||||
db.table({
|
||||
name: 't1ests',
|
||||
fields: [
|
||||
{
|
||||
type: 'hasOne',
|
||||
name: 'u1ser',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'name',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(db.isDefined('t1ests')).toBeTruthy();
|
||||
expect(db.isDefined('u1sers')).toBeTruthy();
|
||||
expect([...db.getTable('t1ests').getFields().keys()]).toEqual(['u1ser']);
|
||||
expect([...db.getTable('u1sers').getFields().keys()]).toEqual(['name']);
|
||||
});
|
||||
|
||||
it('hasMany', async () => {
|
||||
db.table({
|
||||
name: 't2ests',
|
||||
fields: [
|
||||
{
|
||||
type: 'hasMany',
|
||||
name: 'u2sers',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'name',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(db.isDefined('t2ests')).toBeTruthy();
|
||||
expect(db.isDefined('u2sers')).toBeTruthy();
|
||||
expect([...db.getTable('t2ests').getFields().keys()]).toEqual(['u2sers']);
|
||||
expect([...db.getTable('u2sers').getFields().keys()]).toEqual(['name']);
|
||||
});
|
||||
|
||||
it('belongsTo', async () => {
|
||||
db.table({
|
||||
name: 't3ests',
|
||||
fields: [
|
||||
{
|
||||
type: 'belongsTo',
|
||||
name: 'u3ser',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'name',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(db.isDefined('t3ests')).toBeTruthy();
|
||||
expect(db.isDefined('u3sers')).toBeTruthy();
|
||||
expect([...db.getTable('t3ests').getFields().keys()]).toEqual(['u3ser']);
|
||||
expect([...db.getTable('u3sers').getFields().keys()]).toEqual(['name']);
|
||||
});
|
||||
|
||||
it('belongsToMany', async () => {
|
||||
db.table({
|
||||
name: 't4ests',
|
||||
fields: [
|
||||
{
|
||||
type: 'belongsToMany',
|
||||
name: 'u4sers',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'name',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(db.isDefined('t4ests')).toBeTruthy();
|
||||
expect(db.isDefined('u4sers')).toBeTruthy();
|
||||
expect([...db.getTable('t4ests').getFields().keys()]).toEqual(['u4sers']);
|
||||
expect([...db.getTable('u4sers').getFields().keys()]).toEqual(['name']);
|
||||
});
|
||||
});
|
@ -372,6 +372,16 @@ export interface BelongsToManyAccessors {
|
||||
|
||||
export abstract class Relation extends Field {
|
||||
|
||||
public targetTableInit() {
|
||||
const { target, fields = [] } = this.options;
|
||||
if (target && fields.length) {
|
||||
this.context.database.table({
|
||||
name: target,
|
||||
fields,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public getAssociationType() {
|
||||
if (this instanceof HASONE) {
|
||||
return 'hasOne';
|
||||
@ -476,6 +486,8 @@ export class HASONE extends HasOneOrMany {
|
||||
}
|
||||
|
||||
super({target, ...options}, context);
|
||||
|
||||
this.targetTableInit();
|
||||
}
|
||||
|
||||
public getAccessors(): HasOneAccessors {
|
||||
@ -503,6 +515,8 @@ export class HASMANY extends HasOneOrMany {
|
||||
}
|
||||
|
||||
super({target, ...options}, context);
|
||||
|
||||
this.targetTableInit();
|
||||
}
|
||||
|
||||
public getAssociationOptions(): HasManyOptions {
|
||||
@ -527,6 +541,7 @@ export class BELONGSTO extends Relation {
|
||||
|
||||
super({target, ...options}, context);
|
||||
|
||||
this.targetTableInit();
|
||||
this.updateOptionsAfterTargetModelBeDefined();
|
||||
}
|
||||
|
||||
@ -615,6 +630,7 @@ export class BELONGSTOMANY extends Relation {
|
||||
...options,
|
||||
}, context);
|
||||
|
||||
this.targetTableInit();
|
||||
this.updateOptionsAfterTargetModelBeDefined();
|
||||
|
||||
// through table 未特殊定义时,默认根据 through 信息配置 through table
|
||||
|
Loading…
Reference in New Issue
Block a user