This repository was archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.ts
76 lines (61 loc) · 1.56 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { statSync } from 'fs'
export const randomBoolean = () => Math.random() < 0.5
const ensureSQLite = (strapi: Strapi.Strapi) => {
console.log('verifying db as local SQLite')
const db: { config: { connection: { client: string } } } = strapi.db as any // from debugging
if (db.config.connection.client !== 'sqlite') {
throw new Error('strapi is NOT using local SQLite! Please, verify usage of SQLite before clearing data')
}
}
export const clearData = async (strapi: Strapi.Strapi) => {
ensureSQLite(strapi)
const collectionTypeUids = ['api::todo.todo', 'plugin::users-permissions.user']
const bulkClears = []
for (const collectionTypeUid of collectionTypeUids) {
const collectionClear = strapi.query(collectionTypeUid).deleteMany({
where: {
id: {
$notNull: true,
},
},
});
bulkClears.push(collectionClear)
}
await Promise.all(bulkClears)
}
type UploadFile = {
data: UploadFileData,
file: UploadFileFile,
}
type UploadFileData = {
ref: string
refId: string
field: string
}
type UploadFileFile = {
name: string
path: string
type: string
}
export const uploadFile = async (strapi: Strapi.Strapi, {
data,
file,
}: UploadFile) => {
const { refId, ref, field } = data
const { name, path, type } = file
const fileStat = statSync(path);
const [uploadedFile] = await strapi.plugins.upload.services.upload.upload({
data: {
refId,
ref,
field
},
files: {
path,
name,
type,
size: fileStat.size,
},
});
return uploadedFile
}