mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-02 11:12:20 +08:00
Merge branch 'main' into next
This commit is contained in:
commit
63ba93dfd1
@ -15,10 +15,13 @@ export class ToManyInterface extends BaseInterface {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
str = `${str}`.trim();
|
||||||
|
|
||||||
const items = str.split(',');
|
const items = str.split(',');
|
||||||
|
|
||||||
const { filterKey, targetCollection, transaction } = ctx;
|
const { filterKey, targetCollection, transaction } = ctx;
|
||||||
|
|
||||||
|
console.log({ filterKey });
|
||||||
const targetInstances = await targetCollection.repository.find({
|
const targetInstances = await targetCollection.repository.find({
|
||||||
filter: {
|
filter: {
|
||||||
[filterKey]: items,
|
[filterKey]: items,
|
||||||
@ -28,7 +31,7 @@ export class ToManyInterface extends BaseInterface {
|
|||||||
|
|
||||||
// check if all items are found
|
// check if all items are found
|
||||||
items.forEach((item) => {
|
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}`);
|
throw new Error(`"${item}" not found in ${targetCollection.model.name} ${filterKey}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -583,6 +583,8 @@ describe('xlsx importer', () => {
|
|||||||
describe('import with associations', () => {
|
describe('import with associations', () => {
|
||||||
let User;
|
let User;
|
||||||
let Post;
|
let Post;
|
||||||
|
let Tag;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
User = app.db.collection({
|
User = app.db.collection({
|
||||||
name: 'users',
|
name: 'users',
|
||||||
@ -614,12 +616,98 @@ describe('xlsx importer', () => {
|
|||||||
target: 'users',
|
target: 'users',
|
||||||
interface: 'm2o',
|
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();
|
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 () => {
|
it('should validate to many association', async () => {
|
||||||
const columns = [
|
const columns = [
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user