|
| 1 | +/* eslint-disable @typescript-eslint/no-non-null-assertion */ |
| 2 | +import { ref, type MikroORM } from "@mikro-orm/core"; |
| 3 | +import { Author } from "../entities/Author"; |
| 4 | +import { Chat } from "../entities/Chat"; |
| 5 | +import { Message } from "../entities/Message"; |
| 6 | +import { Publisher } from "../entities/Publisher"; |
| 7 | +import { Book } from "../entities/Book"; |
| 8 | + |
| 9 | +export async function populateDatabase(em: MikroORM["em"]): Promise<void> { |
| 10 | + const authors = [ |
| 11 | + new Author({ id: 1, name: "a", email: "[email protected]" }), |
| 12 | + new Author({ id: 2, name: "b", email: "[email protected]" }), |
| 13 | + new Author({ id: 3, name: "c", email: "[email protected]" }), |
| 14 | + new Author({ id: 4, name: "d", email: "[email protected]" }), |
| 15 | + new Author({ id: 5, name: "e", email: "[email protected]" }), |
| 16 | + ]; |
| 17 | + authors[0]!.friends.add([authors[1]!, authors[3]!, authors[4]!]); |
| 18 | + authors[0]!.friends.add([authors[1]!, authors[3]!, authors[4]!]); |
| 19 | + authors[1]!.friends.add([authors[0]!]); |
| 20 | + authors[2]!.friends.add([authors[3]!]); |
| 21 | + authors[3]!.friends.add([authors[0]!, authors[2]!]); |
| 22 | + authors[4]!.friends.add([authors[0]!]); |
| 23 | + authors[0]!.buddies.add([authors[1]!, authors[3]!, authors[4]!]); |
| 24 | + authors[0]!.buddies.add([authors[1]!, authors[3]!, authors[4]!]); |
| 25 | + authors[1]!.buddies.add([authors[0]!]); |
| 26 | + authors[2]!.buddies.add([authors[3]!]); |
| 27 | + authors[3]!.buddies.add([authors[0]!, authors[2]!]); |
| 28 | + authors[4]!.buddies.add([authors[0]!]); |
| 29 | + em.persist(authors); |
| 30 | + |
| 31 | + const chats = [ |
| 32 | + new Chat({ owner: authors[0]!, recipient: authors[1]! }), |
| 33 | + new Chat({ owner: authors[0]!, recipient: authors[2]! }), |
| 34 | + new Chat({ owner: authors[0]!, recipient: authors[4]! }), |
| 35 | + new Chat({ owner: authors[2]!, recipient: authors[0]! }), |
| 36 | + ]; |
| 37 | + chats[0]!.messages.add([new Message({ content: "A1" }), new Message({ content: "A2" })]); |
| 38 | + chats[1]!.messages.add([new Message({ content: "B1" }), new Message({ content: "B2" })]); |
| 39 | + chats[3]!.messages.add([new Message({ content: "C1" })]); |
| 40 | + em.persist(chats); |
| 41 | + |
| 42 | + const publishers = [new Publisher({ id: 1, name: "AAA" }), new Publisher({ id: 2, name: "BBB" })]; |
| 43 | + em.persist(publishers); |
| 44 | + |
| 45 | + const books = [ |
| 46 | + new Book({ id: 1, title: "One", author: authors[0]! }), |
| 47 | + new Book({ id: 2, title: "Two", author: authors[0]! }), |
| 48 | + new Book({ id: 3, title: "Three", author: authors[1]! }), |
| 49 | + new Book({ id: 4, title: "Four", author: authors[2]! }), |
| 50 | + new Book({ id: 5, title: "Five", author: authors[2]! }), |
| 51 | + new Book({ id: 6, title: "Six", author: authors[2]! }), |
| 52 | + ]; |
| 53 | + books[0]!.publisher = ref(publishers[0]!); |
| 54 | + books[1]!.publisher = ref(publishers[1]!); |
| 55 | + books[2]!.publisher = ref(publishers[1]!); |
| 56 | + books[3]!.publisher = ref(publishers[1]!); |
| 57 | + books[4]!.publisher = ref(publishers[1]!); |
| 58 | + books[5]!.publisher = ref(publishers[1]!); |
| 59 | + em.persist(books); |
| 60 | + |
| 61 | + await em.flush(); |
| 62 | + em.clear(); |
| 63 | +} |
0 commit comments