|
| 1 | +import { faker } from '@faker-js/faker'; |
| 2 | +import { DataSource } from 'typeorm'; |
| 3 | +import { dataSource } from '../datasource'; |
| 4 | +import { City } from '../cities/entities/city.entity'; |
| 5 | +import { Location } from '../locations/entities/location.entity'; |
| 6 | +import { Category } from '../categories/entities/category.entity'; |
| 7 | +import { Tag } from '../tags/entities/tag.entity'; |
| 8 | +import { Item } from '../items/entities/item.entity'; |
| 9 | +import { |
| 10 | + ItemCopy, |
| 11 | + ItemCondition, |
| 12 | +} from '../item-copies/entities/item-copy.entity'; |
| 13 | + |
| 14 | +// Deterministic data so repeated runs / environments produce the same set. |
| 15 | +faker.seed(20240601); |
| 16 | + |
| 17 | +const CITY_NAMES = ['Praha', 'Brno', 'Ostrava', 'Plzeň']; |
| 18 | + |
| 19 | +const CATEGORY_NAMES = [ |
| 20 | + 'Electronics', |
| 21 | + 'Books', |
| 22 | + 'Tools', |
| 23 | + 'Office', |
| 24 | + 'Sports', |
| 25 | + 'Furniture', |
| 26 | +]; |
| 27 | + |
| 28 | +const TAG_NAMES = [ |
| 29 | + 'fragile', |
| 30 | + 'heavy', |
| 31 | + 'portable', |
| 32 | + 'new', |
| 33 | + 'refurbished', |
| 34 | + 'shared', |
| 35 | + 'high-demand', |
| 36 | +]; |
| 37 | + |
| 38 | +const CONDITIONS = [ |
| 39 | + ItemCondition.Good, |
| 40 | + ItemCondition.Good, |
| 41 | + ItemCondition.Good, |
| 42 | + ItemCondition.Damaged, |
| 43 | + ItemCondition.Lost, |
| 44 | +]; |
| 45 | + |
| 46 | +/** Look up an existing row by a unique column, otherwise create it. */ |
| 47 | +async function upsert<T extends object>( |
| 48 | + ds: DataSource, |
| 49 | + entity: new () => T, |
| 50 | + where: Partial<T>, |
| 51 | + data: Partial<T> |
| 52 | +): Promise<T> { |
| 53 | + const repo = ds.getRepository(entity); |
| 54 | + const existing = await repo.findOne({ where: where as never }); |
| 55 | + if (existing) { |
| 56 | + return existing; |
| 57 | + } |
| 58 | + const created = repo.create({ ...where, ...data } as never) as T; |
| 59 | + return repo.save(created as never) as Promise<T>; |
| 60 | +} |
| 61 | + |
| 62 | +export async function seed(ds: DataSource): Promise<void> { |
| 63 | + const force = |
| 64 | + process.argv.includes('--force') || process.env.SEED_FORCE === 'true'; |
| 65 | + |
| 66 | + const cityRepo = ds.getRepository(City); |
| 67 | + const existingCities = await cityRepo.count(); |
| 68 | + if (existingCities > 0 && !force) { |
| 69 | + console.log( |
| 70 | + `Database already contains ${existingCities} cities. Skipping seed. ` + |
| 71 | + `Pass --force (or SEED_FORCE=true) to seed anyway.` |
| 72 | + ); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + console.log('Seeding cities and locations...'); |
| 77 | + const locations: Location[] = []; |
| 78 | + for (const name of CITY_NAMES) { |
| 79 | + const city = await upsert(ds, City, { name }, { archived_at: null }); |
| 80 | + for (let i = 0; i < faker.number.int({ min: 1, max: 3 }); i++) { |
| 81 | + const locName = `${name} - ${faker.location.street()}`; |
| 82 | + const location = await upsert( |
| 83 | + ds, |
| 84 | + Location, |
| 85 | + { name: locName }, |
| 86 | + { city_id: city.id, archived_at: null } |
| 87 | + ); |
| 88 | + locations.push(location); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + console.log('Seeding categories...'); |
| 93 | + const categories: Category[] = []; |
| 94 | + for (const name of CATEGORY_NAMES) { |
| 95 | + categories.push( |
| 96 | + await upsert(ds, Category, { name }, { archived_at: null }) |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + console.log('Seeding tags...'); |
| 101 | + const tags: Tag[] = []; |
| 102 | + for (const name of TAG_NAMES) { |
| 103 | + tags.push(await upsert(ds, Tag, { name }, {})); |
| 104 | + } |
| 105 | + |
| 106 | + console.log('Seeding items and copies...'); |
| 107 | + const itemRepo = ds.getRepository(Item); |
| 108 | + const copyRepo = ds.getRepository(ItemCopy); |
| 109 | + let copyCountTotal = 0; |
| 110 | + for (let i = 0; i < 25; i++) { |
| 111 | + const item = itemRepo.create({ |
| 112 | + name: faker.commerce.productName(), |
| 113 | + description: faker.commerce.productDescription(), |
| 114 | + image_url: null, |
| 115 | + default_loan_days: faker.helpers.arrayElement([7, 14, 30]), |
| 116 | + archived_at: null, |
| 117 | + categories: faker.helpers.arrayElements( |
| 118 | + categories, |
| 119 | + faker.number.int({ min: 1, max: 3 }) |
| 120 | + ), |
| 121 | + tags: faker.helpers.arrayElements( |
| 122 | + tags, |
| 123 | + faker.number.int({ min: 0, max: 3 }) |
| 124 | + ), |
| 125 | + }); |
| 126 | + const savedItem = await itemRepo.save(item); |
| 127 | + |
| 128 | + const copyCount = faker.number.int({ min: 1, max: 4 }); |
| 129 | + for (let c = 0; c < copyCount; c++) { |
| 130 | + const copy = copyRepo.create({ |
| 131 | + item_id: savedItem.id, |
| 132 | + location_id: faker.helpers.arrayElement(locations).id, |
| 133 | + condition: faker.helpers.arrayElement(CONDITIONS), |
| 134 | + archived_at: null, |
| 135 | + }); |
| 136 | + await copyRepo.save(copy); |
| 137 | + copyCountTotal++; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + console.log('Seed complete:'); |
| 142 | + console.log(` cities: ${await cityRepo.count()}`); |
| 143 | + console.log(` locations: ${await ds.getRepository(Location).count()}`); |
| 144 | + console.log(` categories: ${await ds.getRepository(Category).count()}`); |
| 145 | + console.log(` tags: ${await ds.getRepository(Tag).count()}`); |
| 146 | + console.log(` items: ${await itemRepo.count()}`); |
| 147 | + console.log(` copies: ${copyCountTotal}`); |
| 148 | +} |
| 149 | + |
| 150 | +async function main(): Promise<void> { |
| 151 | + await dataSource.initialize(); |
| 152 | + try { |
| 153 | + await seed(dataSource); |
| 154 | + } finally { |
| 155 | + await dataSource.destroy(); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +if (require.main === module) { |
| 160 | + main().catch(err => { |
| 161 | + console.error('Seed failed:', err); |
| 162 | + process.exitCode = 1; |
| 163 | + }); |
| 164 | +} |
0 commit comments