Merge branch 'main' into next

This commit is contained in:
nocobase[bot] 2024-11-06 05:47:09 +00:00
commit 4508ba564c
3 changed files with 150 additions and 3 deletions

View File

@ -19,7 +19,7 @@ export class ToOneInterface extends BaseInterface {
return null; return null;
} }
const { filterKey, targetCollection, transaction } = ctx; const { filterKey, associationField, targetCollection, transaction } = ctx;
const targetInstance = await targetCollection.repository.findOne({ const targetInstance = await targetCollection.repository.findOne({
filter: { filter: {
@ -31,8 +31,9 @@ export class ToOneInterface extends BaseInterface {
if (!targetInstance) { if (!targetInstance) {
throw new Error(`"${str}" not found in ${targetCollection.model.name} ${filterKey}`); throw new Error(`"${str}" not found in ${targetCollection.model.name} ${filterKey}`);
} }
const primaryKeyAttribute = targetCollection.model.primaryKeyAttribute;
return targetInstance[primaryKeyAttribute]; const targetKey = associationField.targetKey || targetCollection.model.primaryKeyAttribute;
return targetInstance[targetKey];
} }
} }

View File

@ -281,6 +281,151 @@ describe('xlsx importer', () => {
}); });
}); });
describe('import with belongs to association', async () => {
let Profile;
let User;
beforeEach(async () => {
Profile = app.db.collection({
name: 'profiles',
autoGenId: false,
fields: [
{
type: 'bigInt',
name: 'id',
primaryKey: true,
autoIncrement: true,
},
{
type: 'string',
name: 'name',
},
{
type: 'string',
name: 'userName',
},
{
type: 'belongsTo',
name: 'user',
target: 'users',
foreignKey: 'userName',
targetKey: 'name',
},
],
});
User = app.db.collection({
name: 'users',
autoGenId: false,
fields: [
{
type: 'bigInt',
name: 'id',
primaryKey: true,
autoIncrement: true,
},
{
type: 'string',
name: 'name',
unique: true,
},
],
});
await app.db.sync();
const user = await User.repository.create({
values: {
name: 'User1',
},
});
});
it('should import with foreignKey', async () => {
const columns = [
{
dataIndex: ['name'],
defaultTitle: '名称',
},
{
dataIndex: ['userName'],
defaultTitle: '用户名',
},
];
const templateCreator = new TemplateCreator({
collection: Profile,
columns,
});
const template = await templateCreator.run();
const worksheet = template.Sheets[template.SheetNames[0]];
XLSX.utils.sheet_add_aoa(worksheet, [['test', 'User1']], {
origin: 'A2',
});
const importer = new XlsxImporter({
collectionManager: app.mainDataSource.collectionManager,
collection: Profile,
columns,
workbook: template,
});
await importer.run();
const profile = await Profile.repository.findOne({
appends: ['user'],
});
expect(profile.get('user').get('name')).toBe('User1');
expect(profile.get('name')).toBe('test');
});
it('should import with association field', async () => {
const columns = [
{
dataIndex: ['name'],
defaultTitle: '名称',
},
{
dataIndex: ['user', 'name'],
defaultTitle: '用户名',
},
];
const templateCreator = new TemplateCreator({
collection: Profile,
columns,
});
const template = await templateCreator.run();
const worksheet = template.Sheets[template.SheetNames[0]];
XLSX.utils.sheet_add_aoa(worksheet, [['test', 'User1']], {
origin: 'A2',
});
const importer = new XlsxImporter({
collectionManager: app.mainDataSource.collectionManager,
collection: Profile,
columns,
workbook: template,
});
await importer.run();
const profile = await Profile.repository.findOne({
appends: ['user'],
});
expect(profile.get('user').get('name')).toBe('User1');
expect(profile.get('name')).toBe('test');
});
});
describe('import with associations', () => { describe('import with associations', () => {
let User; let User;
let Post; let Post;

View File

@ -172,6 +172,7 @@ export class XlsxImporter extends EventEmitter {
}; };
if (column.dataIndex.length > 1) { if (column.dataIndex.length > 1) {
ctx.associationField = field;
ctx.targetCollection = (field as IRelationField).targetCollection(); ctx.targetCollection = (field as IRelationField).targetCollection();
ctx.filterKey = column.dataIndex[1]; ctx.filterKey = column.dataIndex[1];
} }