From bfeaf456b9d821f2fdb54ecd797da8d476d5f87a Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 1 Dec 2023 13:54:24 +0800 Subject: [PATCH] feat: improve mock records --- .../src/server/index.ts | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-mock-collections/src/server/index.ts b/packages/plugins/@nocobase/plugin-mock-collections/src/server/index.ts index 0c970932a..c382afe20 100644 --- a/packages/plugins/@nocobase/plugin-mock-collections/src/server/index.ts +++ b/packages/plugins/@nocobase/plugin-mock-collections/src/server/index.ts @@ -195,9 +195,9 @@ export class PluginMockCollectionsServer extends Plugin { if (collection.options.template === 'file') { return mockAttachment(); } - const values = {}; + const v = {}; if (collection.options.sortable) { - values['sort'] = i + 1; + v['sort'] = i + 1; } for (const field of collection.fields.values()) { if (!field.options.interface) { @@ -208,19 +208,28 @@ export class PluginMockCollectionsServer extends Plugin { } const fn = fieldInterfaces[field.options.interface]; if (fn?.mock) { - values[field.name] = await fn.mock(field.options, { mockCollectionData, maxDepth, depth }); + v[field.name] = await fn.mock(field.options, { mockCollectionData, maxDepth, depth }); } } - return values; + return v; }), ); return count == 1 ? items[0] : items; }; const repository = ctx.db.getRepository(resourceName); - const data = await mockCollectionData(resourceName, count); + let size = count; + if (Array.isArray(values)) { + size = values.length; + } + const data = await mockCollectionData(resourceName, size); // ctx.body = data; ctx.body = await repository.create({ - values: (Array.isArray(data) ? data : [data]).map((item) => ({ ...item, ...values })), + values: (Array.isArray(data) ? data : [data]).map((item, index) => { + if (Array.isArray(values)) { + return { ...item, ...values[index] }; + } + return { ...item, ...values }; + }), }); await next(); },