-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport-seed.ts
More file actions
71 lines (57 loc) · 2.05 KB
/
Copy pathexport-seed.ts
File metadata and controls
71 lines (57 loc) · 2.05 KB
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
/**
* Eksportuje dane z bazy do pliku seed-data.json.
* Pomija: Users, Clients, WorkoutLogs, RoundLogs, SetLogs, ExerciseLogs.
*/
import 'dotenv/config'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { getPayload } from 'payload'
import config from '../payload.config.js'
const dirname = path.dirname(fileURLToPath(import.meta.url))
const OUTPUT = path.resolve(dirname, '../data/seed-data.json')
async function fetchAll<T>(
payload: Awaited<ReturnType<typeof getPayload>>,
collection: Parameters<typeof payload.find>[0]['collection'],
): Promise<T[]> {
const result = await payload.find({
collection,
limit: 10000,
depth: 0,
pagination: false,
} as Parameters<typeof payload.find>[0])
return result.docs as T[]
}
async function run() {
const payload = await getPayload({ config })
payload.logger.info('Eksportowanie danych...')
const exercises = await fetchAll(payload, 'exercises')
payload.logger.info(` exercises: ${exercises.length}`)
const plans = await fetchAll(payload, 'plans')
payload.logger.info(` plans: ${plans.length}`)
const microcycles = await fetchAll(payload, 'microcycles')
payload.logger.info(` microcycles: ${microcycles.length}`)
const workouts = await fetchAll(payload, 'workouts')
payload.logger.info(` workouts: ${workouts.length}`)
const workoutGroups = await fetchAll(payload, 'workout-groups')
payload.logger.info(` workout-groups: ${workoutGroups.length}`)
const workoutExerciseRows = await fetchAll(payload, 'workout-exercise-rows')
payload.logger.info(` workout-exercise-rows: ${workoutExerciseRows.length}`)
const seedData = {
exportedAt: new Date().toISOString(),
exercises,
plans,
microcycles,
workouts,
workoutGroups,
workoutExerciseRows,
}
fs.mkdirSync(path.dirname(OUTPUT), { recursive: true })
fs.writeFileSync(OUTPUT, JSON.stringify(seedData, null, 2), 'utf-8')
payload.logger.info(`\nEksport zakończony → ${OUTPUT}`)
process.exit(0)
}
run().catch((err) => {
console.error(err)
process.exit(1)
})