fix: skip if targetKey or sourceKey exists

This commit is contained in:
chenos 2022-04-09 14:10:00 +08:00
parent c406e5eb8b
commit 1828e15bb9
2 changed files with 19 additions and 7 deletions

View File

@ -1,4 +1,4 @@
import Database, { Collection as DBCollection, StringFieldOptions } from '@nocobase/database'; import Database, { Collection as DBCollection } from '@nocobase/database';
import Application from '@nocobase/server'; import Application from '@nocobase/server';
import { createApp } from '..'; import { createApp } from '..';
@ -38,6 +38,7 @@ describe('hasMany field options', () => {
target: 'foos', target: 'foos',
}, },
}); });
await field.reload();
const json = field.toJSON(); const json = field.toJSON();
expect(json).toMatchObject({ expect(json).toMatchObject({
type: 'hasMany', type: 'hasMany',
@ -62,6 +63,7 @@ describe('hasMany field options', () => {
targetKey: 'ghi', targetKey: 'ghi',
}, },
}); });
await field.reload();
expect(field.toJSON()).toMatchObject({ expect(field.toJSON()).toMatchObject({
name: 'foos', name: 'foos',
type: 'hasMany', type: 'hasMany',

View File

@ -3,25 +3,35 @@ import { uid } from '@nocobase/utils';
import { Model } from 'sequelize'; import { Model } from 'sequelize';
const setTargetKey = (db: Database, model: Model) => { const setTargetKey = (db: Database, model: Model) => {
if (model.get('targetKey')) {
return;
}
const target = model.get('target') as any; const target = model.get('target') as any;
if (db.hasCollection(target)) { if (db.hasCollection(target)) {
const targetModel = db.getCollection(target).model; const targetModel = db.getCollection(target).model;
model.set('targetKey', targetModel.primaryKeyAttribute || 'id'); model.set('targetKey', targetModel.primaryKeyAttribute || 'id');
} else {
model.set('targetKey', 'id');
} }
}; };
const setSourceKey = (db: Database, model: Model) => { const setSourceKey = (db: Database, model: Model) => {
if (model.get('sourceKey')) {
return;
}
const source = model.get('collectionName') as any; const source = model.get('collectionName') as any;
if (db.hasCollection(source)) { if (db.hasCollection(source)) {
const sourceModel = db.getCollection(source).model; const sourceModel = db.getCollection(source).model;
model.set('sourceKey', sourceModel.primaryKeyAttribute || 'id'); model.set('sourceKey', sourceModel.primaryKeyAttribute || 'id');
} else {
model.set('sourceKey', 'id');
} }
}; };
export const beforeInitOptions = { export const beforeInitOptions = {
belongsTo(model: Model, { database }) { belongsTo(model: Model, { database }) {
const defaults = { const defaults = {
targetKey: 'id', // targetKey: 'id',
foreignKey: `f_${uid()}`, foreignKey: `f_${uid()}`,
}; };
for (const key in defaults) { for (const key in defaults) {
@ -34,8 +44,8 @@ export const beforeInitOptions = {
}, },
belongsToMany(model: Model, { database }) { belongsToMany(model: Model, { database }) {
const defaults = { const defaults = {
targetKey: 'id', // targetKey: 'id',
sourceKey: 'id', // sourceKey: 'id',
through: `t_${uid()}`, through: `t_${uid()}`,
foreignKey: `f_${uid()}`, foreignKey: `f_${uid()}`,
otherKey: `f_${uid()}`, otherKey: `f_${uid()}`,
@ -51,8 +61,8 @@ export const beforeInitOptions = {
}, },
hasMany(model: Model, { database }) { hasMany(model: Model, { database }) {
const defaults = { const defaults = {
targetKey: 'id', // targetKey: 'id',
sourceKey: 'id', // sourceKey: 'id',
foreignKey: `f_${uid()}`, foreignKey: `f_${uid()}`,
target: `t_${uid()}`, target: `t_${uid()}`,
}; };
@ -67,7 +77,7 @@ export const beforeInitOptions = {
}, },
hasOne(model: Model, { database }) { hasOne(model: Model, { database }) {
const defaults = { const defaults = {
sourceKey: 'id', // sourceKey: 'id',
foreignKey: `f_${uid()}`, foreignKey: `f_${uid()}`,
}; };
for (const key in defaults) { for (const key in defaults) {