Skip to content

Commit

Permalink
feat: use astro:db instead of kysely + better-sqlite3 (#20)
Browse files Browse the repository at this point in the history
* feat: use astro:db instead of kysely + better-sqlite3

* chore: lockfile
  • Loading branch information
Princesseuh committed May 1, 2024
1 parent 9209e72 commit af54d1e
Show file tree
Hide file tree
Showing 10 changed files with 608 additions and 710 deletions.
1 change: 0 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ module.exports = {
"plugin:@typescript-eslint/strict-type-checked",
"plugin:@typescript-eslint/stylistic-type-checked",
"plugin:astro/recommended",
"prettier",
],
parserOptions: {
project: true,
Expand Down
4 changes: 2 additions & 2 deletions api/catalogue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ pub async fn handler(_req: Request) -> Result<Response<Body>, Error> {
let mut next_statement = NextStatement::Where;

let mut statement = String::from(
"SELECT a.*, c.src, c.placeholder FROM catalogue a
INNER JOIN cover c
"SELECT a.*, c.src, c.placeholder FROM Catalogue a
INNER JOIN Cover c
ON c.id = a.cover",
);

Expand Down
16 changes: 13 additions & 3 deletions astro.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import markdoc from "@astrojs/markdoc";
import tailwind from "@astrojs/tailwind";
import expressiveCode from "astro-expressive-code";
import { defineConfig } from "astro/config";
import { cp, rename } from "fs/promises";

import markdoc from "@astrojs/markdoc";
import { rename } from "fs/promises";
import db from "@astrojs/db";

// https://astro.build/config
export default defineConfig({
Expand Down Expand Up @@ -32,7 +33,6 @@ export default defineConfig({
hooks: {
"astro:build:done": async (options) => {
const rssPages = options.pages.filter((page) => page.pathname.includes("rss"));

for (const { pathname: rssPage } of rssPages) {
await rename(
new URL(`${rssPage}index.html`, options.dir),
Expand All @@ -42,5 +42,15 @@ export default defineConfig({
},
},
},
db(),
// Copy the content.db file to the api folder so the serverless function can access it
{
name: "copy-db-api",
hooks: {
"astro:build:generated": async () => {
await cp(new URL(".astro/content.db", import.meta.url), new URL("api/cataloguedb.db", import.meta.url))
},
},
},
],
});
33 changes: 33 additions & 0 deletions db/config.ts
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
}
});
81 changes: 81 additions & 0 deletions db/seed.ts
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];
}
}
18 changes: 7 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
"devDependencies": {
"@astrojs/tailwind": "^5.1.0",
"@types/eslint": "^8.56.10",
"@types/markdown-it": "^14.0.1",
"@types/node": "^20.12.7",
"@types/prettier": "^2.7.3",
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1",
"astro": "^4.7.0",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"astro": "4.7.0",
"eslint": "^8.57.0",
"eslint-plugin-astro": "^1.0.3",
"eslint-plugin-astro": "^1.1.1",
"kleur": "^4.1.5",
"prettier": "^3.2.5",
"prettier-plugin-astro": "^0.13.0",
Expand All @@ -48,25 +47,22 @@
"dependencies": {
"@11ty/eleventy-fetch": "^4.0.1",
"@astrojs/check": "^0.5.10",
"@astrojs/db": "^0.10.6",
"@astrojs/markdoc": "^0.11.0",
"@types/better-sqlite3": "^7.6.10",
"@types/igdb-api-node": "^5.0.3",
"astro-capo": "^0.0.1",
"astro-expressive-code": "^0.35.2",
"better-sqlite3": "^9.6.0",
"dotenv": "^16.4.5",
"github-slugger": "^2.0.0",
"gray-matter": "^4.0.3",
"igdb-api-node": "^5.0.2",
"kysely": "^0.27.3",
"linkedom": "^0.16.11",
"markdown-it": "^14.1.0",
"prettier-plugin-tailwindcss": "^0.5.14",
"sharp": "^0.33.3",
"tailwindcss": "^3.4.3",
"tiny-decode": "^0.1.3",
"tsx": "^4.7.3",
"tsx": "^4.8.1",
"ultrahtml": "^1.5.3",
"vercel": "^34.1.3"
"vercel": "^34.1.7"
}
}

0 comments on commit af54d1e

Please sign in to comment.