Skip to content

Commit ac67b61

Browse files
committed
refactor: remove TypeORM and related code in favor of Drizzle
1 parent d85e39c commit ac67b61

File tree

8 files changed

+77
-792
lines changed

8 files changed

+77
-792
lines changed

package-lock.json

Lines changed: 55 additions & 579 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"test": "jest",
88
"start": "npx serverless offline",
99
"load-fixtures": "ts-node src/scripts/loadBookFixtures.ts",
10-
"load-fixtures-drizzle": "ts-node src/scripts/loadBookFixturesDrizzle.ts",
1110
"load-fixtures:production": "NODE_ENV=production ts-node src/scripts/loadBookFixtures.ts",
1211
"fetch-covers": "ts-node src/scripts/fetchBookCovers.ts",
1312
"typeorm": "typeorm-ts-node-commonjs"
@@ -25,7 +24,6 @@
2524
"pg": "^8.11.3",
2625
"pg-hstore": "^2.3.4",
2726
"reflect-metadata": "^0.1.13",
28-
"typeorm": "^0.3.17",
2927
"winston": "^3.11.0"
3028
},
3129
"devDependencies": {

src/data-source.ts

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/entity/Book.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/entity/Favorite.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/entity/User.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/scripts/loadBookFixtures.ts

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1+
import { client, db } from "../db";
12
import { 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";
64
import booksData from "../fixtures/raw/books.json";
75

86
const 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);

src/scripts/loadBookFixturesDrizzle.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)