-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use astro:db instead of kysely + better-sqlite3 (#20)
* feat: use astro:db instead of kysely + better-sqlite3 * chore: lockfile
- Loading branch information
1 parent
9209e72
commit af54d1e
Showing
10 changed files
with
608 additions
and
710 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { column, defineDb, defineTable } from 'astro:db'; | ||
|
||
const Cover = defineTable({ | ||
columns: { | ||
id: column.number({ primaryKey: true }), | ||
src: column.text(), | ||
width: column.number(), | ||
height: column.number(), | ||
placeholder: column.text() | ||
} | ||
}); | ||
|
||
const Catalogue = defineTable({ | ||
columns: { | ||
id: column.number({ primaryKey: true }), | ||
type: column.text(), | ||
title: column.text(), | ||
author: column.text(), | ||
cover: column.number({ references: () => Cover.columns.id }), | ||
rating: column.text(), | ||
finishedDate: column.number({ optional: true }), | ||
platform: column.text({ optional: true }), | ||
metadata: column.text() | ||
} | ||
}); | ||
|
||
// https://astro.build/db/config | ||
export default defineDb({ | ||
tables: { | ||
Cover, | ||
Catalogue | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { getCatalogueData, type CatalogueType, type allCatalogueTypes } from "$data/catalogue"; | ||
import { getConfiguredImageService, getImage } from "astro:assets"; | ||
import { getCollection } from "astro:content"; | ||
import { Catalogue, Cover, db } from "astro:db"; | ||
import type { LocalImageServiceWithPlaceholder } from "src/imageService"; | ||
|
||
// https://astro.build/db/seed | ||
export default async function seed() { | ||
await prepareDB(); | ||
} | ||
|
||
export async function prepareDB() { | ||
const games = await getCollection("games"); | ||
const movies = await getCollection("movies"); | ||
const shows = await getCollection("shows"); | ||
const books = await getCollection("books"); | ||
|
||
const catalogueContent = [...games, ...movies, ...shows, ...books]; | ||
for (const entry of catalogueContent) { | ||
await addCatalogueEntry(entry); | ||
} | ||
} | ||
|
||
export async function addCatalogueEntry(entry: allCatalogueTypes) { | ||
const { cover, type, ...data } = entry.data; | ||
const [processedCover, placeholderURL] = await getCoverAndPlaceholder(cover); | ||
const metadata = await getCatalogueData(entry); | ||
|
||
const author = getAuthorFromEntryMetadata(type, metadata); | ||
|
||
const coverId = await db.insert(Cover).values({ | ||
src: processedCover.src, | ||
width: processedCover.attributes.width, | ||
height: processedCover.attributes.height, | ||
placeholder: placeholderURL, | ||
}).returning({ id: Cover.id }); | ||
|
||
const firstCoverId = coverId[0]?.id ?? -1; | ||
|
||
const insertData = { | ||
type: type, | ||
title: data.title, | ||
rating: data.rating, | ||
cover: firstCoverId, | ||
author: author, | ||
finishedDate: data.finishedDate === "N/A" ? null : data.finishedDate.getTime(), | ||
platform: entry.data.type === "book" || entry.data.type === "game" ? entry.data.platform : null, | ||
metadata: JSON.stringify(metadata), | ||
}; | ||
|
||
await db.insert(Catalogue).values(insertData).returning({ id: Catalogue.id }); | ||
} | ||
|
||
async function getCoverAndPlaceholder(cover: ImageMetadata) { | ||
return await Promise.all([ | ||
getImage({ src: cover, width: 240 }), | ||
(async () => { | ||
const imageService = (await getConfiguredImageService()) as LocalImageServiceWithPlaceholder; | ||
const placeholderURL = await imageService.generatePlaceholder( | ||
cover.src, | ||
cover.width, | ||
cover.height, | ||
); | ||
return placeholderURL; | ||
})(), | ||
]); | ||
} | ||
|
||
// TODO: Type this properly | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function getAuthorFromEntryMetadata(type: CatalogueType, metadata: any) { | ||
switch (type) { | ||
case "game": | ||
return metadata.companies[0].name; | ||
case "book": | ||
return metadata.authors[0] ?? "Unknown"; | ||
case "movie": | ||
case "show": | ||
return metadata.companies[0]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.