mirror of
https://gitee.com/nocobase/nocobase.git
synced 2024-12-02 04:07:50 +08:00
Merge branch 'main' into next
This commit is contained in:
commit
f218c0b1c6
@ -15,6 +15,8 @@ export class ToManyInterface extends BaseInterface {
|
||||
return null;
|
||||
}
|
||||
|
||||
str = `${str}`.trim();
|
||||
|
||||
const items = str.split(',');
|
||||
|
||||
const { filterKey, targetCollection, transaction } = ctx;
|
||||
@ -28,7 +30,7 @@ export class ToManyInterface extends BaseInterface {
|
||||
|
||||
// check if all items are found
|
||||
items.forEach((item) => {
|
||||
if (!targetInstances.find((targetInstance) => targetInstance[filterKey] === item)) {
|
||||
if (!targetInstances.find((targetInstance) => targetInstance[filterKey] == item)) {
|
||||
throw new Error(`"${item}" not found in ${targetCollection.model.name} ${filterKey}`);
|
||||
}
|
||||
});
|
||||
|
@ -583,6 +583,8 @@ describe('xlsx importer', () => {
|
||||
describe('import with associations', () => {
|
||||
let User;
|
||||
let Post;
|
||||
let Tag;
|
||||
|
||||
beforeEach(async () => {
|
||||
User = app.db.collection({
|
||||
name: 'users',
|
||||
@ -614,12 +616,98 @@ describe('xlsx importer', () => {
|
||||
target: 'users',
|
||||
interface: 'm2o',
|
||||
},
|
||||
{
|
||||
type: 'belongsToMany',
|
||||
name: 'tags',
|
||||
target: 'tags',
|
||||
interface: 'm2m',
|
||||
through: 'postsTags',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
Tag = app.db.collection({
|
||||
name: 'tags',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'name',
|
||||
},
|
||||
{
|
||||
type: 'belongsToMany',
|
||||
name: 'posts',
|
||||
target: 'posts',
|
||||
interface: 'm2m',
|
||||
through: 'postsTags',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await app.db.sync();
|
||||
});
|
||||
|
||||
it('should import many to many with id', async () => {
|
||||
await Tag.repository.create({
|
||||
values: [
|
||||
{
|
||||
title: 't1',
|
||||
},
|
||||
{
|
||||
title: 't2',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const columns = [
|
||||
{
|
||||
dataIndex: ['title'],
|
||||
defaultTitle: '名称',
|
||||
},
|
||||
{
|
||||
dataIndex: ['tags', 'id'],
|
||||
defaultTitle: 'IDS',
|
||||
},
|
||||
];
|
||||
|
||||
const templateCreator = new TemplateCreator({
|
||||
collection: Post,
|
||||
columns,
|
||||
});
|
||||
|
||||
const template = await templateCreator.run();
|
||||
|
||||
const worksheet = template.Sheets[template.SheetNames[0]];
|
||||
|
||||
XLSX.utils.sheet_add_aoa(
|
||||
worksheet,
|
||||
[
|
||||
['test', '1,2'],
|
||||
['test2', 1],
|
||||
],
|
||||
{
|
||||
origin: 'A2',
|
||||
},
|
||||
);
|
||||
|
||||
const importer = new XlsxImporter({
|
||||
collectionManager: app.mainDataSource.collectionManager,
|
||||
collection: Post,
|
||||
columns,
|
||||
workbook: template,
|
||||
});
|
||||
|
||||
await importer.run();
|
||||
|
||||
const posts = await Post.repository.find({
|
||||
appends: ['tags'],
|
||||
});
|
||||
|
||||
expect(posts.length).toBe(2);
|
||||
|
||||
expect(posts[0]['tags'].map((item: any) => item.id)).toEqual([1, 2]);
|
||||
expect(posts[1]['tags'].map((item: any) => item.id)).toEqual([1]);
|
||||
});
|
||||
|
||||
it('should validate to many association', async () => {
|
||||
const columns = [
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user