fix: import with belongs to associations (#5417)

* fix: import with belongs to associations

* chore: test
This commit is contained in:
ChengLei Shao 2024-11-06 13:17:27 +08:00 committed by GitHub
parent f8ca0da8b3
commit 88cc322a8c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 150 additions and 3 deletions

View File

@ -19,7 +19,7 @@ export class ToOneInterface extends BaseInterface {
return null;
}
const { filterKey, targetCollection, transaction } = ctx;
const { filterKey, associationField, targetCollection, transaction } = ctx;
const targetInstance = await targetCollection.repository.findOne({
filter: {
@ -31,8 +31,9 @@ export class ToOneInterface extends BaseInterface {
if (!targetInstance) {
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', () => {
let User;
let Post;

View File

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