mirror of
https://github.com/strapi/strapi.git
synced 2025-09-22 14:59:07 +00:00
Make deleteAllRecords accept contentTypes to delete rather than exceptions
This commit is contained in:
parent
8a0db019bc
commit
bba2c8a994
@ -87,9 +87,10 @@ class LocalFileSourceProvider implements ISourceProvider {
|
||||
}
|
||||
|
||||
async getSchemas() {
|
||||
const schemas = await collect(this.streamSchemas() as Readable);
|
||||
// const schemas = await collect(this.streamSchemas() as Readable);
|
||||
|
||||
return keyBy('uid', schemas);
|
||||
// return keyBy('uid', schemas);
|
||||
return null;
|
||||
}
|
||||
|
||||
streamEntities(): NodeJS.ReadableStream {
|
||||
|
@ -1,31 +1,38 @@
|
||||
import type { ContentTypeSchema } from '@strapi/strapi';
|
||||
|
||||
|
||||
const defaultExceptions = ['admin::permission', 'admin::user', 'admin::role', 'admin::api-token', 'admin::api-token-permission', 'plugin::i18n.locale']
|
||||
|
||||
export type DeleteOptions = {
|
||||
exceptions?: string[],
|
||||
conditions?: {
|
||||
[contentTypeUid: string]: {
|
||||
where: any
|
||||
}
|
||||
}
|
||||
}
|
||||
contentTypes?: ContentTypeSchema[];
|
||||
conditions?: {
|
||||
[contentTypeUid: string]: {
|
||||
where: any;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
export const deleteAllRecords = async (strapi: Strapi.Strapi, deleteOptions?: DeleteOptions) => {
|
||||
const conditions = deleteOptions?.conditions ?? {};
|
||||
const exceptions = deleteOptions?.exceptions ?? defaultExceptions;
|
||||
const contentTypes: ContentTypeSchema[] = Object.values(strapi.contentTypes);
|
||||
let count = 0;
|
||||
await Promise.all(contentTypes.map(async (contentType) => {
|
||||
const filters = conditions[contentType.uid] ?? {}
|
||||
if (!exceptions.includes(contentType.uid)) {
|
||||
const result = await strapi?.db.query(contentType.uid).deleteMany({
|
||||
filters
|
||||
});
|
||||
count += result.count;
|
||||
}
|
||||
}))
|
||||
const conditions = deleteOptions?.conditions ?? {};
|
||||
const exceptions = [
|
||||
'admin::permission',
|
||||
'admin::user',
|
||||
'admin::role',
|
||||
'admin::api-token',
|
||||
'admin::api-token-permission',
|
||||
];
|
||||
const allContentTypes: ContentTypeSchema[] = Object.values(strapi.contentTypes);
|
||||
const defaultContentTypes: ContentTypeSchema[] = allContentTypes.filter(
|
||||
(contentType) => !exceptions.includes(contentType.uid)
|
||||
);
|
||||
const contentTypes: ContentTypeSchema[] = deleteOptions?.contentTypes ?? defaultContentTypes;
|
||||
let count = 0;
|
||||
await Promise.all(
|
||||
contentTypes.map(async (contentType) => {
|
||||
const filters = conditions[contentType.uid] ?? {};
|
||||
const result = await strapi?.db.query(contentType.uid).deleteMany({
|
||||
filters,
|
||||
});
|
||||
count += result.count;
|
||||
})
|
||||
);
|
||||
|
||||
return { count }
|
||||
}
|
||||
return { count };
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user