feat: add sub fields for the relation field

This commit is contained in:
chenos 2020-12-16 10:17:30 +08:00
parent 50a081c861
commit 1006b1a9a5
2 changed files with 119 additions and 0 deletions

View 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']);
});
});

View File

@ -372,6 +372,16 @@ export interface BelongsToManyAccessors {
export abstract class Relation extends Field { 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() { public getAssociationType() {
if (this instanceof HASONE) { if (this instanceof HASONE) {
return 'hasOne'; return 'hasOne';
@ -476,6 +486,8 @@ export class HASONE extends HasOneOrMany {
} }
super({target, ...options}, context); super({target, ...options}, context);
this.targetTableInit();
} }
public getAccessors(): HasOneAccessors { public getAccessors(): HasOneAccessors {
@ -503,6 +515,8 @@ export class HASMANY extends HasOneOrMany {
} }
super({target, ...options}, context); super({target, ...options}, context);
this.targetTableInit();
} }
public getAssociationOptions(): HasManyOptions { public getAssociationOptions(): HasManyOptions {
@ -527,6 +541,7 @@ export class BELONGSTO extends Relation {
super({target, ...options}, context); super({target, ...options}, context);
this.targetTableInit();
this.updateOptionsAfterTargetModelBeDefined(); this.updateOptionsAfterTargetModelBeDefined();
} }
@ -615,6 +630,7 @@ export class BELONGSTOMANY extends Relation {
...options, ...options,
}, context); }, context);
this.targetTableInit();
this.updateOptionsAfterTargetModelBeDefined(); this.updateOptionsAfterTargetModelBeDefined();
// through table 未特殊定义时,默认根据 through 信息配置 through table // through table 未特殊定义时,默认根据 through 信息配置 through table