mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 21:49:25 +08:00
Merge branch 'main' into next
This commit is contained in:
commit
b4f262d2da
@ -19,7 +19,7 @@ export class ToManyInterface extends BaseInterface {
|
|||||||
|
|
||||||
const items = str.split(',');
|
const items = str.split(',');
|
||||||
|
|
||||||
const { filterKey, targetCollection, transaction } = ctx;
|
const { filterKey, targetCollection, transaction, field } = ctx;
|
||||||
|
|
||||||
const targetInstances = await targetCollection.repository.find({
|
const targetInstances = await targetCollection.repository.find({
|
||||||
filter: {
|
filter: {
|
||||||
@ -36,7 +36,19 @@ export class ToManyInterface extends BaseInterface {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const primaryKeyAttribute = targetCollection.model.primaryKeyAttribute;
|
const primaryKeyAttribute = targetCollection.model.primaryKeyAttribute;
|
||||||
|
const targetKey = field.options.targetKey;
|
||||||
|
|
||||||
return targetInstances.map((targetInstance) => targetInstance[primaryKeyAttribute]);
|
const values = targetInstances.map((targetInstance) => {
|
||||||
|
const result = {
|
||||||
|
[targetKey]: targetInstance[targetKey],
|
||||||
|
};
|
||||||
|
|
||||||
|
if (targetKey !== primaryKeyAttribute) {
|
||||||
|
result[primaryKeyAttribute] = targetInstance[primaryKeyAttribute];
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
return values;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2067,4 +2067,84 @@ describe('xlsx importer', () => {
|
|||||||
expect(users[0].get('name')).toBe('User1');
|
expect(users[0].get('name')).toBe('User1');
|
||||||
expect(users[1].get('name')).toBe('User2');
|
expect(users[1].get('name')).toBe('User2');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should import with associations by target key', async () => {
|
||||||
|
const Post = app.db.collection({
|
||||||
|
name: 'posts',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'title',
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'tags',
|
||||||
|
type: 'belongsToMany',
|
||||||
|
through: 'post_tag',
|
||||||
|
targetKey: 'name',
|
||||||
|
interface: 'm2m',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const Tag = app.db.collection({
|
||||||
|
name: 'tags',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'name',
|
||||||
|
type: 'string',
|
||||||
|
unique: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
await app.db.sync();
|
||||||
|
|
||||||
|
await Tag.repository.create({
|
||||||
|
values: {
|
||||||
|
name: 't1',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const templateCreator = new TemplateCreator({
|
||||||
|
collection: Post,
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
dataIndex: ['title'],
|
||||||
|
defaultTitle: '标题',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
dataIndex: ['tags', 'name'],
|
||||||
|
defaultTitle: 'TagsName',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const template = (await templateCreator.run({ returnXLSXWorkbook: true })) as XLSX.WorkBook;
|
||||||
|
|
||||||
|
const worksheet = template.Sheets[template.SheetNames[0]];
|
||||||
|
|
||||||
|
XLSX.utils.sheet_add_aoa(worksheet, [['Post1', 't1']], {
|
||||||
|
origin: 'A2',
|
||||||
|
});
|
||||||
|
|
||||||
|
const importer = new XlsxImporter({
|
||||||
|
collectionManager: app.mainDataSource.collectionManager,
|
||||||
|
collection: Post,
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
dataIndex: ['title'],
|
||||||
|
defaultTitle: '标题',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
dataIndex: ['tags', 'name'],
|
||||||
|
defaultTitle: 'TagsName',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
workbook: template,
|
||||||
|
});
|
||||||
|
|
||||||
|
await importer.run();
|
||||||
|
|
||||||
|
expect(await Post.repository.count()).toBe(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user