1+ import { client , db } from "../db" ;
12import { faker } from "@faker-js/faker" ;
2- import { FixtureDataSource } from "../data-source" ;
3- import { User } from "../entity/User" ;
4- import { Book } from "../entity/Book" ;
5- import { Favorite } from "../entity/Favorite" ;
3+ import { book , favorite } from "../schema" ;
64import booksData from "../fixtures/raw/books.json" ;
75
86const initializeBooks = async ( bookData ) => {
@@ -12,45 +10,28 @@ const initializeBooks = async (bookData) => {
1210 ) ;
1311 return ;
1412 }
15- const book = new Book ( ) ;
16- book . title = bookData . title ;
17- book . author = bookData . author ;
18- book . publicationDate = new Date ( bookData . publicationDate ) ;
19- book . image = bookData . image || faker . image . url ( { width : 150 , height : 150 } ) ;
20- book . rating = bookData . rating ;
21- book . ratingsCount = bookData . ratingsCount ;
22- return FixtureDataSource . manager . save ( book ) ;
23- } ;
2413
25- const initializeUsers = async ( ) => {
26- const user = new User ( ) ;
27- user . auth0Id = "1234567890" ;
28- return FixtureDataSource . manager . save ( user ) ;
29- } ;
14+ const bookValues = {
15+ title : bookData . title ,
16+ author : bookData . author ,
17+ publicationDate : new Date ( bookData . publicationDate ) . toISOString ( ) ,
18+ image : bookData . image || faker . image . url ( { width : 150 , height : 150 } ) ,
19+ rating : bookData . rating ,
20+ ratingsCount : bookData . ratingsCount ,
21+ } ;
3022
31- const initializeFavorites = async ( user , book ) => {
32- const favorite = new Favorite ( ) ;
33- favorite . userId = user . id ;
34- favorite . bookId = book . id ;
35- return FixtureDataSource . manager . save ( favorite ) ;
23+ return db . insert ( book ) . values ( bookValues ) . returning ( ) ;
3624} ;
3725
38- FixtureDataSource . initialize ( )
39- . then ( async ( ) => {
40- console . log ( "Dropping and recreating the tables..." ) ;
41- await FixtureDataSource . manager . query ( 'TRUNCATE "favorite" CASCADE' ) ;
42- await FixtureDataSource . manager . query ( 'TRUNCATE "user", "book" CASCADE' ) ;
43- await FixtureDataSource . synchronize ( true ) ;
44-
45- console . log ( "Inserting new books into the database..." ) ;
46- const books = await Promise . all ( booksData . map ( initializeBooks ) ) ;
26+ ( async ( ) => {
27+ console . log ( "Truncating the favorite and book table..." ) ;
28+ await db . delete ( favorite ) ;
29+ await db . delete ( book ) ;
4730
48- console . log ( "Inserting new users into the database..." ) ;
49- const user = await initializeUsers ( ) ;
50-
51- console . log ( "Marking some books as favorites for the user..." ) ;
52- await Promise . all (
53- books . slice ( 0 , 5 ) . map ( ( book ) => initializeFavorites ( user , book ) )
54- ) ;
55- } )
56- . catch ( ( error ) => console . log ( error ) ) ;
31+ console . log ( "Inserting new books into the database..." ) ;
32+ const books = await Promise . all (
33+ booksData . map ( ( bookData ) => initializeBooks ( bookData ) )
34+ ) ;
35+ console . log ( `Inserted ${ books . length } books.` ) ;
36+ await client . end ( ) ;
37+ } ) ( ) . catch ( console . error ) ;
0 commit comments