feat: improve mock records

This commit is contained in:
chenos 2023-12-01 13:54:24 +08:00
parent cb7f1d7aa9
commit bfeaf456b9

View File

@ -195,9 +195,9 @@ export class PluginMockCollectionsServer extends Plugin {
if (collection.options.template === 'file') { if (collection.options.template === 'file') {
return mockAttachment(); return mockAttachment();
} }
const values = {}; const v = {};
if (collection.options.sortable) { if (collection.options.sortable) {
values['sort'] = i + 1; v['sort'] = i + 1;
} }
for (const field of collection.fields.values()) { for (const field of collection.fields.values()) {
if (!field.options.interface) { if (!field.options.interface) {
@ -208,19 +208,28 @@ export class PluginMockCollectionsServer extends Plugin {
} }
const fn = fieldInterfaces[field.options.interface]; const fn = fieldInterfaces[field.options.interface];
if (fn?.mock) { 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; return count == 1 ? items[0] : items;
}; };
const repository = ctx.db.getRepository(resourceName); 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 = data;
ctx.body = await repository.create({ 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(); await next();
}, },